Platform.c 8.02 KB
Newer Older
1
2
3
4
5
6
7
/*
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.
*/

Hisham Muhammad's avatar
Hisham Muhammad committed
8
#include "Platform.h"
9
10
#include "IOPriority.h"
#include "IOPriorityPanel.h"
Hisham Muhammad's avatar
Hisham Muhammad committed
11
#include "LinuxProcess.h"
Hisham Muhammad's avatar
Hisham Muhammad committed
12
#include "LinuxProcessList.h"
13
14
#include "Battery.h"

15
16
17
18
19
20
21
22
23
#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"
24
#include "LinuxProcess.h"
25
#include "CRT.h"
Hisham Muhammad's avatar
Hisham Muhammad committed
26

27
#include <math.h>
28
#include <assert.h>
Michael Klein's avatar
Michael Klein committed
29
30
31
#include <limits.h>
#include <stdio.h>
#include <stdlib.h>
32

Hisham Muhammad's avatar
Hisham Muhammad committed
33
34
/*{
#include "Action.h"
35
#include "MainPanel.h"
36
#include "BatteryMeter.h"
37
#include "LinuxProcess.h"
38
#include "SignalsPanel.h"
39
#include "Settings.h"
Hisham Muhammad's avatar
Hisham Muhammad committed
40
}*/
41

42
43
44
45
#ifndef CLAMP
#define CLAMP(x,low,high) (((x)>(high))?(high):(((x)<(low))?(low):(x)))
#endif

46
47
int Platform_numberOfFields = LAST_PROCESSFIELD;

Explorer09's avatar
Explorer09 committed
48
const SignalItem Platform_signals[] = {
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
   { .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 },
};

85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
ScreenDefaults Platform_defaultScreens[] = {
   {
      .name = "Default",
      .columns = "PID USER PRIORITY NICE M_SIZE M_RESIDENT M_SHARE STATE PERCENT_CPU PERCENT_MEM TIME Command",
      .sortKey = "PERCENT_CPU",
   },
   {
      .name = "I/O",
      .columns = "PID USER IO_PRIORITY IO_RATE IO_READ_RATE IO_WRITE_RATE PERCENT_SWAP_DELAY PERCENT_IO_DELAY Command",
      .sortKey = "IO_RATE",
   },
   {
      .name = "Perf Counters",
      .columns = "PID USER PERCENT_CPU PROCESSOR MCYCLE MINSTR IPC PERCENT_MISS PERCENT_BMISS Command",
      .sortKey = "MCYCLE",
   },
   {
      .name = "L1 Data Cache",
      .columns = "PID USER PERCENT_CPU L1DREADS L1DRMISSES L1DWRITES L1DWMISSES Command",
104
      .sortKey = "L1DREADS",
105
106
107
108
109
   },
};

const unsigned int Platform_numberOfDefaultScreens = sizeof(Platform_defaultScreens)/sizeof(ScreenDefaults);

Explorer09's avatar
Explorer09 committed
110
const unsigned int Platform_numberOfSignals = sizeof(Platform_signals)/sizeof(SignalItem);
111

Hisham Muhammad's avatar
Hisham Muhammad committed
112
113
114
static Htop_Reaction Platform_actionSetIOPriority(State* st) {
   Panel* panel = st->panel;

Hisham Muhammad's avatar
Hisham Muhammad committed
115
   LinuxProcess* p = (LinuxProcess*) Panel_getSelected(panel);
116
117
118
   if (!p) return HTOP_OK;
   IOPriority ioprio = p->ioPriority;
   Panel* ioprioPanel = IOPriorityPanel_new(ioprio);
119
   void* set = Action_pickFromVector(st, ioprioPanel, 21);
120
121
   if (set) {
      IOPriority ioprio = IOPriorityPanel_getIOPriority(ioprioPanel);
122
      bool ok = MainPanel_foreachProcess((MainPanel*)panel, (MainPanel_ForeachProcessFn) LinuxProcess_setIOPriority, (Arg){ .i = ioprio }, NULL);
123
124
125
126
127
128
129
130
131
132
      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;
}
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154

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
};

155
156
157
158
int Platform_getUptime() {
   double uptime = 0;
   FILE* fd = fopen(PROCDIR "/uptime", "r");
   if (fd) {
159
      int n = fscanf(fd, "%64lf", &uptime);
160
      fclose(fd);
161
      if (n <= 0) return 0;
162
   }
Hisham Muhammad's avatar
Hisham Muhammad committed
163
   return (int) floor(uptime);
164
}
165
166
167
168
169
170
171
172
173
174
175
176
177
178

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);
   }
}

179
180
181
182
int Platform_getMaxPid() {
   FILE* file = fopen(PROCDIR "/sys/kernel/pid_max", "r");
   if (!file) return -1;
   int maxPid = 4194303;
Christian Hesse's avatar
Christian Hesse committed
183
184
   int match = fscanf(file, "%32d", &maxPid);
   (void) match;
185
186
187
188
   fclose(file);
   return maxPid;
}

Hisham Muhammad's avatar
Hisham Muhammad committed
189
190
191
192
193
194
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;
195
196
   v[CPU_METER_NICE] = cpuData->nicePeriod / total * 100.0;
   v[CPU_METER_NORMAL] = cpuData->userPeriod / total * 100.0;
Hisham Muhammad's avatar
Hisham Muhammad committed
197
   if (this->pl->settings->detailedCPUTime) {
198
199
200
201
202
203
      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;
Hisham Muhammad's avatar
Hisham Muhammad committed
204
205
206
207
208
      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];
209
      }
Hisham Muhammad's avatar
Hisham Muhammad committed
210
211
212
213
214
215
   } 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];
   }
216
   percent = CLAMP(percent, 0.0, 100.0);
Hisham Muhammad's avatar
Hisham Muhammad committed
217
218
219
220
221
   if (isnan(percent)) percent = 0.0;
   return percent;
}

void Platform_setMemoryValues(Meter* this) {
Hisham Muhammad's avatar
Hisham Muhammad committed
222
   ProcessList* pl = (ProcessList*) this->pl;
Hisham Muhammad's avatar
Hisham Muhammad committed
223
224
225
226
227
228
229
230
231
232
233
   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) {
Hisham Muhammad's avatar
Hisham Muhammad committed
234
   ProcessList* pl = (ProcessList*) this->pl;
Hisham Muhammad's avatar
Hisham Muhammad committed
235
236
237
   this->total = pl->totalSwap;
   this->values[0] = pl->usedSwap;
}
Michael Klein's avatar
Michael Klein committed
238
239
240

char* Platform_getProcessEnv(pid_t pid) {
   char procname[32+1];
241
   xSnprintf(procname, 32, "/proc/%d/environ", pid);
Michael Klein's avatar
Michael Klein committed
242
243
244
245
   FILE* fd = fopen(procname, "r");
   char *env = NULL;
   if (fd) {
      size_t capacity = 4096, size = 0, bytes;
Hisham's avatar
Hisham committed
246
      env = xMalloc(capacity);
Michael Klein's avatar
Michael Klein committed
247
248
249
      while (env && (bytes = fread(env+size, 1, capacity-size, fd)) > 0) {
         size += bytes;
         capacity *= 2;
Hisham's avatar
Hisham committed
250
         env = xRealloc(env, capacity);
Michael Klein's avatar
Michael Klein committed
251
252
253
254
      }
      fclose(fd);
      if (size < 2 || env[size-1] || env[size-2]) {
         if (size + 2 < capacity) {
Hisham's avatar
Hisham committed
255
            env = xRealloc(env, capacity+2);
Michael Klein's avatar
Michael Klein committed
256
257
258
259
260
261
262
         }
         env[size] = 0;
         env[size+1] = 0;
      }
   }
   return env;
}