Process.h 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340
  1. #ifndef HEADER_Process
  2. #define HEADER_Process
  3. /*
  4. htop - Process.h
  5. (C) 2004-2015 Hisham H. Muhammad
  6. (C) 2020 Red Hat, Inc. All Rights Reserved.
  7. Released under the GNU GPLv2+, see the COPYING file
  8. in the source distribution for its full text.
  9. */
  10. #include <limits.h>
  11. #include <stdbool.h>
  12. #include <stdint.h>
  13. #include <sys/types.h>
  14. #include "Object.h"
  15. #include "RichString.h"
  16. #include "Row.h"
  17. #include "RowField.h"
  18. #define PROCESS_FLAG_IO 0x00000001
  19. #define PROCESS_FLAG_CWD 0x00000002
  20. #define PROCESS_FLAG_SCHEDPOL 0x00000004
  21. #define DEFAULT_HIGHLIGHT_SECS 5
  22. /* Sentinel value for an unknown niceness in Process.nice */
  23. #define PROCESS_NICE_UNKNOWN (-LONG_MAX)
  24. typedef enum Tristate_ {
  25. TRI_INITIAL = 0,
  26. TRI_OFF = -1,
  27. TRI_ON = 1,
  28. } Tristate;
  29. /* Core process states (shared by platforms)
  30. * NOTE: The enum has an ordering that is important!
  31. * See processStateChar in process.c for ProcessSate -> letter mapping */
  32. typedef enum ProcessState_ {
  33. UNKNOWN = 1,
  34. RUNNABLE,
  35. RUNNING,
  36. QUEUED,
  37. WAITING,
  38. UNINTERRUPTIBLE_WAIT,
  39. BLOCKED,
  40. PAGING,
  41. STOPPED,
  42. TRACED,
  43. ZOMBIE,
  44. DEFUNCT,
  45. IDLE,
  46. SLEEPING
  47. } ProcessState;
  48. struct Machine_; // IWYU pragma: keep
  49. struct Settings_; // IWYU pragma: keep
  50. /* Holds information about regions of the cmdline that should be
  51. * highlighted (e.g. program basename, delimiter, comm). */
  52. typedef struct ProcessCmdlineHighlight_ {
  53. size_t offset; /* first character to highlight */
  54. size_t length; /* How many characters to highlight, zero if unused */
  55. int attr; /* The attributes used to highlight */
  56. int flags; /* Special flags used for selective highlighting, zero for always */
  57. } ProcessCmdlineHighlight;
  58. /* ProcessMergedCommand is populated by Process_makeCommandStr: It
  59. * contains the merged Command string, and the information needed by
  60. * Process_writeCommand to color the string. str will be NULL for kernel
  61. * threads and zombies */
  62. typedef struct ProcessMergedCommand_ {
  63. uint64_t lastUpdate; /* Marker based on settings->lastUpdate to track when the rendering needs refreshing */
  64. char* str; /* merged Command string */
  65. size_t highlightCount; /* how many portions of cmdline to highlight */
  66. ProcessCmdlineHighlight highlights[8]; /* which portions of cmdline to highlight */
  67. } ProcessMergedCommand;
  68. typedef struct Process_ {
  69. /* Super object for emulated OOP */
  70. Row super;
  71. /* Process group identifier */
  72. int pgrp;
  73. /* Session identifier */
  74. int session;
  75. /* Foreground group identifier of the controlling terminal */
  76. int tpgid;
  77. /* This is a kernel (helper) task */
  78. bool isKernelThread;
  79. /* This is a userland thread / LWP */
  80. bool isUserlandThread;
  81. /* This process is running inside a container */
  82. Tristate isRunningInContainer;
  83. /* Controlling terminal identifier of the process */
  84. unsigned long int tty_nr;
  85. /* Controlling terminal name of the process */
  86. char* tty_name;
  87. /* User identifier */
  88. uid_t st_uid;
  89. /* User name */
  90. const char* user;
  91. /* Non root owned process with elevated privileges
  92. * Linux:
  93. * - from file capabilities
  94. * - inherited from the ambient set
  95. */
  96. Tristate elevated_priv;
  97. /* Process runtime (in hundredth of a second) */
  98. unsigned long long int time;
  99. /*
  100. * Process name including arguments.
  101. * Use Process_getCommand() for Command actually displayed.
  102. */
  103. char* cmdline;
  104. /* End Offset in cmdline of the process basename */
  105. int cmdlineBasenameEnd;
  106. /* Start Offset in cmdline of the process basename */
  107. int cmdlineBasenameStart;
  108. /* The process' "command" name */
  109. char* procComm;
  110. /* The main process executable */
  111. char* procExe;
  112. /* The process/thread working directory */
  113. char* procCwd;
  114. /* Offset in procExe of the process basename */
  115. int procExeBasenameOffset;
  116. /* Tells if the executable has been replaced in the filesystem since start */
  117. bool procExeDeleted;
  118. /* Tells if the process uses replaced shared libraries since start */
  119. bool usesDeletedLib;
  120. /* CPU number last executed on */
  121. int processor;
  122. /* CPU usage during last cycle (in percent) */
  123. float percent_cpu;
  124. /* Memory usage during last cycle (in percent) */
  125. float percent_mem;
  126. /* Scheduling priority */
  127. long int priority;
  128. /* Nice value */
  129. long int nice;
  130. /* Number of threads in this process */
  131. long int nlwp;
  132. /* Process start time (in seconds elapsed since the Epoch) */
  133. time_t starttime_ctime;
  134. /* Process start time (cached formatted string) */
  135. char starttime_show[8];
  136. /* Total program size (in kilobytes) */
  137. long m_virt;
  138. /* Resident set size (in kilobytes) */
  139. long m_resident;
  140. /* Number of minor faults the process has made which have not required loading a memory page from disk */
  141. unsigned long int minflt;
  142. /* Number of major faults the process has made which have required loading a memory page from disk */
  143. unsigned long int majflt;
  144. /* Process state enum field (platform dependent) */
  145. ProcessState state;
  146. /* Current scheduling policy */
  147. int scheduling_policy;
  148. /*
  149. * Internal state for merged Command display
  150. */
  151. ProcessMergedCommand mergedCommand;
  152. } Process;
  153. typedef struct ProcessFieldData_ {
  154. /* Name (displayed in setup menu) */
  155. const char* name;
  156. /* Title (display in main screen); must have same width as the printed values */
  157. const char* title;
  158. /* Description (displayed in setup menu) */
  159. const char* description;
  160. /* Scan flag to enable scan-method otherwise not run */
  161. uint32_t flags;
  162. /* Whether the values are process identifiers; adjusts the width of title and values if true */
  163. bool pidColumn;
  164. /* Whether the column should be sorted in descending order by default */
  165. bool defaultSortDesc;
  166. /* Whether the column width is dynamically adjusted (the minimum width is determined by the title length) */
  167. bool autoWidth;
  168. /* Whether the title of a column with dynamically adjusted width is right aligned (default is left aligned) */
  169. bool autoTitleRightAlign;
  170. } ProcessFieldData;
  171. #define LAST_PROCESSFIELD LAST_RESERVED_FIELD
  172. typedef int32_t ProcessField; /* see ReservedField list in RowField.h */
  173. // Implemented in platform-specific code:
  174. void Process_writeField(const Process* this, RichString* str, ProcessField field);
  175. int Process_compare(const void* v1, const void* v2);
  176. int Process_compareByParent(const Row* r1, const Row* r2);
  177. void Process_delete(Object* cast);
  178. extern const ProcessFieldData Process_fields[LAST_PROCESSFIELD];
  179. #define Process_pidDigits Row_pidDigits
  180. #define Process_uidDigits Row_uidDigits
  181. typedef Process* (*Process_New)(const struct Machine_*);
  182. typedef int (*Process_CompareByKey)(const Process*, const Process*, ProcessField);
  183. typedef struct ProcessClass_ {
  184. const RowClass super;
  185. const Process_CompareByKey compareByKey;
  186. } ProcessClass;
  187. #define As_Process(this_) ((const ProcessClass*)((this_)->super.super.klass))
  188. #define Process_compareByKey(p1_, p2_, key_) (As_Process(p1_)->compareByKey ? (As_Process(p1_)->compareByKey(p1_, p2_, key_)) : Process_compareByKey_Base(p1_, p2_, key_))
  189. static inline void Process_setPid(Process* this, pid_t pid) {
  190. this->super.id = pid;
  191. }
  192. static inline pid_t Process_getPid(const Process* this) {
  193. return (pid_t)this->super.id;
  194. }
  195. static inline void Process_setThreadGroup(Process* this, pid_t pid) {
  196. this->super.group = pid;
  197. }
  198. static inline pid_t Process_getThreadGroup(const Process* this) {
  199. return (pid_t)this->super.group;
  200. }
  201. static inline void Process_setParent(Process* this, pid_t pid) {
  202. this->super.parent = pid;
  203. }
  204. static inline pid_t Process_getParent(const Process* this) {
  205. return (pid_t)this->super.parent;
  206. }
  207. static inline pid_t Process_getGroupOrParent(const Process* this) {
  208. return Row_getGroupOrParent(&this->super);
  209. }
  210. static inline bool Process_isKernelThread(const Process* this) {
  211. return this->isKernelThread;
  212. }
  213. static inline bool Process_isUserlandThread(const Process* this) {
  214. return this->isUserlandThread;
  215. }
  216. static inline bool Process_isThread(const Process* this) {
  217. return Process_isUserlandThread(this) || Process_isKernelThread(this);
  218. }
  219. #define CMDLINE_HIGHLIGHT_FLAG_SEPARATOR 0x00000001
  220. #define CMDLINE_HIGHLIGHT_FLAG_BASENAME 0x00000002
  221. #define CMDLINE_HIGHLIGHT_FLAG_COMM 0x00000004
  222. #define CMDLINE_HIGHLIGHT_FLAG_DELETED 0x00000008
  223. #define CMDLINE_HIGHLIGHT_FLAG_PREFIXDIR 0x00000010
  224. void Process_fillStarttimeBuffer(Process* this);
  225. void Process_done(Process* this);
  226. extern const ProcessClass Process_class;
  227. void Process_init(Process* this, const struct Machine_* host);
  228. const char* Process_rowGetSortKey(Row* super);
  229. bool Process_rowChangePriorityBy(Row* super, Arg delta);
  230. bool Process_rowSendSignal(Row* super, Arg sgn);
  231. bool Process_rowIsHighlighted(const Row* super);
  232. bool Process_rowIsVisible(const Row* super, const struct Table_* table);
  233. bool Process_rowMatchesFilter(const Row* super, const struct Table_* table);
  234. static inline int Process_pidEqualCompare(const void* v1, const void* v2) {
  235. return Row_idEqualCompare(v1, v2);
  236. }
  237. int Process_compareByKey_Base(const Process* p1, const Process* p2, ProcessField key);
  238. const char* Process_getCommand(const Process* this);
  239. void Process_updateComm(Process* this, const char* comm);
  240. void Process_updateCmdline(Process* this, const char* cmdline, int basenameStart, int basenameEnd);
  241. void Process_updateExe(Process* this, const char* exe);
  242. /* This function constructs the string that is displayed by
  243. * Process_writeCommand and also returned by Process_getCommand */
  244. void Process_makeCommandStr(Process* this, const struct Settings_ *settings);
  245. void Process_writeCommand(const Process* this, int attr, int baseAttr, RichString* str);
  246. void Process_updateCPUFieldWidths(float percentage);
  247. #endif