DarwinMachine.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. /*
  2. htop - DarwinMachine.c
  3. (C) 2014 Hisham H. Muhammad
  4. (C) 2023 htop dev team
  5. Released under the GNU GPLv2+, see the COPYING file
  6. in the source distribution for its full text.
  7. */
  8. #include "darwin/DarwinMachine.h"
  9. #include <errno.h>
  10. #include <stdbool.h>
  11. #include <stdio.h>
  12. #include <stdlib.h>
  13. #include <string.h>
  14. #include <unistd.h>
  15. #include <sys/mman.h>
  16. #include <sys/sysctl.h>
  17. #include "CRT.h"
  18. #include "Machine.h"
  19. #include "darwin/Platform.h"
  20. #include "darwin/PlatformHelpers.h"
  21. #include "generic/openzfs_sysctl.h"
  22. #include "zfs/ZfsArcStats.h"
  23. static void DarwinMachine_getHostInfo(host_basic_info_data_t* p) {
  24. mach_msg_type_number_t info_size = HOST_BASIC_INFO_COUNT;
  25. if (0 != host_info(mach_host_self(), HOST_BASIC_INFO, (host_info_t)p, &info_size)) {
  26. CRT_fatalError("Unable to retrieve host info");
  27. }
  28. }
  29. static void DarwinMachine_freeCPULoadInfo(processor_cpu_load_info_t* p) {
  30. if (!p)
  31. return;
  32. if (!*p)
  33. return;
  34. if (0 != munmap(*p, vm_page_size)) {
  35. CRT_fatalError("Unable to free old CPU load information");
  36. }
  37. *p = NULL;
  38. }
  39. static unsigned DarwinMachine_allocateCPULoadInfo(processor_cpu_load_info_t* p) {
  40. mach_msg_type_number_t info_size = sizeof(processor_cpu_load_info_t);
  41. unsigned cpu_count;
  42. // TODO Improving the accuracy of the load counts would help a lot.
  43. if (0 != host_processor_info(mach_host_self(), PROCESSOR_CPU_LOAD_INFO, &cpu_count, (processor_info_array_t*)p, &info_size)) {
  44. CRT_fatalError("Unable to retrieve CPU info");
  45. }
  46. return cpu_count;
  47. }
  48. static void DarwinMachine_getVMStats(DarwinMachine* this) {
  49. #ifdef HAVE_STRUCT_VM_STATISTICS64
  50. mach_msg_type_number_t info_size = HOST_VM_INFO64_COUNT;
  51. if (host_statistics64(mach_host_self(), HOST_VM_INFO64, (host_info_t)&this->vm_stats, &info_size) != 0) {
  52. CRT_fatalError("Unable to retrieve VM statistics64");
  53. }
  54. #else
  55. mach_msg_type_number_t info_size = HOST_VM_INFO_COUNT;
  56. if (host_statistics(mach_host_self(), HOST_VM_INFO, (host_info_t)&this->vm_stats, &info_size) != 0) {
  57. CRT_fatalError("Unable to retrieve VM statistics");
  58. }
  59. #endif
  60. }
  61. void Machine_scan(Machine* super) {
  62. DarwinMachine* host = (DarwinMachine*) super;
  63. /* Update the global data (CPU times and VM stats) */
  64. DarwinMachine_freeCPULoadInfo(&host->prev_load);
  65. host->prev_load = host->curr_load;
  66. DarwinMachine_allocateCPULoadInfo(&host->curr_load);
  67. DarwinMachine_getVMStats(host);
  68. openzfs_sysctl_updateArcStats(&host->zfs);
  69. }
  70. Machine* Machine_new(UsersTable* usersTable, uid_t userId) {
  71. DarwinMachine* this = xCalloc(1, sizeof(DarwinMachine));
  72. Machine* super = &this->super;
  73. Machine_init(super, usersTable, userId);
  74. /* Initialize the CPU information */
  75. super->activeCPUs = DarwinMachine_allocateCPULoadInfo(&this->prev_load);
  76. super->existingCPUs = super->activeCPUs;
  77. DarwinMachine_getHostInfo(&this->host_info);
  78. DarwinMachine_allocateCPULoadInfo(&this->curr_load);
  79. /* Initialize the VM statistics */
  80. DarwinMachine_getVMStats(this);
  81. /* Initialize the ZFS kstats, if zfs.kext loaded */
  82. openzfs_sysctl_init(&this->zfs);
  83. openzfs_sysctl_updateArcStats(&this->zfs);
  84. return super;
  85. }
  86. void Machine_delete(Machine* super) {
  87. DarwinMachine* this = (DarwinMachine*) super;
  88. DarwinMachine_freeCPULoadInfo(&this->prev_load);
  89. Machine_done(super);
  90. free(this);
  91. }
  92. bool Machine_isCPUonline(const Machine* host, unsigned int id) {
  93. assert(id < host->existingCPUs);
  94. // TODO: support offline CPUs and hot swapping
  95. (void) host; (void) id;
  96. return true;
  97. }