-
Notifications
You must be signed in to change notification settings - Fork 0
/
manager.cpp
329 lines (271 loc) · 7.31 KB
/
manager.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
/*
* Copyright (C) 2015 Mountassir El Hafi, ([email protected])
*
* Manager.cpp: Part of gmonitor
*
* gmonitor is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 3 as
* published by the Free Software Foundation.
*
* gmonitor is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
* You should have received a copy of the GNU General Public License
* along with gmonitor. If not, see <http://www.gnu.org/licenses/>.
*/
#include <cstdlib>
#include <unistd.h>
#include <stdio.h>
#include "manager.h"
//get all available gpus on the system
void Manager::buildGpus()
{
vector<string> gpuList;
statsReader.getGpuList(&gpuList);
for(size_t i = 0, l = gpuList.size(); i < l; ++i)
{
string name, id;
decomposeGpuIdentifier(gpuList[i], &id, &name);
allGpus.push_back(Gpu (id, name));
}
}
//update gpu states manually
void Manager::updateGpuStates(Gpu *gpu, const GpuStates &states)
{
gpu->setTotalMemory(states.totalMemory);
gpu->setUsedMemory(states.memoryUsage);
gpu->setGpuUsage(states.gpuUsage);
gpu->setMemoryBandwidth(states.memoryBandwidth);
gpu->setPciBandwidth(states.pciBandwidth);
gpu->setTemperature(states.coreTemp);
}
//get gpu identifier and name from Nvidia output;
//[0] mountassir-workstation:0[gpu:0] (GeForce GTX 770)
//to gpuId = [gpu:0] and gpuName = GeForce GTX 770
void Manager::decomposeGpuIdentifier(const string &gpuIdentifier,
string *gpuId,
string *gpuName)
{
size_t idStart = gpuIdentifier.find_last_of("[");
size_t idEnd = gpuIdentifier.find_last_of("]");
*gpuId = gpuIdentifier.substr(idStart, (idEnd - idStart) + 1);
size_t nameStart = gpuIdentifier.find_last_of("(");
size_t nameEnd = gpuIdentifier.find_last_of(")");
*gpuName = gpuIdentifier.substr(nameStart + 1, nameEnd - nameStart -1);
}
//generate random gpu state
//(used for testing when no gpu available)
void Manager::generateState(GpuStates *state)
{
state->gpuUsage = rand() % 100 + 1;
state->memoryUsage = rand() % 100 + 1;
state->pciBandwidth = rand() % 100 + 1;
state->memoryBandwidth = rand() % 100 + 1;
state->coreTemp = rand() % 100 + 1;
}
//print all available gpus
void Manager::dumpAllGpus()
{
for(size_t i = 0, l = allGpus.size(); i < l; ++i)
{
cout << allGpus[i];
}
}
//probe Nvidia drivers for current gpu states
void Manager::refreshGpus()
{
for(size_t i = 0, l = allGpus.size(); i < l; ++i)
{
GpuStates states;
statsReader.getGpuStates(&states, allGpus[i].getGpuId());
allGpus[i].updateAllStates(states);
}
}
//start monitoring the given gpus
void Manager::startMonitoring(const vector<int> &gpusToMonitor)
{
buildGpus();
if(noGpusToMonitor())
{
return;
}
if(gpusToMonitor.size() == 0)
{
if(displayMode == CURRENT_THEN_HISTORY)
{
monitorCurrentThenPreviousStates();
}
else
{
monitorCurrentAndPreviousStates();
}
}
else
{
if(displayMode == CURRENT_THEN_HISTORY)
{
monitorCurrentThenPreviousStates(gpusToMonitor);
}
else
{
monitorCurrentAndPreviousStates(gpusToMonitor);
}
}
}
//monitor and display; current state only,
//history only or both for each gpu
void Manager::monitorCurrentAndPreviousStates()
{
if(noGpusToMonitor())
{
return;
}
while(true)
{
refreshGpus();
consoleWriter.clearConsole();
for(size_t i = 0; i < allGpus.size(); ++i)
{
consoleWriter.printGpuIdentifier(allGpus[i].getGpuId(),
allGpus[i].getGpuName(),
allGpus[i].getTotalMemory());
if( (displayMode == CURRENT_STATE_ONLY) ||
(displayMode == CURRENT_NEXT_TO_HISTORY))
{
printCurrentGpuState(allGpus[i]);
}
if( (displayMode == HISTORY_STATES_ONLY) ||
(displayMode == CURRENT_NEXT_TO_HISTORY))
{
printHistoryGpuState(allGpus[i]);
}
}
sleep(refreshRate);
}
}
//monitor and display; current state for all gpus
//then all of their previous states
void Manager::monitorCurrentThenPreviousStates()
{
if(noGpusToMonitor())
{
return;
}
while(true)
{
refreshGpus();
consoleWriter.clearConsole();
for(size_t i = 0; i < allGpus.size(); ++i)
{
consoleWriter.printGpuIdentifier(allGpus[i].getGpuId(),
allGpus[i].getGpuName(),
allGpus[i].getTotalMemory());
printCurrentGpuState(allGpus[i]);
}
for(size_t i = 0; i < allGpus.size(); ++i)
{
consoleWriter.printGpuIdentifier(allGpus[i].getGpuId(),
allGpus[i].getGpuName(),
allGpus[i].getTotalMemory());
printHistoryGpuState(allGpus[i]);
}
sleep(refreshRate);
}
}
//monitor and display; current state only,
//history only or both for the specified gpus only
void Manager::monitorCurrentAndPreviousStates(const vector<int> &gpusToMonitor)
{
if(noGpusToMonitor())
{
return;
}
while(true)
{
refreshGpus();
consoleWriter.clearConsole();
for(size_t i = 0; i < gpusToMonitor.size(); ++i)
{
size_t gpuIndex = gpusToMonitor[i];
if( (gpuIndex >= 0) && (gpuIndex < allGpus.size()) )
{
consoleWriter.printGpuIdentifier(allGpus[gpuIndex].getGpuId(),
allGpus[gpuIndex].getGpuName(),
allGpus[gpuIndex].getTotalMemory());
if( (displayMode == CURRENT_STATE_ONLY) ||
(displayMode == CURRENT_NEXT_TO_HISTORY))
{
printCurrentGpuState(allGpus[gpuIndex]);
}
if( (displayMode == HISTORY_STATES_ONLY) ||
(displayMode == CURRENT_NEXT_TO_HISTORY))
{
printHistoryGpuState(allGpus[gpuIndex]);
}
}
}
sleep(refreshRate);
}
}
//monitor and display; current state for the specified gpus only
//then all of their previous states
void Manager::monitorCurrentThenPreviousStates(const vector<int> &gpusToMonitor)
{
if(noGpusToMonitor())
{
return;
}
while(true)
{
refreshGpus();
consoleWriter.clearConsole();
for(size_t i = 0; i < gpusToMonitor.size(); ++i)
{
size_t gpuIndex = gpusToMonitor[i];
if( (gpuIndex >= 0) && (gpuIndex < allGpus.size()) )
{
consoleWriter.printGpuIdentifier(allGpus[gpuIndex].getGpuId(),
allGpus[gpuIndex].getGpuName(),
allGpus[gpuIndex].getTotalMemory());
printCurrentGpuState(allGpus[gpuIndex]);
}
}
for(size_t i = 0; i < gpusToMonitor.size(); ++i)
{
size_t gpuIndex = gpusToMonitor[i];
if( (gpuIndex >= 0) && (gpuIndex < allGpus.size()) )
{
consoleWriter.printGpuIdentifier(allGpus[gpuIndex].getGpuId(),
allGpus[gpuIndex].getGpuName(),
allGpus[gpuIndex].getTotalMemory());
printHistoryGpuState(allGpus[gpuIndex]);
}
}
sleep(refreshRate);
}
}
//print the most recent state this gpu was at
void Manager::printCurrentGpuState(const Gpu &gpu)
{
GpuStates state;
gpu.exportCurrentState(&state);
consoleWriter.printCurrentGpuState(state);
}
//print all previous gpu states
void Manager::printHistoryGpuState(const Gpu &gpu)
{
vector<GpuStates> states;
gpu.exportAllStates(&states);
consoleWriter.printHistoryStates(states);
}
//check if their is no gpu to monitor
bool Manager::noGpusToMonitor()
{
if(allGpus.size() == 0)
{
cout << "No gpus to monitor, terminating." << endl;
return true;
}
return false;
}