Moritz Pflanzer | beabe3b | 2017-08-31 14:56:32 +0100 | [diff] [blame] | 1 | /* |
| 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 | |
| 30 | class profiler { |
| 31 | private: |
| 32 | static const int maxevents = 10000; |
| 33 | unsigned long times[maxevents]; |
| 34 | int events[maxevents]; |
| 35 | int currentevent; |
| 36 | int countfd; |
| 37 | |
| 38 | public: |
| 39 | profiler() { |
| 40 | currentevent=0; |
| 41 | countfd=open_cycle_counter(); |
| 42 | } |
| 43 | |
| 44 | ~profiler() { |
| 45 | close(countfd); |
| 46 | int tots[5]; |
| 47 | unsigned long counts[5]; |
| 48 | const char * descs[] = { "Prepare A", "Prepare B", "Kernel", "Merge" }; |
| 49 | |
| 50 | for (int i=1; i<5; i++) { |
| 51 | tots[i] = 0; |
| 52 | counts[i] = 0; |
| 53 | } |
| 54 | |
| 55 | printf("Profiled events:\n"); |
| 56 | for (int i=0; i<currentevent; i++) { |
| 57 | printf("%10s: %ld\n", descs[events[i]-1], times[i]); |
| 58 | tots[events[i]]++; |
| 59 | counts[events[i]] += times[i]; |
| 60 | } |
| 61 | |
| 62 | printf("%20s %9s %9s %9s\n", "", "Events", "Total", "Average"); |
| 63 | for (int i=1; i<5; i++) { |
| 64 | printf("%20s: %9d %9ld %9ld\n",descs[i-1],tots[i],counts[i],counts[i]/tots[i]); |
| 65 | } |
| 66 | } |
| 67 | |
| 68 | template <typename T> |
| 69 | void operator() (int i, T func) { |
| 70 | if (currentevent==maxevents) { |
| 71 | func(); |
| 72 | } else { |
| 73 | start_counter(countfd); |
| 74 | func(); |
| 75 | long long cycs = stop_counter(countfd); |
| 76 | events[currentevent] = i; |
| 77 | times[currentevent++] = cycs; |
| 78 | } |
| 79 | } |
| 80 | }; |
| 81 | |
| 82 | #else |
| 83 | |
| 84 | class profiler { |
| 85 | public: |
| 86 | template <typename T> |
| 87 | void operator() (int i, T func) { |
| 88 | func(); |
| 89 | } |
| 90 | }; |
| 91 | |
| 92 | #endif |
| 93 | |
| 94 | #define PROFILE_PREPA 1 |
| 95 | #define PROFILE_PREPB 2 |
| 96 | #define PROFILE_KERNEL 3 |
| 97 | #define PROFILE_MERGE 4 |