blob: f7a1d1c70c82830cb4f9c8c1fb44f45cf1c4ea18 [file] [log] [blame]
Moritz Pflanzerbeabe3b2017-08-31 14:56:32 +01001/*
2 * Copyright (c) 2017 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
30class profiler {
31private:
32 static const int maxevents = 10000;
33 unsigned long times[maxevents];
Pablo Tello6ff12a02017-11-02 16:09:35 +000034 unsigned long units[maxevents];
Moritz Pflanzerbeabe3b2017-08-31 14:56:32 +010035 int events[maxevents];
36 int currentevent;
37 int countfd;
38
39public:
40 profiler() {
41 currentevent=0;
42 countfd=open_cycle_counter();
43 }
44
45 ~profiler() {
46 close(countfd);
47 int tots[5];
48 unsigned long counts[5];
Pablo Tello6ff12a02017-11-02 16:09:35 +000049 unsigned long tunits[5];
Moritz Pflanzerbeabe3b2017-08-31 14:56:32 +010050 const char * descs[] = { "Prepare A", "Prepare B", "Kernel", "Merge" };
51
52 for (int i=1; i<5; i++) {
53 tots[i] = 0;
54 counts[i] = 0;
Pablo Tello6ff12a02017-11-02 16:09:35 +000055 tunits[i] = 0;
Moritz Pflanzerbeabe3b2017-08-31 14:56:32 +010056 }
57
58 printf("Profiled events:\n");
59 for (int i=0; i<currentevent; i++) {
Moritz Pflanzerbeabe3b2017-08-31 14:56:32 +010060 tots[events[i]]++;
61 counts[events[i]] += times[i];
Pablo Tello6ff12a02017-11-02 16:09:35 +000062 tunits[events[i]] += units[i];
Moritz Pflanzerbeabe3b2017-08-31 14:56:32 +010063 }
64
Pablo Tello6ff12a02017-11-02 16:09:35 +000065 printf("%20s %9s %9s %9s %12s %9s\n", "", "Events", "Total", "Average", "Bytes/MACs", "Per cycle");
Moritz Pflanzerbeabe3b2017-08-31 14:56:32 +010066 for (int i=1; i<5; i++) {
Pablo Tello6ff12a02017-11-02 16:09:35 +000067 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]);
Moritz Pflanzerbeabe3b2017-08-31 14:56:32 +010068 }
69 }
70
71 template <typename T>
Pablo Tello6ff12a02017-11-02 16:09:35 +000072 void operator() (int i, unsigned long u, T func) {
Moritz Pflanzerbeabe3b2017-08-31 14:56:32 +010073 if (currentevent==maxevents) {
74 func();
75 } else {
Pablo Tello6ff12a02017-11-02 16:09:35 +000076 events[currentevent] = i;
77 units[currentevent] = u;
Moritz Pflanzerbeabe3b2017-08-31 14:56:32 +010078 start_counter(countfd);
79 func();
80 long long cycs = stop_counter(countfd);
Moritz Pflanzerbeabe3b2017-08-31 14:56:32 +010081 times[currentevent++] = cycs;
82 }
83 }
84};
85
86#else
87
88class profiler {
89public:
90 template <typename T>
Pablo Tello6ff12a02017-11-02 16:09:35 +000091 void operator() (int i, unsigned long u, T func) {
Moritz Pflanzerbeabe3b2017-08-31 14:56:32 +010092 func();
93 }
94};
95
96#endif
97
98#define PROFILE_PREPA 1
99#define PROFILE_PREPB 2
100#define PROFILE_KERNEL 3
101#define PROFILE_MERGE 4
Pablo Tello6ff12a02017-11-02 16:09:35 +0000102
103