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
4a3348ce
Commit
4a3348ce
authored
Oct 12, 2014
by
x.zhou
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Turn the global mutex into instance variables
To fix #52
parent
0fcd190c
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
14 additions
and
12 deletions
+14
-12
benchmark.h
include/benchmark/benchmark.h
+2
-0
benchmark.cc
src/benchmark.cc
+12
-12
No files found.
include/benchmark/benchmark.h
View file @
4a3348ce
...
@@ -142,6 +142,7 @@ BENCHMARK(BM_MultiThreaded)->Threads(4);
...
@@ -142,6 +142,7 @@ BENCHMARK(BM_MultiThreaded)->Threads(4);
#include <string>
#include <string>
#include <thread>
#include <thread>
#include <vector>
#include <vector>
#include <mutex>
#include "macros.h"
#include "macros.h"
...
@@ -462,6 +463,7 @@ class Benchmark {
...
@@ -462,6 +463,7 @@ class Benchmark {
std
::
vector
<
int
>
rangeX_
;
std
::
vector
<
int
>
rangeX_
;
std
::
vector
<
int
>
rangeY_
;
std
::
vector
<
int
>
rangeY_
;
std
::
vector
<
int
>
thread_counts_
;
std
::
vector
<
int
>
thread_counts_
;
std
::
mutex
mutex_
;
// Special value placed in thread_counts_ to stand for NumCPUs()
// Special value placed in thread_counts_ to stand for NumCPUs()
static
const
int
kNumCpuMarker
=
-
1
;
static
const
int
kNumCpuMarker
=
-
1
;
...
...
src/benchmark.cc
View file @
4a3348ce
...
@@ -184,7 +184,6 @@ inline std::string HumanReadableNumber(double n) {
...
@@ -184,7 +184,6 @@ inline std::string HumanReadableNumber(double n) {
// For non-dense Range, intermediate values are powers of kRangeMultiplier.
// For non-dense Range, intermediate values are powers of kRangeMultiplier.
static
const
int
kRangeMultiplier
=
8
;
static
const
int
kRangeMultiplier
=
8
;
static
std
::
mutex
benchmark_mutex
;
std
::
mutex
starting_mutex
;
std
::
mutex
starting_mutex
;
std
::
condition_variable
starting_cv
;
std
::
condition_variable
starting_cv
;
...
@@ -326,6 +325,7 @@ class BenchmarkFamilies {
...
@@ -326,6 +325,7 @@ class BenchmarkFamilies {
~
BenchmarkFamilies
();
~
BenchmarkFamilies
();
std
::
vector
<
Benchmark
*>
families_
;
std
::
vector
<
Benchmark
*>
families_
;
std
::
mutex
mutex_
;
};
};
BenchmarkFamilies
*
BenchmarkFamilies
::
GetInstance
()
{
BenchmarkFamilies
*
BenchmarkFamilies
::
GetInstance
()
{
...
@@ -342,7 +342,7 @@ BenchmarkFamilies::~BenchmarkFamilies() {
...
@@ -342,7 +342,7 @@ BenchmarkFamilies::~BenchmarkFamilies() {
}
}
int
BenchmarkFamilies
::
AddBenchmark
(
Benchmark
*
family
)
{
int
BenchmarkFamilies
::
AddBenchmark
(
Benchmark
*
family
)
{
std
::
lock_guard
<
std
::
mutex
>
l
(
benchmark_mutex
);
std
::
lock_guard
<
std
::
mutex
>
l
(
mutex_
);
// This loop attempts to reuse an entry that was previously removed to avoid
// This loop attempts to reuse an entry that was previously removed to avoid
// unncessary growth of the vector.
// unncessary growth of the vector.
for
(
size_t
index
=
0
;
index
<
families_
.
size
();
++
index
)
{
for
(
size_t
index
=
0
;
index
<
families_
.
size
();
++
index
)
{
...
@@ -357,7 +357,7 @@ int BenchmarkFamilies::AddBenchmark(Benchmark* family) {
...
@@ -357,7 +357,7 @@ int BenchmarkFamilies::AddBenchmark(Benchmark* family) {
}
}
void
BenchmarkFamilies
::
RemoveBenchmark
(
int
index
)
{
void
BenchmarkFamilies
::
RemoveBenchmark
(
int
index
)
{
std
::
lock_guard
<
std
::
mutex
>
l
(
benchmark_mutex
);
std
::
lock_guard
<
std
::
mutex
>
l
(
mutex_
);
families_
[
index
]
=
NULL
;
families_
[
index
]
=
NULL
;
// Don't shrink families_ here, we might be called by the destructor of
// Don't shrink families_ here, we might be called by the destructor of
// BenchmarkFamilies which iterates over the vector.
// BenchmarkFamilies which iterates over the vector.
...
@@ -374,7 +374,7 @@ void BenchmarkFamilies::FindBenchmarks(
...
@@ -374,7 +374,7 @@ void BenchmarkFamilies::FindBenchmarks(
return
;
return
;
}
}
std
::
lock_guard
<
std
::
mutex
>
l
(
benchmark_mutex
);
std
::
lock_guard
<
std
::
mutex
>
l
(
mutex_
);
for
(
internal
::
Benchmark
*
family
:
families_
)
{
for
(
internal
::
Benchmark
*
family
:
families_
)
{
if
(
family
==
nullptr
)
continue
;
// Family was deleted
if
(
family
==
nullptr
)
continue
;
// Family was deleted
...
@@ -707,7 +707,7 @@ Benchmark::~Benchmark() {
...
@@ -707,7 +707,7 @@ Benchmark::~Benchmark() {
}
}
Benchmark
*
Benchmark
::
Arg
(
int
x
)
{
Benchmark
*
Benchmark
::
Arg
(
int
x
)
{
std
::
lock_guard
<
std
::
mutex
>
l
(
benchmark_mutex
);
std
::
lock_guard
<
std
::
mutex
>
l
(
mutex_
);
rangeX_
.
push_back
(
x
);
rangeX_
.
push_back
(
x
);
return
this
;
return
this
;
}
}
...
@@ -716,7 +716,7 @@ Benchmark* Benchmark::Range(int start, int limit) {
...
@@ -716,7 +716,7 @@ Benchmark* Benchmark::Range(int start, int limit) {
std
::
vector
<
int
>
arglist
;
std
::
vector
<
int
>
arglist
;
AddRange
(
&
arglist
,
start
,
limit
,
kRangeMultiplier
);
AddRange
(
&
arglist
,
start
,
limit
,
kRangeMultiplier
);
std
::
lock_guard
<
std
::
mutex
>
l
(
benchmark_mutex
);
std
::
lock_guard
<
std
::
mutex
>
l
(
mutex_
);
for
(
size_t
i
=
0
;
i
<
arglist
.
size
();
++
i
)
rangeX_
.
push_back
(
arglist
[
i
]);
for
(
size_t
i
=
0
;
i
<
arglist
.
size
();
++
i
)
rangeX_
.
push_back
(
arglist
[
i
]);
return
this
;
return
this
;
}
}
...
@@ -724,13 +724,13 @@ Benchmark* Benchmark::Range(int start, int limit) {
...
@@ -724,13 +724,13 @@ Benchmark* Benchmark::Range(int start, int limit) {
Benchmark
*
Benchmark
::
DenseRange
(
int
start
,
int
limit
)
{
Benchmark
*
Benchmark
::
DenseRange
(
int
start
,
int
limit
)
{
CHECK_GE
(
start
,
0
);
CHECK_GE
(
start
,
0
);
CHECK_LE
(
start
,
limit
);
CHECK_LE
(
start
,
limit
);
std
::
lock_guard
<
std
::
mutex
>
l
(
benchmark_mutex
);
std
::
lock_guard
<
std
::
mutex
>
l
(
mutex_
);
for
(
int
arg
=
start
;
arg
<=
limit
;
++
arg
)
rangeX_
.
push_back
(
arg
);
for
(
int
arg
=
start
;
arg
<=
limit
;
++
arg
)
rangeX_
.
push_back
(
arg
);
return
this
;
return
this
;
}
}
Benchmark
*
Benchmark
::
ArgPair
(
int
x
,
int
y
)
{
Benchmark
*
Benchmark
::
ArgPair
(
int
x
,
int
y
)
{
std
::
lock_guard
<
std
::
mutex
>
l
(
benchmark_mutex
);
std
::
lock_guard
<
std
::
mutex
>
l
(
mutex_
);
rangeX_
.
push_back
(
x
);
rangeX_
.
push_back
(
x
);
rangeY_
.
push_back
(
y
);
rangeY_
.
push_back
(
y
);
return
this
;
return
this
;
...
@@ -741,7 +741,7 @@ Benchmark* Benchmark::RangePair(int lo1, int hi1, int lo2, int hi2) {
...
@@ -741,7 +741,7 @@ Benchmark* Benchmark::RangePair(int lo1, int hi1, int lo2, int hi2) {
AddRange
(
&
arglist1
,
lo1
,
hi1
,
kRangeMultiplier
);
AddRange
(
&
arglist1
,
lo1
,
hi1
,
kRangeMultiplier
);
AddRange
(
&
arglist2
,
lo2
,
hi2
,
kRangeMultiplier
);
AddRange
(
&
arglist2
,
lo2
,
hi2
,
kRangeMultiplier
);
std
::
lock_guard
<
std
::
mutex
>
l
(
benchmark_mutex
);
std
::
lock_guard
<
std
::
mutex
>
l
(
mutex_
);
rangeX_
.
resize
(
arglist1
.
size
());
rangeX_
.
resize
(
arglist1
.
size
());
std
::
copy
(
arglist1
.
begin
(),
arglist1
.
end
(),
rangeX_
.
begin
());
std
::
copy
(
arglist1
.
begin
(),
arglist1
.
end
(),
rangeX_
.
begin
());
rangeY_
.
resize
(
arglist2
.
size
());
rangeY_
.
resize
(
arglist2
.
size
());
...
@@ -756,7 +756,7 @@ Benchmark* Benchmark::Apply(void (*custom_arguments)(Benchmark* benchmark)) {
...
@@ -756,7 +756,7 @@ Benchmark* Benchmark::Apply(void (*custom_arguments)(Benchmark* benchmark)) {
Benchmark
*
Benchmark
::
Threads
(
int
t
)
{
Benchmark
*
Benchmark
::
Threads
(
int
t
)
{
CHECK_GT
(
t
,
0
);
CHECK_GT
(
t
,
0
);
std
::
lock_guard
<
std
::
mutex
>
l
(
benchmark_mutex
);
std
::
lock_guard
<
std
::
mutex
>
l
(
mutex_
);
thread_counts_
.
push_back
(
t
);
thread_counts_
.
push_back
(
t
);
return
this
;
return
this
;
}
}
...
@@ -765,13 +765,13 @@ Benchmark* Benchmark::ThreadRange(int min_threads, int max_threads) {
...
@@ -765,13 +765,13 @@ Benchmark* Benchmark::ThreadRange(int min_threads, int max_threads) {
CHECK_GT
(
min_threads
,
0
);
CHECK_GT
(
min_threads
,
0
);
CHECK_GE
(
max_threads
,
min_threads
);
CHECK_GE
(
max_threads
,
min_threads
);
std
::
lock_guard
<
std
::
mutex
>
l
(
benchmark_mutex
);
std
::
lock_guard
<
std
::
mutex
>
l
(
mutex_
);
AddRange
(
&
thread_counts_
,
min_threads
,
max_threads
,
2
);
AddRange
(
&
thread_counts_
,
min_threads
,
max_threads
,
2
);
return
this
;
return
this
;
}
}
Benchmark
*
Benchmark
::
ThreadPerCpu
()
{
Benchmark
*
Benchmark
::
ThreadPerCpu
()
{
std
::
lock_guard
<
std
::
mutex
>
l
(
benchmark_mutex
);
std
::
lock_guard
<
std
::
mutex
>
l
(
mutex_
);
thread_counts_
.
push_back
(
NumCPUs
());
thread_counts_
.
push_back
(
NumCPUs
());
return
this
;
return
this
;
}
}
...
...
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