wattsup.cpp
13.3 KB
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
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
/*
* This class provides methods for interacting with wattsup meter.
*
* @file
* @author Muhammad Fah'd Azeemi <muhammad.fahad@ucdconnect.ie>
* @version 1.0
*/
/*--------------------------------------------------------*/
#include <sys/types.h>
#include <signal.h>
#include <sys/stat.h>
#include <dirent.h>
#include <fcntl.h>
#include <cstdlib>
#include <cstdio>
#include <cstring>
#include <unistd.h>
#include <iostream>
#include <fstream>
#include <sstream>
#include <algorithm>
#include <omp.h>
#include <numeric>
#include <time.h>
/*--------------------------------------------------------*/
#include "wattsup.hpp"
#include "debug.hpp"
#include "wattsuphelpers.hpp"
/*--------------------------------------------------------*/
namespace hcl {
/*--------------------------------------------------------*/
class Wattsup::Impl {
public:
Impl(const unsigned int nMeters,
const std::string* pathsToMeters,
const std::string* params,
const bool removeUSBLockFiles = 1);
~Impl();
/*
* Starts WattsUp measurement
*/
int start();
/*
* Stops WattsUp measurement
*/
int stop();
/*
* Provides the energy consumed during a measurement
*/
double energy() const;
double power() const;
/*
* Returns the power consumptions during a measurement
*/
std::vector<double> powers(
const unsigned int meter
) const;
/*
* Clean up all data related to a measurement
*/
int gc();
int execute(
const Measure& measure,
const std::string& executableName,
const std::string& executableParams,
const Precision* inPrecision,
Precision* outPrecision,
double* sampleMean,
double* sd);
int basePower(
const Precision* inPrecision,
Precision* outPrecision,
double* sampleMean,
double* sd);
private:
int measureTime(
const std::string& executableName,
const std::string& executableParams,
const Precision* inPrecision,
Precision* outPrecision,
double* sampleMean,
double* sd);
int measureEnergyOrPower(
const Measure& measure,
const std::string& executableName,
const std::string& executableParams,
const Precision* inPrecision,
Precision* outPrecision,
double* sampleMean,
double* sd);
int measureEnergyOrPowerNullPrecision(
const Measure& measure,
const std::string& executableName,
const std::string& executableParams,
double* sampleMean,
double* sd);
int getNumInvocations(
const std::string& executableName,
const std::string& executableParams,
const Precision* inPrecision,
Precision* outPrecision,
unsigned int* numInvocations);
int Popen(
const std::string& executableName,
const std::string& executableParams,
const unsigned int numInvocations
);
private:
/*
* Return the filename for the meter given by the id
*/
std::string getOutputFileName(
const size_t meterId
) const;
private:
/*
* Paths to the WattsUp Meters
*/
std::vector<std::string> mPathsToMeters;
/*
* Params to the WattsUp Meters
*/
std::vector<std::string> mParams;
/*
* The PIDs of the processes invoking the scripts for the meters
*/
std::vector<pid_t> mPids;
/*
* Directory where to write the meter readings
*/
std::string mOutputDirectory;
/*
* The mode for the directory and the data files
*/
mode_t mMode;
/*
* The column of the power in the WattsUp data file
*/
unsigned int mPowerCol;
/*
* Number of header lines in WattsUp Output
*/
unsigned int* mMeterPreLines;
unsigned int mNumHeaderLines;
/*
* Remove lock files...
*/
bool mLockUSBFiles;
};
/*--------------------------------------------------------*/
Wattsup::Wattsup(const unsigned int nMeters,
const std::string* pathsToMeters,
const std::string* params,
const bool removeUSBLockFiles)
: wattsupImpl(new Impl(
nMeters, pathsToMeters,
params, removeUSBLockFiles))
{}
/*--------------------------------------------------------*/
Wattsup::~Wattsup() { delete wattsupImpl; }
/*--------------------------------------------------------*/
int
Wattsup::start()
{
return wattsupImpl->start();
}
/*--------------------------------------------------------*/
int
Wattsup::stop()
{
return wattsupImpl->stop();
}
/*--------------------------------------------------------*/
double
Wattsup::energy() const
{
return wattsupImpl->energy();
}
/*--------------------------------------------------------*/
double
Wattsup::power() const
{
return wattsupImpl->power();
}
/*--------------------------------------------------------*/
std::vector<double>
Wattsup::powers(
const unsigned int meter
) const
{
return wattsupImpl->powers(meter);
}
/*--------------------------------------------------------*/
int
Wattsup::basePower(
const Precision* inPrecision,
Precision* outPrecision,
double* sampleMean,
double* sd)
{
return wattsupImpl->basePower(
inPrecision, outPrecision, sampleMean, sd);
}
/*--------------------------------------------------------*/
int
Wattsup::gc()
{
return wattsupImpl->gc();
}
/*--------------------------------------------------------*/
Wattsup::Impl::Impl(
const unsigned int nMeters,
const std::string* pathsToMeters,
const std::string* params,
const bool removeUSBLockFiles
)
{
mOutputDirectory = "HCLWattsUpData";
mPowerCol = 4;
mMode = 0777;
Debug::Info(
"Number of Wattsup meters %u.\n",
nMeters);
for (unsigned int meter = 0; meter < nMeters; meter++)
{
Debug::Info(
"Meter %s, %s.\n",
pathsToMeters[meter].c_str(),
params[meter].c_str());
mPathsToMeters.push_back(pathsToMeters[meter]);
mParams.push_back(params[meter]);
}
mNumHeaderLines = 5;
mLockUSBFiles = removeUSBLockFiles;
mMeterPreLines = (unsigned int*)malloc(
sizeof(unsigned int)*nMeters);
for (unsigned int meter = 0; meter < nMeters; meter++)
{
mMeterPreLines[meter] = 0;
}
}
/*--------------------------------------------------------*/
Wattsup::Impl::~Impl()
{
free(mMeterPreLines);
}
/*--------------------------------------------------------*/
std::string
Wattsup::Impl::getOutputFileName(
const size_t meterId
) const
{
std::stringstream ss;
ss << mOutputDirectory << "/" << meterId << ".csv";
return ss.str();
}
/*--------------------------------------------------------*/
int
Wattsup::Impl::start()
{
struct stat st = {0};
if (stat(mOutputDirectory.c_str(), &st) == -1)
{
Debug::Info(
"Creating data directory %s.\n",
mOutputDirectory.c_str());
int rc = mkdir(mOutputDirectory.c_str(), mMode);
if (rc == -1)
{
std::cerr << "Unable to create directory "
<< mOutputDirectory
<< ", check permissions"
<< std::endl;
return -1;
}
}
/*
* Always remove the lock files...
*/
size_t nMeters = mPathsToMeters.size();
if (mLockUSBFiles)
{
for (size_t meter = 0; meter < nMeters; meter++)
{
std::stringstream ss;
ss << "/var/lock/LCK..ttyUSB" << meter;
Debug::Info(
"Removing lock file %s.\n",
ss.str().c_str());
remove(ss.str().c_str());
}
}
Debug::Info(
"Number of meters %zu.\n",
nMeters);
for (size_t meter = 0; meter < nMeters; meter++)
{
Debug::Info(
"Launching meter %zu.\n",
meter);
mPids.push_back(fork());
if (mPids[meter] < 0)
{
std::cerr << "HCL Internal Error..."
<< "Unable to fork()..."
<< std::endl;
return -1;
}
if (mPids[meter] == 0)
{
Debug::Info(
"Child process %d: opening file %s.\n",
mPids[meter], getOutputFileName(meter).c_str());
int fd = open(
getOutputFileName(meter).c_str(),
O_WRONLY | O_CREAT | O_TRUNC, mMode);
if (fd == -1)
{
Debug::Error(
"Child process: %d, open(%s) failure...\n",
mPids[meter], getOutputFileName(meter).c_str());
}
dup2(fd, 1);
close(fd);
dup2(open("/dev/null", O_WRONLY), 2);
int rc = execl(
mPathsToMeters[meter].c_str(),
mPathsToMeters[meter].c_str(),
mParams[meter].c_str(), NULL);
if (rc == -1)
{
/*
* Best situation is to remove the file...
*/
remove(getOutputFileName(meter).c_str());
Debug::Error(
"Child process: %d, execl() failure...\n",
mPids[meter]);
}
}
}
/*
* In the parent process, we check if in each data file,
* necessary header records have been written...
*/
while (1)
{
#pragma omp parallel num_threads(nMeters)
{
#pragma omp for
for (size_t meter = 0; meter < nMeters; meter++)
{
std::ifstream ifs(
getOutputFileName(meter),
std::ifstream::in);
std::string line;
unsigned int nLines = 0;
while (getline(ifs, line))
{
std::size_t errorFound = line.find("Error");
if (errorFound != std::string::npos)
{
std::cerr << "Error from meter "
<< meter
<< ", Best option is to exit now..."
<< std::endl;
this->stop();
exit(EXIT_FAILURE);
}
nLines++;
}
ifs.close();
mMeterPreLines[meter] = nLines;
}
}
bool allMeterHeadersWritten = true;
for (size_t meter = 0; meter < nMeters; meter++)
{
if (!(mMeterPreLines[meter] > mNumHeaderLines))
{
allMeterHeadersWritten = false;
break;
}
}
if (allMeterHeadersWritten)
{
if (Debug::getVerbosity())
{
std::cout << "Number of lines in meters before measurement: ";
for (size_t meter = 0; meter < nMeters; meter++)
{
std::cout << mMeterPreLines[meter] << " ";
}
std::cout << std::endl;
}
break;
}
/*
* Nanosleep for 10 milliseconds...
*/
struct timespec tim, tim2;
tim.tv_sec = 0;
tim.tv_nsec = 1e07;
if (nanosleep(&tim, &tim2) < 0)
{
fprintf(
stderr,
"nanosleep issue...\n");
}
}
return 0;
}
/*--------------------------------------------------------*/
int
Wattsup::Impl::stop()
{
for (size_t pid = 0; pid < mPids.size(); pid++)
{
int status = kill(mPids[pid], SIGINT);
if (status != 0)
{
return status;
}
}
return 0;
}
/*--------------------------------------------------------*/
double
Wattsup::Impl::energy() const
{
double energy = 0.0;
for (size_t meter = 0; meter < mPathsToMeters.size(); meter++)
{
std::string fileName = getOutputFileName(meter);
std::vector<double> tokens = parse(
fileName,
mMeterPreLines[meter], mPowerCol);
energy += computeEnergy(&tokens);
}
return energy;
}
/*--------------------------------------------------------*/
double
Wattsup::Impl::power() const
{
double power = 0.0;
for (size_t meter = 0; meter < mPathsToMeters.size(); meter++)
{
std::string fileName = getOutputFileName(meter);
std::vector<double> tokens = parse(
fileName,
mMeterPreLines[meter], mPowerCol);
power += (std::accumulate(tokens.begin(), tokens.end(), 0.0))
/ tokens.size();
}
return power;
}
/*--------------------------------------------------------*/
std::vector<double>
Wattsup::Impl::powers(
const unsigned int meter
) const
{
std::string fileName = getOutputFileName(meter);
return parse(fileName, mMeterPreLines[meter], mPowerCol);
}
/*--------------------------------------------------------*/
int
Wattsup::Impl::gc()
{
Debug::Info(
"Cleaning up files for meters %zu.\n",
mPathsToMeters.size());
for (size_t meter = 0; meter < mPathsToMeters.size(); meter++)
{
std::string fileName = getOutputFileName(meter);
Debug::Info(
"Removing %s.\n",
fileName.c_str());
remove(fileName.c_str());
}
Debug::Info(
"Removing directory %s.\n",
mOutputDirectory.c_str());
rmdir(mOutputDirectory.c_str());
mPids.clear();
return 0;
}
/*--------------------------------------------------------*/
}
/*--------------------------------------------------------*/
#include "wattsupprecision.cpp"
/*--------------------------------------------------------*/