QBenchmarkPerfEventsMeasurer: move the perf_event_attr data to the stack

There's no need for it to be a global variable. It needed to be global
because we configured the event globally before start(), but in commit
4731baf6d3a18857e86cc16de000bc42e84bf6de (6.5.0) we instead introduced
the Q_GLOBAL_STATIC with a QList containing the globally-configured
events.

This is using designated initializers despite their being a C++20
feature, because GCC and Clang have supported them as an extension to C+
+ for a long time. This file has needed that extension since that commit
anyway.

Pick-to: 6.8
Change-Id: I1c9080d23df9f7ba3cbafffd7eae7bd57cd69b67
Reviewed-by: Tor Arne Vestbø <tor.arne.vestbo@qt.io>
This commit is contained in:
Thiago Macieira 2024-09-19 16:57:32 -07:00
parent f566b09ed7
commit 31b4150dc3

View File

@ -53,26 +53,8 @@ struct PerfEvent
quint32 type;
quint64 config;
};
static perf_event_attr attr;
Q_GLOBAL_STATIC(QList<PerfEvent>, eventTypes);
static void initPerf()
{
static bool done;
if (!done) {
memset(&attr, 0, sizeof attr);
attr.size = sizeof attr;
attr.read_format = PERF_FORMAT_TOTAL_TIME_ENABLED | PERF_FORMAT_TOTAL_TIME_RUNNING;
attr.disabled = true; // we'll enable later
attr.inherit = true; // let children processes inherit the monitoring
attr.pinned = true; // keep it running in the hardware
attr.inherit_stat = true; // aggregate all the info from child processes
attr.task = true; // trace fork/exits
done = true;
}
}
static QList<PerfEvent> defaultCounters()
{
return {
@ -407,7 +389,6 @@ static QTest::QBenchmarkMetric metricForEvent(PerfEvent counter)
void QBenchmarkPerfEventsMeasurer::setCounter(const char *name)
{
initPerf();
eventTypes->clear();
std::string_view input = name;
if (qsizetype idx = input.find(':'); idx >= 0)
@ -473,7 +454,18 @@ void QBenchmarkPerfEventsMeasurer::init()
void QBenchmarkPerfEventsMeasurer::start()
{
initPerf();
QT_WARNING_DISABLE_GCC("-Wmissing-field-initializers")
QT_WARNING_DISABLE_CLANG("-Wmissing-field-initializers")
perf_event_attr attr = {
.size = sizeof attr,
.read_format = PERF_FORMAT_TOTAL_TIME_ENABLED | PERF_FORMAT_TOTAL_TIME_RUNNING,
.disabled = true, // we'll enable later
.inherit = true, // let children processes inherit the monitoring
.pinned = true, // keep it running in the hardware
.inherit_stat = true, // aggregate all the info from child processes
.task = true, // trace fork/exits
};
QList<PerfEvent> &counters = *eventTypes;
if (counters.isEmpty())
counters = defaultCounters();