From 283707c5e5bc436b78ea23bf5500cb6b16a01148 Mon Sep 17 00:00:00 2001 From: Daniel Lange Date: Mon, 11 Apr 2016 13:00:27 +0200 Subject: Imported Upstream version 0.9 --- Process.c | 129 +++++++++++++++++++++++++++++++++++++++----------------------- 1 file changed, 81 insertions(+), 48 deletions(-) (limited to 'Process.c') diff --git a/Process.c b/Process.c index 8f3762c..0f2f1f5 100644 --- a/Process.c +++ b/Process.c @@ -1,6 +1,6 @@ /* htop - Process.c -(C) 2004-2006 Hisham H. Muhammad +(C) 2004-2010 Hisham H. Muhammad Released under the GNU GPL, see the COPYING file in the source distribution for its full text. */ @@ -27,6 +27,7 @@ in the source distribution for its full text. #include #include #include +#include #ifdef HAVE_PLPA #include @@ -39,10 +40,20 @@ in the source distribution for its full text. #endif #define PAGE_SIZE_KB ( PAGE_SIZE / ONE_K ) -#define PROCESS_COMM_LEN 300 - /*{ +#ifndef Process_isKernelThread +#define Process_isKernelThread(_process) (_process->pgrp == 0) +#endif + +#ifndef Process_isUserlandThread +#define Process_isUserlandThread(_process) (_process->pid != _process->tgid) +#endif + +#ifndef Process_isThread +#define Process_isThread(_process) (Process_isUserlandThread(_process) || Process_isKernelThread(_process)) +#endif + typedef enum ProcessField_ { PID = 1, COMM, STATE, PPID, PGRP, SESSION, TTY_NR, TPGID, FLAGS, MINFLT, CMINFLT, MAJFLT, CMAJFLT, UTIME, STIME, CUTIME, CSTIME, PRIORITY, NICE, ITREALVALUE, STARTTIME, VSIZE, RSS, RLIM, STARTCODE, ENDCODE, @@ -58,6 +69,9 @@ typedef enum ProcessField_ { #ifdef HAVE_TASKSTATS RCHAR, WCHAR, SYSCR, SYSCW, RBYTES, WBYTES, CNCLWB, IO_READ_RATE, IO_WRITE_RATE, IO_RATE, #endif + #ifdef HAVE_CGROUP + CGROUP, + #endif LAST_PROCESSFIELD } ProcessField; @@ -69,16 +83,18 @@ typedef struct Process_ { struct ProcessList_ *pl; bool updated; - unsigned int pid; + pid_t pid; char* comm; int indent; char state; bool tag; - unsigned int ppid; + bool showChildren; + bool show; + pid_t ppid; unsigned int pgrp; unsigned int session; unsigned int tty_nr; - unsigned int tgid; + pid_t tgid; int tpgid; unsigned long int flags; #ifdef DEBUG @@ -94,9 +110,10 @@ typedef struct Process_ { long int priority; long int nice; long int nlwp; + char starttime_show[8]; + time_t starttime_ctime; #ifdef DEBUG long int itrealvalue; - unsigned long int starttime; unsigned long int vsize; long int rss; unsigned long int rlim; @@ -146,6 +163,9 @@ typedef struct Process_ { double io_rate_write_bps; unsigned long long io_rate_write_time; #endif + #ifdef HAVE_CGROUP + char* cgroup; + #endif } Process; }*/ @@ -156,7 +176,7 @@ char* PROCESS_CLASS = "Process"; #define PROCESS_CLASS NULL #endif -char *Process_fieldNames[] = { +const char *Process_fieldNames[] = { "", "PID", "Command", "STATE", "PPID", "PGRP", "SESSION", "TTY_NR", "TPGID", "FLAGS", "MINFLT", "CMINFLT", "MAJFLT", "CMAJFLT", "UTIME", "STIME", "CUTIME", "CSTIME", "PRIORITY", "NICE", "ITREALVALUE", @@ -175,14 +195,17 @@ char *Process_fieldNames[] = { "RCHAR", "WCHAR", "SYSCR", "SYSCW", "RBYTES", "WBYTES", "CNCLWB", "IO_READ_RATE", "IO_WRITE_RATE", "IO_RATE", #endif +#ifdef HAVE_CGROUP + "CGROUP", +#endif "*** report bug! ***" }; -char *Process_fieldTitles[] = { +const char *Process_fieldTitles[] = { "", " PID ", "Command ", "S ", " PPID ", " PGRP ", " SESN ", " TTY ", "TPGID ", "- ", "- ", "- ", "- ", "- ", " UTIME+ ", " STIME+ ", "- ", "- ", "PRI ", " NI ", "- ", - "- ", "- ", "- ", "- ", "- ", "- ", "- ", + "START ", "- ", "- ", "- ", "- ", "- ", "- ", "- ", "- ", "- ", "- ", "- ", "- ", "- ", "- ", "- ", "- ", "CPU ", " VIRT ", " RES ", " SHR ", " CODE ", " DATA ", " LIB ", " DIRTY ", " UID ", "CPU% ", "MEM% ", @@ -194,9 +217,13 @@ char *Process_fieldTitles[] = { " VXID ", #endif #ifdef HAVE_TASKSTATS - " RD_CHAR ", " WR_CHAR ", " RD_SYSC ", " WR_SYSC ", " IO_RD ", " IO_WR ", " IO_CANCEL ", + " RD_CHAR ", " WR_CHAR ", " RD_SYSC ", " WR_SYSC ", " IO_RBYTES ", " IO_WBYTES ", " IO_CANCEL ", " IORR ", " IOWR ", " IO ", #endif +#ifdef HAVE_CGROUP + " CGROUP ", +#endif +"*** report bug! ***" }; static int Process_getuid = -1; @@ -205,13 +232,18 @@ static int Process_getuid = -1; #define ONE_M (ONE_K * ONE_K) #define ONE_G (ONE_M * ONE_K) -static void Process_printLargeNumber(Process* this, RichString *str, unsigned long number) { +static void Process_printLargeNumber(Process* this, RichString* str, unsigned long number) { char buffer[11]; int len; if(number >= (10 * ONE_M)) { - len = snprintf(buffer, 10, "%3.1fG ", (float)number / ONE_M); - RichString_appendn(str, CRT_colors[LARGE_NUMBER], buffer, len); - } else if(number >= (100000)) { + if(number >= (100 * ONE_M)) { + len = snprintf(buffer, 10, "%4ldG ", number / ONE_M); + RichString_appendn(str, CRT_colors[LARGE_NUMBER], buffer, len); + } else { + len = snprintf(buffer, 10, "%3.1fG ", (float)number / ONE_M); + RichString_appendn(str, CRT_colors[LARGE_NUMBER], buffer, len); + } + } else if (number >= 100000) { len = snprintf(buffer, 10, "%4ldM ", number / ONE_K); int attr = this->pl->highlightMegabytes ? CRT_colors[PROCESS_MEGABYTES] @@ -254,10 +286,10 @@ static void Process_printTime(RichString* str, unsigned long t) { } static inline void Process_writeCommand(Process* this, int attr, int baseattr, RichString* str) { - int start = str->len; + int start = RichString_size(str); RichString_append(str, attr, this->comm); if (this->pl->highlightBaseName) { - int finish = str->len - 1; + int finish = RichString_size(str) - 1; int space = RichString_findChar(str, ' ', start); if (space != -1) finish = space - 1; @@ -287,10 +319,10 @@ static inline void Process_outputRate(Process* this, RichString* str, int attr, } static void Process_writeField(Process* this, RichString* str, ProcessField field) { - char buffer[PROCESS_COMM_LEN]; + char buffer[128]; buffer[127] = '\0'; int attr = CRT_colors[DEFAULT_COLOR]; int baseattr = CRT_colors[PROCESS_BASENAME]; - int n = PROCESS_COMM_LEN; + int n = sizeof(buffer) - 1; switch (field) { case PID: snprintf(buffer, n, "%5u ", this->pid); break; @@ -303,7 +335,7 @@ static void Process_writeField(Process* this, RichString* str, ProcessField fiel case PROCESSOR: snprintf(buffer, n, "%3d ", this->processor+1); break; case NLWP: snprintf(buffer, n, "%4ld ", this->nlwp); break; case COMM: { - if (this->pl->highlightThreads && (this->pid != this->tgid || this->m_size == 0)) { + if (this->pl->highlightThreads && Process_isThread(this)) { attr = CRT_colors[PROCESS_THREAD]; baseattr = CRT_colors[PROCESS_THREAD_BASENAME]; } @@ -325,9 +357,9 @@ static void Process_writeField(Process* this, RichString* str, ProcessField fiel n -= 4; } if (this->pl->direction == 1) - snprintf(buf, n, " `- "); + snprintf(buf, n, " `%s ", this->showChildren ? "-" : "+" ); else - snprintf(buf, n, " ,- "); + snprintf(buf, n, " ,%s ", this->showChildren ? "-" : "+" ); RichString_append(str, CRT_colors[PROCESS_TREE], buffer); Process_writeCommand(this, attr, baseattr, str); return; @@ -363,7 +395,7 @@ static void Process_writeField(Process* this, RichString* str, ProcessField fiel case M_SHARE: Process_printLargeNumber(this, str, this->m_share * PAGE_SIZE_KB); return; case ST_UID: snprintf(buffer, n, "%4d ", this->st_uid); break; case USER: { - if (Process_getuid != this->st_uid) + if (Process_getuid != (int) this->st_uid) attr = CRT_colors[PROCESS_SHADOW]; if (this->user) { snprintf(buffer, n, "%-8s ", this->user); @@ -399,6 +431,7 @@ static void Process_writeField(Process* this, RichString* str, ProcessField fiel } break; } + case STARTTIME: snprintf(buffer, n, "%s", this->starttime_show); break; #ifdef HAVE_OPENVZ case CTID: snprintf(buffer, n, "%5u ", this->ctid); break; case VPID: snprintf(buffer, n, "%5u ", this->vpid); break; @@ -418,6 +451,9 @@ static void Process_writeField(Process* this, RichString* str, ProcessField fiel case IO_WRITE_RATE: Process_outputRate(this, str, attr, buffer, n, this->io_rate_write_bps); return; case IO_RATE: Process_outputRate(this, str, attr, buffer, n, this->io_rate_read_bps + this->io_rate_write_bps); return; #endif + #ifdef HAVE_CGROUP + case CGROUP: snprintf(buffer, n, "%-10s ", this->cgroup); break; + #endif default: snprintf(buffer, n, "- "); @@ -428,10 +464,10 @@ static void Process_writeField(Process* this, RichString* str, ProcessField fiel static void Process_display(Object* cast, RichString* out) { Process* this = (Process*) cast; ProcessField* fields = this->pl->fields; - RichString_init(out); + RichString_prune(out); for (int i = 0; fields[i]; i++) Process_writeField(this, out, fields[i]); - if (this->pl->shadowOtherUsers && this->st_uid != Process_getuid) + if (this->pl->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]); @@ -442,6 +478,9 @@ void Process_delete(Object* cast) { Process* this = (Process*) cast; assert (this != NULL); if (this->comm) free(this->comm); +#ifdef HAVE_CGROUP + if (this->cgroup) free(this->cgroup); +#endif free(this); } @@ -453,36 +492,20 @@ Process* Process_new(struct ProcessList_ *pl) { this->pid = 0; this->pl = pl; this->tag = false; + this->showChildren = true; + this->show = true; this->updated = false; this->utime = 0; this->stime = 0; this->comm = NULL; this->indent = 0; +#ifdef HAVE_CGROUP + this->cgroup = NULL; +#endif if (Process_getuid == -1) Process_getuid = getuid(); return this; } -Process* Process_clone(Process* this) { - Process* clone = malloc(sizeof(Process)); - #if HAVE_TASKSTATS - this->io_rchar = 0; - this->io_wchar = 0; - this->io_syscr = 0; - this->io_syscw = 0; - this->io_read_bytes = 0; - this->io_rate_read_bps = 0; - this->io_rate_read_time = 0; - this->io_write_bytes = 0; - this->io_rate_write_bps = 0; - this->io_rate_write_time = 0; - this->io_cancelled_write_bytes = 0; - #endif - memcpy(clone, this, sizeof(Process)); - this->comm = NULL; - this->pid = 0; - return clone; -} - void Process_toggleTag(Process* this) { this->tag = this->tag == true ? false : true; } @@ -508,8 +531,8 @@ bool Process_setAffinity(Process* this, unsigned long mask) { } #endif -void Process_sendSignal(Process* this, int signal) { - kill(this->pid, signal); +void Process_sendSignal(Process* this, int sgn) { + kill(this->pid, sgn); } int Process_pidCompare(const void* v1, const void* v2) { @@ -574,6 +597,12 @@ int Process_compare(const void* v1, const void* v2) { return strcmp(p1->comm, p2->comm); case NLWP: return (p1->nlwp - p2->nlwp); + case STARTTIME: { + if (p1->starttime_ctime == p2->starttime_ctime) + return (p1->pid - p2->pid); + else + return (p1->starttime_ctime - p2->starttime_ctime); + } #ifdef HAVE_OPENVZ case CTID: return (p1->ctid - p2->ctid); @@ -596,6 +625,10 @@ int Process_compare(const void* v1, const void* v2) { case IO_WRITE_RATE: diff = p2->io_rate_write_bps - p1->io_rate_write_bps; goto test_diff; case IO_RATE: diff = (p2->io_rate_read_bps + p2->io_rate_write_bps) - (p1->io_rate_read_bps + p1->io_rate_write_bps); goto test_diff; #endif + #ifdef HAVE_CGROUP + case CGROUP: + return strcmp(p1->cgroup ? p1->cgroup : "", p2->cgroup ? p2->cgroup : ""); + #endif default: return (p1->pid - p2->pid); -- cgit v1.2.3