From 178e3c870436febde791d03beee857666b5c4e42 Mon Sep 17 00:00:00 2001 From: David Malcolm Date: Fri, 13 Feb 2026 17:12:36 -0500 Subject: [PATCH] FIXME: WIP on sysprof TODO: * replace hardcoded includes and libs with configury stuff * make optional * use counters or whatnot to access memory usage levels * track current function, to allow collating data by that * show GC events (e.g. collection started/finished) * etc --- gcc/ggc-common.cc | 35 +++++++++++++++++++++++++++++++++++ gcc/ggc-page.cc | 6 ++++++ gcc/passes.cc | 11 +++++++++++ gcc/timevar.cc | 24 ++++++++++++++++++++++++ gcc/timevar.h | 9 +++++++++ 5 files changed, 85 insertions(+) diff --git a/gcc/ggc-common.cc b/gcc/ggc-common.cc index 6d3dca78f03..880bf611393 100644 --- a/gcc/ggc-common.cc +++ b/gcc/ggc-common.cc @@ -30,6 +30,7 @@ along with GCC; see the file COPYING3. If not see #include "hosthooks.h" #include "plugin.h" #include "options.h" +#include /* When true, protect the contents of the identifier hash table. */ bool ggc_protect_identifiers = true; @@ -1123,6 +1124,8 @@ ggc_min_heapsize_heuristic (void) } #endif +static void init_ggc_counters (); + void init_ggc_heuristics (void) { @@ -1130,6 +1133,8 @@ init_ggc_heuristics (void) param_ggc_min_expand = ggc_min_expand_heuristic (); param_ggc_min_heapsize = ggc_min_heapsize_heuristic (); #endif + + init_ggc_counters (); } /* GGC memory usage. */ @@ -1271,6 +1276,36 @@ public: /* GCC memory description. */ static mem_alloc_description ggc_mem_desc; +static void +init_counter (SysprofCaptureCounter *c, + const char *category, + const char *name, + const char *description) +{ + memset (c, 0, sizeof (*c)); + strncpy (c->category, category, sizeof (c->category)); + strncpy (c->name, name, sizeof (c->name)); + strncpy (c->description, description, sizeof (c->description)); + // FIXME +} + +static void +init_ggc_counters () +{ + sysprof_collector_init (); + + SysprofCaptureCounter counters[6] = {}; + + init_counter (&counters[0], "GGC", "alloated", "FIXME"); + init_counter (&counters[1], "GGC", "times", "FIXME"); + init_counter (&counters[2], "GGC", "peak", "FIXME"); + init_counter (&counters[3], "GGC", "freed", "FIXME"); + init_counter (&counters[4], "GGC", "collected", "FIXME"); + init_counter (&counters[5], "GGC", "overhead", "FIXME"); + + sysprof_collector_define_counters (counters, 6); +} + /* Dump per-site memory statistics. */ void diff --git a/gcc/ggc-page.cc b/gcc/ggc-page.cc index e3cccc9d079..e10c073dd5c 100644 --- a/gcc/ggc-page.cc +++ b/gcc/ggc-page.cc @@ -1534,6 +1534,12 @@ ggc_internal_alloc (size_t size, void (*f)(void *), size_t s, size_t n (fmt_size_t) size, (fmt_size_t) object_size, result, (void *) entry); +#if 0 + SysprofCaptureCounterValue counters[6]; + sysprof_collector_set_counters (const unsigned int *counters_ids, + counters, 6); +#endif + return result; } diff --git a/gcc/passes.cc b/gcc/passes.cc index 9ec37eeb2a9..eb9dd12c24d 100644 --- a/gcc/passes.cc +++ b/gcc/passes.cc @@ -67,6 +67,9 @@ along with GCC; see the file COPYING3. If not see #include "topics/pass-events.h" #include "channels.h" +// FIXME: +#include + /* Reserved TODOs */ #define TODO_verify_il (1u << 31) @@ -2652,9 +2655,17 @@ execute_one_pass (opt_pass *pass) do_per_function (verify_curr_properties, (void *)(size_t)pass->properties_required); + SysprofTimeStamp ts = SYSPROF_CAPTURE_CURRENT_TIME; + /* Do it! */ todo_after = pass->execute (cfun); + sysprof_collector_mark (ts, + SYSPROF_CAPTURE_CURRENT_TIME - ts, + "GCC Passes", + pass->name, + current_function_name ()); + if (todo_after & TODO_discard_function) { /* Stop timevar. */ diff --git a/gcc/timevar.cc b/gcc/timevar.cc index 2b2dd9386af..5c66c94f3f1 100644 --- a/gcc/timevar.cc +++ b/gcc/timevar.cc @@ -263,6 +263,22 @@ timer::push (timevar_id_t timevar) push_internal (tv); } +void +timer::timevar_def::sysprof_mark_start () +{ + this->sysprof_start_time = SYSPROF_CAPTURE_CURRENT_TIME; +} + +void +timer::timevar_def::sysprof_mark_end () +{ + sysprof_collector_mark (this->sysprof_start_time, + SYSPROF_CAPTURE_CURRENT_TIME - this->sysprof_start_time, + "GCC Timers", + this->name, + nullptr); // current_function_name ()); +} + /* Push TV onto the timing stack, either one of the builtin ones for a timevar_id_t, or one provided by client code to libgccjit. */ @@ -291,6 +307,7 @@ timer::push_internal (struct timevar_def *tv) /* Reset the start time; from now on, time is attributed to TIMEVAR. */ m_start_time = now; + tv->sysprof_mark_start (); /* See if we have a previously-allocated stack instance. If so, take it off the list. If not, malloc a new one. */ @@ -357,6 +374,8 @@ timer::pop_internal () element just exposed on the stack. */ m_start_time = now; + popped->timevar->sysprof_mark_end (); + /* Don't delete the stack element; instead, add it to the list of unused elements for later use. */ popped->next = m_unused_stack_instances; @@ -392,6 +411,7 @@ timer::start (timevar_id_t timevar) tv->standalone = 1; get_time (&tv->start_time); + tv->sysprof_mark_start (); } /* Stop timing TIMEVAR. Time elapsed since timevar_start was called @@ -420,6 +440,8 @@ timer::stop (timevar_id_t timevar) get_time (&now); timevar_accumulate (&tv->elapsed, &tv->start_time, &now); + + tv->sysprof_mark_end (); } @@ -456,6 +478,7 @@ timer::cond_start (timevar_id_t timevar) tv->standalone = 1; get_time (&tv->start_time); + tv->sysprof_mark_start (); return false; /* The timevar was not already running. */ } @@ -488,6 +511,7 @@ timer::cond_stop (timevar_id_t timevar) tv->standalone = 0; /* Enable a restart. */ get_time (&now); + tv->sysprof_mark_end (); timevar_accumulate (&tv->elapsed, &tv->start_time, &now); } diff --git a/gcc/timevar.h b/gcc/timevar.h index 1e7503d43ba..6f9e7d6828a 100644 --- a/gcc/timevar.h +++ b/gcc/timevar.h @@ -21,6 +21,9 @@ #ifndef GCC_TIMEVAR_H #define GCC_TIMEVAR_H +// FIXME: +#include + namespace json { class value; } /* Timing variables are used to measure elapsed time in various @@ -134,6 +137,9 @@ class timer /* Private type: a timing variable. */ struct timevar_def { + void sysprof_mark_start (); + void sysprof_mark_end (); + std::unique_ptr make_json () const; /* Elapsed time for this variable. */ @@ -143,6 +149,9 @@ class timer using timevar_start, this contains the start time. */ struct timevar_time_def start_time; + // FIXME + SysprofTimeStamp sysprof_start_time; + /* The name of this timing variable. */ const char *name; -- 2.49.0