|
| 1 | +// Copyright (c) 2022 PaddlePaddle Authors. All Rights Reserved. |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +#include "paddle/fluid/platform/profiler/cuda_tracer.h" |
| 16 | +#include <string> |
| 17 | +#include <unordered_map> |
| 18 | +#include "glog/logging.h" |
| 19 | +#include "paddle/fluid/framework/new_executor/workqueue/workqueue_utils.h" |
| 20 | +#include "paddle/fluid/platform/os_info.h" |
| 21 | +#include "paddle/fluid/platform/profiler/cupti_data_process.h" |
| 22 | + |
| 23 | +#define CUPTI_CALL(call) \ |
| 24 | + do { \ |
| 25 | + CUptiResult _status = call; \ |
| 26 | + if (_status != CUPTI_SUCCESS) { \ |
| 27 | + const char* errstr; \ |
| 28 | + dynload::cuptiGetResultString(_status, &errstr); \ |
| 29 | + LOG(ERROR) << "Function " << #call << " failed with error " << errstr; \ |
| 30 | + exit(-1); \ |
| 31 | + } \ |
| 32 | + } while (0) |
| 33 | + |
| 34 | +namespace paddle { |
| 35 | +namespace platform { |
| 36 | + |
| 37 | +namespace details { |
| 38 | +std::unordered_map<uint32_t, uint64_t> CreateThreadIdMapping() { |
| 39 | + std::unordered_map<uint32_t, uint64_t> mapping; |
| 40 | + std::unordered_map<uint64_t, ThreadId> ids = GetAllThreadIds(); |
| 41 | + for (const auto& id : ids) { |
| 42 | + mapping[id.second.cupti_tid] = id.second.sys_tid; |
| 43 | + } |
| 44 | + return mapping; |
| 45 | +} |
| 46 | +} // namespace details |
| 47 | + |
| 48 | +CudaTracer::CudaTracer() {} |
| 49 | + |
| 50 | +void CudaTracer::PrepareTracing() { |
| 51 | + PADDLE_ENFORCE_EQ( |
| 52 | + state_ == TracerState::UNINITED || state_ == TracerState::STOPED, true, |
| 53 | + platform::errors::PreconditionNotMet("Tracer must be UNINITED")); |
| 54 | + EnableCuptiActivity(); |
| 55 | + state_ = TracerState::READY; |
| 56 | +} |
| 57 | + |
| 58 | +void CudaTracer::StartTracing() { |
| 59 | + PADDLE_ENFORCE_EQ( |
| 60 | + state_ == TracerState::READY, true, |
| 61 | + platform::errors::PreconditionNotMet("Tracer must be READY or STOPPED")); |
| 62 | + ConsumeBuffers(); |
| 63 | + tracing_start_ns_ = PosixInNsec(); |
| 64 | + state_ = TracerState::STARTED; |
| 65 | +} |
| 66 | + |
| 67 | +void CudaTracer::StopTracing() { |
| 68 | + PADDLE_ENFORCE_EQ( |
| 69 | + state_, TracerState::STARTED, |
| 70 | + platform::errors::PreconditionNotMet("Tracer must be STARTED")); |
| 71 | + DisableCuptiActivity(); |
| 72 | + state_ = TracerState::STOPED; |
| 73 | +} |
| 74 | + |
| 75 | +void CudaTracer::CollectTraceData(TraceEventCollector* collector) { |
| 76 | + PADDLE_ENFORCE_EQ( |
| 77 | + state_, TracerState::STOPED, |
| 78 | + platform::errors::PreconditionNotMet("Tracer must be STOPED")); |
| 79 | + ProcessCuptiActivity(collector); |
| 80 | +} |
| 81 | + |
| 82 | +int CudaTracer::ProcessCuptiActivity(TraceEventCollector* collector) { |
| 83 | + int record_cnt = 0; |
| 84 | +#ifdef PADDLE_WITH_CUPTI |
| 85 | + CUPTI_CALL(dynload::cuptiActivityFlushAll(CUPTI_ACTIVITY_FLAG_FLUSH_FORCED)); |
| 86 | + auto mapping = details::CreateThreadIdMapping(); |
| 87 | + std::vector<ActivityBuffer> buffers = ConsumeBuffers(); |
| 88 | + for (auto& buffer : buffers) { |
| 89 | + if (buffer.addr == nullptr || buffer.valid_size == 0) { |
| 90 | + continue; |
| 91 | + } |
| 92 | + |
| 93 | + CUpti_Activity* record = nullptr; |
| 94 | + while (true) { |
| 95 | + CUptiResult status = dynload::cuptiActivityGetNextRecord( |
| 96 | + buffer.addr, buffer.valid_size, &record); |
| 97 | + if (status == CUPTI_SUCCESS) { |
| 98 | + details::ProcessCuptiActivityRecord(record, tracing_start_ns_, mapping, |
| 99 | + collector); |
| 100 | + ++record_cnt; |
| 101 | + } else if (status == CUPTI_ERROR_MAX_LIMIT_REACHED) { |
| 102 | + break; |
| 103 | + } else { |
| 104 | + CUPTI_CALL(status); |
| 105 | + } |
| 106 | + } |
| 107 | + |
| 108 | + ReleaseBuffer(buffer.addr); |
| 109 | + } |
| 110 | +#endif |
| 111 | + return record_cnt; |
| 112 | +} |
| 113 | + |
| 114 | +void CudaTracer::EnableCuptiActivity() { |
| 115 | +#ifdef PADDLE_WITH_CUPTI |
| 116 | + CUPTI_CALL(dynload::cuptiActivityRegisterCallbacks(BufferRequestedCallback, |
| 117 | + BufferCompletedCallback)); |
| 118 | + |
| 119 | + CUPTI_CALL(dynload::cuptiActivityEnable(CUPTI_ACTIVITY_KIND_MEMCPY)); |
| 120 | + CUPTI_CALL( |
| 121 | + dynload::cuptiActivityEnable(CUPTI_ACTIVITY_KIND_CONCURRENT_KERNEL)); |
| 122 | + CUPTI_CALL(dynload::cuptiActivityEnable(CUPTI_ACTIVITY_KIND_DRIVER)); |
| 123 | + CUPTI_CALL(dynload::cuptiActivityEnable(CUPTI_ACTIVITY_KIND_RUNTIME)); |
| 124 | + CUPTI_CALL(dynload::cuptiActivityEnable(CUPTI_ACTIVITY_KIND_MEMSET)); |
| 125 | + VLOG(3) << "enable cupti activity"; |
| 126 | +#endif |
| 127 | +} |
| 128 | + |
| 129 | +void CudaTracer::DisableCuptiActivity() { |
| 130 | +#ifdef PADDLE_WITH_CUPTI |
| 131 | + CUPTI_CALL(dynload::cuptiActivityDisable(CUPTI_ACTIVITY_KIND_MEMCPY)); |
| 132 | + CUPTI_CALL( |
| 133 | + dynload::cuptiActivityDisable(CUPTI_ACTIVITY_KIND_CONCURRENT_KERNEL)); |
| 134 | + CUPTI_CALL(dynload::cuptiActivityDisable(CUPTI_ACTIVITY_KIND_DRIVER)); |
| 135 | + CUPTI_CALL(dynload::cuptiActivityDisable(CUPTI_ACTIVITY_KIND_RUNTIME)); |
| 136 | + CUPTI_CALL(dynload::cuptiActivityDisable(CUPTI_ACTIVITY_KIND_MEMSET)); |
| 137 | + VLOG(3) << "disable cupti activity"; |
| 138 | +#endif |
| 139 | +} |
| 140 | + |
| 141 | +#ifdef PADDLE_WITH_CUPTI |
| 142 | +void CUPTIAPI CudaTracer::BufferRequestedCallback(uint8_t** buffer, |
| 143 | + size_t* size, |
| 144 | + size_t* max_num_records) { |
| 145 | + GetInstance().AllocateBuffer(buffer, size); |
| 146 | + *max_num_records = 0; |
| 147 | +} |
| 148 | + |
| 149 | +void CUPTIAPI CudaTracer::BufferCompletedCallback(CUcontext ctx, |
| 150 | + uint32_t stream_id, |
| 151 | + uint8_t* buffer, size_t size, |
| 152 | + size_t valid_size) { |
| 153 | + GetInstance().ProduceBuffer(buffer, valid_size); |
| 154 | + size_t dropped = 0; |
| 155 | + CUPTI_CALL( |
| 156 | + dynload::cuptiActivityGetNumDroppedRecords(ctx, stream_id, &dropped)); |
| 157 | + if (dropped != 0) { |
| 158 | + LOG(WARNING) << "Stream " << stream_id << " Dropped " << dropped |
| 159 | + << " activity records"; |
| 160 | + } |
| 161 | +} |
| 162 | +#endif |
| 163 | + |
| 164 | +void CudaTracer::AllocateBuffer(uint8_t** buffer, size_t* size) { |
| 165 | + constexpr size_t kBufSize = 1 << 23; // 8 MB |
| 166 | + constexpr size_t kBufAlign = 8; // 8 B |
| 167 | + *buffer = reinterpret_cast<uint8_t*>( |
| 168 | + paddle::framework::AlignedMalloc(kBufSize, kBufAlign)); |
| 169 | + *size = kBufSize; |
| 170 | +} |
| 171 | + |
| 172 | +void CudaTracer::ProduceBuffer(uint8_t* buffer, size_t valid_size) { |
| 173 | + std::lock_guard<std::mutex> guard(activity_buffer_lock_); |
| 174 | + activity_buffers_.emplace_back(buffer, valid_size); |
| 175 | +} |
| 176 | + |
| 177 | +std::vector<CudaTracer::ActivityBuffer> CudaTracer::ConsumeBuffers() { |
| 178 | + std::vector<ActivityBuffer> buffers; |
| 179 | + { |
| 180 | + std::lock_guard<std::mutex> guard(activity_buffer_lock_); |
| 181 | + buffers.swap(activity_buffers_); |
| 182 | + } |
| 183 | + return buffers; |
| 184 | +} |
| 185 | + |
| 186 | +void CudaTracer::ReleaseBuffer(uint8_t* buffer) { |
| 187 | + paddle::framework::AlignedFree(buffer); |
| 188 | +} |
| 189 | + |
| 190 | +} // namespace platform |
| 191 | +} // namespace paddle |
0 commit comments