FreeBSDProcess.c 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. /*
  2. htop - FreeBSDProcess.c
  3. (C) 2015 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 "config.h" // IWYU pragma: keep
  8. #include "freebsd/FreeBSDProcess.h"
  9. #include <stdlib.h>
  10. #include "CRT.h"
  11. #include "Macros.h"
  12. #include "Process.h"
  13. #include "RichString.h"
  14. #include "Scheduling.h"
  15. #include "XUtils.h"
  16. const char* const nodevStr = "nodev";
  17. const ProcessFieldData Process_fields[LAST_PROCESSFIELD] = {
  18. [0] = { .name = "", .title = NULL, .description = NULL, .flags = 0, },
  19. [PID] = { .name = "PID", .title = "PID", .description = "Process/thread ID", .flags = 0, .pidColumn = true, },
  20. [COMM] = { .name = "Command", .title = "Command ", .description = "Command line", .flags = 0, },
  21. [STATE] = { .name = "STATE", .title = "S ", .description = "Process state (S sleeping, R running, D disk, Z zombie, T traced, W paging)", .flags = 0, },
  22. [PPID] = { .name = "PPID", .title = "PPID", .description = "Parent process ID", .flags = 0, .pidColumn = true, },
  23. [PGRP] = { .name = "PGRP", .title = "PGRP", .description = "Process group ID", .flags = 0, .pidColumn = true, },
  24. [SESSION] = { .name = "SESSION", .title = "SID", .description = "Process's session ID", .flags = 0, .pidColumn = true, },
  25. [TTY] = { .name = "TTY", .title = "TTY ", .description = "Controlling terminal", .flags = 0, },
  26. [TPGID] = { .name = "TPGID", .title = "TPGID", .description = "Process ID of the fg process group of the controlling terminal", .flags = 0, .pidColumn = true, },
  27. [MAJFLT] = { .name = "MAJFLT", .title = " MAJFLT ", .description = "Number of copy-on-write faults", .flags = 0, .defaultSortDesc = true, },
  28. [PRIORITY] = { .name = "PRIORITY", .title = "PRI ", .description = "Kernel's internal priority for the process", .flags = 0, },
  29. [NICE] = { .name = "NICE", .title = " NI ", .description = "Nice value (the higher the value, the more it lets other processes take priority)", .flags = 0, },
  30. [STARTTIME] = { .name = "STARTTIME", .title = "START ", .description = "Time the process was started", .flags = 0, },
  31. [ELAPSED] = { .name = "ELAPSED", .title = "ELAPSED ", .description = "Time since the process was started", .flags = 0, },
  32. [PROCESSOR] = { .name = "PROCESSOR", .title = "CPU ", .description = "Id of the CPU the process last executed on", .flags = 0, },
  33. [M_VIRT] = { .name = "M_VIRT", .title = " VIRT ", .description = "Total program size in virtual memory", .flags = 0, .defaultSortDesc = true, },
  34. [M_RESIDENT] = { .name = "M_RESIDENT", .title = " RES ", .description = "Resident set size, size of the text and data sections, plus stack usage", .flags = 0, .defaultSortDesc = true, },
  35. [ST_UID] = { .name = "ST_UID", .title = "UID", .description = "User ID of the process owner", .flags = 0, },
  36. [PERCENT_CPU] = { .name = "PERCENT_CPU", .title = " CPU%", .description = "Percentage of the CPU time the process used in the last sampling", .flags = 0, .defaultSortDesc = true, .autoWidth = true, .autoTitleRightAlign = true, },
  37. [PERCENT_NORM_CPU] = { .name = "PERCENT_NORM_CPU", .title = "NCPU%", .description = "Normalized percentage of the CPU time the process used in the last sampling (normalized by cpu count)", .flags = 0, .defaultSortDesc = true, .autoWidth = true, },
  38. [PERCENT_MEM] = { .name = "PERCENT_MEM", .title = "MEM% ", .description = "Percentage of the memory the process is using, based on resident memory size", .flags = 0, .defaultSortDesc = true, },
  39. [USER] = { .name = "USER", .title = "USER ", .description = "Username of the process owner (or user ID if name cannot be determined)", .flags = 0, },
  40. [TIME] = { .name = "TIME", .title = " TIME+ ", .description = "Total time the process has spent in user and system time", .flags = 0, .defaultSortDesc = true, },
  41. [NLWP] = { .name = "NLWP", .title = "NLWP ", .description = "Number of threads in the process", .flags = 0, .defaultSortDesc = true, },
  42. [TGID] = { .name = "TGID", .title = "TGID", .description = "Thread group ID (i.e. process ID)", .flags = 0, .pidColumn = true, },
  43. [PROC_COMM] = { .name = "COMM", .title = "COMM ", .description = "comm string of the process", .flags = 0, },
  44. [PROC_EXE] = { .name = "EXE", .title = "EXE ", .description = "Basename of exe of the process", .flags = 0, },
  45. [CWD] = { .name = "CWD", .title = "CWD ", .description = "The current working directory of the process", .flags = PROCESS_FLAG_CWD, },
  46. #ifdef SCHEDULER_SUPPORT
  47. [SCHEDULERPOLICY] = { .name = "SCHEDULERPOLICY", .title = "SCHED ", .description = "Current scheduling policy of the process", .flags = PROCESS_FLAG_SCHEDPOL, },
  48. #endif
  49. [JID] = { .name = "JID", .title = "JID", .description = "Jail prison ID", .flags = 0, .pidColumn = true, },
  50. [JAIL] = { .name = "JAIL", .title = "JAIL ", .description = "Jail prison name", .flags = 0, },
  51. [SCHEDCLASS] = { .name = "SCHEDCLASS", .title = "SC", .description = "Scheduling Class (Timesharing, Realtime, Idletime)", .flags = 0, },
  52. [EMULATION] = { .name = "EMULATION", .title = "EMULATION ", .description = "System call emulation environment (ABI)", .flags = 0, },
  53. };
  54. Process* FreeBSDProcess_new(const Machine* machine) {
  55. FreeBSDProcess* this = xCalloc(1, sizeof(FreeBSDProcess));
  56. Object_setClass(this, Class(FreeBSDProcess));
  57. Process_init(&this->super, machine);
  58. return (Process*)this;
  59. }
  60. void Process_delete(Object* cast) {
  61. FreeBSDProcess* this = (FreeBSDProcess*) cast;
  62. Process_done((Process*)cast);
  63. free(this->emul);
  64. free(this->jname);
  65. free(this);
  66. }
  67. static const char FreeBSD_schedclassChars[MAX_SCHEDCLASS] = {
  68. [SCHEDCLASS_UNKNOWN] = '?', // Something went wrong or the base system has a new scheduling class
  69. [SCHEDCLASS_INTR_THREAD] = '-', // interrupt thread, these have special handling of priority
  70. [SCHEDCLASS_IDLE] = 'i', // idletime scheduling
  71. [SCHEDCLASS_TIMESHARE] = ' ', // timesharing process scheduling (regular processes are timeshared)
  72. [SCHEDCLASS_REALTIME] = 'r', // realtime scheduling
  73. };
  74. static void FreeBSDProcess_rowWriteField(const Row* super, RichString* str, ProcessField field) {
  75. const FreeBSDProcess* fp = (const FreeBSDProcess*) super;
  76. char buffer[256]; buffer[255] = '\0';
  77. char sched_class;
  78. int attr = CRT_colors[DEFAULT_COLOR];
  79. size_t n = sizeof(buffer) - 1;
  80. switch (field) {
  81. // add FreeBSD-specific fields here
  82. case JID: xSnprintf(buffer, n, "%*d ", Process_pidDigits, fp->jid); break;
  83. case JAIL:
  84. Row_printLeftAlignedField(str, attr, fp->jname ? fp->jname : "N/A", 11);
  85. return;
  86. case EMULATION:
  87. Row_printLeftAlignedField(str, attr, fp->emul ? fp->emul : "N/A", 16);
  88. return;
  89. case SCHEDCLASS:
  90. assert(0 <= fp->sched_class && fp->sched_class < ARRAYSIZE(FreeBSD_schedclassChars));
  91. sched_class = FreeBSD_schedclassChars[fp->sched_class];
  92. assert(sched_class);
  93. xSnprintf(buffer, n, " %c", sched_class);
  94. break;
  95. default:
  96. Process_writeField(&fp->super, str, field);
  97. return;
  98. }
  99. RichString_appendWide(str, attr, buffer);
  100. }
  101. static int FreeBSDProcess_compareByKey(const Process* v1, const Process* v2, ProcessField key) {
  102. const FreeBSDProcess* p1 = (const FreeBSDProcess*)v1;
  103. const FreeBSDProcess* p2 = (const FreeBSDProcess*)v2;
  104. switch (key) {
  105. // add FreeBSD-specific fields here
  106. case JID:
  107. return SPACESHIP_NUMBER(p1->jid, p2->jid);
  108. case JAIL:
  109. return SPACESHIP_NULLSTR(p1->jname, p2->jname);
  110. case EMULATION:
  111. return SPACESHIP_NULLSTR(p1->emul, p2->emul);
  112. case SCHEDCLASS:
  113. return SPACESHIP_NUMBER(p1->sched_class, p2->sched_class);
  114. default:
  115. return Process_compareByKey_Base(v1, v2, key);
  116. }
  117. }
  118. const ProcessClass FreeBSDProcess_class = {
  119. .super = {
  120. .super = {
  121. .extends = Class(Process),
  122. .display = Row_display,
  123. .delete = Process_delete,
  124. .compare = Process_compare
  125. },
  126. .isHighlighted = Process_rowIsHighlighted,
  127. .isVisible = Process_rowIsVisible,
  128. .matchesFilter = Process_rowMatchesFilter,
  129. .compareByParent = Process_compareByParent,
  130. .sortKeyString = Process_rowGetSortKey,
  131. .writeField = FreeBSDProcess_rowWriteField
  132. },
  133. .compareByKey = FreeBSDProcess_compareByKey
  134. };