Port it to use the new Linux perf_event API rather than the old perfmon patch.
[beedb.git] / perf / perftest.h
blob4a7ca7bfeda63bc8bdad1e80b77ff337f8b841b3
1 /*
2 Copyright 2009 Kristian Nielsen
4 This file is part of BeeDB.
6 Foobar is free software: you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation, either version 2 of the License, or
9 (at your option) any later version.
11 Foobar is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with Foobar. If not, see <http://www.gnu.org/licenses/>.
22 Helper code for BeeDB performance regression testing.
25 #include <stdarg.h>
27 /* For now, we are libperfmon specific. */
28 #include <perfmon/pfmlib_perf_event.h>
31 class perftest {
32 public:
33 /* The number of times to run each test benchmark different measurements. */
34 static const int num_runs= 6;
36 /* Virtual class, performance tests are implemented as derived classes. */
37 class test {
38 public:
40 Constructor for one test case.
42 Test description parameters:
44 text
45 Overall name of the test. Eg. "btree insert", or "memcpy".
47 variant
48 Identifies different variants of the test identified by parameter
49 text. Could be different implementations of the same algorithm
50 compared against each other. Idea is it makes sense to directly
51 compare performance of different variants against each other.
53 param1
54 param2
55 These are arbitrary parameters that the performance could be graphed
56 against. Idea is that the tests will run for different values of
57 these parameters, with eg. graphs drawn with performance as a
58 function of these parameters. Either or both can be left NULL,
59 meaning there is no such parameter to graph against. Could be eg.
60 buffer size, or number of columns in a row.
62 workunits
63 This is the amount of work done during one iteration of the loop
64 being timed in the run() method. Idea is that workunits*iterations
65 would be some kind of measure of total amount of work done.
67 Note that it is explicitly permitted to pass pointers for text, variant,
68 param1, and param2 that are initialised only after calling this
69 constructor (but before calling run()).
71 test(perftest *tester, const char *text, const char *variant= NULL,
72 const char *param1= NULL, const char *param2= NULL,
73 uint64_t workunits= 0);
74 virtual void run(uint64_t loops) = 0;
75 virtual ~test() = 0;
77 double gettime() { return tester->gettime(); }
78 void force_value(uint64_t value) { tester->force_value(value); }
79 void fatal_error(const char *format, ...) NORETURN PRINTF_LIKE(2, 3);
81 void prepare(int run) { current_run= run; }
83 const char *text;
84 const char *variant;
85 const char *param1;
86 const char *param2;
87 uint64_t workunits;
88 double elapsed_time[num_runs];
90 protected:
92 Start the timer running.
93 Call just before starting the computation to be tested.
95 void start();
96 void record_time(const char *text);
98 perftest *const tester;
100 private:
101 int current_run;
102 double start_time;
105 /* Methods. */
106 perftest();
107 ~perftest();
108 void run_test(test *t, uint64_t loops);
109 void start_perfmon(int run);
110 void record_perfmon(int run);
111 void record_perfmon_not_time_critical(int run);
112 void report_perfmon();
114 static double gettime();
115 static void force_value(uint64_t value);
116 static void fatal_error(const char *format, ...) NORETURN PRINTF_LIKE(1, 2);
117 static void fatal_error(const char *format, va_list ap) NORETURN PRINTF_LIKE(1, 0);
119 /* perf_event specific stuff. */
120 static const unsigned int num_fixed_counters= 2;
121 static const unsigned int num_unfixed_counters= 2;
122 static const unsigned int num_counters=
123 num_fixed_counters + num_unfixed_counters;
125 private:
126 perf_event_attr perf_attr[num_runs][num_counters];
127 const char *perf_event_names[num_runs][num_counters];
128 uint64_t perf_counter_values[num_runs][num_counters];
129 int perf_fds[num_counters];
132 Quick&dirty way to disable all performance counter usage (to allow
133 external profiling tools to be used.
135 static const bool skip_perfmon= false;
137 void prepare_perfmon_event(int idx, int run, const char *event_name);
138 void setup_perfmon();
141 #include "port/perftest.h"