jobobject.go 7.9 KB

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