diff options
author | Hisham Muhammad <hisham@gobolinux.org> | 2015-03-16 01:43:04 -0300 |
---|---|---|
committer | Hisham Muhammad <hisham@gobolinux.org> | 2015-03-16 01:43:04 -0300 |
commit | be1700cf9479f4199bb3810db831da943691b8f7 (patch) | |
tree | a4b6689f58ea8abe6db455aa8976bd6cc5377fd8 /linux | |
parent | bc928d7f47f787514d2eb58d4f1083e0160ad167 (diff) |
Isolate portable and Linux-specific process fields.
Diffstat (limited to 'linux')
-rw-r--r-- | linux/LinuxProcess.c | 348 | ||||
-rw-r--r-- | linux/LinuxProcess.h | 111 | ||||
-rw-r--r-- | linux/LinuxProcessList.c | 135 | ||||
-rw-r--r-- | linux/Platform.c | 7 | ||||
-rw-r--r-- | linux/Platform.h | 2 |
5 files changed, 437 insertions, 166 deletions
diff --git a/linux/LinuxProcess.c b/linux/LinuxProcess.c index c45e7d9d..8ed0ebc6 100644 --- a/linux/LinuxProcess.c +++ b/linux/LinuxProcess.c @@ -13,6 +13,7 @@ in the source distribution for its full text. #include <stdlib.h> #include <unistd.h> +#include <string.h> #include <sys/syscall.h> /*{ @@ -22,28 +23,66 @@ in the source distribution for its full text. #define PROCESS_FLAG_LINUX_VSERVER 0x0400 #define PROCESS_FLAG_LINUX_CGROUP 0x0800 +typedef enum UnsupportedProcessFields { + FLAGS = 9, + ITREALVALUE = 20, + VSIZE = 22, + RSS = 23, + RLIM = 24, + STARTCODE = 25, + ENDCODE = 26, + STARTSTACK = 27, + KSTKESP = 28, + KSTKEIP = 29, + SIGNAL = 30, + BLOCKED = 31, + SSIGIGNORE = 32, + SIGCATCH = 33, + WCHAN = 34, + NSWAP = 35, + CNSWAP = 36, + EXIT_SIGNAL = 37, +} UnsupportedProcessField; + typedef enum LinuxProcessFields { - 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, - STARTSTACK, KSTKESP, KSTKEIP, SIGNAL, BLOCKED, SSIGIGNORE, SIGCATCH, WCHAN, NSWAP, CNSWAP, EXIT_SIGNAL, - PROCESSOR, M_SIZE, M_RESIDENT, M_SHARE, M_TRS, M_DRS, M_LRS, M_DT, ST_UID, PERCENT_CPU, PERCENT_MEM, - USER, TIME, NLWP, TGID, + CMINFLT = 11, + CMAJFLT = 13, + UTIME = 14, + STIME = 15, + CUTIME = 16, + CSTIME = 17, + PROCESSOR = 38, + M_SHARE = 41, + M_TRS = 42, + M_DRS = 43, + M_LRS = 44, + M_DT = 45, #ifdef HAVE_OPENVZ - CTID, VPID, + CTID = 100, + VPID = 101, #endif #ifdef HAVE_VSERVER - VXID, + VXID = 102, #endif #ifdef HAVE_TASKSTATS - RCHAR, WCHAR, SYSCR, SYSCW, RBYTES, WBYTES, CNCLWB, IO_READ_RATE, IO_WRITE_RATE, IO_RATE, + RCHAR = 103, + WCHAR = 104, + SYSCR = 105, + SYSCW = 106, + RBYTES = 107, + WBYTES = 108, + CNCLWB = 109, + IO_READ_RATE = 110, + IO_WRITE_RATE = 111, + IO_RATE = 112, #endif #ifdef HAVE_CGROUP - CGROUP, + CGROUP = 113, #endif #ifdef HAVE_OOM - OOM, + OOM = 114, #endif - IO_PRIORITY, + IO_PRIORITY = 115, LAST_PROCESSFIELD } LinuxProcessField; @@ -52,93 +91,137 @@ typedef enum LinuxProcessFields { typedef struct LinuxProcess_ { Process super; IOPriority ioPriority; + unsigned long int cminflt; + unsigned long int cmajflt; + unsigned long long int utime; + unsigned long long int stime; + unsigned long long int cutime; + unsigned long long int cstime; + int processor; + long m_share; + long m_trs; + long m_drs; + long m_lrs; + long m_dt; + #ifdef HAVE_TASKSTATS + unsigned long long io_rchar; + unsigned long long io_wchar; + unsigned long long io_syscr; + unsigned long long io_syscw; + unsigned long long io_read_bytes; + unsigned long long io_write_bytes; + unsigned long long io_cancelled_write_bytes; + unsigned long long io_rate_read_time; + unsigned long long io_rate_write_time; + double io_rate_read_bps; + double io_rate_write_bps; + #endif + #ifdef HAVE_OPENVZ + unsigned int ctid; + unsigned int vpid; + #endif + #ifdef HAVE_VSERVER + unsigned int vxid; + #endif + #ifdef HAVE_CGROUP + char* cgroup; + #endif + #ifdef HAVE_OOM + unsigned int oom; + #endif } LinuxProcess; -#define Process_delete LinuxProcess_delete +#ifndef Process_isKernelThread +#define Process_isKernelThread(_process) (_process->pgrp == 0) +#endif + +#ifndef Process_isUserlandThread +#define Process_isUserlandThread(_process) (_process->pid != _process->tgid) +#endif }*/ ProcessFieldData Process_fields[] = { - { .name = "", .title = NULL, .description = NULL, .flags = 0, }, - { .name = "PID", .title = " PID ", .description = "Process/thread ID", .flags = 0, }, - { .name = "Command", .title = "Command ", .description = "Command line", .flags = 0, }, - { .name = "STATE", .title = "S ", .description = "Process state (S sleeping, R running, D disk, Z zombie, T traced, W paging)", .flags = 0, }, - { .name = "PPID", .title = " PPID ", .description = "Parent process ID", .flags = 0, }, - { .name = "PGRP", .title = " PGRP ", .description = "Process group ID", .flags = 0, }, - { .name = "SESSION", .title = " SESN ", .description = "Process's session ID", .flags = 0, }, - { .name = "TTY_NR", .title = " TTY ", .description = "Controlling terminal", .flags = 0, }, - { .name = "TPGID", .title = " TPGID ", .description = "Process ID of the fg process group of the controlling terminal", .flags = 0, }, - { .name = "FLAGS", .title = NULL, .description = NULL, .flags = 0, }, - { .name = "MINFLT", .title = " MINFLT ", .description = "Number of minor faults which have not required loading a memory page from disk", .flags = 0, }, - { .name = "CMINFLT", .title = " CMINFLT ", .description = "Children processes' minor faults", .flags = 0, }, - { .name = "MAJFLT", .title = " MAJFLT ", .description = "Number of major faults which have required loading a memory page from disk", .flags = 0, }, - { .name = "CMAJFLT", .title = " CMAJFLT ", .description = "Children processes' major faults", .flags = 0, }, - { .name = "UTIME", .title = " UTIME+ ", .description = "User CPU time - time the process spent executing in user mode", .flags = 0, }, - { .name = "STIME", .title = " STIME+ ", .description = "System CPU time - time the kernel spent running system calls for this process", .flags = 0, }, - { .name = "CUTIME", .title = " CUTIME+ ", .description = "Children processes' user CPU time", .flags = 0, }, - { .name = "CSTIME", .title = " CSTIME+ ", .description = "Children processes' system CPU time", .flags = 0, }, - { .name = "PRIORITY", .title = "PRI ", .description = "Kernel's internal priority for the process", .flags = 0, }, - { .name = "NICE", .title = " NI ", .description = "Nice value (the higher the value, the more it lets other processes take priority)", .flags = 0, }, - { .name = "ITREALVALUE", .title = NULL, .description = NULL, .flags = 0, }, - { .name = "STARTTIME", .title = "START ", .description = "Time the process was started", .flags = 0, }, - { .name = "VSIZE", .title = NULL, .description = NULL, .flags = 0, }, - { .name = "RSS", .title = NULL, .description = NULL, .flags = 0, }, - { .name = "RLIM", .title = NULL, .description = NULL, .flags = 0, }, - { .name = "STARTCODE", .title = NULL, .description = NULL, .flags = 0, }, - { .name = "ENDCODE", .title = NULL, .description = NULL, .flags = 0, }, - { .name = "STARTSTACK", .title = NULL, .description = NULL, .flags = 0, }, - { .name = "KSTKESP", .title = NULL, .description = NULL, .flags = 0, }, - { .name = "KSTKEIP", .title = NULL, .description = NULL, .flags = 0, }, - { .name = "SIGNAL", .title = NULL, .description = NULL, .flags = 0, }, - { .name = "BLOCKED", .title = NULL, .description = NULL, .flags = 0, }, - { .name = "SIGIGNORE", .title = NULL, .description = NULL, .flags = 0, }, - { .name = "SIGCATCH", .title = NULL, .description = NULL, .flags = 0, }, - { .name = "WCHAN", .title = NULL, .description = NULL, .flags = 0, }, - { .name = "NSWAP", .title = NULL, .description = NULL, .flags = 0, }, - { .name = "CNSWAP", .title = NULL, .description = NULL, .flags = 0, }, - { .name = "EXIT_SIGNAL", .title = NULL, .description = NULL, .flags = 0, }, - { .name = "PROCESSOR", .title = "CPU ", .description = "Id of the CPU the process last executed on", .flags = 0, }, - { .name = "M_SIZE", .title = " VIRT ", .description = "Total program size in virtual memory", .flags = 0, }, - { .name = "M_RESIDENT", .title = " RES ", .description = "Resident set size, size of the text and data sections, plus stack usage", .flags = 0, }, - { .name = "M_SHARE", .title = " SHR ", .description = "Size of the process's shared pages", .flags = 0, }, - { .name = "M_TRS", .title = " CODE ", .description = "Size of the text segment of the process", .flags = 0, }, - { .name = "M_DRS", .title = " DATA ", .description = "Size of the data segment plus stack usage of the process", .flags = 0, }, - { .name = "M_LRS", .title = " LIB ", .description = "The library size of the process", .flags = 0, }, - { .name = "M_DT", .title = " DIRTY ", .description = "Size of the dirty pages of the process", .flags = 0, }, - { .name = "ST_UID", .title = " UID ", .description = "User ID of the process owner", .flags = 0, }, - { .name = "PERCENT_CPU", .title = "CPU% ", .description = "Percentage of the CPU time the process used in the last sampling", .flags = 0, }, - { .name = "PERCENT_MEM", .title = "MEM% ", .description = "Percentage of the memory the process is using, based on resident memory size", .flags = 0, }, - { .name = "USER", .title = "USER ", .description = "Username of the process owner (or user ID if name cannot be determined)", .flags = 0, }, - { .name = "TIME", .title = " TIME+ ", .description = "Total time the process has spent in user and system time", .flags = 0, }, - { .name = "NLWP", .title = "NLWP ", .description = "Number of threads in the process", .flags = 0, }, - { .name = "TGID", .title = " TGID ", .description = "Thread group ID (i.e. process ID)", .flags = 0, }, + [0] = { .name = "", .title = NULL, .description = NULL, .flags = 0, }, + [PID] = { .name = "PID", .title = " PID ", .description = "Process/thread ID", .flags = 0, }, + [COMM] = { .name = "Command", .title = "Command ", .description = "Command line", .flags = 0, }, + [STATE] = { .name = "STATE", .title = "S ", .description = "Process state (S sleeping, R running, D disk, Z zombie, T traced, W paging)", .flags = 0, }, + [PPID] = { .name = "PPID", .title = " PPID ", .description = "Parent process ID", .flags = 0, }, + [PGRP] = { .name = "PGRP", .title = " PGRP ", .description = "Process group ID", .flags = 0, }, + [SESSION] = { .name = "SESSION", .title = " SESN ", .description = "Process's session ID", .flags = 0, }, + [TTY_NR] = { .name = "TTY_NR", .title = " TTY ", .description = "Controlling terminal", .flags = 0, }, + [TPGID] = { .name = "TPGID", .title = " TPGID ", .description = "Process ID of the fg process group of the controlling terminal", .flags = 0, }, + [FLAGS] = { .name = "FLAGS", .title = NULL, .description = NULL, .flags = 0, }, + [MINFLT] = { .name = "MINFLT", .title = " MINFLT ", .description = "Number of minor faults which have not required loading a memory page from disk", .flags = 0, }, + [CMINFLT] = { .name = "CMINFLT", .title = " CMINFLT ", .description = "Children processes' minor faults", .flags = 0, }, + [MAJFLT] = { .name = "MAJFLT", .title = " MAJFLT ", .description = "Number of major faults which have required loading a memory page from disk", .flags = 0, }, + [CMAJFLT] = { .name = "CMAJFLT", .title = " CMAJFLT ", .description = "Children processes' major faults", .flags = 0, }, + [UTIME] = { .name = "UTIME", .title = " UTIME+ ", .description = "User CPU time - time the process spent executing in user mode", .flags = 0, }, + [STIME] = { .name = "STIME", .title = " STIME+ ", .description = "System CPU time - time the kernel spent running system calls for this process", .flags = 0, }, + [CUTIME] = { .name = "CUTIME", .title = " CUTIME+ ", .description = "Children processes' user CPU time", .flags = 0, }, + [CSTIME] = { .name = "CSTIME", .title = " CSTIME+ ", .description = "Children processes' system CPU time", .flags = 0, }, + [PRIORITY] = { .name = "PRIORITY", .title = "PRI ", .description = "Kernel's internal priority for the process", .flags = 0, }, + [NICE] = { .name = "NICE", .title = " NI ", .description = "Nice value (the higher the value, the more it lets other processes take priority)", .flags = 0, }, + [ITREALVALUE] = { .name = "ITREALVALUE", .title = NULL, .description = NULL, .flags = 0, }, + [STARTTIME] = { .name = "STARTTIME", .title = "START ", .description = "Time the process was started", .flags = 0, }, + [VSIZE] = { .name = "VSIZE", .title = NULL, .description = NULL, .flags = 0, }, + [RSS] = { .name = "RSS", .title = NULL, .description = NULL, .flags = 0, }, + [RLIM] = { .name = "RLIM", .title = NULL, .description = NULL, .flags = 0, }, + [STARTCODE] = { .name = "STARTCODE", .title = NULL, .description = NULL, .flags = 0, }, + [ENDCODE] = { .name = "ENDCODE", .title = NULL, .description = NULL, .flags = 0, }, + [STARTSTACK] = { .name = "STARTSTACK", .title = NULL, .description = NULL, .flags = 0, }, + [KSTKESP] = { .name = "KSTKESP", .title = NULL, .description = NULL, .flags = 0, }, + [KSTKEIP] = { .name = "KSTKEIP", .title = NULL, .description = NULL, .flags = 0, }, + [SIGNAL] = { .name = "SIGNAL", .title = NULL, .description = NULL, .flags = 0, }, + [BLOCKED] = { .name = "BLOCKED", .title = NULL, .description = NULL, .flags = 0, }, + [SSIGIGNORE] = { .name = "SIGIGNORE", .title = NULL, .description = NULL, .flags = 0, }, + [SIGCATCH] = { .name = "SIGCATCH", .title = NULL, .description = NULL, .flags = 0, }, + [WCHAN] = { .name = "WCHAN", .title = NULL, .description = NULL, .flags = 0, }, + [NSWAP] = { .name = "NSWAP", .title = NULL, .description = NULL, .flags = 0, }, + [CNSWAP] = { .name = "CNSWAP", .title = NULL, .description = NULL, .flags = 0, }, + [EXIT_SIGNAL] = { .name = "EXIT_SIGNAL", .title = NULL, .description = NULL, .flags = 0, }, + [PROCESSOR] = { .name = "PROCESSOR", .title = "CPU ", .description = "Id of the CPU the process last executed on", .flags = 0, }, + [M_SIZE] = { .name = "M_SIZE", .title = " VIRT ", .description = "Total program size in virtual memory", .flags = 0, }, + [M_RESIDENT] = { .name = "M_RESIDENT", .title = " RES ", .description = "Resident set size, size of the text and data sections, plus stack usage", .flags = 0, }, + [M_SHARE] = { .name = "M_SHARE", .title = " SHR ", .description = "Size of the process's shared pages", .flags = 0, }, + [M_TRS] = { .name = "M_TRS", .title = " CODE ", .description = "Size of the text segment of the process", .flags = 0, }, + [M_DRS] = { .name = "M_DRS", .title = " DATA ", .description = "Size of the data segment plus stack usage of the process", .flags = 0, }, + [M_LRS] = { .name = "M_LRS", .title = " LIB ", .description = "The library size of the process", .flags = 0, }, + [M_DT] = { .name = "M_DT", .title = " DIRTY ", .description = "Size of the dirty pages of the process", .flags = 0, }, + [ST_UID] = { .name = "ST_UID", .title = " UID ", .description = "User ID of the process owner", .flags = 0, }, + [PERCENT_CPU] = { .name = "PERCENT_CPU", .title = "CPU% ", .description = "Percentage of the CPU time the process used in the last sampling", .flags = 0, }, + [PERCENT_MEM] = { .name = "PERCENT_MEM", .title = "MEM% ", .description = "Percentage of the memory the process is using, based on resident memory size", .flags = 0, }, + [USER] = { .name = "USER", .title = "USER ", .description = "Username of the process owner (or user ID if name cannot be determined)", .flags = 0, }, + [TIME] = { .name = "TIME", .title = " TIME+ ", .description = "Total time the process has spent in user and system time", .flags = 0, }, + [NLWP] = { .name = "NLWP", .title = "NLWP ", .description = "Number of threads in the process", .flags = 0, }, + [TGID] = { .name = "TGID", .title = " TGID ", .description = "Thread group ID (i.e. process ID)", .flags = 0, }, #ifdef HAVE_OPENVZ - { .name = "CTID", .title = " CTID ", .description = "OpenVZ container ID (a.k.a. virtual environment ID)", .flags = PROCESS_FLAG_LINUX_OPENVZ, }, - { .name = "VPID", .title = " VPID ", .description = "OpenVZ process ID", .flags = PROCESS_FLAG_LINUX_OPENVZ, }, + [CTID] = { .name = "CTID", .title = " CTID ", .description = "OpenVZ container ID (a.k.a. virtual environment ID)", .flags = PROCESS_FLAG_LINUX_OPENVZ, }, + [VPID] = { .name = "VPID", .title = " VPID ", .description = "OpenVZ process ID", .flags = PROCESS_FLAG_LINUX_OPENVZ, }, #endif #ifdef HAVE_VSERVER - { .name = "VXID", .title = " VXID ", .description = "VServer process ID", .flags = PROCESS_FLAG_LINUX_VSERVER, }, + [VXID] = { .name = "VXID", .title = " VXID ", .description = "VServer process ID", .flags = PROCESS_FLAG_LINUX_VSERVER, }, #endif #ifdef HAVE_TASKSTATS - { .name = "RCHAR", .title = " RD_CHAR ", .description = "Number of bytes the process has read", .flags = PROCESS_FLAG_IO, }, - { .name = "WCHAR", .title = " WR_CHAR ", .description = "Number of bytes the process has written", .flags = PROCESS_FLAG_IO, }, - { .name = "SYSCR", .title = " RD_SYSC ", .description = "Number of read(2) syscalls for the process", .flags = PROCESS_FLAG_IO, }, - { .name = "SYSCW", .title = " WR_SYSC ", .description = "Number of write(2) syscalls for the process", .flags = PROCESS_FLAG_IO, }, - { .name = "RBYTES", .title = " IO_RBYTES ", .description = "Bytes of read(2) I/O for the process", .flags = PROCESS_FLAG_IO, }, - { .name = "WBYTES", .title = " IO_WBYTES ", .description = "Bytes of write(2) I/O for the process", .flags = PROCESS_FLAG_IO, }, - { .name = "CNCLWB", .title = " IO_CANCEL ", .description = "Bytes of cancelled write(2) I/O", .flags = PROCESS_FLAG_IO, }, - { .name = "IO_READ_RATE", .title = " DISK READ ", .description = "The I/O rate of read(2) in bytes per second for the process", .flags = PROCESS_FLAG_IO, }, - { .name = "IO_WRITE_RATE", .title = " DISK WRITE ", .description = "The I/O rate of write(2) in bytes per second for the process", .flags = PROCESS_FLAG_IO, }, - { .name = "IO_RATE", .title = " DISK R/W ", .description = "Total I/O rate in bytes per second", .flags = PROCESS_FLAG_IO, }, + [RCHAR] = { .name = "RCHAR", .title = " RD_CHAR ", .description = "Number of bytes the process has read", .flags = PROCESS_FLAG_IO, }, + [WCHAR] = { .name = "WCHAR", .title = " WR_CHAR ", .description = "Number of bytes the process has written", .flags = PROCESS_FLAG_IO, }, + [SYSCR] = { .name = "SYSCR", .title = " RD_SYSC ", .description = "Number of read(2) syscalls for the process", .flags = PROCESS_FLAG_IO, }, + [SYSCW] = { .name = "SYSCW", .title = " WR_SYSC ", .description = "Number of write(2) syscalls for the process", .flags = PROCESS_FLAG_IO, }, + [RBYTES] = { .name = "RBYTES", .title = " IO_RBYTES ", .description = "Bytes of read(2) I/O for the process", .flags = PROCESS_FLAG_IO, }, + [WBYTES] = { .name = "WBYTES", .title = " IO_WBYTES ", .description = "Bytes of write(2) I/O for the process", .flags = PROCESS_FLAG_IO, }, + [CNCLWB] = { .name = "CNCLWB", .title = " IO_CANCEL ", .description = "Bytes of cancelled write(2) I/O", .flags = PROCESS_FLAG_IO, }, + [IO_READ_RATE] = { .name = "IO_READ_RATE", .title = " DISK READ ", .description = "The I/O rate of read(2) in bytes per second for the process", .flags = PROCESS_FLAG_IO, }, + [IO_WRITE_RATE] = { .name = "IO_WRITE_RATE", .title = " DISK WRITE ", .description = "The I/O rate of write(2) in bytes per second for the process", .flags = PROCESS_FLAG_IO, }, + [IO_RATE] = { .name = "IO_RATE", .title = " DISK R/W ", .description = "Total I/O rate in bytes per second", .flags = PROCESS_FLAG_IO, }, #endif #ifdef HAVE_CGROUP - { .name = "CGROUP", .title = " CGROUP ", .description = "Which cgroup the process is in", .flags = PROCESS_FLAG_LINUX_CGROUP, }, + [CGROUP] = { .name = "CGROUP", .title = " CGROUP ", .description = "Which cgroup the process is in", .flags = PROCESS_FLAG_LINUX_CGROUP, }, #endif #ifdef HAVE_OOM - { .name = "OOM", .title = " OOM ", .description = "OOM (Out-of-Memory) killer score", .flags = 0, }, + [OOM] = { .name = "OOM", .title = " OOM ", .description = "OOM (Out-of-Memory) killer score", .flags = 0, }, #endif - { .name = "IO_PRIORITY", .title = "IO ", .description = "I/O priority", .flags = PROCESS_FLAG_LINUX_IOPRIO, }, - { .name = "*** report bug! ***", .title = NULL, .description = NULL, .flags = 0, }, + [IO_PRIORITY] = { .name = "IO_PRIORITY", .title = "IO ", .description = "I/O priority", .flags = PROCESS_FLAG_LINUX_IOPRIO, }, + [LAST_PROCESSFIELD] = { .name = "*** report bug! ***", .title = NULL, .description = NULL, .flags = 0, }, }; char* Process_pidFormat = "%7u "; @@ -187,9 +270,12 @@ LinuxProcess* LinuxProcess_new(Settings* settings) { return this; } -void LinuxProcess_delete(Object* cast) { +void Process_delete(Object* cast) { LinuxProcess* this = (LinuxProcess*) cast; Process_done((Process*)cast); +#ifdef HAVE_CGROUP + free(this->cgroup); +#endif free(this); } @@ -216,10 +302,48 @@ bool LinuxProcess_setIOPriority(LinuxProcess* this, IOPriority ioprio) { void Process_writeField(Process* this, RichString* str, ProcessField field) { LinuxProcess* lp = (LinuxProcess*) this; + bool coloring = this->settings->highlightMegabytes; char buffer[256]; buffer[255] = '\0'; int attr = CRT_colors[DEFAULT_COLOR]; int n = sizeof(buffer) - 1; - switch (field) { + switch ((int)field) { + case CMINFLT: Process_colorNumber(str, lp->cminflt, coloring); return; + case CMAJFLT: Process_colorNumber(str, lp->cmajflt, coloring); return; + case PROCESSOR: snprintf(buffer, n, "%3d ", Settings_cpuId(this->settings, lp->processor)); break; + case M_DRS: Process_humanNumber(str, lp->m_drs * PAGE_SIZE_KB, coloring); return; + case M_DT: Process_humanNumber(str, lp->m_dt * PAGE_SIZE_KB, coloring); return; + case M_LRS: Process_humanNumber(str, lp->m_lrs * PAGE_SIZE_KB, coloring); return; + case M_TRS: Process_humanNumber(str, lp->m_trs * PAGE_SIZE_KB, coloring); return; + case M_SHARE: Process_humanNumber(str, lp->m_share * PAGE_SIZE_KB, coloring); return; + case UTIME: Process_printTime(str, lp->utime); return; + case STIME: Process_printTime(str, lp->stime); return; + case CUTIME: Process_printTime(str, lp->cutime); return; + case CSTIME: Process_printTime(str, lp->cstime); return; + #ifdef HAVE_TASKSTATS + case RCHAR: Process_colorNumber(str, lp->io_rchar, coloring); return; + case WCHAR: Process_colorNumber(str, lp->io_wchar, coloring); return; + case SYSCR: Process_colorNumber(str, lp->io_syscr, coloring); return; + case SYSCW: Process_colorNumber(str, lp->io_syscw, coloring); return; + case RBYTES: Process_colorNumber(str, lp->io_read_bytes, coloring); return; + case WBYTES: Process_colorNumber(str, lp->io_write_bytes, coloring); return; + case CNCLWB: Process_colorNumber(str, lp->io_cancelled_write_bytes, coloring); return; + case IO_READ_RATE: Process_outputRate(str, buffer, n, lp->io_rate_read_bps, coloring); return; + case IO_WRITE_RATE: Process_outputRate(str, buffer, n, lp->io_rate_write_bps, coloring); return; + case IO_RATE: Process_outputRate(str, buffer, n, lp->io_rate_read_bps + lp->io_rate_write_bps, coloring); return; + #endif + #ifdef HAVE_OPENVZ + case CTID: snprintf(buffer, n, "%7u ", lp->ctid); break; + case VPID: snprintf(buffer, n, Process_pidFormat, lp->vpid); break; + #endif + #ifdef HAVE_VSERVER + case VXID: snprintf(buffer, n, "%5u ", lp->vxid); break; + #endif + #ifdef HAVE_CGROUP + case CGROUP: snprintf(buffer, n, "%-10s ", lp->cgroup); break; + #endif + #ifdef HAVE_OOM + case OOM: snprintf(buffer, n, Process_pidFormat, lp->oom); break; + #endif case IO_PRIORITY: { int klass = IOPriority_class(lp->ioPriority); if (klass == IOPRIO_CLASS_NONE) { @@ -255,10 +379,64 @@ long Process_compare(const void* v1, const void* v2) { p2 = (LinuxProcess*)v1; p1 = (LinuxProcess*)v2; } - switch (settings->sortKey) { + long long diff; + switch ((int)settings->sortKey) { + case PROCESSOR: + return (p1->processor - p2->processor); + case M_DRS: + return (p2->m_drs - p1->m_drs); + case M_DT: + return (p2->m_dt - p1->m_dt); + case M_LRS: + return (p2->m_lrs - p1->m_lrs); + case M_TRS: + return (p2->m_trs - p1->m_trs); + case M_SHARE: + return (p2->m_share - p1->m_share); + case UTIME: diff = p2->utime - p1->utime; goto test_diff; + case CUTIME: diff = p2->cutime - p1->cutime; goto test_diff; + case STIME: diff = p2->stime - p1->stime; goto test_diff; + case CSTIME: diff = p2->cstime - p2->cstime; goto test_diff; + #ifdef HAVE_TASKSTATS + case RCHAR: diff = p2->io_rchar - p1->io_rchar; goto test_diff; + case WCHAR: diff = p2->io_wchar - p1->io_wchar; goto test_diff; + case SYSCR: diff = p2->io_syscr - p1->io_syscr; goto test_diff; + case SYSCW: diff = p2->io_syscw - p1->io_syscw; goto test_diff; + case RBYTES: diff = p2->io_read_bytes - p1->io_read_bytes; goto test_diff; + case WBYTES: diff = p2->io_write_bytes - p1->io_write_bytes; goto test_diff; + case CNCLWB: diff = p2->io_cancelled_write_bytes - p1->io_cancelled_write_bytes; goto test_diff; + case IO_READ_RATE: diff = p2->io_rate_read_bps - p1->io_rate_read_bps; goto test_diff; + 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_OPENVZ + case CTID: + return (p1->ctid - p2->ctid); + case VPID: + return (p1->vpid - p2->vpid); + #endif + #ifdef HAVE_VSERVER + case VXID: + return (p1->vxid - p2->vxid); + #endif + #ifdef HAVE_CGROUP + case CGROUP: + return strcmp(p1->cgroup ? p1->cgroup : "", p2->cgroup ? p2->cgroup : ""); + #endif + #ifdef HAVE_OOM + case OOM: + return (p1->oom - p2->oom); + #endif case IO_PRIORITY: return LinuxProcess_effectiveIOPriority(p1) - LinuxProcess_effectiveIOPriority(p2); default: return Process_defaultCompare(v1, v2); } + test_diff: + return (diff > 0) ? 1 : (diff < 0 ? -1 : 0); +} + +bool Process_isThread(Process* this) { + return (Process_isUserlandThread(this) || Process_isKernelThread(this)); } + diff --git a/linux/LinuxProcess.h b/linux/LinuxProcess.h index 34007185..2db1a924 100644 --- a/linux/LinuxProcess.h +++ b/linux/LinuxProcess.h @@ -15,28 +15,66 @@ in the source distribution for its full text. #define PROCESS_FLAG_LINUX_VSERVER 0x0400 #define PROCESS_FLAG_LINUX_CGROUP 0x0800 +typedef enum UnsupportedProcessFields { + FLAGS = 9, + ITREALVALUE = 20, + VSIZE = 22, + RSS = 23, + RLIM = 24, + STARTCODE = 25, + ENDCODE = 26, + STARTSTACK = 27, + KSTKESP = 28, + KSTKEIP = 29, + SIGNAL = 30, + BLOCKED = 31, + SSIGIGNORE = 32, + SIGCATCH = 33, + WCHAN = 34, + NSWAP = 35, + CNSWAP = 36, + EXIT_SIGNAL = 37, +} UnsupportedProcessField; + typedef enum LinuxProcessFields { - 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, - STARTSTACK, KSTKESP, KSTKEIP, SIGNAL, BLOCKED, SSIGIGNORE, SIGCATCH, WCHAN, NSWAP, CNSWAP, EXIT_SIGNAL, - PROCESSOR, M_SIZE, M_RESIDENT, M_SHARE, M_TRS, M_DRS, M_LRS, M_DT, ST_UID, PERCENT_CPU, PERCENT_MEM, - USER, TIME, NLWP, TGID, + CMINFLT = 11, + CMAJFLT = 13, + UTIME = 14, + STIME = 15, + CUTIME = 16, + CSTIME = 17, + PROCESSOR = 38, + M_SHARE = 41, + M_TRS = 42, + M_DRS = 43, + M_LRS = 44, + M_DT = 45, #ifdef HAVE_OPENVZ - CTID, VPID, + CTID = 100, + VPID = 101, #endif #ifdef HAVE_VSERVER - VXID, + VXID = 102, #endif #ifdef HAVE_TASKSTATS - RCHAR, WCHAR, SYSCR, SYSCW, RBYTES, WBYTES, CNCLWB, IO_READ_RATE, IO_WRITE_RATE, IO_RATE, + RCHAR = 103, + WCHAR = 104, + SYSCR = 105, + SYSCW = 106, + RBYTES = 107, + WBYTES = 108, + CNCLWB = 109, + IO_READ_RATE = 110, + IO_WRITE_RATE = 111, + IO_RATE = 112, #endif #ifdef HAVE_CGROUP - CGROUP, + CGROUP = 113, #endif #ifdef HAVE_OOM - OOM, + OOM = 114, #endif - IO_PRIORITY, + IO_PRIORITY = 115, LAST_PROCESSFIELD } LinuxProcessField; @@ -45,9 +83,53 @@ typedef enum LinuxProcessFields { typedef struct LinuxProcess_ { Process super; IOPriority ioPriority; + unsigned long int cminflt; + unsigned long int cmajflt; + unsigned long long int utime; + unsigned long long int stime; + unsigned long long int cutime; + unsigned long long int cstime; + int processor; + long m_share; + long m_trs; + long m_drs; + long m_lrs; + long m_dt; + #ifdef HAVE_TASKSTATS + unsigned long long io_rchar; + unsigned long long io_wchar; + unsigned long long io_syscr; + unsigned long long io_syscw; + unsigned long long io_read_bytes; + unsigned long long io_write_bytes; + unsigned long long io_cancelled_write_bytes; + unsigned long long io_rate_read_time; + unsigned long long io_rate_write_time; + double io_rate_read_bps; + double io_rate_write_bps; + #endif + #ifdef HAVE_OPENVZ + unsigned int ctid; + unsigned int vpid; + #endif + #ifdef HAVE_VSERVER + unsigned int vxid; + #endif + #ifdef HAVE_CGROUP + char* cgroup; + #endif + #ifdef HAVE_OOM + unsigned int oom; + #endif } LinuxProcess; -#define Process_delete LinuxProcess_delete +#ifndef Process_isKernelThread +#define Process_isKernelThread(_process) (_process->pgrp == 0) +#endif + +#ifndef Process_isUserlandThread +#define Process_isUserlandThread(_process) (_process->pid != _process->tgid) +#endif extern ProcessFieldData Process_fields[]; @@ -59,7 +141,7 @@ void Process_setupColumnWidths(); LinuxProcess* LinuxProcess_new(Settings* settings); -void LinuxProcess_delete(Object* cast); +void Process_delete(Object* cast); /* [1] Note that before kernel 2.6.26 a process that has not asked for @@ -79,4 +161,7 @@ void Process_writeField(Process* this, RichString* str, ProcessField field); long Process_compare(const void* v1, const void* v2); +bool Process_isThread(Process* this); + + #endif diff --git a/linux/LinuxProcessList.c b/linux/LinuxProcessList.c index e0e8fbd3..a4268632 100644 --- a/linux/LinuxProcessList.c +++ b/linux/LinuxProcessList.c @@ -152,6 +152,7 @@ static ssize_t xread(int fd, void *buf, size_t count) { } static bool LinuxProcessList_readStatFile(Process *process, const char* dirname, const char* name, char* command) { + LinuxProcess* lp = (LinuxProcess*) process; char filename[MAX_NAME+1]; snprintf(filename, MAX_NAME, "%s/%s/stat", dirname, name); int fd = open(filename, O_RDONLY); @@ -194,19 +195,19 @@ static bool LinuxProcessList_readStatFile(Process *process, const char* dirname, location += 1; process->minflt = strtoull(location, &location, 10); location += 1; - process->cminflt = strtoull(location, &location, 10); + lp->cminflt = strtoull(location, &location, 10); location += 1; process->majflt = strtoull(location, &location, 10); location += 1; - process->cmajflt = strtoull(location, &location, 10); + lp->cmajflt = strtoull(location, &location, 10); location += 1; - process->utime = strtoull(location, &location, 10); + lp->utime = strtoull(location, &location, 10); location += 1; - process->stime = strtoull(location, &location, 10); + lp->stime = strtoull(location, &location, 10); location += 1; - process->cutime = strtoull(location, &location, 10); + lp->cutime = strtoull(location, &location, 10); location += 1; - process->cstime = strtoull(location, &location, 10); + lp->cstime = strtoull(location, &location, 10); location += 1; process->priority = strtol(location, &location, 10); location += 1; @@ -218,7 +219,9 @@ static bool LinuxProcessList_readStatFile(Process *process, const char* dirname, process->exit_signal = strtol(location, &location, 10); location += 1; assert(location != NULL); - process->processor = strtol(location, &location, 10); + lp->processor = strtol(location, &location, 10); + + process->time = lp->utime + lp->stime; return true; } @@ -246,7 +249,7 @@ static bool LinuxProcessList_statProcessDir(Process* process, const char* dirnam #ifdef HAVE_TASKSTATS -static void LinuxProcessList_readIoFile(Process* process, const char* dirname, char* name, unsigned long long now) { +static void LinuxProcessList_readIoFile(LinuxProcess* process, const char* dirname, char* name, unsigned long long now) { char filename[MAX_NAME+1]; filename[MAX_NAME] = '\0'; @@ -307,7 +310,7 @@ static void LinuxProcessList_readIoFile(Process* process, const char* dirname, c -static bool LinuxProcessList_readStatmFile(Process* process, const char* dirname, const char* name) { +static bool LinuxProcessList_readStatmFile(LinuxProcess* process, const char* dirname, const char* name) { char filename[MAX_NAME+1]; snprintf(filename, MAX_NAME, "%s/%s/statm", dirname, name); int fd = open(filename, O_RDONLY); @@ -320,8 +323,8 @@ static bool LinuxProcessList_readStatmFile(Process* process, const char* dirname char *p = buf; errno = 0; - process->m_size = strtol(p, &p, 10); if (*p == ' ') p++; - process->m_resident = strtol(p, &p, 10); if (*p == ' ') p++; + process->super.m_size = strtol(p, &p, 10); if (*p == ' ') p++; + process->super.m_resident = strtol(p, &p, 10); if (*p == ' ') p++; process->m_share = strtol(p, &p, 10); if (*p == ' ') p++; process->m_trs = strtol(p, &p, 10); if (*p == ' ') p++; process->m_lrs = strtol(p, &p, 10); if (*p == ' ') p++; @@ -360,7 +363,7 @@ static void LinuxProcessList_readOpenVZData(ProcessList* this, Process* process, #ifdef HAVE_CGROUP -static void LinuxProcessList_readCGroupFile(Process* process, const char* dirname, const char* name) { +static void LinuxProcessList_readCGroupFile(LinuxProcess* process, const char* dirname, const char* name) { char filename[MAX_NAME+1]; snprintf(filename, MAX_NAME, "%s/%s/cgroup", dirname, name); FILE* file = fopen(filename, "r"); @@ -390,7 +393,7 @@ static void LinuxProcessList_readCGroupFile(Process* process, const char* dirnam #ifdef HAVE_VSERVER -static void LinuxProcessList_readVServerData(Process* process, const char* dirname, const char* name) { +static void LinuxProcessList_readVServerData(LinuxProcess* process, const char* dirname, const char* name) { char filename[MAX_NAME+1]; snprintf(filename, MAX_NAME, "%s/%s/status", dirname, name); FILE* file = fopen(filename, "r"); @@ -423,7 +426,7 @@ static void LinuxProcessList_readVServerData(Process* process, const char* dirna #ifdef HAVE_OOM -static void LinuxProcessList_readOomData(Process* process, const char* dirname, const char* name) { +static void LinuxProcessList_readOomData(LinuxProcess* process, const char* dirname, const char* name) { char filename[MAX_NAME+1]; snprintf(filename, MAX_NAME, "%s/%s/oom_score", dirname, name); FILE* file = fopen(filename, "r"); @@ -514,95 +517,97 @@ static bool LinuxProcessList_processEntries(LinuxProcessList* this, const char* if (pid <= 0) continue; - Process* process = NULL; - Process* existingProcess = (Process*) Hashtable_get(this->super.processTable, pid); + Process* proc = NULL; + Process* existingProc = (Process*) Hashtable_get(this->super.processTable, pid); - if (existingProcess) { - assert(Vector_indexOf(this->processes, existingProcess, Process_pidCompare) != -1); - process = existingProcess; - assert(process->pid == pid); + if (existingProc) { + assert(Vector_indexOf(this->processes, existingProc, Process_pidCompare) != -1); + proc = existingProc; + assert(proc->pid == pid); } else { - process = (Process*) LinuxProcess_new(settings); - assert(process->comm == NULL); - process->pid = pid; - process->tgid = parent ? parent->pid : pid; + proc = (Process*) LinuxProcess_new(settings); + assert(proc->comm == NULL); + proc->pid = pid; + proc->tgid = parent ? parent->pid : pid; } + + LinuxProcess* lp = (LinuxProcess*) proc; char subdirname[MAX_NAME+1]; snprintf(subdirname, MAX_NAME, "%s/%s/task", dirname, name); - LinuxProcessList_processEntries(this, subdirname, process, period, tv); + LinuxProcessList_processEntries(this, subdirname, proc, period, tv); #ifdef HAVE_TASKSTATS if (settings->flags & PROCESS_FLAG_IO) - LinuxProcessList_readIoFile(process, dirname, name, now); + LinuxProcessList_readIoFile(lp, dirname, name, now); #endif - if (! LinuxProcessList_readStatmFile(process, dirname, name)) + if (! LinuxProcessList_readStatmFile(lp, dirname, name)) goto errorReadingProcess; - process->show = ! ((hideKernelThreads && Process_isKernelThread(process)) || (hideUserlandThreads && Process_isUserlandThread(process))); + proc->show = ! ((hideKernelThreads && Process_isKernelThread(proc)) || (hideUserlandThreads && Process_isUserlandThread(proc))); char command[MAX_NAME+1]; - unsigned long long int lasttimes = (process->utime + process->stime); - if (! LinuxProcessList_readStatFile(process, dirname, name, command)) + unsigned long long int lasttimes = (lp->utime + lp->stime); + if (! LinuxProcessList_readStatFile(proc, dirname, name, command)) goto errorReadingProcess; if (settings->flags & PROCESS_FLAG_LINUX_IOPRIO) - LinuxProcess_updateIOPriority((LinuxProcess*)process); - float percent_cpu = (process->utime + process->stime - lasttimes) / period * 100.0; - process->percent_cpu = MAX(MIN(percent_cpu, cpus*100.0), 0.0); - if (isnan(process->percent_cpu)) process->percent_cpu = 0.0; - process->percent_mem = (process->m_resident * PAGE_SIZE_KB) / (double)(this->totalMem) * 100.0; + LinuxProcess_updateIOPriority(lp); + float percent_cpu = (lp->utime + lp->stime - lasttimes) / period * 100.0; + proc->percent_cpu = MAX(MIN(percent_cpu, cpus*100.0), 0.0); + if (isnan(proc->percent_cpu)) proc->percent_cpu = 0.0; + proc->percent_mem = (proc->m_resident * PAGE_SIZE_KB) / (double)(this->totalMem) * 100.0; - if(!existingProcess) { + if(!existingProc) { - if (! LinuxProcessList_statProcessDir(process, dirname, name, curTime)) + if (! LinuxProcessList_statProcessDir(proc, dirname, name, curTime)) goto errorReadingProcess; - process->user = UsersTable_getRef(this->super.usersTable, process->st_uid); + proc->user = UsersTable_getRef(this->super.usersTable, proc->st_uid); #ifdef HAVE_OPENVZ - LinuxProcessList_readOpenVZData(this, process, dirname, name); + LinuxProcessList_readOpenVZData(this, lp, dirname, name); #endif #ifdef HAVE_VSERVER if (settings->flags & PROCESS_FLAG_LINUX_VSERVER) - LinuxProcessList_readVServerData(process, dirname, name); + LinuxProcessList_readVServerData(lp, dirname, name); #endif - if (! LinuxProcessList_readCmdlineFile(process, dirname, name)) + if (! LinuxProcessList_readCmdlineFile(proc, dirname, name)) goto errorReadingProcess; - ProcessList_add((ProcessList*)this, process); + ProcessList_add((ProcessList*)this, proc); } else { if (settings->updateProcessNames) { - if (! LinuxProcessList_readCmdlineFile(process, dirname, name)) + if (! LinuxProcessList_readCmdlineFile(proc, dirname, name)) goto errorReadingProcess; } } #ifdef HAVE_CGROUP if (settings->flags & PROCESS_FLAG_LINUX_CGROUP) - LinuxProcessList_readCGroupFile(process, dirname, name); + LinuxProcessList_readCGroupFile(lp, dirname, name); #endif #ifdef HAVE_OOM - LinuxProcessList_readOomData(process, dirname, name); + LinuxProcessList_readOomData(lp, dirname, name); #endif - if (process->state == 'Z') { - free(process->comm); - process->basenameOffset = -1; - process->comm = strdup(command); - } else if (Process_isThread(process)) { - if (settings->showThreadNames || Process_isKernelThread(process) || process->state == 'Z') { - free(process->comm); - process->basenameOffset = -1; - process->comm = strdup(command); + if (proc->state == 'Z') { + free(proc->comm); + proc->basenameOffset = -1; + proc->comm = strdup(command); + } else if (Process_isThread(proc)) { + if (settings->showThreadNames || Process_isKernelThread(proc) || proc->state == 'Z') { + free(proc->comm); + proc->basenameOffset = -1; + proc->comm = strdup(command); } else if (settings->showThreadNames) { - if (! LinuxProcessList_readCmdlineFile(process, dirname, name)) + if (! LinuxProcessList_readCmdlineFile(proc, dirname, name)) goto errorReadingProcess; } - if (Process_isKernelThread(process)) { + if (Process_isKernelThread(proc)) { this->kernelThreads++; } else { this->userlandThreads++; @@ -610,23 +615,23 @@ static bool LinuxProcessList_processEntries(LinuxProcessList* this, const char* } this->totalTasks++; - if (process->state == 'R') + if (proc->state == 'R') this->runningTasks++; - process->updated = true; + proc->updated = true; continue; // Exception handler. errorReadingProcess: { - if (process->comm) { - free(process->comm); - process->basenameOffset = -1; - process->comm = NULL; + if (proc->comm) { + free(proc->comm); + proc->basenameOffset = -1; + proc->comm = NULL; } - if (existingProcess) - ProcessList_remove((ProcessList*)this, process); + if (existingProc) + ProcessList_remove((ProcessList*)this, proc); else - LinuxProcess_delete((Object*)process); + Process_delete((Object*)proc); } } closedir(dir); diff --git a/linux/Platform.c b/linux/Platform.c index 3cd8c0c8..d89b4820 100644 --- a/linux/Platform.c +++ b/linux/Platform.c @@ -21,6 +21,7 @@ in the source distribution for its full text. #include "UptimeMeter.h" #include "ClockMeter.h" #include "HostnameMeter.h" +#include "LinuxProcess.h" #include <math.h> #include <assert.h> @@ -29,10 +30,12 @@ in the source distribution for its full text. #include "Action.h" #include "MainPanel.h" #include "BatteryMeter.h" -#include "LinuxProcess.h" - }*/ +ProcessField Platform_defaultFields[] = { PID, USER, PRIORITY, NICE, M_SIZE, M_RESIDENT, M_SHARE, STATE, PERCENT_CPU, PERCENT_MEM, TIME, COMM, 0 }; + +//static ProcessField defaultIoFields[] = { PID, IO_PRIORITY, USER, IO_READ_RATE, IO_WRITE_RATE, IO_RATE, COMM, 0 }; + int Platform_numberOfFields = LAST_PROCESSFIELD; static Htop_Reaction Platform_actionSetIOPriority(State* st) { diff --git a/linux/Platform.h b/linux/Platform.h index bc044a2c..a00208ef 100644 --- a/linux/Platform.h +++ b/linux/Platform.h @@ -12,8 +12,8 @@ in the source distribution for its full text. #include "Action.h" #include "MainPanel.h" #include "BatteryMeter.h" -#include "LinuxProcess.h" +extern ProcessField Platform_defaultFields[]; extern int Platform_numberOfFields; |