LinuxProcess.c 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490
  1. /*
  2. htop - LinuxProcess.c
  3. (C) 2014 Hisham H. Muhammad
  4. (C) 2020 Red Hat, Inc. All Rights Reserved.
  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 "linux/LinuxProcess.h"
  10. #include <assert.h>
  11. #include <math.h>
  12. #include <stdint.h>
  13. #include <stdio.h>
  14. #include <stdlib.h>
  15. #include <syscall.h>
  16. #include <unistd.h>
  17. #include "CRT.h"
  18. #include "Macros.h"
  19. #include "Process.h"
  20. #include "ProvideCurses.h"
  21. #include "RichString.h"
  22. #include "RowField.h"
  23. #include "Scheduling.h"
  24. #include "Settings.h"
  25. #include "XUtils.h"
  26. #include "linux/IOPriority.h"
  27. #include "linux/LinuxMachine.h"
  28. const ProcessFieldData Process_fields[LAST_PROCESSFIELD] = {
  29. [0] = { .name = "", .title = NULL, .description = NULL, .flags = 0, },
  30. [PID] = { .name = "PID", .title = "PID", .description = "Process/thread ID", .flags = 0, .pidColumn = true, },
  31. [COMM] = { .name = "Command", .title = "Command ", .description = "Command line", .flags = 0, },
  32. [STATE] = { .name = "STATE", .title = "S ", .description = "Process state (S sleeping, R running, D disk, Z zombie, T traced, W paging, I idle)", .flags = 0, },
  33. [PPID] = { .name = "PPID", .title = "PPID", .description = "Parent process ID", .flags = 0, .pidColumn = true, },
  34. [PGRP] = { .name = "PGRP", .title = "PGRP", .description = "Process group ID", .flags = 0, .pidColumn = true, },
  35. [SESSION] = { .name = "SESSION", .title = "SID", .description = "Process's session ID", .flags = 0, .pidColumn = true, },
  36. [TTY] = { .name = "TTY", .title = "TTY ", .description = "Controlling terminal", .flags = 0, },
  37. [TPGID] = { .name = "TPGID", .title = "TPGID", .description = "Process ID of the fg process group of the controlling terminal", .flags = 0, .pidColumn = true, },
  38. [MINFLT] = { .name = "MINFLT", .title = " MINFLT ", .description = "Number of minor faults which have not required loading a memory page from disk", .flags = 0, .defaultSortDesc = true, },
  39. [CMINFLT] = { .name = "CMINFLT", .title = " CMINFLT ", .description = "Children processes' minor faults", .flags = 0, .defaultSortDesc = true, },
  40. [MAJFLT] = { .name = "MAJFLT", .title = " MAJFLT ", .description = "Number of major faults which have required loading a memory page from disk", .flags = 0, .defaultSortDesc = true, },
  41. [CMAJFLT] = { .name = "CMAJFLT", .title = " CMAJFLT ", .description = "Children processes' major faults", .flags = 0, .defaultSortDesc = true, },
  42. [UTIME] = { .name = "UTIME", .title = " UTIME+ ", .description = "User CPU time - time the process spent executing in user mode", .flags = 0, .defaultSortDesc = true, },
  43. [STIME] = { .name = "STIME", .title = " STIME+ ", .description = "System CPU time - time the kernel spent running system calls for this process", .flags = 0, .defaultSortDesc = true, },
  44. [CUTIME] = { .name = "CUTIME", .title = " CUTIME+ ", .description = "Children processes' user CPU time", .flags = 0, .defaultSortDesc = true, },
  45. [CSTIME] = { .name = "CSTIME", .title = " CSTIME+ ", .description = "Children processes' system CPU time", .flags = 0, .defaultSortDesc = true, },
  46. [PRIORITY] = { .name = "PRIORITY", .title = "PRI ", .description = "Kernel's internal priority for the process", .flags = 0, },
  47. [NICE] = { .name = "NICE", .title = " NI ", .description = "Nice value (the higher the value, the more it lets other processes take priority)", .flags = 0, },
  48. [STARTTIME] = { .name = "STARTTIME", .title = "START ", .description = "Time the process was started", .flags = 0, },
  49. [ELAPSED] = { .name = "ELAPSED", .title = "ELAPSED ", .description = "Time since the process was started", .flags = 0, },
  50. [PROCESSOR] = { .name = "PROCESSOR", .title = "CPU ", .description = "Id of the CPU the process last executed on", .flags = 0, },
  51. [M_VIRT] = { .name = "M_VIRT", .title = " VIRT ", .description = "Total program size in virtual memory", .flags = 0, .defaultSortDesc = true, },
  52. [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, },
  53. [M_SHARE] = { .name = "M_SHARE", .title = " SHR ", .description = "Size of the process's shared pages", .flags = 0, .defaultSortDesc = true, },
  54. [M_PRIV] = { .name = "M_PRIV", .title = " PRIV ", .description = "The private memory size of the process - resident set size minus shared memory", .flags = 0, .defaultSortDesc = true, },
  55. [M_TRS] = { .name = "M_TRS", .title = " CODE ", .description = "Size of the .text segment of the process (CODE)", .flags = 0, .defaultSortDesc = true, },
  56. [M_DRS] = { .name = "M_DRS", .title = " DATA ", .description = "Size of the .data segment plus stack usage of the process (DATA)", .flags = 0, .defaultSortDesc = true, },
  57. [M_LRS] = { .name = "M_LRS", .title = " LIB ", .description = "The library size of the process (calculated from memory maps)", .flags = PROCESS_FLAG_LINUX_LRS_FIX, .defaultSortDesc = true, },
  58. [ST_UID] = { .name = "ST_UID", .title = "UID", .description = "User ID of the process owner", .flags = 0, },
  59. [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, },
  60. [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, },
  61. [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, },
  62. [USER] = { .name = "USER", .title = "USER ", .description = "Username of the process owner (or user ID if name cannot be determined)", .flags = 0, },
  63. [TIME] = { .name = "TIME", .title = " TIME+ ", .description = "Total time the process has spent in user and system time", .flags = 0, .defaultSortDesc = true, },
  64. [NLWP] = { .name = "NLWP", .title = "NLWP ", .description = "Number of threads in the process", .flags = 0, .defaultSortDesc = true, },
  65. [TGID] = { .name = "TGID", .title = "TGID", .description = "Thread group ID (i.e. process ID)", .flags = 0, .pidColumn = true, },
  66. #ifdef HAVE_OPENVZ
  67. [CTID] = { .name = "CTID", .title = " CTID ", .description = "OpenVZ container ID (a.k.a. virtual environment ID)", .flags = PROCESS_FLAG_LINUX_OPENVZ, },
  68. [VPID] = { .name = "VPID", .title = "VPID", .description = "OpenVZ process ID", .flags = PROCESS_FLAG_LINUX_OPENVZ, .pidColumn = true, },
  69. #endif
  70. #ifdef HAVE_VSERVER
  71. [VXID] = { .name = "VXID", .title = " VXID ", .description = "VServer process ID", .flags = PROCESS_FLAG_LINUX_VSERVER, },
  72. #endif
  73. [RCHAR] = { .name = "RCHAR", .title = "RCHAR ", .description = "Number of bytes the process has read", .flags = PROCESS_FLAG_IO, .defaultSortDesc = true, },
  74. [WCHAR] = { .name = "WCHAR", .title = "WCHAR ", .description = "Number of bytes the process has written", .flags = PROCESS_FLAG_IO, .defaultSortDesc = true, },
  75. [SYSCR] = { .name = "SYSCR", .title = " READ_SYSC ", .description = "Number of read(2) syscalls for the process", .flags = PROCESS_FLAG_IO, .defaultSortDesc = true, },
  76. [SYSCW] = { .name = "SYSCW", .title = " WRITE_SYSC ", .description = "Number of write(2) syscalls for the process", .flags = PROCESS_FLAG_IO, .defaultSortDesc = true, },
  77. [RBYTES] = { .name = "RBYTES", .title = " IO_R ", .description = "Bytes of read(2) I/O for the process", .flags = PROCESS_FLAG_IO, .defaultSortDesc = true, },
  78. [WBYTES] = { .name = "WBYTES", .title = " IO_W ", .description = "Bytes of write(2) I/O for the process", .flags = PROCESS_FLAG_IO, .defaultSortDesc = true, },
  79. [CNCLWB] = { .name = "CNCLWB", .title = " IO_C ", .description = "Bytes of cancelled write(2) I/O", .flags = PROCESS_FLAG_IO, .defaultSortDesc = true, },
  80. [IO_READ_RATE] = { .name = "IO_READ_RATE", .title = " DISK READ ", .description = "The I/O rate of read(2) in bytes per second for the process", .flags = PROCESS_FLAG_IO, .defaultSortDesc = true, },
  81. [IO_WRITE_RATE] = { .name = "IO_WRITE_RATE", .title = " DISK WRITE ", .description = "The I/O rate of write(2) in bytes per second for the process", .flags = PROCESS_FLAG_IO, .defaultSortDesc = true, },
  82. [IO_RATE] = { .name = "IO_RATE", .title = " DISK R/W ", .description = "Total I/O rate in bytes per second", .flags = PROCESS_FLAG_IO, .defaultSortDesc = true, },
  83. [CGROUP] = { .name = "CGROUP", .title = "CGROUP (raw)", .description = "Which cgroup the process is in", .flags = PROCESS_FLAG_LINUX_CGROUP, .autoWidth = true, },
  84. [CCGROUP] = { .name = "CCGROUP", .title = "CGROUP (compressed)", .description = "Which cgroup the process is in (condensed to essentials)", .flags = PROCESS_FLAG_LINUX_CGROUP, .autoWidth = true, },
  85. [CONTAINER] = { .name = "CONTAINER", .title = "CONTAINER", .description = "Name of the container the process is in (guessed by heuristics)", .flags = PROCESS_FLAG_LINUX_CGROUP, .autoWidth = true, },
  86. [OOM] = { .name = "OOM", .title = " OOM ", .description = "OOM (Out-of-Memory) killer score", .flags = PROCESS_FLAG_LINUX_OOM, .defaultSortDesc = true, },
  87. [IO_PRIORITY] = { .name = "IO_PRIORITY", .title = "IO ", .description = "I/O priority", .flags = PROCESS_FLAG_LINUX_IOPRIO, },
  88. #ifdef HAVE_DELAYACCT
  89. [PERCENT_CPU_DELAY] = { .name = "PERCENT_CPU_DELAY", .title = "CPUD% ", .description = "CPU delay %", .flags = PROCESS_FLAG_LINUX_DELAYACCT, .defaultSortDesc = true, },
  90. [PERCENT_IO_DELAY] = { .name = "PERCENT_IO_DELAY", .title = " IOD% ", .description = "Block I/O delay %", .flags = PROCESS_FLAG_LINUX_DELAYACCT, .defaultSortDesc = true, },
  91. [PERCENT_SWAP_DELAY] = { .name = "PERCENT_SWAP_DELAY", .title = "SWPD% ", .description = "Swapin delay %", .flags = PROCESS_FLAG_LINUX_DELAYACCT, .defaultSortDesc = true, },
  92. #endif
  93. [M_PSS] = { .name = "M_PSS", .title = " PSS ", .description = "proportional set size, same as M_RESIDENT but each page is divided by the number of processes sharing it", .flags = PROCESS_FLAG_LINUX_SMAPS, .defaultSortDesc = true, },
  94. [M_SWAP] = { .name = "M_SWAP", .title = " SWAP ", .description = "Size of the process's swapped pages", .flags = PROCESS_FLAG_LINUX_SMAPS, .defaultSortDesc = true, },
  95. [M_PSSWP] = { .name = "M_PSSWP", .title = " PSSWP ", .description = "shows proportional swap share of this mapping, unlike \"Swap\", this does not take into account swapped out page of underlying shmem objects", .flags = PROCESS_FLAG_LINUX_SMAPS, .defaultSortDesc = true, },
  96. [CTXT] = { .name = "CTXT", .title = " CTXT ", .description = "Context switches (incremental sum of voluntary_ctxt_switches and nonvoluntary_ctxt_switches)", .flags = PROCESS_FLAG_LINUX_CTXT, .defaultSortDesc = true, },
  97. [SECATTR] = { .name = "SECATTR", .title = "Security Attribute", .description = "Security attribute of the process (e.g. SELinux or AppArmor)", .flags = PROCESS_FLAG_LINUX_SECATTR, .autoWidth = true, },
  98. [PROC_COMM] = { .name = "COMM", .title = "COMM ", .description = "comm string of the process from /proc/[pid]/comm", .flags = 0, },
  99. [PROC_EXE] = { .name = "EXE", .title = "EXE ", .description = "Basename of exe of the process from /proc/[pid]/exe", .flags = 0, },
  100. [CWD] = { .name = "CWD", .title = "CWD ", .description = "The current working directory of the process", .flags = PROCESS_FLAG_CWD, },
  101. [AUTOGROUP_ID] = { .name = "AUTOGROUP_ID", .title = "AGRP", .description = "The autogroup identifier of the process", .flags = PROCESS_FLAG_LINUX_AUTOGROUP, },
  102. [AUTOGROUP_NICE] = { .name = "AUTOGROUP_NICE", .title = " ANI", .description = "Nice value (the higher the value, the more other processes take priority) associated with the process autogroup", .flags = PROCESS_FLAG_LINUX_AUTOGROUP, },
  103. [ISCONTAINER] = { .name = "ISCONTAINER", .title = "CONT ", .description = "Whether the process is running inside a child container", .flags = PROCESS_FLAG_LINUX_CONTAINER, },
  104. #ifdef SCHEDULER_SUPPORT
  105. [SCHEDULERPOLICY] = { .name = "SCHEDULERPOLICY", .title = "SCHED ", .description = "Current scheduling policy of the process", .flags = PROCESS_FLAG_SCHEDPOL, },
  106. #endif
  107. [GPU_TIME] = { .name = "GPU_TIME", .title = "GPU_TIME ", .description = "Total GPU time", .flags = PROCESS_FLAG_LINUX_GPU, .defaultSortDesc = true, },
  108. [GPU_PERCENT] = { .name = "GPU_PERCENT", .title = " GPU% ", .description = "Percentage of the GPU time the process used in the last sampling", .flags = PROCESS_FLAG_LINUX_GPU, .defaultSortDesc = true, },
  109. };
  110. Process* LinuxProcess_new(const Machine* host) {
  111. LinuxProcess* this = xCalloc(1, sizeof(LinuxProcess));
  112. Object_setClass(this, Class(LinuxProcess));
  113. Process_init(&this->super, host);
  114. return (Process*)this;
  115. }
  116. void Process_delete(Object* cast) {
  117. LinuxProcess* this = (LinuxProcess*) cast;
  118. Process_done((Process*)cast);
  119. free(this->container_short);
  120. free(this->cgroup_short);
  121. free(this->cgroup);
  122. #ifdef HAVE_OPENVZ
  123. free(this->ctid);
  124. #endif
  125. free(this->secattr);
  126. free(this);
  127. }
  128. /*
  129. [1] Note that before kernel 2.6.26 a process that has not asked for
  130. an io priority formally uses "none" as scheduling class, but the
  131. io scheduler will treat such processes as if it were in the best
  132. effort class. The priority within the best effort class will be
  133. dynamically derived from the cpu nice level of the process:
  134. io_priority = (cpu_nice + 20) / 5. -- From ionice(1) man page
  135. */
  136. static int LinuxProcess_effectiveIOPriority(const LinuxProcess* this) {
  137. if (IOPriority_class(this->ioPriority) == IOPRIO_CLASS_NONE) {
  138. return IOPriority_tuple(IOPRIO_CLASS_BE, (this->super.nice + 20) / 5);
  139. }
  140. return this->ioPriority;
  141. }
  142. #ifdef __ANDROID__
  143. #define SYS_ioprio_get __NR_ioprio_get
  144. #define SYS_ioprio_set __NR_ioprio_set
  145. #endif
  146. /*
  147. * Gather I/O scheduling class and priority (thread-specific data)
  148. */
  149. IOPriority LinuxProcess_updateIOPriority(Process* p) {
  150. IOPriority ioprio = 0;
  151. // Other OSes masquerading as Linux (NetBSD?) don't have this syscall
  152. #ifdef SYS_ioprio_get
  153. ioprio = syscall(SYS_ioprio_get, IOPRIO_WHO_PROCESS, Process_getPid(p));
  154. #endif
  155. LinuxProcess* this = (LinuxProcess*) p;
  156. this->ioPriority = ioprio;
  157. return ioprio;
  158. }
  159. static bool LinuxProcess_setIOPriority(Process* p, Arg ioprio) {
  160. // Other OSes masquerading as Linux (NetBSD?) don't have this syscall
  161. #ifdef SYS_ioprio_set
  162. syscall(SYS_ioprio_set, IOPRIO_WHO_PROCESS, Process_getPid(p), ioprio.i);
  163. #endif
  164. return LinuxProcess_updateIOPriority(p) == ioprio.i;
  165. }
  166. bool LinuxProcess_rowSetIOPriority(Row* super, Arg ioprio) {
  167. Process* p = (Process*) super;
  168. assert(Object_isA((const Object*) p, (const ObjectClass*) &Process_class));
  169. return LinuxProcess_setIOPriority(p, ioprio);
  170. }
  171. bool LinuxProcess_isAutogroupEnabled(void) {
  172. char buf[16];
  173. if (xReadfile(PROCDIR "/sys/kernel/sched_autogroup_enabled", buf, sizeof(buf)) < 0)
  174. return false;
  175. return buf[0] == '1';
  176. }
  177. static bool LinuxProcess_changeAutogroupPriorityBy(Process* p, Arg delta) {
  178. char buffer[256];
  179. pid_t pid = Process_getPid(p);
  180. xSnprintf(buffer, sizeof(buffer), PROCDIR "/%d/autogroup", pid);
  181. FILE* file = fopen(buffer, "r+");
  182. if (!file)
  183. return false;
  184. long int identity;
  185. int nice;
  186. int ok = fscanf(file, "/autogroup-%ld nice %d", &identity, &nice);
  187. bool success = false;
  188. if (ok == 2 && fseek(file, 0L, SEEK_SET) == 0) {
  189. xSnprintf(buffer, sizeof(buffer), "%d", nice + delta.i);
  190. success = fputs(buffer, file) > 0;
  191. }
  192. fclose(file);
  193. return success;
  194. }
  195. bool LinuxProcess_rowChangeAutogroupPriorityBy(Row* super, Arg delta) {
  196. Process* p = (Process*) super;
  197. assert(Object_isA((const Object*) p, (const ObjectClass*) &Process_class));
  198. return LinuxProcess_changeAutogroupPriorityBy(p, delta);
  199. }
  200. static double LinuxProcess_totalIORate(const LinuxProcess* lp) {
  201. double totalRate = NAN;
  202. if (isNonnegative(lp->io_rate_read_bps)) {
  203. totalRate = lp->io_rate_read_bps;
  204. if (isNonnegative(lp->io_rate_write_bps)) {
  205. totalRate += lp->io_rate_write_bps;
  206. }
  207. } else if (isNonnegative(lp->io_rate_write_bps)) {
  208. totalRate = lp->io_rate_write_bps;
  209. }
  210. return totalRate;
  211. }
  212. static void LinuxProcess_rowWriteField(const Row* super, RichString* str, ProcessField field) {
  213. const Process* this = (const Process*) super;
  214. const LinuxProcess* lp = (const LinuxProcess*) super;
  215. const Machine* host = (const Machine*) super->host;
  216. const LinuxMachine* lhost = (const LinuxMachine*) super->host;
  217. bool coloring = host->settings->highlightMegabytes;
  218. char buffer[256]; buffer[255] = '\0';
  219. int attr = CRT_colors[DEFAULT_COLOR];
  220. size_t n = sizeof(buffer) - 1;
  221. switch (field) {
  222. case CMINFLT: Row_printCount(str, lp->cminflt, coloring); return;
  223. case CMAJFLT: Row_printCount(str, lp->cmajflt, coloring); return;
  224. case GPU_PERCENT: Row_printPercentage(lp->gpu_percent, buffer, n, 5, &attr); break;
  225. case GPU_TIME: Row_printNanoseconds(str, lp->gpu_time, coloring); return;
  226. case M_DRS: Row_printBytes(str, lp->m_drs * lhost->pageSize, coloring); return;
  227. case M_LRS:
  228. if (lp->m_lrs) {
  229. Row_printBytes(str, lp->m_lrs * lhost->pageSize, coloring);
  230. return;
  231. }
  232. attr = CRT_colors[PROCESS_SHADOW];
  233. xSnprintf(buffer, n, " N/A ");
  234. break;
  235. case M_TRS: Row_printBytes(str, lp->m_trs * lhost->pageSize, coloring); return;
  236. case M_SHARE: Row_printBytes(str, lp->m_share * lhost->pageSize, coloring); return;
  237. case M_PRIV: Row_printKBytes(str, lp->m_priv, coloring); return;
  238. case M_PSS: Row_printKBytes(str, lp->m_pss, coloring); return;
  239. case M_SWAP: Row_printKBytes(str, lp->m_swap, coloring); return;
  240. case M_PSSWP: Row_printKBytes(str, lp->m_psswp, coloring); return;
  241. case UTIME: Row_printTime(str, lp->utime, coloring); return;
  242. case STIME: Row_printTime(str, lp->stime, coloring); return;
  243. case CUTIME: Row_printTime(str, lp->cutime, coloring); return;
  244. case CSTIME: Row_printTime(str, lp->cstime, coloring); return;
  245. case RCHAR: Row_printBytes(str, lp->io_rchar, coloring); return;
  246. case WCHAR: Row_printBytes(str, lp->io_wchar, coloring); return;
  247. case SYSCR: Row_printCount(str, lp->io_syscr, coloring); return;
  248. case SYSCW: Row_printCount(str, lp->io_syscw, coloring); return;
  249. case RBYTES: Row_printBytes(str, lp->io_read_bytes, coloring); return;
  250. case WBYTES: Row_printBytes(str, lp->io_write_bytes, coloring); return;
  251. case CNCLWB: Row_printBytes(str, lp->io_cancelled_write_bytes, coloring); return;
  252. case IO_READ_RATE: Row_printRate(str, lp->io_rate_read_bps, coloring); return;
  253. case IO_WRITE_RATE: Row_printRate(str, lp->io_rate_write_bps, coloring); return;
  254. case IO_RATE: Row_printRate(str, LinuxProcess_totalIORate(lp), coloring); return;
  255. #ifdef HAVE_OPENVZ
  256. case CTID: xSnprintf(buffer, n, "%-8s ", lp->ctid ? lp->ctid : ""); break;
  257. case VPID: xSnprintf(buffer, n, "%*d ", Process_pidDigits, lp->vpid); break;
  258. #endif
  259. #ifdef HAVE_VSERVER
  260. case VXID: xSnprintf(buffer, n, "%5u ", lp->vxid); break;
  261. #endif
  262. case CGROUP:
  263. xSnprintf(buffer, n, "%-*.*s ", Row_fieldWidths[CGROUP], Row_fieldWidths[CGROUP], lp->cgroup ? lp->cgroup : "N/A");
  264. RichString_appendWide(str, attr, buffer);
  265. return;
  266. case CCGROUP:
  267. xSnprintf(buffer, n, "%-*.*s ", Row_fieldWidths[CCGROUP], Row_fieldWidths[CCGROUP], lp->cgroup_short ? lp->cgroup_short : (lp->cgroup ? lp->cgroup : "N/A"));
  268. RichString_appendWide(str, attr, buffer);
  269. return;
  270. case CONTAINER:
  271. xSnprintf(buffer, n, "%-*.*s ", Row_fieldWidths[CONTAINER], Row_fieldWidths[CONTAINER], lp->container_short ? lp->container_short : "N/A");
  272. RichString_appendWide(str, attr, buffer);
  273. return;
  274. case OOM: xSnprintf(buffer, n, "%4u ", lp->oom); break;
  275. case IO_PRIORITY: {
  276. int klass = IOPriority_class(lp->ioPriority);
  277. if (klass == IOPRIO_CLASS_NONE) {
  278. // see note [1] above
  279. xSnprintf(buffer, n, "B%1d ", (int) (this->nice + 20) / 5);
  280. } else if (klass == IOPRIO_CLASS_BE) {
  281. xSnprintf(buffer, n, "B%1d ", IOPriority_data(lp->ioPriority));
  282. } else if (klass == IOPRIO_CLASS_RT) {
  283. attr = CRT_colors[PROCESS_HIGH_PRIORITY];
  284. xSnprintf(buffer, n, "R%1d ", IOPriority_data(lp->ioPriority));
  285. } else if (klass == IOPRIO_CLASS_IDLE) {
  286. attr = CRT_colors[PROCESS_LOW_PRIORITY];
  287. xSnprintf(buffer, n, "id ");
  288. } else {
  289. xSnprintf(buffer, n, "?? ");
  290. }
  291. break;
  292. }
  293. #ifdef HAVE_DELAYACCT
  294. case PERCENT_CPU_DELAY: Row_printPercentage(lp->cpu_delay_percent, buffer, n, 5, &attr); break;
  295. case PERCENT_IO_DELAY: Row_printPercentage(lp->blkio_delay_percent, buffer, n, 5, &attr); break;
  296. case PERCENT_SWAP_DELAY: Row_printPercentage(lp->swapin_delay_percent, buffer, n, 5, &attr); break;
  297. #endif
  298. case CTXT:
  299. if (lp->ctxt_diff > 1000) {
  300. attr |= A_BOLD;
  301. }
  302. xSnprintf(buffer, n, "%5lu ", lp->ctxt_diff);
  303. break;
  304. case SECATTR:
  305. snprintf(buffer, n, "%-*.*s ", Row_fieldWidths[SECATTR], Row_fieldWidths[SECATTR], lp->secattr ? lp->secattr : "N/A");
  306. RichString_appendWide(str, attr, buffer);
  307. return;
  308. case AUTOGROUP_ID:
  309. if (lp->autogroup_id != -1) {
  310. xSnprintf(buffer, n, "%4ld ", lp->autogroup_id);
  311. } else {
  312. attr = CRT_colors[PROCESS_SHADOW];
  313. xSnprintf(buffer, n, " N/A ");
  314. }
  315. break;
  316. case AUTOGROUP_NICE:
  317. if (lp->autogroup_id != -1) {
  318. xSnprintf(buffer, n, "%3d ", lp->autogroup_nice);
  319. attr = lp->autogroup_nice < 0 ? CRT_colors[PROCESS_HIGH_PRIORITY]
  320. : lp->autogroup_nice > 0 ? CRT_colors[PROCESS_LOW_PRIORITY]
  321. : CRT_colors[PROCESS_SHADOW];
  322. } else {
  323. attr = CRT_colors[PROCESS_SHADOW];
  324. xSnprintf(buffer, n, "N/A ");
  325. }
  326. break;
  327. case ISCONTAINER:
  328. switch (this->isRunningInContainer) {
  329. case TRI_ON:
  330. xSnprintf(buffer, n, "YES ");
  331. break;
  332. case TRI_OFF:
  333. xSnprintf(buffer, n, "NO ");
  334. break;
  335. default:
  336. attr = CRT_colors[PROCESS_SHADOW];
  337. xSnprintf(buffer, n, "N/A ");
  338. }
  339. break;
  340. default:
  341. Process_writeField(this, str, field);
  342. return;
  343. }
  344. RichString_appendAscii(str, attr, buffer);
  345. }
  346. static int LinuxProcess_compareByKey(const Process* v1, const Process* v2, ProcessField key) {
  347. const LinuxProcess* p1 = (const LinuxProcess*)v1;
  348. const LinuxProcess* p2 = (const LinuxProcess*)v2;
  349. switch (key) {
  350. case M_DRS:
  351. return SPACESHIP_NUMBER(p1->m_drs, p2->m_drs);
  352. case M_LRS:
  353. return SPACESHIP_NUMBER(p1->m_lrs, p2->m_lrs);
  354. case M_TRS:
  355. return SPACESHIP_NUMBER(p1->m_trs, p2->m_trs);
  356. case M_SHARE:
  357. return SPACESHIP_NUMBER(p1->m_share, p2->m_share);
  358. case M_PRIV:
  359. return SPACESHIP_NUMBER(p1->m_priv, p2->m_priv);
  360. case M_PSS:
  361. return SPACESHIP_NUMBER(p1->m_pss, p2->m_pss);
  362. case M_SWAP:
  363. return SPACESHIP_NUMBER(p1->m_swap, p2->m_swap);
  364. case M_PSSWP:
  365. return SPACESHIP_NUMBER(p1->m_psswp, p2->m_psswp);
  366. case UTIME:
  367. return SPACESHIP_NUMBER(p1->utime, p2->utime);
  368. case CUTIME:
  369. return SPACESHIP_NUMBER(p1->cutime, p2->cutime);
  370. case STIME:
  371. return SPACESHIP_NUMBER(p1->stime, p2->stime);
  372. case CSTIME:
  373. return SPACESHIP_NUMBER(p1->cstime, p2->cstime);
  374. case RCHAR:
  375. return SPACESHIP_NUMBER(p1->io_rchar, p2->io_rchar);
  376. case WCHAR:
  377. return SPACESHIP_NUMBER(p1->io_wchar, p2->io_wchar);
  378. case SYSCR:
  379. return SPACESHIP_NUMBER(p1->io_syscr, p2->io_syscr);
  380. case SYSCW:
  381. return SPACESHIP_NUMBER(p1->io_syscw, p2->io_syscw);
  382. case RBYTES:
  383. return SPACESHIP_NUMBER(p1->io_read_bytes, p2->io_read_bytes);
  384. case WBYTES:
  385. return SPACESHIP_NUMBER(p1->io_write_bytes, p2->io_write_bytes);
  386. case CNCLWB:
  387. return SPACESHIP_NUMBER(p1->io_cancelled_write_bytes, p2->io_cancelled_write_bytes);
  388. case IO_READ_RATE:
  389. return compareRealNumbers(p1->io_rate_read_bps, p2->io_rate_read_bps);
  390. case IO_WRITE_RATE:
  391. return compareRealNumbers(p1->io_rate_write_bps, p2->io_rate_write_bps);
  392. case IO_RATE:
  393. return compareRealNumbers(LinuxProcess_totalIORate(p1), LinuxProcess_totalIORate(p2));
  394. #ifdef HAVE_OPENVZ
  395. case CTID:
  396. return SPACESHIP_NULLSTR(p1->ctid, p2->ctid);
  397. case VPID:
  398. return SPACESHIP_NUMBER(p1->vpid, p2->vpid);
  399. #endif
  400. #ifdef HAVE_VSERVER
  401. case VXID:
  402. return SPACESHIP_NUMBER(p1->vxid, p2->vxid);
  403. #endif
  404. case CGROUP:
  405. return SPACESHIP_NULLSTR(p1->cgroup, p2->cgroup);
  406. case CCGROUP:
  407. return SPACESHIP_NULLSTR(p1->cgroup_short, p2->cgroup_short);
  408. case CONTAINER:
  409. return SPACESHIP_NULLSTR(p1->container_short, p2->container_short);
  410. case OOM:
  411. return SPACESHIP_NUMBER(p1->oom, p2->oom);
  412. #ifdef HAVE_DELAYACCT
  413. case PERCENT_CPU_DELAY:
  414. return compareRealNumbers(p1->cpu_delay_percent, p2->cpu_delay_percent);
  415. case PERCENT_IO_DELAY:
  416. return compareRealNumbers(p1->blkio_delay_percent, p2->blkio_delay_percent);
  417. case PERCENT_SWAP_DELAY:
  418. return compareRealNumbers(p1->swapin_delay_percent, p2->swapin_delay_percent);
  419. #endif
  420. case IO_PRIORITY:
  421. return SPACESHIP_NUMBER(LinuxProcess_effectiveIOPriority(p1), LinuxProcess_effectiveIOPriority(p2));
  422. case CTXT:
  423. return SPACESHIP_NUMBER(p1->ctxt_diff, p2->ctxt_diff);
  424. case SECATTR:
  425. return SPACESHIP_NULLSTR(p1->secattr, p2->secattr);
  426. case AUTOGROUP_ID:
  427. return SPACESHIP_NUMBER(p1->autogroup_id, p2->autogroup_id);
  428. case AUTOGROUP_NICE:
  429. return SPACESHIP_NUMBER(p1->autogroup_nice, p2->autogroup_nice);
  430. case GPU_PERCENT: {
  431. int r = compareRealNumbers(p1->gpu_percent, p2->gpu_percent);
  432. if (r)
  433. return r;
  434. return SPACESHIP_NUMBER(p1->gpu_time, p2->gpu_time);
  435. }
  436. case GPU_TIME:
  437. return SPACESHIP_NUMBER(p1->gpu_time, p2->gpu_time);
  438. case ISCONTAINER:
  439. return SPACESHIP_NUMBER(v1->isRunningInContainer, v2->isRunningInContainer);
  440. default:
  441. return Process_compareByKey_Base(v1, v2, key);
  442. }
  443. }
  444. const ProcessClass LinuxProcess_class = {
  445. .super = {
  446. .super = {
  447. .extends = Class(Process),
  448. .display = Row_display,
  449. .delete = Process_delete,
  450. .compare = Process_compare
  451. },
  452. .isHighlighted = Process_rowIsHighlighted,
  453. .isVisible = Process_rowIsVisible,
  454. .matchesFilter = Process_rowMatchesFilter,
  455. .compareByParent = Process_compareByParent,
  456. .sortKeyString = Process_rowGetSortKey,
  457. .writeField = LinuxProcess_rowWriteField
  458. },
  459. .compareByKey = LinuxProcess_compareByKey
  460. };