"SfR Fresh" - the SfR Freeware/Shareware Archive 
Member "procinfo-ng-2.0.217/linux26_procstat.cpp" of archive procinfo-ng-2.0.217.tar.gz:
As a special service "SfR Fresh" has tried to format the requested source page into HTML format using (guessed) C and C++ source code syntax highlighting with prefixed line numbers.
Alternatively you can here view or download the uninterpreted source code file.
That can be also achieved for any archive member file by clicking within an archive contents listing on the first character of the file(path) respectively on the according byte size field.
1 /*
2 This file is part of procinfo-NG
3
4 procinfo-NG is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; version 2.
7
8 procinfo-NG is distributed in the hope that it will be useful,
9 but WITHOUT ANY WARRANTY; without even the implied warranty of
10 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 GNU General Public License for more details.
12
13 You should have received a copy of the GNU General Public License
14 along with procinfo-NG; if not, write to the Free Software
15 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
16 */
17
18 // Procinfo-NG is Copyright tabris@tabris.net 2007, 2008
19
20 vector <uint64_t> normalizeCPUstats(const double elapsed, const uint64_t CPUcount, const vector <uint64_t> &input) {
21 if(elapsed == 0)
22 return input;
23 vector <uint64_t> output(input.begin(), input.end() - 2);
24 uint64_t timeSum = sumVec(output);
25
26 double factor = (USER_HZ * (CPUcount * elapsed)) / double(timeSum);
27 for(uint32_t i = 0; i < output.size(); i++)
28 output[i] = uint64_t(double(output[i]) * factor);
29
30 return output;
31 }
32
33 // returns multiple lists of uint64s, cpuDiffs, intrDiffs, and a list consisting of context-switches and the boot-time
34 vector <vector <uint64_t> > getProcStat(bool showTotals, const uint32_t CPUcount, const double elapsed) {
35 vector <string> lines = readFile(string("/proc/stat"));
36 vector <uint64_t> cpuDiff, cpuStat, intrDiff, intrStat;
37
38 static vector <uint64_t> oldCPUstat, oldIntrStat;
39 static uint64_t oldCtxtStat = 0;
40
41 uint64_t ctxtStat, ctxtDiff, bootTime;
42 uint64_t cpuTotal = 0;
43
44 for(uint32_t i = 0; i < lines.size(); i++) {
45 vector <string> tokens = splitString(" ", lines[i]);
46 if (!tokens.size()) break; // a) prevents SIGSEGV b) skips empty lines
47 if(tokens[0] == "cpu") {
48 tokens.erase(tokens.begin()); // pop the first token off.
49
50 cpuStat = stringVec2uint64Vec(tokens);
51 if(!oldCPUstat.size())
52 oldCPUstat.resize(cpuStat.size());
53 cpuDiff = (showTotals ? cpuStat : subVec(cpuStat, oldCPUstat));
54 cpuTotal = sumVec(cpuStat);
55 oldCPUstat.assign(cpuStat.begin(), cpuStat.end());
56 cpuDiff.push_back(cpuTotal);
57 } else if(tokens[0] == "intr") {
58 if(tokens.size() <= 2) {
59 intrStat = getIRQcount();
60 } else {
61 // We don't want the second token b/c it's just the total number of interrupts serviced.
62 tokens.erase(tokens.begin()); // pop the first token off.
63 tokens.erase(tokens.begin()); // pop the second token off.
64
65 intrStat = stringVec2uint64Vec(tokens);
66 }
67 if(oldIntrStat.size() < intrStat.size())
68 oldIntrStat.resize(intrStat.size(), 0);
69 intrDiff = (showTotals ? intrStat : subVec(intrStat, oldIntrStat));
70 oldIntrStat.assign(intrStat.begin(), intrStat.end());
71 } else if(tokens[0] == "ctxt") {
72 ctxtStat = string2uint64(tokens[1]);
73 ctxtDiff = (showTotals ? ctxtStat : ctxtStat - oldCtxtStat);
74 oldCtxtStat = ctxtStat;
75 } else if(tokens[0] == "btime") {
76 bootTime = string2uint64(tokens[1]);
77 }
78 }
79 vector <vector <uint64_t> > stats;
80 stats.resize(3);
81 stats[0] = normalizeCPUstats(elapsed, CPUcount, cpuDiff);
82 stats[1] = intrDiff;
83 stats[2].push_back(ctxtDiff);
84 stats[2].push_back(bootTime);
85 return stats;
86 }
87
88 // returns the contents of /proc/vmstat, only the parts we want.
89 // as such it returns a vector of 4 elements, pageInDiff, pageOutDiff, swapInDiff, swapOutDiff
90 vector <uint64_t> getVMstat(bool showTotals) {
91 vector <string> lines = readFile(string("/proc/vmstat"));
92
93 static uint64_t oldPageIn = 0, oldPageOut = 0, oldSwapIn = 0, oldSwapOut = 0;
94 uint64_t pageIn = 0, pageOut = 0, swapIn = 0, swapOut = 0;
95 uint64_t pageInDiff = 0, pageOutDiff = 0, swapInDiff = 0, swapOutDiff = 0;
96
97 static uint64_t oldPageAct = 0, oldPageDeact = 0, oldPageFault = 0;
98 uint64_t pageAct = 0, pageDeact = 0, pageFault = 0;
99 uint64_t pageActDiff = 0, pageDeactDiff = 0, pageFaultDiff = 0;
100
101 for(uint32_t i = 0; i < lines.size(); i++) {
102 vector <string> tokens = splitString(" ", lines[i]);
103 if (!tokens.size()) break;
104 if(tokens[0] == "pgpgin") {
105 pageIn = string2uint64(tokens[1]);
106 pageInDiff = (showTotals ? pageIn : pageIn - oldPageIn);
107 oldPageIn = pageIn;
108 } else if(tokens[0] == "pgpgout") {
109 pageOut = string2uint64(tokens[1]);
110 pageOutDiff = (showTotals ? pageOut : pageOut - oldPageOut);
111 oldPageOut = pageOut;
112 } else if(tokens[0] == "pswpin") {
113 swapIn = string2uint64(tokens[1]);
114 swapInDiff = (showTotals ? swapIn : swapIn - oldSwapIn);
115 oldSwapIn = swapIn;
116 } else if(tokens[0] == "pswpout") {
117 swapOut = string2uint64(tokens[1]);
118 swapOutDiff = (showTotals ? swapOut : swapOut - oldSwapOut);
119 oldSwapOut = swapOut;
120 } else if(tokens[0] == "pgactivate") {
121 pageAct = string2uint64(tokens[1]);
122 pageActDiff = (showTotals ? pageAct : pageAct - oldPageAct);
123 oldPageAct = pageAct;
124 } else if(tokens[0] == "pgdeactivate") {
125 pageDeact = string2uint64(tokens[1]);
126 pageDeactDiff = (showTotals ? pageDeact : pageDeact - oldPageDeact);
127 oldPageDeact = pageDeact;
128 } else if(tokens[0] == "pgfault") {
129 pageFault = string2uint64(tokens[1]);
130 pageFaultDiff = (showTotals ? pageFault : pageFault - oldPageFault);
131 oldPageFault = pageFault;
132 }
133 }
134 vector <uint64_t> vmStat;
135 vmStat.push_back(pageInDiff);
136 vmStat.push_back(pageOutDiff);
137 vmStat.push_back(pageActDiff);
138 vmStat.push_back(pageDeactDiff);
139 vmStat.push_back(pageFaultDiff);
140 vmStat.push_back(swapInDiff);
141 vmStat.push_back(swapOutDiff);
142 return vmStat;
143 }