-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathshowpgcpuse.cpp
More file actions
92 lines (82 loc) · 2.66 KB
/
showpgcpuse.cpp
File metadata and controls
92 lines (82 loc) · 2.66 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
#include <stdio.h>
#include <map>
#include <vector>
#include <string>
#include <iostream>
#include <sstream>
#include <fstream>
#include <algorithm>
using std::string; using std::cout; using std::endl; using std::map; using std::vector;
std::string ltrim( const std::string& s ) {
size_t startpos = s.find_first_not_of(" \n\r\t");
return (startpos == std::string::npos) ? "" : s.substr(startpos);
}
std::string rtrim( const std::string& s ) {
size_t endpos = s.find_last_not_of(" \n\r\t");
return (endpos == std::string::npos) ? "" : s.substr(0, endpos+1);
}
std::string trim( const std::string& s ) {
return rtrim(ltrim(s));
}
std::vector<string> explode( const string& s, const string& separ ) {
std::vector<string> resu; string token("");
for ( auto const &c: s ) {
if (separ.find_first_of(c)==string::npos) token+=c;
else { resu.push_back(token); token=""; }
}
if (token.size()) resu.push_back(token);
return resu;
}
long long total_for(const map<string,long long>& ventil)
{
long long resu=0;
for (auto const &it: ventil) resu+=it.second;
return resu;
}
void disp_stat(const string& title, const map<string,long long>& ventil)
{
cout << title << endl;
long long tot=total_for(ventil);
for (auto const &it: ventil)
{
printf("%s %8llu ms %2llu%%\n",(it.first+" ").substr(0,18).c_str(),it.second,(it.second*100)/tot);
}
cout << endl;
}
int main(int argc, const char *argv[])
{
if (argc<2) { printf("syntax: %s <stats_file>\n",argv[0]); return 1; }
std::ifstream in(argv[1]);
long long maxts=0;
map<string,long long> cpu_by_db, cpu_by_user, cpu_by_from;
long long cpu_total=0;
map<string,unsigned int> proc_by_db, proc_by_user, proc_by_from;
unsigned int proc_total=0;
while (!in.eof())
{
string line;
std::getline(in,line);
line=trim(line);
if (line.substr(0,5)!="START")
{
//cout << ">" << line << "<" << endl;
vector<string> fld=explode(line,"\t");
if ((line[0]>='0') && (line[0]<='9') && (fld.size()>=7))
{
string db=fld[4],user=fld[5],from=fld[6];
maxts=std::max(maxts,atoll(fld[2].c_str()));
//cout << "db=" << db << "user=" << user << endl;
long long cpused=atoll(fld[3].c_str())+5;
cpu_by_db[db]+=cpused; cpu_by_user[user]+=cpused; cpu_by_from[from]+=cpused;
++proc_by_db[db]; ++proc_by_user[user]; ++proc_by_from[from];
cpu_total+=cpused; ++proc_total;
}
}
}
disp_stat("CPU BY DB:",cpu_by_db);
disp_stat("CPU BY USER:",cpu_by_user);
disp_stat("CPU BY ORIGIN:",cpu_by_from);
cout << "Total CPU USED: " << ((float)cpu_total/1000) << " s (avg " << ((float)((cpu_total*1000)/maxts)/10) << "% during " << ((float)maxts/1000) << " s)" << endl;
//TODO: total cpu time
return 0;
}