|
| 1 | +//==-------------- collector.cpp -------------------------------------------==// |
| 2 | +// |
| 3 | +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| 4 | +// See https://llvm.org/LICENSE.txt for license information. |
| 5 | +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
| 6 | +// |
| 7 | +//===----------------------------------------------------------------------===// |
| 8 | + |
| 9 | +#include "writer.hpp" |
| 10 | +#include "xpti/xpti_data_types.h" |
| 11 | + |
| 12 | +#include <cstdint> |
| 13 | +#include <xpti/xpti_trace_framework.h> |
| 14 | + |
| 15 | +#include <chrono> |
| 16 | +#include <memory> |
| 17 | +#include <stdio.h> |
| 18 | +#include <thread> |
| 19 | +#include <unistd.h> |
| 20 | + |
| 21 | +namespace chrono = std::chrono; |
| 22 | + |
| 23 | +Writer *GWriter = nullptr; |
| 24 | + |
| 25 | +struct Measurements { |
| 26 | + size_t TID; |
| 27 | + size_t PID; |
| 28 | + size_t TimeStamp; |
| 29 | +}; |
| 30 | + |
| 31 | +unsigned long process_id() { return static_cast<unsigned long>(getpid()); } |
| 32 | + |
| 33 | +static Measurements measure() { |
| 34 | + size_t TID = std::hash<std::thread::id>{}(std::this_thread::get_id()); |
| 35 | + size_t PID = process_id(); |
| 36 | + auto Now = chrono::high_resolution_clock::now(); |
| 37 | + size_t TS = chrono::time_point_cast<chrono::nanoseconds>(Now) |
| 38 | + .time_since_epoch() |
| 39 | + .count(); |
| 40 | + |
| 41 | + return Measurements{TID, PID, TS}; |
| 42 | +} |
| 43 | + |
| 44 | +XPTI_CALLBACK_API void piBeginEndCallback(uint16_t TraceType, |
| 45 | + xpti::trace_event_data_t *, |
| 46 | + xpti::trace_event_data_t *, |
| 47 | + uint64_t /*Instance*/, |
| 48 | + const void *UserData); |
| 49 | +XPTI_CALLBACK_API void taskBeginEndCallback(uint16_t TraceType, |
| 50 | + xpti::trace_event_data_t *, |
| 51 | + xpti::trace_event_data_t *, |
| 52 | + uint64_t /*Instance*/, |
| 53 | + const void *UserData); |
| 54 | +XPTI_CALLBACK_API void waitBeginEndCallback(uint16_t TraceType, |
| 55 | + xpti::trace_event_data_t *, |
| 56 | + xpti::trace_event_data_t *, |
| 57 | + uint64_t /*Instance*/, |
| 58 | + const void *UserData); |
| 59 | + |
| 60 | +XPTI_CALLBACK_API void xptiTraceInit(unsigned int /*major_version*/, |
| 61 | + unsigned int /*minor_version*/, |
| 62 | + const char * /*version_str*/, |
| 63 | + const char *StreamName) { |
| 64 | + if (GWriter == nullptr) { |
| 65 | + GWriter = new JSONWriter(std::getenv("SYCL_PROF_OUT_FILE")); |
| 66 | + GWriter->init(); |
| 67 | + } |
| 68 | + |
| 69 | + if (std::string_view(StreamName) == "sycl.pi") { |
| 70 | + uint8_t StreamID = xptiRegisterStream(StreamName); |
| 71 | + xptiRegisterCallback(StreamID, xpti::trace_function_begin, |
| 72 | + piBeginEndCallback); |
| 73 | + xptiRegisterCallback(StreamID, xpti::trace_function_end, |
| 74 | + piBeginEndCallback); |
| 75 | + } else if (std::string_view(StreamName) == "sycl") { |
| 76 | + uint8_t StreamID = xptiRegisterStream(StreamName); |
| 77 | + xptiRegisterCallback(StreamID, xpti::trace_task_begin, |
| 78 | + taskBeginEndCallback); |
| 79 | + xptiRegisterCallback(StreamID, xpti::trace_task_end, taskBeginEndCallback); |
| 80 | + xptiRegisterCallback(StreamID, xpti::trace_wait_begin, |
| 81 | + waitBeginEndCallback); |
| 82 | + xptiRegisterCallback(StreamID, xpti::trace_wait_end, waitBeginEndCallback); |
| 83 | + xptiRegisterCallback(StreamID, xpti::trace_barrier_begin, |
| 84 | + waitBeginEndCallback); |
| 85 | + xptiRegisterCallback(StreamID, xpti::trace_barrier_end, |
| 86 | + waitBeginEndCallback); |
| 87 | + } |
| 88 | +} |
| 89 | + |
| 90 | +XPTI_CALLBACK_API void xptiTraceFinish(const char *) { GWriter->finalize(); } |
| 91 | + |
| 92 | +XPTI_CALLBACK_API void piBeginEndCallback(uint16_t TraceType, |
| 93 | + xpti::trace_event_data_t *, |
| 94 | + xpti::trace_event_data_t *, |
| 95 | + uint64_t /*Instance*/, |
| 96 | + const void *UserData) { |
| 97 | + auto [TID, PID, TS] = measure(); |
| 98 | + if (TraceType == xpti::trace_function_begin) { |
| 99 | + GWriter->writeBegin(static_cast<const char *>(UserData), "Plugin", PID, TID, |
| 100 | + TS); |
| 101 | + } else { |
| 102 | + GWriter->writeEnd(static_cast<const char *>(UserData), "Plugin", PID, TID, |
| 103 | + TS); |
| 104 | + } |
| 105 | +} |
| 106 | + |
| 107 | +XPTI_CALLBACK_API void taskBeginEndCallback(uint16_t TraceType, |
| 108 | + xpti::trace_event_data_t *, |
| 109 | + xpti::trace_event_data_t *Event, |
| 110 | + uint64_t /*Instance*/, |
| 111 | + const void *) { |
| 112 | + std::string_view Name = "unknown"; |
| 113 | + |
| 114 | + xpti::metadata_t *Metadata = xptiQueryMetadata(Event); |
| 115 | + for (auto &Item : *Metadata) { |
| 116 | + std::string_view Key{xptiLookupString(Item.first)}; |
| 117 | + if (Key == "kernel_name" || Key == "memory_object") { |
| 118 | + Name = xptiLookupString(Item.second); |
| 119 | + } |
| 120 | + } |
| 121 | + |
| 122 | + auto [TID, PID, TS] = measure(); |
| 123 | + if (TraceType == xpti::trace_task_begin) { |
| 124 | + GWriter->writeBegin(Name, "SYCL", PID, TID, TS); |
| 125 | + } else { |
| 126 | + GWriter->writeEnd(Name, "SYCL", PID, TID, TS); |
| 127 | + } |
| 128 | +} |
| 129 | + |
| 130 | +XPTI_CALLBACK_API void waitBeginEndCallback(uint16_t TraceType, |
| 131 | + xpti::trace_event_data_t *, |
| 132 | + xpti::trace_event_data_t *, |
| 133 | + uint64_t /*Instance*/, |
| 134 | + const void *UserData) { |
| 135 | + auto [TID, PID, TS] = measure(); |
| 136 | + if (TraceType == xpti::trace_wait_begin || |
| 137 | + TraceType == xpti::trace_barrier_begin) { |
| 138 | + GWriter->writeBegin(static_cast<const char *>(UserData), "SYCL", PID, TID, |
| 139 | + TS); |
| 140 | + } else { |
| 141 | + GWriter->writeEnd(static_cast<const char *>(UserData), "SYCL", PID, TID, |
| 142 | + TS); |
| 143 | + } |
| 144 | +} |
0 commit comments