You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

cpu_profiling.cc 4.3 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  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. #include "profiler/device/cpu/cpu_profiling.h"
  17. #include <cxxabi.h>
  18. #include <cmath>
  19. #include <ctime>
  20. #include "profiler/device/cpu/cpu_data_saver.h"
  21. #include "pybind_api/api_register.h"
  22. #include "utils/log_adapter.h"
  23. #include "utils/utils.h"
  24. #include "utils/ms_context.h"
  25. namespace mindspore {
  26. namespace profiler {
  27. namespace cpu {
  28. std::shared_ptr<CPUProfiler> CPUProfiler::profiler_inst_ = std::make_shared<CPUProfiler>();
  29. std::shared_ptr<CPUProfiler> &CPUProfiler::GetInstance() { return profiler_inst_; }
  30. void CPUProfiler::Init(const std::string &profileDataPath = "") {
  31. MS_LOG(INFO) << "Initialize CPU Profiling";
  32. base_time_ = GetHostMonoTimeStamp();
  33. profile_data_path_ = profileDataPath;
  34. MS_LOG(INFO) << " Host start time(ns): " << base_time_ << " profile data path: " << profile_data_path_;
  35. }
  36. void CPUProfiler::StepProfilingEnable(const bool enable_flag) {
  37. MS_LOG(INFO) << "CPU Profiler enable flag: " << enable_flag;
  38. enable_flag_ = enable_flag;
  39. }
  40. void CPUProfiler::SetRunTimeData(const std::string &op_name, const uint32_t pid) {
  41. auto iter = op_info_map_.find(op_name);
  42. if (iter != op_info_map_.end()) {
  43. iter->second.op_count += 1;
  44. } else {
  45. OpInfo op_info;
  46. op_info.op_name = op_name;
  47. op_info.pid = pid;
  48. op_info.op_count = 1;
  49. op_info_map_[op_name] = op_info;
  50. }
  51. op_name_ = op_name;
  52. pid_ = pid;
  53. }
  54. void CPUProfiler::OpDataProducerBegin(const std::string op_name, const uint32_t pid) {
  55. op_time_start_ = GetHostMonoTimeStamp();
  56. op_time_mono_start_ = GetHostMonoTimeStamp();
  57. SetRunTimeData(op_name, pid);
  58. #if ENABLE_GPU
  59. if (MsContext::GetInstance()->get_param<bool>(MS_CTX_ENABLE_MINDRT)) {
  60. // For heterogeneous scene, record op name to gpu_profiler_inst.
  61. auto gpu_profiler_inst = profiler::gpu::GPUProfiler::GetInstance();
  62. // For cpu network, no gpu profiler, do not to raise exception.
  63. if (gpu_profiler_inst && gpu_profiler_inst->GetEnableFlag()) {
  64. gpu_profiler_inst->RecordOneStepStartEndInfo(op_name);
  65. }
  66. }
  67. #endif
  68. }
  69. void CPUProfiler::OpDataProducerEnd() {
  70. float op_time_elapsed = 0;
  71. op_time_stop_ = GetHostMonoTimeStamp();
  72. op_time_elapsed = (op_time_stop_ - op_time_start_) / kNanosecondToMillisecond;
  73. MS_LOG(DEBUG) << "Host Time Elapsed(ms)," << op_name_ << "," << op_time_elapsed;
  74. Profiler::SetRunTimeData(op_name_, op_time_elapsed);
  75. Profiler::SetRunTimeData(op_name_, op_time_mono_start_, op_time_elapsed);
  76. }
  77. void CPUProfiler::Stop() {
  78. MS_LOG(INFO) << "Stop CPU Profiling";
  79. SaveProfileData();
  80. ClearInst();
  81. }
  82. void CPUProfiler::SaveProfileData() {
  83. if (profile_data_path_.empty()) {
  84. MS_LOG(WARNING) << "Profile data path is empty, skip save profile data.";
  85. } else {
  86. auto cpu_data_saver_inst = profiler::cpu::CpuDataSaver::GetInstance();
  87. MS_EXCEPTION_IF_NULL(cpu_data_saver_inst);
  88. cpu_data_saver_inst->ParseOpInfo(op_info_map_);
  89. cpu_data_saver_inst->WriteFile(profile_data_path_);
  90. }
  91. }
  92. void CPUProfiler::ClearInst() { op_info_map_.clear(); }
  93. REGISTER_PYBIND_DEFINE(CPUProfiler_, ([](const py::module *m) {
  94. (void)py::class_<CPUProfiler, std::shared_ptr<CPUProfiler>>(*m, "CPUProfiler")
  95. .def_static("get_instance", &CPUProfiler::GetInstance, "CPUProfiler get_instance.")
  96. .def("init", &CPUProfiler::Init, py::arg("profile_data_path"), "init")
  97. .def("stop", &CPUProfiler::Stop, "stop")
  98. .def("step_profiling_enable", &CPUProfiler::StepProfilingEnable, py::arg("enable_flag"),
  99. "enable or disable step profiling");
  100. }));
  101. } // namespace cpu
  102. } // namespace profiler
  103. } // namespace mindspore