| 1 |
/* sysprof-speedtrack-collector.c |
| 2 |
* |
| 3 |
* Copyright 2020 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 <dlfcn.h> |
| 24 |
#include <fcntl.h> |
| 25 |
#include <glib.h> |
| 26 |
#include <stdlib.h> |
| 27 |
#include <sys/mman.h> |
| 28 |
#include <sys/stat.h> |
| 29 |
#include <sys/syscall.h> |
| 30 |
#include <sys/types.h> |
| 31 |
#include <sysprof-capture.h> |
| 32 |
#include <unistd.h> |
| 33 |
|
| 34 |
#include "backtrace-helper.h" |
| 35 |
|
| 36 |
static void hook_func (void **addr, const char *name); |
| 37 |
static int hook_open (const char *filename, int flags, ...); |
| 38 |
static int hook_close (int fd); |
| 39 |
static int hook_fsync (int fd); |
| 40 |
static int hook_fdatasync (int fd); |
| 41 |
static int hook_msync (void *addr, size_t length, int flags); |
| 42 |
static void hook_sync (void); |
| 43 |
static int hook_syncfs (int fd); |
| 44 |
static ssize_t hook_read (int fd, void *buf, size_t nbyte); |
| 45 |
static ssize_t hook_write (int fd, const void *buf, size_t nbyte); |
| 46 |
static gboolean hook_iteration (GMainContext *context, gboolean may_block); |
| 47 |
|
| 48 |
static __thread gboolean rec_guard; |
| 49 |
static int (*real_open) (const char *filename, int flags, ...) = hook_open; |
| 50 |
static int (*real_close) (int fd) = hook_close; |
| 51 |
static int (*real_fsync) (int fd) = hook_fsync; |
| 52 |
static int (*real_fdatasync) (int fd) = hook_fdatasync; |
| 53 |
static int (*real_msync) (void *addr, size_t length, int flags) = hook_msync; |
| 54 |
static ssize_t (*real_read) (int fd, void *buf, size_t nbyte) = hook_read; |
| 55 |
static ssize_t (*real_write) (int fd, const void *buf, size_t nbyte) = hook_write; |
| 56 |
static void (*real_sync) (void) = hook_sync; |
| 57 |
static int (*real_syncfs) (int fd) = hook_syncfs; |
| 58 |
static gboolean (*real_iteration) (GMainContext *context, gboolean may_block) = hook_iteration; |
| 59 |
|
| 60 |
static inline gboolean |
| 61 |
is_capturing (void) |
| 62 |
{ |
| 63 |
static __thread int tid = 0; |
| 64 |
static int pid = 0; |
| 65 |
|
| 66 |
if (rec_guard) |
| 67 |
return FALSE; |
| 68 |
|
| 69 |
if G_UNLIKELY (tid == 0) |
| 70 |
{ |
| 71 |
#if defined(__linux__) |
| 72 |
tid = syscall (__NR_gettid, 0); |
| 73 |
#elif defined(__APPLE__) |
| 74 |
uint64_t threadid; |
| 75 |
pthread_threadid_np (NULL, &threadid); |
| 76 |
tid = threadid; |
| 77 |
#endif |
| 78 |
} |
| 79 |
|
| 80 |
if G_UNLIKELY (pid == 0) |
| 81 |
pid = getpid (); |
| 82 |
|
| 83 |
return tid == pid; |
| 84 |
} |
| 85 |
|
| 86 |
static void |
| 87 |
hook_func (void **addr, |
| 88 |
const char *name) |
| 89 |
{ |
| 90 |
static GRecMutex m; |
| 91 |
static gboolean did_init; |
| 92 |
|
| 93 |
g_rec_mutex_lock (&m); |
| 94 |
if (!did_init) |
| 95 |
{ |
| 96 |
did_init = TRUE; |
| 97 |
backtrace_init (); |
| 98 |
} |
| 99 |
g_rec_mutex_unlock (&m); |
| 100 |
|
| 101 |
*addr = dlsym (RTLD_NEXT, name); |
| 102 |
} |
| 103 |
|
| 104 |
int |
| 105 |
open (const char *filename, |
| 106 |
int flags, |
| 107 |
...) |
| 108 |
{ |
| 109 |
va_list args; |
| 110 |
mode_t mode; |
| 111 |
|
| 112 |
va_start (args, flags); |
| 113 |
mode = va_arg (args, int); |
| 114 |
va_end (args); |
| 115 |
|
| 116 |
if (is_capturing ()) |
| 117 |
{ |
| 118 |
gchar str[1024]; |
| 119 |
gint64 begin; |
| 120 |
gint64 end; |
| 121 |
int ret; |
| 122 |
|
| 123 |
rec_guard = TRUE; |
| 124 |
|
| 125 |
begin = SYSPROF_CAPTURE_CURRENT_TIME; |
| 126 |
ret = real_open (filename, flags, mode); |
| 127 |
end = SYSPROF_CAPTURE_CURRENT_TIME; |
| 128 |
|
| 129 |
g_snprintf (str, sizeof str, |
| 130 |
"flags = 0x%x, mode = 0%o, filename = %s => %d", |
| 131 |
flags, mode, filename, ret); |
| 132 |
sysprof_collector_sample (backtrace_func, NULL); |
| 133 |
sysprof_collector_mark (begin, end - begin, "speedtrack", "open", str); |
| 134 |
|
| 135 |
rec_guard = FALSE; |
| 136 |
|
| 137 |
return ret; |
| 138 |
} |
| 139 |
|
| 140 |
return real_open (filename, flags, mode); |
| 141 |
} |
| 142 |
|
| 143 |
static int |
| 144 |
hook_open (const char *filename, int flags, ...) |
| 145 |
{ |
| 146 |
va_list args; |
| 147 |
mode_t mode; |
| 148 |
|
| 149 |
va_start (args, flags); |
| 150 |
mode = va_arg (args, int); |
| 151 |
va_end (args); |
| 152 |
|
| 153 |
hook_func ((void **)&real_open, "open"); |
| 154 |
|
| 155 |
return real_open (filename, flags, mode); |
| 156 |
} |
| 157 |
|
| 158 |
int |
| 159 |
close (int fd) |
| 160 |
{ |
| 161 |
if (is_capturing ()) |
| 162 |
{ |
| 163 |
gint64 begin; |
| 164 |
gint64 end; |
| 165 |
gchar fdstr[32]; |
| 166 |
int ret; |
| 167 |
|
| 168 |
rec_guard = TRUE; |
| 169 |
|
| 170 |
begin = SYSPROF_CAPTURE_CURRENT_TIME; |
| 171 |
ret = real_close (fd); |
| 172 |
end = SYSPROF_CAPTURE_CURRENT_TIME; |
| 173 |
|
| 174 |
g_snprintf (fdstr, sizeof fdstr, "fd = %d => %d", fd, ret); |
| 175 |
sysprof_collector_sample (backtrace_func, NULL); |
| 176 |
sysprof_collector_mark (begin, end - begin, "speedtrack", "close", fdstr); |
| 177 |
|
| 178 |
rec_guard = FALSE; |
| 179 |
|
| 180 |
return ret; |
| 181 |
} |
| 182 |
|
| 183 |
return real_close (fd); |
| 184 |
} |
| 185 |
|
| 186 |
static int |
| 187 |
hook_close (int fd) |
| 188 |
{ |
| 189 |
hook_func ((void **)&real_close, "close"); |
| 190 |
return real_close (fd); |
| 191 |
} |
| 192 |
|
| 193 |
int |
| 194 |
fsync (int fd) |
| 195 |
{ |
| 196 |
if (is_capturing ()) |
| 197 |
{ |
| 198 |
gint64 begin; |
| 199 |
gint64 end; |
| 200 |
gchar fdstr[32]; |
| 201 |
int ret; |
| 202 |
|
| 203 |
rec_guard = TRUE; |
| 204 |
|
| 205 |
begin = SYSPROF_CAPTURE_CURRENT_TIME; |
| 206 |
ret = real_fsync (fd); |
| 207 |
end = SYSPROF_CAPTURE_CURRENT_TIME; |
| 208 |
|
| 209 |
g_snprintf (fdstr, sizeof fdstr, "fd = %d => %d", fd, ret); |
| 210 |
sysprof_collector_sample (backtrace_func, NULL); |
| 211 |
sysprof_collector_mark (begin, end - begin, "speedtrack", "fsync", fdstr); |
| 212 |
|
| 213 |
rec_guard = FALSE; |
| 214 |
|
| 215 |
return ret; |
| 216 |
} |
| 217 |
|
| 218 |
return real_fsync (fd); |
| 219 |
} |
| 220 |
|
| 221 |
static int |
| 222 |
hook_fsync (int fd) |
| 223 |
{ |
| 224 |
hook_func ((void **)&real_fsync, "fsync"); |
| 225 |
return real_fsync (fd); |
| 226 |
} |
| 227 |
|
| 228 |
int |
| 229 |
fdatasync (int fd) |
| 230 |
{ |
| 231 |
if (is_capturing ()) |
| 232 |
{ |
| 233 |
gint64 begin; |
| 234 |
gint64 end; |
| 235 |
gchar fdstr[32]; |
| 236 |
int ret; |
| 237 |
|
| 238 |
rec_guard = TRUE; |
| 239 |
|
| 240 |
begin = SYSPROF_CAPTURE_CURRENT_TIME; |
| 241 |
ret = real_fdatasync (fd); |
| 242 |
end = SYSPROF_CAPTURE_CURRENT_TIME; |
| 243 |
|
| 244 |
g_snprintf (fdstr, sizeof fdstr, "fd = %d => %d", fd, ret); |
| 245 |
sysprof_collector_sample (backtrace_func, NULL); |
| 246 |
sysprof_collector_mark (begin, end - begin, "speedtrack", "fdatasync", fdstr); |
| 247 |
|
| 248 |
rec_guard = FALSE; |
| 249 |
|
| 250 |
return ret; |
| 251 |
} |
| 252 |
|
| 253 |
return real_fdatasync (fd); |
| 254 |
} |
| 255 |
|
| 256 |
static int |
| 257 |
hook_fdatasync (int fd) |
| 258 |
{ |
| 259 |
hook_func ((void **)&real_fdatasync, "fdatasync"); |
| 260 |
return real_fdatasync (fd); |
| 261 |
} |
| 262 |
|
| 263 |
int |
| 264 |
msync (void *addr, |
| 265 |
size_t length, |
| 266 |
int flags) |
| 267 |
{ |
| 268 |
if (is_capturing ()) |
| 269 |
{ |
| 270 |
gint64 begin; |
| 271 |
gint64 end; |
| 272 |
gchar str[64]; |
| 273 |
int ret; |
| 274 |
|
| 275 |
rec_guard = TRUE; |
| 276 |
|
| 277 |
begin = SYSPROF_CAPTURE_CURRENT_TIME; |
| 278 |
ret = real_msync (addr, length, flags); |
| 279 |
end = SYSPROF_CAPTURE_CURRENT_TIME; |
| 280 |
|
| 281 |
g_snprintf (str, sizeof str, "addr = %p, length = %"G_GSIZE_FORMAT", flags = %d => %d", |
| 282 |
addr, length, flags, ret); |
| 283 |
sysprof_collector_sample (backtrace_func, NULL); |
| 284 |
sysprof_collector_mark (begin, end - begin, "speedtrack", "msync", str); |
| 285 |
|
| 286 |
rec_guard = FALSE; |
| 287 |
|
| 288 |
return ret; |
| 289 |
} |
| 290 |
|
| 291 |
return real_msync (addr, length, flags); |
| 292 |
} |
| 293 |
|
| 294 |
static int |
| 295 |
hook_msync (void *addr, |
| 296 |
size_t length, |
| 297 |
int flags) |
| 298 |
{ |
| 299 |
hook_func ((void **)&real_msync, "msync"); |
| 300 |
return real_msync (addr, length, flags); |
| 301 |
} |
| 302 |
|
| 303 |
ssize_t |
| 304 |
read (int fd, |
| 305 |
void *buf, |
| 306 |
size_t nbyte) |
| 307 |
{ |
| 308 |
if (is_capturing ()) |
| 309 |
{ |
| 310 |
gint64 begin; |
| 311 |
gint64 end; |
| 312 |
gchar str[64]; |
| 313 |
ssize_t ret; |
| 314 |
|
| 315 |
rec_guard = TRUE; |
| 316 |
|
| 317 |
begin = SYSPROF_CAPTURE_CURRENT_TIME; |
| 318 |
ret = real_read (fd, buf, nbyte); |
| 319 |
end = SYSPROF_CAPTURE_CURRENT_TIME; |
| 320 |
|
| 321 |
g_snprintf (str, sizeof str, "fd = %d, buf = %p, nbyte = %"G_GSIZE_FORMAT" => %"G_GSSIZE_FORMAT, |
| 322 |
fd, buf, nbyte, ret); |
| 323 |
sysprof_collector_sample (backtrace_func, NULL); |
| 324 |
sysprof_collector_mark (begin, end - begin, "speedtrack", "read", str); |
| 325 |
|
| 326 |
rec_guard = FALSE; |
| 327 |
|
| 328 |
return ret; |
| 329 |
} |
| 330 |
|
| 331 |
return real_read (fd, buf, nbyte); |
| 332 |
} |
| 333 |
|
| 334 |
static ssize_t |
| 335 |
hook_read (int fd, |
| 336 |
void *buf, |
| 337 |
size_t nbyte) |
| 338 |
{ |
| 339 |
hook_func ((void **)&real_read, "read"); |
| 340 |
return real_read (fd, buf, nbyte); |
| 341 |
} |
| 342 |
|
| 343 |
ssize_t |
| 344 |
write (int fd, |
| 345 |
const void *buf, |
| 346 |
size_t nbyte) |
| 347 |
{ |
| 348 |
if (is_capturing ()) |
| 349 |
{ |
| 350 |
gint64 begin; |
| 351 |
gint64 end; |
| 352 |
gchar str[64]; |
| 353 |
ssize_t ret; |
| 354 |
|
| 355 |
rec_guard = TRUE; |
| 356 |
|
| 357 |
begin = SYSPROF_CAPTURE_CURRENT_TIME; |
| 358 |
ret = real_write (fd, buf, nbyte); |
| 359 |
end = SYSPROF_CAPTURE_CURRENT_TIME; |
| 360 |
|
| 361 |
g_snprintf (str, sizeof str, "fd = %d, buf = %p, nbyte = %"G_GSIZE_FORMAT" => %"G_GSSIZE_FORMAT, |
| 362 |
fd, buf, nbyte, ret); |
| 363 |
sysprof_collector_sample (backtrace_func, NULL); |
| 364 |
sysprof_collector_mark (begin, end - begin, "speedtrack", "write", str); |
| 365 |
|
| 366 |
rec_guard = FALSE; |
| 367 |
|
| 368 |
return ret; |
| 369 |
} |
| 370 |
|
| 371 |
return real_write (fd, buf, nbyte); |
| 372 |
} |
| 373 |
|
| 374 |
static ssize_t |
| 375 |
hook_write (int fd, |
| 376 |
const void *buf, |
| 377 |
size_t nbyte) |
| 378 |
{ |
| 379 |
hook_func ((void **)&real_write, "write"); |
| 380 |
return real_write (fd, buf, nbyte); |
| 381 |
} |
| 382 |
|
| 383 |
void |
| 384 |
sync (void) |
| 385 |
{ |
| 386 |
if (is_capturing ()) |
| 387 |
{ |
| 388 |
gint64 begin; |
| 389 |
gint64 end; |
| 390 |
|
| 391 |
rec_guard = TRUE; |
| 392 |
|
| 393 |
begin = SYSPROF_CAPTURE_CURRENT_TIME; |
| 394 |
real_sync (); |
| 395 |
end = SYSPROF_CAPTURE_CURRENT_TIME; |
| 396 |
|
| 397 |
sysprof_collector_sample (backtrace_func, NULL); |
| 398 |
sysprof_collector_mark (begin, end - begin, "speedtrack", "sync", ""); |
| 399 |
|
| 400 |
rec_guard = FALSE; |
| 401 |
|
| 402 |
return; |
| 403 |
} |
| 404 |
|
| 405 |
real_sync (); |
| 406 |
} |
| 407 |
|
| 408 |
static void |
| 409 |
hook_sync (void) |
| 410 |
{ |
| 411 |
hook_func ((void **)&real_sync, "sync"); |
| 412 |
real_sync (); |
| 413 |
} |
| 414 |
|
| 415 |
int |
| 416 |
syncfs (int fd) |
| 417 |
{ |
| 418 |
if (is_capturing ()) |
| 419 |
{ |
| 420 |
gint64 begin; |
| 421 |
gint64 end; |
| 422 |
gchar str[32]; |
| 423 |
int ret; |
| 424 |
|
| 425 |
rec_guard = TRUE; |
| 426 |
|
| 427 |
begin = SYSPROF_CAPTURE_CURRENT_TIME; |
| 428 |
ret = real_syncfs (fd); |
| 429 |
end = SYSPROF_CAPTURE_CURRENT_TIME; |
| 430 |
|
| 431 |
g_snprintf (str, sizeof str, "fd = %d => %d", fd, ret); |
| 432 |
|
| 433 |
sysprof_collector_sample (backtrace_func, NULL); |
| 434 |
sysprof_collector_mark (begin, end - begin, "speedtrack", "syncfs", str); |
| 435 |
|
| 436 |
rec_guard = FALSE; |
| 437 |
|
| 438 |
return ret; |
| 439 |
} |
| 440 |
|
| 441 |
return real_syncfs (fd); |
| 442 |
} |
| 443 |
|
| 444 |
static int |
| 445 |
hook_syncfs (int fd) |
| 446 |
{ |
| 447 |
hook_func ((void **)&real_syncfs, "syncfs"); |
| 448 |
return real_syncfs (fd); |
| 449 |
} |
| 450 |
|
| 451 |
gboolean |
| 452 |
g_main_context_iteration (GMainContext *context, |
| 453 |
gboolean may_block) |
| 454 |
{ |
| 455 |
if (is_capturing ()) |
| 456 |
{ |
| 457 |
gint64 begin; |
| 458 |
gint64 end; |
| 459 |
gchar str[128]; |
| 460 |
gboolean ret; |
| 461 |
|
| 462 |
begin = SYSPROF_CAPTURE_CURRENT_TIME; |
| 463 |
ret = real_iteration (context, may_block); |
| 464 |
end = SYSPROF_CAPTURE_CURRENT_TIME; |
| 465 |
|
| 466 |
g_snprintf (str, sizeof str, "context = %p, may_block = %d => %d", context, may_block, ret); |
| 467 |
sysprof_collector_mark (begin, end - begin, "speedtrack", "g_main_context_iteration", str); |
| 468 |
|
| 469 |
return ret; |
| 470 |
} |
| 471 |
|
| 472 |
return real_iteration (context, may_block); |
| 473 |
} |
| 474 |
|
| 475 |
static gboolean |
| 476 |
hook_iteration (GMainContext *context, |
| 477 |
gboolean may_block) |
| 478 |
{ |
| 479 |
hook_func ((void **)&real_iteration, "g_main_context_iteration"); |
| 480 |
return real_iteration (context, may_block); |
| 481 |
} |