| 1 |
/* sysprof-cpu-usage.c |
| 2 |
* |
| 3 |
* Copyright 2023 Christian Hergert <[email protected]> |
| 4 |
* |
| 5 |
* This program is free software: you can redistribute it and/or modify |
| 6 |
* it under the terms of the GNU General Public License as published by |
| 7 |
* the Free Software Foundation, either version 3 of the License, or |
| 8 |
* (at your option) any later version. |
| 9 |
* |
| 10 |
* This program is distributed in the hope that it will be useful, |
| 11 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 |
* GNU General Public License for more details. |
| 14 |
* |
| 15 |
* You should have received a copy of the GNU General Public License |
| 16 |
* along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 17 |
* |
| 18 |
* SPDX-License-Identifier: GPL-3.0-or-later |
| 19 |
*/ |
| 20 |
|
| 21 |
#include "config.h" |
| 22 |
|
| 23 |
#include <ctype.h> |
| 24 |
#include <fcntl.h> |
| 25 |
#include <stdio.h> |
| 26 |
#include <stdlib.h> |
| 27 |
#include <string.h> |
| 28 |
#include <sys/stat.h> |
| 29 |
#include <unistd.h> |
| 30 |
|
| 31 |
#include <glib-unix.h> |
| 32 |
#include <glib/gstdio.h> |
| 33 |
|
| 34 |
#include "line-reader-private.h" |
| 35 |
|
| 36 |
#include "sysprof-cpu-usage.h" |
| 37 |
#include "sysprof-instrument-private.h" |
| 38 |
#include "sysprof-recording-private.h" |
| 39 |
|
| 40 |
#define PROC_STAT_BUF_SIZE (4096*4) |
| 41 |
|
| 42 |
struct _SysprofCpuUsage |
| 43 |
{ |
| 44 |
SysprofInstrument parent_instance; |
| 45 |
}; |
| 46 |
|
| 47 |
struct _SysprofCpuUsageClass |
| 48 |
{ |
| 49 |
SysprofInstrumentClass parent_class; |
| 50 |
}; |
| 51 |
|
| 52 |
G_DEFINE_FINAL_TYPE (SysprofCpuUsage, sysprof_cpu_usage, SYSPROF_TYPE_INSTRUMENT) |
| 53 |
|
| 54 |
typedef struct _CpuInfo |
| 55 |
{ |
| 56 |
int counter_base; |
| 57 |
double total; |
| 58 |
glong last_user; |
| 59 |
glong last_idle; |
| 60 |
glong last_system; |
| 61 |
glong last_nice; |
| 62 |
glong last_iowait; |
| 63 |
glong last_irq; |
| 64 |
glong last_softirq; |
| 65 |
glong last_steal; |
| 66 |
glong last_guest; |
| 67 |
glong last_guest_nice; |
| 68 |
} CpuInfo; |
| 69 |
|
| 70 |
typedef struct _CpuFreq |
| 71 |
{ |
| 72 |
gint64 max; |
| 73 |
int stat_fd; |
| 74 |
char buf[116]; |
| 75 |
} CpuFreq; |
| 76 |
|
| 77 |
typedef struct _Record |
| 78 |
{ |
| 79 |
SysprofRecording *recording; |
| 80 |
DexFuture *cancellable; |
| 81 |
} Record; |
| 82 |
|
| 83 |
static void |
| 84 |
record_free (gpointer data) |
| 85 |
{ |
| 86 |
Record *record = data; |
| 87 |
|
| 88 |
g_clear_object (&record->recording); |
| 89 |
dex_clear (&record->cancellable); |
| 90 |
g_free (record); |
| 91 |
} |
| 92 |
|
| 93 |
static void |
| 94 |
freq_info_clear (gpointer data) |
| 95 |
{ |
| 96 |
CpuFreq *cpu_freq = data; |
| 97 |
g_clear_fd (&cpu_freq->stat_fd, NULL); |
| 98 |
} |
| 99 |
|
| 100 |
static double |
| 101 |
get_cpu_freq (int stat_fd, |
| 102 |
guint cpu, |
| 103 |
double max, |
| 104 |
char *buf, |
| 105 |
gssize len) |
| 106 |
{ |
| 107 |
gint64 val; |
| 108 |
|
| 109 |
if (stat_fd == -1) |
| 110 |
return .0; |
| 111 |
|
| 112 |
if (len <= 0) |
| 113 |
return .0; |
| 114 |
|
| 115 |
buf[len] = 0; |
| 116 |
g_strchug (buf); |
| 117 |
val = g_ascii_strtoll (buf, NULL, 10); |
| 118 |
|
| 119 |
val = CLAMP (val, .0, max); |
| 120 |
|
| 121 |
return (double)val / max * 100.; |
| 122 |
} |
| 123 |
|
| 124 |
static DexFuture * |
| 125 |
sysprof_cpu_usage_record_fiber (gpointer user_data) |
| 126 |
{ |
| 127 |
Record *record = user_data; |
| 128 |
g_autoptr(GArray) cpu_info = NULL; |
| 129 |
g_autoptr(GArray) freq_info = NULL; |
| 130 |
g_autofd int stat_fd = -1; |
| 131 |
g_autofree char *read_buffer = NULL; |
| 132 |
g_autofree SysprofCaptureCounterValue *values = NULL; |
| 133 |
g_autofree SysprofCaptureCounter *counters = NULL; |
| 134 |
g_autofree guint *ids = NULL; |
| 135 |
SysprofCaptureCounter *counter; |
| 136 |
SysprofCaptureWriter *writer; |
| 137 |
guint n_cpu; |
| 138 |
|
| 139 |
g_assert (record != NULL); |
| 140 |
g_assert (SYSPROF_IS_RECORDING (record->recording)); |
| 141 |
g_assert (DEX_IS_FUTURE (record->cancellable)); |
| 142 |
|
| 143 |
writer = _sysprof_recording_writer (record->recording); |
| 144 |
n_cpu = g_get_num_processors (); |
| 145 |
stat_fd = open ("/proc/stat", O_RDONLY|O_CLOEXEC); |
| 146 |
g_unix_set_fd_nonblocking (stat_fd, TRUE, NULL); |
| 147 |
read_buffer = g_malloc (PROC_STAT_BUF_SIZE); |
| 148 |
|
| 149 |
counters = g_new0 (SysprofCaptureCounter, (n_cpu * 2) + 1); |
| 150 |
ids = g_new0 (guint, (n_cpu * 2) + 1); |
| 151 |
values = g_new0 (SysprofCaptureCounterValue, (n_cpu * 2) + 1); |
| 152 |
|
| 153 |
cpu_info = g_array_new (FALSE, TRUE, sizeof (CpuInfo)); |
| 154 |
g_array_set_size (cpu_info, n_cpu); |
| 155 |
|
| 156 |
freq_info = g_array_new (FALSE, TRUE, sizeof (CpuFreq)); |
| 157 |
g_array_set_clear_func (freq_info, freq_info_clear); |
| 158 |
|
| 159 |
/* Create counter information for all of our counters that we will need |
| 160 |
* to submit in upcoming parses. |
| 161 |
*/ |
| 162 |
for (guint i = 0; i < n_cpu; i++) |
| 163 |
{ |
| 164 |
g_autofree char *max_path = g_strdup_printf ("/sys/devices/system/cpu/cpu%u/cpufreq/scaling_max_freq", i); |
| 165 |
g_autofree char *cur_path = g_strdup_printf ("/sys/devices/system/cpu/cpu%u/cpufreq/scaling_cur_freq", i); |
| 166 |
g_autofree char *max_value = NULL; |
| 167 |
CpuFreq cf; |
| 168 |
|
| 169 |
ids[i*2] = sysprof_capture_writer_request_counter (writer, 1); |
| 170 |
ids[i*2+1] = sysprof_capture_writer_request_counter (writer, 1); |
| 171 |
|
| 172 |
counter = &counters[i*2]; |
| 173 |
counter->id = ids[i*2]; |
| 174 |
counter->type = SYSPROF_CAPTURE_COUNTER_DOUBLE; |
| 175 |
counter->value.vdbl = 0; |
| 176 |
g_strlcpy (counter->category, "CPU Percent", sizeof counter->category); |
| 177 |
g_snprintf (counter->name, sizeof counter->name, "Total CPU %d", i); |
| 178 |
g_snprintf (counter->description, sizeof counter->description, |
| 179 |
"Total CPU usage %d", i); |
| 180 |
|
| 181 |
counter = &counters[i*2+1]; |
| 182 |
counter->id = ids[i*2+1]; |
| 183 |
counter->type = SYSPROF_CAPTURE_COUNTER_DOUBLE; |
| 184 |
counter->value.vdbl = 0; |
| 185 |
g_strlcpy (counter->category, "CPU Frequency", sizeof counter->category); |
| 186 |
g_snprintf (counter->name, sizeof counter->name, "CPU %d", i); |
| 187 |
g_snprintf (counter->description, sizeof counter->description, |
| 188 |
"Frequency of CPU %d", i); |
| 189 |
|
| 190 |
cf.stat_fd = open (cur_path, O_RDONLY|O_CLOEXEC); |
| 191 |
g_unix_set_fd_nonblocking (cf.stat_fd, TRUE, NULL); |
| 192 |
cf.buf[0] = 0; |
| 193 |
if (g_file_get_contents (max_path, &max_value, NULL, NULL)) |
| 194 |
cf.max = g_ascii_strtoll (max_value, NULL, 10); |
| 195 |
else |
| 196 |
cf.max = 0; |
| 197 |
g_array_append_val (freq_info, cf); |
| 198 |
} |
| 199 |
|
| 200 |
/* Now create our combined counter */ |
| 201 |
ids[n_cpu*2] = sysprof_capture_writer_request_counter (writer, 1); |
| 202 |
counter = &counters[n_cpu*2]; |
| 203 |
counter->id = ids[n_cpu*2]; |
| 204 |
counter->type = SYSPROF_CAPTURE_COUNTER_DOUBLE; |
| 205 |
counter->value.vdbl = 0; |
| 206 |
g_strlcpy (counter->category, "CPU Percent", sizeof counter->category); |
| 207 |
g_snprintf (counter->name, sizeof counter->name, "Combined"); |
| 208 |
g_snprintf (counter->description, sizeof counter->description, "Combined CPU usage"); |
| 209 |
|
| 210 |
/* Register all the counters as a group */ |
| 211 |
sysprof_capture_writer_define_counters (writer, |
| 212 |
SYSPROF_CAPTURE_CURRENT_TIME, |
| 213 |
-1, |
| 214 |
-1, |
| 215 |
counters, |
| 216 |
n_cpu * 2 + 1); |
| 217 |
|
| 218 |
for (;;) |
| 219 |
{ |
| 220 |
g_autoptr(GPtrArray) futures = g_ptr_array_new_with_free_func (dex_unref); |
| 221 |
g_autoptr(DexFuture) cpu_future = NULL; |
| 222 |
LineReader reader; |
| 223 |
glong total_usage = 0; |
| 224 |
gsize line_len; |
| 225 |
char *line; |
| 226 |
|
| 227 |
/* First collect all our reads and then wait for them to finish before |
| 228 |
* parsing in a pass. With io_uring, this lets us coalesce all the lseek |
| 229 |
* and reads into a single set of iops. |
| 230 |
*/ |
| 231 |
for (guint i = 0; i < n_cpu; i++) |
| 232 |
{ |
| 233 |
CpuFreq *cf = &g_array_index (freq_info, CpuFreq, i); |
| 234 |
|
| 235 |
g_ptr_array_add (futures, dex_aio_read (NULL, cf->stat_fd, cf->buf, sizeof cf->buf - 1, 0)); |
| 236 |
} |
| 237 |
cpu_future = dex_aio_read (NULL, stat_fd, read_buffer, PROC_STAT_BUF_SIZE, 0); |
| 238 |
g_ptr_array_add (futures, dex_ref (cpu_future)); |
| 239 |
|
| 240 |
/* Wait for all IO futures as they point at buffers owned by the array */ |
| 241 |
if (!dex_await (dex_future_allv ((DexFuture **)futures->pdata, futures->len), NULL)) |
| 242 |
break; |
| 243 |
|
| 244 |
/* Now parse all the contents of the stat files which should be |
| 245 |
* populated in the various files. |
| 246 |
*/ |
| 247 |
line_reader_init (&reader, read_buffer, dex_await_int64 (dex_ref (cpu_future), NULL)); |
| 248 |
while ((line = line_reader_next (&reader, &line_len))) |
| 249 |
{ |
| 250 |
CpuInfo *ci; |
| 251 |
char cpu[64]; |
| 252 |
glong user; |
| 253 |
glong sys; |
| 254 |
glong nice; |
| 255 |
glong idle; |
| 256 |
int id; |
| 257 |
int ret; |
| 258 |
glong iowait; |
| 259 |
glong irq; |
| 260 |
glong softirq; |
| 261 |
glong steal; |
| 262 |
glong guest; |
| 263 |
glong guest_nice; |
| 264 |
glong user_calc; |
| 265 |
glong system_calc; |
| 266 |
glong nice_calc; |
| 267 |
glong idle_calc; |
| 268 |
glong iowait_calc; |
| 269 |
glong irq_calc; |
| 270 |
glong softirq_calc; |
| 271 |
glong steal_calc; |
| 272 |
glong guest_calc; |
| 273 |
glong guest_nice_calc; |
| 274 |
glong total; |
| 275 |
|
| 276 |
line[line_len] = 0; |
| 277 |
|
| 278 |
/* CPU lines come first, short-circuit after */ |
| 279 |
if (!g_str_has_prefix (line, "cpu")) |
| 280 |
break; |
| 281 |
|
| 282 |
/* First line is "cpu ..." */ |
| 283 |
if (!g_ascii_isdigit (line[3])) |
| 284 |
continue; |
| 285 |
|
| 286 |
/* Parse the various counters in order */ |
| 287 |
user = nice = sys = idle = id = 0; |
| 288 |
ret = sscanf (line, "%63s %ld %ld %ld %ld %ld %ld %ld %ld %ld %ld", |
| 289 |
cpu, &user, &nice, &sys, &idle, |
| 290 |
&iowait, &irq, &softirq, &steal, &guest, &guest_nice); |
| 291 |
if (ret != 11) |
| 292 |
continue; |
| 293 |
|
| 294 |
/* Get the CPU identifier */ |
| 295 |
ret = sscanf (cpu, "cpu%d", &id); |
| 296 |
if (ret != 1 || id < 0 || id >= n_cpu) |
| 297 |
continue; |
| 298 |
|
| 299 |
ci = &g_array_index (cpu_info, CpuInfo, id); |
| 300 |
|
| 301 |
user_calc = user - ci->last_user; |
| 302 |
nice_calc = nice - ci->last_nice; |
| 303 |
system_calc = sys - ci->last_system; |
| 304 |
idle_calc = idle - ci->last_idle; |
| 305 |
iowait_calc = iowait - ci->last_iowait; |
| 306 |
irq_calc = irq - ci->last_irq; |
| 307 |
softirq_calc = softirq - ci->last_softirq; |
| 308 |
steal_calc = steal - ci->last_steal; |
| 309 |
guest_calc = guest - ci->last_guest; |
| 310 |
guest_nice_calc = guest_nice - ci->last_guest_nice; |
| 311 |
|
| 312 |
total = user_calc + nice_calc + system_calc + idle_calc + iowait_calc + irq_calc + softirq_calc + steal_calc + guest_calc + guest_nice_calc; |
| 313 |
ci->total = ((total - idle_calc) / (double)total) * 100.; |
| 314 |
|
| 315 |
ci->last_user = user; |
| 316 |
ci->last_nice = nice; |
| 317 |
ci->last_idle = idle; |
| 318 |
ci->last_system = sys; |
| 319 |
ci->last_iowait = iowait; |
| 320 |
ci->last_irq = irq; |
| 321 |
ci->last_softirq = softirq; |
| 322 |
ci->last_steal = steal; |
| 323 |
ci->last_guest = guest; |
| 324 |
ci->last_guest_nice = guest_nice; |
| 325 |
} |
| 326 |
|
| 327 |
/* Publish counters to the capture file */ |
| 328 |
for (guint i = 0; i < n_cpu; i++) |
| 329 |
{ |
| 330 |
const CpuInfo *ci = &g_array_index (cpu_info, CpuInfo, i); |
| 331 |
CpuFreq *cf = &g_array_index (freq_info, CpuFreq, i); |
| 332 |
DexFuture *freq_future = g_ptr_array_index (futures, i); |
| 333 |
gssize len = dex_await_int64 (dex_ref (freq_future), NULL); |
| 334 |
|
| 335 |
values[i*2].vdbl = ci->total; |
| 336 |
values[i*2+1].vdbl = get_cpu_freq (cf->stat_fd, i, cf->max, cf->buf, len); |
| 337 |
|
| 338 |
total_usage += ci->total; |
| 339 |
} |
| 340 |
|
| 341 |
values[n_cpu*2].vdbl = total_usage / (double)n_cpu; |
| 342 |
sysprof_capture_writer_set_counters (writer, |
| 343 |
SYSPROF_CAPTURE_CURRENT_TIME, |
| 344 |
-1, |
| 345 |
-1, |
| 346 |
ids, |
| 347 |
values, |
| 348 |
n_cpu * 2 + 1); |
| 349 |
|
| 350 |
/* Wait for cancellation or ⅕ second */ |
| 351 |
dex_await (dex_future_first (dex_ref (record->cancellable), |
| 352 |
dex_timeout_new_usec (G_USEC_PER_SEC / 5), |
| 353 |
NULL), |
| 354 |
NULL); |
| 355 |
if (dex_future_get_status (record->cancellable) != DEX_FUTURE_STATUS_PENDING) |
| 356 |
break; |
| 357 |
} |
| 358 |
|
| 359 |
return dex_future_new_for_boolean (TRUE); |
| 360 |
} |
| 361 |
|
| 362 |
static DexFuture * |
| 363 |
sysprof_cpu_usage_record (SysprofInstrument *instrument, |
| 364 |
SysprofRecording *recording, |
| 365 |
GCancellable *cancellable) |
| 366 |
{ |
| 367 |
Record *record; |
| 368 |
|
| 369 |
g_assert (SYSPROF_IS_CPU_USAGE (instrument)); |
| 370 |
g_assert (SYSPROF_IS_RECORDING (recording)); |
| 371 |
g_assert (G_IS_CANCELLABLE (cancellable)); |
| 372 |
|
| 373 |
record = g_new0 (Record, 1); |
| 374 |
record->recording = g_object_ref (recording); |
| 375 |
record->cancellable = dex_cancellable_new_from_cancellable (cancellable); |
| 376 |
|
| 377 |
return dex_scheduler_spawn (NULL, 0, |
| 378 |
sysprof_cpu_usage_record_fiber, |
| 379 |
record, |
| 380 |
record_free); |
| 381 |
} |
| 382 |
|
| 383 |
static void |
| 384 |
sysprof_cpu_usage_class_init (SysprofCpuUsageClass *klass) |
| 385 |
{ |
| 386 |
SysprofInstrumentClass *instrument_class = SYSPROF_INSTRUMENT_CLASS (klass); |
| 387 |
|
| 388 |
instrument_class->record = sysprof_cpu_usage_record; |
| 389 |
} |
| 390 |
|
| 391 |
static void |
| 392 |
sysprof_cpu_usage_init (SysprofCpuUsage *self) |
| 393 |
{ |
| 394 |
} |
| 395 |
|
| 396 |
SysprofInstrument * |
| 397 |
sysprof_cpu_usage_new (void) |
| 398 |
{ |
| 399 |
return g_object_new (SYSPROF_TYPE_CPU_USAGE, NULL); |
| 400 |
} |