jobobject.go 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  1. //go:build windows
  2. package winapi
  3. import (
  4. "unsafe"
  5. "golang.org/x/sys/windows"
  6. )
  7. // Messages that can be received from an assigned io completion port.
  8. // https://docs.microsoft.com/en-us/windows/win32/api/winnt/ns-winnt-jobobject_associate_completion_port
  9. const (
  10. JOB_OBJECT_MSG_END_OF_JOB_TIME uint32 = 1
  11. JOB_OBJECT_MSG_END_OF_PROCESS_TIME uint32 = 2
  12. JOB_OBJECT_MSG_ACTIVE_PROCESS_LIMIT uint32 = 3
  13. JOB_OBJECT_MSG_ACTIVE_PROCESS_ZERO uint32 = 4
  14. JOB_OBJECT_MSG_NEW_PROCESS uint32 = 6
  15. JOB_OBJECT_MSG_EXIT_PROCESS uint32 = 7
  16. JOB_OBJECT_MSG_ABNORMAL_EXIT_PROCESS uint32 = 8
  17. JOB_OBJECT_MSG_PROCESS_MEMORY_LIMIT uint32 = 9
  18. JOB_OBJECT_MSG_JOB_MEMORY_LIMIT uint32 = 10
  19. JOB_OBJECT_MSG_NOTIFICATION_LIMIT uint32 = 11
  20. )
  21. // Access rights for creating or opening job objects.
  22. //
  23. // https://docs.microsoft.com/en-us/windows/win32/procthread/job-object-security-and-access-rights
  24. const (
  25. JOB_OBJECT_QUERY = 0x0004
  26. JOB_OBJECT_ALL_ACCESS = 0x1F001F
  27. )
  28. // IO limit flags
  29. //
  30. // https://docs.microsoft.com/en-us/windows/win32/api/jobapi2/ns-jobapi2-jobobject_io_rate_control_information
  31. const JOB_OBJECT_IO_RATE_CONTROL_ENABLE = 0x1
  32. const JOBOBJECT_IO_ATTRIBUTION_CONTROL_ENABLE uint32 = 0x1
  33. // https://docs.microsoft.com/en-us/windows/win32/api/winnt/ns-winnt-jobobject_cpu_rate_control_information
  34. const (
  35. JOB_OBJECT_CPU_RATE_CONTROL_ENABLE uint32 = 1 << iota
  36. JOB_OBJECT_CPU_RATE_CONTROL_WEIGHT_BASED
  37. JOB_OBJECT_CPU_RATE_CONTROL_HARD_CAP
  38. JOB_OBJECT_CPU_RATE_CONTROL_NOTIFY
  39. JOB_OBJECT_CPU_RATE_CONTROL_MIN_MAX_RATE
  40. )
  41. // JobObjectInformationClass values. Used for a call to QueryInformationJobObject
  42. //
  43. // https://docs.microsoft.com/en-us/windows/win32/api/jobapi2/nf-jobapi2-queryinformationjobobject
  44. const (
  45. JobObjectBasicAccountingInformation uint32 = 1
  46. JobObjectBasicProcessIdList uint32 = 3
  47. JobObjectBasicAndIoAccountingInformation uint32 = 8
  48. JobObjectLimitViolationInformation uint32 = 13
  49. JobObjectMemoryUsageInformation uint32 = 28
  50. JobObjectNotificationLimitInformation2 uint32 = 33
  51. JobObjectCreateSilo uint32 = 35
  52. JobObjectSiloBasicInformation uint32 = 36
  53. JobObjectIoAttribution uint32 = 42
  54. )
  55. // https://docs.microsoft.com/en-us/windows/win32/api/winnt/ns-winnt-jobobject_basic_limit_information
  56. type JOBOBJECT_BASIC_LIMIT_INFORMATION struct {
  57. PerProcessUserTimeLimit int64
  58. PerJobUserTimeLimit int64
  59. LimitFlags uint32
  60. MinimumWorkingSetSize uintptr
  61. MaximumWorkingSetSize uintptr
  62. ActiveProcessLimit uint32
  63. Affinity uintptr
  64. PriorityClass uint32
  65. SchedulingClass uint32
  66. }
  67. // https://docs.microsoft.com/en-us/windows/win32/api/winnt/ns-winnt-jobobject_cpu_rate_control_information
  68. type JOBOBJECT_CPU_RATE_CONTROL_INFORMATION struct {
  69. ControlFlags uint32
  70. Value uint32
  71. }
  72. // https://docs.microsoft.com/en-us/windows/win32/api/jobapi2/ns-jobapi2-jobobject_io_rate_control_information
  73. type JOBOBJECT_IO_RATE_CONTROL_INFORMATION struct {
  74. MaxIops int64
  75. MaxBandwidth int64
  76. ReservationIops int64
  77. BaseIOSize uint32
  78. VolumeName string
  79. ControlFlags uint32
  80. }
  81. // https://docs.microsoft.com/en-us/windows/win32/api/winnt/ns-winnt-jobobject_basic_process_id_list
  82. type JOBOBJECT_BASIC_PROCESS_ID_LIST struct {
  83. NumberOfAssignedProcesses uint32
  84. NumberOfProcessIdsInList uint32
  85. ProcessIdList [1]uintptr
  86. }
  87. // AllPids returns all the process Ids in the job object.
  88. func (p *JOBOBJECT_BASIC_PROCESS_ID_LIST) AllPids() []uintptr {
  89. return (*[(1 << 27) - 1]uintptr)(unsafe.Pointer(&p.ProcessIdList[0]))[:p.NumberOfProcessIdsInList:p.NumberOfProcessIdsInList]
  90. }
  91. // https://docs.microsoft.com/en-us/windows/win32/api/winnt/ns-winnt-jobobject_basic_accounting_information
  92. type JOBOBJECT_BASIC_ACCOUNTING_INFORMATION struct {
  93. TotalUserTime int64
  94. TotalKernelTime int64
  95. ThisPeriodTotalUserTime int64
  96. ThisPeriodTotalKernelTime int64
  97. TotalPageFaultCount uint32
  98. TotalProcesses uint32
  99. ActiveProcesses uint32
  100. TotalTerminateProcesses uint32
  101. }
  102. // https://docs.microsoft.com/en-us/windows/win32/api/winnt/ns-winnt-jobobject_basic_and_io_accounting_information
  103. type JOBOBJECT_BASIC_AND_IO_ACCOUNTING_INFORMATION struct {
  104. BasicInfo JOBOBJECT_BASIC_ACCOUNTING_INFORMATION
  105. IoInfo windows.IO_COUNTERS
  106. }
  107. // typedef struct _JOBOBJECT_MEMORY_USAGE_INFORMATION {
  108. // ULONG64 JobMemory;
  109. // ULONG64 PeakJobMemoryUsed;
  110. // } JOBOBJECT_MEMORY_USAGE_INFORMATION, *PJOBOBJECT_MEMORY_USAGE_INFORMATION;
  111. type JOBOBJECT_MEMORY_USAGE_INFORMATION struct {
  112. JobMemory uint64
  113. PeakJobMemoryUsed uint64
  114. }
  115. // typedef struct _JOBOBJECT_IO_ATTRIBUTION_STATS {
  116. // ULONG_PTR IoCount;
  117. // ULONGLONG TotalNonOverlappedQueueTime;
  118. // ULONGLONG TotalNonOverlappedServiceTime;
  119. // ULONGLONG TotalSize;
  120. // } JOBOBJECT_IO_ATTRIBUTION_STATS, *PJOBOBJECT_IO_ATTRIBUTION_STATS;
  121. type JOBOBJECT_IO_ATTRIBUTION_STATS struct {
  122. IoCount uintptr
  123. TotalNonOverlappedQueueTime uint64
  124. TotalNonOverlappedServiceTime uint64
  125. TotalSize uint64
  126. }
  127. // typedef struct _JOBOBJECT_IO_ATTRIBUTION_INFORMATION {
  128. // ULONG ControlFlags;
  129. // JOBOBJECT_IO_ATTRIBUTION_STATS ReadStats;
  130. // JOBOBJECT_IO_ATTRIBUTION_STATS WriteStats;
  131. // } JOBOBJECT_IO_ATTRIBUTION_INFORMATION, *PJOBOBJECT_IO_ATTRIBUTION_INFORMATION;
  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 unsafe.Pointer, 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 int32, 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. //
  178. //sys QueryIoRateControlInformationJobObject(jobHandle windows.Handle, volumeName *uint16, ioRateControlInfo **JOBOBJECT_IO_RATE_CONTROL_INFORMATION, infoBlockCount *uint32) (ret uint32, err error) = kernel32.QueryIoRateControlInformationJobObject
  179. // NTSTATUS
  180. // NtOpenJobObject (
  181. // _Out_ PHANDLE JobHandle,
  182. // _In_ ACCESS_MASK DesiredAccess,
  183. // _In_ POBJECT_ATTRIBUTES ObjectAttributes
  184. // );
  185. //
  186. //sys NtOpenJobObject(jobHandle *windows.Handle, desiredAccess uint32, objAttributes *ObjectAttributes) (status uint32) = ntdll.NtOpenJobObject
  187. // NTSTATUS
  188. // NTAPI
  189. // NtCreateJobObject (
  190. // _Out_ PHANDLE JobHandle,
  191. // _In_ ACCESS_MASK DesiredAccess,
  192. // _In_opt_ POBJECT_ATTRIBUTES ObjectAttributes
  193. // );
  194. //
  195. //sys NtCreateJobObject(jobHandle *windows.Handle, desiredAccess uint32, objAttributes *ObjectAttributes) (status uint32) = ntdll.NtCreateJobObject