-
Notifications
You must be signed in to change notification settings - Fork 30
Expand file tree
/
Copy pathinit.go
More file actions
150 lines (127 loc) · 3.21 KB
/
init.go
File metadata and controls
150 lines (127 loc) · 3.21 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
package main
import (
"encoding/hex"
"log"
"strconv"
"unsafe"
"golang.org/x/sys/windows"
"golang.org/x/sys/windows/registry"
)
// https://www.digitalocean.com/community/tutorials/using-ldflags-to-set-version-information-for-go-applications
var Version = "development"
type Device struct {
Idata DevInfoData
reg registry.Key
IrqPolicy int32
DeviceDesc string
DeviceIDs []string
DevObjName string
Driver string
LocationInformation string
FriendlyName string
Class string
IRQLanes []string
// AffinityPolicy
DevicePolicy uint32
DevicePriority uint32
AssignmentSetOverride Bits
// MessageSignaledInterruptProperties
MsiSupported uint32
MessageNumberLimit uint32
MaxMSILimit uint32
InterruptTypeMap Bits
}
func (d *Device) getInstanceID() (string, error) {
n := uint32(0)
setupDiGetDeviceInstanceId(handle, &d.Idata, nil, 0, &n)
buff := make([]uint16, n)
if err := setupDiGetDeviceInstanceId(handle, &d.Idata, unsafe.Pointer(&buff[0]), uint32(len(buff)), &n); err != nil {
return "", err
}
return windows.UTF16ToString(buff), nil
}
const (
// https://docs.microsoft.com/en-us/windows-hardware/drivers/kernel/interrupt-affinity-and-priority
IrqPolicyMachineDefault = iota // 0
IrqPolicyAllCloseProcessors // 1
IrqPolicyOneCloseProcessor // 2
IrqPolicyAllProcessorsInMachine // 3
IrqPolicySpecifiedProcessors // 4
IrqPolicySpreadMessagesAcrossAllProcessors // 5
)
const (
MSI_Off uint32 = iota
MSI_On
MSI_Tristate
MSI_Invalid
)
type Bits uint64
var CPUMap map[Bits]string
var CPUBits []Bits
var InterruptTypeMap = map[Bits]string{
0: "unknown",
1: "LineBased",
2: "Msi",
4: "MsiX",
}
var sysInfo SystemInfo
var handle DevInfo
const ZeroBit = Bits(0)
func init() {
log.SetFlags(log.LstdFlags | log.Lshortfile)
sysInfo = GetSystemInfo()
CPUMap = make(map[Bits]string, sysInfo.NumberOfProcessors)
var index Bits = 1
for i := 0; i < int(sysInfo.NumberOfProcessors); i++ {
indexString := strconv.Itoa(i)
CPUMap[index] = indexString
CPUBits = append(CPUBits, index)
index *= 2
}
}
func Set(b, flag Bits) Bits { return b | flag }
func Clear(b, flag Bits) Bits { return b &^ flag }
func Toggle(b, flag Bits) Bits { return b ^ flag }
func Has(b, flag Bits) bool { return b&flag != 0 }
// https://gist.github.com/chiro-hiro/2674626cebbcb5a676355b7aaac4972d
func i64tob(val uint64) []byte {
r := make([]byte, 8)
for i := range uint64(8) {
r[i] = byte((val >> (i * 8)) & 0xff)
}
return r
}
func btoi64(val []byte) uint64 {
r := uint64(0)
for i := range uint64(8) {
r |= uint64(val[i]) << (8 * i)
}
return r
}
func btoi32(val []byte) uint32 {
r := uint32(0)
for i := range uint32(4) {
r |= uint32(val[i]) << (8 * i)
}
return r
}
func btoi16(val []byte) uint16 {
r := uint16(0)
for i := range uint16(2) {
r |= uint16(val[i]) << (8 * i)
}
return r
}
func ToLittleEndian(v uint64) string {
if v == 0 {
return "00"
}
var b [8]byte
i := 0
for v > 0 {
b[i] = byte(v & 0xff)
v >>= 8
i++
}
return hex.EncodeToString(b[:i])
}