From 71990c836a7b7eebe39d70d96cc0cb3ff5dd5ce4 Mon Sep 17 00:00:00 2001 From: yanghaitao1 Date: Thu, 10 Jun 2021 23:33:40 -0400 Subject: [PATCH] fix review issue --- .../gpu/data/dataset_iterator_kernel.cc | 1 + .../ccsrc/profiler/device/common/memory_profiling.cc | 1 + mindspore/ccsrc/profiler/device/data_saver.cc | 4 ++++ mindspore/ccsrc/profiler/device/gpu/gpu_data_saver.cc | 5 +++++ mindspore/ccsrc/profiler/device/gpu/gpu_profiling.cc | 10 +++++++++- mindspore/ccsrc/profiler/device/profiling.cc | 10 ++++++++-- 6 files changed, 28 insertions(+), 3 deletions(-) diff --git a/mindspore/ccsrc/backend/kernel_compiler/gpu/data/dataset_iterator_kernel.cc b/mindspore/ccsrc/backend/kernel_compiler/gpu/data/dataset_iterator_kernel.cc index 18838083d19..be44bb91aeb 100644 --- a/mindspore/ccsrc/backend/kernel_compiler/gpu/data/dataset_iterator_kernel.cc +++ b/mindspore/ccsrc/backend/kernel_compiler/gpu/data/dataset_iterator_kernel.cc @@ -72,6 +72,7 @@ bool DatasetIteratorKernel::Init(const CNodePtr &kernel_node) { if (profiling_enable_) { std::string path = profiler_inst->ProfileDataPath(); profiling_op_ = std::make_shared(path); + MS_EXCEPTION_IF_NULL(profiling_op_); profiler_inst->RegisterProfilingOp(profiling_op_); } return true; diff --git a/mindspore/ccsrc/profiler/device/common/memory_profiling.cc b/mindspore/ccsrc/profiler/device/common/memory_profiling.cc index b511fb42ebc..8892d946d33 100644 --- a/mindspore/ccsrc/profiler/device/common/memory_profiling.cc +++ b/mindspore/ccsrc/profiler/device/common/memory_profiling.cc @@ -44,6 +44,7 @@ bool MemoryProfiling::IsMemoryProfilingEnable() const { std::shared_ptr MemoryProfiling::AddGraphMemoryNode(uint32_t graph_id) { std::shared_ptr node = std::make_shared(graph_id); + MS_EXCEPTION_IF_NULL(node); graph_memory_[graph_id] = node; return node; } diff --git a/mindspore/ccsrc/profiler/device/data_saver.cc b/mindspore/ccsrc/profiler/device/data_saver.cc index 846c0a438f7..17742d0a7e7 100644 --- a/mindspore/ccsrc/profiler/device/data_saver.cc +++ b/mindspore/ccsrc/profiler/device/data_saver.cc @@ -41,6 +41,10 @@ void DataSaver::ParseOpInfo(const OpInfoMap &op_info_maps) { op_timestamps_map_[item.first] = item.second.start_duration; float proportion = item.second.op_host_cost_time / total_time_sum; auto op_info = std::make_shared(item.second); + if (op_info == nullptr) { + MS_LOG(ERROR) << "Create Operation information node failed when parse operation information."; + return; + } OpDetailInfo op_detail_info = OpDetailInfo(op_info, proportion); op_detail_infos_.emplace_back(op_detail_info); AddOpDetailInfoForType(op_detail_info); diff --git a/mindspore/ccsrc/profiler/device/gpu/gpu_data_saver.cc b/mindspore/ccsrc/profiler/device/gpu/gpu_data_saver.cc index fccdf53f3bc..5c64620a8b7 100644 --- a/mindspore/ccsrc/profiler/device/gpu/gpu_data_saver.cc +++ b/mindspore/ccsrc/profiler/device/gpu/gpu_data_saver.cc @@ -89,6 +89,10 @@ void GpuDataSaver::AddKernelEvent(const Event &event) { void GpuDataSaver::AddKernelEventToDevice(const Event &event, DeviceActivityInfos *device_activity_infos) { // Combine kernel activity with same kernel name auto event_ptr = std::make_shared(event); + if (event_ptr == nullptr) { + MS_LOG(WARNING) << "Create event failed when add event to device."; + return; + } ActivityData activity_data = ActivityData(event_ptr); std::string kernel_name = event.kernel_name; auto iter = device_activity_infos->find(kernel_name); @@ -135,6 +139,7 @@ void GpuDataSaver::WriteActivity(const std::string &saver_base_dir) { std::string timestamp_file_path = timestamp_file_path_base + std::to_string(device_info.first) + ".txt"; std::ofstream activity_timestamp_ofs(timestamp_file_path); if (!activity_timestamp_ofs.is_open()) { + ofs.close(); MS_LOG(WARNING) << "Open file '" << timestamp_file_path << "' failed!"; return; } diff --git a/mindspore/ccsrc/profiler/device/gpu/gpu_profiling.cc b/mindspore/ccsrc/profiler/device/gpu/gpu_profiling.cc index e1f5f992086..b762de0764b 100644 --- a/mindspore/ccsrc/profiler/device/gpu/gpu_profiling.cc +++ b/mindspore/ccsrc/profiler/device/gpu/gpu_profiling.cc @@ -520,7 +520,12 @@ void CUPTIAPI ActivityAllocBuffer(uint8_t **buffer, size_t *size, size_t *maxNum void CUPTIAPI ActivityProcessBuffer(CUcontext ctx, uint32_t streamId, uint8_t *buffer, size_t size, size_t validSize) { PROFILER_ERROR_IF_NULLPTR(buffer); - GPUProfiler::GetInstance()->ProcessBuffer(ctx, streamId, buffer, size, validSize); + auto gpu_profiler_inst = GPUProfiler::GetInstance(); + if (gpu_profiler_inst == nullptr) { + MS_LOG(ERROR) << "GPU profiler instance is nullptr"; + return; + } + gpu_profiler_inst->ProcessBuffer(ctx, streamId, buffer, size, validSize); } void ProcessActivityMemcpyRecord(Event *profilingData, CUpti_Activity *record, CUpti_ActivityMemcpy *memcpy) { @@ -673,6 +678,7 @@ void GPUProfiler::HandleActivityRecord(CUpti_Activity *record) { void GPUProfiler::SetStepTraceOpName(ProfilingTraceInfo trace_op_name) { step_trace_op_name = trace_op_name; } void GPUProfiler::RegisterProfilingOp(std::shared_ptr node) { + PROFILER_ERROR_IF_NULLPTR(node); if (profiling_op_.find(node->Name()) != profiling_op_.end()) { return; } @@ -681,6 +687,8 @@ void GPUProfiler::RegisterProfilingOp(std::shared_ptr node) { } void CUPTIAPI GPUProfiler::AllocBuffer(uint8_t **buffer, size_t *size, size_t *maxNumRecords) { + PROFILER_ERROR_IF_NULLPTR(size); + PROFILER_ERROR_IF_NULLPTR(maxNumRecords); int stat = posix_memalign(reinterpret_cast(buffer), ALIGN_SIZE, BUF_SIZE); if (stat) { MS_LOG(ERROR) << "Out of memory, activity buffer alloc failed."; diff --git a/mindspore/ccsrc/profiler/device/profiling.cc b/mindspore/ccsrc/profiler/device/profiling.cc index d921888aaed..e466b102df5 100644 --- a/mindspore/ccsrc/profiler/device/profiling.cc +++ b/mindspore/ccsrc/profiler/device/profiling.cc @@ -29,9 +29,15 @@ namespace profiler { uint64_t Profiler::GetHostMonoTimeStamp() const { struct timespec ts; #if defined(_WIN32) || defined(_WIN64) - clock_gettime(CLOCK_MONOTONIC, &ts); + if (clock_gettime(CLOCK_MONOTONIC, &ts) != 0) { + MS_LOG(ERROR) << "Get host timestamp failed"; + return 0; + } #else - clock_gettime(CLOCK_MONOTONIC_RAW, &ts); + if (clock_gettime(CLOCK_MONOTONIC_RAW, &ts) != 0) { + MS_LOG(ERROR) << "Get host timestamp failed"; + return 0; + } #endif constexpr uint64_t kNSecondInSecond = 1000000000; uint64_t cur_time_stamp = ts.tv_sec * kNSecondInSecond + ts.tv_nsec;