SolarisProcess.c 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. /*
  2. htop - SolarisProcess.c
  3. (C) 2015 Hisham H. Muhammad
  4. (C) 2017,2018 Guy M. Broome
  5. Released under the GNU GPLv2+, see the COPYING file
  6. in the source distribution for its full text.
  7. */
  8. #include "config.h" // IWYU pragma: keep
  9. #include "solaris/SolarisProcess.h"
  10. #include <stdlib.h>
  11. #include <string.h>
  12. #include <unistd.h>
  13. #include <sys/syscall.h>
  14. #include "ProcessTable.h"
  15. #include "CRT.h"
  16. #include "solaris/Platform.h"
  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, O onproc, Z zombie, T stopped, W waiting)", .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. //[MINFLT] = { .name = "MINFLT", .title = " MINFLT ", .description = "Number of minor faults which have not required loading a memory page from disk", .flags = 0, .defaultSortDesc = true, },
  28. //[MAJFLT] = { .name = "MAJFLT", .title = " MAJFLT ", .description = "Number of major faults which have required loading a memory page from disk", .flags = 0, .defaultSortDesc = true, },
  29. [PRIORITY] = { .name = "PRIORITY", .title = "PRI ", .description = "Kernel's internal priority for the process", .flags = 0, },
  30. [NICE] = { .name = "NICE", .title = " NI ", .description = "Nice value (the higher the value, the more it lets other processes take priority)", .flags = 0, },
  31. [STARTTIME] = { .name = "STARTTIME", .title = "START ", .description = "Time the process was started", .flags = 0, },
  32. [ELAPSED] = { .name = "ELAPSED", .title = "ELAPSED ", .description = "Time since the process was started", .flags = 0, },
  33. [PROCESSOR] = { .name = "PROCESSOR", .title = "CPU ", .description = "Id of the CPU the process last executed on", .flags = 0, },
  34. [M_VIRT] = { .name = "M_VIRT", .title = " VIRT ", .description = "Total program size in virtual memory", .flags = 0, .defaultSortDesc = true, },
  35. [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, },
  36. [ST_UID] = { .name = "ST_UID", .title = "UID", .description = "User ID of the process owner", .flags = 0, },
  37. [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, },
  38. [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, },
  39. [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, },
  40. [USER] = { .name = "USER", .title = "USER ", .description = "Username of the process owner (or user ID if name cannot be determined)", .flags = 0, },
  41. [TIME] = { .name = "TIME", .title = " TIME+ ", .description = "Total time the process has spent in user and system time", .flags = 0, .defaultSortDesc = true, },
  42. [NLWP] = { .name = "NLWP", .title = "NLWP ", .description = "Number of threads in the process", .flags = 0, },
  43. [TGID] = { .name = "TGID", .title = "TGID", .description = "Thread group ID (i.e. process ID)", .flags = 0, .pidColumn = true, },
  44. [PROC_COMM] = { .name = "COMM", .title = "COMM ", .description = "comm string of the process", .flags = 0, },
  45. [PROC_EXE] = { .name = "EXE", .title = "EXE ", .description = "Basename of exe of the process", .flags = 0, },
  46. [CWD] = { .name = "CWD", .title = "CWD ", .description = "The current working directory of the process", .flags = PROCESS_FLAG_CWD, },
  47. [ZONEID] = { .name = "ZONEID", .title = "ZONEID", .description = "Zone ID", .flags = 0, .pidColumn = true, },
  48. [ZONE] = { .name = "ZONE", .title = "ZONE ", .description = "Zone name", .flags = 0, },
  49. [PROJID] = { .name = "PROJID", .title = "PRJID", .description = "Project ID", .flags = 0, .pidColumn = true, },
  50. [TASKID] = { .name = "TASKID", .title = "TSKID", .description = "Task ID", .flags = 0, .pidColumn = true, },
  51. [POOLID] = { .name = "POOLID", .title = "POLID", .description = "Pool ID", .flags = 0, .pidColumn = true, },
  52. [CONTID] = { .name = "CONTID", .title = "CNTID", .description = "Contract ID", .flags = 0, .pidColumn = true, },
  53. [LWPID] = { .name = "LWPID", .title = "LWPID", .description = "LWP ID", .flags = 0, .pidColumn = true, },
  54. };
  55. Process* SolarisProcess_new(const Machine* host) {
  56. SolarisProcess* this = xCalloc(1, sizeof(SolarisProcess));
  57. Object_setClass(this, Class(SolarisProcess));
  58. Process_init(&this->super, host);
  59. return (Process*)this;
  60. }
  61. void Process_delete(Object* cast) {
  62. SolarisProcess* sp = (SolarisProcess*) cast;
  63. Process_done((Process*)cast);
  64. free(sp->zname);
  65. free(sp);
  66. }
  67. static void SolarisProcess_rowWriteField(const Row* super, RichString* str, ProcessField field) {
  68. const SolarisProcess* sp = (const SolarisProcess*) super;
  69. char buffer[256]; buffer[255] = '\0';
  70. int attr = CRT_colors[DEFAULT_COLOR];
  71. size_t n = sizeof(buffer) - 1;
  72. switch (field) {
  73. // add Solaris-specific fields here
  74. case ZONEID: xSnprintf(buffer, n, "%*d ", Process_pidDigits, sp->zoneid); break;
  75. case PROJID: xSnprintf(buffer, n, "%*d ", Process_pidDigits, sp->projid); break;
  76. case TASKID: xSnprintf(buffer, n, "%*d ", Process_pidDigits, sp->taskid); break;
  77. case POOLID: xSnprintf(buffer, n, "%*d ", Process_pidDigits, sp->poolid); break;
  78. case CONTID: xSnprintf(buffer, n, "%*d ", Process_pidDigits, sp->contid); break;
  79. case ZONE: Row_printLeftAlignedField(str, attr, sp->zname ? sp->zname : "global", ZONENAME_MAX/4); return;
  80. case PID: xSnprintf(buffer, n, "%*d ", Process_pidDigits, sp->realpid); break;
  81. case PPID: xSnprintf(buffer, n, "%*d ", Process_pidDigits, sp->realppid); break;
  82. case TGID: xSnprintf(buffer, n, "%*d ", Process_pidDigits, sp->realtgid); break;
  83. case LWPID: xSnprintf(buffer, n, "%*d ", Process_pidDigits, sp->lwpid); break;
  84. default:
  85. Process_writeField(&sp->super, str, field);
  86. return;
  87. }
  88. RichString_appendWide(str, attr, buffer);
  89. }
  90. static int SolarisProcess_compareByKey(const Process* v1, const Process* v2, ProcessField key) {
  91. const SolarisProcess* p1 = (const SolarisProcess*)v1;
  92. const SolarisProcess* p2 = (const SolarisProcess*)v2;
  93. switch (key) {
  94. case ZONEID:
  95. return SPACESHIP_NUMBER(p1->zoneid, p2->zoneid);
  96. case PROJID:
  97. return SPACESHIP_NUMBER(p1->projid, p2->projid);
  98. case TASKID:
  99. return SPACESHIP_NUMBER(p1->taskid, p2->taskid);
  100. case POOLID:
  101. return SPACESHIP_NUMBER(p1->poolid, p2->poolid);
  102. case CONTID:
  103. return SPACESHIP_NUMBER(p1->contid, p2->contid);
  104. case ZONE:
  105. return strcmp(p1->zname ? p1->zname : "global", p2->zname ? p2->zname : "global");
  106. case PID:
  107. return SPACESHIP_NUMBER(p1->realpid, p2->realpid);
  108. case PPID:
  109. return SPACESHIP_NUMBER(p1->realppid, p2->realppid);
  110. case LWPID:
  111. return SPACESHIP_NUMBER(p1->lwpid, p2->lwpid);
  112. default:
  113. return Process_compareByKey_Base(v1, v2, key);
  114. }
  115. }
  116. const ProcessClass SolarisProcess_class = {
  117. .super = {
  118. .super = {
  119. .extends = Class(Process),
  120. .display = Row_display,
  121. .delete = Process_delete,
  122. .compare = Process_compare
  123. },
  124. .isHighlighted = Process_rowIsHighlighted,
  125. .isVisible = Process_rowIsVisible,
  126. .matchesFilter = Process_rowMatchesFilter,
  127. .compareByParent = Process_compareByParent,
  128. .sortKeyString = Process_rowGetSortKey,
  129. .writeField = SolarisProcess_rowWriteField
  130. },
  131. .compareByKey = SolarisProcess_compareByKey
  132. };