8e52395de2d1ba37d966891331b81355ae032c58
   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
 244static void fn_error_va_fl(const char *file, int line, const char *fmt,
 245                           va_list ap)
 246{
 247        const char *event_name = "error";
 248        struct strbuf buf_payload = STRBUF_INIT;
 249
 250        maybe_append_string_va(&buf_payload, fmt, ap);
 251
 252        perf_io_write_fl(file, line, event_name, NULL, NULL, NULL, NULL,
 253                         &buf_payload);
 254        strbuf_release(&buf_payload);
 255}
 256
 257static void fn_command_path_fl(const char *file, int line, const char *pathname)
 258{
 259        const char *event_name = "cmd_path";
 260        struct strbuf buf_payload = STRBUF_INIT;
 261
 262        strbuf_addstr(&buf_payload, pathname);
 263
 264        perf_io_write_fl(file, line, event_name, NULL, NULL, NULL, NULL,
 265                         &buf_payload);
 266        strbuf_release(&buf_payload);
 267}
 268
 269static void fn_command_name_fl(const char *file, int line, const char *name,
 270                               const char *hierarchy)
 271{
 272        const char *event_name = "cmd_name";
 273        struct strbuf buf_payload = STRBUF_INIT;
 274
 275        strbuf_addstr(&buf_payload, name);
 276        if (hierarchy && *hierarchy)
 277                strbuf_addf(&buf_payload, " (%s)", hierarchy);
 278
 279        perf_io_write_fl(file, line, event_name, NULL, NULL, NULL, NULL,
 280                         &buf_payload);
 281        strbuf_release(&buf_payload);
 282}
 283
 284static void fn_command_mode_fl(const char *file, int line, const char *mode)
 285{
 286        const char *event_name = "cmd_mode";
 287        struct strbuf buf_payload = STRBUF_INIT;
 288
 289        strbuf_addstr(&buf_payload, mode);
 290
 291        perf_io_write_fl(file, line, event_name, NULL, NULL, NULL, NULL,
 292                         &buf_payload);
 293        strbuf_release(&buf_payload);
 294}
 295
 296static void fn_alias_fl(const char *file, int line, const char *alias,
 297                        const char **argv)
 298{
 299        const char *event_name = "alias";
 300        struct strbuf buf_payload = STRBUF_INIT;
 301
 302        strbuf_addf(&buf_payload, "alias:%s argv:", alias);
 303        sq_quote_argv_pretty(&buf_payload, argv);
 304
 305        perf_io_write_fl(file, line, event_name, NULL, NULL, NULL, NULL,
 306                         &buf_payload);
 307        strbuf_release(&buf_payload);
 308}
 309
 310static void fn_child_start_fl(const char *file, int line,
 311                              uint64_t us_elapsed_absolute,
 312                              const struct child_process *cmd)
 313{
 314        const char *event_name = "child_start";
 315        struct strbuf buf_payload = STRBUF_INIT;
 316
 317        if (cmd->trace2_hook_name) {
 318                strbuf_addf(&buf_payload, "[ch%d] class:hook hook:%s",
 319                            cmd->trace2_child_id, cmd->trace2_hook_name);
 320        } else {
 321                const char *child_class =
 322                        cmd->trace2_child_class ? cmd->trace2_child_class : "?";
 323                strbuf_addf(&buf_payload, "[ch%d] class:%s",
 324                            cmd->trace2_child_id, child_class);
 325        }
 326
 327        if (cmd->dir) {
 328                strbuf_addstr(&buf_payload, " cd:");
 329                sq_quote_buf_pretty(&buf_payload, cmd->dir);
 330        }
 331
 332        strbuf_addstr(&buf_payload, " argv:");
 333        if (cmd->git_cmd)
 334                strbuf_addstr(&buf_payload, " git");
 335        sq_quote_argv_pretty(&buf_payload, cmd->argv);
 336
 337        perf_io_write_fl(file, line, event_name, NULL, &us_elapsed_absolute,
 338                         NULL, NULL, &buf_payload);
 339        strbuf_release(&buf_payload);
 340}
 341
 342static void fn_child_exit_fl(const char *file, int line,
 343                             uint64_t us_elapsed_absolute, int cid, int pid,
 344                             int code, uint64_t us_elapsed_child)
 345{
 346        const char *event_name = "child_exit";
 347        struct strbuf buf_payload = STRBUF_INIT;
 348
 349        strbuf_addf(&buf_payload, "[ch%d] pid:%d code:%d", cid, pid, code);
 350
 351        perf_io_write_fl(file, line, event_name, NULL, &us_elapsed_absolute,
 352                         &us_elapsed_child, NULL, &buf_payload);
 353        strbuf_release(&buf_payload);
 354}
 355
 356static void fn_thread_start_fl(const char *file, int line,
 357                               uint64_t us_elapsed_absolute)
 358{
 359        const char *event_name = "thread_start";
 360        struct strbuf buf_payload = STRBUF_INIT;
 361
 362        perf_io_write_fl(file, line, event_name, NULL, &us_elapsed_absolute,
 363                         NULL, NULL, &buf_payload);
 364        strbuf_release(&buf_payload);
 365}
 366
 367static void fn_thread_exit_fl(const char *file, int line,
 368                              uint64_t us_elapsed_absolute,
 369                              uint64_t us_elapsed_thread)
 370{
 371        const char *event_name = "thread_exit";
 372        struct strbuf buf_payload = STRBUF_INIT;
 373
 374        perf_io_write_fl(file, line, event_name, NULL, &us_elapsed_absolute,
 375                         &us_elapsed_thread, NULL, &buf_payload);
 376        strbuf_release(&buf_payload);
 377}
 378
 379static void fn_exec_fl(const char *file, int line, uint64_t us_elapsed_absolute,
 380                       int exec_id, const char *exe, const char **argv)
 381{
 382        const char *event_name = "exec";
 383        struct strbuf buf_payload = STRBUF_INIT;
 384
 385        strbuf_addf(&buf_payload, "id:%d ", exec_id);
 386        strbuf_addstr(&buf_payload, "argv:");
 387        if (exe)
 388                strbuf_addf(&buf_payload, " %s", exe);
 389        sq_quote_argv_pretty(&buf_payload, argv);
 390
 391        perf_io_write_fl(file, line, event_name, NULL, &us_elapsed_absolute,
 392                         NULL, NULL, &buf_payload);
 393        strbuf_release(&buf_payload);
 394}
 395
 396static void fn_exec_result_fl(const char *file, int line,
 397                              uint64_t us_elapsed_absolute, int exec_id,
 398                              int code)
 399{
 400        const char *event_name = "exec_result";
 401        struct strbuf buf_payload = STRBUF_INIT;
 402
 403        strbuf_addf(&buf_payload, "id:%d code:%d", exec_id, code);
 404        if (code > 0)
 405                strbuf_addf(&buf_payload, " err:%s", strerror(code));
 406
 407        perf_io_write_fl(file, line, event_name, NULL, &us_elapsed_absolute,
 408                         NULL, NULL, &buf_payload);
 409        strbuf_release(&buf_payload);
 410}
 411
 412static void fn_param_fl(const char *file, int line, const char *param,
 413                        const char *value)
 414{
 415        const char *event_name = "def_param";
 416        struct strbuf buf_payload = STRBUF_INIT;
 417
 418        strbuf_addf(&buf_payload, "%s:%s", param, value);
 419
 420        perf_io_write_fl(file, line, event_name, NULL, NULL, NULL, NULL,
 421                         &buf_payload);
 422        strbuf_release(&buf_payload);
 423}
 424
 425static void fn_repo_fl(const char *file, int line,
 426                       const struct repository *repo)
 427{
 428        const char *event_name = "def_repo";
 429        struct strbuf buf_payload = STRBUF_INIT;
 430
 431        strbuf_addstr(&buf_payload, "worktree:");
 432        sq_quote_buf_pretty(&buf_payload, repo->worktree);
 433
 434        perf_io_write_fl(file, line, event_name, repo, NULL, NULL, NULL,
 435                         &buf_payload);
 436        strbuf_release(&buf_payload);
 437}
 438
 439static void fn_region_enter_printf_va_fl(const char *file, int line,
 440                                         uint64_t us_elapsed_absolute,
 441                                         const char *category,
 442                                         const char *label,
 443                                         const struct repository *repo,
 444                                         const char *fmt, va_list ap)
 445{
 446        const char *event_name = "region_enter";
 447        struct strbuf buf_payload = STRBUF_INIT;
 448
 449        if (label)
 450                strbuf_addf(&buf_payload, "label:%s", label);
 451        if (fmt && *fmt) {
 452                strbuf_addch(&buf_payload, ' ');
 453                maybe_append_string_va(&buf_payload, fmt, ap);
 454        }
 455
 456        perf_io_write_fl(file, line, event_name, repo, &us_elapsed_absolute,
 457                         NULL, category, &buf_payload);
 458        strbuf_release(&buf_payload);
 459}
 460
 461static void fn_region_leave_printf_va_fl(
 462        const char *file, int line, uint64_t us_elapsed_absolute,
 463        uint64_t us_elapsed_region, const char *category, const char *label,
 464        const struct repository *repo, const char *fmt, va_list ap)
 465{
 466        const char *event_name = "region_leave";
 467        struct strbuf buf_payload = STRBUF_INIT;
 468
 469        if (label)
 470                strbuf_addf(&buf_payload, "label:%s", label);
 471        if (fmt && *fmt) {
 472                strbuf_addch(&buf_payload, ' ' );
 473                maybe_append_string_va(&buf_payload, fmt, ap);
 474        }
 475
 476        perf_io_write_fl(file, line, event_name, repo, &us_elapsed_absolute,
 477                         &us_elapsed_region, category, &buf_payload);
 478        strbuf_release(&buf_payload);
 479}
 480
 481static void fn_data_fl(const char *file, int line, uint64_t us_elapsed_absolute,
 482                       uint64_t us_elapsed_region, const char *category,
 483                       const struct repository *repo, const char *key,
 484                       const char *value)
 485{
 486        const char *event_name = "data";
 487        struct strbuf buf_payload = STRBUF_INIT;
 488
 489        strbuf_addf(&buf_payload, "%s:%s", key, value);
 490
 491        perf_io_write_fl(file, line, event_name, repo, &us_elapsed_absolute,
 492                         &us_elapsed_region, category, &buf_payload);
 493        strbuf_release(&buf_payload);
 494}
 495
 496static void fn_data_json_fl(const char *file, int line,
 497                            uint64_t us_elapsed_absolute,
 498                            uint64_t us_elapsed_region, const char *category,
 499                            const struct repository *repo, const char *key,
 500                            const struct json_writer *value)
 501{
 502        const char *event_name = "data_json";
 503        struct strbuf buf_payload = STRBUF_INIT;
 504
 505        strbuf_addf(&buf_payload, "%s:%s", key, value->json.buf);
 506
 507        perf_io_write_fl(file, line, event_name, repo, &us_elapsed_absolute,
 508                         &us_elapsed_region, category, &buf_payload);
 509        strbuf_release(&buf_payload);
 510}
 511
 512static void fn_printf_va_fl(const char *file, int line,
 513                            uint64_t us_elapsed_absolute, const char *fmt,
 514                            va_list ap)
 515{
 516        const char *event_name = "printf";
 517        struct strbuf buf_payload = STRBUF_INIT;
 518
 519        maybe_append_string_va(&buf_payload, fmt, ap);
 520
 521        perf_io_write_fl(file, line, event_name, NULL, &us_elapsed_absolute,
 522                         NULL, NULL, &buf_payload);
 523        strbuf_release(&buf_payload);
 524}
 525
 526struct tr2_tgt tr2_tgt_perf = {
 527        &tr2dst_perf,
 528
 529        fn_init,
 530        fn_term,
 531
 532        fn_version_fl,
 533        fn_start_fl,
 534        fn_exit_fl,
 535        fn_signal,
 536        fn_atexit,
 537        fn_error_va_fl,
 538        fn_command_path_fl,
 539        fn_command_name_fl,
 540        fn_command_mode_fl,
 541        fn_alias_fl,
 542        fn_child_start_fl,
 543        fn_child_exit_fl,
 544        fn_thread_start_fl,
 545        fn_thread_exit_fl,
 546        fn_exec_fl,
 547        fn_exec_result_fl,
 548        fn_param_fl,
 549        fn_repo_fl,
 550        fn_region_enter_printf_va_fl,
 551        fn_region_leave_printf_va_fl,
 552        fn_data_fl,
 553        fn_data_json_fl,
 554        fn_printf_va_fl,
 555};