DragonFlyBSDProcess.c 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. /*
  2. htop - dragonflybsd/DragonFlyBSDProcess.c
  3. (C) 2015 Hisham H. Muhammad
  4. (C) 2017 Diederik de Groot
  5. Released under the GNU GPLv2+, see the COPYING file
  6. in the source distribution for its full text.
  7. */
  8. #include "dragonflybsd/DragonFlyBSDProcess.h"
  9. #include <stdlib.h>
  10. #include <string.h>
  11. #include <unistd.h>
  12. #include <sys/syscall.h>
  13. #include "CRT.h"
  14. #include "dragonflybsd/Platform.h"
  15. const ProcessFieldData Process_fields[LAST_PROCESSFIELD] = {
  16. [0] = { .name = "", .title = NULL, .description = NULL, .flags = 0, },
  17. [PID] = { .name = "PID", .title = "PID", .description = "Process/thread ID", .flags = 0, .pidColumn = true, },
  18. [COMM] = { .name = "Command", .title = "Command ", .description = "Command line", .flags = 0, },
  19. [STATE] = { .name = "STATE", .title = "S ", .description = "Process state (S sleeping (<20s), I Idle, Q Queued for Run, R running, D disk, Z zombie, T traced, W paging, B Blocked, A AskedPage, C Core, J Jailed)", .flags = 0, },
  20. [PPID] = { .name = "PPID", .title = "PPID", .description = "Parent process ID", .flags = 0, .pidColumn = true, },
  21. [PGRP] = { .name = "PGRP", .title = "PGRP", .description = "Process group ID", .flags = 0, .pidColumn = true, },
  22. [SESSION] = { .name = "SESSION", .title = "SID", .description = "Process's session ID", .flags = 0, .pidColumn = true, },
  23. [TTY] = { .name = "TTY", .title = "TTY ", .description = "Controlling terminal", .flags = 0, },
  24. [TPGID] = { .name = "TPGID", .title = "TPGID", .description = "Process ID of the fg process group of the controlling terminal", .flags = 0, .pidColumn = true, },
  25. [MINFLT] = { .name = "MINFLT", .title = " MINFLT ", .description = "Number of minor faults which have not required loading a memory page from disk", .flags = 0, .defaultSortDesc = true, },
  26. [MAJFLT] = { .name = "MAJFLT", .title = " MAJFLT ", .description = "Number of major faults which have required loading a memory page from disk", .flags = 0, .defaultSortDesc = true, },
  27. [PRIORITY] = { .name = "PRIORITY", .title = "PRI ", .description = "Kernel's internal priority for the process", .flags = 0, },
  28. [NICE] = { .name = "NICE", .title = " NI ", .description = "Nice value (the higher the value, the more it lets other processes take priority)", .flags = 0, },
  29. [STARTTIME] = { .name = "STARTTIME", .title = "START ", .description = "Time the process was started", .flags = 0, },
  30. [ELAPSED] = { .name = "ELAPSED", .title = "ELAPSED ", .description = "Time since the process was started", .flags = 0, },
  31. [PROCESSOR] = { .name = "PROCESSOR", .title = "CPU ", .description = "Id of the CPU the process last executed on", .flags = 0, },
  32. [M_VIRT] = { .name = "M_VIRT", .title = " VIRT ", .description = "Total program size in virtual memory", .flags = 0, .defaultSortDesc = true, },
  33. [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, },
  34. [ST_UID] = { .name = "ST_UID", .title = "UID", .description = "User ID of the process owner", .flags = 0, },
  35. [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, },
  36. [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, },
  37. [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, },
  38. [USER] = { .name = "USER", .title = "USER ", .description = "Username of the process owner (or user ID if name cannot be determined)", .flags = 0, },
  39. [TIME] = { .name = "TIME", .title = " TIME+ ", .description = "Total time the process has spent in user and system time", .flags = 0, .defaultSortDesc = true, },
  40. [NLWP] = { .name = "NLWP", .title = "NLWP ", .description = "Number of threads in the process", .flags = 0, },
  41. [TGID] = { .name = "TGID", .title = "TGID", .description = "Thread group ID (i.e. process ID)", .flags = 0, .pidColumn = true, },
  42. [PROC_COMM] = { .name = "COMM", .title = "COMM ", .description = "comm string of the process", .flags = 0, },
  43. [PROC_EXE] = { .name = "EXE", .title = "EXE ", .description = "Basename of exe of the process", .flags = 0, },
  44. [CWD] = { .name = "CWD", .title = "CWD ", .description = "The current working directory of the process", .flags = PROCESS_FLAG_CWD, },
  45. [JID] = { .name = "JID", .title = "JID", .description = "Jail prison ID", .flags = 0, .pidColumn = true, },
  46. [JAIL] = { .name = "JAIL", .title = "JAIL ", .description = "Jail prison name", .flags = 0, },
  47. };
  48. Process* DragonFlyBSDProcess_new(const Machine* host) {
  49. DragonFlyBSDProcess* this = xCalloc(1, sizeof(DragonFlyBSDProcess));
  50. Object_setClass(this, Class(DragonFlyBSDProcess));
  51. Process_init(&this->super, host);
  52. return (Process*)this;
  53. }
  54. void Process_delete(Object* cast) {
  55. DragonFlyBSDProcess* this = (DragonFlyBSDProcess*) cast;
  56. Process_done((Process*)cast);
  57. free(this->jname);
  58. free(this);
  59. }
  60. static void DragonFlyBSDProcess_rowWriteField(const Row* super, RichString* str, ProcessField field) {
  61. const Process* this = (const Process*) super;
  62. const DragonFlyBSDProcess* fp = (const DragonFlyBSDProcess*) super;
  63. char buffer[256]; buffer[255] = '\0';
  64. int attr = CRT_colors[DEFAULT_COLOR];
  65. size_t n = sizeof(buffer) - 1;
  66. switch (field) {
  67. // add Platform-specific fields here
  68. case PID: xSnprintf(buffer, n, "%*d ", Process_pidDigits, Process_isKernelThread(this) ? -1 : Process_getPid(this)); break;
  69. case JID: xSnprintf(buffer, n, "%*d ", Process_pidDigits, fp->jid); break;
  70. case JAIL: Row_printLeftAlignedField(str, attr, fp->jname, 11); return;
  71. default:
  72. Process_writeField(&fp->super, str, field);
  73. return;
  74. }
  75. RichString_appendWide(str, attr, buffer);
  76. }
  77. static int DragonFlyBSDProcess_compareByKey(const Process* v1, const Process* v2, ProcessField key) {
  78. const DragonFlyBSDProcess* p1 = (const DragonFlyBSDProcess*)v1;
  79. const DragonFlyBSDProcess* p2 = (const DragonFlyBSDProcess*)v2;
  80. switch (key) {
  81. // add Platform-specific fields here
  82. case JID:
  83. return SPACESHIP_NUMBER(p1->jid, p2->jid);
  84. case JAIL:
  85. return SPACESHIP_NULLSTR(p1->jname, p2->jname);
  86. default:
  87. return Process_compareByKey_Base(v1, v2, key);
  88. }
  89. }
  90. const ProcessClass DragonFlyBSDProcess_class = {
  91. .super = {
  92. .super = {
  93. .extends = Class(Process),
  94. .display = Row_display,
  95. .delete = Process_delete,
  96. .compare = Process_compare
  97. },
  98. .isHighlighted = Process_rowIsHighlighted,
  99. .isVisible = Process_rowIsVisible,
  100. .matchesFilter = Process_rowMatchesFilter,
  101. .compareByParent = Process_compareByParent,
  102. .sortKeyString = Process_rowGetSortKey,
  103. .writeField = DragonFlyBSDProcess_rowWriteField
  104. },
  105. .compareByKey = DragonFlyBSDProcess_compareByKey
  106. };