1// +build darwin
2// +build cgo
3
4package disk
5
6/*
7#cgo LDFLAGS: -framework CoreFoundation -framework IOKit
8#include <stdint.h>
9#include <CoreFoundation/CoreFoundation.h>
10#include "iostat_darwin.h"
11*/
12import "C"
13
14import (
15	"context"
16
17	"github.com/shirou/gopsutil/v3/internal/common"
18)
19
20func IOCountersWithContext(ctx context.Context, names ...string) (map[string]IOCountersStat, error) {
21	var buf [C.NDRIVE]C.DriveStats
22	n, err := C.readdrivestat(&buf[0], C.int(len(buf)))
23	if err != nil {
24		return nil, err
25	}
26	ret := make(map[string]IOCountersStat, 0)
27	for i := 0; i < int(n); i++ {
28		d := IOCountersStat{
29			ReadBytes:  uint64(buf[i].read),
30			WriteBytes: uint64(buf[i].written),
31			ReadCount:  uint64(buf[i].nread),
32			WriteCount: uint64(buf[i].nwrite),
33			ReadTime:   uint64(buf[i].readtime / 1000 / 1000), // note: read/write time are in ns, but we want ms.
34			WriteTime:  uint64(buf[i].writetime / 1000 / 1000),
35			IoTime:     uint64((buf[i].readtime + buf[i].writetime) / 1000 / 1000),
36			Name:       C.GoString(&buf[i].name[0]),
37		}
38		if len(names) > 0 && !common.StringsHas(names, d.Name) {
39			continue
40		}
41
42		ret[d.Name] = d
43	}
44	return ret, nil
45}
46