-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathavg.c
139 lines (132 loc) · 3.9 KB
/
avg.c
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
/*
Copyright 2024-2025 Lukas Tautz
This file is part of cpu_logger.
cpu_logger is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, version 3 of the License.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
#include "include.h"
void calculate_and_print_averages(int data_fd, int write_to) {
measured_load load;
char buf[8];
double tmp;
#ifdef FEATURE_LOAD
double cpu_load_percent = 0.0, cpu_load_max_percent = 0.0;
#endif
#ifdef FEATURE_STEAL
double cpu_steal_percent = 0.0, cpu_steal_max_percent = 0.0;
#endif
#ifdef FEATURE_IOWAIT
double iowait_percent = 0.0, iowait_max_percent = 0.0;
#endif
#ifdef FEATURE_MEMORY
double memory_usage_percent = 0.0, memory_usage_max_percent = 0.0;
#endif
uint64 points = 0;
while (read(data_fd, &load, sizeof(load)) == sizeof(load)) {
#ifdef FEATURE_LOAD
_ADD_AVG_MAX(cpu_load);
#endif
#ifdef FEATURE_STEAL
_ADD_AVG_MAX(cpu_steal);
#endif
#ifdef FEATURE_IOWAIT
_ADD_AVG_MAX(iowait);
#endif
#ifdef FEATURE_MEMORY
_ADD_AVG_MAX(memory_usage);
#endif
++points;
}
WRITE("\t\t");
#ifdef FEATURE_LOAD
cpu_load_percent /= (points ? points : 1);
WRITE("Load\t\t");
#endif
#ifdef FEATURE_STEAL
cpu_steal_percent /= (points ? points : 1);
WRITE("Steal\t\t");
#endif
#ifdef FEATURE_IOWAIT
iowait_percent /= (points ? points : 1);
WRITE("IOWait\t\t");
#endif
#ifdef FEATURE_MEMORY
memory_usage_percent /= (points ? points : 1);
WRITE("Memory\t\t");
#endif
WRITE("\nAverages:\t");
bool is_first = true;
#ifdef FEATURE_LOAD
_PRINT_PERCENT(cpu_load_percent);
is_first = false;
#endif
#ifdef FEATURE_STEAL
if (!is_first)
WRITE("\t");
_PRINT_PERCENT(cpu_steal_percent);
is_first = false;
#endif
#ifdef FEATURE_IOWAIT
if (!is_first)
WRITE("\t");
_PRINT_PERCENT(iowait_percent);
is_first = false;
#endif
#ifdef FEATURE_MEMORY
if (!is_first)
WRITE("\t");
_PRINT_PERCENT(memory_usage_percent);
#endif
WRITE("\nMaxima:\t\t");
is_first = true;
#ifdef FEATURE_LOAD
_PRINT_PERCENT(cpu_load_max_percent);
is_first = false;
#endif
#ifdef FEATURE_STEAL
if (!is_first)
WRITE("\t");
_PRINT_PERCENT(cpu_steal_max_percent);
is_first = false;
#endif
#ifdef FEATURE_IOWAIT
if (!is_first)
WRITE("\t");
_PRINT_PERCENT(iowait_max_percent);
is_first = false;
#endif
#ifdef FEATURE_MEMORY
if (!is_first)
WRITE("\t");
_PRINT_PERCENT(memory_usage_max_percent);
#endif
WRITE("\n");
}
int main(uint8 argc, char **argv) {
uint64 maxima = 0;
if (argc == 1 || (argc != 2 && !(argc == 3 && (maxima = atol(argv[2])) != 0)) || !memcmp(argv[1], "-h", sizeof("-h")) || !memcmp(argv[1], "--help", sizeof("--help"))) {
WRITE_STDERR("Usage: ");
WRITE_STDERR(argv[0]);
WRITE_STDERR(" FILE [MAXIMA]\nThis program prints the average and max load (as configured) from the binary file FILE generated with cpu_logger.\nWhen MAXIMA is specified and there are more than MAXIMA elements within FILE, only the last MAXIMA elements are displayed and used for average/maxima calculations.\n");
return 4;
}
int fd = open(argv[1], O_RDONLY);
if (fd == -1)
return 1;
if (maxima) {
int64 pos = lseek(fd, 0, SEEK_END) - (maxima * sizeof(measured_load));
if (pos < 0)
pos = 0; // there are less than MAXIMA elements in the file
lseek(fd, pos, SEEK_SET);
}
calculate_and_print_averages(fd, STDOUT);
close(fd);
return 0;
}