| 1 |
/* sysprof-disk-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 <fcntl.h> |
| 24 |
|
| 25 |
#include <glib/gstdio.h> |
| 26 |
|
| 27 |
#include "sysprof-disk-usage.h" |
| 28 |
#include "sysprof-instrument-private.h" |
| 29 |
#include "sysprof-recording-private.h" |
| 30 |
|
| 31 |
#include "line-reader-private.h" |
| 32 |
|
| 33 |
#define ADD_FROM_CHAR(v, c) (((v)*10L)+((c)-'0')) |
| 34 |
|
| 35 |
struct _SysprofDiskUsage |
| 36 |
{ |
| 37 |
SysprofInstrument parent_instance; |
| 38 |
}; |
| 39 |
|
| 40 |
struct _SysprofDiskUsageClass |
| 41 |
{ |
| 42 |
SysprofInstrumentClass parent_class; |
| 43 |
}; |
| 44 |
|
| 45 |
typedef struct _DiskUsage |
| 46 |
{ |
| 47 |
/* Counter IDs */ |
| 48 |
guint reads_total_id; |
| 49 |
guint writes_total_id; |
| 50 |
|
| 51 |
/* Index where values are stored */ |
| 52 |
guint values_at; |
| 53 |
|
| 54 |
char device[32]; |
| 55 |
|
| 56 |
gint64 reads_total; |
| 57 |
gint64 reads_merged; |
| 58 |
gint64 reads_sectors; |
| 59 |
gint64 reads_msec; |
| 60 |
gint64 writes_total; |
| 61 |
gint64 writes_merged; |
| 62 |
gint64 writes_sectors; |
| 63 |
gint64 writes_msec; |
| 64 |
gint64 iops_active; |
| 65 |
gint64 iops_msec; |
| 66 |
gint64 iops_msec_weighted; |
| 67 |
} DiskUsage; |
| 68 |
|
| 69 |
typedef enum _Column |
| 70 |
{ |
| 71 |
/* -2 */ COLUMN_MAJOR, |
| 72 |
/* -1 */ COLUMN_MINOR, |
| 73 |
/* 0 */ COLUMN_NAME, |
| 74 |
/* 1 */ COLUMN_READS_TOTAL, |
| 75 |
/* 2 */ COLUMN_READS_MERGED, |
| 76 |
/* 3 */ COLUMN_READS_SECTORS, |
| 77 |
/* 4 */ COLUMN_READS_MSEC, |
| 78 |
/* 5 */ COLUMN_WRITES_TOTAL, |
| 79 |
/* 6 */ COLUMN_WRITES_MERGED, |
| 80 |
/* 7 */ COLUMN_WRITES_SECTORS, |
| 81 |
/* 8 */ COLUMN_WRITES_MSEC, |
| 82 |
/* 9 */ COLUMN_IOPS_ACTIVE, |
| 83 |
/* 10 */ COLUMN_IOPS_MSEC, |
| 84 |
/* 11 */ COLUMN_IOPS_MSEC_WEIGHTED, |
| 85 |
} Column; |
| 86 |
|
| 87 |
G_DEFINE_FINAL_TYPE (SysprofDiskUsage, sysprof_disk_usage, SYSPROF_TYPE_INSTRUMENT) |
| 88 |
|
| 89 |
typedef struct _Record |
| 90 |
{ |
| 91 |
SysprofRecording *recording; |
| 92 |
DexFuture *cancellable; |
| 93 |
GArray *devices; |
| 94 |
GArray *ids; |
| 95 |
GArray *values; |
| 96 |
} Record; |
| 97 |
|
| 98 |
static void |
| 99 |
record_free (gpointer data) |
| 100 |
{ |
| 101 |
Record *record = data; |
| 102 |
|
| 103 |
g_clear_object (&record->recording); |
| 104 |
g_clear_pointer (&record->devices, g_array_unref); |
| 105 |
g_clear_pointer (&record->ids, g_array_unref); |
| 106 |
g_clear_pointer (&record->values, g_array_unref); |
| 107 |
dex_clear (&record->cancellable); |
| 108 |
g_free (record); |
| 109 |
} |
| 110 |
|
| 111 |
static DiskUsage * |
| 112 |
register_counters_by_name (Record *record, |
| 113 |
const char *name) |
| 114 |
{ |
| 115 |
static const SysprofCaptureCounterValue zeroval = {0}; |
| 116 |
SysprofCaptureCounter ctr[2] = {0}; |
| 117 |
SysprofCaptureWriter *writer; |
| 118 |
DiskUsage ds = {0}; |
| 119 |
|
| 120 |
g_assert (record != NULL); |
| 121 |
g_assert (name != NULL); |
| 122 |
|
| 123 |
writer = _sysprof_recording_writer (record->recording); |
| 124 |
|
| 125 |
ds.values_at = record->ids->len; |
| 126 |
ds.reads_total_id = sysprof_capture_writer_request_counter (writer, 1); |
| 127 |
ds.writes_total_id = sysprof_capture_writer_request_counter (writer, 1); |
| 128 |
|
| 129 |
g_strlcpy (ds.device, name, sizeof ds.device); |
| 130 |
|
| 131 |
g_strlcpy (ctr[0].category, "Disk", sizeof ctr[0].category); |
| 132 |
g_snprintf (ctr[0].name, sizeof ctr[0].name, "Total Reads (%s)", name); |
| 133 |
g_strlcpy (ctr[0].description, name, sizeof ctr[0].description); |
| 134 |
ctr[0].id = ds.reads_total_id; |
| 135 |
ctr[0].type = SYSPROF_CAPTURE_COUNTER_INT64; |
| 136 |
ctr[0].value.v64 = 0; |
| 137 |
|
| 138 |
g_strlcpy (ctr[1].category, "Disk", sizeof ctr[1].category); |
| 139 |
g_snprintf (ctr[1].name, sizeof ctr[1].name, "Total Writes (%s)", name); |
| 140 |
g_strlcpy (ctr[1].description, name, sizeof ctr[1].description); |
| 141 |
ctr[1].id = ds.writes_total_id; |
| 142 |
ctr[1].type = SYSPROF_CAPTURE_COUNTER_INT64; |
| 143 |
ctr[1].value.v64 = 0; |
| 144 |
|
| 145 |
sysprof_capture_writer_define_counters (writer, |
| 146 |
SYSPROF_CAPTURE_CURRENT_TIME, |
| 147 |
-1, |
| 148 |
-1, |
| 149 |
ctr, |
| 150 |
G_N_ELEMENTS (ctr)); |
| 151 |
|
| 152 |
g_array_append_val (record->devices, ds); |
| 153 |
g_array_append_val (record->ids, ds.reads_total_id); |
| 154 |
g_array_append_val (record->ids, ds.writes_total_id); |
| 155 |
g_array_append_val (record->values, zeroval); |
| 156 |
g_array_append_val (record->values, zeroval); |
| 157 |
|
| 158 |
return &g_array_index (record->devices, DiskUsage, record->devices->len-1); |
| 159 |
} |
| 160 |
|
| 161 |
static DiskUsage * |
| 162 |
find_device_by_name (Record *record, |
| 163 |
const char *name) |
| 164 |
{ |
| 165 |
g_assert (record != NULL); |
| 166 |
g_assert (record->devices != NULL); |
| 167 |
g_assert (name != NULL); |
| 168 |
|
| 169 |
for (guint i = 0; i < record->devices->len; i++) |
| 170 |
{ |
| 171 |
DiskUsage *ds = &g_array_index (record->devices, DiskUsage, i); |
| 172 |
|
| 173 |
if (strcmp (name, ds->device) == 0) |
| 174 |
return ds; |
| 175 |
} |
| 176 |
|
| 177 |
return NULL; |
| 178 |
} |
| 179 |
|
| 180 |
static DexFuture * |
| 181 |
sysprof_disk_usage_record_fiber (gpointer user_data) |
| 182 |
{ |
| 183 |
g_autoptr(GByteArray) buf = NULL; |
| 184 |
Record *record = user_data; |
| 185 |
SysprofCaptureWriter *writer; |
| 186 |
g_autofd int stat_fd = -1; |
| 187 |
LineReader reader; |
| 188 |
DiskUsage *combined; |
| 189 |
gint64 combined_reads_total = 0; |
| 190 |
gint64 combined_writes_total = 0; |
| 191 |
gboolean skip_publish = TRUE; |
| 192 |
|
| 193 |
g_assert (record != NULL); |
| 194 |
g_assert (SYSPROF_IS_RECORDING (record->recording)); |
| 195 |
g_assert (DEX_IS_CANCELLABLE (record->cancellable)); |
| 196 |
|
| 197 |
buf = g_byte_array_new (); |
| 198 |
g_byte_array_set_size (buf, 4096*4); |
| 199 |
|
| 200 |
if (-1 == (stat_fd = open ("/proc/diskstats", O_RDONLY|O_CLOEXEC))) |
| 201 |
return dex_future_new_for_errno (errno); |
| 202 |
|
| 203 |
writer = _sysprof_recording_writer (record->recording); |
| 204 |
|
| 205 |
register_counters_by_name (record, "Combined"); |
| 206 |
|
| 207 |
for (;;) |
| 208 |
{ |
| 209 |
g_autoptr(DexFuture) read_future = NULL; |
| 210 |
gssize n_read; |
| 211 |
char *line; |
| 212 |
gsize line_len; |
| 213 |
|
| 214 |
/* Read a new copy of our diskstats or bail from our |
| 215 |
* recording loop. If cancellation future rejects, then |
| 216 |
* we also break out of our recording loop. |
| 217 |
*/ |
| 218 |
read_future = dex_aio_read (NULL, stat_fd, buf->data, buf->len-1, 0); |
| 219 |
if (!dex_await (dex_future_first (dex_ref (record->cancellable), |
| 220 |
dex_ref (read_future), |
| 221 |
NULL), |
| 222 |
NULL)) |
| 223 |
break; |
| 224 |
|
| 225 |
n_read = dex_await_int64 (dex_ref (read_future), NULL); |
| 226 |
if (n_read < 0) |
| 227 |
break; |
| 228 |
|
| 229 |
line_reader_init (&reader, (char *)buf->data, n_read); |
| 230 |
while ((line = line_reader_next (&reader, &line_len))) |
| 231 |
{ |
| 232 |
DiskUsage ds = {0}; |
| 233 |
DiskUsage *found; |
| 234 |
gint64 dummy = 0; |
| 235 |
int column = COLUMN_MAJOR; |
| 236 |
|
| 237 |
line[line_len] = 0; |
| 238 |
|
| 239 |
/* Skip past initial space */ |
| 240 |
while (g_ascii_isspace (*line)) |
| 241 |
line++; |
| 242 |
|
| 243 |
for (const char *ptr = line; *ptr; ptr++) |
| 244 |
{ |
| 245 |
char ch; |
| 246 |
|
| 247 |
/* Skip past space and advance to next column */ |
| 248 |
if (g_ascii_isspace (*ptr)) |
| 249 |
{ |
| 250 |
while (g_ascii_isspace (*ptr)) |
| 251 |
ptr++; |
| 252 |
column++; |
| 253 |
} |
| 254 |
|
| 255 |
ch = *ptr; |
| 256 |
|
| 257 |
switch (column) |
| 258 |
{ |
| 259 |
case COLUMN_MAJOR: |
| 260 |
case COLUMN_MINOR: |
| 261 |
default: |
| 262 |
dummy = ADD_FROM_CHAR (dummy, ch); |
| 263 |
break; |
| 264 |
|
| 265 |
case COLUMN_NAME: |
| 266 |
{ |
| 267 |
guint j; |
| 268 |
|
| 269 |
for (j = 0; j < sizeof ds.device && ds.device[j] != 0; j++) { /* Do Nothing */ } |
| 270 |
if (j < sizeof ds.device) |
| 271 |
ds.device[j] = ch; |
| 272 |
ds.device[sizeof ds.device - 1] = 0; |
| 273 |
|
| 274 |
break; |
| 275 |
} |
| 276 |
|
| 277 |
case COLUMN_READS_TOTAL: |
| 278 |
ds.reads_total = ADD_FROM_CHAR (ds.reads_total, ch); |
| 279 |
break; |
| 280 |
|
| 281 |
case COLUMN_READS_MERGED: |
| 282 |
ds.reads_merged = ADD_FROM_CHAR (ds.reads_merged, ch); |
| 283 |
break; |
| 284 |
|
| 285 |
case COLUMN_READS_SECTORS: |
| 286 |
ds.reads_sectors = ADD_FROM_CHAR (ds.reads_sectors, ch); |
| 287 |
break; |
| 288 |
|
| 289 |
case COLUMN_READS_MSEC: |
| 290 |
ds.reads_msec = ADD_FROM_CHAR (ds.reads_msec, ch); |
| 291 |
break; |
| 292 |
|
| 293 |
case COLUMN_WRITES_TOTAL: |
| 294 |
ds.writes_total = ADD_FROM_CHAR (ds.writes_total, ch); |
| 295 |
break; |
| 296 |
|
| 297 |
case COLUMN_WRITES_MERGED: |
| 298 |
ds.writes_merged = ADD_FROM_CHAR (ds.writes_merged, ch); |
| 299 |
break; |
| 300 |
|
| 301 |
case COLUMN_WRITES_SECTORS: |
| 302 |
ds.writes_sectors = ADD_FROM_CHAR (ds.writes_sectors, ch); |
| 303 |
break; |
| 304 |
|
| 305 |
case COLUMN_WRITES_MSEC: |
| 306 |
ds.writes_msec = ADD_FROM_CHAR (ds.writes_msec, ch); |
| 307 |
break; |
| 308 |
|
| 309 |
case COLUMN_IOPS_ACTIVE: |
| 310 |
ds.iops_active = ADD_FROM_CHAR (ds.iops_active, ch); |
| 311 |
break; |
| 312 |
|
| 313 |
case COLUMN_IOPS_MSEC: |
| 314 |
ds.iops_msec = ADD_FROM_CHAR (ds.iops_msec, ch); |
| 315 |
break; |
| 316 |
|
| 317 |
case COLUMN_IOPS_MSEC_WEIGHTED: |
| 318 |
ds.iops_msec_weighted = ADD_FROM_CHAR (ds.iops_msec_weighted, ch); |
| 319 |
break; |
| 320 |
} |
| 321 |
} |
| 322 |
|
| 323 |
g_strstrip (ds.device); |
| 324 |
|
| 325 |
if (ds.device[0]) |
| 326 |
{ |
| 327 |
guint p; |
| 328 |
gint64 reads_total; |
| 329 |
gint64 writes_total; |
| 330 |
|
| 331 |
if (!(found = find_device_by_name (record, ds.device))) |
| 332 |
found = register_counters_by_name (record, ds.device); |
| 333 |
|
| 334 |
/* Calculate new value, based on diff from previous */ |
| 335 |
reads_total = ds.reads_total - found->reads_total; |
| 336 |
writes_total = ds.writes_total - found->writes_total; |
| 337 |
|
| 338 |
/* Update value for publishing */ |
| 339 |
p = found->values_at; |
| 340 |
g_array_index (record->values, SysprofCaptureCounterValue, p).v64 = reads_total; |
| 341 |
g_array_index (record->values, SysprofCaptureCounterValue, p+1).v64 = writes_total; |
| 342 |
|
| 343 |
/* Update combined values */ |
| 344 |
combined_reads_total += reads_total; |
| 345 |
combined_writes_total += writes_total; |
| 346 |
|
| 347 |
/* Save current value for diff on next poll */ |
| 348 |
found->reads_total = ds.reads_total; |
| 349 |
found->writes_total = ds.writes_total; |
| 350 |
} |
| 351 |
} |
| 352 |
|
| 353 |
if ((combined = find_device_by_name (record, "Combined"))) |
| 354 |
{ |
| 355 |
/* TODO: It would be nice to not double count disk ops multiple |
| 356 |
* times based on the parition, etc. |
| 357 |
* |
| 358 |
* For example: nvme0n1 nvme0n1p1 nvme0n1p2 nvme0n1p3 may get |
| 359 |
* accounted multiple times even though they are all nvme0n1. |
| 360 |
* |
| 361 |
* The other option, is to just not do "Combined" counters. |
| 362 |
*/ |
| 363 |
|
| 364 |
g_array_index (record->values, |
| 365 |
SysprofCaptureCounterValue, |
| 366 |
combined->values_at).v64 |
| 367 |
= combined_reads_total - combined->reads_total; |
| 368 |
g_array_index (record->values, |
| 369 |
SysprofCaptureCounterValue, |
| 370 |
combined->values_at+1).v64 |
| 371 |
= combined_writes_total - combined->writes_total; |
| 372 |
|
| 373 |
combined->reads_total = combined_reads_total; |
| 374 |
combined->writes_total = combined_writes_total; |
| 375 |
} |
| 376 |
|
| 377 |
g_assert (record->ids->len == record->values->len); |
| 378 |
|
| 379 |
if (skip_publish) |
| 380 |
skip_publish = FALSE; |
| 381 |
else |
| 382 |
sysprof_capture_writer_set_counters (writer, |
| 383 |
SYSPROF_CAPTURE_CURRENT_TIME, |
| 384 |
-1, |
| 385 |
-1, |
| 386 |
(const guint *)(gpointer)record->ids->data, |
| 387 |
(const SysprofCaptureCounterValue *)(gpointer)record->values->data, |
| 388 |
record->ids->len); |
| 389 |
|
| 390 |
dex_await (dex_future_first (dex_ref (record->cancellable), |
| 391 |
dex_timeout_new_usec (G_USEC_PER_SEC / 2), |
| 392 |
NULL), |
| 393 |
NULL); |
| 394 |
if (dex_future_get_status (record->cancellable) != DEX_FUTURE_STATUS_PENDING) |
| 395 |
break; |
| 396 |
} |
| 397 |
|
| 398 |
return dex_future_new_for_boolean (TRUE); |
| 399 |
} |
| 400 |
|
| 401 |
static DexFuture * |
| 402 |
sysprof_disk_usage_record (SysprofInstrument *instrument, |
| 403 |
SysprofRecording *recording, |
| 404 |
GCancellable *cancellable) |
| 405 |
{ |
| 406 |
Record *record; |
| 407 |
|
| 408 |
g_assert (SYSPROF_IS_INSTRUMENT (instrument)); |
| 409 |
g_assert (SYSPROF_IS_RECORDING (recording)); |
| 410 |
g_assert (G_IS_CANCELLABLE (cancellable)); |
| 411 |
|
| 412 |
record = g_new0 (Record, 1); |
| 413 |
record->recording = g_object_ref (recording); |
| 414 |
record->cancellable = dex_cancellable_new_from_cancellable (cancellable); |
| 415 |
record->devices = g_array_new (FALSE, FALSE, sizeof (DiskUsage)); |
| 416 |
record->ids = g_array_new (FALSE, FALSE, sizeof (guint)); |
| 417 |
record->values = g_array_new (FALSE, FALSE, sizeof (SysprofCaptureCounterValue)); |
| 418 |
|
| 419 |
return dex_scheduler_spawn (NULL, 0, |
| 420 |
sysprof_disk_usage_record_fiber, |
| 421 |
record, |
| 422 |
record_free); |
| 423 |
} |
| 424 |
|
| 425 |
static void |
| 426 |
sysprof_disk_usage_class_init (SysprofDiskUsageClass *klass) |
| 427 |
{ |
| 428 |
SysprofInstrumentClass *instrument_class = SYSPROF_INSTRUMENT_CLASS (klass); |
| 429 |
|
| 430 |
instrument_class->record = sysprof_disk_usage_record; |
| 431 |
} |
| 432 |
|
| 433 |
static void |
| 434 |
sysprof_disk_usage_init (SysprofDiskUsage *self) |
| 435 |
{ |
| 436 |
} |
| 437 |
|
| 438 |
SysprofInstrument * |
| 439 |
sysprof_disk_usage_new (void) |
| 440 |
{ |
| 441 |
return g_object_new (SYSPROF_TYPE_DISK_USAGE, NULL); |
| 442 |
} |