aboutsummaryrefslogtreecommitdiff
path: root/arm_compute/core/NEON/kernels/convolution/common/profiler.hpp
blob: 01fafa9604402aebede1fede602b17c7b1964e67 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
/*
 * Copyright (c) 2017 ARM Limited.
 *
 * SPDX-License-Identifier: MIT
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to
 * deal in the Software without restriction, including without limitation the
 * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
 * sell copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in all
 * copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 */

#pragma once

#include <algorithm>
#include <cmath>
#include <cstring>
#include <cstdio>
#include <map>
#include <mutex>
#include <thread>
#include <vector>

#include "perf.h"
#include <unistd.h>

#ifdef CYCLE_PROFILING
class EventIDContainer
{
  public:
  EventIDContainer() : container_lock(), event_ids()
  {
  }

  int get_event_id(const char *id)
  {
    std::lock_guard<std::mutex> lock(container_lock);
    if (!event_ids.count(id)) {
      event_ids.emplace(id, event_ids.size());
    }
    return event_ids[id];
  }

  unsigned int size() const
  {
    return event_ids.size();
  }

  auto begin()
  {
    return event_ids.begin();
  }

  auto end()
  {
    return event_ids.end();
  }

  private:
  std::mutex container_lock;
  std::map<const char *, int> event_ids;
};


class ThreadEventCounterContainer
{
  public:
  ThreadEventCounterContainer() : container_lock(), thread_counter_fds()
  {
  }

  int get_counter_fd()
  {
    const auto id = std::this_thread::get_id();
    std::lock_guard<std::mutex> lock(container_lock);
    if (!thread_counter_fds.count(id))
    {
      thread_counter_fds.emplace(id, open_cycle_counter());
    }
    return thread_counter_fds[id];
  }

  ~ThreadEventCounterContainer()
  {
    // Close all counter file descriptors
    for (auto& fd : thread_counter_fds)
    {
      close(fd.second);
    }
  }

  private:
  std::mutex container_lock;
  std::map<std::thread::id, int> thread_counter_fds;
};
#endif  // CYCLE_PROFILING


class profiler {
private:
#ifdef CYCLE_PROFILING
    struct ProfileEntry {
      int event_id;
      long int bytes_read, ops, bytes_written;
      long int duration;
    };

    static const int maxevents = 10000;
    ProfileEntry events[maxevents];
    int currentevent;
    std::mutex event_lock;

    EventIDContainer event_ids;
    ThreadEventCounterContainer thread_counter_fds;

    int get_event_id(const char *id)
    {
      return event_ids.get_event_id(id);
    }
#endif  // CYCLE_PROFILING

public:
#ifdef CYCLE_PROFILING
    profiler() :
      currentevent(0),
      event_lock(),
      event_ids(),
      thread_counter_fds()
    {
    }

    ~profiler() {
      std::lock_guard<std::mutex> lock_events(event_lock);

        // Compute performance from recorded events
        struct ProfileResult {
          ProfileResult() : total_calls(0),
                            total_duration(0),
                            total_bytes_read(0),
                            total_ops(0),
                            total_bytes_written(0) {
          }

          void operator+=(const ProfileEntry &rhs) {
            total_calls++;
            total_duration += rhs.duration;
            total_bytes_read += rhs.bytes_read;
            total_ops += rhs.ops;
            total_bytes_written = rhs.bytes_written;
          }

          float avg_duration(void) const {
            return static_cast<float>(total_duration) /
                   static_cast<float>(total_calls);
          }

          float bytes_read_per_cycle(void) const {
            return static_cast<float>(total_bytes_read) /
                   static_cast<float>(total_duration);
          }

          float ops_per_cycle(void) const {
            return static_cast<float>(total_ops) /
                   static_cast<float>(total_duration);
          }

          float bytes_written_per_cycle(void) const {
            return static_cast<float>(total_bytes_written) /
                   static_cast<float>(total_duration);
          }

          long int total_calls,
                   total_duration,
                   total_bytes_read,
                   total_ops,
                   total_bytes_written;
        };

        std::vector<ProfileResult> totals;
        totals.resize(event_ids.size());
        for (int i = 0; i < currentevent; i++) {
          const auto &event = events[i];
          totals[event.event_id] += event;
        }

        // Get the longest label
        int len_label = 0;
        for (const auto &kv : event_ids) {
          len_label = std::max(len_label, static_cast<int>(strlen(kv.first)));
        }

        // Get the longest values for every other field
        const auto get_length_of_field =
          [totals] (const char *title, auto f, auto len) -> size_t {
            size_t l = strlen(title);
            for (const auto &v : totals) {
              l = std::max(l, len(f(v)));
            }
            return l;
        };

        // Get the strlen for an int
        const auto intlen = [] (long int x) -> size_t {
          size_t len = 0;
          do {
            x /= 10;
            len++;
          } while (x);
          return len;
        };

        // Get the strlen for a float
        const auto floatlen = [] (const int precision) {
          return [precision] (float x) {
            size_t len = 0;

            if (!std::isfinite(x)) {
              return static_cast<size_t>(3);
            }

            do {
              x /= 10.0f;
              len++;
            } while (x > 1.0f);
            return len + 1 + precision;
          };
        };

        const int len_calls = get_length_of_field(
            "Calls", [] (const auto &v) {return v.total_calls;},
            intlen
        );
        const int len_duration = get_length_of_field(
            "Duration", [] (const auto &v) {return v.total_duration;},
            intlen
        );
        const int len_average_duration = get_length_of_field(
            "Average", [] (const auto &v) {return v.avg_duration();},
            floatlen(2)
        );
        const int len_reads_per_cycle = get_length_of_field(
            "Reads / cycle",
            [] (const auto &v) {return v.bytes_read_per_cycle();},
            floatlen(6)
        );
        const int len_ops_per_cycle = get_length_of_field(
            "Ops / cycle",
            [] (const auto &v) {return v.ops_per_cycle();},
            floatlen(6)
        );
        const int len_writes_per_cycle = get_length_of_field(
            "Writes / cycle",
            [] (const auto &v) {return v.bytes_written_per_cycle();},
            floatlen(6)
        );

        // Print header
        printf(
          "%*s    %*s    %*s    %*s    %*s    %*s    %*s\n",
          len_label, "",
          len_calls, "Calls",
          len_duration, "Duration",
          len_average_duration, "Average",
          len_reads_per_cycle, "Reads / cycle",
          len_ops_per_cycle, "Ops / cycle",
          len_writes_per_cycle, "Writes / cycle"
        );
        for (const auto &kv : event_ids) {
          const auto id = kv.second;
          printf(
            "%*s    %*ld    %*ld    %*.2f    %*.6f    %*.6f    %*.6f\n",
            len_label, kv.first,
            len_calls, totals[id].total_calls,
            len_duration, totals[id].total_duration,
            len_average_duration, totals[id].avg_duration(),
            len_reads_per_cycle, totals[id].bytes_read_per_cycle(),
            len_ops_per_cycle, totals[id].ops_per_cycle(),
            len_writes_per_cycle, totals[id].bytes_written_per_cycle()
          );
        }
        printf("\n");
    }
#endif  // CYCLE_PROFILING

    template <typename T>
    void operator() (const char * event,
                     T func,
                     long int bytes_read = 0,
                     long int ops = 0,
                     long int bytes_written = 0) {
#ifdef CYCLE_PROFILING
        if (currentevent==maxevents) {
            func();
        } else {
            const auto countfd = thread_counter_fds.get_counter_fd();
            start_counter(countfd);
            func();
            long long cycs = stop_counter(countfd);

            // Store the profiling data
            std::lock_guard<std::mutex> lock_events(event_lock);
            events[currentevent++] = {
              get_event_id(event), bytes_read, ops, bytes_written, cycs
            };
        }
#else
      (void) event;
      (void) bytes_read;
      (void) ops;
      (void) bytes_written;
      func();
#endif  // CYCLE_PROFILING
    }
};