Skip to content

Commit edba8ea

Browse files
committed
[darwin]: wrap library functions as struct methods
1 parent 4870278 commit edba8ea

File tree

8 files changed

+421
-317
lines changed

8 files changed

+421
-317
lines changed

cpu/cpu_darwin.go

Lines changed: 10 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -61,17 +61,17 @@ func Times(percpu bool) ([]TimesStat, error) {
6161
}
6262

6363
func TimesWithContext(_ context.Context, percpu bool) ([]TimesStat, error) {
64-
lib, err := common.NewLibrary(common.System)
64+
sys, err := common.NewSystemLib()
6565
if err != nil {
6666
return nil, err
6767
}
68-
defer lib.Close()
68+
defer sys.Close()
6969

7070
if percpu {
71-
return perCPUTimes(lib)
71+
return perCPUTimes(sys)
7272
}
7373

74-
return allCPUTimes(lib)
74+
return allCPUTimes(sys)
7575
}
7676

7777
// Returns only one CPUInfoStat on FreeBSD
@@ -138,16 +138,12 @@ func CountsWithContext(_ context.Context, logical bool) (int, error) {
138138
return int(count), nil
139139
}
140140

141-
func perCPUTimes(machLib *common.Library) ([]TimesStat, error) {
142-
machHostSelf := common.GetFunc[common.MachHostSelfFunc](machLib, common.MachHostSelfSym)
143-
machTaskSelf := common.GetFunc[common.MachTaskSelfFunc](machLib, common.MachTaskSelfSym)
144-
hostProcessorInfo := common.GetFunc[common.HostProcessorInfoFunc](machLib, common.HostProcessorInfoSym)
145-
vmDeallocate := common.GetFunc[common.VMDeallocateFunc](machLib, common.VMDeallocateSym)
146-
141+
func perCPUTimes(sys *common.SystemLib) ([]TimesStat, error) {
147142
var count, ncpu uint32
148143
var cpuload *hostCpuLoadInfoData
149144

150-
status := hostProcessorInfo(machHostSelf(), processorCpuLoadInfo, &ncpu, uintptr(unsafe.Pointer(&cpuload)), &count)
145+
status := sys.HostProcessorInfo(sys.MachHostSelf(), processorCpuLoadInfo,
146+
&ncpu, uintptr(unsafe.Pointer(&cpuload)), &count)
151147

152148
if status != common.KERN_SUCCESS {
153149
return nil, fmt.Errorf("host_processor_info error=%d", status)
@@ -157,7 +153,7 @@ func perCPUTimes(machLib *common.Library) ([]TimesStat, error) {
157153
return nil, errors.New("host_processor_info returned nil cpuload")
158154
}
159155

160-
defer vmDeallocate(machTaskSelf(), uintptr(unsafe.Pointer(cpuload)), uintptr(ncpu))
156+
defer sys.VMDeallocate(sys.MachTaskSelf(), uintptr(unsafe.Pointer(cpuload)), uintptr(ncpu))
161157

162158
ret := []TimesStat{}
163159
loads := unsafe.Slice(cpuload, ncpu)
@@ -170,21 +166,17 @@ func perCPUTimes(machLib *common.Library) ([]TimesStat, error) {
170166
Nice: float64(loads[i].cpuTicks[cpuStateNice]) / ClocksPerSec,
171167
Idle: float64(loads[i].cpuTicks[cpuStateIdle]) / ClocksPerSec,
172168
}
173-
174169
ret = append(ret, c)
175170
}
176171

177172
return ret, nil
178173
}
179174

180-
func allCPUTimes(machLib *common.Library) ([]TimesStat, error) {
181-
machHostSelf := common.GetFunc[common.MachHostSelfFunc](machLib, common.MachHostSelfSym)
182-
hostStatistics := common.GetFunc[common.HostStatisticsFunc](machLib, common.HostStatisticsSym)
183-
175+
func allCPUTimes(sys *common.SystemLib) ([]TimesStat, error) {
184176
var cpuload hostCpuLoadInfoData
185177
count := uint32(cpuStateMax)
186178

187-
status := hostStatistics(machHostSelf(), common.HOST_CPU_LOAD_INFO,
179+
status := sys.HostStatistics(sys.MachHostSelf(), common.HOST_CPU_LOAD_INFO,
188180
uintptr(unsafe.Pointer(&cpuload)), &count)
189181

190182
if status != common.KERN_SUCCESS {

cpu/cpu_darwin_arm64.go

Lines changed: 17 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -13,68 +13,56 @@ import (
1313

1414
// https://github.com/shoenig/go-m1cpu/blob/v0.1.6/cpu.go
1515
func getFrequency() (float64, error) {
16-
ioKit, err := common.NewLibrary(common.IOKit)
16+
iokit, err := common.NewIOKitLib()
1717
if err != nil {
1818
return 0, err
1919
}
20-
defer ioKit.Close()
20+
defer iokit.Close()
2121

22-
coreFoundation, err := common.NewLibrary(common.CoreFoundation)
22+
corefoundation, err := common.NewCoreFoundationLib()
2323
if err != nil {
2424
return 0, err
2525
}
26-
defer coreFoundation.Close()
26+
defer corefoundation.Close()
2727

28-
ioServiceMatching := common.GetFunc[common.IOServiceMatchingFunc](ioKit, common.IOServiceMatchingSym)
29-
ioServiceGetMatchingServices := common.GetFunc[common.IOServiceGetMatchingServicesFunc](ioKit, common.IOServiceGetMatchingServicesSym)
30-
ioIteratorNext := common.GetFunc[common.IOIteratorNextFunc](ioKit, common.IOIteratorNextSym)
31-
ioRegistryEntryGetName := common.GetFunc[common.IORegistryEntryGetNameFunc](ioKit, common.IORegistryEntryGetNameSym)
32-
ioRegistryEntryCreateCFProperty := common.GetFunc[common.IORegistryEntryCreateCFPropertyFunc](ioKit, common.IORegistryEntryCreateCFPropertySym)
33-
ioObjectRelease := common.GetFunc[common.IOObjectReleaseFunc](ioKit, common.IOObjectReleaseSym)
34-
35-
cfStringCreateWithCString := common.GetFunc[common.CFStringCreateWithCStringFunc](coreFoundation, common.CFStringCreateWithCStringSym)
36-
cfDataGetLength := common.GetFunc[common.CFDataGetLengthFunc](coreFoundation, common.CFDataGetLengthSym)
37-
cfDataGetBytePtr := common.GetFunc[common.CFDataGetBytePtrFunc](coreFoundation, common.CFDataGetBytePtrSym)
38-
cfRelease := common.GetFunc[common.CFReleaseFunc](coreFoundation, common.CFReleaseSym)
39-
40-
matching := ioServiceMatching("AppleARMIODevice")
28+
matching := iokit.IOServiceMatching("AppleARMIODevice")
4129

4230
var iterator uint32
43-
if status := ioServiceGetMatchingServices(common.KIOMainPortDefault, uintptr(matching), &iterator); status != common.KERN_SUCCESS {
31+
if status := iokit.IOServiceGetMatchingServices(common.KIOMainPortDefault, uintptr(matching), &iterator); status != common.KERN_SUCCESS {
4432
return 0.0, fmt.Errorf("IOServiceGetMatchingServices error=%d", status)
4533
}
46-
defer ioObjectRelease(iterator)
34+
defer iokit.IOObjectRelease(iterator)
4735

48-
pCorekey := cfStringCreateWithCString(common.KCFAllocatorDefault, "voltage-states5-sram", common.KCFStringEncodingUTF8)
49-
defer cfRelease(uintptr(pCorekey))
36+
pCorekey := corefoundation.CFStringCreateWithCString(common.KCFAllocatorDefault, "voltage-states5-sram", common.KCFStringEncodingUTF8)
37+
defer corefoundation.CFRelease(uintptr(pCorekey))
5038

5139
var pCoreHz uint32
5240
for {
53-
service := ioIteratorNext(iterator)
41+
service := iokit.IOIteratorNext(iterator)
5442
if service <= 0 {
5543
break
5644
}
5745

5846
buf := common.NewCStr(512)
59-
ioRegistryEntryGetName(service, buf)
47+
iokit.IORegistryEntryGetName(service, buf)
6048

6149
if buf.GoString() == "pmgr" {
62-
pCoreRef := ioRegistryEntryCreateCFProperty(service, uintptr(pCorekey), common.KCFAllocatorDefault, common.KNilOptions)
63-
length := cfDataGetLength(uintptr(pCoreRef))
64-
data := cfDataGetBytePtr(uintptr(pCoreRef))
50+
pCoreRef := iokit.IORegistryEntryCreateCFProperty(service, uintptr(pCorekey), common.KCFAllocatorDefault, common.KNilOptions)
51+
length := corefoundation.CFDataGetLength(uintptr(pCoreRef))
52+
data := corefoundation.CFDataGetBytePtr(uintptr(pCoreRef))
6553

6654
// composite uint32 from the byte array
6755
buf := unsafe.Slice((*byte)(data), length)
6856

6957
// combine the bytes into a uint32 value
7058
b := buf[length-8 : length-4]
7159
pCoreHz = binary.LittleEndian.Uint32(b)
72-
cfRelease(uintptr(pCoreRef))
73-
ioObjectRelease(service)
60+
corefoundation.CFRelease(uintptr(pCoreRef))
61+
iokit.IOObjectRelease(service)
7462
break
7563
}
7664

77-
ioObjectRelease(service)
65+
iokit.IOObjectRelease(service)
7866
}
7967

8068
return float64(pCoreHz / 1_000_000), nil

disk/disk_darwin.go

Lines changed: 38 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -158,58 +158,41 @@ func LabelWithContext(_ context.Context, _ string) (string, error) {
158158
}
159159

160160
func IOCountersWithContext(_ context.Context, names ...string) (map[string]IOCountersStat, error) {
161-
ioKit, err := common.NewLibrary(common.IOKit)
161+
iokit, err := common.NewIOKitLib()
162162
if err != nil {
163163
return nil, err
164164
}
165-
defer ioKit.Close()
165+
defer iokit.Close()
166166

167-
coreFoundation, err := common.NewLibrary(common.CoreFoundation)
167+
corefoundation, err := common.NewCoreFoundationLib()
168168
if err != nil {
169169
return nil, err
170170
}
171-
defer coreFoundation.Close()
171+
defer corefoundation.Close()
172172

173-
ioServiceMatching := common.GetFunc[common.IOServiceMatchingFunc](ioKit, common.IOServiceMatchingSym)
174-
ioServiceGetMatchingServices := common.GetFunc[common.IOServiceGetMatchingServicesFunc](ioKit, common.IOServiceGetMatchingServicesSym)
175-
ioIteratorNext := common.GetFunc[common.IOIteratorNextFunc](ioKit, common.IOIteratorNextSym)
176-
ioObjectRelease := common.GetFunc[common.IOObjectReleaseFunc](ioKit, common.IOObjectReleaseSym)
173+
match := iokit.IOServiceMatching("IOMedia")
177174

178-
cfDictionaryAddValue := common.GetFunc[common.CFDictionaryAddValueFunc](coreFoundation, common.CFDictionaryAddValueSym)
179-
cfStringCreateWithCString := common.GetFunc[common.CFStringCreateWithCStringFunc](coreFoundation, common.CFStringCreateWithCStringSym)
180-
cfRelease := common.GetFunc[common.CFReleaseFunc](coreFoundation, common.CFReleaseSym)
175+
key := corefoundation.CFStringCreateWithCString(common.KCFAllocatorDefault, common.KIOMediaWholeKey, common.KCFStringEncodingUTF8)
176+
defer corefoundation.CFRelease(uintptr(key))
181177

182-
kCFBooleanTruePtr, _ := coreFoundation.Dlsym("kCFBooleanTrue")
183-
184-
match := ioServiceMatching("IOMedia")
185-
186-
key := cfStringCreateWithCString(common.KCFAllocatorDefault, common.KIOMediaWholeKey, common.KCFStringEncodingUTF8)
187-
defer cfRelease(uintptr(key))
178+
kCFBooleanTruePtr, _ := corefoundation.Dlsym("kCFBooleanTrue")
179+
kCFBooleanTrue := **(**uintptr)(unsafe.Pointer(&kCFBooleanTruePtr))
180+
corefoundation.CFDictionaryAddValue(uintptr(match), uintptr(key), kCFBooleanTrue)
188181

189182
var drives uint32
190-
kCFBooleanTrue := **(**uintptr)(unsafe.Pointer(&kCFBooleanTruePtr))
191-
cfDictionaryAddValue(uintptr(match), uintptr(key), kCFBooleanTrue)
192-
if status := ioServiceGetMatchingServices(common.KIOMainPortDefault, uintptr(match), &drives); status != common.KERN_SUCCESS {
183+
if status := iokit.IOServiceGetMatchingServices(common.KIOMainPortDefault, uintptr(match), &drives); status != common.KERN_SUCCESS {
193184
return nil, fmt.Errorf("IOServiceGetMatchingServices error=%d", status)
194185
}
195-
defer ioObjectRelease(drives)
186+
defer iokit.IOObjectRelease(drives)
196187

197188
ic := &ioCounters{
198-
ioKit: ioKit,
199-
coreFoundation: coreFoundation,
200-
201-
ioRegistryEntryCreateCFProperties: common.GetFunc[common.IORegistryEntryCreateCFPropertiesFunc](ioKit, common.IORegistryEntryCreateCFPropertiesSym),
202-
ioObjectRelease: ioObjectRelease,
203-
204-
cfStringCreateWithCString: cfStringCreateWithCString,
205-
cfDictionaryGetValue: common.GetFunc[common.CFDictionaryGetValueFunc](coreFoundation, common.CFDictionaryGetValueSym),
206-
cfNumberGetValue: common.GetFunc[common.CFNumberGetValueFunc](coreFoundation, common.CFNumberGetValueSym),
207-
cfRelease: cfRelease,
189+
iokit: iokit,
190+
corefoundation: corefoundation,
208191
}
209192

210193
stats := make([]IOCountersStat, 0, 16)
211194
for {
212-
d := ioIteratorNext(drives)
195+
d := iokit.IOIteratorNext(drives)
213196
if d <= 0 {
214197
break
215198
}
@@ -223,7 +206,7 @@ func IOCountersWithContext(_ context.Context, names ...string) (map[string]IOCou
223206
stats = append(stats, *stat)
224207
}
225208

226-
ioObjectRelease(d)
209+
iokit.IOObjectRelease(d)
227210
}
228211

229212
ret := make(map[string]IOCountersStat, 0)
@@ -243,9 +226,9 @@ func IOCountersWithContext(_ context.Context, names ...string) (map[string]IOCou
243226
}
244227

245228
const (
246-
kIOBSDNameKey = "BSD Name"
247-
kIOMediaSizeKey = "Size"
248-
kIOMediaPreferredBlockSizeKey = "Preferred Block Size"
229+
kIOBSDNameKey = "BSD Name"
230+
// kIOMediaSizeKey = "Size"
231+
// kIOMediaPreferredBlockSizeKey = "Preferred Block Size"
249232

250233
kIOBlockStorageDriverStatisticsKey = "Statistics"
251234
kIOBlockStorageDriverStatisticsBytesReadKey = "Bytes (Read)"
@@ -257,48 +240,34 @@ const (
257240
)
258241

259242
type ioCounters struct {
260-
ioKit *common.Library
261-
coreFoundation *common.Library
262-
263-
ioRegistryEntryCreateCFProperties common.IORegistryEntryCreateCFPropertiesFunc
264-
ioObjectRelease common.IOObjectReleaseFunc
265-
266-
cfStringCreateWithCString common.CFStringCreateWithCStringFunc
267-
cfDictionaryGetValue common.CFDictionaryGetValueFunc
268-
cfNumberGetValue common.CFNumberGetValueFunc
269-
cfRelease common.CFReleaseFunc
243+
iokit *common.IOKitLib
244+
corefoundation *common.CoreFoundationLib
270245
}
271246

272247
func (i *ioCounters) getDriveStat(d uint32) (*IOCountersStat, error) {
273-
ioRegistryEntryGetParentEntry := common.GetFunc[common.IORegistryEntryGetParentEntryFunc](i.ioKit, common.IORegistryEntryGetParentEntrySym)
274-
ioObjectConformsTo := common.GetFunc[common.IOObjectConformsToFunc](i.ioKit, common.IOObjectConformsToSym)
275-
276-
cfStringGetLength := common.GetFunc[common.CFStringGetLengthFunc](i.coreFoundation, common.CFStringGetLengthSym)
277-
cfStringGetCString := common.GetFunc[common.CFStringGetCStringFunc](i.coreFoundation, common.CFStringGetCStringSym)
278-
279248
var parent uint32
280-
if status := ioRegistryEntryGetParentEntry(d, common.KIOServicePlane, &parent); status != common.KERN_SUCCESS {
249+
if status := i.iokit.IORegistryEntryGetParentEntry(d, common.KIOServicePlane, &parent); status != common.KERN_SUCCESS {
281250
return nil, fmt.Errorf("IORegistryEntryGetParentEntry error=%d", status)
282251
}
283-
defer i.ioObjectRelease(parent)
252+
defer i.iokit.IOObjectRelease(parent)
284253

285-
if !ioObjectConformsTo(parent, "IOBlockStorageDriver") {
254+
if !i.iokit.IOObjectConformsTo(parent, "IOBlockStorageDriver") {
286255
// return nil, fmt.Errorf("ERROR: the object is not of the IOBlockStorageDriver class")
287256
return nil, nil
288257
}
289258

290259
var props unsafe.Pointer
291-
if status := i.ioRegistryEntryCreateCFProperties(d, unsafe.Pointer(&props), common.KCFAllocatorDefault, common.KNilOptions); status != common.KERN_SUCCESS {
260+
if status := i.iokit.IORegistryEntryCreateCFProperties(d, unsafe.Pointer(&props), common.KCFAllocatorDefault, common.KNilOptions); status != common.KERN_SUCCESS {
292261
return nil, fmt.Errorf("IORegistryEntryCreateCFProperties error=%d", status)
293262
}
294-
defer i.cfRelease(uintptr(props))
263+
defer i.corefoundation.CFRelease(uintptr(props))
295264

296265
key := i.cfStr(kIOBSDNameKey)
297-
defer i.cfRelease(uintptr(key))
298-
name := i.cfDictionaryGetValue(uintptr(props), uintptr(key))
266+
defer i.corefoundation.CFRelease(uintptr(key))
267+
name := i.corefoundation.CFDictionaryGetValue(uintptr(props), uintptr(key))
299268

300-
buf := common.NewCStr(cfStringGetLength(uintptr(name)))
301-
cfStringGetCString(uintptr(name), buf, buf.Length(), common.KCFStringEncodingUTF8)
269+
buf := common.NewCStr(i.corefoundation.CFStringGetLength(uintptr(name)))
270+
i.corefoundation.CFStringGetCString(uintptr(name), buf, buf.Length(), common.KCFStringEncodingUTF8)
302271

303272
stat, err := i.fillStat(parent)
304273
if err != nil {
@@ -314,19 +283,19 @@ func (i *ioCounters) getDriveStat(d uint32) (*IOCountersStat, error) {
314283

315284
func (i *ioCounters) fillStat(d uint32) (*IOCountersStat, error) {
316285
var props unsafe.Pointer
317-
status := i.ioRegistryEntryCreateCFProperties(d, unsafe.Pointer(&props), common.KCFAllocatorDefault, common.KNilOptions)
286+
status := i.iokit.IORegistryEntryCreateCFProperties(d, unsafe.Pointer(&props), common.KCFAllocatorDefault, common.KNilOptions)
318287
if status != common.KERN_SUCCESS {
319288
return nil, fmt.Errorf("IORegistryEntryCreateCFProperties error=%d", status)
320289
}
321290
if props == nil {
322291
return nil, nil
323292
}
324-
defer i.cfRelease(uintptr(props))
293+
defer i.corefoundation.CFRelease(uintptr(props))
325294

326295
key := i.cfStr(kIOBlockStorageDriverStatisticsKey)
327-
defer i.cfRelease(uintptr(key))
296+
defer i.corefoundation.CFRelease(uintptr(key))
328297

329-
v := i.cfDictionaryGetValue(uintptr(props), uintptr(key))
298+
v := i.corefoundation.CFDictionaryGetValue(uintptr(props), uintptr(key))
330299
if v == nil {
331300
return nil, errors.New("CFDictionaryGetValue failed")
332301
}
@@ -343,15 +312,15 @@ func (i *ioCounters) fillStat(d uint32) (*IOCountersStat, error) {
343312

344313
for key, off := range statstab {
345314
s := i.cfStr(key)
346-
if num := i.cfDictionaryGetValue(uintptr(v), uintptr(s)); num != nil {
347-
i.cfNumberGetValue(uintptr(num), common.KCFNumberSInt64Type, uintptr(unsafe.Add(unsafe.Pointer(&stat), off)))
315+
if num := i.corefoundation.CFDictionaryGetValue(uintptr(v), uintptr(s)); num != nil {
316+
i.corefoundation.CFNumberGetValue(uintptr(num), common.KCFNumberSInt64Type, uintptr(unsafe.Add(unsafe.Pointer(&stat), off)))
348317
}
349-
i.cfRelease(uintptr(s))
318+
i.corefoundation.CFRelease(uintptr(s))
350319
}
351320

352321
return &stat, nil
353322
}
354323

355324
func (i *ioCounters) cfStr(str string) unsafe.Pointer {
356-
return i.cfStringCreateWithCString(common.KCFAllocatorDefault, str, common.KCFStringEncodingUTF8)
325+
return i.corefoundation.CFStringCreateWithCString(common.KCFAllocatorDefault, str, common.KCFStringEncodingUTF8)
357326
}

0 commit comments

Comments
 (0)