2018-10-19 18:33:23 +00:00
|
|
|
// +build darwin
|
|
|
|
// +build cgo
|
|
|
|
|
|
|
|
package disk
|
|
|
|
|
|
|
|
/*
|
2020-07-01 12:47:56 +00:00
|
|
|
#cgo LDFLAGS: -framework CoreFoundation -framework IOKit
|
2018-10-19 18:33:23 +00:00
|
|
|
#include <stdint.h>
|
2020-07-01 12:47:56 +00:00
|
|
|
#include <CoreFoundation/CoreFoundation.h>
|
|
|
|
#include "iostat_darwin.h"
|
2018-10-19 18:33:23 +00:00
|
|
|
*/
|
|
|
|
import "C"
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
|
|
|
|
"github.com/shirou/gopsutil/internal/common"
|
|
|
|
)
|
|
|
|
|
|
|
|
func IOCounters(names ...string) (map[string]IOCountersStat, error) {
|
|
|
|
return IOCountersWithContext(context.Background(), names...)
|
|
|
|
}
|
|
|
|
|
|
|
|
func IOCountersWithContext(ctx context.Context, names ...string) (map[string]IOCountersStat, error) {
|
2020-07-01 12:47:56 +00:00
|
|
|
var buf [C.NDRIVE]C.DriveStats
|
|
|
|
n, err := C.readdrivestat(&buf[0], C.int(len(buf)))
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2018-10-19 18:33:23 +00:00
|
|
|
}
|
|
|
|
ret := make(map[string]IOCountersStat, 0)
|
2020-07-01 12:47:56 +00:00
|
|
|
for i := 0; i < int(n); i++ {
|
2018-10-19 18:33:23 +00:00
|
|
|
d := IOCountersStat{
|
2020-07-01 12:47:56 +00: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]),
|
2018-10-19 18:33:23 +00:00
|
|
|
}
|
|
|
|
if len(names) > 0 && !common.StringsHas(names, d.Name) {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
ret[d.Name] = d
|
|
|
|
}
|
|
|
|
return ret, nil
|
|
|
|
}
|