Platform.c 4.82 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"
Hisham Muhammad's avatar
Hisham Muhammad committed
25

26
#include <math.h>
27
#include <assert.h>
28

Hisham Muhammad's avatar
Hisham Muhammad committed
29
30
/*{
#include "Action.h"
31
#include "MainPanel.h"
32
#include "BatteryMeter.h"
33
#include "LinuxProcess.h"
Hisham Muhammad's avatar
Hisham Muhammad committed
34
}*/
35

36
37
38
39
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 };

40
41
int Platform_numberOfFields = LAST_PROCESSFIELD;

Hisham Muhammad's avatar
Hisham Muhammad committed
42
43
44
static Htop_Reaction Platform_actionSetIOPriority(State* st) {
   Panel* panel = st->panel;

Hisham Muhammad's avatar
Hisham Muhammad committed
45
   LinuxProcess* p = (LinuxProcess*) Panel_getSelected(panel);
46
47
48
   if (!p) return HTOP_OK;
   IOPriority ioprio = p->ioPriority;
   Panel* ioprioPanel = IOPriorityPanel_new(ioprio);
49
   void* set = Action_pickFromVector(st, ioprioPanel, 21);
50
51
   if (set) {
      IOPriority ioprio = IOPriorityPanel_getIOPriority(ioprioPanel);
52
      bool ok = MainPanel_foreachProcess((MainPanel*)panel, (MainPanel_ForeachProcessFn) LinuxProcess_setIOPriority, (size_t) ioprio, NULL);
53
54
55
56
57
58
59
60
61
62
      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;
}
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84

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

85
86
87
88
int Platform_getUptime() {
   double uptime = 0;
   FILE* fd = fopen(PROCDIR "/uptime", "r");
   if (fd) {
89
      int n = fscanf(fd, "%64lf", &uptime);
90
      fclose(fd);
91
      if (n <= 0) return 0;
92
   }
Hisham Muhammad's avatar
Hisham Muhammad committed
93
   return (int) floor(uptime);
94
}
95
96
97
98
99
100
101
102
103
104
105
106
107
108

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

109
110
111
112
int Platform_getMaxPid() {
   FILE* file = fopen(PROCDIR "/sys/kernel/pid_max", "r");
   if (!file) return -1;
   int maxPid = 4194303;
113
   (void) fscanf(file, "%32d", &maxPid);
114
115
116
117
   fclose(file);
   return maxPid;
}

Hisham Muhammad's avatar
Hisham Muhammad committed
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
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[0] = cpuData->nicePeriod / total * 100.0;
   v[1] = cpuData->userPeriod / total * 100.0;
   if (this->pl->settings->detailedCPUTime) {
      v[2] = cpuData->systemPeriod / total * 100.0;
      v[3] = cpuData->irqPeriod / total * 100.0;
      v[4] = cpuData->softIrqPeriod / total * 100.0;
      v[5] = cpuData->stealPeriod / total * 100.0;
      v[6] = cpuData->guestPeriod / total * 100.0;
      v[7] = 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 = MIN(100.0, MAX(0.0, percent));      
   if (isnan(percent)) percent = 0.0;
   return percent;
}

void Platform_setMemoryValues(Meter* this) {
Hisham Muhammad's avatar
Hisham Muhammad committed
151
   ProcessList* pl = (ProcessList*) this->pl;
Hisham Muhammad's avatar
Hisham Muhammad committed
152
153
154
155
156
157
158
159
160
161
162
   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
163
   ProcessList* pl = (ProcessList*) this->pl;
Hisham Muhammad's avatar
Hisham Muhammad committed
164
165
166
   this->total = pl->totalSwap;
   this->values[0] = pl->usedSwap;
}