Merge branch 'cmdline-render-cache-rework' of BenBE/htop

This commit is contained in:
Daniel Lange 2022-06-01 08:00:55 +02:00
commit abaec509e6
11 changed files with 32 additions and 48 deletions

View File

@ -79,6 +79,7 @@ static HandlerResult AvailableMetersPanel_eventHandler(Panel* super, int ch) {
} }
if (update) { if (update) {
this->settings->changed = true; this->settings->changed = true;
this->settings->lastUpdate++;
Header_calculateHeight(header); Header_calculateHeight(header);
Header_updateData(header); Header_updateData(header);
Header_draw(header); Header_draw(header);

View File

@ -68,6 +68,7 @@ static HandlerResult ColorsPanel_eventHandler(Panel* super, int ch) {
this->settings->colorScheme = mark; this->settings->colorScheme = mark;
this->settings->changed = true; this->settings->changed = true;
this->settings->lastUpdate++;
CRT_setColors(mark); CRT_setColors(mark);
clear(); clear();

View File

@ -72,6 +72,7 @@ static HandlerResult DisplayOptionsPanel_eventHandler(Panel* super, int ch) {
if (result == HANDLED) { if (result == HANDLED) {
this->settings->changed = true; this->settings->changed = true;
this->settings->lastUpdate++;
Header* header = this->scr->header; Header* header = this->scr->header;
Header_calculateHeight(header); Header_calculateHeight(header);
Header_reinit(header); Header_reinit(header);

View File

@ -52,6 +52,7 @@ static HandlerResult HeaderOptionsPanel_eventHandler(Panel* super, int ch) {
Header_setLayout(this->scr->header, mark); Header_setLayout(this->scr->header, mark);
this->settings->changed = true; this->settings->changed = true;
this->settings->lastUpdate++;
ScreenManager_resize(this->scr); ScreenManager_resize(this->scr);

View File

@ -184,6 +184,7 @@ static HandlerResult MetersPanel_eventHandler(Panel* super, int ch) {
if (result == HANDLED || sideMove) { if (result == HANDLED || sideMove) {
Header* header = this->scr->header; Header* header = this->scr->header;
this->settings->changed = true; this->settings->changed = true;
this->settings->lastUpdate++;
Header_calculateHeight(header); Header_calculateHeight(header);
ScreenManager_resize(this->scr); ScreenManager_resize(this->scr);
} }

View File

@ -414,6 +414,8 @@ void Process_makeCommandStr(Process* this) {
bool stripExeFromCmdline = settings->stripExeFromCmdline; bool stripExeFromCmdline = settings->stripExeFromCmdline;
bool showThreadNames = settings->showThreadNames; bool showThreadNames = settings->showThreadNames;
uint64_t settingsStamp = settings->lastUpdate;
/* Nothing to do to (Re)Generate the Command string, if the process is: /* Nothing to do to (Re)Generate the Command string, if the process is:
* - a kernel thread, or * - a kernel thread, or
* - a zombie from before being under htop's watch, or * - a zombie from before being under htop's watch, or
@ -422,52 +424,27 @@ void Process_makeCommandStr(Process* this) {
return; return;
if (this->state == ZOMBIE && !this->mergedCommand.str) if (this->state == ZOMBIE && !this->mergedCommand.str)
return; return;
if (Process_isUserlandThread(this) && settings->showThreadNames && (showThreadNames == mc->prevShowThreadNames) && (mc->prevMergeSet == showMergedCommand))
return;
/* this->mergedCommand.str needs updating only if its state or contents changed. /* this->mergedCommand.str needs updating only if its state or contents changed.
* Its content is based on the fields cmdline, comm, and exe. */ * Its content is based on the fields cmdline, comm, and exe. */
if ( if (mc->lastUpdate >= settingsStamp)
mc->prevMergeSet == showMergedCommand &&
mc->prevPathSet == showProgramPath &&
mc->prevCommSet == searchCommInCmdline &&
mc->prevCmdlineSet == stripExeFromCmdline &&
mc->prevShowThreadNames == showThreadNames &&
!mc->cmdlineChanged &&
!mc->commChanged &&
!mc->exeChanged
) {
return; return;
}
mc->lastUpdate = settingsStamp;
/* The field separtor "│" has been chosen such that it will not match any /* The field separtor "│" has been chosen such that it will not match any
* valid string used for searching or filtering */ * valid string used for searching or filtering */
const char* SEPARATOR = CRT_treeStr[TREE_STR_VERT]; const char* SEPARATOR = CRT_treeStr[TREE_STR_VERT];
const int SEPARATOR_LEN = strlen(SEPARATOR); const int SEPARATOR_LEN = strlen(SEPARATOR);
/* Check for any changed fields since we last built this string */ /* Accommodate the column text, two field separators and terminating NUL */
if (mc->cmdlineChanged || mc->commChanged || mc->exeChanged) { size_t maxLen = 2 * SEPARATOR_LEN + 1;
free(mc->str); maxLen += this->cmdline ? strlen(this->cmdline) : strlen("(zombie)");
/* Accommodate the column text, two field separators and terminating NUL */ maxLen += this->procComm ? strlen(this->procComm) : 0;
size_t maxLen = 2 * SEPARATOR_LEN + 1; maxLen += this->procExe ? strlen(this->procExe) : 0;
maxLen += this->cmdline ? strlen(this->cmdline) : strlen("(zombie)");
maxLen += this->procComm ? strlen(this->procComm) : 0;
maxLen += this->procExe ? strlen(this->procExe) : 0;
mc->str = xCalloc(1, maxLen); free(mc->str);
} mc->str = xCalloc(1, maxLen);
/* Preserve the settings used in this run */
mc->prevMergeSet = showMergedCommand;
mc->prevPathSet = showProgramPath;
mc->prevCommSet = searchCommInCmdline;
mc->prevCmdlineSet = stripExeFromCmdline;
mc->prevShowThreadNames = showThreadNames;
/* Mark everything as unchanged */
mc->cmdlineChanged = false;
mc->commChanged = false;
mc->exeChanged = false;
/* Reset all locations that need extra handling when actually displaying */ /* Reset all locations that need extra handling when actually displaying */
mc->highlightCount = 0; mc->highlightCount = 0;
@ -1204,7 +1181,8 @@ void Process_updateComm(Process* this, const char* comm) {
free(this->procComm); free(this->procComm);
this->procComm = comm ? xStrdup(comm) : NULL; this->procComm = comm ? xStrdup(comm) : NULL;
this->mergedCommand.commChanged = true;
this->mergedCommand.lastUpdate = 0;
} }
static int skipPotentialPath(const char* cmdline, int end) { static int skipPotentialPath(const char* cmdline, int end) {
@ -1244,7 +1222,8 @@ void Process_updateCmdline(Process* this, const char* cmdline, int basenameStart
this->cmdline = cmdline ? xStrdup(cmdline) : NULL; this->cmdline = cmdline ? xStrdup(cmdline) : NULL;
this->cmdlineBasenameStart = (basenameStart || !cmdline) ? basenameStart : skipPotentialPath(cmdline, basenameEnd); this->cmdlineBasenameStart = (basenameStart || !cmdline) ? basenameStart : skipPotentialPath(cmdline, basenameEnd);
this->cmdlineBasenameEnd = basenameEnd; this->cmdlineBasenameEnd = basenameEnd;
this->mergedCommand.cmdlineChanged = true;
this->mergedCommand.lastUpdate = 0;
} }
void Process_updateExe(Process* this, const char* exe) { void Process_updateExe(Process* this, const char* exe) {
@ -1263,7 +1242,8 @@ void Process_updateExe(Process* this, const char* exe) {
this->procExe = NULL; this->procExe = NULL;
this->procExeBasenameOffset = 0; this->procExeBasenameOffset = 0;
} }
this->mergedCommand.exeChanged = true;
this->mergedCommand.lastUpdate = 0;
} }
uint8_t Process_fieldWidths[LAST_PROCESSFIELD] = { 0 }; uint8_t Process_fieldWidths[LAST_PROCESSFIELD] = { 0 };

View File

@ -96,17 +96,10 @@ typedef struct ProcessCmdlineHighlight_ {
* Process_writeCommand to color the string. str will be NULL for kernel * Process_writeCommand to color the string. str will be NULL for kernel
* threads and zombies */ * threads and zombies */
typedef struct ProcessMergedCommand_ { typedef struct ProcessMergedCommand_ {
uint64_t lastUpdate; /* Marker based on settings->lastUpdate to track when the rendering needs refreshing */
char* str; /* merged Command string */ char* str; /* merged Command string */
size_t highlightCount; /* how many portions of cmdline to highlight */ size_t highlightCount; /* how many portions of cmdline to highlight */
ProcessCmdlineHighlight highlights[8]; /* which portions of cmdline to highlight */ ProcessCmdlineHighlight highlights[8]; /* which portions of cmdline to highlight */
bool cmdlineChanged : 1; /* whether cmdline changed */
bool exeChanged : 1; /* whether exe changed */
bool commChanged : 1; /* whether comm changed */
bool prevMergeSet : 1; /* whether showMergedCommand was set */
bool prevPathSet : 1; /* whether showProgramPath was set */
bool prevCommSet : 1; /* whether findCommInCmdline was set */
bool prevCmdlineSet : 1; /* whether stripExeFromCmdline was set */
bool prevShowThreadNames : 1; /* whether showThreadNames was set */
} ProcessMergedCommand; } ProcessMergedCommand;
typedef struct Process_ { typedef struct Process_ {

View File

@ -315,6 +315,7 @@ void ScreensPanel_update(Panel* super) {
ScreensPanel* this = (ScreensPanel*) super; ScreensPanel* this = (ScreensPanel*) super;
int size = Panel_size(super); int size = Panel_size(super);
this->settings->changed = true; this->settings->changed = true;
this->settings->lastUpdate++;
this->settings->screens = xReallocArray(this->settings->screens, size + 1, sizeof(ScreenSettings*)); this->settings->screens = xReallocArray(this->settings->screens, size + 1, sizeof(ScreenSettings*));
for (int i = 0; i < size; i++) { for (int i = 0; i < size; i++) {
ScreenListItem* item = (ScreenListItem*) Panel_get(super, i); ScreenListItem* item = (ScreenListItem*) Panel_get(super, i);

View File

@ -762,6 +762,8 @@ Settings* Settings_new(unsigned int initialCpuCount, Hashtable* dynamicColumns)
this->ssIndex = 0; this->ssIndex = 0;
this->ss = this->screens[this->ssIndex]; this->ss = this->screens[this->ssIndex];
this->lastUpdate = 1;
return this; return this;
} }

View File

@ -96,6 +96,7 @@ typedef struct Settings_ {
#endif #endif
bool changed; bool changed;
uint64_t lastUpdate;
} Settings; } Settings;
#define Settings_cpuId(settings, cpu) ((settings)->countCPUsFromOne ? (cpu)+1 : (cpu)) #define Settings_cpuId(settings, cpu) ((settings)->countCPUsFromOne ? (cpu)+1 : (cpu))

View File

@ -1351,7 +1351,8 @@ static bool LinuxProcessList_readCmdlineFile(Process* process, openat_arg_t proc
if (process->procExeDeleted) if (process->procExeDeleted)
filename[filenameLen - markerLen] = '\0'; filename[filenameLen - markerLen] = '\0';
process->mergedCommand.exeChanged |= oldExeDeleted ^ process->procExeDeleted; if (oldExeDeleted != process->procExeDeleted)
process->mergedCommand.lastUpdate = 0;
} }
Process_updateExe(process, filename); Process_updateExe(process, filename);
@ -1557,7 +1558,8 @@ static bool LinuxProcessList_recurseProcTree(LinuxProcessList* this, openat_arg_
lp->m_lrs = (proc->isUserlandThread && parent) ? ((const LinuxProcess*)parent)->m_lrs : 0; lp->m_lrs = (proc->isUserlandThread && parent) ? ((const LinuxProcess*)parent)->m_lrs : 0;
} }
proc->mergedCommand.exeChanged |= prev ^ proc->usesDeletedLib; if (prev != proc->usesDeletedLib)
proc->mergedCommand.lastUpdate = 0;
} }
if ((ss->flags & PROCESS_FLAG_LINUX_SMAPS) && !Process_isKernelThread(proc)) { if ((ss->flags & PROCESS_FLAG_LINUX_SMAPS) && !Process_isKernelThread(proc)) {