trace2 / tr2_tgt_perf.con commit trace2: trim whitespace in region messages in perf target format (da4589c)
   1#include "cache.h"
   2#include "config.h"
   3#include "run-command.h"
   4#include "quote.h"
   5#include "version.h"
   6#include "json-writer.h"
   7#include "trace2/tr2_dst.h"
   8#include "trace2/tr2_sid.h"
   9#include "trace2/tr2_sysenv.h"
  10#include "trace2/tr2_tbuf.h"
  11#include "trace2/tr2_tgt.h"
  12#include "trace2/tr2_tls.h"
  13
  14static struct tr2_dst tr2dst_perf = { TR2_SYSENV_PERF, 0, 0, 0 };
  15
  16/*
  17 * Use TR2_SYSENV_PERF_BRIEF to omit the "<time> <file>:<line>"
  18 * fields from each line written to the builtin performance target.
  19 *
  20 * Unit tests may want to use this to help with testing.
  21 */
  22static int tr2env_perf_be_brief;
  23
  24#define TR2FMT_PERF_FL_WIDTH (28)
  25#define TR2FMT_PERF_MAX_EVENT_NAME (12)
  26#define TR2FMT_PERF_REPO_WIDTH (3)
  27#define TR2FMT_PERF_CATEGORY_WIDTH (12)
  28
  29#define TR2_DOTS_BUFFER_SIZE (100)
  30#define TR2_INDENT (2)
  31#define TR2_INDENT_LENGTH(ctx) (((ctx)->nr_open_regions - 1) * TR2_INDENT)
  32
  33static struct strbuf dots = STRBUF_INIT;
  34
  35static int fn_init(void)
  36{
  37        int want = tr2_dst_trace_want(&tr2dst_perf);
  38        int want_brief;
  39        const char *brief;
  40
  41        if (!want)
  42                return want;
  43
  44        strbuf_addchars(&dots, '.', TR2_DOTS_BUFFER_SIZE);
  45
  46        brief = tr2_sysenv_get(TR2_SYSENV_PERF_BRIEF);
  47        if (brief && *brief &&
  48            ((want_brief = git_parse_maybe_bool(brief)) != -1))
  49                tr2env_perf_be_brief = want_brief;
  50
  51        return want;
  52}
  53
  54static void fn_term(void)
  55{
  56        tr2_dst_trace_disable(&tr2dst_perf);
  57
  58        strbuf_release(&dots);
  59}
  60
  61/*
  62 * Format trace line prefix in human-readable classic format for
  63 * the performance target:
  64 *     "[<time> [<file>:<line>] <bar>] <nr_parents> <bar>
  65 *         <thread_name> <bar> <event_name> <bar> [<repo>] <bar>
  66 *         [<elapsed_absolute>] [<elapsed_relative>] <bar>
  67 *         [<category>] <bar> [<dots>] "
  68 */
  69static void perf_fmt_prepare(const char *event_name,
  70                             struct tr2tls_thread_ctx *ctx, const char *file,
  71                             int line, const struct repository *repo,
  72                             uint64_t *p_us_elapsed_absolute,
  73                             uint64_t *p_us_elapsed_relative,
  74                             const char *category, struct strbuf *buf)
  75{
  76        int len;
  77
  78        strbuf_setlen(buf, 0);
  79
  80        if (!tr2env_perf_be_brief) {
  81                struct tr2_tbuf tb_now;
  82                size_t fl_end_col;
  83
  84                tr2_tbuf_local_time(&tb_now);
  85                strbuf_addstr(buf, tb_now.buf);
  86                strbuf_addch(buf, ' ');
  87
  88                fl_end_col = buf->len + TR2FMT_PERF_FL_WIDTH;
  89
  90                if (file && *file) {
  91                        struct strbuf buf_fl = STRBUF_INIT;
  92
  93                        strbuf_addf(&buf_fl, "%s:%d", file, line);
  94
  95                        if (buf_fl.len <= TR2FMT_PERF_FL_WIDTH)
  96                                strbuf_addbuf(buf, &buf_fl);
  97                        else {
  98                                size_t avail = TR2FMT_PERF_FL_WIDTH - 3;
  99                                strbuf_addstr(buf, "...");
 100                                strbuf_add(buf,
 101                                           &buf_fl.buf[buf_fl.len - avail],
 102                                           avail);
 103                        }
 104
 105                        strbuf_release(&buf_fl);
 106                }
 107
 108                while (buf->len < fl_end_col)
 109                        strbuf_addch(buf, ' ');
 110
 111                strbuf_addstr(buf, " | ");
 112        }
 113
 114        strbuf_addf(buf, "d%d | ", tr2_sid_depth());
 115        strbuf_addf(buf, "%-*s | %-*s | ", TR2_MAX_THREAD_NAME,
 116                    ctx->thread_name.buf, TR2FMT_PERF_MAX_EVENT_NAME,
 117                    event_name);
 118
 119        len = buf->len + TR2FMT_PERF_REPO_WIDTH;
 120        if (repo)
 121                strbuf_addf(buf, "r%d ", repo->trace2_repo_id);
 122        while (buf->len < len)
 123                strbuf_addch(buf, ' ');
 124        strbuf_addstr(buf, " | ");
 125
 126        if (p_us_elapsed_absolute)
 127                strbuf_addf(buf, "%9.6f | ",
 128                            ((double)(*p_us_elapsed_absolute)) / 1000000.0);
 129        else
 130                strbuf_addf(buf, "%9s | ", " ");
 131
 132        if (p_us_elapsed_relative)
 133                strbuf_addf(buf, "%9.6f | ",
 134                            ((double)(*p_us_elapsed_relative)) / 1000000.0);
 135        else
 136                strbuf_addf(buf, "%9s | ", " ");
 137
 138        strbuf_addf(buf, "%-*.*s | ", TR2FMT_PERF_CATEGORY_WIDTH,
 139                    TR2FMT_PERF_CATEGORY_WIDTH, (category ? category : ""));
 140
 141        if (ctx->nr_open_regions > 0) {
 142                int len_indent = TR2_INDENT_LENGTH(ctx);
 143                while (len_indent > dots.len) {
 144                        strbuf_addbuf(buf, &dots);
 145                        len_indent -= dots.len;
 146                }
 147                strbuf_addf(buf, "%.*s", len_indent, dots.buf);
 148        }
 149}
 150
 151static void perf_io_write_fl(const char *file, int line, const char *event_name,
 152                             const struct repository *repo,
 153                             uint64_t *p_us_elapsed_absolute,
 154                             uint64_t *p_us_elapsed_relative,
 155                             const char *category,
 156                             const struct strbuf *buf_payload)
 157{
 158        struct tr2tls_thread_ctx *ctx = tr2tls_get_self();
 159        struct strbuf buf_line = STRBUF_INIT;
 160
 161        perf_fmt_prepare(event_name, ctx, file, line, repo,
 162                         p_us_elapsed_absolute, p_us_elapsed_relative, category,
 163                         &buf_line);
 164        strbuf_addbuf(&buf_line, buf_payload);
 165        tr2_dst_write_line(&tr2dst_perf, &buf_line);
 166        strbuf_release(&buf_line);
 167}
 168
 169static void fn_version_fl(const char *file, int line)
 170{
 171        const char *event_name = "version";
 172        struct strbuf buf_payload = STRBUF_INIT;
 173
 174        strbuf_addstr(&buf_payload, git_version_string);
 175
 176        perf_io_write_fl(file, line, event_name, NULL, NULL, NULL, NULL,
 177                         &buf_payload);
 178        strbuf_release(&buf_payload);
 179}
 180
 181static void fn_start_fl(const char *file, int line,
 182                        uint64_t us_elapsed_absolute, const char **argv)
 183{
 184        const char *event_name = "start";
 185        struct strbuf buf_payload = STRBUF_INIT;
 186
 187        sq_quote_argv_pretty(&buf_payload, argv);
 188
 189        perf_io_write_fl(file, line, event_name, NULL, &us_elapsed_absolute,
 190                         NULL, NULL, &buf_payload);
 191        strbuf_release(&buf_payload);
 192}
 193
 194static void fn_exit_fl(const char *file, int line, uint64_t us_elapsed_absolute,
 195                       int code)
 196{
 197        const char *event_name = "exit";
 198        struct strbuf buf_payload = STRBUF_INIT;
 199
 200        strbuf_addf(&buf_payload, "code:%d", code);
 201
 202        perf_io_write_fl(file, line, event_name, NULL, &us_elapsed_absolute,
 203                         NULL, NULL, &buf_payload);
 204        strbuf_release(&buf_payload);
 205}
 206
 207static void fn_signal(uint64_t us_elapsed_absolute, int signo)
 208{
 209        const char *event_name = "signal";
 210        struct strbuf buf_payload = STRBUF_INIT;
 211
 212        strbuf_addf(&buf_payload, "signo:%d", signo);
 213
 214        perf_io_write_fl(__FILE__, __LINE__, event_name, NULL,
 215                         &us_elapsed_absolute, NULL, NULL, &buf_payload);
 216        strbuf_release(&buf_payload);
 217}
 218
 219static void fn_atexit(uint64_t us_elapsed_absolute, int code)
 220{
 221        const char *event_name = "atexit";
 222        struct strbuf buf_payload = STRBUF_INIT;
 223
 224        strbuf_addf(&buf_payload, "code:%d", code);
 225
 226        perf_io_write_fl(__FILE__, __LINE__, event_name, NULL,
 227                         &us_elapsed_absolute, NULL, NULL, &buf_payload);
 228        strbuf_release(&buf_payload);
 229}
 230
 231static void maybe_append_string_va(struct strbuf *buf, const char *fmt,
 232                                   va_list ap)
 233{
 234        if (fmt && *fmt) {
 235                va_list copy_ap;
 236
 237                va_copy(copy_ap, ap);
 238                strbuf_vaddf(buf, fmt, copy_ap);
 239                va_end(copy_ap);
 240                return;
 241        }
 242
 243        if (fmt && *fmt) {
 244                strbuf_addstr(buf, fmt);
 245                return;
 246        }
 247}
 248
 249static void fn_error_va_fl(const char *file, int line, const char *fmt,
 250                           va_list ap)
 251{
 252        const char *event_name = "error";
 253        struct strbuf buf_payload = STRBUF_INIT;
 254
 255        maybe_append_string_va(&buf_payload, fmt, ap);
 256
 257        perf_io_write_fl(file, line, event_name, NULL, NULL, NULL, NULL,
 258                         &buf_payload);
 259        strbuf_release(&buf_payload);
 260}
 261
 262static void fn_command_path_fl(const char *file, int line, const char *pathname)
 263{
 264        const char *event_name = "cmd_path";
 265        struct strbuf buf_payload = STRBUF_INIT;
 266
 267        strbuf_addstr(&buf_payload, pathname);
 268
 269        perf_io_write_fl(file, line, event_name, NULL, NULL, NULL, NULL,
 270                         &buf_payload);
 271        strbuf_release(&buf_payload);
 272}
 273
 274static void fn_command_name_fl(const char *file, int line, const char *name,
 275                               const char *hierarchy)
 276{
 277        const char *event_name = "cmd_name";
 278        struct strbuf buf_payload = STRBUF_INIT;
 279
 280        strbuf_addstr(&buf_payload, name);
 281        if (hierarchy && *hierarchy)
 282                strbuf_addf(&buf_payload, " (%s)", hierarchy);
 283
 284        perf_io_write_fl(file, line, event_name, NULL, NULL, NULL, NULL,
 285                         &buf_payload);
 286        strbuf_release(&buf_payload);
 287}
 288
 289static void fn_command_mode_fl(const char *file, int line, const char *mode)
 290{
 291        const char *event_name = "cmd_mode";
 292        struct strbuf buf_payload = STRBUF_INIT;
 293
 294        strbuf_addstr(&buf_payload, mode);
 295
 296        perf_io_write_fl(file, line, event_name, NULL, NULL, NULL, NULL,
 297                         &buf_payload);
 298        strbuf_release(&buf_payload);
 299}
 300
 301static void fn_alias_fl(const char *file, int line, const char *alias,
 302                        const char **argv)
 303{
 304        const char *event_name = "alias";
 305        struct strbuf buf_payload = STRBUF_INIT;
 306
 307        strbuf_addf(&buf_payload, "alias:%s argv:", alias);
 308        sq_quote_argv_pretty(&buf_payload, argv);
 309
 310        perf_io_write_fl(file, line, event_name, NULL, NULL, NULL, NULL,
 311                         &buf_payload);
 312        strbuf_release(&buf_payload);
 313}
 314
 315static void fn_child_start_fl(const char *file, int line,
 316                              uint64_t us_elapsed_absolute,
 317                              const struct child_process *cmd)
 318{
 319        const char *event_name = "child_start";
 320        struct strbuf buf_payload = STRBUF_INIT;
 321
 322        if (cmd->trace2_hook_name) {
 323                strbuf_addf(&buf_payload, "[ch%d] class:hook hook:%s",
 324                            cmd->trace2_child_id, cmd->trace2_hook_name);
 325        } else {
 326                const char *child_class =
 327                        cmd->trace2_child_class ? cmd->trace2_child_class : "?";
 328                strbuf_addf(&buf_payload, "[ch%d] class:%s",
 329                            cmd->trace2_child_id, child_class);
 330        }
 331
 332        if (cmd->dir) {
 333                strbuf_addstr(&buf_payload, " cd:");
 334                sq_quote_buf_pretty(&buf_payload, cmd->dir);
 335        }
 336
 337        strbuf_addstr(&buf_payload, " argv:");
 338        if (cmd->git_cmd)
 339                strbuf_addstr(&buf_payload, " git");
 340        sq_quote_argv_pretty(&buf_payload, cmd->argv);
 341
 342        perf_io_write_fl(file, line, event_name, NULL, &us_elapsed_absolute,
 343                         NULL, NULL, &buf_payload);
 344        strbuf_release(&buf_payload);
 345}
 346
 347static void fn_child_exit_fl(const char *file, int line,
 348                             uint64_t us_elapsed_absolute, int cid, int pid,
 349                             int code, uint64_t us_elapsed_child)
 350{
 351        const char *event_name = "child_exit";
 352        struct strbuf buf_payload = STRBUF_INIT;
 353
 354        strbuf_addf(&buf_payload, "[ch%d] pid:%d code:%d", cid, pid, code);
 355
 356        perf_io_write_fl(file, line, event_name, NULL, &us_elapsed_absolute,
 357                         &us_elapsed_child, NULL, &buf_payload);
 358        strbuf_release(&buf_payload);
 359}
 360
 361static void fn_thread_start_fl(const char *file, int line,
 362                               uint64_t us_elapsed_absolute)
 363{
 364        const char *event_name = "thread_start";
 365        struct strbuf buf_payload = STRBUF_INIT;
 366
 367        perf_io_write_fl(file, line, event_name, NULL, &us_elapsed_absolute,
 368                         NULL, NULL, &buf_payload);
 369        strbuf_release(&buf_payload);
 370}
 371
 372static void fn_thread_exit_fl(const char *file, int line,
 373                              uint64_t us_elapsed_absolute,
 374                              uint64_t us_elapsed_thread)
 375{
 376        const char *event_name = "thread_exit";
 377        struct strbuf buf_payload = STRBUF_INIT;
 378
 379        perf_io_write_fl(file, line, event_name, NULL, &us_elapsed_absolute,
 380                         &us_elapsed_thread, NULL, &buf_payload);
 381        strbuf_release(&buf_payload);
 382}
 383
 384static void fn_exec_fl(const char *file, int line, uint64_t us_elapsed_absolute,
 385                       int exec_id, const char *exe, const char **argv)
 386{
 387        const char *event_name = "exec";
 388        struct strbuf buf_payload = STRBUF_INIT;
 389
 390        strbuf_addf(&buf_payload, "id:%d ", exec_id);
 391        strbuf_addstr(&buf_payload, "argv:");
 392        if (exe)
 393                strbuf_addf(&buf_payload, " %s", exe);
 394        sq_quote_argv_pretty(&buf_payload, argv);
 395
 396        perf_io_write_fl(file, line, event_name, NULL, &us_elapsed_absolute,
 397                         NULL, NULL, &buf_payload);
 398        strbuf_release(&buf_payload);
 399}
 400
 401static void fn_exec_result_fl(const char *file, int line,
 402                              uint64_t us_elapsed_absolute, int exec_id,
 403                              int code)
 404{
 405        const char *event_name = "exec_result";
 406        struct strbuf buf_payload = STRBUF_INIT;
 407
 408        strbuf_addf(&buf_payload, "id:%d code:%d", exec_id, code);
 409        if (code > 0)
 410                strbuf_addf(&buf_payload, " err:%s", strerror(code));
 411
 412        perf_io_write_fl(file, line, event_name, NULL, &us_elapsed_absolute,
 413                         NULL, NULL, &buf_payload);
 414        strbuf_release(&buf_payload);
 415}
 416
 417static void fn_param_fl(const char *file, int line, const char *param,
 418                        const char *value)
 419{
 420        const char *event_name = "def_param";
 421        struct strbuf buf_payload = STRBUF_INIT;
 422
 423        strbuf_addf(&buf_payload, "%s:%s", param, value);
 424
 425        perf_io_write_fl(file, line, event_name, NULL, NULL, NULL, NULL,
 426                         &buf_payload);
 427        strbuf_release(&buf_payload);
 428}
 429
 430static void fn_repo_fl(const char *file, int line,
 431                       const struct repository *repo)
 432{
 433        const char *event_name = "def_repo";
 434        struct strbuf buf_payload = STRBUF_INIT;
 435
 436        strbuf_addstr(&buf_payload, "worktree:");
 437        sq_quote_buf_pretty(&buf_payload, repo->worktree);
 438
 439        perf_io_write_fl(file, line, event_name, repo, NULL, NULL, NULL,
 440                         &buf_payload);
 441        strbuf_release(&buf_payload);
 442}
 443
 444static void fn_region_enter_printf_va_fl(const char *file, int line,
 445                                         uint64_t us_elapsed_absolute,
 446                                         const char *category,
 447                                         const char *label,
 448                                         const struct repository *repo,
 449                                         const char *fmt, va_list ap)
 450{
 451        const char *event_name = "region_enter";
 452        struct strbuf buf_payload = STRBUF_INIT;
 453
 454        if (label)
 455                strbuf_addf(&buf_payload, "label:%s", label);
 456        if (fmt && *fmt) {
 457                strbuf_addch(&buf_payload, ' ');
 458                maybe_append_string_va(&buf_payload, fmt, ap);
 459        }
 460
 461        perf_io_write_fl(file, line, event_name, repo, &us_elapsed_absolute,
 462                         NULL, category, &buf_payload);
 463        strbuf_release(&buf_payload);
 464}
 465
 466static void fn_region_leave_printf_va_fl(
 467        const char *file, int line, uint64_t us_elapsed_absolute,
 468        uint64_t us_elapsed_region, const char *category, const char *label,
 469        const struct repository *repo, const char *fmt, va_list ap)
 470{
 471        const char *event_name = "region_leave";
 472        struct strbuf buf_payload = STRBUF_INIT;
 473
 474        if (label)
 475                strbuf_addf(&buf_payload, "label:%s", label);
 476        if (fmt && *fmt) {
 477                strbuf_addch(&buf_payload, ' ' );
 478                maybe_append_string_va(&buf_payload, fmt, ap);
 479        }
 480
 481        perf_io_write_fl(file, line, event_name, repo, &us_elapsed_absolute,
 482                         &us_elapsed_region, category, &buf_payload);
 483        strbuf_release(&buf_payload);
 484}
 485
 486static void fn_data_fl(const char *file, int line, uint64_t us_elapsed_absolute,
 487                       uint64_t us_elapsed_region, const char *category,
 488                       const struct repository *repo, const char *key,
 489                       const char *value)
 490{
 491        const char *event_name = "data";
 492        struct strbuf buf_payload = STRBUF_INIT;
 493
 494        strbuf_addf(&buf_payload, "%s:%s", key, value);
 495
 496        perf_io_write_fl(file, line, event_name, repo, &us_elapsed_absolute,
 497                         &us_elapsed_region, category, &buf_payload);
 498        strbuf_release(&buf_payload);
 499}
 500
 501static void fn_data_json_fl(const char *file, int line,
 502                            uint64_t us_elapsed_absolute,
 503                            uint64_t us_elapsed_region, const char *category,
 504                            const struct repository *repo, const char *key,
 505                            const struct json_writer *value)
 506{
 507        const char *event_name = "data_json";
 508        struct strbuf buf_payload = STRBUF_INIT;
 509
 510        strbuf_addf(&buf_payload, "%s:%s", key, value->json.buf);
 511
 512        perf_io_write_fl(file, line, event_name, repo, &us_elapsed_absolute,
 513                         &us_elapsed_region, category, &buf_payload);
 514        strbuf_release(&buf_payload);
 515}
 516
 517static void fn_printf_va_fl(const char *file, int line,
 518                            uint64_t us_elapsed_absolute, const char *fmt,
 519                            va_list ap)
 520{
 521        const char *event_name = "printf";
 522        struct strbuf buf_payload = STRBUF_INIT;
 523
 524        maybe_append_string_va(&buf_payload, fmt, ap);
 525
 526        perf_io_write_fl(file, line, event_name, NULL, &us_elapsed_absolute,
 527                         NULL, NULL, &buf_payload);
 528        strbuf_release(&buf_payload);
 529}
 530
 531struct tr2_tgt tr2_tgt_perf = {
 532        &tr2dst_perf,
 533
 534        fn_init,
 535        fn_term,
 536
 537        fn_version_fl,
 538        fn_start_fl,
 539        fn_exit_fl,
 540        fn_signal,
 541        fn_atexit,
 542        fn_error_va_fl,
 543        fn_command_path_fl,
 544        fn_command_name_fl,
 545        fn_command_mode_fl,
 546        fn_alias_fl,
 547        fn_child_start_fl,
 548        fn_child_exit_fl,
 549        fn_thread_start_fl,
 550        fn_thread_exit_fl,
 551        fn_exec_fl,
 552        fn_exec_result_fl,
 553        fn_param_fl,
 554        fn_repo_fl,
 555        fn_region_enter_printf_va_fl,
 556        fn_region_leave_printf_va_fl,
 557        fn_data_fl,
 558        fn_data_json_fl,
 559        fn_printf_va_fl,
 560};