aboutsummaryrefslogtreecommitdiffstats
path: root/linux
diff options
context:
space:
mode:
authorDaniel Lange <DLange@git.local>2016-04-11 13:01:07 +0200
committerDaniel Lange <DLange@git.local>2016-04-11 13:01:07 +0200
commitff9409b1737627857eb47f64f536a3f66b6a09a4 (patch)
tree61b631ba551e68a4f656b8b76ff7bd0d9955fc64 /linux
parentf75ab6d2c11e8a8e18191b087564aedebbeb96c5 (diff)
downloaddebian_htop-ff9409b1737627857eb47f64f536a3f66b6a09a4.tar.gz
debian_htop-ff9409b1737627857eb47f64f536a3f66b6a09a4.tar.bz2
debian_htop-ff9409b1737627857eb47f64f536a3f66b6a09a4.zip
Imported Upstream version 2.0.0upstream/2.0.0
Diffstat (limited to 'linux')
-rw-r--r--linux/Battery.c331
-rw-r--r--linux/Battery.h41
-rw-r--r--linux/IOPriority.c41
-rw-r--r--linux/IOPriority.h43
-rw-r--r--linux/IOPriorityPanel.c44
-rw-r--r--linux/IOPriorityPanel.h21
-rw-r--r--linux/LinuxCRT.c36
-rw-r--r--linux/LinuxCRT.h17
-rw-r--r--linux/LinuxProcess.c413
-rw-r--r--linux/LinuxProcess.h161
-rw-r--r--linux/LinuxProcessList.c775
-rw-r--r--linux/LinuxProcessList.h94
-rw-r--r--linux/Platform.c239
-rw-r--r--linux/Platform.h48
14 files changed, 2304 insertions, 0 deletions
diff --git a/linux/Battery.c b/linux/Battery.c
new file mode 100644
index 0000000..572bad4
--- /dev/null
+++ b/linux/Battery.c
@@ -0,0 +1,331 @@
+/*
+htop - linux/Battery.c
+(C) 2004-2014 Hisham H. Muhammad
+Released under the GNU GPL, see the COPYING file
+in the source distribution for its full text.
+
+Linux battery readings written by Ian P. Hands (iphands@gmail.com, ihands@redhat.com).
+*/
+
+#ifndef _GNU_SOURCE
+#define _GNU_SOURCE
+#endif
+#include <dirent.h>
+#include <errno.h>
+#include <unistd.h>
+#include <stdlib.h>
+#include <string.h>
+#include <fcntl.h>
+#include <time.h>
+#include "BatteryMeter.h"
+#include "StringUtils.h"
+
+#define SYS_POWERSUPPLY_DIR "/sys/class/power_supply"
+
+// ----------------------------------------
+// READ FROM /proc
+// ----------------------------------------
+
+// This implementation reading from from /proc/acpi is really inefficient,
+// but I think this is on the way out so I did not rewrite it.
+// The /sys implementation below does things the right way.
+
+static unsigned long int parseBatInfo(const char *fileName, const unsigned short int lineNum, const unsigned short int wordNum) {
+ const char batteryPath[] = PROCDIR "/acpi/battery/";
+ DIR* batteryDir = opendir(batteryPath);
+ if (!batteryDir)
+ return 0;
+
+ #define MAX_BATTERIES 64
+ char* batteries[MAX_BATTERIES];
+ unsigned int nBatteries = 0;
+ memset(batteries, 0, MAX_BATTERIES * sizeof(char*));
+
+ struct dirent result;
+ struct dirent* dirEntry;
+ while (nBatteries < MAX_BATTERIES) {
+ int err = readdir_r(batteryDir, &result, &dirEntry);
+ if (err || !dirEntry)
+ break;
+ char* entryName = dirEntry->d_name;
+ if (strncmp(entryName, "BAT", 3))
+ continue;
+ batteries[nBatteries] = xStrdup(entryName);
+ nBatteries++;
+ }
+ closedir(batteryDir);
+
+ unsigned long int total = 0;
+ for (unsigned int i = 0; i < nBatteries; i++) {
+ char infoPath[30];
+ snprintf(infoPath, sizeof infoPath, "%s%s/%s", batteryPath, batteries[i], fileName);
+
+ FILE* file = fopen(infoPath, "r");
+ if (!file) {
+ break;
+ }
+
+ char line[50] = "";
+ for (unsigned short int i = 0; i < lineNum; i++) {
+ char* ok = fgets(line, sizeof line, file);
+ if (!ok) break;
+ }
+
+ fclose(file);
+
+ char *foundNumStr = String_getToken(line, wordNum);
+ const unsigned long int foundNum = atoi(foundNumStr);
+ free(foundNumStr);
+
+ total += foundNum;
+ }
+
+ for (unsigned int i = 0; i < nBatteries; i++) {
+ free(batteries[i]);
+ }
+
+ return total;
+}
+
+static ACPresence procAcpiCheck() {
+ ACPresence isOn = AC_ERROR;
+ const char *power_supplyPath = PROCDIR "/acpi/ac_adapter";
+ DIR *dir = opendir(power_supplyPath);
+ if (!dir) {
+ return AC_ERROR;
+ }
+
+ struct dirent result;
+ struct dirent* dirEntry;
+ for (;;) {
+ int err = readdir_r((DIR *) dir, &result, &dirEntry);
+ if (err || !dirEntry)
+ break;
+
+ char* entryName = (char *) dirEntry->d_name;
+
+ if (entryName[0] != 'A')
+ continue;
+
+ char statePath[50];
+ snprintf((char *) statePath, sizeof statePath, "%s/%s/state", power_supplyPath, entryName);
+ FILE* file = fopen(statePath, "r");
+
+ if (!file) {
+ isOn = AC_ERROR;
+ continue;
+ }
+
+ char line[100];
+ char* ok = fgets(line, sizeof line, file);
+ if (!ok) continue;
+ line[sizeof(line) - 1] = '\0';
+
+ fclose(file);
+
+ const char *isOnline = String_getToken(line, 2);
+
+ if (strcmp(isOnline, "on-line") == 0) {
+ isOn = AC_PRESENT;
+ } else {
+ isOn = AC_ABSENT;
+ }
+ free((char *) isOnline);
+ if (isOn == AC_PRESENT) {
+ break;
+ }
+ }
+
+ if (dir)
+ closedir(dir);
+ return isOn;
+}
+
+static double Battery_getProcBatData() {
+ const unsigned long int totalFull = parseBatInfo("info", 3, 4);
+ if (totalFull == 0)
+ return 0;
+
+ const unsigned long int totalRemain = parseBatInfo("state", 5, 3);
+ if (totalRemain == 0)
+ return 0;
+
+ return totalRemain * 100.0 / (double) totalFull;
+}
+
+static void Battery_getProcData(double* level, ACPresence* isOnAC) {
+ *level = Battery_getProcBatData();
+ *isOnAC = procAcpiCheck();
+}
+
+// ----------------------------------------
+// READ FROM /sys
+// ----------------------------------------
+
+static inline ssize_t xread(int fd, void *buf, size_t count) {
+ // Read some bytes. Retry on EINTR and when we don't get as many bytes as we requested.
+ size_t alreadyRead = 0;
+ for(;;) {
+ ssize_t res = read(fd, buf, count);
+ if (res == -1 && errno == EINTR) continue;
+ if (res > 0) {
+ buf = ((char*)buf)+res;
+ count -= res;
+ alreadyRead += res;
+ }
+ if (res == -1) return -1;
+ if (count == 0 || res == 0) return alreadyRead;
+ }
+}
+
+/**
+ * Returns a pointer to the suffix of `str` if its beginning matches `prefix`.
+ * Returns NULL if the prefix does not match.
+ * Examples:
+ * match("hello world", "hello "); -> "world"
+ * match("hello world", "goodbye "); -> NULL
+ */
+static inline const char* match(const char* str, const char* prefix) {
+ for (;;) {
+ if (*prefix == '\0') {
+ return str;
+ }
+ if (*prefix != *str) {
+ return NULL;
+ }
+ prefix++; str++;
+ }
+}
+
+static void Battery_getSysData(double* level, ACPresence* isOnAC) {
+
+ *level = 0;
+ *isOnAC = AC_ERROR;
+
+ DIR *dir = opendir(SYS_POWERSUPPLY_DIR);
+ if (!dir)
+ return;
+
+ unsigned long int totalFull = 0;
+ unsigned long int totalRemain = 0;
+
+ struct dirent result;
+ struct dirent* dirEntry;
+ for (;;) {
+ int err = readdir_r((DIR *) dir, &result, &dirEntry);
+ if (err || !dirEntry)
+ break;
+ char* entryName = (char *) dirEntry->d_name;
+ const char filePath[50];
+
+ if (entryName[0] == 'B' && entryName[1] == 'A' && entryName[2] == 'T') {
+
+ snprintf((char *) filePath, sizeof filePath, SYS_POWERSUPPLY_DIR "/%s/uevent", entryName);
+ int fd = open(filePath, O_RDONLY);
+ if (fd == -1) {
+ closedir(dir);
+ return;
+ }
+ char buffer[1024];
+ ssize_t buflen = xread(fd, buffer, 1023);
+ close(fd);
+ if (buflen < 1) {
+ closedir(dir);
+ return;
+ }
+ buffer[buflen] = '\0';
+ char *buf = buffer;
+ char *line = NULL;
+ bool full = false;
+ bool now = false;
+ while ((line = strsep(&buf, "\n")) != NULL) {
+ const char* ps = match(line, "POWER_SUPPLY_");
+ if (!ps) {
+ continue;
+ }
+ const char* energy = match(ps, "ENERGY_");
+ if (!energy) {
+ energy = match(ps, "CHARGE_");
+ }
+ if (!energy) {
+ continue;
+ }
+ const char* value = (!full) ? match(energy, "FULL=") : NULL;
+ if (value) {
+ totalFull += atoi(value);
+ full = true;
+ if (now) break;
+ continue;
+ }
+ value = (!now) ? match(energy, "NOW=") : NULL;
+ if (value) {
+ totalRemain += atoi(value);
+ now = true;
+ if (full) break;
+ continue;
+ }
+ }
+ } else if (entryName[0] == 'A') {
+ if (*isOnAC != AC_ERROR) {
+ continue;
+ }
+
+ snprintf((char *) filePath, sizeof filePath, SYS_POWERSUPPLY_DIR "/%s/online", entryName);
+ int fd = open(filePath, O_RDONLY);
+ if (fd == -1) {
+ closedir(dir);
+ return;
+ }
+ char buffer[2] = "";
+ for(;;) {
+ ssize_t res = read(fd, buffer, 1);
+ if (res == -1 && errno == EINTR) continue;
+ break;
+ }
+ close(fd);
+ if (buffer[0] == '0') {
+ *isOnAC = AC_ABSENT;
+ } else if (buffer[0] == '1') {
+ *isOnAC = AC_PRESENT;
+ }
+ }
+ }
+ closedir(dir);
+ *level = totalFull > 0 ? ((double) totalRemain * 100) / (double) totalFull : 0;
+}
+
+static enum { BAT_PROC, BAT_SYS, BAT_ERR } Battery_method = BAT_PROC;
+
+static time_t Battery_cacheTime = 0;
+static double Battery_cacheLevel = 0;
+static ACPresence Battery_cacheIsOnAC = 0;
+
+void Battery_getData(double* level, ACPresence* isOnAC) {
+ time_t now = time(NULL);
+ // update battery reading is slow. Update it each 10 seconds only.
+ if (now < Battery_cacheTime + 10) {
+ *level = Battery_cacheLevel;
+ *isOnAC = Battery_cacheIsOnAC;
+ return;
+ }
+
+ if (Battery_method == BAT_PROC) {
+ Battery_getProcData(level, isOnAC);
+ if (*level == 0) {
+ Battery_method = BAT_SYS;
+ }
+ }
+ if (Battery_method == BAT_SYS) {
+ Battery_getSysData(level, isOnAC);
+ if (*level == 0) {
+ Battery_method = BAT_ERR;
+ }
+ }
+ if (Battery_method == BAT_ERR) {
+ *level = -1;
+ *isOnAC = AC_ERROR;
+ }
+ Battery_cacheLevel = *level;
+ Battery_cacheIsOnAC = *isOnAC;
+ Battery_cacheTime = now;
+}
diff --git a/linux/Battery.h b/linux/Battery.h
new file mode 100644
index 0000000..4cb22a8
--- /dev/null
+++ b/linux/Battery.h
@@ -0,0 +1,41 @@
+/* Do not edit this file. It was automatically generated. */
+
+#ifndef HEADER_Battery
+#define HEADER_Battery
+/*
+htop - linux/Battery.h
+(C) 2004-2014 Hisham H. Muhammad
+Released under the GNU GPL, see the COPYING file
+in the source distribution for its full text.
+
+Linux battery readings written by Ian P. Hands (iphands@gmail.com, ihands@redhat.com).
+*/
+
+#ifndef _GNU_SOURCE
+#define _GNU_SOURCE
+#endif
+
+#define SYS_POWERSUPPLY_DIR "/sys/class/power_supply"
+
+// ----------------------------------------
+// READ FROM /proc
+// ----------------------------------------
+
+// This implementation reading from from /proc/acpi is really inefficient,
+// but I think this is on the way out so I did not rewrite it.
+// The /sys implementation below does things the right way.
+
+// ----------------------------------------
+// READ FROM /sys
+// ----------------------------------------
+
+/**
+ * Returns a pointer to the suffix of `str` if its beginning matches `prefix`.
+ * Returns NULL if the prefix does not match.
+ * Examples:
+ * match("hello world", "hello "); -> "world"
+ * match("hello world", "goodbye "); -> NULL
+ */
+void Battery_getData(double* level, ACPresence* isOnAC);
+
+#endif
diff --git a/linux/IOPriority.c b/linux/IOPriority.c
new file mode 100644
index 0000000..7b19743
--- /dev/null
+++ b/linux/IOPriority.c
@@ -0,0 +1,41 @@
+/*
+htop - IOPriority.c
+(C) 2004-2012 Hisham H. Muhammad
+Released under the GNU GPL, see the COPYING file
+in the source distribution for its full text.
+
+Based on ionice,
+Copyright (C) 2005 Jens Axboe <jens@axboe.dk>
+Released under the terms of the GNU General Public License version 2
+*/
+
+#include "IOPriority.h"
+
+/*{
+
+enum {
+ IOPRIO_CLASS_NONE,
+ IOPRIO_CLASS_RT,
+ IOPRIO_CLASS_BE,
+ IOPRIO_CLASS_IDLE,
+};
+
+#define IOPRIO_WHO_PROCESS 1
+
+#define IOPRIO_CLASS_SHIFT (13)
+#define IOPRIO_PRIO_MASK ((1UL << IOPRIO_CLASS_SHIFT) - 1)
+
+#define IOPriority_class(ioprio_) ((int) ((ioprio_) >> IOPRIO_CLASS_SHIFT) )
+#define IOPriority_data(ioprio_) ((int) ((ioprio_) & IOPRIO_PRIO_MASK) )
+
+typedef int IOPriority;
+
+#define IOPriority_tuple(class_, data_) (((class_) << IOPRIO_CLASS_SHIFT) | data_)
+
+#define IOPriority_error 0xffffffff
+
+#define IOPriority_None IOPriority_tuple(IOPRIO_CLASS_NONE, 0)
+#define IOPriority_Idle IOPriority_tuple(IOPRIO_CLASS_IDLE, 0)
+
+}*/
+
diff --git a/linux/IOPriority.h b/linux/IOPriority.h
new file mode 100644
index 0000000..d69e30d
--- /dev/null
+++ b/linux/IOPriority.h
@@ -0,0 +1,43 @@
+/* Do not edit this file. It was automatically generated. */
+
+#ifndef HEADER_IOPriority
+#define HEADER_IOPriority
+/*
+htop - IOPriority.h
+(C) 2004-2012 Hisham H. Muhammad
+Released under the GNU GPL, see the COPYING file
+in the source distribution for its full text.
+
+Based on ionice,
+Copyright (C) 2005 Jens Axboe <jens@axboe.dk>
+Released under the terms of the GNU General Public License version 2
+*/
+
+
+enum {
+ IOPRIO_CLASS_NONE,
+ IOPRIO_CLASS_RT,
+ IOPRIO_CLASS_BE,
+ IOPRIO_CLASS_IDLE,
+};
+
+#define IOPRIO_WHO_PROCESS 1
+
+#define IOPRIO_CLASS_SHIFT (13)
+#define IOPRIO_PRIO_MASK ((1UL << IOPRIO_CLASS_SHIFT) - 1)
+
+#define IOPriority_class(ioprio_) ((int) ((ioprio_) >> IOPRIO_CLASS_SHIFT) )
+#define IOPriority_data(ioprio_) ((int) ((ioprio_) & IOPRIO_PRIO_MASK) )
+
+typedef int IOPriority;
+
+#define IOPriority_tuple(class_, data_) (((class_) << IOPRIO_CLASS_SHIFT) | data_)
+
+#define IOPriority_error 0xffffffff
+
+#define IOPriority_None IOPriority_tuple(IOPRIO_CLASS_NONE, 0)
+#define IOPriority_Idle IOPriority_tuple(IOPRIO_CLASS_IDLE, 0)
+
+
+
+#endif
diff --git a/linux/IOPriorityPanel.c b/linux/IOPriorityPanel.c
new file mode 100644
index 0000000..9e12c75
--- /dev/null
+++ b/linux/IOPriorityPanel.c
@@ -0,0 +1,44 @@
+/*
+htop - IOPriorityPanel.c
+(C) 2004-2012 Hisham H. Muhammad
+Released under the GNU GPL, see the COPYING file
+in the source distribution for its full text.
+*/
+
+#include "IOPriorityPanel.h"
+
+/*{
+#include "Panel.h"
+#include "IOPriority.h"
+#include "ListItem.h"
+}*/
+
+Panel* IOPriorityPanel_new(IOPriority currPrio) {
+ Panel* this = Panel_new(1, 1, 1, 1, true, Class(ListItem), FunctionBar_newEnterEsc("Set ", "Cancel "));
+
+ Panel_setHeader(this, "IO Priority:");
+ Panel_add(this, (Object*) ListItem_new("None (based on nice)", IOPriority_None));
+ if (currPrio == IOPriority_None) Panel_setSelected(this, 0);
+ struct { int klass; const char* name; } classes[] = {
+ { .klass = IOPRIO_CLASS_RT, .name = "Realtime" },
+ { .klass = IOPRIO_CLASS_BE, .name = "Best-effort" },
+ { .klass = 0, .name = NULL }
+ };
+ for (int c = 0; classes[c].name; c++) {
+ for (int i = 0; i < 8; i++) {
+ char name[50];
+ snprintf(name, sizeof(name)-1, "%s %d %s", classes[c].name, i, i == 0 ? "(High)" : (i == 7 ? "(Low)" : ""));
+ IOPriority ioprio = IOPriority_tuple(classes[c].klass, i);
+ Panel_add(this, (Object*) ListItem_new(name, ioprio));
+ if (currPrio == ioprio) Panel_setSelected(this, Panel_size(this) - 1);
+ }
+ }
+ Panel_add(this, (Object*) ListItem_new("Idle", IOPriority_Idle));
+ if (currPrio == IOPriority_Idle) Panel_setSelected(this, Panel_size(this) - 1);
+ return this;
+}
+
+IOPriority IOPriorityPanel_getIOPriority(Panel* this) {
+ return (IOPriority) ( ((ListItem*) Panel_getSelected(this))->key );
+}
+
diff --git a/linux/IOPriorityPanel.h b/linux/IOPriorityPanel.h
new file mode 100644
index 0000000..9f77a4d
--- /dev/null
+++ b/linux/IOPriorityPanel.h
@@ -0,0 +1,21 @@
+/* Do not edit this file. It was automatically generated. */
+
+#ifndef HEADER_IOPriorityPanel
+#define HEADER_IOPriorityPanel
+/*
+htop - IOPriorityPanel.h
+(C) 2004-2012 Hisham H. Muhammad
+Released under the GNU GPL, see the COPYING file
+in the source distribution for its full text.
+*/
+
+#include "Panel.h"
+#include "IOPriority.h"
+#include "ListItem.h"
+
+Panel* IOPriorityPanel_new(IOPriority currPrio);
+
+IOPriority IOPriorityPanel_getIOPriority(Panel* this);
+
+
+#endif
diff --git a/linux/LinuxCRT.c b/linux/LinuxCRT.c
new file mode 100644
index 0000000..5b2a21f
--- /dev/null
+++ b/linux/LinuxCRT.c
@@ -0,0 +1,36 @@
+/*
+htop - LinuxCRT.c
+(C) 2014 Hisham H. Muhammad
+Released under the GNU GPL, see the COPYING file
+in the source distribution for its full text.
+*/
+
+#include "config.h"
+#include "CRT.h"
+#include <stdio.h>
+#include <stdlib.h>
+#ifdef HAVE_EXECINFO_H
+#include <execinfo.h>
+#endif
+
+void CRT_handleSIGSEGV(int sgn) {
+ (void) sgn;
+ CRT_done();
+ #ifdef __linux
+ fprintf(stderr, "\n\nhtop " VERSION " aborting. Please report bug at http://hisham.hm/htop\n");
+ #ifdef HAVE_EXECINFO_H
+ size_t size = backtrace(backtraceArray, sizeof(backtraceArray) / sizeof(void *));
+ fprintf(stderr, "\n Please include in your report the following backtrace: \n");
+ backtrace_symbols_fd(backtraceArray, size, 2);
+ fprintf(stderr, "\nAdditionally, in order to make the above backtrace useful,");
+ fprintf(stderr, "\nplease also run the following command to generate a disassembly of your binary:");
+ fprintf(stderr, "\n\n objdump -d `which htop` > ~/htop.objdump");
+ fprintf(stderr, "\n\nand then attach the file ~/htop.objdump to your bug report.");
+ fprintf(stderr, "\n\nThank you for helping to improve htop!\n\n");
+ #endif
+ #else
+ fprintf(stderr, "\nUnfortunately, you seem to be using an unsupported platform!");
+ fprintf(stderr, "\nPlease contact your platform package maintainer!\n\n");
+ #endif
+ abort();
+}
diff --git a/linux/LinuxCRT.h b/linux/LinuxCRT.h
new file mode 100644
index 0000000..f8c43e4
--- /dev/null
+++ b/linux/LinuxCRT.h
@@ -0,0 +1,17 @@
+/* Do not edit this file. It was automatically generated. */
+
+#ifndef HEADER_LinuxCRT
+#define HEADER_LinuxCRT
+/*
+htop - LinuxCRT.h
+(C) 2014 Hisham H. Muhammad
+Released under the GNU GPL, see the COPYING file
+in the source distribution for its full text.
+*/
+
+#ifdef HAVE_EXECINFO_H
+#endif
+
+void CRT_handleSIGSEGV(int sgn);
+
+#endif
diff --git a/linux/LinuxProcess.c b/linux/LinuxProcess.c
new file mode 100644
index 0000000..c6c3112
--- /dev/null
+++ b/linux/LinuxProcess.c
@@ -0,0 +1,413 @@
+/*
+htop - LinuxProcess.c
+(C) 2014 Hisham H. Muhammad
+Released under the GNU GPL, see the COPYING file
+in the source distribution for its full text.
+*/
+
+#include "Process.h"
+#include "ProcessList.h"
+#include "LinuxProcess.h"
+#include "Platform.h"
+#include "CRT.h"
+
+#include <stdlib.h>
+#include <unistd.h>
+#include <string.h>
+#include <sys/syscall.h>
+
+/*{
+
+#define PROCESS_FLAG_LINUX_IOPRIO 0x0100
+#define PROCESS_FLAG_LINUX_OPENVZ 0x0200
+#define PROCESS_FLAG_LINUX_VSERVER 0x0400
+#define PROCESS_FLAG_LINUX_CGROUP 0x0800
+#define PROCESS_FLAG_LINUX_OOM 0x1000
+
+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 {
+ CMINFLT = 11,
+ CMAJFLT = 13,
+ UTIME = 14,
+ STIME = 15,
+ CUTIME = 16,
+ CSTIME = 17,
+ M_SHARE = 41,
+ M_TRS = 42,
+ M_DRS = 43,
+ M_LRS = 44,
+ M_DT = 45,
+ #ifdef HAVE_OPENVZ
+ CTID = 100,
+ VPID = 101,
+ #endif
+ #ifdef HAVE_VSERVER
+ VXID = 102,
+ #endif
+ #ifdef HAVE_TASKSTATS
+ 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 = 113,
+ #endif
+ OOM = 114,
+ IO_PRIORITY = 115,
+ LAST_PROCESSFIELD = 116,
+} LinuxProcessField;
+
+#include "IOPriority.h"
+
+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;
+ 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
+ unsigned int oom;
+} LinuxProcess;
+
+#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[] = {
+ [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
+ [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
+ [VXID] = { .name = "VXID", .title = " VXID ", .description = "VServer process ID", .flags = PROCESS_FLAG_LINUX_VSERVER, },
+#endif
+#ifdef HAVE_TASKSTATS
+ [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
+ [CGROUP] = { .name = "CGROUP", .title = " CGROUP ", .description = "Which cgroup the process is in", .flags = PROCESS_FLAG_LINUX_CGROUP, },
+#endif
+ [OOM] = { .name = "OOM", .title = " OOM ", .description = "OOM (Out-of-Memory) killer score", .flags = PROCESS_FLAG_LINUX_OOM, },
+ [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, },
+};
+
+ProcessPidColumn Process_pidColumns[] = {
+ { .id = PID, .label = "PID" },
+ { .id = PPID, .label = "PPID" },
+ #ifdef HAVE_OPENVZ
+ { .id = VPID, .label = "VPID" },
+ #endif
+ { .id = TPGID, .label = "TPGID" },
+ { .id = TGID, .label = "TGID" },
+ { .id = PGRP, .label = "PGRP" },
+ { .id = SESSION, .label = "SESN" },
+ { .id = OOM, .label = "OOM" },
+ { .id = 0, .label = NULL },
+};
+
+ProcessClass LinuxProcess_class = {
+ .super = {
+ .extends = Class(Process),
+ .display = Process_display,
+ .delete = Process_delete,
+ .compare = LinuxProcess_compare
+ },
+ .writeField = (Process_WriteField) LinuxProcess_writeField,
+};
+
+LinuxProcess* LinuxProcess_new(Settings* settings) {
+ LinuxProcess* this = xCalloc(1, sizeof(LinuxProcess));
+ Object_setClass(this, Class(LinuxProcess));
+ Process_init(&this->super, settings);
+ return this;
+}
+
+void Process_delete(Object* cast) {
+ LinuxProcess* this = (LinuxProcess*) cast;
+ Process_done((Process*)cast);
+#ifdef HAVE_CGROUP
+ free(this->cgroup);
+#endif
+ free(this);
+}
+
+/*
+[1] Note that before kernel 2.6.26 a process that has not asked for
+an io priority formally uses "none" as scheduling class, but the
+io scheduler will treat such processes as if it were in the best
+effort class. The priority within the best effort class will be
+dynamically derived from the cpu nice level of the process:
+io_priority = (cpu_nice + 20) / 5. -- From ionice(1) man page
+*/
+#define LinuxProcess_effectiveIOPriority(p_) (IOPriority_class(p_->ioPriority) == IOPRIO_CLASS_NONE ? IOPriority_tuple(IOPRIO_CLASS_BE, (p_->super.nice + 20) / 5) : p_->ioPriority)
+
+IOPriority LinuxProcess_updateIOPriority(LinuxProcess* this) {
+ IOPriority ioprio = syscall(SYS_ioprio_get, IOPRIO_WHO_PROCESS, this->super.pid);
+ this->ioPriority = ioprio;
+ return ioprio;
+}
+
+bool LinuxProcess_setIOPriority(LinuxProcess* this, IOPriority ioprio) {
+ syscall(SYS_ioprio_set, IOPRIO_WHO_PROCESS, this->super.pid, ioprio);
+ return (LinuxProcess_updateIOPriority(this) == ioprio);
+}
+
+void LinuxProcess_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 ((int)field) {
+ case CMINFLT: Process_colorNumber(str, lp->cminflt, coloring); return;
+ case CMAJFLT: Process_colorNumber(str, lp->cmajflt, coloring); return;
+ 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
+ case OOM: snprintf(buffer, n, Process_pidFormat, lp->oom); break;
+ case IO_PRIORITY: {
+ int klass = IOPriority_class(lp->ioPriority);
+ if (klass == IOPRIO_CLASS_NONE) {
+ // see note [1] above
+ snprintf(buffer, n, "B%1d ", (int) (this->nice + 20) / 5);
+ } else if (klass == IOPRIO_CLASS_BE) {
+ snprintf(buffer, n, "B%1d ", IOPriority_data(lp->ioPriority));
+ } else if (klass == IOPRIO_CLASS_RT) {
+ attr = CRT_colors[PROCESS_HIGH_PRIORITY];
+ snprintf(buffer, n, "R%1d ", IOPriority_data(lp->ioPriority));
+ } else if (lp->ioPriority == IOPriority_Idle) {
+ attr = CRT_colors[PROCESS_LOW_PRIORITY];
+ snprintf(buffer, n, "id ");
+ } else {
+ snprintf(buffer, n, "?? ");
+ }
+ break;
+ }
+ default:
+ Process_writeField((Process*)this, str, field);
+ return;
+ }
+ RichString_append(str, attr, buffer);
+}
+
+long LinuxProcess_compare(const void* v1, const void* v2) {
+ LinuxProcess *p1, *p2;
+ Settings *settings = ((Process*)v1)->settings;
+ if (settings->direction == 1) {
+ p1 = (LinuxProcess*)v1;
+ p2 = (LinuxProcess*)v2;
+ } else {
+ p2 = (LinuxProcess*)v1;
+ p1 = (LinuxProcess*)v2;
+ }
+ long long diff;
+ switch ((int)settings->sortKey) {
+ 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 - p1->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 (p2->ctid - p1->ctid);
+ case VPID:
+ return (p2->vpid - p1->vpid);
+ #endif
+ #ifdef HAVE_VSERVER
+ case VXID:
+ return (p2->vxid - p1->vxid);
+ #endif
+ #ifdef HAVE_CGROUP
+ case CGROUP:
+ return strcmp(p1->cgroup ? p1->cgroup : "", p2->cgroup ? p2->cgroup : "");
+ #endif
+ case OOM:
+ return (p2->oom - p1->oom);
+ case IO_PRIORITY:
+ return LinuxProcess_effectiveIOPriority(p1) - LinuxProcess_effectiveIOPriority(p2);
+ default:
+ return Process_compare(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
new file mode 100644
index 0000000..f2d81aa
--- /dev/null
+++ b/linux/LinuxProcess.h
@@ -0,0 +1,161 @@
+/* Do not edit this file. It was automatically generated. */
+
+#ifndef HEADER_LinuxProcess
+#define HEADER_LinuxProcess
+/*
+htop - LinuxProcess.h
+(C) 2014 Hisham H. Muhammad
+Released under the GNU GPL, see the COPYING file
+in the source distribution for its full text.
+*/
+
+
+#define PROCESS_FLAG_LINUX_IOPRIO 0x0100
+#define PROCESS_FLAG_LINUX_OPENVZ 0x0200
+#define PROCESS_FLAG_LINUX_VSERVER 0x0400
+#define PROCESS_FLAG_LINUX_CGROUP 0x0800
+#define PROCESS_FLAG_LINUX_OOM 0x1000
+
+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 {
+ CMINFLT = 11,
+ CMAJFLT = 13,
+ UTIME = 14,
+ STIME = 15,
+ CUTIME = 16,
+ CSTIME = 17,
+ M_SHARE = 41,
+ M_TRS = 42,
+ M_DRS = 43,
+ M_LRS = 44,
+ M_DT = 45,
+ #ifdef HAVE_OPENVZ
+ CTID = 100,
+ VPID = 101,
+ #endif
+ #ifdef HAVE_VSERVER
+ VXID = 102,
+ #endif
+ #ifdef HAVE_TASKSTATS
+ 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 = 113,
+ #endif
+ OOM = 114,
+ IO_PRIORITY = 115,
+ LAST_PROCESSFIELD = 116,
+} LinuxProcessField;
+
+#include "IOPriority.h"
+
+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;
+ 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
+ unsigned int oom;
+} LinuxProcess;
+
+#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[];
+
+extern ProcessPidColumn Process_pidColumns[];
+
+extern ProcessClass LinuxProcess_class;
+
+LinuxProcess* LinuxProcess_new(Settings* settings);
+
+void Process_delete(Object* cast);
+
+/*
+[1] Note that before kernel 2.6.26 a process that has not asked for
+an io priority formally uses "none" as scheduling class, but the
+io scheduler will treat such processes as if it were in the best
+effort class. The priority within the best effort class will be
+dynamically derived from the cpu nice level of the process:
+extern io_priority;
+*/
+#define LinuxProcess_effectiveIOPriority(p_) (IOPriority_class(p_->ioPriority) == IOPRIO_CLASS_NONE ? IOPriority_tuple(IOPRIO_CLASS_BE, (p_->super.nice + 20) / 5) : p_->ioPriority)
+
+IOPriority LinuxProcess_updateIOPriority(LinuxProcess* this);
+
+bool LinuxProcess_setIOPriority(LinuxProcess* this, IOPriority ioprio);
+
+void LinuxProcess_writeField(Process* this, RichString* str, ProcessField field);
+
+long LinuxProcess_compare(const void* v1, const void* v2);
+
+bool Process_isThread(Process* this);
+
+
+#endif
diff --git a/linux/LinuxProcessList.c b/linux/LinuxProcessList.c
new file mode 100644
index 0000000..591210e
--- /dev/null
+++ b/linux/LinuxProcessList.c
@@ -0,0 +1,775 @@
+/*
+htop - LinuxProcessList.c
+(C) 2014 Hisham H. Muhammad
+Released under the GNU GPL, see the COPYING file
+in the source distribution for its full text.
+*/
+
+#include "LinuxProcessList.h"
+#include "LinuxProcess.h"
+#include "CRT.h"
+#include "StringUtils.h"
+#include <errno.h>
+#include <sys/time.h>
+#include <sys/utsname.h>
+#include <sys/stat.h>
+#include <unistd.h>
+#include <dirent.h>
+#include <stdlib.h>
+#include <stdio.h>
+#include <signal.h>
+#include <stdbool.h>
+#include <stdarg.h>
+#include <math.h>
+#include <string.h>
+#include <time.h>
+#include <assert.h>
+#include <sys/types.h>
+#include <fcntl.h>
+
+/*{
+
+#include "ProcessList.h"
+
+typedef struct CPUData_ {
+ unsigned long long int totalTime;
+ unsigned long long int userTime;
+ unsigned long long int systemTime;
+ unsigned long long int systemAllTime;
+ unsigned long long int idleAllTime;
+ unsigned long long int idleTime;
+ unsigned long long int niceTime;
+ unsigned long long int ioWaitTime;
+ unsigned long long int irqTime;
+ unsigned long long int softIrqTime;
+ unsigned long long int stealTime;
+ unsigned long long int guestTime;
+
+ unsigned long long int totalPeriod;
+ unsigned long long int userPeriod;
+ unsigned long long int systemPeriod;
+ unsigned long long int systemAllPeriod;
+ unsigned long long int idleAllPeriod;
+ unsigned long long int idlePeriod;
+ unsigned long long int nicePeriod;
+ unsigned long long int ioWaitPeriod;
+ unsigned long long int irqPeriod;
+ unsigned long long int softIrqPeriod;
+ unsigned long long int stealPeriod;
+ unsigned long long int guestPeriod;
+} CPUData;
+
+typedef struct LinuxProcessList_ {
+ ProcessList super;
+
+ CPUData* cpus;
+
+} LinuxProcessList;
+
+#ifndef PROCDIR
+#define PROCDIR "/proc"
+#endif
+
+#ifndef PROCSTATFILE
+#define PROCSTATFILE PROCDIR "/stat"
+#endif
+
+#ifndef PROCMEMINFOFILE
+#define PROCMEMINFOFILE PROCDIR "/meminfo"
+#endif
+
+#ifndef PROC_LINE_LENGTH
+#define PROC_LINE_LENGTH 512
+#endif
+
+}*/
+
+#ifndef CLAMP
+#define CLAMP(x,low,high) (((x)>(high))?(high):(((x)<(low))?(low):(x)))
+#endif
+
+ProcessList* ProcessList_new(UsersTable* usersTable, Hashtable* pidWhiteList, uid_t userId) {
+ LinuxProcessList* this = xCalloc(1, sizeof(LinuxProcessList));
+ ProcessList* pl = &(this->super);
+ ProcessList_init(pl, Class(LinuxProcess), usersTable, pidWhiteList, userId);
+
+ // Update CPU count:
+ FILE* file = fopen(PROCSTATFILE, "r");
+ if (file == NULL) {
+ CRT_fatalError("Cannot open " PROCSTATFILE);
+ }
+ char buffer[PROC_LINE_LENGTH + 1];
+ int cpus = -1;
+ do {
+ cpus++;
+ char * s = fgets(buffer, PROC_LINE_LENGTH, file);
+ (void) s;
+ } while (String_startsWith(buffer, "cpu"));
+ fclose(file);
+
+ pl->cpuCount = MAX(cpus - 1, 1);
+ this->cpus = xCalloc(cpus, sizeof(CPUData));
+
+ for (int i = 0; i < cpus; i++) {
+ this->cpus[i].totalTime = 1;
+ this->cpus[i].totalPeriod = 1;
+ }
+
+ return pl;
+}
+
+void ProcessList_delete(ProcessList* pl) {
+ LinuxProcessList* this = (LinuxProcessList*) pl;
+ ProcessList_done(pl);
+ free(this->cpus);
+ free(this);
+}
+
+static ssize_t xread(int fd, void *buf, size_t count) {
+ // Read some bytes. Retry on EINTR and when we don't get as many bytes as we requested.
+ size_t alreadyRead = 0;
+ for(;;) {
+ ssize_t res = read(fd, buf, count);
+ if (res == -1 && errno == EINTR) continue;
+ if (res > 0) {
+ buf = ((char*)buf)+res;
+ count -= res;
+ alreadyRead += res;
+ }
+ if (res == -1) return -1;
+ if (count == 0 || res == 0) return alreadyRead;
+ }
+}
+
+static double jiffy = 0.0;
+
+static inline unsigned long long LinuxProcess_adjustTime(unsigned long long t) {
+ if(jiffy == 0.0) jiffy = sysconf(_SC_CLK_TCK);
+ double jiffytime = 1.0 / jiffy;
+ return (unsigned long long) t * jiffytime * 100;
+}
+
+static bool LinuxProcessList_readStatFile(Process *process, const char* dirname, const char* name, char* command, int* commLen) {
+ LinuxProcess* lp = (LinuxProcess*) process;
+ char filename[MAX_NAME+1];
+ snprintf(filename, MAX_NAME, "%s/%s/stat", dirname, name);
+ int fd = open(filename, O_RDONLY);
+ if (fd == -1)
+ return false;
+
+ static char buf[MAX_READ+1];
+
+ int size = xread(fd, buf, MAX_READ);
+ close(fd);
+ if (size <= 0) return false;
+ buf[size] = '\0';
+
+ assert(process->pid == atoi(buf));
+ char *location = strchr(buf, ' ');
+ if (!location) return false;
+
+ location += 2;
+ char *end = strrchr(location, ')');
+ if (!end) return false;
+
+ int commsize = end - location;
+ memcpy(command, location, commsize);
+ command[commsize] = '\0';
+ *commLen = commsize;
+ location = end + 2;
+
+ process->state = location[0];
+ location += 2;
+ process->ppid = strtol(location, &location, 10);
+ location += 1;
+ process->pgrp = strtoul(location, &location, 10);
+ location += 1;
+ process->session = strtoul(location, &location, 10);
+ location += 1;
+ process->tty_nr = strtoul(location, &location, 10);
+ location += 1;
+ process->tpgid = strtol(location, &location, 10);
+ location += 1;
+ process->flags = strtoul(location, &location, 10);
+ location += 1;
+ process->minflt = strtoull(location, &location, 10);
+ location += 1;
+ lp->cminflt = strtoull(location, &location, 10);
+ location += 1;
+ process->majflt = strtoull(location, &location, 10);
+ location += 1;
+ lp->cmajflt = strtoull(location, &location, 10);
+ location += 1;
+ lp->utime = LinuxProcess_adjustTime(strtoull(location, &location, 10));
+ location += 1;
+ lp->stime = LinuxProcess_adjustTime(strtoull(location, &location, 10));
+ location += 1;
+ lp->cutime = LinuxProcess_adjustTime(strtoull(location, &location, 10));
+ location += 1;
+ lp->cstime = LinuxProcess_adjustTime(strtoull(location, &location, 10));
+ location += 1;
+ process->priority = strtol(location, &location, 10);
+ location += 1;
+ process->nice = strtol(location, &location, 10);
+ location += 1;
+ process->nlwp = strtol(location, &location, 10);
+ location += 1;
+ for (int i=0; i<17; i++) location = strchr(location, ' ')+1;
+ process->exit_signal = strtol(location, &location, 10);
+ location += 1;
+ assert(location != NULL);
+ process->processor = strtol(location, &location, 10);
+
+ process->time = lp->utime + lp->stime;
+
+ return true;
+}
+
+
+static bool LinuxProcessList_statProcessDir(Process* process, const char* dirname, char* name, time_t curTime) {
+ char filename[MAX_NAME+1];
+ filename[MAX_NAME] = '\0';
+
+ snprintf(filename, MAX_NAME, "%s/%s", dirname, name);
+ struct stat sstat;
+ int statok = stat(filename, &sstat);
+ if (statok == -1)
+ return false;
+ process->st_uid = sstat.st_uid;
+
+ struct tm date;
+ time_t ctime = sstat.st_ctime;
+ process->starttime_ctime = ctime;
+ (void) localtime_r((time_t*) &ctime, &date);
+ strftime(process->starttime_show, 7, ((ctime > curTime - 86400) ? "%R " : "%b%d "), &date);
+
+ return true;
+}
+
+#ifdef HAVE_TASKSTATS
+
+static void LinuxProcessList_readIoFile(LinuxProcess* process, const char* dirname, char* name, unsigned long long now) {
+ char filename[MAX_NAME+1];
+ filename[MAX_NAME] = '\0';
+
+ snprintf(filename, MAX_NAME, "%s/%s/io", dirname, name);
+ int fd = open(filename, O_RDONLY);
+ if (fd == -1) {
+ process->io_rate_read_bps = -1;
+ process->io_rate_write_bps = -1;
+ return;
+ }
+
+ char buffer[1024];
+ ssize_t buflen = xread(fd, buffer, 1023);
+ close(fd);
+ if (buflen < 1) return;
+ buffer[buflen] = '\0';
+ unsigned long long last_read = process->io_read_bytes;
+ unsigned long long last_write = process->io_write_bytes;
+ char *buf = buffer;
+ char *line = NULL;
+ while ((line = strsep(&buf, "\n")) != NULL) {
+ switch (line[0]) {
+ case 'r':
+ if (line[1] == 'c' && strncmp(line+2, "har: ", 5) == 0)
+ process->io_rchar = strtoull(line+7, NULL, 10);
+ else if (strncmp(line+1, "ead_bytes: ", 11) == 0) {
+ process->io_read_bytes = strtoull(line+12, NULL, 10);
+ process->io_rate_read_bps =
+ ((double)(process->io_read_bytes - last_read))/(((double)(now - process->io_rate_read_time))/1000);
+ process->io_rate_read_time = now;
+ }
+ break;
+ case 'w':
+ if (line[1] == 'c' && strncmp(line+2, "har: ", 5) == 0)
+ process->io_wchar = strtoull(line+7, NULL, 10);
+ else if (strncmp(line+1, "rite_bytes: ", 12) == 0) {
+ process->io_write_bytes = strtoull(line+13, NULL, 10);
+ process->io_rate_write_bps =
+ ((double)(process->io_write_bytes - last_write))/(((double)(now - process->io_rate_write_time))/1000);
+ process->io_rate_write_time = now;
+ }
+ break;
+ case 's':
+ if (line[5] == 'r' && strncmp(line+1, "yscr: ", 6) == 0)
+ process->io_syscr = strtoull(line+7, NULL, 10);
+ else if (strncmp(line+1, "yscw: ", 6) == 0)
+ sscanf(line, "syscw: %32llu", &process->io_syscw);
+ process->io_syscw = strtoull(line+7, NULL, 10);
+ break;
+ case 'c':
+ if (strncmp(line+1, "ancelled_write_bytes: ", 22) == 0)
+ process->io_cancelled_write_bytes = strtoull(line+23, NULL, 10);
+ }
+ }
+}
+
+#endif
+
+
+
+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);
+ if (fd == -1)
+ return false;
+ char buf[PROC_LINE_LENGTH + 1];
+ ssize_t rres = xread(fd, buf, PROC_LINE_LENGTH);
+ close(fd);
+ if (rres < 1) return false;
+
+ char *p = buf;
+ errno = 0;
+ 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++;
+ process->m_drs = strtol(p, &p, 10); if (*p == ' ') p++;
+ process->m_dt = strtol(p, &p, 10);
+ return (errno == 0);
+}
+
+#ifdef HAVE_OPENVZ
+
+static void LinuxProcessList_readOpenVZData(LinuxProcess* process, const char* dirname, const char* name) {
+ if ( (access("/proc/vz", R_OK) != 0)) {
+ process->vpid = process->super.pid;
+ process->ctid = 0;
+ return;
+ }
+ char filename[MAX_NAME+1];
+ snprintf(filename, MAX_NAME, "%s/%s/stat", dirname, name);
+ FILE* file = fopen(filename, "r");
+ if (!file)
+ return;
+ (void) fscanf(file,
+ "%*32u %*32s %*1c %*32u %*32u %*32u %*32u %*32u %*32u %*32u "
+ "%*32u %*32u %*32u %*32u %*32u %*32u %*32u %*32u "
+ "%*32u %*32u %*32u %*32u %*32u %*32u %*32u %*32u "
+ "%*32u %*32u %*32u %*32u %*32u %*32u %*32u %*32u "
+ "%*32u %*32u %*32u %*32u %*32u %*32u %*32u %*32u "
+ "%*32u %*32u %*32u %*32u %*32u %*32u %*32u "
+ "%*32u %*32u %32u %32u",
+ &process->vpid, &process->ctid);
+ fclose(file);
+ return;
+}
+
+#endif
+
+#ifdef HAVE_CGROUP
+
+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");
+ if (!file) {
+ process->cgroup = xStrdup("");
+ return;
+ }
+ char output[PROC_LINE_LENGTH + 1];
+ output[0] = '\0';
+ char* at = output;
+ int left = PROC_LINE_LENGTH;
+ while (!feof(file) && left > 0) {
+ char buffer[PROC_LINE_LENGTH + 1];
+ char *ok = fgets(buffer, PROC_LINE_LENGTH, file);
+ if (!ok) break;
+ char* group = strchr(buffer, ':');
+ if (!group) break;
+ if (at != output) {
+ *at = ';';
+ at++;
+ left--;
+ }
+ int wrote = snprintf(at, left, "%s", group);
+ left -= wrote;
+ }
+ fclose(file);
+ free(process->cgroup);
+ process->cgroup = xStrdup(output);
+}
+
+#endif
+
+#ifdef HAVE_VSERVER
+
+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");
+ if (!file)
+ return;
+ char buffer[PROC_LINE_LENGTH + 1];
+ process->vxid = 0;
+ while (fgets(buffer, PROC_LINE_LENGTH, file)) {
+ if (String_startsWith(buffer, "VxID:")) {
+ int vxid;
+ int ok = sscanf(buffer, "VxID:\t%32d", &vxid);
+ if (ok >= 1) {
+ process->vxid = vxid;
+ }
+ }
+ #if defined HAVE_ANCIENT_VSERVER
+ else if (String_startsWith(buffer, "s_context:")) {
+ int vxid;
+ int ok = sscanf(buffer, "s_context:\t%32d", &vxid);
+ if (ok >= 1) {
+ process->vxid = vxid;
+ }
+ }
+ #endif
+ }
+ fclose(file);
+}
+
+#endif
+
+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");
+ if (!file)
+ return;
+ char buffer[PROC_LINE_LENGTH + 1];
+ if (fgets(buffer, PROC_LINE_LENGTH, file)) {
+ unsigned int oom;
+ int ok = sscanf(buffer, "%32u", &oom);
+ if (ok >= 1) {
+ process->oom = oom;
+ }
+ }
+ fclose(file);
+}
+
+static void setCommand(Process* process, const char* command, int len) {
+ if (process->comm && process->commLen <= len) {
+ strncpy(process->comm, command, len + 1);
+ } else {
+ free(process->comm);
+ process->comm = xStrdup(command);
+ }
+ process->commLen = len;
+}
+
+static bool LinuxProcessList_readCmdlineFile(Process* process, const char* dirname, const char* name) {
+ if (Process_isKernelThread(process))
+ return true;
+
+ char filename[MAX_NAME+1];
+ snprintf(filename, MAX_NAME, "%s/%s/cmdline", dirname, name);
+ int fd = open(filename, O_RDONLY);
+ if (fd == -1)
+ return false;
+
+ char command[4096+1]; // max cmdline length on Linux
+ int amtRead = xread(fd, command, sizeof(command) - 1);
+ close(fd);
+ int tokenEnd = 0;
+ if (amtRead > 0) {
+ for (int i = 0; i < amtRead; i++)
+ if (command[i] == '\0' || command[i] == '\n') {
+ if (tokenEnd == 0) {
+ tokenEnd = i;
+ }
+ command[i] = ' ';
+ }
+ }
+ if (tokenEnd == 0) {
+ tokenEnd = amtRead;
+ }
+ command[amtRead] = '\0';
+ process->basenameOffset = tokenEnd;
+ setCommand(process, command, amtRead);
+
+ return true;
+}
+
+static bool LinuxProcessList_recurseProcTree(LinuxProcessList* this, const char* dirname, Process* parent, double period, struct timeval tv) {
+ ProcessList* pl = (ProcessList*) this;
+ DIR* dir;
+ struct dirent* entry;
+ Settings* settings = pl->settings;
+
+ time_t curTime = tv.tv_sec;
+ #ifdef HAVE_TASKSTATS
+ unsigned long long now = tv.tv_sec*1000LL+tv.tv_usec/1000LL;
+ #endif
+
+ dir = opendir(dirname);
+ if (!dir) return false;
+ int cpus = pl->cpuCount;
+ bool hideKernelThreads = settings->hideKernelThreads;
+ bool hideUserlandThreads = settings->hideUserlandThreads;
+ while ((entry = readdir(dir)) != NULL) {
+ char* name = entry->d_name;
+
+ // The RedHat kernel hides threads with a dot.
+ // I believe this is non-standard.
+ if ((!settings->hideThreads) && name[0] == '.') {
+ name++;
+ }
+
+ // Just skip all non-number directories.
+ if (name[0] < '0' || name[0] > '9') {
+ continue;
+ }
+
+ // filename is a number: process directory
+ int pid = atoi(name);
+
+ if (parent && pid == parent->pid)
+ continue;
+
+ if (pid <= 0)
+ continue;
+
+ bool preExisting = false;
+ Process* proc = ProcessList_getProcess(pl, pid, &preExisting, (Process_New) LinuxProcess_new);
+ 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_recurseProcTree(this, subdirname, proc, period, tv);
+
+ #ifdef HAVE_TASKSTATS
+ if (settings->flags & PROCESS_FLAG_IO)
+ LinuxProcessList_readIoFile(lp, dirname, name, now);
+ #endif
+
+ if (! LinuxProcessList_readStatmFile(lp, dirname, name))
+ goto errorReadingProcess;
+
+ proc->show = ! ((hideKernelThreads && Process_isKernelThread(proc)) || (hideUserlandThreads && Process_isUserlandThread(proc)));
+
+ char command[MAX_NAME+1];
+ unsigned long long int lasttimes = (lp->utime + lp->stime);
+ int commLen = 0;
+ if (! LinuxProcessList_readStatFile(proc, dirname, name, command, &commLen))
+ goto errorReadingProcess;
+ if (settings->flags & PROCESS_FLAG_LINUX_IOPRIO)
+ LinuxProcess_updateIOPriority(lp);
+ float percent_cpu = (lp->utime + lp->stime - lasttimes) / period * 100.0;
+ proc->percent_cpu = CLAMP(percent_cpu, 0.0, cpus * 100.0);
+ if (isnan(proc->percent_cpu)) proc->percent_cpu = 0.0;
+ proc->percent_mem = (proc->m_resident * PAGE_SIZE_KB) / (double)(pl->totalMem) * 100.0;
+
+ if(!preExisting) {
+
+ if (! LinuxProcessList_statProcessDir(proc, dirname, name, curTime))
+ goto errorReadingProcess;
+
+ proc->user = UsersTable_getRef(pl->usersTable, proc->st_uid);
+
+ #ifdef HAVE_OPENVZ
+ if (settings->flags & PROCESS_FLAG_LINUX_OPENVZ) {
+ LinuxProcessList_readOpenVZData(lp, dirname, name);
+ }
+ #endif
+
+ #ifdef HAVE_VSERVER
+ if (settings->flags & PROCESS_FLAG_LINUX_VSERVER) {
+ LinuxProcessList_readVServerData(lp, dirname, name);
+ }
+ #endif
+
+ if (! LinuxProcessList_readCmdlineFile(proc, dirname, name)) {
+ goto errorReadingProcess;
+ }
+
+ ProcessList_add(pl, proc);
+ } else {
+ if (settings->updateProcessNames) {
+ if (! LinuxProcessList_readCmdlineFile(proc, dirname, name)) {
+ goto errorReadingProcess;
+ }
+ }
+ }
+
+ #ifdef HAVE_CGROUP
+ if (settings->flags & PROCESS_FLAG_LINUX_CGROUP)
+ LinuxProcessList_readCGroupFile(lp, dirname, name);
+ #endif
+
+ if (settings->flags & PROCESS_FLAG_LINUX_OOM)
+ LinuxProcessList_readOomData(lp, dirname, name);
+
+ if (proc->state == 'Z') {
+ proc->basenameOffset = -1;
+ setCommand(proc, command, commLen);
+ } else if (Process_isThread(proc)) {
+ if (settings->showThreadNames || Process_isKernelThread(proc) || proc->state == 'Z') {
+ proc->basenameOffset = -1;
+ setCommand(proc, command, commLen);
+ } else if (settings->showThreadNames) {
+ if (! LinuxProcessList_readCmdlineFile(proc, dirname, name))
+ goto errorReadingProcess;
+ }
+ if (Process_isKernelThread(proc)) {
+ pl->kernelThreads++;
+ } else {
+ pl->userlandThreads++;
+ }
+ }
+
+ pl->totalTasks++;
+ if (proc->state == 'R')
+ pl->runningTasks++;
+ proc->updated = true;
+ continue;
+
+ // Exception handler.
+ errorReadingProcess: {
+ if (preExisting) {
+ ProcessList_remove(pl, proc);
+ } else {
+ Process_delete((Object*)proc);
+ }
+ }
+ }
+ closedir(dir);
+ return true;
+}
+
+static inline void LinuxProcessList_scanMemoryInfo(ProcessList* this) {
+ unsigned long long int swapFree = 0;
+ unsigned long long int shmem = 0;
+ unsigned long long int sreclaimable = 0;
+
+ FILE* file = fopen(PROCMEMINFOFILE, "r");
+ if (file == NULL) {
+ CRT_fatalError("Cannot open " PROCMEMINFOFILE);
+ }
+ char buffer[128];
+ while (fgets(buffer, 128, file)) {
+
+ #define tryRead(label, variable) (String_startsWith(buffer, label) && sscanf(buffer + strlen(label), " %32llu kB", variable))
+ switch (buffer[0]) {
+ case 'M':
+ if (tryRead("MemTotal:", &this->totalMem)) {}
+ else if (tryRead("MemFree:", &this->freeMem)) {}
+ else if (tryRead("MemShared:", &this->sharedMem)) {}
+ break;
+ case 'B':
+ if (tryRead("Buffers:", &this->buffersMem)) {}
+ break;
+ case 'C':
+ if (tryRead("Cached:", &this->cachedMem)) {}
+ break;
+ case 'S':
+ switch (buffer[1]) {
+ case 'w':
+ if (tryRead("SwapTotal:", &this->totalSwap)) {}
+ else if (tryRead("SwapFree:", &swapFree)) {}
+ break;
+ case 'h':
+ if (tryRead("Shmem:", &shmem)) {}
+ break;
+ case 'R':
+ if (tryRead("SReclaimable:", &sreclaimable)) {}
+ break;
+ }
+ break;
+ }
+ #undef tryRead
+ }
+
+ this->usedMem = this->totalMem - this->freeMem;
+ this->cachedMem = this->cachedMem + sreclaimable - shmem;
+ this->usedSwap = this->totalSwap - swapFree;
+ fclose(file);
+}
+
+static inline double LinuxProcessList_scanCPUTime(LinuxProcessList* this) {
+
+ FILE* file = fopen(PROCSTATFILE, "r");
+ if (file == NULL) {
+ CRT_fatalError("Cannot open " PROCSTATFILE);
+ }
+ int cpus = this->super.cpuCount;
+ assert(cpus > 0);
+ for (int i = 0; i <= cpus; i++) {
+ char buffer[PROC_LINE_LENGTH + 1];
+ unsigned long long int usertime, nicetime, systemtime, idletime;
+ unsigned long long int ioWait, irq, softIrq, steal, guest, guestnice;
+ ioWait = irq = softIrq = steal = guest = guestnice = 0;
+ // Depending on your kernel version,
+ // 5, 7, 8 or 9 of these fields will be set.
+ // The rest will remain at zero.
+ char* ok = fgets(buffer, PROC_LINE_LENGTH, file);
+ if (!ok) buffer[0] = '\0';
+ if (i == 0)
+ sscanf(buffer, "cpu %16llu %16llu %16llu %16llu %16llu %16llu %16llu %16llu %16llu %16llu", &usertime, &nicetime, &systemtime, &idletime, &ioWait, &irq, &softIrq, &steal, &guest, &guestnice);
+ else {
+ int cpuid;
+ sscanf(buffer, "cpu%4d %16llu %16llu %16llu %16llu %16llu %16llu %16llu %16llu %16llu %16llu", &cpuid, &usertime, &nicetime, &systemtime, &idletime, &ioWait, &irq, &softIrq, &steal, &guest, &guestnice);
+ assert(cpuid == i - 1);
+ }
+ // Guest time is already accounted in usertime
+ usertime = usertime - guest;
+ nicetime = nicetime - guestnice;
+ // Fields existing on kernels >= 2.6
+ // (and RHEL's patched kernel 2.4...)
+ unsigned long long int idlealltime = idletime + ioWait;
+ unsigned long long int systemalltime = systemtime + irq + softIrq;
+ unsigned long long int virtalltime = guest + guestnice;
+ unsigned long long int totaltime = usertime + nicetime + systemalltime + idlealltime + steal + virtalltime;
+ CPUData* cpuData = &(this->cpus[i]);
+ assert (systemtime >= cpuData->systemTime);
+ assert (idletime >= cpuData->idleTime);
+ assert (totaltime >= cpuData->totalTime);
+ assert (systemalltime >= cpuData->systemAllTime);
+ assert (idlealltime >= cpuData->idleAllTime);
+ assert (ioWait >= cpuData->ioWaitTime);
+ assert (irq >= cpuData->irqTime);
+ assert (softIrq >= cpuData->softIrqTime);
+ assert (steal >= cpuData->stealTime);
+ assert (virtalltime >= cpuData->guestTime);
+ // Since we do a subtraction (usertime - guest) and cputime64_to_clock_t()
+ // used in /proc/stat rounds down numbers, it can lead to a case where the
+ // integer overflow.
+ cpuData->userPeriod = (usertime > cpuData->userTime) ? usertime - cpuData->userTime : 0;
+ cpuData->nicePeriod = (nicetime > cpuData->niceTime) ? nicetime - cpuData->niceTime : 0;
+ cpuData->systemPeriod = systemtime - cpuData->systemTime;
+ cpuData->systemAllPeriod = systemalltime - cpuData->systemAllTime;
+ cpuData->idleAllPeriod = idlealltime - cpuData->idleAllTime;
+ cpuData->idlePeriod = idletime - cpuData->idleTime;
+ cpuData->ioWaitPeriod = ioWait - cpuData->ioWaitTime;
+ cpuData->irqPeriod = irq - cpuData->irqTime;
+ cpuData->softIrqPeriod = softIrq - cpuData->softIrqTime;
+ cpuData->stealPeriod = steal - cpuData->stealTime;
+ cpuData->guestPeriod = virtalltime - cpuData->guestTime;
+ cpuData->totalPeriod = totaltime - cpuData->totalTime;
+ cpuData->userTime = usertime;
+ cpuData->niceTime = nicetime;
+ cpuData->systemTime = systemtime;
+ cpuData->systemAllTime = systemalltime;
+ cpuData->idleAllTime = idlealltime;
+ cpuData->idleTime = idletime;
+ cpuData->ioWaitTime = ioWait;
+ cpuData->irqTime = irq;
+ cpuData->softIrqTime = softIrq;
+ cpuData->stealTime = steal;
+ cpuData->guestTime = virtalltime;
+ cpuData->totalTime = totaltime;
+ }
+ double period = (double)this->cpus[0].totalPeriod / cpus;
+ fclose(file);
+ return period;
+}
+
+void ProcessList_goThroughEntries(ProcessList* super) {
+ LinuxProcessList* this = (LinuxProcessList*) super;
+
+ LinuxProcessList_scanMemoryInfo(super);
+ double period = LinuxProcessList_scanCPUTime(this);
+
+ struct timeval tv;
+ gettimeofday(&tv, NULL);
+ LinuxProcessList_recurseProcTree(this, PROCDIR, NULL, period, tv);
+}
diff --git a/linux/LinuxProcessList.h b/linux/LinuxProcessList.h
new file mode 100644
index 0000000..9772581
--- /dev/null
+++ b/linux/LinuxProcessList.h
@@ -0,0 +1,94 @@
+/* Do not edit this file. It was automatically generated. */
+
+#ifndef HEADER_LinuxProcessList
+#define HEADER_LinuxProcessList
+/*
+htop - LinuxProcessList.h
+(C) 2014 Hisham H. Muhammad
+Released under the GNU GPL, see the COPYING file
+in the source distribution for its full text.
+*/
+
+
+#include "ProcessList.h"
+
+typedef struct CPUData_ {
+ unsigned long long int totalTime;
+ unsigned long long int userTime;
+ unsigned long long int systemTime;
+ unsigned long long int systemAllTime;
+ unsigned long long int idleAllTime;
+ unsigned long long int idleTime;
+ unsigned long long int niceTime;
+ unsigned long long int ioWaitTime;
+ unsigned long long int irqTime;
+ unsigned long long int softIrqTime;
+ unsigned long long int stealTime;
+ unsigned long long int guestTime;
+
+ unsigned long long int totalPeriod;
+ unsigned long long int userPeriod;
+ unsigned long long int systemPeriod;
+ unsigned long long int systemAllPeriod;
+ unsigned long long int idleAllPeriod;
+ unsigned long long int idlePeriod;
+ unsigned long long int nicePeriod;
+ unsigned long long int ioWaitPeriod;
+ unsigned long long int irqPeriod;
+ unsigned long long int softIrqPeriod;
+ unsigned long long int stealPeriod;
+ unsigned long long int guestPeriod;
+} CPUData;
+
+typedef struct LinuxProcessList_ {
+ ProcessList super;
+
+ CPUData* cpus;
+
+} LinuxProcessList;
+
+#ifndef PROCDIR
+#define PROCDIR "/proc"
+#endif
+
+#ifndef PROCSTATFILE
+#define PROCSTATFILE PROCDIR "/stat"
+#endif
+
+#ifndef PROCMEMINFOFILE
+#define PROCMEMINFOFILE PROCDIR "/meminfo"
+#endif
+
+#ifndef PROC_LINE_LENGTH
+#define PROC_LINE_LENGTH 512
+#endif
+
+
+#ifndef CLAMP
+#define CLAMP(x,low,high) (((x)>(high))?(high):(((x)<(low))?(low):(x)))
+#endif
+
+ProcessList* ProcessList_new(UsersTable* usersTable, Hashtable* pidWhiteList, uid_t userId);
+
+void ProcessList_delete(ProcessList* pl);
+
+
+#ifdef HAVE_TASKSTATS
+
+#endif
+
+#ifdef HAVE_OPENVZ
+
+#endif
+
+#ifdef HAVE_CGROUP
+
+#endif
+
+#ifdef HAVE_VSERVER
+
+#endif
+
+void ProcessList_goThroughEntries(ProcessList* super);
+
+#endif
diff --git a/linux/Platform.c b/linux/Platform.c
new file mode 100644
index 0000000..04360ca
--- /dev/null
+++ b/linux/Platform.c
@@ -0,0 +1,239 @@
+/*
+htop - linux/Platform.c
+(C) 2014 Hisham H. Muhammad
+Released under the GNU GPL, see the COPYING file
+in the source distribution for its full text.
+*/
+
+#include "Platform.h"
+#include "IOPriority.h"
+#include "IOPriorityPanel.h"
+#include "LinuxProcess.h"
+#include "LinuxProcessList.h"
+#include "Battery.h"
+
+#include "Meter.h"
+#include "CPUMeter.h"
+#include "MemoryMeter.h"
+#include "SwapMeter.h"
+#include "TasksMeter.h"
+#include "LoadAverageMeter.h"
+#include "UptimeMeter.h"
+#include "ClockMeter.h"
+#include "HostnameMeter.h"
+#include "LinuxProcess.h"
+
+#include <math.h>
+#include <assert.h>
+#include <limits.h>
+#include <stdio.h>
+#include <stdlib.h>
+
+/*{
+#include "Action.h"
+#include "MainPanel.h"
+#include "BatteryMeter.h"
+#include "LinuxProcess.h"
+#include "SignalsPanel.h"
+}*/
+
+#ifndef CLAMP
+#define CLAMP(x,low,high) (((x)>(high))?(high):(((x)<(low))?(low):(x)))
+#endif
+
+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;
+
+SignalItem Platform_signals[] = {
+ { .name = " 0 Cancel", .number = 0 },
+ { .name = " 1 SIGHUP", .number = 1 },
+ { .name = " 2 SIGINT", .number = 2 },
+ { .name = " 3 SIGQUIT", .number = 3 },
+ { .name = " 4 SIGILL", .number = 4 },
+ { .name = " 5 SIGTRAP", .number = 5 },
+ { .name = " 6 SIGABRT", .number = 6 },
+ { .name = " 6 SIGIOT", .number = 6 },
+ { .name = " 7 SIGBUS", .number = 7 },
+ { .name = " 8 SIGFPE", .number = 8 },
+ { .name = " 9 SIGKILL", .number = 9 },
+ { .name = "10 SIGUSR1", .number = 10 },
+ { .name = "11 SIGSEGV", .number = 11 },
+ { .name = "12 SIGUSR2", .number = 12 },
+ { .name = "13 SIGPIPE", .number = 13 },
+ { .name = "14 SIGALRM", .number = 14 },
+ { .name = "15 SIGTERM", .number = 15 },
+ { .name = "16 SIGSTKFLT", .number = 16 },
+ { .name = "17 SIGCHLD", .number = 17 },
+ { .name = "18 SIGCONT", .number = 18 },
+ { .name = "19 SIGSTOP", .number = 19 },
+ { .name = "20 SIGTSTP", .number = 20 },
+ { .name = "21 SIGTTIN", .number = 21 },
+ { .name = "22 SIGTTOU", .number = 22 },
+ { .name = "23 SIGURG", .number = 23 },
+ { .name = "24 SIGXCPU", .number = 24 },
+ { .name = "25 SIGXFSZ", .number = 25 },
+ { .name = "26 SIGVTALRM", .number = 26 },
+ { .name = "27 SIGPROF", .number = 27 },
+ { .name = "28 SIGWINCH", .number = 28 },
+ { .name = "29 SIGIO", .number = 29 },
+ { .name = "29 SIGPOLL", .number = 29 },
+ { .name = "30 SIGPWR", .number = 30 },
+ { .name = "31 SIGSYS", .number = 31 },
+};
+
+unsigned int Platform_numberOfSignals = sizeof(Platform_signals)/sizeof(SignalItem);
+
+static Htop_Reaction Platform_actionSetIOPriority(State* st) {
+ Panel* panel = st->panel;
+
+ LinuxProcess* p = (LinuxProcess*) Panel_getSelected(panel);
+ if (!p) return HTOP_OK;
+ IOPriority ioprio = p->ioPriority;
+ Panel* ioprioPanel = IOPriorityPanel_new(ioprio);
+ void* set = Action_pickFromVector(st, ioprioPanel, 21);
+ if (set) {
+ IOPriority ioprio = IOPriorityPanel_getIOPriority(ioprioPanel);
+ bool ok = MainPanel_foreachProcess((MainPanel*)panel, (MainPanel_ForeachProcessFn) LinuxProcess_setIOPriority, (size_t) ioprio, NULL);
+ if (!ok)
+ beep();
+ }
+ Panel_delete((Object*)ioprioPanel);
+ return HTOP_REFRESH | HTOP_REDRAW_BAR | HTOP_UPDATE_PANELHDR;
+}
+
+void Platform_setBindings(Htop_Action* keys) {
+ keys['i'] = Platform_actionSetIOPriority;
+}
+
+MeterClass* Platform_meterTypes[] = {
+ &CPUMeter_class,
+ &ClockMeter_class,
+ &LoadAverageMeter_class,
+ &LoadMeter_class,
+ &MemoryMeter_class,
+ &SwapMeter_class,
+ &TasksMeter_class,
+ &UptimeMeter_class,
+ &BatteryMeter_class,
+ &HostnameMeter_class,
+ &AllCPUsMeter_class,
+ &AllCPUs2Meter_class,
+ &LeftCPUsMeter_class,
+ &RightCPUsMeter_class,
+ &LeftCPUs2Meter_class,
+ &RightCPUs2Meter_class,
+ &BlankMeter_class,
+ NULL
+};
+
+int Platform_getUptime() {
+ double uptime = 0;
+ FILE* fd = fopen(PROCDIR "/uptime", "r");
+ if (fd) {
+ int n = fscanf(fd, "%64lf", &uptime);
+ fclose(fd);
+ if (n <= 0) return 0;
+ }
+ return (int) floor(uptime);
+}
+
+void Platform_getLoadAverage(double* one, double* five, double* fifteen) {
+ int activeProcs, totalProcs, lastProc;
+ *one = 0; *five = 0; *fifteen = 0;
+ FILE *fd = fopen(PROCDIR "/loadavg", "r");
+ if (fd) {
+ int total = fscanf(fd, "%32lf %32lf %32lf %32d/%32d %32d", one, five, fifteen,
+ &activeProcs, &totalProcs, &lastProc);
+ (void) total;
+ assert(total == 6);
+ fclose(fd);
+ }
+}
+
+int Platform_getMaxPid() {
+ FILE* file = fopen(PROCDIR "/sys/kernel/pid_max", "r");
+ if (!file) return -1;
+ int maxPid = 4194303;
+ int match = fscanf(file, "%32d", &maxPid);
+ (void) match;
+ fclose(file);
+ return maxPid;
+}
+
+double Platform_setCPUValues(Meter* this, int cpu) {
+ LinuxProcessList* pl = (LinuxProcessList*) this->pl;
+ CPUData* cpuData = &(pl->cpus[cpu]);
+ double total = (double) ( cpuData->totalPeriod == 0 ? 1 : cpuData->totalPeriod);
+ double percent;
+ double* v = this->values;
+ v[CPU_METER_NICE] = cpuData->nicePeriod / total * 100.0;
+ v[CPU_METER_NORMAL] = cpuData->userPeriod / total * 100.0;
+ if (this->pl->settings->detailedCPUTime) {
+ v[CPU_METER_KERNEL] = cpuData->systemPeriod / total * 100.0;
+ v[CPU_METER_IRQ] = cpuData->irqPeriod / total * 100.0;
+ v[CPU_METER_SOFTIRQ] = cpuData->softIrqPeriod / total * 100.0;
+ v[CPU_METER_STEAL] = cpuData->stealPeriod / total * 100.0;
+ v[CPU_METER_GUEST] = cpuData->guestPeriod / total * 100.0;
+ v[CPU_METER_IOWAIT] = cpuData->ioWaitPeriod / total * 100.0;
+ Meter_setItems(this, 8);
+ if (this->pl->settings->accountGuestInCPUMeter) {
+ percent = v[0]+v[1]+v[2]+v[3]+v[4]+v[5]+v[6];
+ } else {
+ percent = v[0]+v[1]+v[2]+v[3]+v[4];
+ }
+ } else {
+ v[2] = cpuData->systemAllPeriod / total * 100.0;
+ v[3] = (cpuData->stealPeriod + cpuData->guestPeriod) / total * 100.0;
+ Meter_setItems(this, 4);
+ percent = v[0]+v[1]+v[2]+v[3];
+ }
+ percent = CLAMP(percent, 0.0, 100.0);
+ if (isnan(percent)) percent = 0.0;
+ return percent;
+}
+
+void Platform_setMemoryValues(Meter* this) {
+ ProcessList* pl = (ProcessList*) this->pl;
+ long int usedMem = pl->usedMem;
+ long int buffersMem = pl->buffersMem;
+ long int cachedMem = pl->cachedMem;
+ usedMem -= buffersMem + cachedMem;
+ this->total = pl->totalMem;
+ this->values[0] = usedMem;
+ this->values[1] = buffersMem;
+ this->values[2] = cachedMem;
+}
+
+void Platform_setSwapValues(Meter* this) {
+ ProcessList* pl = (ProcessList*) this->pl;
+ this->total = pl->totalSwap;
+ this->values[0] = pl->usedSwap;
+}
+
+char* Platform_getProcessEnv(pid_t pid) {
+ char procname[32+1];
+ snprintf(procname, 32, "/proc/%d/environ", pid);
+ FILE* fd = fopen(procname, "r");
+ char *env = NULL;
+ if (fd) {
+ size_t capacity = 4096, size = 0, bytes;
+ env = xMalloc(capacity);
+ while (env && (bytes = fread(env+size, 1, capacity-size, fd)) > 0) {
+ size += bytes;
+ capacity *= 2;
+ env = xRealloc(env, capacity);
+ }
+ fclose(fd);
+ if (size < 2 || env[size-1] || env[size-2]) {
+ if (size + 2 < capacity) {
+ env = xRealloc(env, capacity+2);
+ }
+ env[size] = 0;
+ env[size+1] = 0;
+ }
+ }
+ return env;
+}
diff --git a/linux/Platform.h b/linux/Platform.h
new file mode 100644
index 0000000..b0d69fb
--- /dev/null
+++ b/linux/Platform.h
@@ -0,0 +1,48 @@
+/* Do not edit this file. It was automatically generated. */
+
+#ifndef HEADER_Platform
+#define HEADER_Platform
+/*
+htop - linux/Platform.h
+(C) 2014 Hisham H. Muhammad
+Released under the GNU GPL, see the COPYING file
+in the source distribution for its full text.
+*/
+
+#include "Action.h"
+#include "MainPanel.h"
+#include "BatteryMeter.h"
+#include "LinuxProcess.h"
+#include "SignalsPanel.h"
+
+#ifndef CLAMP
+#define CLAMP(x,low,high) (((x)>(high))?(high):(((x)<(low))?(low):(x)))
+#endif
+
+extern ProcessField Platform_defaultFields[];
+
+extern int Platform_numberOfFields;
+
+extern SignalItem Platform_signals[];
+
+extern unsigned int Platform_numberOfSignals;
+
+void Platform_setBindings(Htop_Action* keys);
+
+extern MeterClass* Platform_meterTypes[];
+
+int Platform_getUptime();
+
+void Platform_getLoadAverage(double* one, double* five, double* fifteen);
+
+int Platform_getMaxPid();
+
+double Platform_setCPUValues(Meter* this, int cpu);
+
+void Platform_setMemoryValues(Meter* this);
+
+void Platform_setSwapValues(Meter* this);
+
+char* Platform_getProcessEnv(pid_t pid);
+
+#endif

© 2014-2024 Faster IT GmbH | imprint | privacy policy