2017-03-20 05:13:22 +08:00
|
|
|
// +build darwin
|
|
|
|
// +build cgo
|
|
|
|
|
|
|
|
package disk
|
|
|
|
|
|
|
|
/*
|
2019-08-16 01:30:28 +08:00
|
|
|
#cgo LDFLAGS: -framework CoreFoundation -framework IOKit
|
2017-03-20 05:13:22 +08:00
|
|
|
#include <stdint.h>
|
2019-08-16 01:30:28 +08:00
|
|
|
#include <CoreFoundation/CoreFoundation.h>
|
2017-03-20 05:13:22 +08:00
|
|
|
#include "disk_darwin.h"
|
|
|
|
*/
|
|
|
|
import "C"
|
|
|
|
|
|
|
|
import (
|
2017-12-31 14:25:49 +08:00
|
|
|
"context"
|
2017-04-08 02:40:49 +08:00
|
|
|
|
|
|
|
"github.com/shirou/gopsutil/internal/common"
|
2017-03-20 05:13:22 +08:00
|
|
|
)
|
|
|
|
|
2017-04-14 09:53:09 +08:00
|
|
|
func IOCounters(names ...string) (map[string]IOCountersStat, error) {
|
2017-12-31 14:25:49 +08:00
|
|
|
return IOCountersWithContext(context.Background(), names...)
|
|
|
|
}
|
|
|
|
|
|
|
|
func IOCountersWithContext(ctx context.Context, names ...string) (map[string]IOCountersStat, error) {
|
2019-08-16 01:30:28 +08:00
|
|
|
var buf [C.NDRIVE]C.DriveStats
|
|
|
|
n, err := C.readdrivestat(&buf[0], C.int(len(buf)))
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2017-03-20 05:13:22 +08:00
|
|
|
}
|
|
|
|
ret := make(map[string]IOCountersStat, 0)
|
2019-08-16 01:30:28 +08:00
|
|
|
for i := 0; i < int(n); i++ {
|
2017-03-20 05:13:22 +08:00
|
|
|
d := IOCountersStat{
|
2019-08-16 01:30:28 +08:00
|
|
|
ReadBytes: uint64(buf[i].read),
|
|
|
|
WriteBytes: uint64(buf[i].written),
|
|
|
|
ReadCount: uint64(buf[i].nread),
|
|
|
|
WriteCount: uint64(buf[i].nwrite),
|
|
|
|
ReadTime: uint64(buf[i].readtime / 1000 / 1000), // note: read/write time are in ns, but we want ms.
|
|
|
|
WriteTime: uint64(buf[i].writetime / 1000 / 1000),
|
|
|
|
IoTime: uint64((buf[i].readtime + buf[i].writetime) / 1000 / 1000),
|
|
|
|
Name: C.GoString(&buf[i].name[0]),
|
2017-03-20 05:13:22 +08:00
|
|
|
}
|
2017-04-08 02:40:49 +08:00
|
|
|
if len(names) > 0 && !common.StringsHas(names, d.Name) {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2017-03-20 05:13:22 +08:00
|
|
|
ret[d.Name] = d
|
|
|
|
}
|
|
|
|
return ret, nil
|
|
|
|
}
|