jobobject.go 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  1. package winapi
  2. import (
  3. "unsafe"
  4. "golang.org/x/sys/windows"
  5. )
  6. // Messages that can be received from an assigned io completion port.
  7. // https://docs.microsoft.com/en-us/windows/win32/api/winnt/ns-winnt-jobobject_associate_completion_port
  8. const (
  9. JOB_OBJECT_MSG_END_OF_JOB_TIME uint32 = 1
  10. JOB_OBJECT_MSG_END_OF_PROCESS_TIME uint32 = 2
  11. JOB_OBJECT_MSG_ACTIVE_PROCESS_LIMIT uint32 = 3
  12. JOB_OBJECT_MSG_ACTIVE_PROCESS_ZERO uint32 = 4
  13. JOB_OBJECT_MSG_NEW_PROCESS uint32 = 6
  14. JOB_OBJECT_MSG_EXIT_PROCESS uint32 = 7
  15. JOB_OBJECT_MSG_ABNORMAL_EXIT_PROCESS uint32 = 8
  16. JOB_OBJECT_MSG_PROCESS_MEMORY_LIMIT uint32 = 9
  17. JOB_OBJECT_MSG_JOB_MEMORY_LIMIT uint32 = 10
  18. JOB_OBJECT_MSG_NOTIFICATION_LIMIT uint32 = 11
  19. )
  20. // Access rights for creating or opening job objects.
  21. //
  22. // https://docs.microsoft.com/en-us/windows/win32/procthread/job-object-security-and-access-rights
  23. const (
  24. JOB_OBJECT_QUERY = 0x0004
  25. JOB_OBJECT_ALL_ACCESS = 0x1F001F
  26. )
  27. // IO limit flags
  28. //
  29. // https://docs.microsoft.com/en-us/windows/win32/api/jobapi2/ns-jobapi2-jobobject_io_rate_control_information
  30. const JOB_OBJECT_IO_RATE_CONTROL_ENABLE = 0x1
  31. const JOBOBJECT_IO_ATTRIBUTION_CONTROL_ENABLE uint32 = 0x1
  32. // https://docs.microsoft.com/en-us/windows/win32/api/winnt/ns-winnt-jobobject_cpu_rate_control_information
  33. const (
  34. JOB_OBJECT_CPU_RATE_CONTROL_ENABLE uint32 = 1 << iota
  35. JOB_OBJECT_CPU_RATE_CONTROL_WEIGHT_BASED
  36. JOB_OBJECT_CPU_RATE_CONTROL_HARD_CAP
  37. JOB_OBJECT_CPU_RATE_CONTROL_NOTIFY
  38. JOB_OBJECT_CPU_RATE_CONTROL_MIN_MAX_RATE
  39. )
  40. // JobObjectInformationClass values. Used for a call to QueryInformationJobObject
  41. //
  42. // https://docs.microsoft.com/en-us/windows/win32/api/jobapi2/nf-jobapi2-queryinformationjobobject
  43. const (
  44. JobObjectBasicAccountingInformation uint32 = 1
  45. JobObjectBasicProcessIdList uint32 = 3
  46. JobObjectBasicAndIoAccountingInformation uint32 = 8
  47. JobObjectLimitViolationInformation uint32 = 13
  48. JobObjectMemoryUsageInformation uint32 = 28
  49. JobObjectNotificationLimitInformation2 uint32 = 33
  50. JobObjectIoAttribution uint32 = 42
  51. )
  52. // https://docs.microsoft.com/en-us/windows/win32/api/winnt/ns-winnt-jobobject_basic_limit_information
  53. type JOBOBJECT_BASIC_LIMIT_INFORMATION struct {
  54. PerProcessUserTimeLimit int64
  55. PerJobUserTimeLimit int64
  56. LimitFlags uint32
  57. MinimumWorkingSetSize uintptr
  58. MaximumWorkingSetSize uintptr
  59. ActiveProcessLimit uint32
  60. Affinity uintptr
  61. PriorityClass uint32
  62. SchedulingClass uint32
  63. }
  64. // https://docs.microsoft.com/en-us/windows/win32/api/winnt/ns-winnt-jobobject_cpu_rate_control_information
  65. type JOBOBJECT_CPU_RATE_CONTROL_INFORMATION struct {
  66. ControlFlags uint32
  67. Value uint32
  68. }
  69. // https://docs.microsoft.com/en-us/windows/win32/api/jobapi2/ns-jobapi2-jobobject_io_rate_control_information
  70. type JOBOBJECT_IO_RATE_CONTROL_INFORMATION struct {
  71. MaxIops int64
  72. MaxBandwidth int64
  73. ReservationIops int64
  74. BaseIOSize uint32
  75. VolumeName string
  76. ControlFlags uint32
  77. }
  78. // https://docs.microsoft.com/en-us/windows/win32/api/winnt/ns-winnt-jobobject_basic_process_id_list
  79. type JOBOBJECT_BASIC_PROCESS_ID_LIST struct {
  80. NumberOfAssignedProcesses uint32
  81. NumberOfProcessIdsInList uint32
  82. ProcessIdList [1]uintptr
  83. }
  84. // AllPids returns all the process Ids in the job object.
  85. func (p *JOBOBJECT_BASIC_PROCESS_ID_LIST) AllPids() []uintptr {
  86. return (*[(1 << 27) - 1]uintptr)(unsafe.Pointer(&p.ProcessIdList[0]))[:p.NumberOfProcessIdsInList:p.NumberOfProcessIdsInList]
  87. }
  88. // https://docs.microsoft.com/en-us/windows/win32/api/winnt/ns-winnt-jobobject_basic_accounting_information
  89. type JOBOBJECT_BASIC_ACCOUNTING_INFORMATION struct {
  90. TotalUserTime int64
  91. TotalKernelTime int64
  92. ThisPeriodTotalUserTime int64
  93. ThisPeriodTotalKernelTime int64
  94. TotalPageFaultCount uint32
  95. TotalProcesses uint32
  96. ActiveProcesses uint32
  97. TotalTerminateProcesses uint32
  98. }
  99. //https://docs.microsoft.com/en-us/windows/win32/api/winnt/ns-winnt-jobobject_basic_and_io_accounting_information
  100. type JOBOBJECT_BASIC_AND_IO_ACCOUNTING_INFORMATION struct {
  101. BasicInfo JOBOBJECT_BASIC_ACCOUNTING_INFORMATION
  102. IoInfo windows.IO_COUNTERS
  103. }
  104. // typedef struct _JOBOBJECT_MEMORY_USAGE_INFORMATION {
  105. // ULONG64 JobMemory;
  106. // ULONG64 PeakJobMemoryUsed;
  107. // } JOBOBJECT_MEMORY_USAGE_INFORMATION, *PJOBOBJECT_MEMORY_USAGE_INFORMATION;
  108. //
  109. type JOBOBJECT_MEMORY_USAGE_INFORMATION struct {
  110. JobMemory uint64
  111. PeakJobMemoryUsed uint64
  112. }
  113. // typedef struct _JOBOBJECT_IO_ATTRIBUTION_STATS {
  114. // ULONG_PTR IoCount;
  115. // ULONGLONG TotalNonOverlappedQueueTime;
  116. // ULONGLONG TotalNonOverlappedServiceTime;
  117. // ULONGLONG TotalSize;
  118. // } JOBOBJECT_IO_ATTRIBUTION_STATS, *PJOBOBJECT_IO_ATTRIBUTION_STATS;
  119. //
  120. type JOBOBJECT_IO_ATTRIBUTION_STATS struct {
  121. IoCount uintptr
  122. TotalNonOverlappedQueueTime uint64
  123. TotalNonOverlappedServiceTime uint64
  124. TotalSize uint64
  125. }
  126. // typedef struct _JOBOBJECT_IO_ATTRIBUTION_INFORMATION {
  127. // ULONG ControlFlags;
  128. // JOBOBJECT_IO_ATTRIBUTION_STATS ReadStats;
  129. // JOBOBJECT_IO_ATTRIBUTION_STATS WriteStats;
  130. // } JOBOBJECT_IO_ATTRIBUTION_INFORMATION, *PJOBOBJECT_IO_ATTRIBUTION_INFORMATION;
  131. //
  132. type JOBOBJECT_IO_ATTRIBUTION_INFORMATION struct {
  133. ControlFlags uint32
  134. ReadStats JOBOBJECT_IO_ATTRIBUTION_STATS
  135. WriteStats JOBOBJECT_IO_ATTRIBUTION_STATS
  136. }
  137. // https://docs.microsoft.com/en-us/windows/win32/api/winnt/ns-winnt-jobobject_associate_completion_port
  138. type JOBOBJECT_ASSOCIATE_COMPLETION_PORT struct {
  139. CompletionKey windows.Handle
  140. CompletionPort windows.Handle
  141. }
  142. // BOOL IsProcessInJob(
  143. // HANDLE ProcessHandle,
  144. // HANDLE JobHandle,
  145. // PBOOL Result
  146. // );
  147. //
  148. //sys IsProcessInJob(procHandle windows.Handle, jobHandle windows.Handle, result *int32) (err error) = kernel32.IsProcessInJob
  149. // BOOL QueryInformationJobObject(
  150. // HANDLE hJob,
  151. // JOBOBJECTINFOCLASS JobObjectInformationClass,
  152. // LPVOID lpJobObjectInformation,
  153. // DWORD cbJobObjectInformationLength,
  154. // LPDWORD lpReturnLength
  155. // );
  156. //
  157. //sys QueryInformationJobObject(jobHandle windows.Handle, infoClass uint32, jobObjectInfo uintptr, jobObjectInformationLength uint32, lpReturnLength *uint32) (err error) = kernel32.QueryInformationJobObject
  158. // HANDLE OpenJobObjectW(
  159. // DWORD dwDesiredAccess,
  160. // BOOL bInheritHandle,
  161. // LPCWSTR lpName
  162. // );
  163. //
  164. //sys OpenJobObject(desiredAccess uint32, inheritHandle bool, lpName *uint16) (handle windows.Handle, err error) = kernel32.OpenJobObjectW
  165. // DWORD SetIoRateControlInformationJobObject(
  166. // HANDLE hJob,
  167. // JOBOBJECT_IO_RATE_CONTROL_INFORMATION *IoRateControlInfo
  168. // );
  169. //
  170. //sys SetIoRateControlInformationJobObject(jobHandle windows.Handle, ioRateControlInfo *JOBOBJECT_IO_RATE_CONTROL_INFORMATION) (ret uint32, err error) = kernel32.SetIoRateControlInformationJobObject
  171. // DWORD QueryIoRateControlInformationJobObject(
  172. // HANDLE hJob,
  173. // PCWSTR VolumeName,
  174. // JOBOBJECT_IO_RATE_CONTROL_INFORMATION **InfoBlocks,
  175. // ULONG *InfoBlockCount
  176. // );
  177. //sys QueryIoRateControlInformationJobObject(jobHandle windows.Handle, volumeName *uint16, ioRateControlInfo **JOBOBJECT_IO_RATE_CONTROL_INFORMATION, infoBlockCount *uint32) (ret uint32, err error) = kernel32.QueryIoRateControlInformationJobObject
  178. // NTSTATUS
  179. // NtOpenJobObject (
  180. // _Out_ PHANDLE JobHandle,
  181. // _In_ ACCESS_MASK DesiredAccess,
  182. // _In_ POBJECT_ATTRIBUTES ObjectAttributes
  183. // );
  184. //sys NtOpenJobObject(jobHandle *windows.Handle, desiredAccess uint32, objAttributes *ObjectAttributes) (status uint32) = ntdll.NtOpenJobObject
  185. // NTSTATUS
  186. // NTAPI
  187. // NtCreateJobObject (
  188. // _Out_ PHANDLE JobHandle,
  189. // _In_ ACCESS_MASK DesiredAccess,
  190. // _In_opt_ POBJECT_ATTRIBUTES ObjectAttributes
  191. // );
  192. //sys NtCreateJobObject(jobHandle *windows.Handle, desiredAccess uint32, objAttributes *ObjectAttributes) (status uint32) = ntdll.NtCreateJobObject