From c55320e9e2a8916e911bcd39ab37b79e3a7d03b2 Mon Sep 17 00:00:00 2001 From: Daniel Lange Date: Mon, 11 Jan 2021 20:43:27 +0100 Subject: New upstream version 3.0.5 --- Process.c | 170 ++++++++++++++++++++++++++++++++------------------------------ 1 file changed, 89 insertions(+), 81 deletions(-) (limited to 'Process.c') diff --git a/Process.c b/Process.c index 8245f86..a78fd23 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) { @@ -74,53 +66,53 @@ void Process_humanNumber(RichString* str, unsigned long long number, bool colori if (number < 1000) { //Plain number, no markings len = xSnprintf(buffer, sizeof(buffer), "%5llu ", number); - RichString_appendn(str, processColor, buffer, len); + RichString_appendnAscii(str, processColor, buffer, len); } else if (number < 100000) { //2 digit MB, 3 digit KB len = xSnprintf(buffer, sizeof(buffer), "%2llu", number/1000); - RichString_appendn(str, processMegabytesColor, buffer, len); + RichString_appendnAscii(str, processMegabytesColor, buffer, len); number %= 1000; len = xSnprintf(buffer, sizeof(buffer), "%03llu ", number); - RichString_appendn(str, processColor, buffer, len); + RichString_appendnAscii(str, processColor, buffer, len); } else if (number < 1000 * ONE_K) { //3 digit MB number /= ONE_K; len = xSnprintf(buffer, sizeof(buffer), "%4lluM ", number); - RichString_appendn(str, processMegabytesColor, buffer, len); + RichString_appendnAscii(str, processMegabytesColor, buffer, len); } else if (number < 10000 * ONE_K) { //1 digit GB, 3 digit MB number /= ONE_K; len = xSnprintf(buffer, sizeof(buffer), "%1llu", number/1000); - RichString_appendn(str, processGigabytesColor, buffer, len); + RichString_appendnAscii(str, processGigabytesColor, buffer, len); number %= 1000; len = xSnprintf(buffer, sizeof(buffer), "%03lluM ", number); - RichString_appendn(str, processMegabytesColor, buffer, len); + RichString_appendnAscii(str, processMegabytesColor, buffer, len); } else if (number < 100000 * ONE_K) { //2 digit GB, 1 digit MB number /= 100 * ONE_K; len = xSnprintf(buffer, sizeof(buffer), "%2llu", number/10); - RichString_appendn(str, processGigabytesColor, buffer, len); + RichString_appendnAscii(str, processGigabytesColor, buffer, len); number %= 10; len = xSnprintf(buffer, sizeof(buffer), ".%1llu", number); - RichString_appendn(str, processMegabytesColor, buffer, len); - RichString_append(str, processGigabytesColor, "G "); + RichString_appendnAscii(str, processMegabytesColor, buffer, len); + RichString_appendAscii(str, processGigabytesColor, "G "); } else if (number < 1000 * ONE_M) { //3 digit GB number /= ONE_M; len = xSnprintf(buffer, sizeof(buffer), "%4lluG ", number); - RichString_appendn(str, processGigabytesColor, buffer, len); + RichString_appendnAscii(str, processGigabytesColor, buffer, len); } else if (number < 10000ULL * ONE_M) { //1 digit TB, 3 digit GB number /= ONE_M; len = xSnprintf(buffer, sizeof(buffer), "%1llu", number/1000); - RichString_appendn(str, largeNumberColor, buffer, len); + RichString_appendnAscii(str, largeNumberColor, buffer, len); number %= 1000; len = xSnprintf(buffer, sizeof(buffer), "%03lluG ", number); - RichString_appendn(str, processGigabytesColor, buffer, len); + RichString_appendnAscii(str, processGigabytesColor, buffer, len); } else { //2 digit TB and above len = xSnprintf(buffer, sizeof(buffer), "%4.1lfT ", (double)number/ONE_G); - RichString_appendn(str, largeNumberColor, buffer, len); + RichString_appendnAscii(str, largeNumberColor, buffer, len); } } @@ -139,26 +131,25 @@ void Process_colorNumber(RichString* str, unsigned long long number, bool colori } if (number == ULLONG_MAX) { - int len = xSnprintf(buffer, sizeof(buffer), " N/A "); - RichString_appendn(str, CRT_colors[PROCESS_SHADOW], buffer, len); + RichString_appendAscii(str, CRT_colors[PROCESS_SHADOW], " N/A "); } else if (number >= 100000LL * ONE_DECIMAL_T) { xSnprintf(buffer, sizeof(buffer), "%11llu ", number / ONE_DECIMAL_G); - RichString_appendn(str, largeNumberColor, buffer, 12); + RichString_appendnAscii(str, largeNumberColor, buffer, 12); } else if (number >= 100LL * ONE_DECIMAL_T) { xSnprintf(buffer, sizeof(buffer), "%11llu ", number / ONE_DECIMAL_M); - RichString_appendn(str, largeNumberColor, buffer, 8); - RichString_appendn(str, processMegabytesColor, buffer+8, 4); + RichString_appendnAscii(str, largeNumberColor, buffer, 8); + RichString_appendnAscii(str, processMegabytesColor, buffer+8, 4); } else if (number >= 10LL * ONE_DECIMAL_G) { xSnprintf(buffer, sizeof(buffer), "%11llu ", number / ONE_DECIMAL_K); - RichString_appendn(str, largeNumberColor, buffer, 5); - RichString_appendn(str, processMegabytesColor, buffer+5, 3); - RichString_appendn(str, processColor, buffer+8, 4); + RichString_appendnAscii(str, largeNumberColor, buffer, 5); + RichString_appendnAscii(str, processMegabytesColor, buffer+5, 3); + RichString_appendnAscii(str, processColor, buffer+8, 4); } else { xSnprintf(buffer, sizeof(buffer), "%11llu ", number); - RichString_appendn(str, largeNumberColor, buffer, 2); - RichString_appendn(str, processMegabytesColor, buffer+2, 3); - RichString_appendn(str, processColor, buffer+5, 3); - RichString_appendn(str, processShadowColor, buffer+8, 4); + RichString_appendnAscii(str, largeNumberColor, buffer, 2); + RichString_appendnAscii(str, processMegabytesColor, buffer+2, 3); + RichString_appendnAscii(str, processColor, buffer+5, 3); + RichString_appendnAscii(str, processShadowColor, buffer+8, 4); } } @@ -172,16 +163,16 @@ void Process_printTime(RichString* str, unsigned long long totalHundredths) { char buffer[10]; if (hours >= 100) { xSnprintf(buffer, sizeof(buffer), "%7lluh ", hours); - RichString_append(str, CRT_colors[LARGE_NUMBER], buffer); + RichString_appendAscii(str, CRT_colors[LARGE_NUMBER], buffer); } else { if (hours) { xSnprintf(buffer, sizeof(buffer), "%2lluh", hours); - RichString_append(str, CRT_colors[LARGE_NUMBER], buffer); + RichString_appendAscii(str, CRT_colors[LARGE_NUMBER], buffer); xSnprintf(buffer, sizeof(buffer), "%02d:%02d ", minutes, seconds); } else { xSnprintf(buffer, sizeof(buffer), "%2d:%02d.%02d ", minutes, seconds, hundredths); } - RichString_append(str, CRT_colors[DEFAULT_COLOR], buffer); + RichString_appendAscii(str, CRT_colors[DEFAULT_COLOR], buffer); } } @@ -192,34 +183,34 @@ void Process_fillStarttimeBuffer(Process* this) { } static inline void Process_writeCommand(const Process* this, int attr, int baseattr, RichString* str) { - int start = RichString_size(str), finish = 0; + int start = RichString_size(str); + int len = 0; const char* comm = this->comm; if (this->settings->highlightBaseName || !this->settings->showProgramPath) { - int i, basename = 0; - for (i = 0; i < this->basenameOffset; i++) { + int basename = 0; + for (int i = 0; i < this->basenameOffset; i++) { if (comm[i] == '/') { basename = i + 1; } else if (comm[i] == ':') { - finish = i + 1; + len = i + 1; break; } } - if (!finish) { + if (len == 0) { if (this->settings->showProgramPath) { start += basename; } else { comm += basename; } - finish = this->basenameOffset - basename; + len = this->basenameOffset - basename; } - finish += start - 1; } - RichString_append(str, attr, comm); + RichString_appendWide(str, attr, comm); if (this->settings->highlightBaseName) { - RichString_setAttrn(str, baseattr, start, finish); + RichString_setAttrn(str, baseattr, start, len); } } @@ -234,26 +225,30 @@ void Process_outputRate(RichString* str, char* buffer, size_t n, double rate, in } if (isnan(rate)) { - int len = xSnprintf(buffer, n, " N/A "); - RichString_appendn(str, CRT_colors[PROCESS_SHADOW], buffer, len); + RichString_appendAscii(str, CRT_colors[PROCESS_SHADOW], " N/A "); } else if (rate < ONE_K) { int len = snprintf(buffer, n, "%7.2f B/s ", rate); - RichString_appendn(str, processColor, buffer, len); + RichString_appendnAscii(str, processColor, buffer, len); } else if (rate < ONE_M) { int len = snprintf(buffer, n, "%7.2f K/s ", rate / ONE_K); - RichString_appendn(str, processColor, buffer, len); + RichString_appendnAscii(str, processColor, buffer, len); } else if (rate < ONE_G) { int len = snprintf(buffer, n, "%7.2f M/s ", rate / ONE_M); - RichString_appendn(str, processMegabytesColor, buffer, len); + RichString_appendnAscii(str, processMegabytesColor, buffer, len); } else if (rate < ONE_T) { int len = snprintf(buffer, n, "%7.2f G/s ", rate / ONE_G); - RichString_appendn(str, largeNumberColor, buffer, len); + RichString_appendnAscii(str, largeNumberColor, buffer, len); } else { int len = snprintf(buffer, n, "%7.2f T/s ", rate / ONE_T); - RichString_appendn(str, largeNumberColor, buffer, len); + RichString_appendnAscii(str, largeNumberColor, buffer, len); } } +void Process_printLeftAlignedField(RichString* str, int attr, const char* content, unsigned int width) { + int c = RichString_appendnWide(str, attr, content, MINIMUM(width, strlen(content))); + RichString_appendChr(str, ' ', width + 1 - c); +} + void Process_writeField(const Process* this, RichString* str, ProcessField field) { char buffer[256]; buffer[255] = '\0'; int attr = CRT_colors[DEFAULT_COLOR]; @@ -320,17 +315,18 @@ void Process_writeField(const Process* this, RichString* str, ProcessField field buf += written; n -= written; } - const char* draw = CRT_treeStr[lastItem ? (this->settings->direction == 1 ? TREE_STR_BEND : TREE_STR_TEND) : TREE_STR_RTEE]; + + const char* draw = CRT_treeStr[lastItem ? TREE_STR_BEND : TREE_STR_RTEE]; xSnprintf(buf, n, "%s%s ", draw, this->showChildren ? CRT_treeStr[TREE_STR_SHUT] : CRT_treeStr[TREE_STR_OPEN] ); - RichString_append(str, CRT_colors[PROCESS_TREE], buffer); + RichString_appendWide(str, CRT_colors[PROCESS_TREE], buffer); Process_writeCommand(this, attr, baseattr, str); return; } } case MAJFLT: Process_colorNumber(str, this->majflt, coloring); return; case MINFLT: Process_colorNumber(str, this->minflt, coloring); return; - case M_RESIDENT: Process_humanNumber(str, this->m_resident * CRT_pageSizeKB, coloring); return; - case M_VIRT: Process_humanNumber(str, this->m_virt * CRT_pageSizeKB, coloring); return; + case M_RESIDENT: Process_humanNumber(str, this->m_resident, coloring); return; + case M_VIRT: Process_humanNumber(str, this->m_virt, coloring); return; case NICE: { xSnprintf(buffer, n, "%3ld ", this->nice); attr = this->nice < 0 ? CRT_colors[PROCESS_HIGH_PRIORITY] @@ -339,9 +335,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 "); @@ -350,7 +346,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); @@ -366,27 +362,25 @@ 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) attr = CRT_colors[PROCESS_SHADOW]; + if (this->user) { - xSnprintf(buffer, n, "%-9s ", this->user); - } else { - xSnprintf(buffer, n, "%-9d ", this->st_uid); - } - if (buffer[9] != '\0') { - buffer[9] = ' '; - buffer[10] = '\0'; + Process_printLeftAlignedField(str, attr, this->user, 9); + return; } + + xSnprintf(buffer, n, "%-9d ", this->st_uid); break; } default: xSnprintf(buffer, n, "- "); } - RichString_append(str, attr, buffer); + RichString_appendWide(str, attr, buffer); } void Process_display(const Object* cast, RichString* out) { @@ -449,13 +443,13 @@ void Process_init(Process* this, const struct Settings_* settings) { } void Process_toggleTag(Process* this) { - this->tag = this->tag == true ? false : true; + this->tag = !this->tag; } bool Process_isNew(const Process* this) { assert(this->processList); if (this->processList->scanTs >= this->seenTs) { - return this->processList->scanTs - this->seenTs <= this->processList->settings->highlightDelaySecs; + return this->processList->scanTs - this->seenTs <= 1000 * this->processList->settings->highlightDelaySecs; } return false; } @@ -486,18 +480,18 @@ bool Process_sendSignal(Process* this, Arg sgn) { return ok; } -long Process_pidCompare(const void* v1, const void* v2) { +int Process_pidCompare(const void* v1, const void* v2) { const Process* p1 = (const Process*)v1; const Process* p2 = (const Process*)v2; - return (p1->pid - p2->pid); + + return SPACESHIP_NUMBER(p1->pid, p2->pid); } -long Process_compare(const void* v1, const void* v2) { +int Process_compare(const void* v1, const void* v2) { const Process *p1, *p2; const Settings *settings = ((const Process*)v1)->settings; - int r; - if (settings->direction == 1) { + if (Settings_getActiveDirection(settings) == 1) { p1 = (const Process*)v1; p2 = (const Process*)v2; } else { @@ -505,7 +499,21 @@ long Process_compare(const void* v1, const void* v2) { p1 = (const Process*)v2; } - switch (settings->sortKey) { + ProcessField key = Settings_getActiveSortKey(settings); + + int result = Process_compareByKey(p1, p2, key); + + // Implement tie-breaker (needed to make tree mode more stable) + if (!result) + result = SPACESHIP_NUMBER(p1->pid, p2->pid); + + return result; +} + +int Process_compareByKey_Base(const Process* p1, const Process* p2, ProcessField key) { + int r; + + switch (key) { case PERCENT_CPU: case PERCENT_NORM_CPU: return SPACESHIP_NUMBER(p2->percent_cpu, p1->percent_cpu); -- cgit v1.2.3