From e91f3b6f69aa4f882fdb9afceaf06671453dd758 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Christian=20G=C3=B6ttsche?= Date: Tue, 15 Dec 2020 19:44:46 +0100 Subject: [PATCH 1/5] LinuxProcess: drop dead Process columns --- linux/LinuxProcess.c | 18 ------------------ linux/LinuxProcess.h | 20 -------------------- 2 files changed, 38 deletions(-) diff --git a/linux/LinuxProcess.c b/linux/LinuxProcess.c index 72955c8f4..24d735e13 100644 --- a/linux/LinuxProcess.c +++ b/linux/LinuxProcess.c @@ -40,7 +40,6 @@ ProcessFieldData Process_fields[] = { [SESSION] = { .name = "SESSION", .title = " SID ", .description = "Process's session ID", .flags = 0, }, [TTY_NR] = { .name = "TTY_NR", .title = "TTY ", .description = "Controlling terminal", .flags = 0, }, [TPGID] = { .name = "TPGID", .title = " TPGID ", .description = "Process ID of the fg process group of the controlling terminal", .flags = 0, }, - [FLAGS] = { .name = "FLAGS", .title = NULL, .description = NULL, .flags = 0, }, [MINFLT] = { .name = "MINFLT", .title = " MINFLT ", .description = "Number of minor faults which have not required loading a memory page from disk", .flags = 0, }, [CMINFLT] = { .name = "CMINFLT", .title = " CMINFLT ", .description = "Children processes' minor faults", .flags = 0, }, [MAJFLT] = { .name = "MAJFLT", .title = " MAJFLT ", .description = "Number of major faults which have required loading a memory page from disk", .flags = 0, }, @@ -51,24 +50,7 @@ ProcessFieldData Process_fields[] = { [CSTIME] = { .name = "CSTIME", .title = " CSTIME+ ", .description = "Children processes' system CPU time", .flags = 0, }, [PRIORITY] = { .name = "PRIORITY", .title = "PRI ", .description = "Kernel's internal priority for the process", .flags = 0, }, [NICE] = { .name = "NICE", .title = " NI ", .description = "Nice value (the higher the value, the more it lets other processes take priority)", .flags = 0, }, - [ITREALVALUE] = { .name = "ITREALVALUE", .title = NULL, .description = NULL, .flags = 0, }, [STARTTIME] = { .name = "STARTTIME", .title = "START ", .description = "Time the process was started", .flags = 0, }, - [VSIZE] = { .name = "VSIZE", .title = NULL, .description = NULL, .flags = 0, }, - [RSS] = { .name = "RSS", .title = NULL, .description = NULL, .flags = 0, }, - [RLIM] = { .name = "RLIM", .title = NULL, .description = NULL, .flags = 0, }, - [STARTCODE] = { .name = "STARTCODE", .title = NULL, .description = NULL, .flags = 0, }, - [ENDCODE] = { .name = "ENDCODE", .title = NULL, .description = NULL, .flags = 0, }, - [STARTSTACK] = { .name = "STARTSTACK", .title = NULL, .description = NULL, .flags = 0, }, - [KSTKESP] = { .name = "KSTKESP", .title = NULL, .description = NULL, .flags = 0, }, - [KSTKEIP] = { .name = "KSTKEIP", .title = NULL, .description = NULL, .flags = 0, }, - [SIGNAL] = { .name = "SIGNAL", .title = NULL, .description = NULL, .flags = 0, }, - [BLOCKED] = { .name = "BLOCKED", .title = NULL, .description = NULL, .flags = 0, }, - [SSIGIGNORE] = { .name = "SIGIGNORE", .title = NULL, .description = NULL, .flags = 0, }, - [SIGCATCH] = { .name = "SIGCATCH", .title = NULL, .description = NULL, .flags = 0, }, - [WCHAN] = { .name = "WCHAN", .title = NULL, .description = NULL, .flags = 0, }, - [NSWAP] = { .name = "NSWAP", .title = NULL, .description = NULL, .flags = 0, }, - [CNSWAP] = { .name = "CNSWAP", .title = NULL, .description = NULL, .flags = 0, }, - [EXIT_SIGNAL] = { .name = "EXIT_SIGNAL", .title = NULL, .description = NULL, .flags = 0, }, [PROCESSOR] = { .name = "PROCESSOR", .title = "CPU ", .description = "Id of the CPU the process last executed on", .flags = 0, }, [M_VIRT] = { .name = "M_VIRT", .title = " VIRT ", .description = "Total program size in virtual memory", .flags = 0, }, [M_RESIDENT] = { .name = "M_RESIDENT", .title = " RES ", .description = "Resident set size, size of the text and data sections, plus stack usage", .flags = 0, }, diff --git a/linux/LinuxProcess.h b/linux/LinuxProcess.h index b12bf4563..992e548da 100644 --- a/linux/LinuxProcess.h +++ b/linux/LinuxProcess.h @@ -29,26 +29,6 @@ in the source distribution for its full text. #define PROCESS_FLAG_LINUX_LRS_FIX 0x00010000 #define PROCESS_FLAG_LINUX_CWD 0x00020000 -typedef enum UnsupportedProcessField_ { - FLAGS = 9, - ITREALVALUE = 20, - VSIZE = 22, - RSS = 23, - RLIM = 24, - STARTCODE = 25, - ENDCODE = 26, - STARTSTACK = 27, - KSTKESP = 28, - KSTKEIP = 29, - SIGNAL = 30, - BLOCKED = 31, - SSIGIGNORE = 32, - SIGCATCH = 33, - WCHAN = 34, - NSWAP = 35, - CNSWAP = 36, - EXIT_SIGNAL = 37, -} UnsupportedProcessField; typedef enum LinuxProcessField_ { CMINFLT = 11, From effb0f6fc05789a9d71b4bbc18ecc4bbcf5f8cb4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Christian=20G=C3=B6ttsche?= Date: Tue, 15 Dec 2020 19:44:48 +0100 Subject: [PATCH 2/5] Rework enum ProcessField Use only one enum instead of a global and a platform specific one. Drop Platform_numberOfFields global variable. Set known size of Process_fields array --- AvailableColumnsPanel.c | 2 +- Makefile.am | 19 +++++++---- Process.c | 2 +- Process.h | 8 ++++- Settings.c | 6 ++-- darwin/DarwinProcess.c | 7 ++-- darwin/DarwinProcess.h | 7 +--- darwin/Platform.c | 2 -- darwin/Platform.h | 3 -- darwin/ProcessField.h | 16 +++++++++ dragonflybsd/DragonFlyBSDProcess.c | 7 ++-- dragonflybsd/DragonFlyBSDProcess.h | 9 +---- dragonflybsd/Platform.c | 2 -- dragonflybsd/Platform.h | 3 -- dragonflybsd/ProcessField.h | 17 ++++++++++ freebsd/FreeBSDProcess.c | 7 ++-- freebsd/FreeBSDProcess.h | 9 +---- freebsd/Platform.c | 2 -- freebsd/Platform.h | 4 --- freebsd/ProcessField.h | 17 ++++++++++ htop.c | 4 +-- linux/LinuxProcess.c | 7 ++-- linux/LinuxProcess.h | 50 +--------------------------- linux/Platform.c | 4 +-- linux/Platform.h | 2 -- linux/ProcessField.h | 53 ++++++++++++++++++++++++++++++ openbsd/OpenBSDProcess.c | 10 ++---- openbsd/OpenBSDProcess.h | 7 +--- openbsd/Platform.c | 2 -- openbsd/Platform.h | 4 --- openbsd/ProcessField.h | 15 +++++++++ solaris/Platform.c | 4 --- solaris/Platform.h | 5 --- solaris/ProcessField.h | 22 +++++++++++++ solaris/SolarisProcess.c | 7 ++-- solaris/SolarisProcess.h | 14 +------- unsupported/Platform.c | 2 -- unsupported/Platform.h | 2 -- unsupported/ProcessField.h | 15 +++++++++ unsupported/UnsupportedProcess.c | 3 +- unsupported/UnsupportedProcess.h | 2 +- 41 files changed, 208 insertions(+), 175 deletions(-) create mode 100644 darwin/ProcessField.h create mode 100644 dragonflybsd/ProcessField.h create mode 100644 freebsd/ProcessField.h create mode 100644 linux/ProcessField.h create mode 100644 openbsd/ProcessField.h create mode 100644 solaris/ProcessField.h create mode 100644 unsupported/ProcessField.h diff --git a/AvailableColumnsPanel.c b/AvailableColumnsPanel.c index 8945bd2c4..fb0357cfa 100644 --- a/AvailableColumnsPanel.c +++ b/AvailableColumnsPanel.c @@ -77,7 +77,7 @@ AvailableColumnsPanel* AvailableColumnsPanel_new(Panel* columns) { Panel_setHeader(super, "Available Columns"); - for (int i = 1; i < Platform_numberOfFields; i++) { + for (int i = 1; i < LAST_PROCESSFIELD; i++) { if (i != COMM && Process_fields[i].description) { char description[256]; xSnprintf(description, sizeof(description), "%s - %s", Process_fields[i].name, Process_fields[i].description); diff --git a/Makefile.am b/Makefile.am index 09790fa45..80527829e 100644 --- a/Makefile.am +++ b/Makefile.am @@ -133,6 +133,7 @@ linux_platform_headers = \ linux/LinuxProcessList.h \ linux/Platform.h \ linux/PressureStallMeter.h \ + linux/ProcessField.h \ linux/SELinuxMeter.h \ linux/SystemdMeter.h \ linux/ZramMeter.h \ @@ -164,9 +165,10 @@ endif # ------- freebsd_platform_headers = \ - freebsd/Platform.h \ freebsd/FreeBSDProcessList.h \ freebsd/FreeBSDProcess.h \ + freebsd/Platform.h \ + freebsd/ProcessField.h \ zfs/ZfsArcMeter.h \ zfs/ZfsCompressedArcMeter.h \ zfs/ZfsArcStats.h \ @@ -184,9 +186,10 @@ endif # ------------ dragonflybsd_platform_headers = \ - dragonflybsd/Platform.h \ dragonflybsd/DragonFlyBSDProcessList.h \ - dragonflybsd/DragonFlyBSDProcess.h + dragonflybsd/DragonFlyBSDProcess.h \ + dragonflybsd/Platform.h \ + dragonflybsd/ProcessField.h if HTOP_DRAGONFLYBSD AM_LDFLAGS += -lkvm -lkinfo @@ -200,9 +203,10 @@ endif # ------- openbsd_platform_headers = \ - openbsd/Platform.h \ openbsd/OpenBSDProcessList.h \ - openbsd/OpenBSDProcess.h + openbsd/OpenBSDProcess.h \ + openbsd/Platform.h \ + openbsd/ProcessField.h if HTOP_OPENBSD myhtopplatsources = openbsd/Platform.c openbsd/OpenBSDProcessList.c \ @@ -215,9 +219,10 @@ endif # ------ darwin_platform_headers = \ - darwin/Platform.h \ darwin/DarwinProcess.h \ darwin/DarwinProcessList.h \ + darwin/Platform.h \ + darwin/ProcessField.h \ zfs/ZfsArcMeter.h \ zfs/ZfsCompressedArcMeter.h \ zfs/ZfsArcStats.h \ @@ -237,6 +242,7 @@ endif solaris_platform_headers = \ solaris/Platform.h \ + solaris/ProcessField.h \ solaris/SolarisProcess.h \ solaris/SolarisProcessList.h \ zfs/ZfsArcMeter.h \ @@ -256,6 +262,7 @@ endif unsupported_platform_headers = \ unsupported/Platform.h \ + unsupported/ProcessField.h \ unsupported/UnsupportedProcess.h \ unsupported/UnsupportedProcessList.h diff --git a/Process.c b/Process.c index b1412d95d..2b39a2ab1 100644 --- a/Process.c +++ b/Process.c @@ -518,7 +518,7 @@ long Process_compare(const void* v1, const void* v2) { long Process_compareByKey_Base(const Process* p1, const Process* p2, ProcessField key) { int r; - switch ((int) key) { + switch (key) { case PERCENT_CPU: case PERCENT_NORM_CPU: return SPACESHIP_NUMBER(p2->percent_cpu, p1->percent_cpu); diff --git a/Process.h b/Process.h index b58135ed1..9a134bb31 100644 --- a/Process.h +++ b/Process.h @@ -13,6 +13,7 @@ in the source distribution for its full text. #include #include "Object.h" +#include "ProcessField.h" #include "RichString.h" @@ -45,6 +46,11 @@ typedef enum ProcessField_ { NLWP = 51, TGID = 52, PERCENT_NORM_CPU = 53, + + /* Platform specific fields, defined in ${platform}/ProcessField.h */ + PLATFORM_PROCESS_FIELDS + + LAST_PROCESSFIELD } ProcessField; typedef struct ProcessPidColumn_ { @@ -123,7 +129,7 @@ void Process_writeField(const Process* this, RichString* str, ProcessField field long Process_compare(const void* v1, const void* v2); void Process_delete(Object* cast); bool Process_isThread(const Process* this); -extern ProcessFieldData Process_fields[]; +extern ProcessFieldData Process_fields[LAST_PROCESSFIELD]; extern ProcessPidColumn Process_pidColumns[]; extern char Process_pidFormat[20]; diff --git a/Settings.c b/Settings.c index 9122955dc..96990a83d 100644 --- a/Settings.c +++ b/Settings.c @@ -96,10 +96,10 @@ static void readFields(ProcessField* fields, uint32_t* flags, const char* line) free(trim); int i, j; *flags = 0; - for (j = 0, i = 0; i < Platform_numberOfFields && ids[i]; i++) { + for (j = 0, i = 0; i < LAST_PROCESSFIELD && ids[i]; i++) { // This "+1" is for compatibility with the older enum format. int id = atoi(ids[i]) + 1; - if (id > 0 && id < Platform_numberOfFields && Process_fields[id].name) { + if (id > 0 && id < LAST_PROCESSFIELD && Process_fields[id].name) { fields[j] = id; *flags |= Process_fields[id].flags; j++; @@ -355,7 +355,7 @@ Settings* Settings_new(int initialCpuCount) { #ifdef HAVE_LIBHWLOC this->topologyAffinity = false; #endif - this->fields = xCalloc(Platform_numberOfFields + 1, sizeof(ProcessField)); + this->fields = xCalloc(LAST_PROCESSFIELD + 1, sizeof(ProcessField)); // TODO: turn 'fields' into a Vector, // (and ProcessFields into proper objects). this->flags = 0; diff --git a/darwin/DarwinProcess.c b/darwin/DarwinProcess.c index c463193b5..8f11e338b 100644 --- a/darwin/DarwinProcess.c +++ b/darwin/DarwinProcess.c @@ -18,7 +18,7 @@ in the source distribution for its full text. #include "Process.h" -ProcessFieldData Process_fields[] = { +ProcessFieldData Process_fields[LAST_PROCESSFIELD] = { [0] = { .name = "", .title = NULL, .description = NULL, .flags = 0, }, [PID] = { .name = "PID", .title = " PID ", .description = "Process/thread ID", .flags = 0, }, [COMM] = { .name = "Command", .title = "Command ", .description = "Command line", .flags = 0, }, @@ -45,7 +45,6 @@ ProcessFieldData Process_fields[] = { [NLWP] = { .name = "NLWP", .title = "NLWP ", .description = "Number of threads in the process", .flags = 0, }, [TGID] = { .name = "TGID", .title = " TGID ", .description = "Thread group ID (i.e. process ID)", .flags = 0, }, [TRANSLATED] = { .name = "TRANSLATED", .title = "T ", .description = "Translation info (T translated, N native)", .flags = 0, }, - [LAST_PROCESSFIELD] = { .name = "*** report bug! ***", .title = NULL, .description = NULL, .flags = 0, }, }; Process* DarwinProcess_new(const Settings* settings) { @@ -73,7 +72,7 @@ static void DarwinProcess_writeField(const Process* this, RichString* str, Proce char buffer[256]; buffer[255] = '\0'; int attr = CRT_colors[DEFAULT_COLOR]; int n = sizeof(buffer) - 1; - switch ((int) field) { + switch (field) { // add Platform-specific fields here case TRANSLATED: xSnprintf(buffer, n, "%c ", dp->translated ? 'T' : 'N'); break; default: @@ -87,7 +86,7 @@ static long DarwinProcess_compareByKey(const Process* v1, const Process* v2, Pro const DarwinProcess* p1 = (const DarwinProcess*)v1; const DarwinProcess* p2 = (const DarwinProcess*)v2; - switch ((int) key) { + switch (key) { // add Platform-specific fields here case TRANSLATED: return SPACESHIP_NUMBER(p1->translated, p2->translated); diff --git a/darwin/DarwinProcess.h b/darwin/DarwinProcess.h index 33dbcd14d..60b57dfa5 100644 --- a/darwin/DarwinProcess.h +++ b/darwin/DarwinProcess.h @@ -12,11 +12,6 @@ in the source distribution for its full text. #include "DarwinProcessList.h" #include "Settings.h" -typedef enum DarwinProcessFields_ { - // Add platform-specific fields here, with ids >= 100 - TRANSLATED = 100, - LAST_PROCESSFIELD = 101, -} DarwinProcessField; typedef struct DarwinProcess_ { Process super; @@ -29,7 +24,7 @@ typedef struct DarwinProcess_ { extern const ProcessClass DarwinProcess_class; -extern ProcessFieldData Process_fields[]; +extern ProcessFieldData Process_fields[LAST_PROCESSFIELD]; Process* DarwinProcess_new(const Settings* settings); diff --git a/darwin/Platform.c b/darwin/Platform.c index 235044f93..0fe2ac7f9 100644 --- a/darwin/Platform.c +++ b/darwin/Platform.c @@ -110,8 +110,6 @@ const MeterClass* const Platform_meterTypes[] = { NULL }; -int Platform_numberOfFields = LAST_PROCESSFIELD; - double Platform_timebaseToNS = 1.0; void Platform_init(void) { diff --git a/darwin/Platform.h b/darwin/Platform.h index 74b461907..1c743725a 100644 --- a/darwin/Platform.h +++ b/darwin/Platform.h @@ -19,12 +19,9 @@ in the source distribution for its full text. #include "ProcessLocksScreen.h" #include "SignalsPanel.h" -extern ProcessFieldData Process_fields[]; extern ProcessField Platform_defaultFields[]; -extern int Platform_numberOfFields; - extern double Platform_timebaseToNS; extern const SignalItem Platform_signals[]; diff --git a/darwin/ProcessField.h b/darwin/ProcessField.h new file mode 100644 index 000000000..5a8090fe6 --- /dev/null +++ b/darwin/ProcessField.h @@ -0,0 +1,16 @@ +#ifndef HEADER_DarwinProcessField +#define HEADER_DarwinProcessField +/* +htop - darwin/ProcessField.h +(C) 2020 htop dev team +Released under the GNU GPLv2, see the COPYING file +in the source distribution for its full text. +*/ + + +#define PLATFORM_PROCESS_FIELDS \ + TRANSLATED = 100, \ + // End of list + + +#endif /* HEADER_DarwinProcessField */ diff --git a/dragonflybsd/DragonFlyBSDProcess.c b/dragonflybsd/DragonFlyBSDProcess.c index 32ca12fd4..31211725b 100644 --- a/dragonflybsd/DragonFlyBSDProcess.c +++ b/dragonflybsd/DragonFlyBSDProcess.c @@ -29,7 +29,7 @@ const ProcessClass DragonFlyBSDProcess_class = { .compareByKey = DragonFlyBSDProcess_compareByKey }; -ProcessFieldData Process_fields[] = { +ProcessFieldData Process_fields[LAST_PROCESSFIELD] = { [0] = { .name = "", .title = NULL, .description = NULL, .flags = 0, }, [PID] = { .name = "PID", .title = " PID ", .description = "Process/thread ID", .flags = 0, }, [COMM] = { .name = "Command", .title = "Command ", .description = "Command line", .flags = 0, }, @@ -57,7 +57,6 @@ ProcessFieldData Process_fields[] = { [TGID] = { .name = "TGID", .title = " TGID ", .description = "Thread group ID (i.e. process ID)", .flags = 0, }, [JID] = { .name = "JID", .title = " JID ", .description = "Jail prison ID", .flags = 0, }, [JAIL] = { .name = "JAIL", .title = "JAIL ", .description = "Jail prison name", .flags = 0, }, - [LAST_PROCESSFIELD] = { .name = "*** report bug! ***", .title = NULL, .description = NULL, .flags = 0, }, }; ProcessPidColumn Process_pidColumns[] = { @@ -90,7 +89,7 @@ void DragonFlyBSDProcess_writeField(const Process* this, RichString* str, Proces char buffer[256]; buffer[255] = '\0'; int attr = CRT_colors[DEFAULT_COLOR]; int n = sizeof(buffer) - 1; - switch ((int) field) { + switch (field) { // add Platform-specific fields here case PID: xSnprintf(buffer, n, Process_pidFormat, (fp->kernel ? -1 : this->pid)); break; case JID: xSnprintf(buffer, n, Process_pidFormat, fp->jid); break; @@ -113,7 +112,7 @@ long DragonFlyBSDProcess_compareByKey(const Process* v1, const Process* v2, Proc const DragonFlyBSDProcess* p1 = (const DragonFlyBSDProcess*)v1; const DragonFlyBSDProcess* p2 = (const DragonFlyBSDProcess*)v2; - switch ((int) key) { + switch (key) { // add Platform-specific fields here case JID: return SPACESHIP_NUMBER(p1->jid, p2->jid); diff --git a/dragonflybsd/DragonFlyBSDProcess.h b/dragonflybsd/DragonFlyBSDProcess.h index 0bb173ec5..7d5d3e522 100644 --- a/dragonflybsd/DragonFlyBSDProcess.h +++ b/dragonflybsd/DragonFlyBSDProcess.h @@ -8,13 +8,6 @@ Released under the GNU GPLv2, see the COPYING file in the source distribution for its full text. */ -typedef enum DragonFlyBSDProcessFields { - // Add platform-specific fields here, with ids >= 100 - JID = 100, - JAIL = 101, - LAST_PROCESSFIELD = 102, -} DragonFlyBSDProcessField; - typedef struct DragonFlyBSDProcess_ { Process super; int kernel; @@ -29,7 +22,7 @@ typedef struct DragonFlyBSDProcess_ { extern const ProcessClass DragonFlyBSDProcess_class; -extern ProcessFieldData Process_fields[]; +extern ProcessFieldData Process_fields[LAST_PROCESSFIELD]; extern ProcessPidColumn Process_pidColumns[]; diff --git a/dragonflybsd/Platform.c b/dragonflybsd/Platform.c index 40a73487b..6f2d7bdc5 100644 --- a/dragonflybsd/Platform.c +++ b/dragonflybsd/Platform.c @@ -33,8 +33,6 @@ in the source distribution for its full text. ProcessField Platform_defaultFields[] = { PID, USER, PRIORITY, NICE, M_VIRT, M_RESIDENT, STATE, PERCENT_CPU, PERCENT_MEM, TIME, COMM, 0 }; -int Platform_numberOfFields = LAST_PROCESSFIELD; - const SignalItem Platform_signals[] = { { .name = " 0 Cancel", .number = 0 }, { .name = " 1 SIGHUP", .number = 1 }, diff --git a/dragonflybsd/Platform.h b/dragonflybsd/Platform.h index 5456539e2..51d4c2cf8 100644 --- a/dragonflybsd/Platform.h +++ b/dragonflybsd/Platform.h @@ -17,12 +17,9 @@ in the source distribution for its full text. #include "ProcessLocksScreen.h" #include "SignalsPanel.h" -extern ProcessFieldData Process_fields[]; extern ProcessField Platform_defaultFields[]; -extern int Platform_numberOfFields; - extern const SignalItem Platform_signals[]; extern const unsigned int Platform_numberOfSignals; diff --git a/dragonflybsd/ProcessField.h b/dragonflybsd/ProcessField.h new file mode 100644 index 000000000..a32e3d3bc --- /dev/null +++ b/dragonflybsd/ProcessField.h @@ -0,0 +1,17 @@ +#ifndef HEADER_DragonFlyBSDProcessField +#define HEADER_DragonFlyBSDProcessField +/* +htop - dragonflybsd/ProcessField.h +(C) 2020 htop dev team +Released under the GNU GPLv2, see the COPYING file +in the source distribution for its full text. +*/ + + +#define PLATFORM_PROCESS_FIELDS \ + JID = 100, \ + JAIL = 101, \ + // End of list + + +#endif /* HEADER_DragonFlyBSDProcessField */ diff --git a/freebsd/FreeBSDProcess.c b/freebsd/FreeBSDProcess.c index a7c079c04..ff4b2df13 100644 --- a/freebsd/FreeBSDProcess.c +++ b/freebsd/FreeBSDProcess.c @@ -18,7 +18,7 @@ in the source distribution for its full text. const char* const nodevStr = "nodev"; -ProcessFieldData Process_fields[] = { +ProcessFieldData Process_fields[LAST_PROCESSFIELD] = { [0] = { .name = "", .title = NULL, .description = NULL, .flags = 0, }, [PID] = { .name = "PID", .title = " PID ", .description = "Process/thread ID", .flags = 0, }, [COMM] = { .name = "Command", .title = "Command ", .description = "Command line", .flags = 0, }, @@ -47,7 +47,6 @@ ProcessFieldData Process_fields[] = { [TGID] = { .name = "TGID", .title = " TGID ", .description = "Thread group ID (i.e. process ID)", .flags = 0, }, [JID] = { .name = "JID", .title = " JID ", .description = "Jail prison ID", .flags = 0, }, [JAIL] = { .name = "JAIL", .title = "JAIL ", .description = "Jail prison name", .flags = 0, }, - [LAST_PROCESSFIELD] = { .name = "*** report bug! ***", .title = NULL, .description = NULL, .flags = 0, }, }; ProcessPidColumn Process_pidColumns[] = { @@ -80,7 +79,7 @@ static void FreeBSDProcess_writeField(const Process* this, RichString* str, Proc char buffer[256]; buffer[255] = '\0'; int attr = CRT_colors[DEFAULT_COLOR]; int n = sizeof(buffer) - 1; - switch ((int) field) { + switch (field) { // add FreeBSD-specific fields here case JID: xSnprintf(buffer, n, Process_pidFormat, fp->jid); break; case JAIL: { @@ -112,7 +111,7 @@ static long FreeBSDProcess_compareByKey(const Process* v1, const Process* v2, Pr const FreeBSDProcess* p1 = (const FreeBSDProcess*)v1; const FreeBSDProcess* p2 = (const FreeBSDProcess*)v2; - switch ((int) key) { + switch (key) { // add FreeBSD-specific fields here case JID: return SPACESHIP_NUMBER(p1->jid, p2->jid); diff --git a/freebsd/FreeBSDProcess.h b/freebsd/FreeBSDProcess.h index 891197646..96b70ec40 100644 --- a/freebsd/FreeBSDProcess.h +++ b/freebsd/FreeBSDProcess.h @@ -18,13 +18,6 @@ in the source distribution for its full text. extern const char* const nodevStr; -typedef enum FreeBSDProcessFields_ { - // Add platform-specific fields here, with ids >= 100 - JID = 100, - JAIL = 101, - LAST_PROCESSFIELD = 102, -} FreeBSDProcessField; - typedef struct FreeBSDProcess_ { Process super; int kernel; @@ -43,7 +36,7 @@ static inline bool Process_isUserlandThread(const Process* this) { extern const ProcessClass FreeBSDProcess_class; -extern ProcessFieldData Process_fields[]; +extern ProcessFieldData Process_fields[LAST_PROCESSFIELD]; extern ProcessPidColumn Process_pidColumns[]; diff --git a/freebsd/Platform.c b/freebsd/Platform.c index bc77cf460..3cec913de 100644 --- a/freebsd/Platform.c +++ b/freebsd/Platform.c @@ -49,8 +49,6 @@ in the source distribution for its full text. ProcessField Platform_defaultFields[] = { PID, USER, PRIORITY, NICE, M_VIRT, M_RESIDENT, STATE, PERCENT_CPU, PERCENT_MEM, TIME, COMM, 0 }; -int Platform_numberOfFields = LAST_PROCESSFIELD; - const SignalItem Platform_signals[] = { { .name = " 0 Cancel", .number = 0 }, { .name = " 1 SIGHUP", .number = 1 }, diff --git a/freebsd/Platform.h b/freebsd/Platform.h index 5b3b01952..793746e7e 100644 --- a/freebsd/Platform.h +++ b/freebsd/Platform.h @@ -19,12 +19,8 @@ in the source distribution for its full text. #include "SignalsPanel.h" -extern ProcessFieldData Process_fields[]; - extern ProcessField Platform_defaultFields[]; -extern int Platform_numberOfFields; - extern const SignalItem Platform_signals[]; extern const unsigned int Platform_numberOfSignals; diff --git a/freebsd/ProcessField.h b/freebsd/ProcessField.h new file mode 100644 index 000000000..cc8928267 --- /dev/null +++ b/freebsd/ProcessField.h @@ -0,0 +1,17 @@ +#ifndef HEADER_FreeBSDProcessField +#define HEADER_FreeBSDProcessField +/* +htop - freebsd/ProcessField.h +(C) 2020 htop dev team +Released under the GNU GPLv2, see the COPYING file +in the source distribution for its full text. +*/ + + +#define PLATFORM_PROCESS_FIELDS \ + JID = 100, \ + JAIL = 101, \ + // End of list + + +#endif /* HEADER_FreeBSDProcessField */ diff --git a/htop.c b/htop.c index 7d67a05ae..4b43ed2ac 100644 --- a/htop.c +++ b/htop.c @@ -125,14 +125,14 @@ static CommandLineSettings parseArguments(int argc, char** argv) { case 's': assert(optarg); /* please clang analyzer, cause optarg can be NULL in the 'u' case */ if (String_eq(optarg, "help")) { - for (int j = 1; j < Platform_numberOfFields; j++) { + for (int j = 1; j < LAST_PROCESSFIELD; j++) { const char* name = Process_fields[j].name; if (name) printf ("%s\n", name); } exit(0); } flags.sortKey = 0; - for (int j = 1; j < Platform_numberOfFields; j++) { + for (int j = 1; j < LAST_PROCESSFIELD; j++) { if (Process_fields[j].name == NULL) continue; if (String_eq(optarg, Process_fields[j].name)) { diff --git a/linux/LinuxProcess.c b/linux/LinuxProcess.c index 24d735e13..a8b62cc01 100644 --- a/linux/LinuxProcess.c +++ b/linux/LinuxProcess.c @@ -30,7 +30,7 @@ int pageSizeKB; /* Used to identify kernel threads in Comm and Exe columns */ static const char *const kthreadID = "KTHREAD"; -ProcessFieldData Process_fields[] = { +ProcessFieldData Process_fields[LAST_PROCESSFIELD] = { [0] = { .name = "", .title = NULL, .description = NULL, .flags = 0, }, [PID] = { .name = "PID", .title = " PID ", .description = "Process/thread ID", .flags = 0, }, [COMM] = { .name = "Command", .title = "Command ", .description = "Command line", .flags = 0, }, @@ -100,7 +100,6 @@ ProcessFieldData Process_fields[] = { [PROC_COMM] = { .name = "COMM", .title = "COMM ", .description = "comm string of the process from /proc/[pid]/comm", .flags = 0, }, [PROC_EXE] = { .name = "EXE", .title = "EXE ", .description = "Basename of exe of the process from /proc/[pid]/exe", .flags = 0, }, [CWD] = { .name ="CWD", .title = "CWD ", .description = "The current working directory of the process", .flags = PROCESS_FLAG_LINUX_CWD, }, - [LAST_PROCESSFIELD] = { .name = "*** report bug! ***", .title = NULL, .description = NULL, .flags = 0, }, }; ProcessPidColumn Process_pidColumns[] = { @@ -608,7 +607,7 @@ static void LinuxProcess_writeField(const Process* this, RichString* str, Proces char buffer[256]; buffer[255] = '\0'; int attr = CRT_colors[DEFAULT_COLOR]; size_t n = sizeof(buffer) - 1; - switch ((int)field) { + switch (field) { case TTY_NR: { if (lp->ttyDevice) { xSnprintf(buffer, n, "%-9s", lp->ttyDevice + 5 /* skip "/dev/" */); @@ -753,7 +752,7 @@ static long LinuxProcess_compareByKey(const Process* v1, const Process* v2, Proc const LinuxProcess* p1 = (const LinuxProcess*)v1; const LinuxProcess* p2 = (const LinuxProcess*)v2; - switch ((int) key) { + switch (key) { case M_DRS: return SPACESHIP_NUMBER(p2->m_drs, p1->m_drs); case M_DT: diff --git a/linux/LinuxProcess.h b/linux/LinuxProcess.h index 992e548da..16667827e 100644 --- a/linux/LinuxProcess.h +++ b/linux/LinuxProcess.h @@ -30,54 +30,6 @@ in the source distribution for its full text. #define PROCESS_FLAG_LINUX_CWD 0x00020000 -typedef enum LinuxProcessField_ { - CMINFLT = 11, - CMAJFLT = 13, - UTIME = 14, - STIME = 15, - CUTIME = 16, - CSTIME = 17, - M_SHARE = 41, - M_TRS = 42, - M_DRS = 43, - M_LRS = 44, - M_DT = 45, - #ifdef HAVE_OPENVZ - CTID = 100, - VPID = 101, - #endif - #ifdef HAVE_VSERVER - VXID = 102, - #endif - RCHAR = 103, - WCHAR = 104, - SYSCR = 105, - SYSCW = 106, - RBYTES = 107, - WBYTES = 108, - CNCLWB = 109, - IO_READ_RATE = 110, - IO_WRITE_RATE = 111, - IO_RATE = 112, - CGROUP = 113, - OOM = 114, - IO_PRIORITY = 115, - #ifdef HAVE_DELAYACCT - PERCENT_CPU_DELAY = 116, - PERCENT_IO_DELAY = 117, - PERCENT_SWAP_DELAY = 118, - #endif - M_PSS = 119, - M_SWAP = 120, - M_PSSWP = 121, - CTXT = 122, - SECATTR = 123, - PROC_COMM = 124, - PROC_EXE = 125, - CWD = 126, - LAST_PROCESSFIELD = 127, -} LinuxProcessField; - /* LinuxProcessMergedCommand is populated by LinuxProcess_makeCommandStr: It * contains the merged Command string, and the information needed by * LinuxProcess_writeCommand to color the string. str will be NULL for kernel @@ -175,7 +127,7 @@ extern int pageSize; extern int pageSizeKB; -extern ProcessFieldData Process_fields[]; +extern ProcessFieldData Process_fields[LAST_PROCESSFIELD]; extern ProcessPidColumn Process_pidColumns[]; diff --git a/linux/Platform.c b/linux/Platform.c index 590fc7aab..d048a804c 100644 --- a/linux/Platform.c +++ b/linux/Platform.c @@ -65,9 +65,7 @@ in the source distribution for its full text. #endif -ProcessField Platform_defaultFields[] = { PID, USER, PRIORITY, NICE, M_VIRT, M_RESIDENT, (int)M_SHARE, STATE, PERCENT_CPU, PERCENT_MEM, TIME, COMM, 0 }; - -int Platform_numberOfFields = LAST_PROCESSFIELD; +ProcessField Platform_defaultFields[] = { PID, USER, PRIORITY, NICE, M_VIRT, M_RESIDENT, M_SHARE, STATE, PERCENT_CPU, PERCENT_MEM, TIME, COMM, 0 }; const SignalItem Platform_signals[] = { { .name = " 0 Cancel", .number = 0 }, diff --git a/linux/Platform.h b/linux/Platform.h index 280b997c8..be0331b99 100644 --- a/linux/Platform.h +++ b/linux/Platform.h @@ -20,8 +20,6 @@ in the source distribution for its full text. extern ProcessField Platform_defaultFields[]; -extern int Platform_numberOfFields; - extern const SignalItem Platform_signals[]; extern const unsigned int Platform_numberOfSignals; diff --git a/linux/ProcessField.h b/linux/ProcessField.h new file mode 100644 index 000000000..6e2eff382 --- /dev/null +++ b/linux/ProcessField.h @@ -0,0 +1,53 @@ +#ifndef HEADER_LinuxProcessField +#define HEADER_LinuxProcessField +/* +htop - linux/ProcessField.h +(C) 2020 htop dev team +Released under the GNU GPLv2, see the COPYING file +in the source distribution for its full text. +*/ + + +#define PLATFORM_PROCESS_FIELDS \ + CMINFLT = 11, \ + CMAJFLT = 13, \ + UTIME = 14, \ + STIME = 15, \ + CUTIME = 16, \ + CSTIME = 17, \ + M_SHARE = 41, \ + M_TRS = 42, \ + M_DRS = 43, \ + M_LRS = 44, \ + M_DT = 45, \ + CTID = 100, \ + VPID = 101, \ + VXID = 102, \ + RCHAR = 103, \ + WCHAR = 104, \ + SYSCR = 105, \ + SYSCW = 106, \ + RBYTES = 107, \ + WBYTES = 108, \ + CNCLWB = 109, \ + IO_READ_RATE = 110, \ + IO_WRITE_RATE = 111, \ + IO_RATE = 112, \ + CGROUP = 113, \ + OOM = 114, \ + IO_PRIORITY = 115, \ + PERCENT_CPU_DELAY = 116, \ + PERCENT_IO_DELAY = 117, \ + PERCENT_SWAP_DELAY = 118, \ + M_PSS = 119, \ + M_SWAP = 120, \ + M_PSSWP = 121, \ + CTXT = 122, \ + SECATTR = 123, \ + PROC_COMM = 124, \ + PROC_EXE = 125, \ + CWD = 126, \ + // End of list + + +#endif /* HEADER_LinuxProcessField */ diff --git a/openbsd/OpenBSDProcess.c b/openbsd/OpenBSDProcess.c index 9756f9bb8..0ac08c660 100644 --- a/openbsd/OpenBSDProcess.c +++ b/openbsd/OpenBSDProcess.c @@ -16,7 +16,7 @@ in the source distribution for its full text. #include "XUtils.h" -ProcessFieldData Process_fields[] = { +ProcessFieldData Process_fields[LAST_PROCESSFIELD] = { [0] = { .name = "", .title = NULL, @@ -167,12 +167,6 @@ ProcessFieldData Process_fields[] = { .description = "Thread group ID (i.e. process ID)", .flags = 0, }, - [LAST_PROCESSFIELD] = { - .name = "*** report bug! ***", - .title = NULL, - .description = NULL, - .flags = 0, - }, }; ProcessPidColumn Process_pidColumns[] = { @@ -219,7 +213,7 @@ static long OpenBSDProcess_compareByKey(const Process* v1, const Process* v2, Pr // remove if actually used (void)p1; (void)p2; - switch ((int) key) { + switch (key) { // add OpenBSD-specific fields here default: return Process_compareByKey_Base(v1, v2, key); diff --git a/openbsd/OpenBSDProcess.h b/openbsd/OpenBSDProcess.h index 2d015136d..6b28025af 100644 --- a/openbsd/OpenBSDProcess.h +++ b/openbsd/OpenBSDProcess.h @@ -15,11 +15,6 @@ in the source distribution for its full text. #include "Settings.h" -typedef enum OpenBSDProcessFields_ { - // Add platform-specific fields here, with ids >= 100 - LAST_PROCESSFIELD = 100, -} OpenBSDProcessField; - typedef struct OpenBSDProcess_ { Process super; } OpenBSDProcess; @@ -30,7 +25,7 @@ typedef struct OpenBSDProcess_ { extern const ProcessClass OpenBSDProcess_class; -extern ProcessFieldData Process_fields[]; +extern ProcessFieldData Process_fields[LAST_PROCESSFIELD]; extern ProcessPidColumn Process_pidColumns[]; diff --git a/openbsd/Platform.c b/openbsd/Platform.c index dae807249..30a58ec41 100644 --- a/openbsd/Platform.c +++ b/openbsd/Platform.c @@ -44,8 +44,6 @@ in the source distribution for its full text. ProcessField Platform_defaultFields[] = { PID, USER, PRIORITY, NICE, M_VIRT, M_RESIDENT, STATE, PERCENT_CPU, PERCENT_MEM, TIME, COMM, 0 }; -int Platform_numberOfFields = LAST_PROCESSFIELD; - /* * See /usr/include/sys/signal.h */ diff --git a/openbsd/Platform.h b/openbsd/Platform.h index 0e2d435f8..4774f921d 100644 --- a/openbsd/Platform.h +++ b/openbsd/Platform.h @@ -20,12 +20,8 @@ in the source distribution for its full text. #include "SignalsPanel.h" -extern ProcessFieldData Process_fields[]; - extern ProcessField Platform_defaultFields[]; -extern int Platform_numberOfFields; - /* see /usr/include/sys/signal.h */ extern const SignalItem Platform_signals[]; diff --git a/openbsd/ProcessField.h b/openbsd/ProcessField.h new file mode 100644 index 000000000..be4e51e76 --- /dev/null +++ b/openbsd/ProcessField.h @@ -0,0 +1,15 @@ +#ifndef HEADER_OpenBSDProcessField +#define HEADER_OpenBSDProcessField +/* +htop - openbsd/ProcessField.h +(C) 2020 htop dev team +Released under the GNU GPLv2, see the COPYING file +in the source distribution for its full text. +*/ + + +#define PLATFORM_PROCESS_FIELDS \ + // End of list + + +#endif /* HEADER_OpenBSDProcessField */ diff --git a/solaris/Platform.c b/solaris/Platform.c index 014eaf557..b674998b1 100644 --- a/solaris/Platform.c +++ b/solaris/Platform.c @@ -119,10 +119,6 @@ const MeterClass* const Platform_meterTypes[] = { NULL }; -int Platform_numberOfFields = LAST_PROCESSFIELD; - -extern char Process_pidFormat[20]; - void Platform_init(void) { /* no platform-specific setup needed */ } diff --git a/solaris/Platform.h b/solaris/Platform.h index c3381152b..4daf71649 100644 --- a/solaris/Platform.h +++ b/solaris/Platform.h @@ -25,7 +25,6 @@ in the source distribution for its full text. #define kill(pid, signal) kill(pid / 1024, signal) -extern ProcessFieldData Process_fields[]; typedef struct var kvar_t; typedef struct envAccum_ { @@ -45,10 +44,6 @@ extern ProcessField Platform_defaultFields[]; extern const MeterClass* const Platform_meterTypes[]; -extern int Platform_numberOfFields; - -extern char Process_pidFormat[20]; - void Platform_init(void); void Platform_done(void); diff --git a/solaris/ProcessField.h b/solaris/ProcessField.h new file mode 100644 index 000000000..eb9f1577e --- /dev/null +++ b/solaris/ProcessField.h @@ -0,0 +1,22 @@ +#ifndef HEADER_SolarisProcessField +#define HEADER_SolarisProcessField +/* +htop - solaris/ProcessField.h +(C) 2020 htop dev team +Released under the GNU GPLv2, see the COPYING file +in the source distribution for its full text. +*/ + + +#define PLATFORM_PROCESS_FIELDS \ + ZONEID = 100, \ + ZONE = 101, \ + PROJID = 102, \ + TASKID = 103, \ + POOLID = 104, \ + CONTID = 105, \ + LWPID = 106, \ + // End of list + + +#endif /* HEADER_SolarisProcessField */ diff --git a/solaris/SolarisProcess.c b/solaris/SolarisProcess.c index f6470a3c2..3d4f95383 100644 --- a/solaris/SolarisProcess.c +++ b/solaris/SolarisProcess.c @@ -29,7 +29,7 @@ const ProcessClass SolarisProcess_class = { .compareByKey = SolarisProcess_compareByKey }; -ProcessFieldData Process_fields[] = { +ProcessFieldData Process_fields[LAST_PROCESSFIELD] = { [0] = { .name = "", .title = NULL, .description = NULL, .flags = 0, }, [PID] = { .name = "PID", .title = " PID ", .description = "Process/thread ID", .flags = 0, }, [COMM] = { .name = "Command", .title = "Command ", .description = "Command line", .flags = 0, }, @@ -62,7 +62,6 @@ ProcessFieldData Process_fields[] = { [POOLID] = { .name = "POOLID", .title = " POLID ", .description = "Pool ID", .flags = 0, }, [CONTID] = { .name = "CONTID", .title = " CNTID ", .description = "Contract ID", .flags = 0, }, [LWPID] = { .name = "LWPID", .title = " LWPID ", .description = "LWP ID", .flags = 0, }, - [LAST_PROCESSFIELD] = { .name = "*** report bug! ***", .title = NULL, .description = NULL, .flags = 0, }, }; ProcessPidColumn Process_pidColumns[] = { @@ -100,7 +99,7 @@ void SolarisProcess_writeField(const Process* this, RichString* str, ProcessFiel char buffer[256]; buffer[255] = '\0'; int attr = CRT_colors[DEFAULT_COLOR]; int n = sizeof(buffer) - 1; - switch ((int) field) { + switch (field) { // add Solaris-specific fields here case ZONEID: xSnprintf(buffer, n, Process_pidFormat, sp->zoneid); break; case PROJID: xSnprintf(buffer, n, Process_pidFormat, sp->projid); break; @@ -122,7 +121,7 @@ long SolarisProcess_compareByKey(const void* v1, const void* v2, ProcessField ke const SolarisProcess* p1 = (const SolarisProcess*)v1; const SolarisProcess* p2 = (const SolarisProcess*)v2; - switch ((int) key) { + switch (key) { case ZONEID: return SPACESHIP_NUMBER(p1->zoneid, p2->zoneid); case PROJID: diff --git a/solaris/SolarisProcess.h b/solaris/SolarisProcess.h index 12e6c99f1..5f79754a2 100644 --- a/solaris/SolarisProcess.h +++ b/solaris/SolarisProcess.h @@ -13,18 +13,6 @@ in the source distribution for its full text. #include #include -typedef enum SolarisProcessField_ { - // Add platform-specific fields here, with ids >= 100 - ZONEID = 100, - ZONE = 101, - PROJID = 102, - TASKID = 103, - POOLID = 104, - CONTID = 105, - LWPID = 106, - LAST_PROCESSFIELD = 107, -} SolarisProcessField; - typedef struct SolarisProcess_ { Process super; int kernel; @@ -46,7 +34,7 @@ typedef struct SolarisProcess_ { extern const ProcessClass SolarisProcess_class; -extern ProcessFieldData Process_fields[]; +extern ProcessFieldData Process_fields[LAST_PROCESSFIELD]; extern ProcessPidColumn Process_pidColumns[]; diff --git a/unsupported/Platform.c b/unsupported/Platform.c index c4c459907..75b005028 100644 --- a/unsupported/Platform.c +++ b/unsupported/Platform.c @@ -59,8 +59,6 @@ const MeterClass* const Platform_meterTypes[] = { NULL }; -int Platform_numberOfFields = 100; - ProcessPidColumn Process_pidColumns[] = { { .id = 0, .label = NULL }, }; diff --git a/unsupported/Platform.h b/unsupported/Platform.h index 2b4f0aead..7ba5f063f 100644 --- a/unsupported/Platform.h +++ b/unsupported/Platform.h @@ -23,8 +23,6 @@ extern ProcessField Platform_defaultFields[]; extern const MeterClass* const Platform_meterTypes[]; -extern int Platform_numberOfFields; - extern char Process_pidFormat[20]; extern ProcessPidColumn Process_pidColumns[]; diff --git a/unsupported/ProcessField.h b/unsupported/ProcessField.h new file mode 100644 index 000000000..8c07107d5 --- /dev/null +++ b/unsupported/ProcessField.h @@ -0,0 +1,15 @@ +#ifndef HEADER_UnsupportedProcessField +#define HEADER_UnsupportedProcessField +/* +htop - unsupported/ProcessField.h +(C) 2020 htop dev team +Released under the GNU GPLv2, see the COPYING file +in the source distribution for its full text. +*/ + + +#define PLATFORM_PROCESS_FIELDS \ + // End of list + + +#endif /* HEADER_UnsupportedProcessField */ diff --git a/unsupported/UnsupportedProcess.c b/unsupported/UnsupportedProcess.c index edac2d621..153f86f61 100644 --- a/unsupported/UnsupportedProcess.c +++ b/unsupported/UnsupportedProcess.c @@ -9,7 +9,7 @@ in the source distribution for its full text. #include "UnsupportedProcess.h" #include -ProcessFieldData Process_fields[] = { +ProcessFieldData Process_fields[LAST_PROCESSFIELD] = { [0] = { .name = "", .title = NULL, .description = NULL, .flags = 0, }, [PID] = { .name = "PID", .title = " PID ", .description = "Process/thread ID", .flags = 0, }, [COMM] = { .name = "Command", .title = "Command ", .description = "Command line", .flags = 0, }, @@ -35,7 +35,6 @@ ProcessFieldData Process_fields[] = { [TIME] = { .name = "TIME", .title = " TIME+ ", .description = "Total time the process has spent in user and system time", .flags = 0, }, [NLWP] = { .name = "NLWP", .title = "NLWP ", .description = "Number of threads in the process", .flags = 0, }, [TGID] = { .name = "TGID", .title = " TGID ", .description = "Thread group ID (i.e. process ID)", .flags = 0, }, - [100] = { .name = "*** report bug! ***", .title = NULL, .description = NULL, .flags = 0, }, }; Process* UnsupportedProcess_new(Settings* settings) { diff --git a/unsupported/UnsupportedProcess.h b/unsupported/UnsupportedProcess.h index 8c220944a..a9be326e1 100644 --- a/unsupported/UnsupportedProcess.h +++ b/unsupported/UnsupportedProcess.h @@ -11,7 +11,7 @@ in the source distribution for its full text. #define Process_delete UnsupportedProcess_delete -extern ProcessFieldData Process_fields[]; +extern ProcessFieldData Process_fields[LAST_PROCESSFIELD]; Process* UnsupportedProcess_new(Settings* settings); From bd5cf1ba3a28ef61bdce13d6299ec61c788547d9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Christian=20G=C3=B6ttsche?= Date: Tue, 15 Dec 2020 19:44:52 +0100 Subject: [PATCH 3/5] Merge Process_pidColumns into Process_fields and rework auto-fit for PID-like columns --- Process.c | 26 +++++--------- Process.h | 12 +++---- ProcessList.c | 28 ++++++++------- darwin/DarwinProcess.c | 14 ++++---- darwin/DarwinProcess.h | 2 +- darwin/Platform.c | 10 ------ darwin/Platform.h | 2 -- dragonflybsd/DragonFlyBSDProcess.c | 31 ++++++---------- dragonflybsd/DragonFlyBSDProcess.h | 4 +-- freebsd/FreeBSDProcess.c | 29 +++++---------- freebsd/FreeBSDProcess.h | 4 +-- linux/LinuxProcess.c | 31 +++++----------- linux/LinuxProcess.h | 4 +-- openbsd/OpenBSDProcess.c | 30 +++++++--------- openbsd/OpenBSDProcess.h | 4 +-- solaris/SolarisProcess.c | 58 +++++++++++------------------- solaris/SolarisProcess.h | 4 +-- unsupported/Platform.c | 4 --- unsupported/Platform.h | 4 --- unsupported/UnsupportedProcess.c | 14 ++++---- unsupported/UnsupportedProcess.h | 2 +- 21 files changed, 112 insertions(+), 205 deletions(-) diff --git a/Process.c b/Process.c index 2b39a2ab1..fc105cbeb 100644 --- a/Process.c +++ b/Process.c @@ -38,23 +38,15 @@ in the source distribution for its full text. static uid_t Process_getuid = (uid_t)-1; -char Process_pidFormat[20] = "%7d "; - -static char Process_titleBuffer[20][20]; +int Process_pidDigits = 7; void Process_setupColumnWidths() { int maxPid = Platform_getMaxPid(); if (maxPid == -1) return; - int digits = ceil(log10(maxPid)); - assert(digits < 20); - for (int i = 0; Process_pidColumns[i].label; i++) { - assert(i < 20); - xSnprintf(Process_titleBuffer[i], 20, "%*s ", digits, Process_pidColumns[i].label); - Process_fields[Process_pidColumns[i].id].title = Process_titleBuffer[i]; - } - xSnprintf(Process_pidFormat, sizeof(Process_pidFormat), "%%%dd ", digits); + Process_pidDigits = ceil(log10(maxPid)); + assert(Process_pidDigits <= PROCESS_MAX_PID_DIGITS); } void Process_humanNumber(RichString* str, unsigned long long number, bool coloring) { @@ -338,9 +330,9 @@ void Process_writeField(const Process* this, RichString* str, ProcessField field break; } case NLWP: xSnprintf(buffer, n, "%4ld ", this->nlwp); break; - case PGRP: xSnprintf(buffer, n, Process_pidFormat, this->pgrp); break; - case PID: xSnprintf(buffer, n, Process_pidFormat, this->pid); break; - case PPID: xSnprintf(buffer, n, Process_pidFormat, this->ppid); break; + case PGRP: xSnprintf(buffer, n, "%*d ", Process_pidDigits, this->pgrp); break; + case PID: xSnprintf(buffer, n, "%*d ", Process_pidDigits, this->pid); break; + case PPID: xSnprintf(buffer, n, "%*d ", Process_pidDigits, this->ppid); break; case PRIORITY: { if(this->priority <= -100) xSnprintf(buffer, n, " RT "); @@ -349,7 +341,7 @@ void Process_writeField(const Process* this, RichString* str, ProcessField field break; } case PROCESSOR: xSnprintf(buffer, n, "%3d ", Settings_cpuId(this->settings, this->processor)); break; - case SESSION: xSnprintf(buffer, n, Process_pidFormat, this->session); break; + case SESSION: xSnprintf(buffer, n, "%*d ", Process_pidDigits, this->session); break; case STARTTIME: xSnprintf(buffer, n, "%s", this->starttime_show); break; case STATE: { xSnprintf(buffer, n, "%c ", this->state); @@ -365,8 +357,8 @@ void Process_writeField(const Process* this, RichString* str, ProcessField field } case ST_UID: xSnprintf(buffer, n, "%5d ", this->st_uid); break; case TIME: Process_printTime(str, this->time); return; - case TGID: xSnprintf(buffer, n, Process_pidFormat, this->tgid); break; - case TPGID: xSnprintf(buffer, n, Process_pidFormat, this->tpgid); break; + case TGID: xSnprintf(buffer, n, "%*d ", Process_pidDigits, this->tgid); break; + case TPGID: xSnprintf(buffer, n, "%*d ", Process_pidDigits, this->tpgid); break; case TTY_NR: xSnprintf(buffer, n, "%3u:%3u ", major(this->tty_nr), minor(this->tty_nr)); break; case USER: { if (Process_getuid != this->st_uid) diff --git a/Process.h b/Process.h index 9a134bb31..91d394094 100644 --- a/Process.h +++ b/Process.h @@ -53,11 +53,6 @@ typedef enum ProcessField_ { LAST_PROCESSFIELD } ProcessField; -typedef struct ProcessPidColumn_ { - int id; - const char* label; -} ProcessPidColumn; - struct Settings_; typedef struct Process_ { @@ -122,6 +117,7 @@ typedef struct ProcessFieldData_ { const char* title; const char* description; uint32_t flags; + bool pidColumn; } ProcessFieldData; // Implemented in platform-specific code: @@ -129,9 +125,9 @@ void Process_writeField(const Process* this, RichString* str, ProcessField field long Process_compare(const void* v1, const void* v2); void Process_delete(Object* cast); bool Process_isThread(const Process* this); -extern ProcessFieldData Process_fields[LAST_PROCESSFIELD]; -extern ProcessPidColumn Process_pidColumns[]; -extern char Process_pidFormat[20]; +extern const ProcessFieldData Process_fields[LAST_PROCESSFIELD]; +#define PROCESS_MAX_PID_DIGITS 19 +extern int Process_pidDigits; typedef Process*(*Process_New)(const struct Settings_*); typedef void (*Process_WriteField)(const Process*, RichString*, ProcessField); diff --git a/ProcessList.c b/ProcessList.c index 02ce373f5..d0bd89fe1 100644 --- a/ProcessList.c +++ b/ProcessList.c @@ -79,6 +79,20 @@ void ProcessList_setPanel(ProcessList* this, Panel* panel) { this->panel = panel; } +static const char* alignedProcessFieldTitle(ProcessField field) { + const char* title = Process_fields[field].title; + if (!title) + return "- "; + + if (!Process_fields[field].pidColumn) + return title; + + static char titleBuffer[PROCESS_MAX_PID_DIGITS + /* space */ 1 + /* null-terminator */ + 1]; + xSnprintf(titleBuffer, sizeof(titleBuffer), "%*s ", Process_pidDigits, title); + + return titleBuffer; +} + void ProcessList_printHeader(ProcessList* this, RichString* header) { RichString_prune(header); @@ -88,11 +102,6 @@ void ProcessList_printHeader(ProcessList* this, RichString* header) { ProcessField key = Settings_getActiveSortKey(settings); for (int i = 0; fields[i]; i++) { - const char* field = Process_fields[fields[i]].title; - if (!field) { - field = "- "; - } - int color; if (settings->treeView && settings->treeViewAlwaysByPID) { color = CRT_colors[PANEL_HEADER_FOCUS]; @@ -102,7 +111,7 @@ void ProcessList_printHeader(ProcessList* this, RichString* header) { color = CRT_colors[PANEL_HEADER_FOCUS]; } - RichString_appendWide(header, color, field); + RichString_appendWide(header, color, alignedProcessFieldTitle(fields[i])); if (COMM == fields[i] && settings->showMergedCommand) { RichString_appendAscii(header, color, "(merged)"); } @@ -456,12 +465,7 @@ ProcessField ProcessList_keyAt(const ProcessList* this, int at) { const ProcessField* fields = this->settings->fields; ProcessField field; for (int i = 0; (field = fields[i]); i++) { - const char* title = Process_fields[field].title; - if (!title) { - title = "- "; - } - - int len = strlen(title); + int len = strlen(alignedProcessFieldTitle(field)); if (at >= x && at <= x + len) { return field; } diff --git a/darwin/DarwinProcess.c b/darwin/DarwinProcess.c index 8f11e338b..94bb193e0 100644 --- a/darwin/DarwinProcess.c +++ b/darwin/DarwinProcess.c @@ -18,16 +18,16 @@ in the source distribution for its full text. #include "Process.h" -ProcessFieldData Process_fields[LAST_PROCESSFIELD] = { +const ProcessFieldData Process_fields[LAST_PROCESSFIELD] = { [0] = { .name = "", .title = NULL, .description = NULL, .flags = 0, }, - [PID] = { .name = "PID", .title = " PID ", .description = "Process/thread ID", .flags = 0, }, + [PID] = { .name = "PID", .title = "PID", .description = "Process/thread ID", .flags = 0, .pidColumn = true, }, [COMM] = { .name = "Command", .title = "Command ", .description = "Command line", .flags = 0, }, [STATE] = { .name = "STATE", .title = "S ", .description = "Process state (S sleeping, R running, D disk, Z zombie, T traced, W paging)", .flags = 0, }, - [PPID] = { .name = "PPID", .title = " PPID ", .description = "Parent process ID", .flags = 0, }, - [PGRP] = { .name = "PGRP", .title = " PGRP ", .description = "Process group ID", .flags = 0, }, - [SESSION] = { .name = "SESSION", .title = " SID ", .description = "Process's session ID", .flags = 0, }, + [PPID] = { .name = "PPID", .title = "PPID", .description = "Parent process ID", .flags = 0, .pidColumn = true, }, + [PGRP] = { .name = "PGRP", .title = "PGRP", .description = "Process group ID", .flags = 0, .pidColumn = true, }, + [SESSION] = { .name = "SESSION", .title = "SID", .description = "Process's session ID", .flags = 0, .pidColumn = true, }, [TTY_NR] = { .name = "TTY_NR", .title = " TTY ", .description = "Controlling terminal", .flags = 0, }, - [TPGID] = { .name = "TPGID", .title = " TPGID ", .description = "Process ID of the fg process group of the controlling terminal", .flags = 0, }, + [TPGID] = { .name = "TPGID", .title = "TPGID", .description = "Process ID of the fg process group of the controlling terminal", .flags = 0, .pidColumn = true, }, [MINFLT] = { .name = "MINFLT", .title = " MINFLT ", .description = "Number of minor faults which have not required loading a memory page from disk", .flags = 0, }, [MAJFLT] = { .name = "MAJFLT", .title = " MAJFLT ", .description = "Number of major faults which have required loading a memory page from disk", .flags = 0, }, [PRIORITY] = { .name = "PRIORITY", .title = "PRI ", .description = "Kernel's internal priority for the process", .flags = 0, }, @@ -43,7 +43,7 @@ ProcessFieldData Process_fields[LAST_PROCESSFIELD] = { [USER] = { .name = "USER", .title = "USER ", .description = "Username of the process owner (or user ID if name cannot be determined)", .flags = 0, }, [TIME] = { .name = "TIME", .title = " TIME+ ", .description = "Total time the process has spent in user and system time", .flags = 0, }, [NLWP] = { .name = "NLWP", .title = "NLWP ", .description = "Number of threads in the process", .flags = 0, }, - [TGID] = { .name = "TGID", .title = " TGID ", .description = "Thread group ID (i.e. process ID)", .flags = 0, }, + [TGID] = { .name = "TGID", .title = "TGID", .description = "Thread group ID (i.e. process ID)", .flags = 0, .pidColumn = true, }, [TRANSLATED] = { .name = "TRANSLATED", .title = "T ", .description = "Translation info (T translated, N native)", .flags = 0, }, }; diff --git a/darwin/DarwinProcess.h b/darwin/DarwinProcess.h index 60b57dfa5..21da34af9 100644 --- a/darwin/DarwinProcess.h +++ b/darwin/DarwinProcess.h @@ -24,7 +24,7 @@ typedef struct DarwinProcess_ { extern const ProcessClass DarwinProcess_class; -extern ProcessFieldData Process_fields[LAST_PROCESSFIELD]; +extern const ProcessFieldData Process_fields[LAST_PROCESSFIELD]; Process* DarwinProcess_new(const Settings* settings); diff --git a/darwin/Platform.c b/darwin/Platform.c index 0fe2ac7f9..13ebceb17 100644 --- a/darwin/Platform.c +++ b/darwin/Platform.c @@ -166,16 +166,6 @@ int Platform_getMaxPid() { return 99999; } -ProcessPidColumn Process_pidColumns[] = { - { .id = PID, .label = "PID" }, - { .id = PPID, .label = "PPID" }, - { .id = TPGID, .label = "TPGID" }, - { .id = TGID, .label = "TGID" }, - { .id = PGRP, .label = "PGRP" }, - { .id = SESSION, .label = "SID" }, - { .id = 0, .label = NULL }, -}; - static double Platform_setCPUAverageValues(Meter* mtr) { const ProcessList* dpl = mtr->pl; int cpus = dpl->cpuCount; diff --git a/darwin/Platform.h b/darwin/Platform.h index 1c743725a..23132bb23 100644 --- a/darwin/Platform.h +++ b/darwin/Platform.h @@ -42,8 +42,6 @@ void Platform_getLoadAverage(double* one, double* five, double* fifteen); int Platform_getMaxPid(void); -extern ProcessPidColumn Process_pidColumns[]; - double Platform_setCPUValues(Meter* mtr, int cpu); void Platform_setMemoryValues(Meter* mtr); diff --git a/dragonflybsd/DragonFlyBSDProcess.c b/dragonflybsd/DragonFlyBSDProcess.c index 31211725b..20fe5e78b 100644 --- a/dragonflybsd/DragonFlyBSDProcess.c +++ b/dragonflybsd/DragonFlyBSDProcess.c @@ -29,16 +29,16 @@ const ProcessClass DragonFlyBSDProcess_class = { .compareByKey = DragonFlyBSDProcess_compareByKey }; -ProcessFieldData Process_fields[LAST_PROCESSFIELD] = { +const ProcessFieldData Process_fields[LAST_PROCESSFIELD] = { [0] = { .name = "", .title = NULL, .description = NULL, .flags = 0, }, - [PID] = { .name = "PID", .title = " PID ", .description = "Process/thread ID", .flags = 0, }, + [PID] = { .name = "PID", .title = "PID", .description = "Process/thread ID", .flags = 0, .pidColumn = true, }, [COMM] = { .name = "Command", .title = "Command ", .description = "Command line", .flags = 0, }, [STATE] = { .name = "STATE", .title = "S ", .description = "Process state (S sleeping (<20s), I Idle, Q Queued for Run, R running, D disk, Z zombie, T traced, W paging, B Blocked, A AskedPage, C Core, J Jailed)", .flags = 0, }, - [PPID] = { .name = "PPID", .title = " PPID ", .description = "Parent process ID", .flags = 0, }, - [PGRP] = { .name = "PGRP", .title = " PGRP ", .description = "Process group ID", .flags = 0, }, - [SESSION] = { .name = "SESSION", .title = " SID ", .description = "Process's session ID", .flags = 0, }, + [PPID] = { .name = "PPID", .title = "PPID", .description = "Parent process ID", .flags = 0, .pidColumn = true, }, + [PGRP] = { .name = "PGRP", .title = "PGRP", .description = "Process group ID", .flags = 0, .pidColumn = true, }, + [SESSION] = { .name = "SESSION", .title = "SID", .description = "Process's session ID", .flags = 0, .pidColumn = true, }, [TTY_NR] = { .name = "TTY_NR", .title = " TTY ", .description = "Controlling terminal", .flags = 0, }, - [TPGID] = { .name = "TPGID", .title = " TPGID ", .description = "Process ID of the fg process group of the controlling terminal", .flags = 0, }, + [TPGID] = { .name = "TPGID", .title = "TPGID", .description = "Process ID of the fg process group of the controlling terminal", .flags = 0, .pidColumn = true, }, [MINFLT] = { .name = "MINFLT", .title = " MINFLT ", .description = "Number of minor faults which have not required loading a memory page from disk", .flags = 0, }, [MAJFLT] = { .name = "MAJFLT", .title = " MAJFLT ", .description = "Number of major faults which have required loading a memory page from disk", .flags = 0, }, [PRIORITY] = { .name = "PRIORITY", .title = "PRI ", .description = "Kernel's internal priority for the process", .flags = 0, }, @@ -54,22 +54,11 @@ ProcessFieldData Process_fields[LAST_PROCESSFIELD] = { [USER] = { .name = "USER", .title = "USER ", .description = "Username of the process owner (or user ID if name cannot be determined)", .flags = 0, }, [TIME] = { .name = "TIME", .title = " TIME+ ", .description = "Total time the process has spent in user and system time", .flags = 0, }, [NLWP] = { .name = "NLWP", .title = "NLWP ", .description = "Number of threads in the process", .flags = 0, }, - [TGID] = { .name = "TGID", .title = " TGID ", .description = "Thread group ID (i.e. process ID)", .flags = 0, }, - [JID] = { .name = "JID", .title = " JID ", .description = "Jail prison ID", .flags = 0, }, + [TGID] = { .name = "TGID", .title = "TGID", .description = "Thread group ID (i.e. process ID)", .flags = 0, .pidColumn = true, }, + [JID] = { .name = "JID", .title = "JID", .description = "Jail prison ID", .flags = 0, .pidColumn = true, }, [JAIL] = { .name = "JAIL", .title = "JAIL ", .description = "Jail prison name", .flags = 0, }, }; -ProcessPidColumn Process_pidColumns[] = { - { .id = JID, .label = "JID" }, - { .id = PID, .label = "PID" }, - { .id = PPID, .label = "PPID" }, - { .id = TPGID, .label = "TPGID" }, - { .id = TGID, .label = "TGID" }, - { .id = PGRP, .label = "PGRP" }, - { .id = SESSION, .label = "SID" }, - { .id = 0, .label = NULL }, -}; - Process* DragonFlyBSDProcess_new(const Settings* settings) { DragonFlyBSDProcess* this = xCalloc(1, sizeof(DragonFlyBSDProcess)); Object_setClass(this, Class(DragonFlyBSDProcess)); @@ -91,8 +80,8 @@ void DragonFlyBSDProcess_writeField(const Process* this, RichString* str, Proces int n = sizeof(buffer) - 1; switch (field) { // add Platform-specific fields here - case PID: xSnprintf(buffer, n, Process_pidFormat, (fp->kernel ? -1 : this->pid)); break; - case JID: xSnprintf(buffer, n, Process_pidFormat, fp->jid); break; + case PID: xSnprintf(buffer, n, "%*d ", Process_pidDigits, (fp->kernel ? -1 : this->pid)); break; + case JID: xSnprintf(buffer, n, "%*d ", Process_pidDigits, fp->jid); break; case JAIL: { xSnprintf(buffer, n, "%-11s ", fp->jname); if (buffer[11] != '\0') { diff --git a/dragonflybsd/DragonFlyBSDProcess.h b/dragonflybsd/DragonFlyBSDProcess.h index 7d5d3e522..1527417bc 100644 --- a/dragonflybsd/DragonFlyBSDProcess.h +++ b/dragonflybsd/DragonFlyBSDProcess.h @@ -22,9 +22,7 @@ typedef struct DragonFlyBSDProcess_ { extern const ProcessClass DragonFlyBSDProcess_class; -extern ProcessFieldData Process_fields[LAST_PROCESSFIELD]; - -extern ProcessPidColumn Process_pidColumns[]; +extern const ProcessFieldData Process_fields[LAST_PROCESSFIELD]; Process* DragonFlyBSDProcess_new(const Settings* settings); diff --git a/freebsd/FreeBSDProcess.c b/freebsd/FreeBSDProcess.c index ff4b2df13..01edb8eb4 100644 --- a/freebsd/FreeBSDProcess.c +++ b/freebsd/FreeBSDProcess.c @@ -18,16 +18,16 @@ in the source distribution for its full text. const char* const nodevStr = "nodev"; -ProcessFieldData Process_fields[LAST_PROCESSFIELD] = { +const ProcessFieldData Process_fields[LAST_PROCESSFIELD] = { [0] = { .name = "", .title = NULL, .description = NULL, .flags = 0, }, - [PID] = { .name = "PID", .title = " PID ", .description = "Process/thread ID", .flags = 0, }, + [PID] = { .name = "PID", .title = "PID", .description = "Process/thread ID", .flags = 0, .pidColumn = true, }, [COMM] = { .name = "Command", .title = "Command ", .description = "Command line", .flags = 0, }, [STATE] = { .name = "STATE", .title = "S ", .description = "Process state (S sleeping, R running, D disk, Z zombie, T traced, W paging)", .flags = 0, }, - [PPID] = { .name = "PPID", .title = " PPID ", .description = "Parent process ID", .flags = 0, }, - [PGRP] = { .name = "PGRP", .title = " PGRP ", .description = "Process group ID", .flags = 0, }, - [SESSION] = { .name = "SESSION", .title = " SID ", .description = "Process's session ID", .flags = 0, }, + [PPID] = { .name = "PPID", .title = "PPID", .description = "Parent process ID", .flags = 0, .pidColumn = true, }, + [PGRP] = { .name = "PGRP", .title = "PGRP", .description = "Process group ID", .flags = 0, .pidColumn = true, }, + [SESSION] = { .name = "SESSION", .title = "SID", .description = "Process's session ID", .flags = 0, .pidColumn = true, }, [TTY_NR] = { .name = "TTY_NR", .title = " TTY ", .description = "Controlling terminal", .flags = PROCESS_FLAG_FREEBSD_TTY, }, - [TPGID] = { .name = "TPGID", .title = " TPGID ", .description = "Process ID of the fg process group of the controlling terminal", .flags = 0, }, + [TPGID] = { .name = "TPGID", .title = "TPGID", .description = "Process ID of the fg process group of the controlling terminal", .flags = 0, .pidColumn = true, }, [MINFLT] = { .name = "MINFLT", .title = " MINFLT ", .description = "Number of minor faults which have not required loading a memory page from disk", .flags = 0, }, [MAJFLT] = { .name = "MAJFLT", .title = " MAJFLT ", .description = "Number of major faults which have required loading a memory page from disk", .flags = 0, }, [PRIORITY] = { .name = "PRIORITY", .title = "PRI ", .description = "Kernel's internal priority for the process", .flags = 0, }, @@ -44,22 +44,11 @@ ProcessFieldData Process_fields[LAST_PROCESSFIELD] = { [USER] = { .name = "USER", .title = "USER ", .description = "Username of the process owner (or user ID if name cannot be determined)", .flags = 0, }, [TIME] = { .name = "TIME", .title = " TIME+ ", .description = "Total time the process has spent in user and system time", .flags = 0, }, [NLWP] = { .name = "NLWP", .title = "NLWP ", .description = "Number of threads in the process", .flags = 0, }, - [TGID] = { .name = "TGID", .title = " TGID ", .description = "Thread group ID (i.e. process ID)", .flags = 0, }, - [JID] = { .name = "JID", .title = " JID ", .description = "Jail prison ID", .flags = 0, }, + [TGID] = { .name = "TGID", .title = "TGID", .description = "Thread group ID (i.e. process ID)", .flags = 0, .pidColumn = true, }, + [JID] = { .name = "JID", .title = "JID", .description = "Jail prison ID", .flags = 0, .pidColumn = true, }, [JAIL] = { .name = "JAIL", .title = "JAIL ", .description = "Jail prison name", .flags = 0, }, }; -ProcessPidColumn Process_pidColumns[] = { - { .id = JID, .label = "JID" }, - { .id = PID, .label = "PID" }, - { .id = PPID, .label = "PPID" }, - { .id = TPGID, .label = "TPGID" }, - { .id = TGID, .label = "TGID" }, - { .id = PGRP, .label = "PGRP" }, - { .id = SESSION, .label = "SID" }, - { .id = 0, .label = NULL }, -}; - Process* FreeBSDProcess_new(const Settings* settings) { FreeBSDProcess* this = xCalloc(1, sizeof(FreeBSDProcess)); Object_setClass(this, Class(FreeBSDProcess)); @@ -81,7 +70,7 @@ static void FreeBSDProcess_writeField(const Process* this, RichString* str, Proc int n = sizeof(buffer) - 1; switch (field) { // add FreeBSD-specific fields here - case JID: xSnprintf(buffer, n, Process_pidFormat, fp->jid); break; + case JID: xSnprintf(buffer, n, "%*d ", Process_pidDigits, fp->jid); break; case JAIL: { xSnprintf(buffer, n, "%-11s ", fp->jname); if (buffer[11] != '\0') { diff --git a/freebsd/FreeBSDProcess.h b/freebsd/FreeBSDProcess.h index 96b70ec40..6bf2c93df 100644 --- a/freebsd/FreeBSDProcess.h +++ b/freebsd/FreeBSDProcess.h @@ -36,9 +36,7 @@ static inline bool Process_isUserlandThread(const Process* this) { extern const ProcessClass FreeBSDProcess_class; -extern ProcessFieldData Process_fields[LAST_PROCESSFIELD]; - -extern ProcessPidColumn Process_pidColumns[]; +extern const ProcessFieldData Process_fields[LAST_PROCESSFIELD]; Process* FreeBSDProcess_new(const Settings* settings); diff --git a/linux/LinuxProcess.c b/linux/LinuxProcess.c index a8b62cc01..18679ff79 100644 --- a/linux/LinuxProcess.c +++ b/linux/LinuxProcess.c @@ -30,16 +30,16 @@ int pageSizeKB; /* Used to identify kernel threads in Comm and Exe columns */ static const char *const kthreadID = "KTHREAD"; -ProcessFieldData Process_fields[LAST_PROCESSFIELD] = { +const ProcessFieldData Process_fields[LAST_PROCESSFIELD] = { [0] = { .name = "", .title = NULL, .description = NULL, .flags = 0, }, - [PID] = { .name = "PID", .title = " PID ", .description = "Process/thread ID", .flags = 0, }, + [PID] = { .name = "PID", .title = "PID", .description = "Process/thread ID", .flags = 0, .pidColumn = true, }, [COMM] = { .name = "Command", .title = "Command ", .description = "Command line", .flags = 0, }, [STATE] = { .name = "STATE", .title = "S ", .description = "Process state (S sleeping, R running, D disk, Z zombie, T traced, W paging, I idle)", .flags = 0, }, - [PPID] = { .name = "PPID", .title = " PPID ", .description = "Parent process ID", .flags = 0, }, - [PGRP] = { .name = "PGRP", .title = " PGRP ", .description = "Process group ID", .flags = 0, }, - [SESSION] = { .name = "SESSION", .title = " SID ", .description = "Process's session ID", .flags = 0, }, + [PPID] = { .name = "PPID", .title = "PPID", .description = "Parent process ID", .flags = 0, .pidColumn = true, }, + [PGRP] = { .name = "PGRP", .title = "PGRP", .description = "Process group ID", .flags = 0, .pidColumn = true, }, + [SESSION] = { .name = "SESSION", .title = "SID", .description = "Process's session ID", .flags = 0, .pidColumn = true, }, [TTY_NR] = { .name = "TTY_NR", .title = "TTY ", .description = "Controlling terminal", .flags = 0, }, - [TPGID] = { .name = "TPGID", .title = " TPGID ", .description = "Process ID of the fg process group of the controlling terminal", .flags = 0, }, + [TPGID] = { .name = "TPGID", .title = "TPGID", .description = "Process ID of the fg process group of the controlling terminal", .flags = 0, .pidColumn = true, }, [MINFLT] = { .name = "MINFLT", .title = " MINFLT ", .description = "Number of minor faults which have not required loading a memory page from disk", .flags = 0, }, [CMINFLT] = { .name = "CMINFLT", .title = " CMINFLT ", .description = "Children processes' minor faults", .flags = 0, }, [MAJFLT] = { .name = "MAJFLT", .title = " MAJFLT ", .description = "Number of major faults which have required loading a memory page from disk", .flags = 0, }, @@ -66,10 +66,10 @@ ProcessFieldData Process_fields[LAST_PROCESSFIELD] = { [USER] = { .name = "USER", .title = "USER ", .description = "Username of the process owner (or user ID if name cannot be determined)", .flags = 0, }, [TIME] = { .name = "TIME", .title = " TIME+ ", .description = "Total time the process has spent in user and system time", .flags = 0, }, [NLWP] = { .name = "NLWP", .title = "NLWP ", .description = "Number of threads in the process", .flags = 0, }, - [TGID] = { .name = "TGID", .title = " TGID ", .description = "Thread group ID (i.e. process ID)", .flags = 0, }, + [TGID] = { .name = "TGID", .title = "TGID", .description = "Thread group ID (i.e. process ID)", .flags = 0, .pidColumn = true, }, #ifdef HAVE_OPENVZ [CTID] = { .name = "CTID", .title = " CTID ", .description = "OpenVZ container ID (a.k.a. virtual environment ID)", .flags = PROCESS_FLAG_LINUX_OPENVZ, }, - [VPID] = { .name = "VPID", .title = " VPID ", .description = "OpenVZ process ID", .flags = PROCESS_FLAG_LINUX_OPENVZ, }, + [VPID] = { .name = "VPID", .title = "VPID", .description = "OpenVZ process ID", .flags = PROCESS_FLAG_LINUX_OPENVZ, .pidColumn = true, }, #endif #ifdef HAVE_VSERVER [VXID] = { .name = "VXID", .title = " VXID ", .description = "VServer process ID", .flags = PROCESS_FLAG_LINUX_VSERVER, }, @@ -102,19 +102,6 @@ ProcessFieldData Process_fields[LAST_PROCESSFIELD] = { [CWD] = { .name ="CWD", .title = "CWD ", .description = "The current working directory of the process", .flags = PROCESS_FLAG_LINUX_CWD, }, }; -ProcessPidColumn Process_pidColumns[] = { - { .id = PID, .label = "PID" }, - { .id = PPID, .label = "PPID" }, - #ifdef HAVE_OPENVZ - { .id = VPID, .label = "VPID" }, - #endif - { .id = TPGID, .label = "TPGID" }, - { .id = TGID, .label = "TGID" }, - { .id = PGRP, .label = "PGRP" }, - { .id = SESSION, .label = "SID" }, - { .id = 0, .label = NULL }, -}; - /* This function returns the string displayed in Command column, so that sorting * happens on what is displayed - whether comm, full path, basename, etc.. So * this follows LinuxProcess_writeField(COMM) and LinuxProcess_writeCommand */ @@ -662,7 +649,7 @@ static void LinuxProcess_writeField(const Process* this, RichString* str, Proces } #ifdef HAVE_OPENVZ case CTID: xSnprintf(buffer, n, "%-8s ", lp->ctid ? lp->ctid : ""); break; - case VPID: xSnprintf(buffer, n, Process_pidFormat, lp->vpid); break; + case VPID: xSnprintf(buffer, n, "%*d ", Process_pidDigits, lp->vpid); break; #endif #ifdef HAVE_VSERVER case VXID: xSnprintf(buffer, n, "%5u ", lp->vxid); break; diff --git a/linux/LinuxProcess.h b/linux/LinuxProcess.h index 16667827e..719a4e9bf 100644 --- a/linux/LinuxProcess.h +++ b/linux/LinuxProcess.h @@ -127,9 +127,7 @@ extern int pageSize; extern int pageSizeKB; -extern ProcessFieldData Process_fields[LAST_PROCESSFIELD]; - -extern ProcessPidColumn Process_pidColumns[]; +extern const ProcessFieldData Process_fields[LAST_PROCESSFIELD]; extern const ProcessClass LinuxProcess_class; diff --git a/openbsd/OpenBSDProcess.c b/openbsd/OpenBSDProcess.c index 0ac08c660..642e9bea5 100644 --- a/openbsd/OpenBSDProcess.c +++ b/openbsd/OpenBSDProcess.c @@ -16,7 +16,7 @@ in the source distribution for its full text. #include "XUtils.h" -ProcessFieldData Process_fields[LAST_PROCESSFIELD] = { +const ProcessFieldData Process_fields[LAST_PROCESSFIELD] = { [0] = { .name = "", .title = NULL, @@ -25,9 +25,10 @@ ProcessFieldData Process_fields[LAST_PROCESSFIELD] = { }, [PID] = { .name = "PID", - .title = " PID ", + .title = "PID", .description = "Process/thread ID", .flags = 0, + .pidColumn = true, }, [COMM] = { .name = "Command", @@ -43,21 +44,24 @@ ProcessFieldData Process_fields[LAST_PROCESSFIELD] = { }, [PPID] = { .name = "PPID", - .title = " PPID ", + .title = "PPID", .description = "Parent process ID", .flags = 0, + .pidColumn = true, }, [PGRP] = { .name = "PGRP", - .title = " PGRP ", + .title = "PGRP", .description = "Process group ID", .flags = 0, + .pidColumn = true, }, [SESSION] = { .name = "SESSION", - .title = " SESN ", + .title = "SESN", .description = "Process's session ID", .flags = 0, + .pidColumn = true, }, [TTY_NR] = { .name = "TTY_NR", @@ -67,9 +71,10 @@ ProcessFieldData Process_fields[LAST_PROCESSFIELD] = { }, [TPGID] = { .name = "TPGID", - .title = " TPGID ", + .title = "TPGID", .description = "Process ID of the fg process group of the controlling terminal", .flags = 0, + .pidColumn = true, }, [MINFLT] = { .name = "MINFLT", @@ -163,22 +168,13 @@ ProcessFieldData Process_fields[LAST_PROCESSFIELD] = { }, [TGID] = { .name = "TGID", - .title = " TGID ", + .title = "TGID", .description = "Thread group ID (i.e. process ID)", .flags = 0, + .pidColumn = true, }, }; -ProcessPidColumn Process_pidColumns[] = { - { .id = PID, .label = "PID" }, - { .id = PPID, .label = "PPID" }, - { .id = TPGID, .label = "TPGID" }, - { .id = TGID, .label = "TGID" }, - { .id = PGRP, .label = "PGRP" }, - { .id = SESSION, .label = "SESN" }, - { .id = 0, .label = NULL }, -}; - Process* OpenBSDProcess_new(const Settings* settings) { OpenBSDProcess* this = xCalloc(sizeof(OpenBSDProcess), 1); Object_setClass(this, Class(OpenBSDProcess)); diff --git a/openbsd/OpenBSDProcess.h b/openbsd/OpenBSDProcess.h index 6b28025af..6aab29a0c 100644 --- a/openbsd/OpenBSDProcess.h +++ b/openbsd/OpenBSDProcess.h @@ -25,9 +25,7 @@ typedef struct OpenBSDProcess_ { extern const ProcessClass OpenBSDProcess_class; -extern ProcessFieldData Process_fields[LAST_PROCESSFIELD]; - -extern ProcessPidColumn Process_pidColumns[]; +extern const ProcessFieldData Process_fields[LAST_PROCESSFIELD]; Process* OpenBSDProcess_new(const Settings* settings); diff --git a/solaris/SolarisProcess.c b/solaris/SolarisProcess.c index 3d4f95383..e3597360d 100644 --- a/solaris/SolarisProcess.c +++ b/solaris/SolarisProcess.c @@ -29,16 +29,16 @@ const ProcessClass SolarisProcess_class = { .compareByKey = SolarisProcess_compareByKey }; -ProcessFieldData Process_fields[LAST_PROCESSFIELD] = { +const ProcessFieldData Process_fields[LAST_PROCESSFIELD] = { [0] = { .name = "", .title = NULL, .description = NULL, .flags = 0, }, - [PID] = { .name = "PID", .title = " PID ", .description = "Process/thread ID", .flags = 0, }, + [PID] = { .name = "PID", .title = "PID", .description = "Process/thread ID", .flags = 0, .pidColumn = true, }, [COMM] = { .name = "Command", .title = "Command ", .description = "Command line", .flags = 0, }, [STATE] = { .name = "STATE", .title = "S ", .description = "Process state (S sleeping, R running, O onproc, Z zombie, T stopped, W waiting)", .flags = 0, }, - [PPID] = { .name = "PPID", .title = " PPID ", .description = "Parent process ID", .flags = 0, }, - [PGRP] = { .name = "PGRP", .title = " PGRP ", .description = "Process group ID", .flags = 0, }, - [SESSION] = { .name = "SESSION", .title = " SID ", .description = "Process's session ID", .flags = 0, }, + [PPID] = { .name = "PPID", .title = "PPID", .description = "Parent process ID", .flags = 0, .pidColumn = true, }, + [PGRP] = { .name = "PGRP", .title = "PGRP", .description = "Process group ID", .flags = 0, .pidColumn = true, }, + [SESSION] = { .name = "SESSION", .title = "SID", .description = "Process's session ID", .flags = 0, .pidColumn = true, }, [TTY_NR] = { .name = "TTY_NR", .title = " TTY ", .description = "Controlling terminal", .flags = 0, }, - [TPGID] = { .name = "TPGID", .title = " TPGID ", .description = "Process ID of the fg process group of the controlling terminal", .flags = 0, }, + [TPGID] = { .name = "TPGID", .title = "TPGID", .description = "Process ID of the fg process group of the controlling terminal", .flags = 0, .pidColumn = true, }, [MINFLT] = { .name = "MINFLT", .title = " MINFLT ", .description = "Number of minor faults which have not required loading a memory page from disk", .flags = 0, }, [MAJFLT] = { .name = "MAJFLT", .title = " MAJFLT ", .description = "Number of major faults which have required loading a memory page from disk", .flags = 0, }, [PRIORITY] = { .name = "PRIORITY", .title = "PRI ", .description = "Kernel's internal priority for the process", .flags = 0, }, @@ -54,30 +54,14 @@ ProcessFieldData Process_fields[LAST_PROCESSFIELD] = { [USER] = { .name = "USER", .title = "USER ", .description = "Username of the process owner (or user ID if name cannot be determined)", .flags = 0, }, [TIME] = { .name = "TIME", .title = " TIME+ ", .description = "Total time the process has spent in user and system time", .flags = 0, }, [NLWP] = { .name = "NLWP", .title = "NLWP ", .description = "Number of threads in the process", .flags = 0, }, - [TGID] = { .name = "TGID", .title = " TGID ", .description = "Thread group ID (i.e. process ID)", .flags = 0, }, - [ZONEID] = { .name = "ZONEID", .title = " ZONEID ", .description = "Zone ID", .flags = 0, }, + [TGID] = { .name = "TGID", .title = "TGID", .description = "Thread group ID (i.e. process ID)", .flags = 0, .pidColumn = true, }, + [ZONEID] = { .name = "ZONEID", .title = "ZONEID", .description = "Zone ID", .flags = 0, .pidColumn = true, }, [ZONE] = { .name = "ZONE", .title = "ZONE ", .description = "Zone name", .flags = 0, }, - [PROJID] = { .name = "PROJID", .title = " PRJID ", .description = "Project ID", .flags = 0, }, - [TASKID] = { .name = "TASKID", .title = " TSKID ", .description = "Task ID", .flags = 0, }, - [POOLID] = { .name = "POOLID", .title = " POLID ", .description = "Pool ID", .flags = 0, }, - [CONTID] = { .name = "CONTID", .title = " CNTID ", .description = "Contract ID", .flags = 0, }, - [LWPID] = { .name = "LWPID", .title = " LWPID ", .description = "LWP ID", .flags = 0, }, -}; - -ProcessPidColumn Process_pidColumns[] = { - { .id = ZONEID, .label = "ZONEID" }, - { .id = TASKID, .label = "TSKID" }, - { .id = PROJID, .label = "PRJID" }, - { .id = POOLID, .label = "POLID" }, - { .id = CONTID, .label = "CNTID" }, - { .id = PID, .label = "PID" }, - { .id = PPID, .label = "PPID" }, - { .id = LWPID, .label = "LWPID" }, - { .id = TPGID, .label = "TPGID" }, - { .id = TGID, .label = "TGID" }, - { .id = PGRP, .label = "PGRP" }, - { .id = SESSION, .label = "SID" }, - { .id = 0, .label = NULL }, + [PROJID] = { .name = "PROJID", .title = "PRJID", .description = "Project ID", .flags = 0, .pidColumn = true, }, + [TASKID] = { .name = "TASKID", .title = "TSKID", .description = "Task ID", .flags = 0, .pidColumn = true, }, + [POOLID] = { .name = "POOLID", .title = "POLID", .description = "Pool ID", .flags = 0, .pidColumn = true, }, + [CONTID] = { .name = "CONTID", .title = "CNTID", .description = "Contract ID", .flags = 0, .pidColumn = true, }, + [LWPID] = { .name = "LWPID", .title = "LWPID", .description = "LWP ID", .flags = 0, .pidColumn = true, }, }; Process* SolarisProcess_new(const Settings* settings) { @@ -101,15 +85,15 @@ void SolarisProcess_writeField(const Process* this, RichString* str, ProcessFiel int n = sizeof(buffer) - 1; switch (field) { // add Solaris-specific fields here - case ZONEID: xSnprintf(buffer, n, Process_pidFormat, sp->zoneid); break; - case PROJID: xSnprintf(buffer, n, Process_pidFormat, sp->projid); break; - case TASKID: xSnprintf(buffer, n, Process_pidFormat, sp->taskid); break; - case POOLID: xSnprintf(buffer, n, Process_pidFormat, sp->poolid); break; - case CONTID: xSnprintf(buffer, n, Process_pidFormat, sp->contid); break; + case ZONEID: xSnprintf(buffer, n, "%*d ", Process_pidDigits, sp->zoneid); break; + case PROJID: xSnprintf(buffer, n, "%*d ", Process_pidDigits, sp->projid); break; + case TASKID: xSnprintf(buffer, n, "%*d ", Process_pidDigits, sp->taskid); break; + case POOLID: xSnprintf(buffer, n, "%*d ", Process_pidDigits, sp->poolid); break; + case CONTID: xSnprintf(buffer, n, "%*d ", Process_pidDigits, sp->contid); break; case ZONE: xSnprintf(buffer, n, "%-*s ", ZONENAME_MAX/4, sp->zname); break; - case PID: xSnprintf(buffer, n, Process_pidFormat, sp->realpid); break; - case PPID: xSnprintf(buffer, n, Process_pidFormat, sp->realppid); break; - case LWPID: xSnprintf(buffer, n, Process_pidFormat, sp->lwpid); break; + case PID: xSnprintf(buffer, n, "%*d ", Process_pidDigits, sp->realpid); break; + case PPID: xSnprintf(buffer, n, "%*d ", Process_pidDigits, sp->realppid); break; + case LWPID: xSnprintf(buffer, n, "%*d ", Process_pidDigits, sp->lwpid); break; default: Process_writeField(this, str, field); return; diff --git a/solaris/SolarisProcess.h b/solaris/SolarisProcess.h index 5f79754a2..0f1cb46cd 100644 --- a/solaris/SolarisProcess.h +++ b/solaris/SolarisProcess.h @@ -34,9 +34,7 @@ typedef struct SolarisProcess_ { extern const ProcessClass SolarisProcess_class; -extern ProcessFieldData Process_fields[LAST_PROCESSFIELD]; - -extern ProcessPidColumn Process_pidColumns[]; +extern const ProcessFieldData Process_fields[LAST_PROCESSFIELD]; Process* SolarisProcess_new(const Settings* settings); diff --git a/unsupported/Platform.c b/unsupported/Platform.c index 75b005028..b435e1860 100644 --- a/unsupported/Platform.c +++ b/unsupported/Platform.c @@ -59,10 +59,6 @@ const MeterClass* const Platform_meterTypes[] = { NULL }; -ProcessPidColumn Process_pidColumns[] = { - { .id = 0, .label = NULL }, -}; - void Platform_init(void) { /* no platform-specific setup needed */ } diff --git a/unsupported/Platform.h b/unsupported/Platform.h index 7ba5f063f..5f7773811 100644 --- a/unsupported/Platform.h +++ b/unsupported/Platform.h @@ -23,10 +23,6 @@ extern ProcessField Platform_defaultFields[]; extern const MeterClass* const Platform_meterTypes[]; -extern char Process_pidFormat[20]; - -extern ProcessPidColumn Process_pidColumns[]; - void Platform_init(void); void Platform_done(void); diff --git a/unsupported/UnsupportedProcess.c b/unsupported/UnsupportedProcess.c index 153f86f61..eb52910b7 100644 --- a/unsupported/UnsupportedProcess.c +++ b/unsupported/UnsupportedProcess.c @@ -9,16 +9,16 @@ in the source distribution for its full text. #include "UnsupportedProcess.h" #include -ProcessFieldData Process_fields[LAST_PROCESSFIELD] = { +const ProcessFieldData Process_fields[LAST_PROCESSFIELD] = { [0] = { .name = "", .title = NULL, .description = NULL, .flags = 0, }, - [PID] = { .name = "PID", .title = " PID ", .description = "Process/thread ID", .flags = 0, }, + [PID] = { .name = "PID", .title = "PID", .description = "Process/thread ID", .flags = 0, .pidColumn = true, }, [COMM] = { .name = "Command", .title = "Command ", .description = "Command line", .flags = 0, }, [STATE] = { .name = "STATE", .title = "S ", .description = "Process state (S sleeping, R running, D disk, Z zombie, T traced, W paging)", .flags = 0, }, - [PPID] = { .name = "PPID", .title = " PPID ", .description = "Parent process ID", .flags = 0, }, - [PGRP] = { .name = "PGRP", .title = " PGRP ", .description = "Process group ID", .flags = 0, }, - [SESSION] = { .name = "SESSION", .title = " SID ", .description = "Process's session ID", .flags = 0, }, + [PPID] = { .name = "PPID", .title = "PPID", .description = "Parent process ID", .flags = 0, .pidColumn = true, }, + [PGRP] = { .name = "PGRP", .title = "PGRP", .description = "Process group ID", .flags = 0, .pidColumn = true, }, + [SESSION] = { .name = "SESSION", .title = "SID", .description = "Process's session ID", .flags = 0, .pidColumn = true, }, [TTY_NR] = { .name = "TTY_NR", .title = " TTY ", .description = "Controlling terminal", .flags = 0, }, - [TPGID] = { .name = "TPGID", .title = " TPGID ", .description = "Process ID of the fg process group of the controlling terminal", .flags = 0, }, + [TPGID] = { .name = "TPGID", .title = "TPGID", .description = "Process ID of the fg process group of the controlling terminal", .flags = 0, .pidColumn = true, }, [MINFLT] = { .name = "MINFLT", .title = " MINFLT ", .description = "Number of minor faults which have not required loading a memory page from disk", .flags = 0, }, [MAJFLT] = { .name = "MAJFLT", .title = " MAJFLT ", .description = "Number of major faults which have required loading a memory page from disk", .flags = 0, }, [PRIORITY] = { .name = "PRIORITY", .title = "PRI ", .description = "Kernel's internal priority for the process", .flags = 0, }, @@ -34,7 +34,7 @@ ProcessFieldData Process_fields[LAST_PROCESSFIELD] = { [USER] = { .name = "USER", .title = "USER ", .description = "Username of the process owner (or user ID if name cannot be determined)", .flags = 0, }, [TIME] = { .name = "TIME", .title = " TIME+ ", .description = "Total time the process has spent in user and system time", .flags = 0, }, [NLWP] = { .name = "NLWP", .title = "NLWP ", .description = "Number of threads in the process", .flags = 0, }, - [TGID] = { .name = "TGID", .title = " TGID ", .description = "Thread group ID (i.e. process ID)", .flags = 0, }, + [TGID] = { .name = "TGID", .title = "TGID", .description = "Thread group ID (i.e. process ID)", .flags = 0, .pidColumn = true, }, }; Process* UnsupportedProcess_new(Settings* settings) { diff --git a/unsupported/UnsupportedProcess.h b/unsupported/UnsupportedProcess.h index a9be326e1..e1812f165 100644 --- a/unsupported/UnsupportedProcess.h +++ b/unsupported/UnsupportedProcess.h @@ -11,7 +11,7 @@ in the source distribution for its full text. #define Process_delete UnsupportedProcess_delete -extern ProcessFieldData Process_fields[LAST_PROCESSFIELD]; +extern const ProcessFieldData Process_fields[LAST_PROCESSFIELD]; Process* UnsupportedProcess_new(Settings* settings); From 1ca21063b9e27ee3d44fb602a64e72966101cb80 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Christian=20G=C3=B6ttsche?= Date: Fri, 18 Dec 2020 15:49:37 +0100 Subject: [PATCH 4/5] Enable -Wformat=2 Now that the global format variable Process_pidFormat is gone, enable the compiler warning -Wformat=2. --- BatteryMeter.c | 29 +++++++++++++---------------- configure.ac | 1 + 2 files changed, 14 insertions(+), 16 deletions(-) diff --git a/BatteryMeter.c b/BatteryMeter.c index 4836809be..2c808c72c 100644 --- a/BatteryMeter.c +++ b/BatteryMeter.c @@ -35,24 +35,21 @@ static void BatteryMeter_updateValues(Meter* this, char* buffer, size_t len) { this->values[0] = percent; - const char *onAcText, *onBatteryText, *unknownText; - - unknownText = "%.1f%%"; - if (this->mode == TEXT_METERMODE) { - onAcText = "%.1f%% (Running on A/C)"; - onBatteryText = "%.1f%% (Running on battery)"; - } else { - onAcText = "%.1f%%(A/C)"; - onBatteryText = "%.1f%%(bat)"; + const char* text; + switch (isOnAC) { + case AC_PRESENT: + text = this->mode == TEXT_METERMODE ? " (Running on A/C)" : "(A/C)"; + break; + case AC_ABSENT: + text = this->mode == TEXT_METERMODE ? " (Running on battery)" : "(bat)"; + break; + case AC_ERROR: + default: + text = ""; + break; } - if (isOnAC == AC_PRESENT) { - xSnprintf(buffer, len, onAcText, percent); - } else if (isOnAC == AC_ABSENT) { - xSnprintf(buffer, len, onBatteryText, percent); - } else { - xSnprintf(buffer, len, unknownText, percent); - } + xSnprintf(buffer, len, "%.1f%%%s", percent, text); } const MeterClass BatteryMeter_class = { diff --git a/configure.ac b/configure.ac index 5c39b405a..f303ff03f 100644 --- a/configure.ac +++ b/configure.ac @@ -333,6 +333,7 @@ AM_CFLAGS="\ -Wcast-qual\ -Wextra\ -Wfloat-equal\ + -Wformat=2\ -Wmissing-format-attribute\ -Wmissing-noreturn\ -Wmissing-prototypes\ From 2584621e58274558f9b89745787449c9de2fc844 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Christian=20G=C3=B6ttsche?= Date: Sat, 19 Dec 2020 18:10:03 +0100 Subject: [PATCH 5/5] Mark Platform_defaultFields const --- Settings.c | 2 +- darwin/Platform.c | 2 +- darwin/Platform.h | 2 +- dragonflybsd/Platform.c | 2 +- dragonflybsd/Platform.h | 2 +- freebsd/Platform.c | 2 +- freebsd/Platform.h | 2 +- linux/Platform.c | 2 +- linux/Platform.h | 2 +- openbsd/Platform.c | 2 +- openbsd/Platform.h | 2 +- solaris/Platform.c | 2 +- solaris/Platform.h | 2 +- unsupported/Platform.c | 2 +- unsupported/Platform.h | 2 +- 15 files changed, 15 insertions(+), 15 deletions(-) diff --git a/Settings.c b/Settings.c index 96990a83d..a0d42f39a 100644 --- a/Settings.c +++ b/Settings.c @@ -359,7 +359,7 @@ Settings* Settings_new(int initialCpuCount) { // TODO: turn 'fields' into a Vector, // (and ProcessFields into proper objects). this->flags = 0; - ProcessField* defaults = Platform_defaultFields; + const ProcessField* defaults = Platform_defaultFields; for (int i = 0; defaults[i]; i++) { this->fields[i] = defaults[i]; this->flags |= Process_fields[defaults[i]].flags; diff --git a/darwin/Platform.c b/darwin/Platform.c index 13ebceb17..a0ea6721f 100644 --- a/darwin/Platform.c +++ b/darwin/Platform.c @@ -39,7 +39,7 @@ in the source distribution for its full text. #endif -ProcessField Platform_defaultFields[] = { PID, USER, PRIORITY, NICE, M_VIRT, M_RESIDENT, STATE, PERCENT_CPU, PERCENT_MEM, TIME, COMM, 0 }; +const ProcessField Platform_defaultFields[] = { PID, USER, PRIORITY, NICE, M_VIRT, M_RESIDENT, STATE, PERCENT_CPU, PERCENT_MEM, TIME, COMM, 0 }; const SignalItem Platform_signals[] = { { .name = " 0 Cancel", .number = 0 }, diff --git a/darwin/Platform.h b/darwin/Platform.h index 23132bb23..3e0369b8b 100644 --- a/darwin/Platform.h +++ b/darwin/Platform.h @@ -20,7 +20,7 @@ in the source distribution for its full text. #include "SignalsPanel.h" -extern ProcessField Platform_defaultFields[]; +extern const ProcessField Platform_defaultFields[]; extern double Platform_timebaseToNS; diff --git a/dragonflybsd/Platform.c b/dragonflybsd/Platform.c index 6f2d7bdc5..8ce216c18 100644 --- a/dragonflybsd/Platform.c +++ b/dragonflybsd/Platform.c @@ -31,7 +31,7 @@ in the source distribution for its full text. #include -ProcessField Platform_defaultFields[] = { PID, USER, PRIORITY, NICE, M_VIRT, M_RESIDENT, STATE, PERCENT_CPU, PERCENT_MEM, TIME, COMM, 0 }; +const ProcessField Platform_defaultFields[] = { PID, USER, PRIORITY, NICE, M_VIRT, M_RESIDENT, STATE, PERCENT_CPU, PERCENT_MEM, TIME, COMM, 0 }; const SignalItem Platform_signals[] = { { .name = " 0 Cancel", .number = 0 }, diff --git a/dragonflybsd/Platform.h b/dragonflybsd/Platform.h index 51d4c2cf8..3c5d9cb2b 100644 --- a/dragonflybsd/Platform.h +++ b/dragonflybsd/Platform.h @@ -18,7 +18,7 @@ in the source distribution for its full text. #include "SignalsPanel.h" -extern ProcessField Platform_defaultFields[]; +extern const ProcessField Platform_defaultFields[]; extern const SignalItem Platform_signals[]; diff --git a/freebsd/Platform.c b/freebsd/Platform.c index 3cec913de..c74ab6da3 100644 --- a/freebsd/Platform.c +++ b/freebsd/Platform.c @@ -47,7 +47,7 @@ in the source distribution for its full text. #include "zfs/ZfsCompressedArcMeter.h" -ProcessField Platform_defaultFields[] = { PID, USER, PRIORITY, NICE, M_VIRT, M_RESIDENT, STATE, PERCENT_CPU, PERCENT_MEM, TIME, COMM, 0 }; +const ProcessField Platform_defaultFields[] = { PID, USER, PRIORITY, NICE, M_VIRT, M_RESIDENT, STATE, PERCENT_CPU, PERCENT_MEM, TIME, COMM, 0 }; const SignalItem Platform_signals[] = { { .name = " 0 Cancel", .number = 0 }, diff --git a/freebsd/Platform.h b/freebsd/Platform.h index 793746e7e..36895b814 100644 --- a/freebsd/Platform.h +++ b/freebsd/Platform.h @@ -19,7 +19,7 @@ in the source distribution for its full text. #include "SignalsPanel.h" -extern ProcessField Platform_defaultFields[]; +extern const ProcessField Platform_defaultFields[]; extern const SignalItem Platform_signals[]; diff --git a/linux/Platform.c b/linux/Platform.c index d048a804c..dd80ded70 100644 --- a/linux/Platform.c +++ b/linux/Platform.c @@ -65,7 +65,7 @@ in the source distribution for its full text. #endif -ProcessField Platform_defaultFields[] = { PID, USER, PRIORITY, NICE, M_VIRT, M_RESIDENT, M_SHARE, STATE, PERCENT_CPU, PERCENT_MEM, TIME, COMM, 0 }; +const ProcessField Platform_defaultFields[] = { PID, USER, PRIORITY, NICE, M_VIRT, M_RESIDENT, M_SHARE, STATE, PERCENT_CPU, PERCENT_MEM, TIME, COMM, 0 }; const SignalItem Platform_signals[] = { { .name = " 0 Cancel", .number = 0 }, diff --git a/linux/Platform.h b/linux/Platform.h index be0331b99..fe8144840 100644 --- a/linux/Platform.h +++ b/linux/Platform.h @@ -18,7 +18,7 @@ in the source distribution for its full text. #include "ProcessLocksScreen.h" #include "SignalsPanel.h" -extern ProcessField Platform_defaultFields[]; +extern const ProcessField Platform_defaultFields[]; extern const SignalItem Platform_signals[]; diff --git a/openbsd/Platform.c b/openbsd/Platform.c index 30a58ec41..8ee814110 100644 --- a/openbsd/Platform.c +++ b/openbsd/Platform.c @@ -42,7 +42,7 @@ in the source distribution for its full text. #include "XUtils.h" -ProcessField Platform_defaultFields[] = { PID, USER, PRIORITY, NICE, M_VIRT, M_RESIDENT, STATE, PERCENT_CPU, PERCENT_MEM, TIME, COMM, 0 }; +const ProcessField Platform_defaultFields[] = { PID, USER, PRIORITY, NICE, M_VIRT, M_RESIDENT, STATE, PERCENT_CPU, PERCENT_MEM, TIME, COMM, 0 }; /* * See /usr/include/sys/signal.h diff --git a/openbsd/Platform.h b/openbsd/Platform.h index 4774f921d..e7a59662e 100644 --- a/openbsd/Platform.h +++ b/openbsd/Platform.h @@ -20,7 +20,7 @@ in the source distribution for its full text. #include "SignalsPanel.h" -extern ProcessField Platform_defaultFields[]; +extern const ProcessField Platform_defaultFields[]; /* see /usr/include/sys/signal.h */ extern const SignalItem Platform_signals[]; diff --git a/solaris/Platform.c b/solaris/Platform.c index b674998b1..09747cd4c 100644 --- a/solaris/Platform.c +++ b/solaris/Platform.c @@ -86,7 +86,7 @@ const SignalItem Platform_signals[] = { const unsigned int Platform_numberOfSignals = ARRAYSIZE(Platform_signals); -ProcessField Platform_defaultFields[] = { PID, LWPID, USER, PRIORITY, NICE, M_VIRT, M_RESIDENT, STATE, PERCENT_CPU, PERCENT_MEM, TIME, COMM, 0 }; +const ProcessField Platform_defaultFields[] = { PID, LWPID, USER, PRIORITY, NICE, M_VIRT, M_RESIDENT, STATE, PERCENT_CPU, PERCENT_MEM, TIME, COMM, 0 }; const MeterClass* const Platform_meterTypes[] = { &CPUMeter_class, diff --git a/solaris/Platform.h b/solaris/Platform.h index 4daf71649..de2b2c928 100644 --- a/solaris/Platform.h +++ b/solaris/Platform.h @@ -40,7 +40,7 @@ extern const SignalItem Platform_signals[]; extern const unsigned int Platform_numberOfSignals; -extern ProcessField Platform_defaultFields[]; +extern const ProcessField Platform_defaultFields[]; extern const MeterClass* const Platform_meterTypes[]; diff --git a/unsupported/Platform.c b/unsupported/Platform.c index b435e1860..94e1b7c7b 100644 --- a/unsupported/Platform.c +++ b/unsupported/Platform.c @@ -28,7 +28,7 @@ const SignalItem Platform_signals[] = { const unsigned int Platform_numberOfSignals = ARRAYSIZE(Platform_signals); -ProcessField Platform_defaultFields[] = { PID, USER, PRIORITY, NICE, M_VIRT, M_RESIDENT, STATE, PERCENT_CPU, PERCENT_MEM, TIME, COMM, 0 }; +const ProcessField Platform_defaultFields[] = { PID, USER, PRIORITY, NICE, M_VIRT, M_RESIDENT, STATE, PERCENT_CPU, PERCENT_MEM, TIME, COMM, 0 }; const MeterClass* const Platform_meterTypes[] = { &CPUMeter_class, diff --git a/unsupported/Platform.h b/unsupported/Platform.h index 5f7773811..4254122ec 100644 --- a/unsupported/Platform.h +++ b/unsupported/Platform.h @@ -19,7 +19,7 @@ extern const SignalItem Platform_signals[]; extern const unsigned int Platform_numberOfSignals; -extern ProcessField Platform_defaultFields[]; +extern const ProcessField Platform_defaultFields[]; extern const MeterClass* const Platform_meterTypes[];