Skip to content

Commit 55de4a4

Browse files
author
Henry Qin
committed
Add utility for auto-benchmarking single function.
This change also separates Stats.h into StatsMinimal.h, which is directly useable by C.
1 parent 8f5fc6d commit 55de4a4

9 files changed

Lines changed: 257 additions & 26 deletions

File tree

Makefile

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ ifndef CHECK_TARGET
1717
CHECK_TARGET=$$(find $(SRC_DIR) $(WRAPPER_DIR) '(' -name '*.h' -or -name '*.cc' ')' -not -path '$(TOP)/googletest/*' )
1818
endif
1919

20-
OBJECT_NAMES := CacheTrace.o TimeTrace.o Cycles.o Util.o Stats.o mkdir.o timetrace_wrapper.o cycles_wrapper.o
20+
OBJECT_NAMES := CacheTrace.o TimeTrace.o Cycles.o Util.o Stats.o Perf.o mkdir.o timetrace_wrapper.o cycles_wrapper.o perf_wrapper.o
2121

2222
OBJECTS = $(patsubst %,$(OBJECT_DIR)/%,$(OBJECT_NAMES))
2323
HEADERS= $(shell find $(SRC_DIR) $(WRAPPER_DIR) -name '*.h')
@@ -40,6 +40,9 @@ $(OBJECT_DIR)/timetrace_wrapper_test: $(OBJECT_DIR)/timetrace_wrapper_test.o $(O
4040
$(OBJECT_DIR)/cycles_wrapper_test: $(OBJECT_DIR)/cycles_wrapper_test.o $(OBJECT_DIR)/libPerfUtils.a
4141
$(CC) $(CFLAGS) -lstdc++ -o $@ $^
4242

43+
$(OBJECT_DIR)/perf_wrapper_test: $(OBJECT_DIR)/perf_wrapper_test.o $(OBJECT_DIR)/libPerfUtils.a
44+
$(CC) $(CFLAGS) -lstdc++ -o $@ $^
45+
4346
-include $(DEP)
4447

4548
$(OBJECT_DIR)/%.d: $(WRAPPER_DIR)/%.c | $(OBJECT_DIR)
@@ -73,13 +76,18 @@ GMOCK_DIR=../googletest/googlemock
7376
TEST_LIBS=-Lobj/ -lPerfUtils $(OBJECT_DIR)/libgtest.a $(OBJECT_DIR)/libgmock.a
7477
INCLUDE+=-I${GTEST_DIR}/include -I${GMOCK_DIR}/include
7578

76-
test: $(OBJECT_DIR)/UtilTest
79+
test: $(OBJECT_DIR)/UtilTest $(OBJECT_DIR)/PerfTest
7780
$(OBJECT_DIR)/UtilTest
81+
$(OBJECT_DIR)/PerfTest
7882

7983
$(OBJECT_DIR)/UtilTest: $(OBJECT_DIR)/UtilTest.o $(OBJECT_DIR)/libgtest.a $(OBJECT_DIR)/libgmock.a \
8084
$(OBJECT_DIR)/libPerfUtils.a
8185
$(CXX) $(INCLUDE) $(CXXFLAGS) $< $(GTEST_DIR)/src/gtest_main.cc $(TEST_LIBS) $(LIBS) -o $@
8286

87+
$(OBJECT_DIR)/PerfTest: $(OBJECT_DIR)/PerfTest.o $(OBJECT_DIR)/libgtest.a $(OBJECT_DIR)/libgmock.a \
88+
$(OBJECT_DIR)/libPerfUtils.a
89+
$(CXX) $(INCLUDE) $(CXXFLAGS) $< $(GTEST_DIR)/src/gtest_main.cc $(TEST_LIBS) $(LIBS) -o $@
90+
8391
$(OBJECT_DIR)/libgtest.a:
8492
$(CXX) -I${GTEST_DIR}/include -I${GTEST_DIR} \
8593
-pthread -c ${GTEST_DIR}/src/gtest-all.cc \

cwrapper/perf_wrapper.cc

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
/* Copyright (c) 2018 Stanford University
2+
*
3+
* Permission to use, copy, modify, and distribute this software for any
4+
* purpose with or without fee is hereby granted, provided that the above
5+
* copyright notice and this permission notice appear in all copies.
6+
*
7+
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR(S) DISCLAIM ALL WARRANTIES
8+
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
9+
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL AUTHORS BE LIABLE FOR
10+
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
11+
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
12+
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
13+
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
14+
*/
15+
16+
#include "Perf.h"
17+
18+
#ifdef __cplusplus
19+
extern "C" {
20+
#endif
21+
Statistics
22+
bench(void (*function)(void), int numIterations) {
23+
return PerfUtils::bench(function, numIterations);
24+
}
25+
26+
#ifdef __cplusplus
27+
}
28+
#endif

cwrapper/perf_wrapper.h

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
/* Copyright (c) 2018 Stanford University
2+
*
3+
* Permission to use, copy, modify, and distribute this software for any
4+
* purpose with or without fee is hereby granted, provided that the above
5+
* copyright notice and this permission notice appear in all copies.
6+
*
7+
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR(S) DISCLAIM ALL WARRANTIES
8+
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
9+
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL AUTHORS BE LIABLE FOR
10+
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
11+
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
12+
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
13+
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
14+
*/
15+
16+
#ifndef WRAPPER_PERFUTIL_PERF_H
17+
#define WRAPPER_PERFUTIL_PERF_H
18+
19+
20+
#ifdef __cplusplus
21+
extern "C" {
22+
#endif
23+
24+
#include "StatsMinimal.h"
25+
26+
typedef struct Statistics Statistics;
27+
Statistics bench(void (*function)(void), int numIterations);
28+
29+
#ifdef __cplusplus
30+
}
31+
#endif
32+
33+
#endif // WRAPPER_PERFUTIL_PERF_H

cwrapper/perf_wrapper_test.c

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
/* Copyright (c) 2018 Stanford University
2+
*
3+
* Permission to use, copy, modify, and distribute this software for any purpose
4+
* with or without fee is hereby granted, provided that the above copyright
5+
* notice and this permission notice appear in all copies.
6+
*
7+
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR(S) DISCLAIM ALL WARRANTIES
8+
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
9+
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL AUTHORS BE LIABLE FOR ANY
10+
* SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER
11+
* RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF
12+
* CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
13+
* CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
14+
*/
15+
16+
#include "perf_wrapper.h"
17+
#include <stdio.h>
18+
19+
#define RED(X) "\033[31m" X "\033[0m"
20+
#define GREEN(X) "\033[32m" X "\033[0m"
21+
22+
void fixedCycles(int N) {
23+
for (int i = 0; i < N; i++) {
24+
asm volatile("nop");
25+
}
26+
}
27+
28+
void fiveHundredCycles() {
29+
fixedCycles(500);
30+
}
31+
32+
int
33+
main() {
34+
Statistics stats = bench(fiveHundredCycles, 100000);
35+
if (stats.count == 100000 && stats.count > stats.min) {
36+
puts(GREEN("perf_wrapper_test PASSED"));
37+
} else {
38+
puts(RED("perf_wrapper FAILED"));
39+
}
40+
}

src/Perf.cc

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
/* Copyright (c) 2012 Stanford University
2+
*
3+
* Permission to use, copy, modify, and distribute this software for any
4+
* purpose with or without fee is hereby granted, provided that the above
5+
* copyright notice and this permission notice appear in all copies.
6+
*
7+
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR(S) DISCLAIM ALL WARRANTIES
8+
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
9+
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL AUTHORS BE LIABLE FOR
10+
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
11+
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
12+
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
13+
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
14+
*/
15+
#include "Perf.h"
16+
17+
#include <string.h>
18+
#include <stdint.h>
19+
20+
#include "Cycles.h"
21+
22+
namespace PerfUtils {
23+
24+
/**
25+
* Run the given function for numIterations, and compute statistics on the run times.
26+
*/
27+
Statistics bench(void (*function)(void), int numIterations) {
28+
uint64_t* latencies = new uint64_t[numIterations];
29+
uint64_t startTime;
30+
31+
// Page in the memory
32+
memset(latencies, 0, numIterations * sizeof(uint64_t));
33+
34+
for (int i = 0; i < numIterations; i++) {
35+
startTime = Cycles::rdtsc();
36+
function();
37+
latencies[i] = Cycles::rdtsc() - startTime;
38+
}
39+
40+
Statistics stats = computeStatistics(latencies, numIterations);
41+
delete[] latencies;
42+
return stats;
43+
}
44+
}

src/Perf.h

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
/* Copyright (c) 2012 Stanford University
2+
*
3+
* Permission to use, copy, modify, and distribute this software for any
4+
* purpose with or without fee is hereby granted, provided that the above
5+
* copyright notice and this permission notice appear in all copies.
6+
*
7+
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR(S) DISCLAIM ALL WARRANTIES
8+
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
9+
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL AUTHORS BE LIABLE FOR
10+
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
11+
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
12+
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
13+
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
14+
*/
15+
#include "Stats.h"
16+
namespace PerfUtils {
17+
Statistics bench(void (*function)(void), int numIterations);
18+
}

src/PerfTest.cc

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
/* Copyright (c) 2014-2017 Stanford University
2+
*
3+
* Permission to use, copy, modify, and distribute this software for any purpose
4+
* with or without fee is hereby granted, provided that the above copyright
5+
* notice and this permission notice appear in all copies.
6+
*
7+
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR(S) DISCLAIM ALL WARRANTIES
8+
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
9+
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL AUTHORS BE LIABLE FOR ANY
10+
* SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER
11+
* RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF
12+
* CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
13+
* CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
14+
*/
15+
16+
#include "Perf.h"
17+
18+
#include <fcntl.h>
19+
#include <string.h>
20+
#include <sys/stat.h>
21+
#include <sys/types.h>
22+
23+
#include "gmock/gmock.h"
24+
#include "gtest/gtest.h"
25+
26+
27+
void fixedCycles(int N) {
28+
for (int i = 0; i < N; i++) {
29+
asm volatile("nop");
30+
}
31+
}
32+
33+
TEST(PerfTest, bench) {
34+
Statistics stats = PerfUtils::bench([]() {fixedCycles(500);}, 100000);
35+
EXPECT_EQ(100000, stats.count);
36+
EXPECT_LE(20, stats.min);
37+
}

src/Stats.h

Lines changed: 1 addition & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -16,30 +16,7 @@
1616
#ifndef PERFUTILS_STATS_H
1717
#define PERFUTILS_STATS_H
1818

19-
#include <stddef.h>
20-
#include <stdint.h>
21-
22-
struct Statistics {
23-
size_t count;
24-
uint64_t average;
25-
uint64_t min;
26-
uint64_t median;
27-
uint64_t P10;
28-
uint64_t P20;
29-
uint64_t P30;
30-
uint64_t P40;
31-
uint64_t P50;
32-
uint64_t P60;
33-
uint64_t P70;
34-
uint64_t P80;
35-
uint64_t P90;
36-
uint64_t P99;
37-
uint64_t P999;
38-
uint64_t P9999;
39-
uint64_t max;
40-
};
41-
42-
Statistics computeStatistics(uint64_t* rawdata, size_t count);
19+
#include "StatsMinimal.h"
4320

4421
void printStatistics(const char* label, uint64_t* rawdata, size_t count,
4522
const char* datadir = NULL);

src/StatsMinimal.h

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
/* Copyright (c) 2017 Stanford University
2+
*
3+
* Permission to use, copy, modify, and distribute this software for any
4+
* purpose with or without fee is hereby granted, provided that the above
5+
* copyright notice and this permission notice appear in all copies.
6+
*
7+
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR(S) DISCLAIM ALL WARRANTIES
8+
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
9+
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL AUTHORS BE LIABLE FOR
10+
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
11+
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
12+
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
13+
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
14+
*/
15+
#ifndef PERFUTILS_STATS_MINIMAL_H
16+
#define PERFUTILS_STATS_MINIMAL_H
17+
18+
#include <stdint.h>
19+
#include <stddef.h>
20+
21+
/**
22+
* This file exists to make the computeStatistics function available from C
23+
* without an extra wrapper around the stats component of PerfUtils.
24+
*/
25+
struct Statistics {
26+
size_t count;
27+
uint64_t average;
28+
uint64_t min;
29+
uint64_t median;
30+
uint64_t P10;
31+
uint64_t P20;
32+
uint64_t P30;
33+
uint64_t P40;
34+
uint64_t P50;
35+
uint64_t P60;
36+
uint64_t P70;
37+
uint64_t P80;
38+
uint64_t P90;
39+
uint64_t P99;
40+
uint64_t P999;
41+
uint64_t P9999;
42+
uint64_t max;
43+
};
44+
45+
struct Statistics computeStatistics(uint64_t* rawdata, size_t count);
46+
#endif // PERFUTILS_STATS_MINIMAL_H

0 commit comments

Comments
 (0)