-
Notifications
You must be signed in to change notification settings - Fork 0
/
kernel_metrics.h
271 lines (234 loc) · 5.3 KB
/
kernel_metrics.h
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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
/* SPDX-License-Identifier: GPL-2.0-or-later */
/* Copyright 2023 Brian Witte
*/
#ifndef METRICS_H
#define METRICS_H
#include <sys/resource.h>
#include <sys/sysinfo.h>
#include <sys/time.h>
#include <stdio.h>
#include <time.h>
#include <unistd.h>
#include <stdlib.h>
#include <stdint.h>
#include <stddef.h>
static inline void kmh_get_start_time(const char *ps_name, char *buffer,
size_t buffer_size)
{
pid_t pid = getpid();
time_t now = time(NULL);
struct tm *local_time = localtime(&now);
char time_str[30];
strftime(time_str, sizeof(time_str), "%Y-%m-%d %H:%M:%S", local_time);
snprintf(buffer, buffer_size, "%s (pid: %d) started at %s\n", ps_name,
pid, time_str);
}
static inline double kmh_calculate_running_time()
{
struct sysinfo info;
sysinfo(&info);
double uptime_seconds = (double)info.uptime;
pid_t pid = getpid();
char path[40];
snprintf(path, sizeof(path), "/proc/%d/stat", pid);
FILE *fp = fopen(path, "r");
if (fp) {
long long start_time;
// Skip the first 21 words in /proc/[pid]/stat
for (int i = 0; i < 21; i++) {
fscanf(fp, "%*s");
}
fscanf(fp, "%lld", &start_time);
fclose(fp);
double ticks_per_second = sysconf(_SC_CLK_TCK);
double start_time_seconds =
(double)start_time / ticks_per_second;
return uptime_seconds - start_time_seconds;
}
return 0.0;
}
static inline void kmh_get_process_info(char *buffer, size_t buffer_size)
{
pid_t pid = getpid();
struct rusage usage;
getrusage(RUSAGE_SELF, &usage);
double running_time = kmh_calculate_running_time();
snprintf(
buffer, buffer_size,
"pid: %d, cpu utime = %ld.%06ld sec, mem maxrss = %ld kB, running time: %.2f seconds\n",
pid, usage.ru_utime.tv_sec, usage.ru_utime.tv_usec,
usage.ru_maxrss, running_time);
}
typedef struct {
void *data;
void (*update)(void *data);
void *(*get_value)(void *data);
} km_point;
static inline km_point *create_point(void *(*get_value_func)(void *),
void (*update_func)(void *), void *data)
{
km_point *point = (km_point *)malloc(sizeof(km_point));
if (point) {
point->data = data;
point->get_value = get_value_func;
point->update = update_func;
}
return point;
}
static inline void *point_get_value(km_point *point)
{
if (point && point->get_value) {
return point->get_value(point->data);
}
return NULL;
}
static inline void point_update(km_point *point)
{
if (point && point->update) {
point->update(point->data);
}
}
static inline void destroy_point(km_point *point)
{
if (point) {
free(point);
}
}
typedef struct {
int64_t count;
} km_counter;
static inline km_counter *create_counter()
{
km_counter *counter = (km_counter *)malloc(sizeof(km_counter));
if (counter) {
counter->count = 0;
}
return counter;
}
static inline void counter_inc(km_counter *counter, int64_t value)
{
if (counter) {
counter->count += value;
}
}
static inline void counter_dec(km_counter *counter, int64_t value)
{
if (counter) {
counter->count -= value;
}
}
static inline int64_t counter_get_value(km_counter *counter)
{
return counter ? counter->count : 0;
}
static inline void destroy_counter(km_counter *counter)
{
if (counter) {
free(counter);
}
}
typedef struct {
long long count;
double totalTime;
} km_rangemap;
static inline km_rangemap *create_rangemap()
{
km_rangemap *rangemap = (km_rangemap *)malloc(sizeof(km_rangemap));
if (rangemap) {
rangemap->count = 0;
rangemap->totalTime = 0;
}
return rangemap;
}
static inline void rangemap_update(km_rangemap *rangemap, double duration)
{
if (rangemap) {
rangemap->count++;
rangemap->totalTime += duration;
}
}
static inline double rangemap_mean(const km_rangemap *rangemap)
{
if (!rangemap || rangemap->count == 0)
return 0;
return rangemap->totalTime / rangemap->count;
}
static inline void destroy_rangemap(km_rangemap *rangemap)
{
if (rangemap) {
free(rangemap);
}
}
typedef struct {
long long eventCount;
time_t startTime;
} km_meter;
static inline km_meter *create_meter()
{
km_meter *meter = (km_meter *)malloc(sizeof(km_meter));
if (meter) {
meter->eventCount = 0;
meter->startTime = time(NULL);
}
return meter;
}
static inline void meter_mark(km_meter *meter, long long count)
{
if (meter) {
meter->eventCount += count;
}
}
static inline double meter_mean_rate(const km_meter *meter)
{
if (!meter || meter->eventCount == 0)
return 0;
time_t now = time(NULL);
double elapsedSeconds = difftime(now, meter->startTime);
if (elapsedSeconds == 0)
return 0;
return (double)meter->eventCount / elapsedSeconds;
}
static inline void destroy_meter(km_meter *meter)
{
if (meter) {
free(meter);
}
}
typedef struct {
km_meter meter;
km_rangemap rangemap;
time_t startTime;
} km_timer;
static inline km_timer *create_timer()
{
km_timer *timer = (km_timer *)malloc(sizeof(km_timer));
if (timer) {
timer->meter = *create_meter();
timer->rangemap = *create_rangemap();
timer->startTime = time(NULL);
}
return timer;
}
static inline void timer_start(km_timer *timer)
{
if (timer) {
timer->startTime = clock();
}
}
static inline void timer_stop(km_timer *timer)
{
if (timer) {
clock_t endTime = clock();
double duration =
(double)(endTime - timer->startTime) / CLOCKS_PER_SEC;
rangemap_update(&timer->rangemap, duration);
meter_mark(&timer->meter, 1);
}
}
static inline void destroy_timer(km_timer *timer)
{
if (timer) {
free(timer);
}
}
#endif // METRICS_H