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