-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathavg.c
109 lines (102 loc) · 2.91 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
/*
Copyright 2024 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_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_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_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_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_MEMORY
if (!is_first)
WRITE("\t");
_PRINT_PERCENT(memory_usage_max_percent);
#endif
WRITE("\n");
}
int main(uint8 argc, char **argv) {
if (argc != 2 || !memcmp(argv[1], "-h", sizeof("-h")) || !memcmp(argv[1], "--help", sizeof("--help"))) {
WRITE_STDERR("Usage: ");
WRITE_STDERR(argv[0]);
WRITE_STDERR(" FILE\nThis program prints the average and max load (as configured) from the binary file FILE generated with cpu_logger.\n");
return 1;
}
int fd = open(argv[1], O_RDONLY);
if (fd == -1)
return 1;
calculate_and_print_averages(fd, STDOUT);
close(fd);
}