blob: 143192b5893a26d89b353396ec679d1f7ed0b02f [file] [log] [blame]
Pablo Tello89519332017-11-17 11:52:36 +00001
2/*
3 * Copyright (c) 2017 ARM Limited.
4 *
5 * SPDX-License-Identifier: MIT
6 *
7 * Permission is hereby granted, free of charge, to any person obtaining a copy
8 * of this software and associated documentation files (the "Software"), to
9 * deal in the Software without restriction, including without limitation the
10 * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
11 * sell copies of the Software, and to permit persons to whom the Software is
12 * furnished to do so, subject to the following conditions:
13 *
14 * The above copyright notice and this permission notice shall be included in all
15 * copies or substantial portions of the Software.
16 *
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
23 * SOFTWARE.
24 */
25#pragma once
26
27#include <algorithm>
28#include <cmath>
29#include <cstring>
30#include <cstdio>
31#include <map>
32#include <vector>
33
34#include "perf.h"
35#include <unistd.h>
36
37class profiler {
38private:
39#ifdef CYCLE_PROFILING
40 struct ProfileEntry {
41 int event_id;
42 long int bytes_read, ops, bytes_written;
43 long int duration;
44 };
45
46 static const int maxevents = 10000;
47 ProfileEntry events[maxevents];
48 int currentevent;
49 int countfd;
50
51 std::map<const char *, int> event_ids;
52
53 int get_event_id(const char *id) {
54 if (!event_ids.count(id)) {
55 event_ids.emplace(id, event_ids.size());
56 }
57 return event_ids[id];
58 }
59#endif // CYCLE_PROFILING
60
61public:
62#ifdef CYCLE_PROFILING
63 profiler() {
64 currentevent = 0;
65 countfd = open_cycle_counter();
66 }
67
68 ~profiler() {
69 close(countfd);
70
71 // Compute performance from recorded events
72 struct ProfileResult {
73 ProfileResult() : total_calls(0),
74 total_duration(0),
75 total_bytes_read(0),
76 total_ops(0),
77 total_bytes_written(0) {
78 }
79
80 void operator+=(const ProfileEntry &rhs) {
81 total_calls++;
82 total_duration += rhs.duration;
83 total_bytes_read += rhs.bytes_read;
84 total_ops += rhs.ops;
85 total_bytes_written = rhs.bytes_written;
86 }
87
88 float avg_duration(void) const {
89 return static_cast<float>(total_duration) /
90 static_cast<float>(total_calls);
91 }
92
93 float bytes_read_per_cycle(void) const {
94 return static_cast<float>(total_bytes_read) /
95 static_cast<float>(total_duration);
96 }
97
98 float ops_per_cycle(void) const {
99 return static_cast<float>(total_ops) /
100 static_cast<float>(total_duration);
101 }
102
103 float bytes_written_per_cycle(void) const {
104 return static_cast<float>(total_bytes_written) /
105 static_cast<float>(total_duration);
106 }
107
108 long int total_calls,
109 total_duration,
110 total_bytes_read,
111 total_ops,
112 total_bytes_written;
113 };
114
115 std::vector<ProfileResult> totals;
116 totals.resize(event_ids.size());
117 for (int i = 0; i < currentevent; i++) {
118 const auto &event = events[i];
119 totals[event.event_id] += event;
120 }
121
122 // Get the longest label
123 int len_label = 0;
124 for (const auto &kv : event_ids) {
125 len_label = std::max(len_label, static_cast<int>(strlen(kv.first)));
126 }
127
128 // Get the longest values for every other field
129 const auto get_length_of_field =
130 [totals] (const char *title, auto f, auto len) -> size_t {
131 size_t l = strlen(title);
132 for (const auto &v : totals) {
133 l = std::max(l, len(f(v)));
134 }
135 return l;
136 };
137
138 // Get the strlen for an int
139 const auto intlen = [] (long int x) -> size_t {
140 size_t len = 0;
141 do {
142 x /= 10;
143 len++;
144 } while (x);
145 return len;
146 };
147
148 // Get the strlen for a float
149 const auto floatlen = [] (const int precision) {
150 return [precision] (float x) {
151 size_t len = 0;
152
153 if (!std::isfinite(x)) {
154 return static_cast<size_t>(3);
155 }
156
157 do {
158 x /= 10.0f;
159 len++;
160 } while (x > 1.0f);
161 return len + 1 + precision;
162 };
163 };
164
165 const int len_calls = get_length_of_field(
166 "Calls", [] (const auto &v) {return v.total_calls;},
167 intlen
168 );
169 const int len_duration = get_length_of_field(
170 "Duration", [] (const auto &v) {return v.total_duration;},
171 intlen
172 );
173 const int len_average_duration = get_length_of_field(
174 "Average", [] (const auto &v) {return v.avg_duration();},
175 floatlen(2)
176 );
177 const int len_reads_per_cycle = get_length_of_field(
178 "Reads / cycle",
179 [] (const auto &v) {return v.bytes_read_per_cycle();},
180 floatlen(6)
181 );
182 const int len_ops_per_cycle = get_length_of_field(
183 "Ops / cycle",
184 [] (const auto &v) {return v.ops_per_cycle();},
185 floatlen(6)
186 );
187 const int len_writes_per_cycle = get_length_of_field(
188 "Writes / cycle",
189 [] (const auto &v) {return v.bytes_written_per_cycle();},
190 floatlen(6)
191 );
192
193 // Print header
194 printf(
195 "%*s %*s %*s %*s %*s %*s %*s\n",
196 len_label, "",
197 len_calls, "Calls",
198 len_duration, "Duration",
199 len_average_duration, "Average",
200 len_reads_per_cycle, "Reads / cycle",
201 len_ops_per_cycle, "Ops / cycle",
202 len_writes_per_cycle, "Writes / cycle"
203 );
204 for (const auto &kv : event_ids) {
205 const auto id = kv.second;
206 printf(
207 "%*s %*ld %*ld %*.2f %*.6f %*.6f %*.6f\n",
208 len_label, kv.first,
209 len_calls, totals[id].total_calls,
210 len_duration, totals[id].total_duration,
211 len_average_duration, totals[id].avg_duration(),
212 len_reads_per_cycle, totals[id].bytes_read_per_cycle(),
213 len_ops_per_cycle, totals[id].ops_per_cycle(),
214 len_writes_per_cycle, totals[id].bytes_written_per_cycle()
215 );
216 }
217 printf("\n");
218 }
219#endif // CYCLE_PROFILING
220
221 template <typename T>
222 void operator() (const char * event,
223 T func,
224 long int bytes_read = 0,
225 long int ops = 0,
226 long int bytes_written = 0) {
227#ifdef CYCLE_PROFILING
228 if (currentevent==maxevents) {
229 func();
230 } else {
231 start_counter(countfd);
232 func();
233 long long cycs = stop_counter(countfd);
234
235 // Store the profiling data
236 events[currentevent++] = {
237 get_event_id(event), bytes_read, ops, bytes_written, cycs
238 };
239 }
240#else
241 func();
242#endif // CYCLE_PROFILING
243 }
244};