• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /**
2  * Copyright 2021 Huawei Technologies Co., Ltd
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #include "profiler/device/profiling.h"
18 
19 #include <cxxabi.h>
20 #include <cmath>
21 #include <ctime>
22 #include "pybind_api/api_register.h"
23 #include "utils/log_adapter.h"
24 #include "utils/utils.h"
25 #if ENABLE_GPU
26 #include "profiler/device/gpu/gpu_profiling.h"
27 #endif
28 #if ENABLE_D
29 #include "profiler/device/ascend/ascend_profiling.h"
30 #endif
31 
32 namespace mindspore {
33 namespace profiler {
34 std::shared_ptr<ProfilerManager> ProfilerManager::profiler_manager_inst_ = std::make_shared<ProfilerManager>();
35 
GetHostMonoTimeStamp() const36 uint64_t Profiler::GetHostMonoTimeStamp() const {
37   struct timespec ts;
38 #if defined(_WIN32) || defined(_WIN64)
39   if (clock_gettime(CLOCK_MONOTONIC, &ts) != 0) {
40     MS_LOG(ERROR) << "Get host timestamp failed";
41     return 0;
42   }
43 #else
44   if (clock_gettime(CLOCK_MONOTONIC_RAW, &ts) != 0) {
45     MS_LOG(ERROR) << "Get host timestamp failed";
46     return 0;
47   }
48 #endif
49   constexpr uint64_t kNSecondInSecond = 1000000000;
50   uint64_t cur_time_stamp = ts.tv_sec * kNSecondInSecond + ts.tv_nsec;
51   return cur_time_stamp;
52 }
53 
SetRunTimeData(const std::string & op_name,const float time_elapsed)54 void Profiler::SetRunTimeData(const std::string &op_name, const float time_elapsed) {
55   auto iter = op_info_map_.find(op_name);
56   if (iter != op_info_map_.end()) {
57     iter->second.op_host_cost_time += time_elapsed;
58   }
59 }
60 
SetRunTimeData(const std::string & op_name,const uint64_t start,const float duration)61 void Profiler::SetRunTimeData(const std::string &op_name, const uint64_t start, const float duration) {
62   auto iter = op_info_map_.find(op_name);
63   if (iter != op_info_map_.end()) {
64     iter->second.start_duration.emplace_back(StartDuration({start, duration}));
65   }
66 }
67 
RecordOneStepStartEndInfo()68 void Profiler::RecordOneStepStartEndInfo() {
69   std::lock_guard<std::mutex> locker(record_mutex_);
70   all_step_start_end_info_.push_back(step_start_end_info_);
71   step_start_end_info_.iter_start_op_name = "";
72   step_start_end_info_.fp_start_op_name = "";
73 }
74 
RecordOneStepStartEndInfo(const std::string op_name)75 void Profiler::RecordOneStepStartEndInfo(const std::string op_name) {
76   std::lock_guard<std::mutex> locker(record_mutex_);
77   if (step_start_end_info_.iter_start_op_name.empty()) {
78     step_start_end_info_.iter_start_op_name = op_name;
79     step_start_end_info_.fp_start_op_name = op_name;
80   }
81 
82   std::string fp_start_op_name = step_start_end_info_.fp_start_op_name;
83 
84   auto op_type_begin_iter = fp_start_op_name.rfind('/') + 1;
85   auto op_type_end_iter = fp_start_op_name.rfind('-');
86   auto op_type = fp_start_op_name.substr(op_type_begin_iter, op_type_end_iter - op_type_begin_iter);
87   if (op_type == "InitDataSetQueue" || op_type == "GetNext") {
88     step_start_end_info_.fp_start_op_name = op_name;
89   }
90   step_start_end_info_.iter_end_op_name = op_name;
91 }
92 
GetInstance()93 std::shared_ptr<ProfilerManager> &ProfilerManager::GetInstance() {
94   MS_EXCEPTION_IF_NULL(profiler_manager_inst_);
95   return profiler_manager_inst_;
96 }
97 
GetProfilingEnableFlag() const98 bool ProfilerManager::GetProfilingEnableFlag() const {
99 #if ENABLE_GPU
100   return profiler::gpu::GPUProfiler::GetInstance()->GetEnableFlag();
101 #endif
102 #if ENABLE_D
103   auto ascend_instance = profiler::ascend::AscendProfiler::GetInstance();
104   MS_EXCEPTION_IF_NULL(ascend_instance);
105   return ascend_instance->GetProfilingEnableFlag();
106 #endif
107   return false;
108 }
109 
RecordOneStepStartEndInfo() const110 void ProfilerManager::RecordOneStepStartEndInfo() const {
111 #if ENABLE_GPU
112   auto gpu_profiler_inst = profiler::gpu::GPUProfiler::GetInstance();
113   if (gpu_profiler_inst->GetEnableFlag()) {
114     gpu_profiler_inst->RecordOneStepStartEndInfo();
115   }
116 #endif
117 }
118 
GetProfilingOptions() const119 std::string ProfilerManager::GetProfilingOptions() const {
120 #if ENABLE_D
121   auto ascend_instance = profiler::ascend::AscendProfiler::GetInstance();
122   MS_EXCEPTION_IF_NULL(ascend_instance);
123   return ascend_instance->GetProfilingOptions();
124 #endif
125   return "";
126 }
127 }  // namespace profiler
128 }  // namespace mindspore
129