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
956993void 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;
0 commit comments