| 1 |
|
| 2 |
|
| 3 |
package windows |
| 4 |
|
| 5 |
import ( |
| 6 |
"bytes" |
| 7 |
"encoding/binary" |
| 8 |
"io" |
| 9 |
"runtime" |
| 10 |
"syscall" |
| 11 |
"time" |
| 12 |
"unsafe" |
| 13 |
|
| 14 |
"github.com/pkg/errors" |
| 15 |
) |
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
const sizeofSystemProcessorPerformanceInformation = 48 |
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
type ProcessBasicInformation struct { |
| 25 |
ExitStatus uint |
| 26 |
PebBaseAddress uintptr |
| 27 |
AffinityMask uint |
| 28 |
BasePriority uint |
| 29 |
UniqueProcessID uint |
| 30 |
InheritedFromUniqueProcessID uint |
| 31 |
} |
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
func NtQueryProcessBasicInformation(handle syscall.Handle) (ProcessBasicInformation, error) { |
| 38 |
var processBasicInfo ProcessBasicInformation |
| 39 |
processBasicInfoPtr := (*byte)(unsafe.Pointer(&processBasicInfo)) |
| 40 |
size := uint32(unsafe.Sizeof(processBasicInfo)) |
| 41 |
ntStatus, _ := _NtQueryInformationProcess(handle, 0, processBasicInfoPtr, size, nil) |
| 42 |
if ntStatus != 0 { |
| 43 |
return ProcessBasicInformation{}, errors.Errorf("NtQueryInformationProcess failed, NTSTATUS=0x%X", ntStatus) |
| 44 |
} |
| 45 |
|
| 46 |
return processBasicInfo, nil |
| 47 |
} |
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
type SystemProcessorPerformanceInformation struct { |
| 52 |
IdleTime time.Duration |
| 53 |
KernelTime time.Duration |
| 54 |
UserTime time.Duration |
| 55 |
} |
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
type _SYSTEM_PROCESSOR_PERFORMANCE_INFORMATION struct { |
| 63 |
IdleTime int64 |
| 64 |
KernelTime int64 |
| 65 |
UserTime int64 |
| 66 |
Reserved1 [2]int64 |
| 67 |
Reserved2 uint32 |
| 68 |
} |
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
func NtQuerySystemProcessorPerformanceInformation() ([]SystemProcessorPerformanceInformation, error) { |
| 75 |
|
| 76 |
|
| 77 |
const STATUS_SUCCESS = 0 |
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
const systemProcessorPerformanceInformation = 8 |
| 82 |
|
| 83 |
|
| 84 |
b := make([]byte, runtime.NumCPU()*sizeofSystemProcessorPerformanceInformation) |
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
var returnLength uint32 |
| 89 |
ntStatus, _ := _NtQuerySystemInformation(systemProcessorPerformanceInformation, &b[0], uint32(len(b)), &returnLength) |
| 90 |
if ntStatus != STATUS_SUCCESS { |
| 91 |
return nil, errors.Errorf("NtQuerySystemInformation failed, NTSTATUS=0x%X, bufLength=%v, returnLength=%v", ntStatus, len(b), returnLength) |
| 92 |
} |
| 93 |
|
| 94 |
return readSystemProcessorPerformanceInformationBuffer(b) |
| 95 |
} |
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
func readSystemProcessorPerformanceInformationBuffer(b []byte) ([]SystemProcessorPerformanceInformation, error) { |
| 102 |
n := len(b) / sizeofSystemProcessorPerformanceInformation |
| 103 |
r := bytes.NewReader(b) |
| 104 |
|
| 105 |
rtn := make([]SystemProcessorPerformanceInformation, 0, n) |
| 106 |
for i := 0; i < n; i++ { |
| 107 |
_, err := r.Seek(int64(i*sizeofSystemProcessorPerformanceInformation), io.SeekStart) |
| 108 |
if err != nil { |
| 109 |
return nil, errors.Wrapf(err, "failed to seek to cpuN=%v in buffer", i) |
| 110 |
} |
| 111 |
|
| 112 |
times := make([]uint64, 3) |
| 113 |
for j := range times { |
| 114 |
err := binary.Read(r, binary.LittleEndian, ×[j]) |
| 115 |
if err != nil { |
| 116 |
return nil, errors.Wrapf(err, "failed reading cpu times for cpuN=%v", i) |
| 117 |
} |
| 118 |
} |
| 119 |
|
| 120 |
idleTime := time.Duration(times[0] * 100) |
| 121 |
kernelTime := time.Duration(times[1] * 100) |
| 122 |
userTime := time.Duration(times[2] * 100) |
| 123 |
|
| 124 |
rtn = append(rtn, SystemProcessorPerformanceInformation{ |
| 125 |
IdleTime: idleTime, |
| 126 |
KernelTime: kernelTime - idleTime, |
| 127 |
UserTime: userTime, |
| 128 |
}) |
| 129 |
} |
| 130 |
|
| 131 |
return rtn, nil |
| 132 |
} |