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