2014-11-24 20:55:49 +00:00
|
|
|
/*
|
|
|
|
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.
|
|
|
|
*/
|
|
|
|
|
2014-11-24 21:22:50 +00:00
|
|
|
#include "Platform.h"
|
2014-11-24 20:55:49 +00:00
|
|
|
#include "IOPriority.h"
|
|
|
|
#include "IOPriorityPanel.h"
|
2014-11-24 21:22:50 +00:00
|
|
|
#include "LinuxProcess.h"
|
2015-01-22 01:27:31 +00:00
|
|
|
#include "LinuxProcessList.h"
|
2014-11-27 23:02:52 +00:00
|
|
|
#include "Battery.h"
|
|
|
|
|
2014-11-27 21:18:14 +00:00
|
|
|
#include "Meter.h"
|
|
|
|
#include "CPUMeter.h"
|
|
|
|
#include "MemoryMeter.h"
|
|
|
|
#include "SwapMeter.h"
|
|
|
|
#include "TasksMeter.h"
|
|
|
|
#include "LoadAverageMeter.h"
|
|
|
|
#include "UptimeMeter.h"
|
2020-08-20 03:59:41 +00:00
|
|
|
#include "PressureStallMeter.h"
|
2014-11-27 21:18:14 +00:00
|
|
|
#include "ClockMeter.h"
|
|
|
|
#include "HostnameMeter.h"
|
2019-07-07 02:37:02 +00:00
|
|
|
#include "zfs/ZfsArcMeter.h"
|
2019-09-03 18:26:02 +00:00
|
|
|
#include "zfs/ZfsCompressedArcMeter.h"
|
2015-03-16 04:43:04 +00:00
|
|
|
#include "LinuxProcess.h"
|
2014-11-24 21:22:50 +00:00
|
|
|
|
2014-11-27 21:41:14 +00:00
|
|
|
#include <math.h>
|
2014-11-27 21:57:24 +00:00
|
|
|
#include <assert.h>
|
2015-12-03 21:16:10 +00:00
|
|
|
#include <limits.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
2014-11-27 21:41:14 +00:00
|
|
|
|
2014-11-24 20:55:49 +00:00
|
|
|
|
Avoid conversion warning
linux/Platform.c:47:90: error: implicit conversion from ‘enum LinuxProcessFields’ to ‘enum ProcessFields’ [-Werror=enum-conversion]
47 | ProcessField Platform_defaultFields[] = { PID, USER, PRIORITY, NICE, M_SIZE, M_RESIDENT, M_SHARE, STATE, PERCENT_CPU, PERCENT_MEM, TIME, COMM, 0 };
|
2020-08-20 19:26:10 +00:00
|
|
|
ProcessField Platform_defaultFields[] = { PID, USER, PRIORITY, NICE, M_SIZE, M_RESIDENT, (int)M_SHARE, STATE, PERCENT_CPU, PERCENT_MEM, TIME, COMM, 0 };
|
2015-03-16 04:43:04 +00:00
|
|
|
|
|
|
|
//static ProcessField defaultIoFields[] = { PID, IO_PRIORITY, USER, IO_READ_RATE, IO_WRITE_RATE, IO_RATE, COMM, 0 };
|
|
|
|
|
2015-03-15 23:29:13 +00:00
|
|
|
int Platform_numberOfFields = LAST_PROCESSFIELD;
|
|
|
|
|
2016-08-30 12:41:17 +00:00
|
|
|
const SignalItem Platform_signals[] = {
|
2015-10-06 06:02:49 +00:00
|
|
|
{ .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 },
|
|
|
|
};
|
|
|
|
|
2016-08-30 12:41:17 +00:00
|
|
|
const unsigned int Platform_numberOfSignals = sizeof(Platform_signals)/sizeof(SignalItem);
|
2015-10-06 06:02:49 +00:00
|
|
|
|
2015-01-22 01:27:31 +00:00
|
|
|
static Htop_Reaction Platform_actionSetIOPriority(State* st) {
|
|
|
|
Panel* panel = st->panel;
|
|
|
|
|
2014-11-24 21:22:50 +00:00
|
|
|
LinuxProcess* p = (LinuxProcess*) Panel_getSelected(panel);
|
2014-11-24 20:55:49 +00:00
|
|
|
if (!p) return HTOP_OK;
|
2020-08-25 10:01:54 +00:00
|
|
|
IOPriority ioprio1 = p->ioPriority;
|
|
|
|
Panel* ioprioPanel = IOPriorityPanel_new(ioprio1);
|
2018-11-11 05:48:08 +00:00
|
|
|
void* set = Action_pickFromVector(st, ioprioPanel, 21, true);
|
2014-11-24 20:55:49 +00:00
|
|
|
if (set) {
|
2020-08-25 10:01:54 +00:00
|
|
|
IOPriority ioprio2 = IOPriorityPanel_getIOPriority(ioprioPanel);
|
|
|
|
bool ok = MainPanel_foreachProcess((MainPanel*)panel, (MainPanel_ForeachProcessFn) LinuxProcess_setIOPriority, (Arg){ .i = ioprio2 }, NULL);
|
2014-11-24 20:55:49 +00:00
|
|
|
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;
|
|
|
|
}
|
2014-11-27 21:18:14 +00:00
|
|
|
|
|
|
|
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,
|
2020-06-11 21:21:52 +00:00
|
|
|
&AllCPUs4Meter_class,
|
2014-11-27 21:18:14 +00:00
|
|
|
&LeftCPUsMeter_class,
|
|
|
|
&RightCPUsMeter_class,
|
|
|
|
&LeftCPUs2Meter_class,
|
|
|
|
&RightCPUs2Meter_class,
|
2020-06-11 21:21:52 +00:00
|
|
|
&LeftCPUs4Meter_class,
|
|
|
|
&RightCPUs4Meter_class,
|
2014-11-27 21:18:14 +00:00
|
|
|
&BlankMeter_class,
|
2020-08-20 03:59:41 +00:00
|
|
|
&PressureStallCPUSomeMeter_class,
|
|
|
|
&PressureStallIOSomeMeter_class,
|
|
|
|
&PressureStallIOFullMeter_class,
|
|
|
|
&PressureStallMemorySomeMeter_class,
|
|
|
|
&PressureStallMemoryFullMeter_class,
|
2019-07-07 02:37:02 +00:00
|
|
|
&ZfsArcMeter_class,
|
2019-09-03 18:26:02 +00:00
|
|
|
&ZfsCompressedArcMeter_class,
|
2014-11-27 21:18:14 +00:00
|
|
|
NULL
|
|
|
|
};
|
|
|
|
|
2014-11-27 21:41:14 +00:00
|
|
|
int Platform_getUptime() {
|
|
|
|
double uptime = 0;
|
|
|
|
FILE* fd = fopen(PROCDIR "/uptime", "r");
|
|
|
|
if (fd) {
|
2015-02-23 06:34:06 +00:00
|
|
|
int n = fscanf(fd, "%64lf", &uptime);
|
2014-11-27 21:41:14 +00:00
|
|
|
fclose(fd);
|
2015-02-23 06:34:06 +00:00
|
|
|
if (n <= 0) return 0;
|
2014-11-27 21:41:14 +00:00
|
|
|
}
|
2020-09-23 11:52:49 +00:00
|
|
|
return floor(uptime);
|
2014-11-27 21:41:14 +00:00
|
|
|
}
|
2014-11-27 21:57:24 +00:00
|
|
|
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-11-27 22:10:23 +00:00
|
|
|
int Platform_getMaxPid() {
|
|
|
|
FILE* file = fopen(PROCDIR "/sys/kernel/pid_max", "r");
|
|
|
|
if (!file) return -1;
|
|
|
|
int maxPid = 4194303;
|
2015-05-15 09:33:25 +00:00
|
|
|
int match = fscanf(file, "%32d", &maxPid);
|
|
|
|
(void) match;
|
2014-11-27 22:10:23 +00:00
|
|
|
fclose(file);
|
|
|
|
return maxPid;
|
|
|
|
}
|
|
|
|
|
2015-01-22 01:27:31 +00:00
|
|
|
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;
|
2015-10-23 15:46:21 +00:00
|
|
|
v[CPU_METER_NICE] = cpuData->nicePeriod / total * 100.0;
|
|
|
|
v[CPU_METER_NORMAL] = cpuData->userPeriod / total * 100.0;
|
2015-01-22 01:27:31 +00:00
|
|
|
if (this->pl->settings->detailedCPUTime) {
|
2015-10-23 15:46:21 +00:00
|
|
|
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;
|
2015-01-22 01:27:31 +00:00
|
|
|
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];
|
2015-10-06 12:04:22 +00:00
|
|
|
}
|
2015-01-22 01:27:31 +00:00
|
|
|
} 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];
|
|
|
|
}
|
Introduce CLAMP macro. Unify all MIN(MAX(a,b),c) uses.
With the CLAMP macro replacing the combination of MIN and MAX, we will
have at least two advantages:
1. It's more obvious semantically.
2. There are no more mixes of confusing uses like MIN(MAX(a,b),c) and
MAX(MIN(a,b),c) and MIN(a,MAX(b,c)) appearing everywhere. We unify
the 'clamping' with a single macro.
Note that the behavior of this CLAMP macro is different from
the combination `MAX(low,MIN(x,high))`.
* This CLAMP macro expands to two comparisons instead of three from
MAX and MIN combination. In theory, this makes the code slightly
smaller, in case that (low) or (high) or both are computed at
runtime, so that compilers cannot optimize them. (The third
comparison will matter if (low)>(high); see below.)
* CLAMP has a side effect, that if (low)>(high) it will produce weird
results. Unlike MIN & MAX which will force either (low) or (high) to
win. No assertion of ((low)<=(high)) is done in this macro, for now.
This CLAMP macro is implemented like described in glib
<http://developer.gnome.org/glib/stable/glib-Standard-Macros.html>
and does not handle weird uses like CLAMP(a++, low++, high--) .
2016-01-15 12:26:01 +00:00
|
|
|
percent = CLAMP(percent, 0.0, 100.0);
|
2015-01-22 01:27:31 +00:00
|
|
|
if (isnan(percent)) percent = 0.0;
|
2019-08-10 04:34:48 +00:00
|
|
|
|
2019-08-11 05:19:32 +00:00
|
|
|
v[CPU_METER_FREQUENCY] = cpuData->frequency;
|
2019-08-10 04:34:48 +00:00
|
|
|
|
2015-01-22 01:27:31 +00:00
|
|
|
return percent;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Platform_setMemoryValues(Meter* this) {
|
2015-03-16 06:25:43 +00:00
|
|
|
ProcessList* pl = (ProcessList*) this->pl;
|
2020-09-24 18:13:25 +00:00
|
|
|
LinuxProcessList* lpl = (LinuxProcessList*) this->pl;
|
|
|
|
|
2015-01-22 01:27:31 +00:00
|
|
|
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;
|
2020-09-24 18:13:25 +00:00
|
|
|
|
|
|
|
if (lpl->zfs.enabled != 0) {
|
|
|
|
this->values[0] -= lpl->zfs.size;
|
|
|
|
this->values[2] += lpl->zfs.size;
|
|
|
|
}
|
2015-01-22 01:27:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void Platform_setSwapValues(Meter* this) {
|
2015-03-16 06:25:43 +00:00
|
|
|
ProcessList* pl = (ProcessList*) this->pl;
|
2015-01-22 01:27:31 +00:00
|
|
|
this->total = pl->totalSwap;
|
|
|
|
this->values[0] = pl->usedSwap;
|
|
|
|
}
|
2015-12-03 21:16:10 +00:00
|
|
|
|
2019-07-07 02:37:02 +00:00
|
|
|
void Platform_setZfsArcValues(Meter* this) {
|
|
|
|
LinuxProcessList* lpl = (LinuxProcessList*) this->pl;
|
|
|
|
|
2019-09-03 18:21:33 +00:00
|
|
|
ZfsArcMeter_readStats(this, &(lpl->zfs));
|
2019-07-07 02:37:02 +00:00
|
|
|
}
|
|
|
|
|
2019-09-03 18:26:02 +00:00
|
|
|
void Platform_setZfsCompressedArcValues(Meter* this) {
|
|
|
|
LinuxProcessList* lpl = (LinuxProcessList*) this->pl;
|
|
|
|
|
|
|
|
ZfsCompressedArcMeter_readStats(this, &(lpl->zfs));
|
|
|
|
}
|
2015-12-03 21:16:10 +00:00
|
|
|
char* Platform_getProcessEnv(pid_t pid) {
|
2020-09-15 10:29:46 +00:00
|
|
|
char procname[128];
|
|
|
|
xSnprintf(procname, sizeof(procname), PROCDIR "/%d/environ", pid);
|
2015-12-03 21:16:10 +00:00
|
|
|
FILE* fd = fopen(procname, "r");
|
|
|
|
char *env = NULL;
|
|
|
|
if (fd) {
|
|
|
|
size_t capacity = 4096, size = 0, bytes;
|
2016-02-02 14:53:02 +00:00
|
|
|
env = xMalloc(capacity);
|
2020-09-12 16:14:39 +00:00
|
|
|
while ((bytes = fread(env+size, 1, capacity-size, fd)) > 0) {
|
2015-12-03 21:16:10 +00:00
|
|
|
size += bytes;
|
|
|
|
capacity *= 2;
|
2016-02-02 14:53:02 +00:00
|
|
|
env = xRealloc(env, capacity);
|
2015-12-03 21:16:10 +00:00
|
|
|
}
|
|
|
|
fclose(fd);
|
|
|
|
if (size < 2 || env[size-1] || env[size-2]) {
|
|
|
|
if (size + 2 < capacity) {
|
2016-02-02 14:53:02 +00:00
|
|
|
env = xRealloc(env, capacity+2);
|
2015-12-03 21:16:10 +00:00
|
|
|
}
|
|
|
|
env[size] = 0;
|
|
|
|
env[size+1] = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return env;
|
|
|
|
}
|
2020-08-20 03:59:41 +00:00
|
|
|
|
|
|
|
void Platform_getPressureStall(const char *file, bool some, double* ten, double* sixty, double* threehundred) {
|
|
|
|
*ten = *sixty = *threehundred = 0;
|
|
|
|
char procname[128+1];
|
|
|
|
xSnprintf(procname, 128, PROCDIR "/pressure/%s", file);
|
|
|
|
FILE *fd = fopen(procname, "r");
|
|
|
|
if (!fd) {
|
|
|
|
*ten = *sixty = *threehundred = NAN;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
int total = fscanf(fd, "some avg10=%32lf avg60=%32lf avg300=%32lf total=%*f ", ten, sixty, threehundred);
|
|
|
|
if (!some) {
|
|
|
|
total = fscanf(fd, "full avg10=%32lf avg60=%32lf avg300=%32lf total=%*f ", ten, sixty, threehundred);
|
|
|
|
}
|
|
|
|
(void) total;
|
|
|
|
assert(total == 3);
|
|
|
|
fclose(fd);
|
|
|
|
}
|