Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make PauseTiming() and ResumeTiming() per thread. #286

Merged
merged 21 commits into from
Sep 3, 2016
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
58 changes: 28 additions & 30 deletions include/benchmark/benchmark_api.h
Original file line number Diff line number Diff line change
Expand Up @@ -270,31 +270,25 @@ enum BigO {
// computational complexity for the benchmark.
typedef double(BigOFunc)(int);

namespace internal {
class ThreadTimer;
class ThreadManager;
}

// State is passed to a running Benchmark and contains state for the
// benchmark to use.
class State {
public:
State(size_t max_iters, const std::vector<int>& ranges,
int thread_i, int n_threads);

// Returns true if the benchmark should continue through another iteration.
// NOTE: A benchmark may not return from the test until KeepRunning() has
// returned false.
bool KeepRunning() {
if (BENCHMARK_BUILTIN_EXPECT(!started_, false)) {
assert(!finished_);
started_ = true;
ResumeTiming();
StartKeepRunning();
}
bool const res = total_iterations_++ < max_iterations;
if (BENCHMARK_BUILTIN_EXPECT(!res, false)) {
assert(started_ && (!finished_ || error_occurred_));
if (!error_occurred_) {
PauseTiming();
}
// Total iterations now is one greater than max iterations. Fix this.
total_iterations_ = max_iterations;
finished_ = true;
FinishKeepRunning();
}
return res;
}
Expand All @@ -304,10 +298,11 @@ class State {
// Stop the benchmark timer. If not called, the timer will be
// automatically stopped after KeepRunning() returns false for the first time.
//
// For threaded benchmarks the PauseTiming() function acts
// like a barrier. I.e., the ith call by a particular thread to this
// function will block until all active threads have made their ith call.
// The timer will stop when the last thread has called this function.
// For threaded benchmarks the PauseTiming() function only pauses the timing
// for the current thread.
//
// NOTE: The "real time" measurement is per-thread. If different threads
// report different measurements the largest one is reported.
//
// NOTE: PauseTiming()/ResumeTiming() are relatively
// heavyweight, and so their use should generally be avoided
Expand All @@ -319,11 +314,6 @@ class State {
// Start the benchmark timer. The timer is NOT running on entrance to the
// benchmark function. It begins running after the first call to KeepRunning()
//
// For threaded benchmarks the ResumeTiming() function acts
// like a barrier. I.e., the ith call by a particular thread to this
// function will block until all active threads have made their ith call.
// The timer will start when the last thread has called this function.
//
// NOTE: PauseTiming()/ResumeTiming() are relatively
// heavyweight, and so their use should generally be avoided
// within each benchmark iteration, if possible.
Expand All @@ -335,10 +325,10 @@ class State {
// thread and report an error with the specified 'msg'. After this call
// the user may explicitly 'return' from the benchmark.
//
// For threaded benchmarks only the current thread stops executing. If
// multiple threads report an error only the first error message is used.
// The current thread is no longer considered 'active' by
// 'PauseTiming()' and 'ResumingTiming()'.
// For threaded benchmarks only the current thread stops executing and future
// calls to `KeepRunning()` will block until all threads have completed
// the `KeepRunning()` loop. If multiple threads report an error only the
// first error message is used.
//
// NOTE: Calling 'SkipWithError(...)' does not cause the benchmark to exit
// the current scope immediately. If the function is called from within
Expand All @@ -351,10 +341,8 @@ class State {
// is used instead of automatically measured time if UseManualTime() was
// specified.
//
// For threaded benchmarks the SetIterationTime() function acts
// like a barrier. I.e., the ith call by a particular thread to this
// function will block until all threads have made their ith call.
// The time will be set by the last thread to call this function.
// For threaded benchmarks the final value will be set to the largest
// reported values.
void SetIterationTime(double seconds);

// Set the number of bytes processed by the current benchmark
Expand Down Expand Up @@ -465,7 +453,17 @@ class State {
const int threads;
const size_t max_iterations;

// TODO make me private
State(size_t max_iters, const std::vector<int>& ranges,
int thread_i, int n_threads,
internal::ThreadTimer* timer,
internal::ThreadManager* manager);

private:
void StartKeepRunning();
void FinishKeepRunning();
internal::ThreadTimer* timer_;
internal::ThreadManager* manager_;
BENCHMARK_DISALLOW_COPY_AND_ASSIGN(State);
};

Expand Down
7 changes: 4 additions & 3 deletions src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,17 @@ endif()

# Define the source files
set(SOURCE_FILES "benchmark.cc" "colorprint.cc" "commandlineflags.cc"
"console_reporter.cc" "csv_reporter.cc" "json_reporter.cc"
"log.cc" "reporter.cc" "sleep.cc" "string_util.cc"
"sysinfo.cc" "walltime.cc" "complexity.cc")
"console_reporter.cc" "cpu_thread_time.cc" "csv_reporter.cc"
"json_reporter.cc" "log.cc" "reporter.cc" "sleep.cc"
"string_util.cc" "sysinfo.cc" "walltime.cc" "complexity.cc")
# Add headers to the list of source files. cmake does not require this,
# but IDEs such as Visual Studio need this to add the headers
# to the generated project.
set(_d "${PROJECT_SOURCE_DIR}/include/benchmark")
list(APPEND SOURCE_FILES "${_d}/benchmark.h" "${_d}/benchmark_api.h"
"${_d}/macros.h" "${_d}/reporter.h" "arraysize.h" "check.h"
"colorprint.h" "commandlineflags.h" "complexity.h"
"cpu_thread_time.h"
"cycleclock.h" "internal_macros.h" "log.h" "mutex.h"
"re.h" "sleep.h" "stat.h" "string_util.h" "sysinfo.h"
"walltime.h")
Expand Down
Loading