Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
B
benchmark
Project
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Chen Yisong
benchmark
Commits
674d0498
Unverified
Commit
674d0498
authored
Mar 16, 2018
by
Dominic Hamon
Committed by
GitHub
Mar 16, 2018
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Move thread classes out to clean up monolithic code (#554)
parent
a9beffda
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
134 additions
and
109 deletions
+134
-109
benchmark.cc
src/benchmark.cc
+3
-109
thread_manager.h
src/thread_manager.h
+62
-0
thread_timer.h
src/thread_timer.h
+69
-0
No files found.
src/benchmark.cc
View file @
674d0498
...
...
@@ -29,10 +29,10 @@
#include <condition_variable>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <fstream>
#include <iostream>
#include <memory>
#include <string>
#include <thread>
#include "check.h"
...
...
@@ -46,7 +46,8 @@
#include "re.h"
#include "statistics.h"
#include "string_util.h"
#include "timers.h"
#include "thread_manager.h"
#include "thread_timer.h"
DEFINE_bool
(
benchmark_list_tests
,
false
,
"Print a list of benchmarks. This option overrides all other "
...
...
@@ -110,113 +111,6 @@ namespace internal {
void
UseCharPointer
(
char
const
volatile
*
)
{}
class
ThreadManager
{
public
:
ThreadManager
(
int
num_threads
)
:
alive_threads_
(
num_threads
),
start_stop_barrier_
(
num_threads
)
{}
Mutex
&
GetBenchmarkMutex
()
const
RETURN_CAPABILITY
(
benchmark_mutex_
)
{
return
benchmark_mutex_
;
}
bool
StartStopBarrier
()
EXCLUDES
(
end_cond_mutex_
)
{
return
start_stop_barrier_
.
wait
();
}
void
NotifyThreadComplete
()
EXCLUDES
(
end_cond_mutex_
)
{
start_stop_barrier_
.
removeThread
();
if
(
--
alive_threads_
==
0
)
{
MutexLock
lock
(
end_cond_mutex_
);
end_condition_
.
notify_all
();
}
}
void
WaitForAllThreads
()
EXCLUDES
(
end_cond_mutex_
)
{
MutexLock
lock
(
end_cond_mutex_
);
end_condition_
.
wait
(
lock
.
native_handle
(),
[
this
]()
{
return
alive_threads_
==
0
;
});
}
public
:
struct
Result
{
double
real_time_used
=
0
;
double
cpu_time_used
=
0
;
double
manual_time_used
=
0
;
int64_t
bytes_processed
=
0
;
int64_t
items_processed
=
0
;
int
complexity_n
=
0
;
std
::
string
report_label_
;
std
::
string
error_message_
;
bool
has_error_
=
false
;
UserCounters
counters
;
};
GUARDED_BY
(
GetBenchmarkMutex
())
Result
results
;
private
:
mutable
Mutex
benchmark_mutex_
;
std
::
atomic
<
int
>
alive_threads_
;
Barrier
start_stop_barrier_
;
Mutex
end_cond_mutex_
;
Condition
end_condition_
;
};
// Timer management class
class
ThreadTimer
{
public
:
ThreadTimer
()
=
default
;
// Called by each thread
void
StartTimer
()
{
running_
=
true
;
start_real_time_
=
ChronoClockNow
();
start_cpu_time_
=
ThreadCPUUsage
();
}
// Called by each thread
void
StopTimer
()
{
CHECK
(
running_
);
running_
=
false
;
real_time_used_
+=
ChronoClockNow
()
-
start_real_time_
;
// Floating point error can result in the subtraction producing a negative
// time. Guard against that.
cpu_time_used_
+=
std
::
max
<
double
>
(
ThreadCPUUsage
()
-
start_cpu_time_
,
0
);
}
// Called by each thread
void
SetIterationTime
(
double
seconds
)
{
manual_time_used_
+=
seconds
;
}
bool
running
()
const
{
return
running_
;
}
// REQUIRES: timer is not running
double
real_time_used
()
{
CHECK
(
!
running_
);
return
real_time_used_
;
}
// REQUIRES: timer is not running
double
cpu_time_used
()
{
CHECK
(
!
running_
);
return
cpu_time_used_
;
}
// REQUIRES: timer is not running
double
manual_time_used
()
{
CHECK
(
!
running_
);
return
manual_time_used_
;
}
private
:
bool
running_
=
false
;
// Is the timer running
double
start_real_time_
=
0
;
// If running_
double
start_cpu_time_
=
0
;
// If running_
// Accumulated time so far (does not contain current slice if running_)
double
real_time_used_
=
0
;
double
cpu_time_used_
=
0
;
// Manually set iteration time. User sets this with SetIterationTime(seconds).
double
manual_time_used_
=
0
;
};
namespace
{
BenchmarkReporter
::
Run
CreateRunReport
(
...
...
src/thread_manager.h
0 → 100644
View file @
674d0498
#ifndef BENCHMARK_THREAD_MANAGER_H
#define BENCHMARK_THREAD_MANAGER_H
#include "mutex.h"
namespace
benchmark
{
namespace
internal
{
class
ThreadManager
{
public
:
ThreadManager
(
int
num_threads
)
:
alive_threads_
(
num_threads
),
start_stop_barrier_
(
num_threads
)
{}
Mutex
&
GetBenchmarkMutex
()
const
RETURN_CAPABILITY
(
benchmark_mutex_
)
{
return
benchmark_mutex_
;
}
bool
StartStopBarrier
()
EXCLUDES
(
end_cond_mutex_
)
{
return
start_stop_barrier_
.
wait
();
}
void
NotifyThreadComplete
()
EXCLUDES
(
end_cond_mutex_
)
{
start_stop_barrier_
.
removeThread
();
if
(
--
alive_threads_
==
0
)
{
MutexLock
lock
(
end_cond_mutex_
);
end_condition_
.
notify_all
();
}
}
void
WaitForAllThreads
()
EXCLUDES
(
end_cond_mutex_
)
{
MutexLock
lock
(
end_cond_mutex_
);
end_condition_
.
wait
(
lock
.
native_handle
(),
[
this
]()
{
return
alive_threads_
==
0
;
});
}
public
:
struct
Result
{
double
real_time_used
=
0
;
double
cpu_time_used
=
0
;
double
manual_time_used
=
0
;
int64_t
bytes_processed
=
0
;
int64_t
items_processed
=
0
;
int
complexity_n
=
0
;
std
::
string
report_label_
;
std
::
string
error_message_
;
bool
has_error_
=
false
;
UserCounters
counters
;
};
GUARDED_BY
(
GetBenchmarkMutex
())
Result
results
;
private
:
mutable
Mutex
benchmark_mutex_
;
std
::
atomic
<
int
>
alive_threads_
;
Barrier
start_stop_barrier_
;
Mutex
end_cond_mutex_
;
Condition
end_condition_
;
};
}
// namespace internal
}
// namespace benchmark
#endif // BENCHMARK_THREAD_MANAGER_H
src/thread_timer.h
0 → 100644
View file @
674d0498
#ifndef BENCHMARK_THREAD_TIMER_H
#define BENCHMARK_THREAD_TIMER_H
#include "check.h"
#include "timers.h"
namespace
benchmark
{
namespace
internal
{
class
ThreadTimer
{
public
:
ThreadTimer
()
=
default
;
// Called by each thread
void
StartTimer
()
{
running_
=
true
;
start_real_time_
=
ChronoClockNow
();
start_cpu_time_
=
ThreadCPUUsage
();
}
// Called by each thread
void
StopTimer
()
{
CHECK
(
running_
);
running_
=
false
;
real_time_used_
+=
ChronoClockNow
()
-
start_real_time_
;
// Floating point error can result in the subtraction producing a negative
// time. Guard against that.
cpu_time_used_
+=
std
::
max
<
double
>
(
ThreadCPUUsage
()
-
start_cpu_time_
,
0
);
}
// Called by each thread
void
SetIterationTime
(
double
seconds
)
{
manual_time_used_
+=
seconds
;
}
bool
running
()
const
{
return
running_
;
}
// REQUIRES: timer is not running
double
real_time_used
()
{
CHECK
(
!
running_
);
return
real_time_used_
;
}
// REQUIRES: timer is not running
double
cpu_time_used
()
{
CHECK
(
!
running_
);
return
cpu_time_used_
;
}
// REQUIRES: timer is not running
double
manual_time_used
()
{
CHECK
(
!
running_
);
return
manual_time_used_
;
}
private
:
bool
running_
=
false
;
// Is the timer running
double
start_real_time_
=
0
;
// If running_
double
start_cpu_time_
=
0
;
// If running_
// Accumulated time so far (does not contain current slice if running_)
double
real_time_used_
=
0
;
double
cpu_time_used_
=
0
;
// Manually set iteration time. User sets this with SetIterationTime(seconds).
double
manual_time_used_
=
0
;
};
}
// namespace internal
}
// namespace benchmark
#endif // BENCHMARK_THREAD_TIMER_H
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment