Skip to content

Commit

Permalink
Squashed 'vendor/github.com/htop-dev/htop/' changes from 1a7b6d4a6b..…
Browse files Browse the repository at this point in the history
…81432ba5cf

81432ba5cf Consolidate CRT_delay and CRT_crashSettings into one
a750f38d7c Resolve a problem in F2/Setup update interval adjustments
ebb5898b18 Use pkg-config instead of hard-coded paths.
c0bb3a402f CI: Fix NetBSD build.
11bf6a74dd Fix nice handling for FreeBSD
8edd449a23 Clamp graph meter values in "double" type
a8b5d68520 Linux: use fast integer parsing for /proc/<pid>/stat
89400e673c Fixed typo in PCP GPU meter configuration file
cf72728f0d Permit zero items in drawing a Graph meter
1887699420 Adjust GraphMeterMode_draw() code comments
25391b2f16 Reorder GraphMeterMode_draw() code slightly
d888b99b3a Disabling the NetBSD CI job hard-failing due to changes in their ncurses(w) packaging

git-subtree-dir: vendor/github.com/htop-dev/htop
git-subtree-split: 81432ba5cff2a813b6b4088830a3035fb2002620
  • Loading branch information
natoscott committed Aug 6, 2024
1 parent f087436 commit 24216ef
Show file tree
Hide file tree
Showing 13 changed files with 191 additions and 75 deletions.
46 changes: 23 additions & 23 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -242,29 +242,29 @@ jobs:
gmake -k
build-netbsd-latest-gcc:
runs-on: ubuntu-22.04
timeout-minutes: 20
steps:
- uses: actions/checkout@v4
with:
submodules: recursive
- name: Compile
uses: vmactions/netbsd-vm@v1
with:
release: '10.0'
usesh: true
prepare: |
PATH="/usr/pkg/sbin:/usr/pkg/bin:$PATH"
PKG_PATH="https://ftp.netbsd.org/pub/pkgsrc/packages/NetBSD/amd64/10.0/All/"
export PATH PKG_PATH
/usr/sbin/pkg_add pkgin
pkgin -y install autoconf automake libtool ncurses ncursesw gmake git
git config --global --add safe.directory /home/runner/work/htop/htop
run: |
set -e
./autogen.sh
CPPFLAGS="-I/usr/pkg/include" ./configure --enable-unicode --enable-werror
gmake -k
runs-on: ubuntu-22.04
timeout-minutes: 20
steps:
- uses: actions/checkout@v4
with:
submodules: recursive
- name: Compile
uses: vmactions/netbsd-vm@v1
with:
release: '10.0'
usesh: true
prepare: |
PATH="/usr/pkg/sbin:/usr/pkg/bin:$PATH"
PKG_PATH="https://ftp.netbsd.org/pub/pkgsrc/packages/NetBSD/amd64/10.0/All/"
export PATH PKG_PATH
/usr/sbin/pkg_add pkgin
pkgin -y install autoconf automake libtool ncurses pkg-config gmake git
git config --global --add safe.directory /home/runner/work/htop/htop
run: |
set -e
./autogen.sh
CPPFLAGS=$(pkg-config --cflags ncurses) LDFLAGS=$(pkg-config --libs ncurses) ./configure --enable-unicode --enable-werror
gmake -k
build-openbsd-latest-clang:
runs-on: ubuntu-22.04
Expand Down
16 changes: 7 additions & 9 deletions CRT.c
Original file line number Diff line number Diff line change
Expand Up @@ -92,8 +92,7 @@ bool CRT_utf8 = false;

const char* const* CRT_treeStr = CRT_treeStrAscii;

static const Settings* CRT_crashSettings;
static const int* CRT_delay;
static const Settings* CRT_settings;

const char* CRT_degreeSign;

Expand Down Expand Up @@ -836,7 +835,7 @@ ATTR_NORETURN
static void CRT_handleSIGTERM(int sgn) {
CRT_done();

if (!CRT_crashSettings->changed)
if (!CRT_settings->changed)
_exit(0);

const char* signal_str = strsignal(sgn);
Expand Down Expand Up @@ -1055,8 +1054,7 @@ void CRT_init(const Settings* settings, bool allowUnicode, bool retainScreenOnEx

redirectStderr();
noecho();
CRT_crashSettings = settings;
CRT_delay = &(settings->delay);
CRT_settings = settings;
CRT_colors = CRT_colorSchemes[settings->colorScheme];
CRT_colorScheme = settings->colorScheme;

Expand All @@ -1065,7 +1063,7 @@ void CRT_init(const Settings* settings, bool allowUnicode, bool retainScreenOnEx
CRT_colorSchemes[COLORSCHEME_BROKENGRAY][i] = color == (A_BOLD | ColorPairGrayBlack) ? ColorPair(White, Black) : color;
}

halfdelay(*CRT_delay);
halfdelay(settings->delay);
nonl();
intrflush(stdscr, false);
keypad(stdscr, true);
Expand Down Expand Up @@ -1180,7 +1178,7 @@ int CRT_readKey(void) {
cbreak();
nodelay(stdscr, FALSE);
int ret = getch();
halfdelay(*CRT_delay);
halfdelay(CRT_settings->delay);
return ret;
}

Expand All @@ -1191,7 +1189,7 @@ void CRT_disableDelay(void) {
}

void CRT_enableDelay(void) {
halfdelay(*CRT_delay);
halfdelay(CRT_settings->delay);
}

void CRT_setColors(int colorScheme) {
Expand Down Expand Up @@ -1312,7 +1310,7 @@ void CRT_handleSIGSEGV(int signal) {
full_write_str(STDERR_FILENO,
"Setting information:\n"
"--------------------\n");
Settings_write(CRT_crashSettings, true);
Settings_write(CRT_settings, true);
full_write_str(STDERR_FILENO, "\n\n");

#ifdef PRINT_BACKTRACE
Expand Down
4 changes: 4 additions & 0 deletions CRT.h
Original file line number Diff line number Diff line change
Expand Up @@ -214,6 +214,10 @@ void CRT_disableDelay(void);

void CRT_enableDelay(void);

static inline void CRT_updateDelay(void) {
CRT_enableDelay(); // pushes new delay setting into halfdelay(3X)
}

void CRT_setColors(int colorScheme);

#endif
1 change: 1 addition & 0 deletions DisplayOptionsPanel.c
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ static HandlerResult DisplayOptionsPanel_eventHandler(Panel* super, int ch) {
if (result == HANDLED) {
this->settings->changed = true;
this->settings->lastUpdate++;
CRT_updateDelay();
Header* header = this->scr->header;
Header_calculateHeight(header);
Header_reinit(header);
Expand Down
27 changes: 19 additions & 8 deletions Meter.c
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,7 @@ static const char* const GraphMeterMode_dotsAscii[] = {
};

static void GraphMeterMode_draw(Meter* this, int x, int y, int w) {
// Draw the caption
const char* caption = Meter_getCaption(this);
attrset(CRT_colors[METER_TEXT]);
const int captionLen = 3;
Expand All @@ -198,6 +199,8 @@ static void GraphMeterMode_draw(Meter* this, int x, int y, int w) {
w -= captionLen;

GraphData* data = &this->drawData;

// Expand the graph data buffer if necessary
assert(data->nValues / 2 <= INT_MAX);
if (w > (int)(data->nValues / 2) && MAX_METER_GRAPHDATA_VALUES > data->nValues) {
size_t oldNValues = data->nValues;
Expand All @@ -212,6 +215,7 @@ static void GraphMeterMode_draw(Meter* this, int x, int y, int w) {
if (nValues < 1)
return;

// Record new value if necessary
const Machine* host = this->host;
if (!timercmp(&host->realtime, &(data->time), <)) {
int globalDelay = host->settings->delay;
Expand All @@ -220,17 +224,17 @@ static void GraphMeterMode_draw(Meter* this, int x, int y, int w) {

memmove(&data->values[0], &data->values[1], (nValues - 1) * sizeof(*data->values));

data->values[nValues - 1] = sumPositiveValues(this->values, this->curItems);
data->values[nValues - 1] = 0.0;
if (this->curItems > 0) {
assert(this->values);
data->values[nValues - 1] = sumPositiveValues(this->values, this->curItems);
}
}

if (w <= 0)
return;

if ((size_t)w > nValues / 2) {
x += w - nValues / 2;
w = nValues / 2;
}

// Graph drawing style (character set, etc.)
const char* const* GraphMeterMode_dots;
int GraphMeterMode_pixPerRow;
#ifdef HAVE_LIBNCURSESW
Expand All @@ -244,12 +248,19 @@ static void GraphMeterMode_draw(Meter* this, int x, int y, int w) {
GraphMeterMode_pixPerRow = PIXPERROW_ASCII;
}

// Starting positions of graph data and terminal column
if ((size_t)w > nValues / 2) {
x += w - nValues / 2;
w = nValues / 2;
}
size_t i = nValues - (size_t)w * 2;

// Draw the actual graph
for (int col = 0; i < nValues - 1; i += 2, col++) {
int pix = GraphMeterMode_pixPerRow * GRAPH_HEIGHT;
double total = MAXIMUM(this->total, 1);
int v1 = CLAMP((int) lround(data->values[i] / total * pix), 1, pix);
int v2 = CLAMP((int) lround(data->values[i + 1] / total * pix), 1, pix);
int v1 = (int) lround(CLAMP(data->values[i] / total * pix, 1.0, pix));
int v2 = (int) lround(CLAMP(data->values[i + 1] / total * pix, 1.0, pix));

int colorIdx = GRAPH_1;
for (int line = 0; line < GRAPH_HEIGHT; line++) {
Expand Down
13 changes: 9 additions & 4 deletions Process.c
Original file line number Diff line number Diff line change
Expand Up @@ -649,10 +649,15 @@ void Process_writeField(const Process* this, RichString* str, RowField field) {
case M_RESIDENT: Row_printKBytes(str, this->m_resident, coloring); return;
case M_VIRT: Row_printKBytes(str, this->m_virt, coloring); return;
case NICE:
xSnprintf(buffer, n, "%3ld ", this->nice);
attr = this->nice < 0 ? CRT_colors[PROCESS_HIGH_PRIORITY]
: this->nice > 0 ? CRT_colors[PROCESS_LOW_PRIORITY]
: CRT_colors[PROCESS_SHADOW];
if (this->nice == PROCESS_NICE_UNKNOWN) {
xSnprintf(buffer, n, "N/A ");
attr = CRT_colors[PROCESS_SHADOW];
} else {
xSnprintf(buffer, n, "%3ld ", this->nice);
attr = this->nice < 0 ? CRT_colors[PROCESS_HIGH_PRIORITY]
: this->nice > 0 ? CRT_colors[PROCESS_LOW_PRIORITY]
: CRT_colors[PROCESS_SHADOW];
}
break;
case NLWP:
if (this->nlwp == 1)
Expand Down
4 changes: 4 additions & 0 deletions Process.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ Released under the GNU GPLv2+, see the COPYING file
in the source distribution for its full text.
*/

#include <limits.h>
#include <stdbool.h>
#include <stdint.h>
#include <sys/types.h>
Expand All @@ -24,6 +25,9 @@ in the source distribution for its full text.

#define DEFAULT_HIGHLIGHT_SECS 5

/* Sentinel value for an unknown niceness in Process.nice */
#define PROCESS_NICE_UNKNOWN (-LONG_MAX)

typedef enum Tristate_ {
TRI_INITIAL = 0,
TRI_OFF = -1,
Expand Down
22 changes: 22 additions & 0 deletions freebsd/FreeBSDProcess.c
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ const ProcessFieldData Process_fields[LAST_PROCESSFIELD] = {
#endif
[JID] = { .name = "JID", .title = "JID", .description = "Jail prison ID", .flags = 0, .pidColumn = true, },
[JAIL] = { .name = "JAIL", .title = "JAIL ", .description = "Jail prison name", .flags = 0, },
[SCHEDCLASS] = { .name = "SCHEDCLASS", .title = "SC", .description = "Scheduling Class (Timesharing, Realtime, Idletime)", .flags = 0, },
[EMULATION] = { .name = "EMULATION", .title = "EMULATION ", .description = "System call emulation environment (ABI)", .flags = 0, },
};

Expand All @@ -73,22 +74,41 @@ void Process_delete(Object* cast) {
free(this);
}

static const char FreeBSD_schedclassChars[MAX_SCHEDCLASS] = {
[SCHEDCLASS_UNKNOWN] = '?', // Something went wrong or the base system has a new scheduling class
[SCHEDCLASS_INTR_THREAD] = '-', // interrupt thread, these have special handling of priority
[SCHEDCLASS_IDLE] = 'i', // idletime scheduling
[SCHEDCLASS_TIMESHARE] = ' ', // timesharing process scheduling (regular processes are timeshared)
[SCHEDCLASS_REALTIME] = 'r', // realtime scheduling
};

static void FreeBSDProcess_rowWriteField(const Row* super, RichString* str, ProcessField field) {
const FreeBSDProcess* fp = (const FreeBSDProcess*) super;

char buffer[256]; buffer[255] = '\0';
char sched_class;
int attr = CRT_colors[DEFAULT_COLOR];
size_t n = sizeof(buffer) - 1;

switch (field) {
// add FreeBSD-specific fields here
case JID: xSnprintf(buffer, n, "%*d ", Process_pidDigits, fp->jid); break;

case JAIL:
Row_printLeftAlignedField(str, attr, fp->jname ? fp->jname : "N/A", 11);
return;

case EMULATION:
Row_printLeftAlignedField(str, attr, fp->emul ? fp->emul : "N/A", 16);
return;

case SCHEDCLASS:
assert(0 <= fp->sched_class && fp->sched_class < ARRAYSIZE(FreeBSD_schedclassChars));
sched_class = FreeBSD_schedclassChars[fp->sched_class];
assert(sched_class);
xSnprintf(buffer, n, " %c", sched_class);
break;

default:
Process_writeField(&fp->super, str, field);
return;
Expand All @@ -109,6 +129,8 @@ static int FreeBSDProcess_compareByKey(const Process* v1, const Process* v2, Pro
return SPACESHIP_NULLSTR(p1->jname, p2->jname);
case EMULATION:
return SPACESHIP_NULLSTR(p1->emul, p2->emul);
case SCHEDCLASS:
return SPACESHIP_NUMBER(p1->sched_class, p2->sched_class);
default:
return Process_compareByKey_Base(v1, v2, key);
}
Expand Down
11 changes: 11 additions & 0 deletions freebsd/FreeBSDProcess.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,23 @@ in the source distribution for its full text.
#include "Process.h"
#include "Machine.h"

typedef enum {
SCHEDCLASS_UNKNOWN = 0,

SCHEDCLASS_INTR_THREAD, /* interrupt thread */
SCHEDCLASS_REALTIME,
SCHEDCLASS_TIMESHARE, /* Regular scheduling */
SCHEDCLASS_IDLE,

MAX_SCHEDCLASS,
} FreeBSDSchedClass;

typedef struct FreeBSDProcess_ {
Process super;
int jid;
char* jname;
char* emul;
FreeBSDSchedClass sched_class;
} FreeBSDProcess;

extern const ProcessClass FreeBSDProcess_class;
Expand Down
47 changes: 39 additions & 8 deletions freebsd/FreeBSDProcessTable.c
Original file line number Diff line number Diff line change
Expand Up @@ -248,14 +248,45 @@ void ProcessTable_goThroughEntries(ProcessTable* super) {

proc->priority = kproc->ki_pri.pri_level - PZERO;

if (String_eq("intr", kproc->ki_comm) && (kproc->ki_flag & P_SYSTEM)) {
proc->nice = 0; //@etosan: intr kernel process (not thread) has weird nice value
} else if (kproc->ki_pri.pri_class == PRI_TIMESHARE) {
proc->nice = kproc->ki_nice - NZERO;
} else if (PRI_IS_REALTIME(kproc->ki_pri.pri_class)) {
proc->nice = PRIO_MIN - 1 - (PRI_MAX_REALTIME - kproc->ki_pri.pri_level);
} else {
proc->nice = PRIO_MAX + 1 + kproc->ki_pri.pri_level - PRI_MIN_IDLE;
switch (PRI_BASE(kproc->ki_pri.pri_class)) {
/* Handling of the below is explained in the FreeBSD base system in:
* /usr/src/usr.bin/top/machine.c (function format_nice) */
case PRI_ITHD:
fp->sched_class = SCHEDCLASS_INTR_THREAD;
proc->nice = 0;
break;

case PRI_REALTIME:
fp->sched_class = SCHEDCLASS_REALTIME;

/* Different for KPROCs and user procs */
if (kproc->ki_flag & P_KPROC) {
proc->nice = kproc->ki_pri.pri_native - PRI_MIN_REALTIME;
} else {
proc->nice = kproc->ki_pri.pri_user - PRI_MIN_REALTIME;
}
break;

case PRI_IDLE:
fp->sched_class = SCHEDCLASS_IDLE;

/* Different for KPROCs and user procs */
if (kproc->ki_flag & P_KPROC) {
proc->nice = kproc->ki_pri.pri_native - PRI_MIN_IDLE;
} else {
proc->nice = kproc->ki_pri.pri_user - PRI_MIN_IDLE;
}
break;

case PRI_TIMESHARE:
fp->sched_class = SCHEDCLASS_TIMESHARE;
proc->nice = kproc->ki_nice - NZERO;
break;

default:
fp->sched_class = SCHEDCLASS_UNKNOWN;
proc->nice = PROCESS_NICE_UNKNOWN;
break;
}

/* Taken from: https://github.com/freebsd/freebsd-src/blob/1ad2d87778970582854082bcedd2df0394fd4933/sys/sys/proc.h#L851 */
Expand Down
1 change: 1 addition & 0 deletions freebsd/ProcessField.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ in the source distribution for its full text.
JID = 100, \
JAIL = 101, \
EMULATION = 102, \
SCHEDCLASS = 103, \
\
DUMMY_BUMP_FIELD = CWD, \
// End of list
Expand Down
Loading

0 comments on commit 24216ef

Please sign in to comment.