blob: 1b944c4ccd7af94f8c27272c2ba5ce435b4e1865 [file] [log] [blame]
Pablo Telloeb82fd22018-02-23 13:43:50 +00001/*
2 * Copyright (c) 2017-2018 ARM Limited.
3 *
4 * SPDX-License-Identifier: MIT
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to
8 * deal in the Software without restriction, including without limitation the
9 * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
10 * sell copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included in all
14 * copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22 * SOFTWARE.
23 */
24#pragma once
25
26#ifdef CYCLE_PROFILING
27
28#include "../perf.h"
29
30#ifndef NO_MULTI_THREADING
31#include <mutex>
32#endif
33
Anthony Barbier5f707732018-07-03 16:22:02 +010034namespace arm_gemm {
35
Pablo Telloeb82fd22018-02-23 13:43:50 +000036#ifndef NO_MULTI_THREADING
37extern std::mutex report_mutex;
38#endif
39
Anthony Barbier5f707732018-07-03 16:22:02 +010040class profiler {
Pablo Telloeb82fd22018-02-23 13:43:50 +000041private:
Anthony Barbier5f707732018-07-03 16:22:02 +010042 static const int maxevents = 100000;
43 unsigned long times[maxevents] = { };
44 unsigned long units[maxevents] = { };
45 int events[maxevents] = { };
46 int currentevent=0;
47 int countfd=0;
Pablo Telloeb82fd22018-02-23 13:43:50 +000048
Anthony Barbier5f707732018-07-03 16:22:02 +010049 class ScopedProfilerClass {
Michalis Spyroue7e96e02018-04-13 13:44:10 +010050 private:
51 profiler &_parent;
Anthony Barbier5f707732018-07-03 16:22:02 +010052 bool legal=false;
Michalis Spyroue7e96e02018-04-13 13:44:10 +010053
54 public:
Anthony Barbier5f707732018-07-03 16:22:02 +010055 ScopedProfilerClass(profiler &prof, int i, unsigned long u) : _parent(prof) {
56 if (prof.currentevent==maxevents)
Michalis Spyroue7e96e02018-04-13 13:44:10 +010057 return;
58
Anthony Barbier5f707732018-07-03 16:22:02 +010059 prof.events[prof.currentevent]=i;
60 prof.units[prof.currentevent]=u;
61 legal=true;
Michalis Spyroue7e96e02018-04-13 13:44:10 +010062 start_counter(prof.countfd);
63 }
64
Anthony Barbier5f707732018-07-03 16:22:02 +010065 ~ScopedProfilerClass() {
66 if (!legal) return;
Michalis Spyroue7e96e02018-04-13 13:44:10 +010067
Anthony Barbier5f707732018-07-03 16:22:02 +010068 long long cycs = stop_counter(_parent.countfd);
Michalis Spyroue7e96e02018-04-13 13:44:10 +010069 _parent.times[_parent.currentevent++] = cycs;
70 }
71 };
72
Pablo Telloeb82fd22018-02-23 13:43:50 +000073public:
Anthony Barbier5f707732018-07-03 16:22:02 +010074 profiler() {
75 countfd=open_cycle_counter();
Pablo Telloeb82fd22018-02-23 13:43:50 +000076 }
77
Anthony Barbier5f707732018-07-03 16:22:02 +010078 ~profiler() {
Pablo Telloeb82fd22018-02-23 13:43:50 +000079 close(countfd);
Anthony Barbier5f707732018-07-03 16:22:02 +010080 int tots[5];
Pablo Telloeb82fd22018-02-23 13:43:50 +000081 unsigned long counts[5];
82 unsigned long tunits[5];
Anthony Barbier5f707732018-07-03 16:22:02 +010083 const char * descs[] = { "Prepare A", "Prepare B", "Kernel", "Merge" };
Pablo Telloeb82fd22018-02-23 13:43:50 +000084
Anthony Barbier5f707732018-07-03 16:22:02 +010085 for (int i=1; i<5; i++) {
86 tots[i] = 0;
Pablo Telloeb82fd22018-02-23 13:43:50 +000087 counts[i] = 0;
88 tunits[i] = 0;
89 }
90
Anthony Barbier5f707732018-07-03 16:22:02 +010091 for (int i=0; i<currentevent; i++) {
92// printf("%10s: %ld\n", descs[events[i]-1], times[i]);
Pablo Telloeb82fd22018-02-23 13:43:50 +000093 tots[events[i]]++;
94 counts[events[i]] += times[i];
95 tunits[events[i]] += units[i];
96 }
97
98#ifdef NO_MULTI_THREADING
99 printf("Profiled events:\n");
100#else
101 std::lock_guard<std::mutex> lock(report_mutex);
102 printf("Profiled events (cpu %d):\n", sched_getcpu());
103#endif
104
105 printf("%20s %9s %9s %9s %12s %9s\n", "", "Events", "Total", "Average", "Bytes/MACs", "Per cycle");
Anthony Barbier5f707732018-07-03 16:22:02 +0100106 for (int i=1; i<5; i++) {
107 printf("%20s: %9d %9ld %9ld %12lu %9.2f\n",descs[i-1],tots[i],counts[i],counts[i]/tots[i],tunits[i],(float)tunits[i]/counts[i]);
Pablo Telloeb82fd22018-02-23 13:43:50 +0000108 }
109 }
110
111 template <typename T>
Anthony Barbier5f707732018-07-03 16:22:02 +0100112 void operator() (int i, unsigned long u, T func) {
113 if (currentevent==maxevents) {
Pablo Telloeb82fd22018-02-23 13:43:50 +0000114 func();
Anthony Barbier5f707732018-07-03 16:22:02 +0100115 } else {
Pablo Telloeb82fd22018-02-23 13:43:50 +0000116 events[currentevent] = i;
Anthony Barbier5f707732018-07-03 16:22:02 +0100117 units[currentevent] = u;
Pablo Telloeb82fd22018-02-23 13:43:50 +0000118 start_counter(countfd);
119 func();
Anthony Barbier5f707732018-07-03 16:22:02 +0100120 long long cycs = stop_counter(countfd);
Pablo Telloeb82fd22018-02-23 13:43:50 +0000121 times[currentevent++] = cycs;
122 }
123 }
Anthony Barbier5f707732018-07-03 16:22:02 +0100124
125 ScopedProfilerClass ScopedProfiler(int i, unsigned long u) {
Michalis Spyroue7e96e02018-04-13 13:44:10 +0100126 return ScopedProfilerClass(*this, i, u);
Pablo Telloeb82fd22018-02-23 13:43:50 +0000127 }
128};
129
130#endif // CYCLE_PROFILING
131
132} // namespace arm_gemm
133
134#define PROFILE_PREPA 1
135#define PROFILE_PREPB 2
136#define PROFILE_KERNEL 3
137#define PROFILE_MERGE 4