Pablo Tello | eb82fd2 | 2018-02-23 13:43:50 +0000 | [diff] [blame] | 1 | /* |
| 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 Barbier | 5f70773 | 2018-07-03 16:22:02 +0100 | [diff] [blame] | 34 | namespace arm_gemm { |
| 35 | |
Pablo Tello | eb82fd2 | 2018-02-23 13:43:50 +0000 | [diff] [blame] | 36 | #ifndef NO_MULTI_THREADING |
| 37 | extern std::mutex report_mutex; |
| 38 | #endif |
| 39 | |
Anthony Barbier | 5f70773 | 2018-07-03 16:22:02 +0100 | [diff] [blame] | 40 | class profiler { |
Pablo Tello | eb82fd2 | 2018-02-23 13:43:50 +0000 | [diff] [blame] | 41 | private: |
Anthony Barbier | 5f70773 | 2018-07-03 16:22:02 +0100 | [diff] [blame] | 42 | 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 Tello | eb82fd2 | 2018-02-23 13:43:50 +0000 | [diff] [blame] | 48 | |
Anthony Barbier | 5f70773 | 2018-07-03 16:22:02 +0100 | [diff] [blame] | 49 | class ScopedProfilerClass { |
Michalis Spyrou | e7e96e0 | 2018-04-13 13:44:10 +0100 | [diff] [blame] | 50 | private: |
| 51 | profiler &_parent; |
Anthony Barbier | 5f70773 | 2018-07-03 16:22:02 +0100 | [diff] [blame] | 52 | bool legal=false; |
Michalis Spyrou | e7e96e0 | 2018-04-13 13:44:10 +0100 | [diff] [blame] | 53 | |
| 54 | public: |
Anthony Barbier | 5f70773 | 2018-07-03 16:22:02 +0100 | [diff] [blame] | 55 | ScopedProfilerClass(profiler &prof, int i, unsigned long u) : _parent(prof) { |
| 56 | if (prof.currentevent==maxevents) |
Michalis Spyrou | e7e96e0 | 2018-04-13 13:44:10 +0100 | [diff] [blame] | 57 | return; |
| 58 | |
Anthony Barbier | 5f70773 | 2018-07-03 16:22:02 +0100 | [diff] [blame] | 59 | prof.events[prof.currentevent]=i; |
| 60 | prof.units[prof.currentevent]=u; |
| 61 | legal=true; |
Michalis Spyrou | e7e96e0 | 2018-04-13 13:44:10 +0100 | [diff] [blame] | 62 | start_counter(prof.countfd); |
| 63 | } |
| 64 | |
Anthony Barbier | 5f70773 | 2018-07-03 16:22:02 +0100 | [diff] [blame] | 65 | ~ScopedProfilerClass() { |
| 66 | if (!legal) return; |
Michalis Spyrou | e7e96e0 | 2018-04-13 13:44:10 +0100 | [diff] [blame] | 67 | |
Anthony Barbier | 5f70773 | 2018-07-03 16:22:02 +0100 | [diff] [blame] | 68 | long long cycs = stop_counter(_parent.countfd); |
Michalis Spyrou | e7e96e0 | 2018-04-13 13:44:10 +0100 | [diff] [blame] | 69 | _parent.times[_parent.currentevent++] = cycs; |
| 70 | } |
| 71 | }; |
| 72 | |
Pablo Tello | eb82fd2 | 2018-02-23 13:43:50 +0000 | [diff] [blame] | 73 | public: |
Anthony Barbier | 5f70773 | 2018-07-03 16:22:02 +0100 | [diff] [blame] | 74 | profiler() { |
| 75 | countfd=open_cycle_counter(); |
Pablo Tello | eb82fd2 | 2018-02-23 13:43:50 +0000 | [diff] [blame] | 76 | } |
| 77 | |
Anthony Barbier | 5f70773 | 2018-07-03 16:22:02 +0100 | [diff] [blame] | 78 | ~profiler() { |
Pablo Tello | eb82fd2 | 2018-02-23 13:43:50 +0000 | [diff] [blame] | 79 | close(countfd); |
Anthony Barbier | 5f70773 | 2018-07-03 16:22:02 +0100 | [diff] [blame] | 80 | int tots[5]; |
Pablo Tello | eb82fd2 | 2018-02-23 13:43:50 +0000 | [diff] [blame] | 81 | unsigned long counts[5]; |
| 82 | unsigned long tunits[5]; |
Anthony Barbier | 5f70773 | 2018-07-03 16:22:02 +0100 | [diff] [blame] | 83 | const char * descs[] = { "Prepare A", "Prepare B", "Kernel", "Merge" }; |
Pablo Tello | eb82fd2 | 2018-02-23 13:43:50 +0000 | [diff] [blame] | 84 | |
Anthony Barbier | 5f70773 | 2018-07-03 16:22:02 +0100 | [diff] [blame] | 85 | for (int i=1; i<5; i++) { |
| 86 | tots[i] = 0; |
Pablo Tello | eb82fd2 | 2018-02-23 13:43:50 +0000 | [diff] [blame] | 87 | counts[i] = 0; |
| 88 | tunits[i] = 0; |
| 89 | } |
| 90 | |
Anthony Barbier | 5f70773 | 2018-07-03 16:22:02 +0100 | [diff] [blame] | 91 | for (int i=0; i<currentevent; i++) { |
| 92 | // printf("%10s: %ld\n", descs[events[i]-1], times[i]); |
Pablo Tello | eb82fd2 | 2018-02-23 13:43:50 +0000 | [diff] [blame] | 93 | 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 Barbier | 5f70773 | 2018-07-03 16:22:02 +0100 | [diff] [blame] | 106 | 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 Tello | eb82fd2 | 2018-02-23 13:43:50 +0000 | [diff] [blame] | 108 | } |
| 109 | } |
| 110 | |
| 111 | template <typename T> |
Anthony Barbier | 5f70773 | 2018-07-03 16:22:02 +0100 | [diff] [blame] | 112 | void operator() (int i, unsigned long u, T func) { |
| 113 | if (currentevent==maxevents) { |
Pablo Tello | eb82fd2 | 2018-02-23 13:43:50 +0000 | [diff] [blame] | 114 | func(); |
Anthony Barbier | 5f70773 | 2018-07-03 16:22:02 +0100 | [diff] [blame] | 115 | } else { |
Pablo Tello | eb82fd2 | 2018-02-23 13:43:50 +0000 | [diff] [blame] | 116 | events[currentevent] = i; |
Anthony Barbier | 5f70773 | 2018-07-03 16:22:02 +0100 | [diff] [blame] | 117 | units[currentevent] = u; |
Pablo Tello | eb82fd2 | 2018-02-23 13:43:50 +0000 | [diff] [blame] | 118 | start_counter(countfd); |
| 119 | func(); |
Anthony Barbier | 5f70773 | 2018-07-03 16:22:02 +0100 | [diff] [blame] | 120 | long long cycs = stop_counter(countfd); |
Pablo Tello | eb82fd2 | 2018-02-23 13:43:50 +0000 | [diff] [blame] | 121 | times[currentevent++] = cycs; |
| 122 | } |
| 123 | } |
Anthony Barbier | 5f70773 | 2018-07-03 16:22:02 +0100 | [diff] [blame] | 124 | |
| 125 | ScopedProfilerClass ScopedProfiler(int i, unsigned long u) { |
Michalis Spyrou | e7e96e0 | 2018-04-13 13:44:10 +0100 | [diff] [blame] | 126 | return ScopedProfilerClass(*this, i, u); |
Pablo Tello | eb82fd2 | 2018-02-23 13:43:50 +0000 | [diff] [blame] | 127 | } |
| 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 |