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
|
#include <iostream>
#include <fstream>
#include <string>
#include <map>
#include <utility>
using namespace std;
const int NUM_SERVER_VALUES = 11;
typedef map<string, pair<int, int> > serverInfo;
// This code reads the stats file and displays the current values
void ReadAndShowServerInfo(serverInfo& info, const string serverValueNames[], bool first)
{
ifstream myFile;
myFile.open("/sys/block/sda/stat");
cout << "\n" << (first ? "First" : "Second") << " set of values:" << endl;
for(int i = 0; i < NUM_SERVER_VALUES; i++)
{
myFile >> (first ? info[ serverValueNames[i] ].first : info[ serverValueNames[i] ].second);
cout << serverValueNames[i] << ": " << (first ? info[ serverValueNames[i] ].first : info[ serverValueNames[i] ].second) << endl;
}
myFile.close();
}
int main()
{
// number of readIO processed
// number of readIO merged
// number of sectors read
// total wait time for read requests
// number of writeIO processed
// number of writeIO merged
// number of sectors written
// total wait time for write requests
// number of IO currently in flight
// total time this block device has been active
// total wait time for all requests
string serverValueNames[] = { "Read I/Os", "Read Merges", "Read Sectors", "Read Ticks",
"Write I/Os", "Write Merges", "Write Sectors", "Write Ticks",
"in_flight", "io_Ticks", "time_in_queue" };
serverInfo info;
for(int i = 0; i < NUM_SERVER_VALUES; i++)
{
info[ serverValueNames[i] ] = pair<int, int>(0, 0);
}
// This code reads the stats file and displays the
// starting values we are going to work with.
ReadAndShowServerInfo(info, serverValueNames, true);
sleep(30);
// This code reads the stats file again, and displays
// the updated values we will use to calculate the differences
ReadAndShowServerInfo(info, serverValueNames, false);
// This block of code calculates the difference of our values
cout << "\nDifferences between first and second values:" << endl;
for(int i = 0; i < NUM_SERVER_VALUES; i++)
{
cout << serverValueNames[i] << " Difference: " << info[ serverValueNames[i] ].second - info[ serverValueNames[i] ].first << endl;
}
cout << endl;
return 0;
}
|