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]; |
Pablo Tello | 6ff12a0 | 2017-11-02 16:09:35 +0000 | [diff] [blame^] | 34 | unsigned long units[maxevents]; |
Moritz Pflanzer | beabe3b | 2017-08-31 14:56:32 +0100 | [diff] [blame] | 35 | int events[maxevents]; |
| 36 | int currentevent; |
| 37 | int countfd; |
| 38 | |
| 39 | public: |
| 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 Tello | 6ff12a0 | 2017-11-02 16:09:35 +0000 | [diff] [blame^] | 49 | unsigned long tunits[5]; |
Moritz Pflanzer | beabe3b | 2017-08-31 14:56:32 +0100 | [diff] [blame] | 50 | 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 Tello | 6ff12a0 | 2017-11-02 16:09:35 +0000 | [diff] [blame^] | 55 | tunits[i] = 0; |
Moritz Pflanzer | beabe3b | 2017-08-31 14:56:32 +0100 | [diff] [blame] | 56 | } |
| 57 | |
| 58 | printf("Profiled events:\n"); |
| 59 | for (int i=0; i<currentevent; i++) { |
Moritz Pflanzer | beabe3b | 2017-08-31 14:56:32 +0100 | [diff] [blame] | 60 | tots[events[i]]++; |
| 61 | counts[events[i]] += times[i]; |
Pablo Tello | 6ff12a0 | 2017-11-02 16:09:35 +0000 | [diff] [blame^] | 62 | tunits[events[i]] += units[i]; |
Moritz Pflanzer | beabe3b | 2017-08-31 14:56:32 +0100 | [diff] [blame] | 63 | } |
| 64 | |
Pablo Tello | 6ff12a0 | 2017-11-02 16:09:35 +0000 | [diff] [blame^] | 65 | printf("%20s %9s %9s %9s %12s %9s\n", "", "Events", "Total", "Average", "Bytes/MACs", "Per cycle"); |
Moritz Pflanzer | beabe3b | 2017-08-31 14:56:32 +0100 | [diff] [blame] | 66 | for (int i=1; i<5; i++) { |
Pablo Tello | 6ff12a0 | 2017-11-02 16:09:35 +0000 | [diff] [blame^] | 67 | 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 Pflanzer | beabe3b | 2017-08-31 14:56:32 +0100 | [diff] [blame] | 68 | } |
| 69 | } |
| 70 | |
| 71 | template <typename T> |
Pablo Tello | 6ff12a0 | 2017-11-02 16:09:35 +0000 | [diff] [blame^] | 72 | void operator() (int i, unsigned long u, T func) { |
Moritz Pflanzer | beabe3b | 2017-08-31 14:56:32 +0100 | [diff] [blame] | 73 | if (currentevent==maxevents) { |
| 74 | func(); |
| 75 | } else { |
Pablo Tello | 6ff12a0 | 2017-11-02 16:09:35 +0000 | [diff] [blame^] | 76 | events[currentevent] = i; |
| 77 | units[currentevent] = u; |
Moritz Pflanzer | beabe3b | 2017-08-31 14:56:32 +0100 | [diff] [blame] | 78 | start_counter(countfd); |
| 79 | func(); |
| 80 | long long cycs = stop_counter(countfd); |
Moritz Pflanzer | beabe3b | 2017-08-31 14:56:32 +0100 | [diff] [blame] | 81 | times[currentevent++] = cycs; |
| 82 | } |
| 83 | } |
| 84 | }; |
| 85 | |
| 86 | #else |
| 87 | |
| 88 | class profiler { |
| 89 | public: |
| 90 | template <typename T> |
Pablo Tello | 6ff12a0 | 2017-11-02 16:09:35 +0000 | [diff] [blame^] | 91 | void operator() (int i, unsigned long u, T func) { |
Moritz Pflanzer | beabe3b | 2017-08-31 14:56:32 +0100 | [diff] [blame] | 92 | 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 Tello | 6ff12a0 | 2017-11-02 16:09:35 +0000 | [diff] [blame^] | 102 | |
| 103 | |