DarwinMachine.c 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  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(vm_statistics_t p) {
  49. mach_msg_type_number_t info_size = HOST_VM_INFO_COUNT;
  50. if (host_statistics(mach_host_self(), HOST_VM_INFO, (host_info_t)p, &info_size) != 0) {
  51. CRT_fatalError("Unable to retrieve VM statistics");
  52. }
  53. }
  54. void Machine_scan(Machine* super) {
  55. DarwinMachine* host = (DarwinMachine*) super;
  56. /* Update the global data (CPU times and VM stats) */
  57. DarwinMachine_freeCPULoadInfo(&host->prev_load);
  58. host->prev_load = host->curr_load;
  59. DarwinMachine_allocateCPULoadInfo(&host->curr_load);
  60. DarwinMachine_getVMStats(&host->vm_stats);
  61. openzfs_sysctl_updateArcStats(&host->zfs);
  62. }
  63. Machine* Machine_new(UsersTable* usersTable, uid_t userId) {
  64. DarwinMachine* this = xCalloc(1, sizeof(DarwinMachine));
  65. Machine* super = &this->super;
  66. Machine_init(super, usersTable, userId);
  67. /* Initialize the CPU information */
  68. super->activeCPUs = DarwinMachine_allocateCPULoadInfo(&this->prev_load);
  69. super->existingCPUs = super->activeCPUs;
  70. DarwinMachine_getHostInfo(&this->host_info);
  71. DarwinMachine_allocateCPULoadInfo(&this->curr_load);
  72. /* Initialize the VM statistics */
  73. DarwinMachine_getVMStats(&this->vm_stats);
  74. /* Initialize the ZFS kstats, if zfs.kext loaded */
  75. openzfs_sysctl_init(&this->zfs);
  76. openzfs_sysctl_updateArcStats(&this->zfs);
  77. return super;
  78. }
  79. void Machine_delete(Machine* super) {
  80. DarwinMachine* this = (DarwinMachine*) super;
  81. DarwinMachine_freeCPULoadInfo(&this->prev_load);
  82. Machine_done(super);
  83. free(this);
  84. }
  85. bool Machine_isCPUonline(const Machine* host, unsigned int id) {
  86. assert(id < host->existingCPUs);
  87. // TODO: support offline CPUs and hot swapping
  88. (void) host; (void) id;
  89. return true;
  90. }