github.com/isyscore/isc-gobase@v1.5.3-0.20231218061332-cbc7451899e9/system/disk/disk_darwin_cgo.go (about)

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