|
| 1 | +#pragma once |
| 2 | + |
| 3 | +#include "tracesmith/capture/profiler.hpp" |
| 4 | +#include <mutex> |
| 5 | +#include <unordered_map> |
| 6 | + |
| 7 | +#ifdef TRACESMITH_ENABLE_MACA |
| 8 | +#include <mcc/mcc.h> |
| 9 | +#include <mcpti/mcpti.h> |
| 10 | +#endif |
| 11 | + |
| 12 | +namespace tracesmith { |
| 13 | + |
| 14 | +/** |
| 15 | + * MCPTI-based GPU Profiler for MetaX GPUs (C500/C550) |
| 16 | + * |
| 17 | + * Uses MetaX MCPTI (MACA Profiling Tools Interface) to capture: |
| 18 | + * - Kernel launches and completions |
| 19 | + * - Memory operations (H2D, D2H, D2D, memset) |
| 20 | + * - Synchronization events |
| 21 | + * - Stream operations |
| 22 | + * |
| 23 | + * Requirements: |
| 24 | + * - MetaX GPU (C500, C550, etc.) |
| 25 | + * - MACA SDK with MCPTI headers and library |
| 26 | + * - Driver with profiling permissions |
| 27 | + * |
| 28 | + * Note: MCPTI API is highly compatible with NVIDIA CUPTI |
| 29 | + */ |
| 30 | +class MCPTIProfiler : public IPlatformProfiler { |
| 31 | +public: |
| 32 | + MCPTIProfiler(); |
| 33 | + ~MCPTIProfiler() override; |
| 34 | + |
| 35 | + // IPlatformProfiler interface |
| 36 | + PlatformType platformType() const override { return PlatformType::MACA; } |
| 37 | + bool isAvailable() const override; |
| 38 | + |
| 39 | + bool initialize(const ProfilerConfig& config) override; |
| 40 | + void finalize() override; |
| 41 | + |
| 42 | + bool startCapture() override; |
| 43 | + bool stopCapture() override; |
| 44 | + bool isCapturing() const override { return capturing_; } |
| 45 | + |
| 46 | + size_t getEvents(std::vector<TraceEvent>& events, size_t max_count = 0) override; |
| 47 | + std::vector<DeviceInfo> getDeviceInfo() const override; |
| 48 | + |
| 49 | + void setEventCallback(EventCallback callback) override; |
| 50 | + |
| 51 | + uint64_t eventsCaptured() const override { return events_captured_; } |
| 52 | + uint64_t eventsDropped() const override { return events_dropped_; } |
| 53 | + |
| 54 | +#ifdef TRACESMITH_ENABLE_MACA |
| 55 | + // MCPTI-specific methods |
| 56 | + |
| 57 | + /** |
| 58 | + * Set activity buffer size (default: 32MB) |
| 59 | + */ |
| 60 | + void setBufferSize(size_t size_bytes); |
| 61 | + |
| 62 | + /** |
| 63 | + * Enable/disable specific activity types |
| 64 | + */ |
| 65 | + void enableActivityKind(MCpti_ActivityKind kind, bool enable); |
| 66 | + |
| 67 | + /** |
| 68 | + * Get MCPTI version |
| 69 | + */ |
| 70 | + uint32_t getMcptiVersion() const; |
| 71 | + |
| 72 | +private: |
| 73 | + // MCPTI callback handlers (static for C API) |
| 74 | + static void MCPTIAPI bufferRequested(uint8_t** buffer, size_t* size, size_t* maxNumRecords); |
| 75 | + static void MCPTIAPI bufferCompleted(MCcontext ctx, uint32_t streamId, |
| 76 | + uint8_t* buffer, size_t size, size_t validSize); |
| 77 | + static void MCPTIAPI callbackHandler(void* userdata, MCpti_CallbackDomain domain, |
| 78 | + MCpti_CallbackId cbid, const void* cbdata); |
| 79 | + |
| 80 | + // Activity processing |
| 81 | + void processActivity(MCpti_Activity* record); |
| 82 | + void processKernelActivity(const MCpti_ActivityKernel4* kernel); |
| 83 | + void processMemcpyActivity(const MCpti_ActivityMemcpy* memcpy); |
| 84 | + void processMemsetActivity(const MCpti_ActivityMemset* memset); |
| 85 | + void processSyncActivity(const MCpti_ActivitySynchronization* sync); |
| 86 | + |
| 87 | + // Event creation helpers |
| 88 | + TraceEvent createKernelEvent(const MCpti_ActivityKernel4* kernel); |
| 89 | + TraceEvent createMemcpyEvent(const MCpti_ActivityMemcpy* memcpy); |
| 90 | + TraceEvent createMemsetEvent(const MCpti_ActivityMemset* memset); |
| 91 | + TraceEvent createSyncEvent(const MCpti_ActivitySynchronization* sync); |
| 92 | + |
| 93 | + // Thread-safe event storage |
| 94 | + void addEvent(TraceEvent&& event); |
| 95 | + |
| 96 | + // MCPTI handles |
| 97 | + MCpti_SubscriberHandle subscriber_; |
| 98 | + |
| 99 | + // Activity buffer management |
| 100 | + size_t buffer_size_; |
| 101 | + static constexpr size_t DEFAULT_BUFFER_SIZE = 32 * 1024 * 1024; // 32MB |
| 102 | + static constexpr size_t ALIGN_SIZE = 8; |
| 103 | + |
| 104 | + // Enabled activity kinds |
| 105 | + std::vector<MCpti_ActivityKind> enabled_activities_; |
| 106 | + |
| 107 | + // Correlation ID tracking (to match kernel launch with completion) |
| 108 | + std::unordered_map<uint64_t, Timestamp> kernel_start_times_; |
| 109 | + std::mutex correlation_mutex_; |
| 110 | + |
| 111 | +#endif // TRACESMITH_ENABLE_MACA |
| 112 | + |
| 113 | + // Configuration |
| 114 | + ProfilerConfig config_; |
| 115 | + |
| 116 | + // State |
| 117 | + bool initialized_; |
| 118 | + bool capturing_; |
| 119 | + |
| 120 | + // Event storage |
| 121 | + std::vector<TraceEvent> events_; |
| 122 | + std::mutex events_mutex_; |
| 123 | + EventCallback callback_; |
| 124 | + |
| 125 | + // Statistics |
| 126 | + uint64_t events_captured_; |
| 127 | + uint64_t events_dropped_; |
| 128 | + |
| 129 | + // Correlation ID counter |
| 130 | + std::atomic<uint64_t> correlation_counter_; |
| 131 | + |
| 132 | + // Singleton instance for static callbacks |
| 133 | + static MCPTIProfiler* instance_; |
| 134 | +}; |
| 135 | + |
| 136 | +/** |
| 137 | + * Check if MACA and MCPTI are available on this system |
| 138 | + */ |
| 139 | +bool isMACAAvailable(); |
| 140 | + |
| 141 | +/** |
| 142 | + * Get MACA driver version |
| 143 | + */ |
| 144 | +int getMACADriverVersion(); |
| 145 | + |
| 146 | +/** |
| 147 | + * Get number of MetaX GPU devices |
| 148 | + */ |
| 149 | +int getMACADeviceCount(); |
| 150 | + |
| 151 | +} // namespace tracesmith |
0 commit comments