Skip to content

Commit

Permalink
Add to setup menu, use CLOCK_MONOTONIC, use H instead of I for flag
Browse files Browse the repository at this point in the history
  • Loading branch information
adsr committed Oct 13, 2020
1 parent d5f0832 commit cf1f9a7
Show file tree
Hide file tree
Showing 9 changed files with 63 additions and 29 deletions.
1 change: 1 addition & 0 deletions DisplayOptionsPanel.c
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ DisplayOptionsPanel* DisplayOptionsPanel_new(Settings* settings, ScreenManager*
Panel_add(super, (Object*) CheckItem_newByRef(xStrdup("Also show CPU percentage numerically"), &(settings->showCPUUsage)));
Panel_add(super, (Object*) CheckItem_newByRef(xStrdup("Also show CPU frequency"), &(settings->showCPUFrequency)));
Panel_add(super, (Object*) CheckItem_newByRef(xStrdup("Enable the mouse"), &(settings->enableMouse)));
Panel_add(super, (Object*) CheckItem_newByRef(xStrdup("Highlight new and old processes"), &(settings->highlightNewOldProcs)));
#ifdef HAVE_LIBHWLOC
Panel_add(super, (Object*) CheckItem_newByRef(xStrdup("Show topology when selecting affinity by default"), &(settings->topologyAffinity)));
#endif
Expand Down
24 changes: 12 additions & 12 deletions Process.c
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ in the source distribution for its full text.
*/

#include "Process.h"
#include "ProcessList.h"
#include "Settings.h"

#include "config.h"
Expand All @@ -30,7 +31,6 @@ in the source distribution for its full text.
#include <time.h>
#include <assert.h>
#include <math.h>
#include <time.h>
#ifdef MAJOR_IN_MKDEV
#include <sys/mkdev.h>
#elif defined(MAJOR_IN_SYSMACROS)
Expand Down Expand Up @@ -370,18 +370,19 @@ void Process_writeField(const Process* this, RichString* str, ProcessField field
void Process_display(const Object* cast, RichString* out) {
const Process* this = (const Process*) cast;
const ProcessField* fields = this->settings->fields;
time_t now = time(NULL);
RichString_prune(out);
for (int i = 0; fields[i]; i++)
As_Process(this)->writeField(this, out, fields[i]);
if (this->settings->shadowOtherUsers && (int)this->st_uid != Process_getuid)
RichString_setAttr(out, CRT_colors[PROCESS_SHADOW]);
if (this->tag == true)
RichString_setAttr(out, CRT_colors[PROCESS_TAG]);
if (Process_isNew(this, now))
RichString_setAttr(out, CRT_colors[PROCESS_NEW]);
if (Process_isTomb(this))
RichString_setAttr(out, CRT_colors[PROCESS_TOMB]);
if (this->settings->highlightNewOldProcs) {
if (Process_isNew(this))
RichString_setAttr(out, CRT_colors[PROCESS_NEW]);
if (Process_isTomb(this))
RichString_setAttr(out, CRT_colors[PROCESS_TOMB]);
}
assert(out->chlen > 0);
}

Expand Down Expand Up @@ -414,15 +415,14 @@ void Process_toggleTag(Process* this) {
this->tag = this->tag == true ? false : true;
}

bool Process_isNew(const Process* this, const time_t now) {
if (this->starttime_ctime > 0) {
return (now - this->starttime_ctime) <= 5;
}
return false;
bool Process_isNew(const Process* this) {
if (_scanTs >= this->seenTs)
return (_scanTs - this->seenTs <= HIGHLIGHT_NSEC);
return false;
}

bool Process_isTomb(const Process* this) {
return (this->tomb_time > 0);
return (this->tombTs > 0);
}

bool Process_setPriority(Process* this, int priority) {
Expand Down
7 changes: 5 additions & 2 deletions Process.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ in the source distribution for its full text.
#include "Object.h"

#define PROCESS_FLAG_IO 0x0001
#define HIGHLIGHT_NSEC 5

typedef enum ProcessFields {
NULL_PROCESSFIELD = 0,
Expand Down Expand Up @@ -86,7 +87,6 @@ typedef struct Process_ {
uid_t st_uid;
unsigned long int flags;
int processor;
time_t tomb_time;

float percent_cpu;
float percent_mem;
Expand All @@ -103,6 +103,9 @@ typedef struct Process_ {

int exit_signal;

long seenTs;
long tombTs;

unsigned long int minflt;
unsigned long int majflt;
} Process;
Expand Down Expand Up @@ -170,7 +173,7 @@ void Process_init(Process* this, struct Settings_* settings);

void Process_toggleTag(Process* this);

bool Process_isNew(const Process* this, const time_t now);
bool Process_isNew(const Process* this);

bool Process_isTomb(const Process* this);

Expand Down
30 changes: 26 additions & 4 deletions ProcessList.c
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ in the source distribution for its full text.
#include <string.h>
#include <time.h>

long _scanTs = 0;

ProcessList* ProcessList_init(ProcessList* this, const ObjectClass* klass, UsersTable* usersTable, Hashtable* pidMatchList, uid_t userId) {
this->processes = Vector_new(klass, true, DEFAULT_SIZE);
Expand All @@ -29,6 +30,9 @@ ProcessList* ProcessList_init(ProcessList* this, const ObjectClass* klass, Users
// set later by platform-specific code
this->cpuCount = 0;

this->scanTs = 0;
this->firstScanTs = 0;

#ifdef HAVE_LIBHWLOC
this->topologyOk = false;
if (hwloc_topology_init(&this->topology) == 0) {
Expand Down Expand Up @@ -84,6 +88,13 @@ void ProcessList_add(ProcessList* this, Process* p) {
assert(Vector_indexOf(this->processes, p, Process_pidCompare) == -1);
assert(Hashtable_get(this->processTable, p->pid) == NULL);

if (this->scanTs == this->firstScanTs) {
// prevent highlighting processes found in first scan
p->seenTs = this->firstScanTs - HIGHLIGHT_NSEC - 1;
} else {
p->seenTs = this->scanTs;
}

Vector_add(this->processes, p);
Hashtable_put(this->processTable, p->pid, p);

Expand Down Expand Up @@ -284,6 +295,7 @@ Process* ProcessList_getProcess(ProcessList* this, pid_t pid, bool* preExisting,
}

void ProcessList_scan(ProcessList* this) {
struct timespec now;

// mark all process as "dirty"
for (int i = 0; i < Vector_size(this->processes); i++) {
Expand All @@ -297,20 +309,30 @@ void ProcessList_scan(ProcessList* this) {
this->kernelThreads = 0;
this->runningTasks = 0;

// set scanTs
if (clock_gettime(CLOCK_MONOTONIC, &now) == 0) {
if (this->firstScanTs == 0) {
this->firstScanTs = (long)now.tv_sec;
}
this->scanTs = (long)now.tv_sec;
_scanTs = this->scanTs;
}

ProcessList_goThroughEntries(this);

for (int i = Vector_size(this->processes) - 1; i >= 0; i--) {
Process* p = (Process*) Vector_get(this->processes, i);
if (p->tomb_time > 0) {

if (p->tombTs > 0) {
// remove tombed process
if (time(NULL) >= p->tomb_time) {
if (this->scanTs >= p->tombTs) {
ProcessList_remove(this, p);
}
} else if (p->updated == false) {
// process no longer exists
if (this->settings->highlightNewOldNsec > 0) {
if (this->settings->highlightNewOldProcs) {
// mark tombed
p->tomb_time = time(NULL) + this->settings->highlightNewOldNsec;
p->tombTs = this->scanTs + HIGHLIGHT_NSEC;
} else {
// immediately remove
ProcessList_remove(this, p);
Expand Down
4 changes: 4 additions & 0 deletions ProcessList.h
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,8 @@ typedef struct ProcessList_ {

int cpuCount;

long scanTs;
long firstScanTs;
} ProcessList;

ProcessList* ProcessList_new(UsersTable* ut, Hashtable* pidMatchList, uid_t userId);
Expand Down Expand Up @@ -97,4 +99,6 @@ Process* ProcessList_getProcess(ProcessList* this, pid_t pid, bool* preExisting,

void ProcessList_scan(ProcessList* this);

extern long _scanTs;

#endif
1 change: 1 addition & 0 deletions ScreenManager.c
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@ static void checkRecalculation(ScreenManager* this, double* oldTime, int* sortTi
struct timeval tv;
gettimeofday(&tv, NULL);
double newTime = ((double)tv.tv_sec * 10) + ((double)tv.tv_usec / 100000);

*timedOut = (newTime - *oldTime > this->settings->delay);
*rescan = *rescan || *timedOut;
if (newTime < *oldTime) *rescan = true; // clock was adjusted?
Expand Down
2 changes: 1 addition & 1 deletion Settings.h
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ typedef struct Settings_ {
bool highlightBaseName;
bool highlightMegabytes;
bool highlightThreads;
int highlightNewOldNsec;
bool highlightNewOldProcs;
bool updateProcessNames;
bool accountGuestInCPUMeter;
bool headerMargin;
Expand Down
5 changes: 4 additions & 1 deletion htop.1.in
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ htop \- interactive process viewer
.SH "SYNOPSIS"
.LP
.B htop
.RB [ \-dCFhpustv ]
.RB [ \-dCFhpustvH ]
.SH "DESCRIPTION"
.LP
.B htop
Expand Down Expand Up @@ -62,6 +62,9 @@ Output version information and exit
.TP
\fB\-t \-\-tree
Show processes in tree view
.TP
\fB\-H \-\-highlight-new-old\fR
Highlight new and old processes
.SH "INTERACTIVE COMMANDS"
.LP
The following commands are supported while in
Expand Down
18 changes: 9 additions & 9 deletions htop.c
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ static void printHelpFlag(void) {
"-u --user[=USERNAME] Show only processes for a given user (or $USER)\n"
"-U --no-unicode Do not use unicode but plain ASCII\n"
"-V --version Print version info\n"
"-I --highlight-new-old=N Highlight new and old processes for N seconds\n"
"-H --highlight-new-old Highlight new and old processes\n"
"\n"
"Long options may be passed with a single dash.\n\n"
"Press F1 inside htop for online help.\n"
Expand All @@ -68,7 +68,7 @@ typedef struct CommandLineSettings_ {
bool enableMouse;
bool treeView;
bool allowUnicode;
int highlightNewOldNsec;
bool highlightNewOldProcs;
} CommandLineSettings;

static CommandLineSettings parseArguments(int argc, char** argv) {
Expand All @@ -83,7 +83,7 @@ static CommandLineSettings parseArguments(int argc, char** argv) {
.enableMouse = true,
.treeView = false,
.allowUnicode = true,
.highlightNewOldNsec = 0,
.highlightNewOldProcs = false
};

static struct option long_opts[] =
Expand All @@ -100,13 +100,13 @@ static CommandLineSettings parseArguments(int argc, char** argv) {
{"tree", no_argument, 0, 't'},
{"pid", required_argument, 0, 'p'},
{"filter", required_argument, 0, 'F'},
{"highlight-new-old", required_argument, 0, 'I'},
{"highlight-new-old", no_argument, 0, 'H'},
{0,0,0,0}
};

int opt, opti=0;
/* Parse arguments */
while ((opt = getopt_long(argc, argv, "hVMCs:td:u::Up:F:I:", long_opts, &opti))) {
while ((opt = getopt_long(argc, argv, "hVMCs:td:u::Up:F:H", long_opts, &opti))) {
if (opt == EOF) break;
switch (opt) {
case 'h':
Expand Down Expand Up @@ -193,8 +193,8 @@ static CommandLineSettings parseArguments(int argc, char** argv) {

break;
}
case 'I': {
flags.highlightNewOldNsec = optarg ? atoi(optarg) : 0;
case 'H': {
flags.highlightNewOldProcs = true;
break;
}
default:
Expand Down Expand Up @@ -270,8 +270,8 @@ int main(int argc, char** argv) {
settings->enableMouse = false;
if (flags.treeView)
settings->treeView = true;
if (flags.highlightNewOldNsec > 0)
settings->highlightNewOldNsec = flags.highlightNewOldNsec;
if (flags.highlightNewOldProcs)
settings->highlightNewOldProcs = true;

CRT_init(settings->delay, settings->colorScheme, flags.allowUnicode);

Expand Down

0 comments on commit cf1f9a7

Please sign in to comment.