2014-11-24 20:55:49 +00:00
|
|
|
/*
|
|
|
|
htop - linux/Platform.c
|
|
|
|
(C) 2014 Hisham H. Muhammad
|
2020-10-05 07:51:32 +00:00
|
|
|
Released under the GNU GPLv2, see the COPYING file
|
2014-11-24 20:55:49 +00:00
|
|
|
in the source distribution for its full text.
|
|
|
|
*/
|
|
|
|
|
2020-09-19 11:55:23 +00:00
|
|
|
#include "config.h"
|
|
|
|
|
2014-11-24 21:22:50 +00:00
|
|
|
#include "Platform.h"
|
2020-09-19 11:55:23 +00:00
|
|
|
|
|
|
|
#include <assert.h>
|
|
|
|
#include <ctype.h>
|
2020-11-11 21:15:35 +00:00
|
|
|
#include <dirent.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <inttypes.h>
|
|
|
|
#include <limits.h>
|
2020-09-19 11:55:23 +00:00
|
|
|
#include <math.h>
|
|
|
|
#include <stdio.h>
|
2020-11-17 07:12:38 +00:00
|
|
|
#include <errno.h>
|
|
|
|
#include <unistd.h>
|
2020-09-19 11:55:23 +00:00
|
|
|
|
|
|
|
#include "BatteryMeter.h"
|
|
|
|
#include "ClockMeter.h"
|
2020-11-11 21:15:35 +00:00
|
|
|
#include "Compat.h"
|
2020-09-19 11:55:23 +00:00
|
|
|
#include "CPUMeter.h"
|
|
|
|
#include "DateMeter.h"
|
|
|
|
#include "DateTimeMeter.h"
|
|
|
|
#include "DiskIOMeter.h"
|
|
|
|
#include "HostnameMeter.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"
|
2020-09-19 11:55:23 +00:00
|
|
|
#include "LoadAverageMeter.h"
|
|
|
|
#include "Macros.h"
|
|
|
|
#include "MainPanel.h"
|
2014-11-27 21:18:14 +00:00
|
|
|
#include "Meter.h"
|
|
|
|
#include "MemoryMeter.h"
|
2020-09-19 11:55:23 +00:00
|
|
|
#include "NetworkIOMeter.h"
|
|
|
|
#include "Object.h"
|
|
|
|
#include "Panel.h"
|
|
|
|
#include "PressureStallMeter.h"
|
|
|
|
#include "ProcessList.h"
|
|
|
|
#include "ProvideCurses.h"
|
|
|
|
#include "SELinuxMeter.h"
|
|
|
|
#include "Settings.h"
|
2014-11-27 21:18:14 +00:00
|
|
|
#include "SwapMeter.h"
|
2020-10-07 13:42:13 +00:00
|
|
|
#include "SystemdMeter.h"
|
2014-11-27 21:18:14 +00:00
|
|
|
#include "TasksMeter.h"
|
|
|
|
#include "UptimeMeter.h"
|
2020-10-14 18:21:09 +00:00
|
|
|
#include "XUtils.h"
|
2020-09-22 11:54:15 +00:00
|
|
|
#include "ZramMeter.h"
|
2014-11-24 21:22:50 +00:00
|
|
|
|
2020-09-19 11:55:23 +00:00
|
|
|
#include "zfs/ZfsArcMeter.h"
|
|
|
|
#include "zfs/ZfsArcStats.h"
|
|
|
|
#include "zfs/ZfsCompressedArcMeter.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 },
|
|
|
|
};
|
|
|
|
|
2020-09-28 19:14:50 +00:00
|
|
|
const unsigned int Platform_numberOfSignals = ARRAYSIZE(Platform_signals);
|
2015-10-06 06:02:49 +00:00
|
|
|
|
2020-11-17 22:19:42 +00:00
|
|
|
static enum { BAT_PROC, BAT_SYS, BAT_ERR } Platform_Battery_method = BAT_PROC;
|
|
|
|
static time_t Platform_Battery_cacheTime;
|
|
|
|
static double Platform_Battery_cacheLevel = NAN;
|
|
|
|
static ACPresence Platform_Battery_cacheIsOnAC;
|
|
|
|
|
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);
|
2020-11-01 00:09:51 +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);
|
2020-11-01 00:09:51 +00:00
|
|
|
bool ok = MainPanel_foreachProcess((MainPanel*)panel, LinuxProcess_setIOPriority, (Arg) { .i = ioprio2 }, NULL);
|
|
|
|
if (!ok) {
|
2014-11-24 20:55:49 +00:00
|
|
|
beep();
|
2020-11-01 00:09:51 +00:00
|
|
|
}
|
2014-11-24 20:55:49 +00:00
|
|
|
}
|
|
|
|
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
|
|
|
|
2020-10-05 11:19:50 +00:00
|
|
|
const MeterClass* const Platform_meterTypes[] = {
|
2014-11-27 21:18:14 +00:00
|
|
|
&CPUMeter_class,
|
|
|
|
&ClockMeter_class,
|
2020-10-05 11:52:58 +00:00
|
|
|
&DateMeter_class,
|
|
|
|
&DateTimeMeter_class,
|
2014-11-27 21:18:14 +00:00
|
|
|
&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,
|
2020-09-24 19:56:40 +00:00
|
|
|
&AllCPUs8Meter_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,
|
2020-09-24 19:56:40 +00:00
|
|
|
&LeftCPUs8Meter_class,
|
|
|
|
&RightCPUs8Meter_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,
|
2020-09-22 11:54:15 +00:00
|
|
|
&ZramMeter_class,
|
2020-09-13 17:46:34 +00:00
|
|
|
&DiskIOMeter_class,
|
2020-10-08 14:34:54 +00:00
|
|
|
&NetworkIOMeter_class,
|
2020-10-07 15:18:02 +00:00
|
|
|
&SELinuxMeter_class,
|
2020-10-07 13:42:13 +00:00
|
|
|
&SystemdMeter_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);
|
2020-11-01 00:09:51 +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;
|
2020-10-31 22:28:02 +00:00
|
|
|
*one = 0;
|
|
|
|
*five = 0;
|
|
|
|
*fifteen = 0;
|
|
|
|
|
|
|
|
FILE* fd = fopen(PROCDIR "/loadavg", "r");
|
2014-11-27 21:57:24 +00:00
|
|
|
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");
|
2020-11-01 00:09:51 +00:00
|
|
|
if (!file)
|
|
|
|
return -1;
|
|
|
|
|
2014-11-27 22:10:23 +00:00
|
|
|
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) {
|
2020-10-21 19:25:50 +00:00
|
|
|
const LinuxProcessList* pl = (const LinuxProcessList*) this->pl;
|
|
|
|
const CPUData* cpuData = &(pl->cpus[cpu]);
|
2015-01-22 01:27:31 +00:00
|
|
|
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;
|
2020-10-04 15:55:08 +00:00
|
|
|
this->curItems = 8;
|
2015-01-22 01:27:31 +00:00
|
|
|
if (this->pl->settings->accountGuestInCPUMeter) {
|
2020-10-31 22:28:02 +00:00
|
|
|
percent = v[0] + v[1] + v[2] + v[3] + v[4] + v[5] + v[6];
|
2015-01-22 01:27:31 +00:00
|
|
|
} else {
|
2020-10-31 22:28:02 +00:00
|
|
|
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;
|
2020-10-04 15:55:08 +00:00
|
|
|
this->curItems = 4;
|
2020-10-31 22:28:02 +00:00
|
|
|
percent = v[0] + v[1] + v[2] + v[3];
|
2015-01-22 01:27:31 +00:00
|
|
|
}
|
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);
|
2020-11-01 00:09:51 +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
|
|
|
|
2020-09-10 17:56:33 +00:00
|
|
|
#ifdef HAVE_LIBSENSORS
|
|
|
|
v[CPU_METER_TEMPERATURE] = cpuData->temperature;
|
|
|
|
#else
|
|
|
|
v[CPU_METER_TEMPERATURE] = NAN;
|
|
|
|
#endif
|
|
|
|
|
2015-01-22 01:27:31 +00:00
|
|
|
return percent;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Platform_setMemoryValues(Meter* this) {
|
2020-10-21 19:25:50 +00:00
|
|
|
const ProcessList* pl = this->pl;
|
|
|
|
const LinuxProcessList* lpl = (const LinuxProcessList*) pl;
|
2020-09-24 18:13:25 +00:00
|
|
|
|
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) {
|
2020-10-21 19:25:50 +00:00
|
|
|
const ProcessList* pl = 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
|
|
|
|
2020-09-22 11:54:15 +00:00
|
|
|
void Platform_setZramValues(Meter* this) {
|
|
|
|
const LinuxProcessList* lpl = (const LinuxProcessList*) this->pl;
|
|
|
|
this->total = lpl->zram.totalZram;
|
|
|
|
this->values[0] = lpl->zram.usedZramComp;
|
|
|
|
this->values[1] = lpl->zram.usedZramOrig;
|
|
|
|
}
|
|
|
|
|
2019-07-07 02:37:02 +00:00
|
|
|
void Platform_setZfsArcValues(Meter* this) {
|
2020-10-21 19:25:50 +00:00
|
|
|
const LinuxProcessList* lpl = (const LinuxProcessList*) this->pl;
|
2019-07-07 02:37:02 +00:00
|
|
|
|
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) {
|
2020-10-21 19:25:50 +00:00
|
|
|
const LinuxProcessList* lpl = (const LinuxProcessList*) this->pl;
|
2019-09-03 18:26:02 +00:00
|
|
|
|
|
|
|
ZfsCompressedArcMeter_readStats(this, &(lpl->zfs));
|
|
|
|
}
|
2020-11-17 07:12:38 +00:00
|
|
|
|
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");
|
2020-10-31 19:52:20 +00:00
|
|
|
if (!fd)
|
2020-10-26 18:18:29 +00:00
|
|
|
return NULL;
|
|
|
|
|
2020-10-31 22:28:02 +00:00
|
|
|
char* env = NULL;
|
2020-10-26 18:18:29 +00:00
|
|
|
|
|
|
|
size_t capacity = 0;
|
|
|
|
size_t size = 0;
|
|
|
|
ssize_t bytes = 0;
|
|
|
|
|
|
|
|
do {
|
|
|
|
size += bytes;
|
|
|
|
capacity += 4096;
|
|
|
|
env = xRealloc(env, capacity);
|
|
|
|
} while ((bytes = fread(env + size, 1, capacity - size, fd)) > 0);
|
|
|
|
|
|
|
|
fclose(fd);
|
|
|
|
|
|
|
|
if (bytes < 0) {
|
|
|
|
free(env);
|
|
|
|
return NULL;
|
2015-12-03 21:16:10 +00:00
|
|
|
}
|
2020-10-26 18:18:29 +00:00
|
|
|
|
|
|
|
size += bytes;
|
|
|
|
|
|
|
|
env = xRealloc(env, size + 2);
|
|
|
|
|
|
|
|
env[size] = '\0';
|
2020-10-31 22:28:02 +00:00
|
|
|
env[size + 1] = '\0';
|
2020-10-26 18:18:29 +00:00
|
|
|
|
2015-12-03 21:16:10 +00:00
|
|
|
return env;
|
|
|
|
}
|
2020-08-20 03:59:41 +00:00
|
|
|
|
2020-11-11 21:15:35 +00:00
|
|
|
/*
|
|
|
|
* Return the absolute path of a file given its pid&inode number
|
|
|
|
*
|
|
|
|
* Based on implementation of lslocks from util-linux:
|
|
|
|
* https://sources.debian.org/src/util-linux/2.36-3/misc-utils/lslocks.c/#L162
|
|
|
|
*/
|
|
|
|
char* Platform_getInodeFilename(pid_t pid, ino_t inode) {
|
|
|
|
struct stat sb;
|
|
|
|
struct dirent *de;
|
|
|
|
DIR *dirp;
|
|
|
|
size_t len;
|
|
|
|
int fd;
|
|
|
|
|
|
|
|
char path[PATH_MAX];
|
|
|
|
char sym[PATH_MAX];
|
|
|
|
char* ret = NULL;
|
|
|
|
|
|
|
|
memset(path, 0, sizeof(path));
|
|
|
|
memset(sym, 0, sizeof(sym));
|
|
|
|
|
|
|
|
xSnprintf(path, sizeof(path), "%s/%d/fd/", PROCDIR, pid);
|
|
|
|
if (strlen(path) >= (sizeof(path) - 2))
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
if (!(dirp = opendir(path)))
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
if ((fd = dirfd(dirp)) < 0 )
|
|
|
|
goto out;
|
|
|
|
|
|
|
|
while ((de = readdir(dirp))) {
|
|
|
|
if (String_eq(de->d_name, ".") || String_eq(de->d_name, ".."))
|
|
|
|
continue;
|
|
|
|
|
|
|
|
/* care only for numerical descriptors */
|
|
|
|
if (!strtoull(de->d_name, (char **) NULL, 10))
|
|
|
|
continue;
|
|
|
|
|
|
|
|
if (!Compat_fstatat(fd, path, de->d_name, &sb, 0) && inode != sb.st_ino)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
if ((len = Compat_readlinkat(fd, path, de->d_name, sym, sizeof(sym) - 1)) < 1)
|
|
|
|
goto out;
|
|
|
|
|
|
|
|
sym[len] = '\0';
|
|
|
|
|
|
|
|
ret = xStrdup(sym);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
out:
|
|
|
|
closedir(dirp);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
FileLocks_ProcessData* Platform_getProcessLocks(pid_t pid) {
|
|
|
|
FileLocks_ProcessData* pdata = xCalloc(1, sizeof(FileLocks_ProcessData));
|
|
|
|
|
|
|
|
FILE* f = fopen(PROCDIR "/locks", "r");
|
|
|
|
if (!f) {
|
|
|
|
pdata->error = true;
|
|
|
|
return pdata;
|
|
|
|
}
|
|
|
|
|
|
|
|
char buffer[1024];
|
|
|
|
FileLocks_LockData** data_ref = &pdata->locks;
|
|
|
|
while(fgets(buffer, sizeof(buffer), f)) {
|
|
|
|
if (!strchr(buffer, '\n'))
|
|
|
|
continue;
|
|
|
|
|
|
|
|
int lock_id;
|
|
|
|
char lock_type[16];
|
|
|
|
char lock_excl[16];
|
|
|
|
char lock_rw[16];
|
|
|
|
pid_t lock_pid;
|
|
|
|
unsigned int lock_dev[2];
|
|
|
|
uint64_t lock_inode;
|
|
|
|
char lock_start[25];
|
|
|
|
char lock_end[25];
|
|
|
|
|
|
|
|
if (10 != sscanf(buffer, "%d: %15s %15s %15s %d %x:%x:%"PRIu64" %24s %24s",
|
|
|
|
&lock_id, lock_type, lock_excl, lock_rw, &lock_pid,
|
|
|
|
&lock_dev[0], &lock_dev[1], &lock_inode,
|
|
|
|
lock_start, lock_end))
|
|
|
|
continue;
|
|
|
|
|
|
|
|
if (pid != lock_pid)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
FileLocks_LockData* ldata = xCalloc(1, sizeof(FileLocks_LockData));
|
|
|
|
FileLocks_Data* data = &ldata->data;
|
|
|
|
data->id = lock_id;
|
2020-11-13 20:34:21 +00:00
|
|
|
data->locktype = xStrdup(lock_type);
|
|
|
|
data->exclusive = xStrdup(lock_excl);
|
|
|
|
data->readwrite = xStrdup(lock_rw);
|
|
|
|
data->filename = Platform_getInodeFilename(lock_pid, lock_inode);
|
2020-11-11 21:15:35 +00:00
|
|
|
data->dev[0] = lock_dev[0];
|
|
|
|
data->dev[1] = lock_dev[1];
|
|
|
|
data->inode = lock_inode;
|
|
|
|
data->start = strtoull(lock_start, NULL, 10);
|
|
|
|
if (!String_eq(lock_end, "EOF")) {
|
|
|
|
data->end = strtoull(lock_end, NULL, 10);
|
|
|
|
} else {
|
|
|
|
data->end = ULLONG_MAX;
|
|
|
|
}
|
|
|
|
|
|
|
|
*data_ref = ldata;
|
|
|
|
data_ref = &ldata->next;
|
|
|
|
}
|
|
|
|
|
|
|
|
fclose(f);
|
|
|
|
return pdata;
|
|
|
|
}
|
|
|
|
|
2020-10-31 22:28:02 +00:00
|
|
|
void Platform_getPressureStall(const char* file, bool some, double* ten, double* sixty, double* threehundred) {
|
2020-08-20 03:59:41 +00:00
|
|
|
*ten = *sixty = *threehundred = 0;
|
2020-10-31 22:28:02 +00:00
|
|
|
char procname[128 + 1];
|
2020-08-20 03:59:41 +00:00
|
|
|
xSnprintf(procname, 128, PROCDIR "/pressure/%s", file);
|
2020-10-31 22:28:02 +00:00
|
|
|
FILE* fd = fopen(procname, "r");
|
2020-08-20 03:59:41 +00:00
|
|
|
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);
|
|
|
|
}
|
2020-09-13 17:46:34 +00:00
|
|
|
|
2020-10-21 15:06:32 +00:00
|
|
|
bool Platform_getDiskIO(DiskIOData* data) {
|
2020-10-31 22:28:02 +00:00
|
|
|
FILE* fd = fopen(PROCDIR "/diskstats", "r");
|
2020-10-20 19:40:51 +00:00
|
|
|
if (!fd)
|
|
|
|
return false;
|
|
|
|
|
2020-09-13 17:46:34 +00:00
|
|
|
unsigned long int read_sum = 0, write_sum = 0, timeSpend_sum = 0;
|
|
|
|
char lineBuffer[256];
|
|
|
|
while (fgets(lineBuffer, sizeof(lineBuffer), fd)) {
|
|
|
|
char diskname[32];
|
|
|
|
unsigned long int read_tmp, write_tmp, timeSpend_tmp;
|
|
|
|
if (sscanf(lineBuffer, "%*d %*d %31s %*u %*u %lu %*u %*u %*u %lu %*u %*u %lu", diskname, &read_tmp, &write_tmp, &timeSpend_tmp) == 4) {
|
|
|
|
if (String_startsWith(diskname, "dm-"))
|
|
|
|
continue;
|
|
|
|
|
|
|
|
/* only count root disks, e.g. do not count IO from sda and sda1 twice */
|
|
|
|
if ((diskname[0] == 's' || diskname[0] == 'h')
|
|
|
|
&& diskname[1] == 'd'
|
|
|
|
&& isalpha((unsigned char)diskname[2])
|
|
|
|
&& isdigit((unsigned char)diskname[3]))
|
|
|
|
continue;
|
|
|
|
|
|
|
|
/* only count root disks, e.g. do not count IO from mmcblk0 and mmcblk0p1 twice */
|
|
|
|
if (diskname[0] == 'm'
|
|
|
|
&& diskname[1] == 'm'
|
|
|
|
&& diskname[2] == 'c'
|
|
|
|
&& diskname[3] == 'b'
|
|
|
|
&& diskname[4] == 'l'
|
|
|
|
&& diskname[5] == 'k'
|
|
|
|
&& isdigit((unsigned char)diskname[6])
|
|
|
|
&& diskname[7] == 'p')
|
|
|
|
continue;
|
|
|
|
|
|
|
|
read_sum += read_tmp;
|
|
|
|
write_sum += write_tmp;
|
|
|
|
timeSpend_sum += timeSpend_tmp;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
fclose(fd);
|
|
|
|
/* multiply with sector size */
|
2020-10-21 15:06:32 +00:00
|
|
|
data->totalBytesRead = 512 * read_sum;
|
|
|
|
data->totalBytesWritten = 512 * write_sum;
|
|
|
|
data->totalMsTimeSpend = timeSpend_sum;
|
2020-10-20 19:40:51 +00:00
|
|
|
return true;
|
2020-09-13 17:46:34 +00:00
|
|
|
}
|
2020-10-08 14:34:54 +00:00
|
|
|
|
2020-10-31 22:28:02 +00:00
|
|
|
bool Platform_getNetworkIO(unsigned long int* bytesReceived,
|
|
|
|
unsigned long int* packetsReceived,
|
|
|
|
unsigned long int* bytesTransmitted,
|
|
|
|
unsigned long int* packetsTransmitted) {
|
|
|
|
FILE* fd = fopen(PROCDIR "/net/dev", "r");
|
2020-10-20 19:40:51 +00:00
|
|
|
if (!fd)
|
|
|
|
return false;
|
2020-10-08 14:34:54 +00:00
|
|
|
|
|
|
|
unsigned long int bytesReceivedSum = 0, packetsReceivedSum = 0, bytesTransmittedSum = 0, packetsTransmittedSum = 0;
|
|
|
|
char lineBuffer[512];
|
|
|
|
while (fgets(lineBuffer, sizeof(lineBuffer), fd)) {
|
|
|
|
char interfaceName[32];
|
|
|
|
unsigned long int bytesReceivedParsed, packetsReceivedParsed, bytesTransmittedParsed, packetsTransmittedParsed;
|
2020-10-20 19:40:51 +00:00
|
|
|
if (sscanf(lineBuffer, "%31s %lu %lu %*u %*u %*u %*u %*u %*u %lu %lu",
|
|
|
|
interfaceName,
|
|
|
|
&bytesReceivedParsed,
|
|
|
|
&packetsReceivedParsed,
|
|
|
|
&bytesTransmittedParsed,
|
|
|
|
&packetsTransmittedParsed) != 5)
|
2020-10-08 14:34:54 +00:00
|
|
|
continue;
|
|
|
|
|
2020-10-03 19:20:43 +00:00
|
|
|
if (String_eq(interfaceName, "lo:"))
|
2020-10-08 14:34:54 +00:00
|
|
|
continue;
|
|
|
|
|
|
|
|
bytesReceivedSum += bytesReceivedParsed;
|
|
|
|
packetsReceivedSum += packetsReceivedParsed;
|
|
|
|
bytesTransmittedSum += bytesTransmittedParsed;
|
|
|
|
packetsTransmittedSum += packetsTransmittedParsed;
|
|
|
|
}
|
|
|
|
|
|
|
|
fclose(fd);
|
|
|
|
|
|
|
|
*bytesReceived = bytesReceivedSum;
|
|
|
|
*packetsReceived = packetsReceivedSum;
|
|
|
|
*bytesTransmitted = bytesTransmittedSum;
|
|
|
|
*packetsTransmitted = packetsTransmittedSum;
|
2020-10-20 19:40:51 +00:00
|
|
|
return true;
|
2020-10-08 14:34:54 +00:00
|
|
|
}
|
2020-11-17 07:12:38 +00:00
|
|
|
|
|
|
|
// Linux battery reading by Ian P. Hands (iphands@gmail.com, ihands@redhat.com).
|
|
|
|
|
|
|
|
#define MAX_BATTERIES 64
|
|
|
|
#define PROC_BATTERY_DIR PROCDIR "/acpi/battery"
|
|
|
|
#define PROC_POWERSUPPLY_DIR PROCDIR "/acpi/ac_adapter"
|
|
|
|
#define SYS_POWERSUPPLY_DIR "/sys/class/power_supply"
|
|
|
|
|
|
|
|
// ----------------------------------------
|
|
|
|
// READ FROM /proc
|
|
|
|
// ----------------------------------------
|
|
|
|
|
|
|
|
static unsigned long int parseBatInfo(const char* fileName, const unsigned short int lineNum, const unsigned short int wordNum) {
|
|
|
|
const char batteryPath[] = PROC_BATTERY_DIR;
|
|
|
|
DIR* batteryDir = opendir(batteryPath);
|
|
|
|
if (!batteryDir)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
char* batteries[MAX_BATTERIES];
|
|
|
|
unsigned int nBatteries = 0;
|
|
|
|
memset(batteries, 0, MAX_BATTERIES * sizeof(char*));
|
|
|
|
|
|
|
|
while (nBatteries < MAX_BATTERIES) {
|
|
|
|
struct dirent* dirEntry = readdir(batteryDir);
|
|
|
|
if (!dirEntry)
|
|
|
|
break;
|
|
|
|
|
|
|
|
char* entryName = dirEntry->d_name;
|
|
|
|
if (!String_startsWith(entryName, "BAT"))
|
|
|
|
continue;
|
|
|
|
|
|
|
|
batteries[nBatteries] = xStrdup(entryName);
|
|
|
|
nBatteries++;
|
|
|
|
}
|
|
|
|
closedir(batteryDir);
|
|
|
|
|
|
|
|
unsigned long int total = 0;
|
|
|
|
for (unsigned int i = 0; i < nBatteries; i++) {
|
|
|
|
char infoPath[30];
|
|
|
|
xSnprintf(infoPath, sizeof infoPath, "%s%s/%s", batteryPath, batteries[i], fileName);
|
|
|
|
|
|
|
|
FILE* file = fopen(infoPath, "r");
|
|
|
|
if (!file)
|
|
|
|
break;
|
|
|
|
|
|
|
|
char* line = NULL;
|
|
|
|
for (unsigned short int j = 0; j < lineNum; j++) {
|
|
|
|
free(line);
|
|
|
|
line = String_readLine(file);
|
|
|
|
if (!line)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
fclose(file);
|
|
|
|
|
|
|
|
if (!line)
|
|
|
|
break;
|
|
|
|
|
|
|
|
char* foundNumStr = String_getToken(line, wordNum);
|
|
|
|
const unsigned long int foundNum = atoi(foundNumStr);
|
|
|
|
free(foundNumStr);
|
|
|
|
free(line);
|
|
|
|
|
|
|
|
total += foundNum;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (unsigned int i = 0; i < nBatteries; i++)
|
|
|
|
free(batteries[i]);
|
|
|
|
|
|
|
|
return total;
|
|
|
|
}
|
|
|
|
|
|
|
|
static ACPresence procAcpiCheck(void) {
|
|
|
|
ACPresence isOn = AC_ERROR;
|
|
|
|
const char* power_supplyPath = PROC_POWERSUPPLY_DIR;
|
|
|
|
DIR* dir = opendir(power_supplyPath);
|
|
|
|
if (!dir)
|
|
|
|
return AC_ERROR;
|
|
|
|
|
|
|
|
for (;;) {
|
|
|
|
struct dirent* dirEntry = readdir(dir);
|
|
|
|
if (!dirEntry)
|
|
|
|
break;
|
|
|
|
|
|
|
|
const char* entryName = dirEntry->d_name;
|
|
|
|
|
|
|
|
if (entryName[0] != 'A')
|
|
|
|
continue;
|
|
|
|
|
|
|
|
char statePath[256];
|
|
|
|
xSnprintf(statePath, sizeof(statePath), "%s/%s/state", power_supplyPath, entryName);
|
|
|
|
FILE* file = fopen(statePath, "r");
|
|
|
|
if (!file) {
|
|
|
|
isOn = AC_ERROR;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
char* line = String_readLine(file);
|
|
|
|
|
|
|
|
fclose(file);
|
|
|
|
|
|
|
|
if (!line)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
char* isOnline = String_getToken(line, 2);
|
|
|
|
free(line);
|
|
|
|
|
|
|
|
if (String_eq(isOnline, "on-line"))
|
|
|
|
isOn = AC_PRESENT;
|
|
|
|
else
|
|
|
|
isOn = AC_ABSENT;
|
|
|
|
free(isOnline);
|
|
|
|
if (isOn == AC_PRESENT)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (dir)
|
|
|
|
closedir(dir);
|
|
|
|
|
|
|
|
return isOn;
|
|
|
|
}
|
|
|
|
|
|
|
|
static double Platform_Battery_getProcBatInfo(void) {
|
|
|
|
const unsigned long int totalFull = parseBatInfo("info", 3, 4);
|
|
|
|
if (totalFull == 0)
|
|
|
|
return NAN;
|
|
|
|
|
|
|
|
const unsigned long int totalRemain = parseBatInfo("state", 5, 3);
|
|
|
|
if (totalRemain == 0)
|
|
|
|
return NAN;
|
|
|
|
|
|
|
|
return totalRemain * 100.0 / (double) totalFull;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void Platform_Battery_getProcData(double* level, ACPresence* isOnAC) {
|
|
|
|
*isOnAC = procAcpiCheck();
|
|
|
|
*level = AC_ERROR != *isOnAC ? Platform_Battery_getProcBatInfo() : NAN;
|
|
|
|
}
|
|
|
|
|
|
|
|
// ----------------------------------------
|
|
|
|
// 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) {
|
|
|
|
if (errno == EINTR)
|
|
|
|
continue;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (res > 0) {
|
|
|
|
buf = ((char*)buf) + res;
|
|
|
|
count -= res;
|
|
|
|
alreadyRead += res;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (count == 0 || res == 0)
|
|
|
|
return alreadyRead;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void Platform_Battery_getSysData(double* level, ACPresence* isOnAC) {
|
|
|
|
|
|
|
|
*level = NAN;
|
|
|
|
*isOnAC = AC_ERROR;
|
|
|
|
|
|
|
|
DIR* dir = opendir(SYS_POWERSUPPLY_DIR);
|
|
|
|
if (!dir)
|
|
|
|
return;
|
|
|
|
|
|
|
|
unsigned long int totalFull = 0;
|
|
|
|
unsigned long int totalRemain = 0;
|
|
|
|
|
|
|
|
for (;;) {
|
|
|
|
struct dirent* dirEntry = readdir(dir);
|
|
|
|
if (!dirEntry)
|
|
|
|
break;
|
|
|
|
|
|
|
|
const char* entryName = dirEntry->d_name;
|
|
|
|
char filePath[256];
|
|
|
|
|
|
|
|
xSnprintf(filePath, sizeof filePath, SYS_POWERSUPPLY_DIR "/%s/type", entryName);
|
|
|
|
int fd1 = open(filePath, O_RDONLY);
|
|
|
|
if (fd1 == -1)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
char type[8];
|
|
|
|
ssize_t typelen = xread(fd1, type, 7);
|
|
|
|
close(fd1);
|
|
|
|
if (typelen < 1)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
if (type[0] == 'B' && type[1] == 'a' && type[2] == 't') {
|
|
|
|
xSnprintf(filePath, sizeof filePath, SYS_POWERSUPPLY_DIR "/%s/uevent", entryName);
|
|
|
|
int fd2 = open(filePath, O_RDONLY);
|
|
|
|
if (fd2 == -1) {
|
|
|
|
closedir(dir);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
char buffer[1024];
|
|
|
|
ssize_t buflen = xread(fd2, buffer, 1023);
|
|
|
|
close(fd2);
|
|
|
|
if (buflen < 1) {
|
|
|
|
closedir(dir);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
buffer[buflen] = '\0';
|
|
|
|
char* buf = buffer;
|
|
|
|
char* line = NULL;
|
|
|
|
bool full = false;
|
|
|
|
bool now = false;
|
|
|
|
int fullSize = 0;
|
|
|
|
double capacityLevel = NAN;
|
|
|
|
|
|
|
|
#define match(str,prefix) \
|
|
|
|
(String_startsWith(str,prefix) ? (str) + strlen(prefix) : NULL)
|
|
|
|
|
|
|
|
while ((line = strsep(&buf, "\n")) != NULL) {
|
|
|
|
const char* ps = match(line, "POWER_SUPPLY_");
|
|
|
|
if (!ps)
|
|
|
|
continue;
|
|
|
|
const char* capacity = match(ps, "CAPACITY=");
|
|
|
|
if (capacity)
|
|
|
|
capacityLevel = atoi(capacity) / 100.0;
|
|
|
|
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) {
|
|
|
|
fullSize = atoi(value);
|
|
|
|
totalFull += fullSize;
|
|
|
|
full = true;
|
|
|
|
if (now)
|
|
|
|
break;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
value = (!now) ? match(energy, "NOW=") : NULL;
|
|
|
|
if (value) {
|
|
|
|
totalRemain += atoi(value);
|
|
|
|
now = true;
|
|
|
|
if (full)
|
|
|
|
break;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#undef match
|
|
|
|
|
|
|
|
if (!now && full && !isnan(capacityLevel))
|
|
|
|
totalRemain += (capacityLevel * fullSize);
|
|
|
|
|
|
|
|
} else if (entryName[0] == 'A') {
|
|
|
|
if (*isOnAC != AC_ERROR)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
xSnprintf(filePath, sizeof filePath, SYS_POWERSUPPLY_DIR "/%s/online", entryName);
|
|
|
|
int fd3 = open(filePath, O_RDONLY);
|
|
|
|
if (fd3 == -1) {
|
|
|
|
closedir(dir);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
char buffer[2] = "";
|
|
|
|
for (;;) {
|
|
|
|
ssize_t res = read(fd3, buffer, 1);
|
|
|
|
if (res == -1 && errno == EINTR)
|
|
|
|
continue;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
close(fd3);
|
|
|
|
if (buffer[0] == '0')
|
|
|
|
*isOnAC = AC_ABSENT;
|
|
|
|
else if (buffer[0] == '1')
|
|
|
|
*isOnAC = AC_PRESENT;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
closedir(dir);
|
|
|
|
|
|
|
|
*level = totalFull > 0 ? ((double) totalRemain * 100.0) / (double) totalFull : NAN;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Platform_getBattery(double* level, ACPresence* isOnAC) {
|
|
|
|
time_t now = time(NULL);
|
|
|
|
// update battery reading is slow. Update it each 10 seconds only.
|
|
|
|
if (now < Platform_Battery_cacheTime + 10) {
|
|
|
|
*level = Platform_Battery_cacheLevel;
|
|
|
|
*isOnAC = Platform_Battery_cacheIsOnAC;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (Platform_Battery_method == BAT_PROC) {
|
|
|
|
Platform_Battery_getProcData(level, isOnAC);
|
|
|
|
if (isnan(*level))
|
|
|
|
Platform_Battery_method = BAT_SYS;
|
|
|
|
}
|
|
|
|
if (Platform_Battery_method == BAT_SYS) {
|
|
|
|
Platform_Battery_getSysData(level, isOnAC);
|
|
|
|
if (isnan(*level))
|
|
|
|
Platform_Battery_method = BAT_ERR;
|
|
|
|
}
|
|
|
|
if (Platform_Battery_method == BAT_ERR) {
|
|
|
|
*level = NAN;
|
|
|
|
*isOnAC = AC_ERROR;
|
|
|
|
} else {
|
|
|
|
*level = CLAMP(*level, 0.0, 100.0);
|
|
|
|
}
|
|
|
|
Platform_Battery_cacheLevel = *level;
|
|
|
|
Platform_Battery_cacheIsOnAC = *isOnAC;
|
|
|
|
Platform_Battery_cacheTime = now;
|
|
|
|
}
|