| 87 | } |
| 88 | |
| 89 | void PerfettoProfiler::openclTraceEnd() |
| 90 | { |
| 91 | uint64_t cpu_sync_time = getTsNs(); |
| 92 | ARM_COMPUTE_TRACE_CUSTOM_EVENT_END(ARM_COMPUTE_PROF_CAT_SCHEDULER, 0, cpu_sync_time); |
| 93 | if (!opencl_clock || !opencl_tracing_enabled || ACL_PROFILE_LEVEL < 1) |
| 94 | { |
| 95 | return; |
| 96 | } |
| 97 | opencl_clock->stop(); |
| 98 | opencl_clock->test_stop(); |
| 99 | |
| 100 | #if (ACL_PROFILE_LEVEL > 1) |
| 101 | // Print the RAW GPU timestamps |
| 102 | std::cout << "RAW GPU timestamps:" << std::endl; |
| 103 | for (const auto &instrument : opencl_clock->measurements()) |
| 104 | { |
| 105 | std::cout << instrument.first << ": " << instrument.second << std::endl; |
| 106 | } |
| 107 | #endif |
| 108 | |
| 109 | // The difference between the instrument map and this map is that. |
| 110 | // MeasurementsMap elements does have an awareness of the timestamps in other GPU stages. |
| 111 | // Gathering all the timestamps stages in the value of the map makes drawing spans in the CPU timeline easier. |
| 112 | // |-------------------+----------------------------------------------------------------| |
| 113 | // | Map Name | Key | Value | |
| 114 | // |-------------------+--------------------------+-------------------------------------| |
| 115 | // | MeasurementsMap | [stage][kernel]#ID | GPU timestamp as string | |
| 116 | // | | (e.g., "[start]foo#1" ) | (e.g., "123456789 ns") | |
| 117 | // |-------------------+--------------------------+-------------------------------------| |
| 118 | // | gpu_spans_map | [kernel]#ID | vector of stage timestamps (CPU ns) | |
| 119 | // | | (e.g., "foo#1" ) | [queued, flushed, start, end] | |
| 120 | // |-------------------+--------------------------+-------------------------------------| |
| 121 | std::map<std::string, std::vector<uint64_t>> gpu_spans_map; |
| 122 | |
| 123 | uint64_t gpu_sync_time = 0; |
| 124 | // TODO : find a better way to sync GPU and CPU times |
| 125 | // Here we are finding the GPU timestamp that have the highest value. |
| 126 | // This is the closest to the end of ::sync() call. |
| 127 | for (const auto &instrument : opencl_clock->measurements()) |
| 128 | { |
| 129 | uint64_t gpu_ts = std::stoull(instrument.second.value().to_string()); |
| 130 | if (gpu_ts > gpu_sync_time) |
| 131 | { |
| 132 | gpu_sync_time = gpu_ts; |
| 133 | } |
| 134 | } |
| 135 | |
| 136 | for (const auto &instrument : opencl_clock->measurements()) |
| 137 | { |
| 138 | const std::string &key = instrument.first; |
| 139 | const std::string time_str = instrument.second.value().to_string(); |
| 140 | uint64_t gpu_time = std::stoull(time_str); |
| 141 | uint64_t cpu_time = gpu_time + cpu_sync_time - gpu_sync_time; |
| 142 | |
| 143 | if (key.empty() || key[0] != '[') |
| 144 | continue; |
| 145 | |
| 146 | // Find the closing bracket |