-
Notifications
You must be signed in to change notification settings - Fork 0
/
uptime.cpp
99 lines (76 loc) · 2.76 KB
/
uptime.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
#include <iostream>
#include <fstream>
#include <sstream>
#include <cstdlib>
#include <string>
#include <iomanip>
#include <sys/sysinfo.h>
/* To enable the usage of uSleep() */
#include <ctime>
#include <unistd.h>
#ifndef FUNCTIONS_H
#include"functions.hpp"
#endif
using namespace std;
/**
* Method for calculating energy
* @param - seconds
* @return - calculated engergy as a double
*/
double calcEnergy(long long unsigned int secs, int TDP){
int million = 1000000;
double energy = secs * TDP;
double e = energy/million;
return e;
}
/**
* Method for parsing /proc/uptime, obtaing system information and neatly displaying it as required
*/
void getUpTime(){
//open /proc/uptime file
ifstream file("/proc/uptime");
string line;
//Checks whether state of stream is good. If not it reutrns an error message.
if(!file.good()){
cerr << "Could not open file.... exitting..." << endl;
exit(EXIT_FAILURE);
}
//Lopps through the file and reads it line by line
while(getline(file, line)){
stringstream linestream(line);
string token;
//The list of variables to be used
int hoursUP, hoursIdle, minutesUP, minutesIdle;
long long unsigned int total_Uptime, space,total_Idle;
//Gets and returns system information
struct sysinfo uptimeInfo;
sysinfo (&uptimeInfo);
//sets the the value of uptime to the variable
total_Uptime = uptimeInfo.uptime;
//
if(linestream >> token){
linestream>> total_Idle;
}
//Calculating the number of minutes and hours from seconds
minutesUP = total_Uptime / 60;
hoursUP = minutesUP / 60;
minutesIdle = total_Idle / 60;
hoursIdle = minutesIdle / 60;
//Displays the system information
cout << "SYSTEM" << endl;
cout <<"\t\t\t" <<"UP for " << int(hoursUP) << " hours " << int(minutesUP%60) << " minutes " << int(total_Uptime%60) << " seconds." << endl;
cout << "\t\t\t" <<"IDLE for " << int(hoursIdle) << " hours " << int(minutesIdle%60) << " minutes " << int(total_Idle%60) << " seconds." << endl;
cout << "----------------------------------------------------------------------------" << endl;
//Idle and active power from the system im using
int TDP_Active = 30;
int TDP_Idle = 8;
//Displays the energy information
cout << "ENERGY" << endl;
cout << setprecision(2)<<fixed;
cout<< "\t\t\t"<<"In Active State: " << calcEnergy(total_Uptime,TDP_Active)<< " Mjoules"<<endl;
cout <<"\t\t\t"<<"In Idle State: " << calcEnergy(total_Idle, TDP_Idle)<< " Mjoules" <<endl;
cout << "----------------------------------------------------------------------------" << endl;
}
// close /proc/uptime file
file.close();
}