DarwinProcessTable.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. /*
  2. htop - DarwinProcessTable.c
  3. (C) 2014 Hisham H. Muhammad
  4. Released under the GNU GPLv2+, see the COPYING file
  5. in the source distribution for its full text.
  6. */
  7. #include "darwin/DarwinProcessTable.h"
  8. #include <errno.h>
  9. #include <libproc.h>
  10. #include <stdbool.h>
  11. #include <stdio.h>
  12. #include <stdlib.h>
  13. #include <string.h>
  14. #include <unistd.h>
  15. #include <utmpx.h>
  16. #include <sys/mman.h>
  17. #include <sys/sysctl.h>
  18. #include "CRT.h"
  19. #include "ProcessTable.h"
  20. #include "darwin/DarwinMachine.h"
  21. #include "darwin/DarwinProcess.h"
  22. #include "darwin/Platform.h"
  23. #include "darwin/PlatformHelpers.h"
  24. #include "generic/openzfs_sysctl.h"
  25. #include "zfs/ZfsArcStats.h"
  26. static struct kinfo_proc* ProcessTable_getKInfoProcs(size_t* count) {
  27. int mib[4] = { CTL_KERN, KERN_PROC, KERN_PROC_ALL, 0 };
  28. struct kinfo_proc* processes = NULL;
  29. for (unsigned int retry = 0; retry < 4; retry++) {
  30. size_t size = 0;
  31. if (sysctl(mib, 4, NULL, &size, NULL, 0) < 0 || size == 0) {
  32. CRT_fatalError("Unable to get size of kproc_infos");
  33. }
  34. size += 16 * retry * retry * sizeof(struct kinfo_proc);
  35. processes = xRealloc(processes, size);
  36. if (sysctl(mib, 4, processes, &size, NULL, 0) == 0) {
  37. *count = size / sizeof(struct kinfo_proc);
  38. return processes;
  39. }
  40. if (errno != ENOMEM)
  41. break;
  42. }
  43. CRT_fatalError("Unable to get kinfo_procs");
  44. }
  45. ProcessTable* ProcessTable_new(Machine* host, Hashtable* pidMatchList) {
  46. DarwinProcessTable* this = xCalloc(1, sizeof(DarwinProcessTable));
  47. Object_setClass(this, Class(ProcessTable));
  48. ProcessTable* super = &this->super;
  49. ProcessTable_init(super, Class(DarwinProcess), host, pidMatchList);
  50. return super;
  51. }
  52. void ProcessTable_delete(Object* cast) {
  53. DarwinProcessTable* this = (DarwinProcessTable*) cast;
  54. ProcessTable_done(&this->super);
  55. free(this);
  56. }
  57. void ProcessTable_goThroughEntries(ProcessTable* super) {
  58. const Machine* host = super->super.host;
  59. const DarwinMachine* dhost = (const DarwinMachine*) host;
  60. DarwinProcessTable* dpt = (DarwinProcessTable*) super;
  61. bool preExisting = true;
  62. struct kinfo_proc* ps;
  63. size_t count;
  64. DarwinProcess* proc;
  65. /* Get the time difference */
  66. dpt->global_diff = 0;
  67. for (unsigned int i = 0; i < host->existingCPUs; ++i) {
  68. for (size_t j = 0; j < CPU_STATE_MAX; ++j) {
  69. dpt->global_diff += dhost->curr_load[i].cpu_ticks[j] - dhost->prev_load[i].cpu_ticks[j];
  70. }
  71. }
  72. const double time_interval_ns = Platform_schedulerTicksToNanoseconds(dpt->global_diff) / (double) host->activeCPUs;
  73. /* We use kinfo_procs for initial data since :
  74. *
  75. * 1) They always succeed.
  76. * 2) They contain the basic information.
  77. *
  78. * We attempt to fill-in additional information with libproc.
  79. */
  80. ps = ProcessTable_getKInfoProcs(&count);
  81. for (size_t i = 0; i < count; ++i) {
  82. proc = (DarwinProcess*)ProcessTable_getProcess(super, ps[i].kp_proc.p_pid, &preExisting, DarwinProcess_new);
  83. DarwinProcess_setFromKInfoProc(&proc->super, &ps[i], preExisting);
  84. DarwinProcess_setFromLibprocPidinfo(proc, dpt, time_interval_ns);
  85. if (proc->super.st_uid != ps[i].kp_eproc.e_ucred.cr_uid) {
  86. proc->super.st_uid = ps[i].kp_eproc.e_ucred.cr_uid;
  87. proc->super.user = UsersTable_getRef(host->usersTable, proc->super.st_uid);
  88. }
  89. // Disabled for High Sierra due to bug in macOS High Sierra
  90. bool isScanThreadSupported = !Platform_KernelVersionIsBetween((KernelVersion) {17, 0, 0}, (KernelVersion) {17, 5, 0});
  91. if (isScanThreadSupported) {
  92. DarwinProcess_scanThreads(proc, dpt);
  93. }
  94. super->totalTasks += 1;
  95. if (!preExisting) {
  96. ProcessTable_add(super, &proc->super);
  97. }
  98. }
  99. free(ps);
  100. }