Skip to content

Commit 4ed2aee

Browse files
committed
feat(google_benchmark): run each benchmark in its own process
In analysis builds, store the original argv in `Initialize` and route every benchmark through `Isolation::run`, one child per benchmark including its repetitions. A child only matches its own benchmark name, which overrides `--benchmark_filter`, and does not print the mode line and context again. Walltime builds are unchanged.
1 parent c86f5b1 commit 4ed2aee

3 files changed

Lines changed: 62 additions & 7 deletions

File tree

‎google_benchmark/src/benchmark.cc‎

Lines changed: 47 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
#include "internal_macros.h"
2121

2222
#include "measurement.hpp"
23+
#include "isolation.h"
2324

2425
#ifndef BENCHMARK_OS_WINDOWS
2526
#if !defined(BENCHMARK_OS_FUCHSIA) && !defined(BENCHMARK_OS_QURT)
@@ -464,9 +465,19 @@ void RunBenchmarks(const std::vector<BenchmarkInstance>& benchmarks,
464465
// Note the file_reporter can be null.
465466
BM_CHECK(display_reporter != nullptr);
466467

468+
#ifdef CODSPEED_ANALYSIS
469+
const codspeed::Isolation& isolation = codspeed::Isolation::current();
470+
// Process isolation children only run one benchmark. The parent already
471+
// printed the mode and the context.
472+
const bool is_isolated_child = isolation.only_benchmark() != nullptr;
473+
#endif
474+
467475
#ifdef CODSPEED_ENABLED
468476
auto& Err = display_reporter->GetErrorStream();
469-
Err << "Codspeed mode: " << CODSPEED_MODE_DISPLAY << "\n";
477+
#ifdef CODSPEED_ANALYSIS
478+
if (!is_isolated_child)
479+
#endif
480+
Err << "Codspeed mode: " << CODSPEED_MODE_DISPLAY << "\n";
470481
#endif // CODSPEED_ENABLED
471482

472483
bool might_have_aggregates = FLAGS_benchmark_repetitions > 1;
@@ -493,8 +504,18 @@ void RunBenchmarks(const std::vector<BenchmarkInstance>& benchmarks,
493504
std::map<int /*family_index*/, BenchmarkReporter::PerFamilyRunReports>
494505
per_family_reports;
495506

496-
if (display_reporter->ReportContext(context) &&
497-
((file_reporter == nullptr) || file_reporter->ReportContext(context))) {
507+
#ifdef CODSPEED_ANALYSIS
508+
const bool context_reported =
509+
is_isolated_child ||
510+
(display_reporter->ReportContext(context) &&
511+
((file_reporter == nullptr) || file_reporter->ReportContext(context)));
512+
#else
513+
const bool context_reported =
514+
display_reporter->ReportContext(context) &&
515+
((file_reporter == nullptr) || file_reporter->ReportContext(context));
516+
#endif
517+
518+
if (context_reported) {
498519
FlushStreams(display_reporter);
499520
FlushStreams(file_reporter);
500521

@@ -560,10 +581,26 @@ void RunBenchmarks(const std::vector<BenchmarkInstance>& benchmarks,
560581

561582
#ifdef CODSPEED_WALLTIME
562583
std::vector<codspeed::RawWalltimeBenchmark> codspeed_walltime_data;
584+
#endif
585+
#ifdef CODSPEED_ANALYSIS
586+
// The child of a benchmark runs all of its repetitions, so the parent
587+
// starts only one child per benchmark.
588+
std::vector<bool> isolated(runners.size(), false);
563589
#endif
564590
for (size_t repetition_index : repetition_indices) {
565591
internal::BenchmarkRunner& runner = runners[repetition_index];
566592

593+
#ifdef CODSPEED_ANALYSIS
594+
if (isolation.kind() == codspeed::Isolation::Kind::ProcessPerBenchmark) {
595+
if (!isolated[repetition_index]) {
596+
isolated[repetition_index] = true;
597+
isolation.run(runner.GetBenchmarkName(),
598+
[&runner]() { runner.DoOneRepetition(); });
599+
}
600+
continue;
601+
}
602+
#endif
603+
567604
#ifdef CODSPEED_WALLTIME
568605
auto* codspeed = codspeed::CodSpeed::getInstance();
569606
if (codspeed != nullptr && runner.IsFirstRepetition()) {
@@ -954,6 +991,13 @@ void PrintDefaultHelp() {
954991
}
955992

956993
void Initialize(int* argc, char** argv, void (*HelperPrintf)()) {
994+
#ifdef CODSPEED_ANALYSIS
995+
// Keep the full command line: parsing below removes the benchmark flags, and
996+
// process isolation starts the children with the original arguments.
997+
codspeed::Isolation::set_argv(*argc, argv);
998+
// Reject an invalid CODSPEED_PROCESS_ISOLATION before running anything.
999+
codspeed::Isolation::current();
1000+
#endif
9571001
internal::HelperPrintf = HelperPrintf;
9581002
internal::ParseCommandLineFlags(argc, argv);
9591003
internal::LogLevel() = FLAGS_v;

‎google_benchmark/src/benchmark_register.cc‎

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,9 @@
1313
// limitations under the License.
1414

1515
#include "benchmark_register.h"
16+
#ifdef CODSPEED_ANALYSIS
17+
#include "isolation.h"
18+
#endif
1619

1720
#ifndef BENCHMARK_OS_WINDOWS
1821
#if !defined(BENCHMARK_OS_FUCHSIA) && !defined(BENCHMARK_OS_QURT)
@@ -172,9 +175,17 @@ bool BenchmarkFamilies::FindBenchmarks(
172175
num_threads);
173176

174177
const auto full_name = instance.name().str();
175-
if (full_name.rfind(kDisabledPrefix, 0) != 0 &&
176-
((re.Match(full_name) && !is_negative_filter) ||
177-
(!re.Match(full_name) && is_negative_filter))) {
178+
bool matches_filter = (re.Match(full_name) && !is_negative_filter) ||
179+
(!re.Match(full_name) && is_negative_filter);
180+
#ifdef CODSPEED_ANALYSIS
181+
// A process isolation child runs exactly one benchmark, whatever the
182+
// command line filter says.
183+
if (const std::string* only =
184+
codspeed::Isolation::current().only_benchmark()) {
185+
matches_filter = full_name == *only;
186+
}
187+
#endif
188+
if (full_name.rfind(kDisabledPrefix, 0) != 0 && matches_filter) {
178189
benchmarks->push_back(std::move(instance));
179190

180191
++per_family_instance_index;

‎google_benchmark/src/benchmark_runner.h‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ class BenchmarkRunner {
124124

125125
void RunWarmUp();
126126

127-
#ifdef CODSPEED_WALLTIME
127+
#if defined(CODSPEED_WALLTIME) || defined(CODSPEED_ANALYSIS)
128128
public:
129129
inline std::string GetBenchmarkName() {
130130
return b.name().str();

0 commit comments

Comments
 (0)