Row.h 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. #ifndef HEADER_Row
  2. #define HEADER_Row
  3. /*
  4. htop - Row.h
  5. (C) 2004-2015 Hisham H. Muhammad
  6. (C) 2023 htop dev team
  7. Released under the GNU GPLv2+, see the COPYING file
  8. in the source distribution for its full text.
  9. */
  10. #include <stdbool.h>
  11. #include <stddef.h>
  12. #include <stdint.h>
  13. #include <sys/types.h>
  14. #include "Object.h"
  15. #include "RichString.h"
  16. #include "RowField.h"
  17. extern uint8_t Row_fieldWidths[LAST_RESERVED_FIELD];
  18. #define ROW_MIN_PID_DIGITS 5
  19. #define ROW_MAX_PID_DIGITS 19
  20. #define ROW_MIN_UID_DIGITS 5
  21. #define ROW_MAX_UID_DIGITS 20
  22. extern int Row_pidDigits;
  23. extern int Row_uidDigits;
  24. struct Machine_; // IWYU pragma: keep
  25. struct Settings_; // IWYU pragma: keep
  26. struct Table_; // IWYU pragma: keep
  27. /* Class representing entities (such as processes) that can be
  28. * represented in a tabular form in the lower half of the htop
  29. * display. */
  30. typedef struct Row_ {
  31. /* Super object for emulated OOP */
  32. Object super;
  33. /* Pointer to quasi-global data */
  34. const struct Machine_* host;
  35. int id;
  36. int group;
  37. int parent;
  38. /* Has no known parent */
  39. bool isRoot;
  40. /* Whether the row was tagged by the user */
  41. bool tag;
  42. /* Whether to display this row */
  43. bool show;
  44. /* Whether this row was shown last cycle */
  45. bool wasShown;
  46. /* Whether to show children of this row in tree-mode */
  47. bool showChildren;
  48. /* Whether the row was updated during the last scan */
  49. bool updated;
  50. /*
  51. * Internal state for tree-mode.
  52. */
  53. int32_t indent;
  54. unsigned int tree_depth;
  55. /*
  56. * Internal time counts for showing new and exited processes.
  57. */
  58. uint64_t seenStampMs;
  59. uint64_t tombStampMs;
  60. } Row;
  61. typedef Row* (*Row_New)(const struct Machine_*);
  62. typedef void (*Row_WriteField)(const Row*, RichString*, RowField);
  63. typedef bool (*Row_IsHighlighted)(const Row*);
  64. typedef bool (*Row_IsVisible)(const Row*, const struct Table_*);
  65. typedef bool (*Row_MatchesFilter)(const Row*, const struct Table_*);
  66. typedef const char* (*Row_SortKeyString)(Row*);
  67. typedef int (*Row_CompareByParent)(const Row*, const Row*);
  68. int Row_compare(const void* v1, const void* v2);
  69. typedef struct RowClass_ {
  70. const ObjectClass super;
  71. const Row_IsHighlighted isHighlighted;
  72. const Row_IsVisible isVisible;
  73. const Row_WriteField writeField;
  74. const Row_MatchesFilter matchesFilter;
  75. const Row_SortKeyString sortKeyString;
  76. const Row_CompareByParent compareByParent;
  77. } RowClass;
  78. #define As_Row(this_) ((const RowClass*)((this_)->super.klass))
  79. #define Row_isHighlighted(r_) (As_Row(r_)->isHighlighted ? (As_Row(r_)->isHighlighted(r_)) : false)
  80. #define Row_isVisible(r_, t_) (As_Row(r_)->isVisible ? (As_Row(r_)->isVisible(r_, t_)) : true)
  81. #define Row_matchesFilter(r_, t_) (As_Row(r_)->matchesFilter ? (As_Row(r_)->matchesFilter(r_, t_)) : false)
  82. #define Row_sortKeyString(r_) (As_Row(r_)->sortKeyString ? (As_Row(r_)->sortKeyString(r_)) : "")
  83. #define Row_compareByParent(r1_, r2_) (As_Row(r1_)->compareByParent ? (As_Row(r1_)->compareByParent(r1_, r2_)) : Row_compareByParent_Base(r1_, r2_))
  84. #define ONE_K 1024UL
  85. #define ONE_M (ONE_K * ONE_K)
  86. #define ONE_G (ONE_M * ONE_K)
  87. #define ONE_T (1ULL * ONE_G * ONE_K)
  88. #define ONE_P (1ULL * ONE_T * ONE_K)
  89. #define ONE_DECIMAL_K 1000UL
  90. #define ONE_DECIMAL_M (ONE_DECIMAL_K * ONE_DECIMAL_K)
  91. #define ONE_DECIMAL_G (ONE_DECIMAL_M * ONE_DECIMAL_K)
  92. #define ONE_DECIMAL_T (1ULL * ONE_DECIMAL_G * ONE_DECIMAL_K)
  93. #define ONE_DECIMAL_P (1ULL * ONE_DECIMAL_T * ONE_DECIMAL_K)
  94. extern const RowClass Row_class;
  95. void Row_init(Row* this, const struct Machine_* host);
  96. void Row_done(Row* this);
  97. void Row_display(const Object* cast, RichString* out);
  98. void Row_toggleTag(Row* this);
  99. void Row_resetFieldWidths(void);
  100. void Row_updateFieldWidth(RowField key, size_t width);
  101. void Row_printLeftAlignedField(RichString* str, int attr, const char* content, unsigned int width);
  102. const char* RowField_alignedTitle(const struct Settings_* settings, RowField field);
  103. RowField RowField_keyAt(const struct Settings_* settings, int at);
  104. /* Sets the size of the PID column based on the passed PID */
  105. void Row_setPidColumnWidth(pid_t maxPid);
  106. /* Sets the size of the UID column based on the passed UID */
  107. void Row_setUidColumnWidth(uid_t maxUid);
  108. /* Takes number in kibibytes (base 1024). Prints 6 columns. */
  109. void Row_printKBytes(RichString* str, unsigned long long number, bool coloring);
  110. /* Takes number in bytes (base 1024). Prints 6 columns. */
  111. void Row_printBytes(RichString* str, unsigned long long number, bool coloring);
  112. /* Takes number as count (base 1000). Prints 12 columns. */
  113. void Row_printCount(RichString* str, unsigned long long number, bool coloring);
  114. /* Takes time in hundredths of a seconds. Prints 9 columns. */
  115. void Row_printTime(RichString* str, unsigned long long totalHundredths, bool coloring);
  116. /* Takes time in nanoseconds. Prints 9 columns. */
  117. void Row_printNanoseconds(RichString* str, unsigned long long totalNanoseconds, bool coloring);
  118. /* Takes rate in bare unit (base 1024) per second. Prints 12 columns. */
  119. void Row_printRate(RichString* str, double rate, bool coloring);
  120. int Row_printPercentage(float val, char* buffer, size_t n, uint8_t width, int* attr);
  121. void Row_display(const Object* cast, RichString* out);
  122. static inline int Row_idEqualCompare(const void* v1, const void* v2) {
  123. const int p1 = ((const Row*)v1)->id;
  124. const int p2 = ((const Row*)v2)->id;
  125. return p1 != p2; /* return zero when equal */
  126. }
  127. /* Routines used primarily with the tree view */
  128. static inline int Row_getGroupOrParent(const Row* this) {
  129. return this->group == this->id ? this->parent : this->group;
  130. }
  131. static inline bool Row_isChildOf(const Row* this, int id) {
  132. return id == Row_getGroupOrParent(this);
  133. }
  134. int Row_compareByParent_Base(const void* v1, const void* v2);
  135. #endif