github.com/keybase/client/go@v0.0.0-20241007131713-f10651d043c8/runtimestats/stats_darwin.go (about)

     1  //go:build darwin
     2  // +build darwin
     3  
     4  package runtimestats
     5  
     6  /*
     7  #import <mach/mach.h>
     8  #import <mach/task.h>
     9  #import <assert.h>
    10  
    11  float tot_cpu = 0;
    12  int64_t tot_resident = 0;
    13  int64_t tot_virtual = 0;
    14  int64_t tot_free = 0;
    15  
    16  void GetTaskStats() {
    17      // get cpu usage
    18      thread_array_t thread_list;
    19      mach_msg_type_number_t thread_count;
    20      thread_info_data_t thinfo;
    21      mach_msg_type_number_t thread_info_count;
    22      thread_basic_info_t basic_info_th;
    23      if (task_threads(mach_task_self(), &thread_list, &thread_count) != KERN_SUCCESS) {
    24          return;
    25      }
    26      tot_cpu = 0;
    27      for (int j = 0; j < thread_count; j++) {
    28          thread_info_count = THREAD_INFO_MAX;
    29          if (thread_info(thread_list[j], THREAD_BASIC_INFO, (thread_info_t)thinfo,
    30              &thread_info_count) != KERN_SUCCESS) {
    31              return;
    32          }
    33          basic_info_th = (thread_basic_info_t)thinfo;
    34          if (!(basic_info_th->flags & TH_FLAGS_IDLE)) {
    35              tot_cpu += basic_info_th->cpu_usage / (float)TH_USAGE_SCALE * 100.0;
    36          }
    37      }
    38      vm_deallocate(mach_task_self(), (vm_offset_t)thread_list, thread_count * sizeof(thread_t));
    39  
    40      // get real memory used
    41      task_vm_info_data_t task_info_data;
    42      mach_msg_type_number_t count = TASK_VM_INFO_COUNT;
    43      if (task_info(mach_task_self(), TASK_VM_INFO, (task_info_t)&task_info_data, &count) != KERN_SUCCESS) {
    44          return;
    45      }
    46      tot_resident = task_info_data.resident_size - task_info_data.reusable;
    47      tot_virtual = task_info_data.virtual_size;
    48  
    49      // get system free memory
    50      mach_port_t host_port;
    51      mach_msg_type_number_t host_size;
    52      vm_size_t pagesize;
    53      host_port = mach_host_self();
    54      host_size = sizeof(vm_statistics_data_t) / sizeof(integer_t);
    55      host_page_size(host_port, &pagesize);
    56      vm_statistics_data_t vm_stat;
    57      if (host_statistics(host_port, HOST_VM_INFO, (host_info_t)&vm_stat, &host_size) != KERN_SUCCESS) {
    58          return;
    59      }
    60      tot_free = vm_stat.free_count * pagesize;
    61  }
    62  
    63  int64_t TotalResident() {
    64      return tot_resident;
    65  }
    66  
    67  int TotalCPU() {
    68      return (int)(tot_cpu * 100);
    69  }
    70  
    71  int64_t TotalVirtual() {
    72      return tot_virtual;
    73  }
    74  
    75  int64_t TotalFree() {
    76      return tot_free;
    77  }
    78  */
    79  import "C"
    80  
    81  func getStats() (res statsResult) {
    82  	C.GetTaskStats()
    83  	res.TotalCPU = int(C.TotalCPU())
    84  	res.TotalResident = int64(C.TotalResident())
    85  	res.TotalVirtual = int64(C.TotalVirtual())
    86  	res.TotalFree = int64(C.TotalFree())
    87  	return res
    88  }