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 3.8 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  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 <time.h>
  18. #include <cxxabi.h>
  19. #include <cmath>
  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. namespace mindspore {
  25. namespace profiler {
  26. namespace cpu {
  27. std::shared_ptr<CPUProfiler> CPUProfiler::profiler_inst_ = nullptr;
  28. std::shared_ptr<CPUProfiler> CPUProfiler::GetInstance() {
  29. if (profiler_inst_ == nullptr) {
  30. profiler_inst_ = std::shared_ptr<CPUProfiler>(new (std::nothrow) CPUProfiler());
  31. }
  32. return profiler_inst_;
  33. }
  34. void CPUProfiler::Init(const std::string &profileDataPath = "") {
  35. MS_LOG(INFO) << "Initialize CPU Profiling";
  36. base_time_ = GetHostMonoTimeStamp();
  37. profile_data_path_ = profileDataPath;
  38. MS_LOG(INFO) << " Host start time(ns): " << base_time_ << " profile data path: " << profile_data_path_;
  39. }
  40. void CPUProfiler::StepProfilingEnable(const bool enable_flag) {
  41. MS_LOG(INFO) << "CPU Profiler enable flag: " << enable_flag;
  42. enable_flag_ = enable_flag;
  43. }
  44. void CPUProfiler::SetRunTimeData(const std::string &op_name, const uint32_t pid) {
  45. auto iter = op_info_map_.find(op_name);
  46. if (iter != op_info_map_.end()) {
  47. iter->second.op_count += 1;
  48. } else {
  49. OpInfo op_info;
  50. op_info.op_name = op_name;
  51. op_info.pid = pid;
  52. op_info.op_count = 1;
  53. op_info_map_[op_name] = op_info;
  54. }
  55. op_name_ = op_name;
  56. pid_ = pid;
  57. }
  58. void CPUProfiler::OpDataProducerBegin(const std::string op_name, const uint32_t pid) {
  59. op_time_start_ = GetHostMonoTimeStamp();
  60. op_time_mono_start_ = GetHostMonoTimeStamp();
  61. SetRunTimeData(op_name, pid);
  62. }
  63. void CPUProfiler::OpDataProducerEnd() {
  64. float op_time_elapsed = 0;
  65. op_time_stop_ = GetHostMonoTimeStamp();
  66. op_time_elapsed = (op_time_stop_ - op_time_start_) / kNanosecondToMillisecond;
  67. MS_LOG(DEBUG) << "Host Time Elapsed(us)," << op_name_ << "," << op_time_elapsed;
  68. Profiler::SetRunTimeData(op_name_, op_time_elapsed);
  69. Profiler::SetRunTimeData(op_name_, op_time_mono_start_, op_time_elapsed);
  70. }
  71. void CPUProfiler::Stop() {
  72. MS_LOG(INFO) << "Stop CPU Profiling";
  73. SaveProfileData();
  74. ClearInst();
  75. }
  76. void CPUProfiler::SaveProfileData() {
  77. if (profile_data_path_.empty()) {
  78. MS_LOG(WARNING) << "Profile data path is empty, skip save profile data.";
  79. } else {
  80. CpuDataSaver dataSaver;
  81. dataSaver.ParseOpInfo(op_info_map_);
  82. dataSaver.WriteFile(profile_data_path_);
  83. }
  84. }
  85. void CPUProfiler::ClearInst() { op_info_map_.clear(); }
  86. REGISTER_PYBIND_DEFINE(CPUProfiler_, ([](const py::module *m) {
  87. (void)py::class_<CPUProfiler, std::shared_ptr<CPUProfiler>>(*m, "CPUProfiler")
  88. .def_static("get_instance", &CPUProfiler::GetInstance, "CPUProfiler get_instance.")
  89. .def("init", &CPUProfiler::Init, py::arg("profile_data_path"), "init")
  90. .def("stop", &CPUProfiler::Stop, "stop")
  91. .def("step_profiling_enable", &CPUProfiler::StepProfilingEnable, py::arg("enable_flag"),
  92. "enable or disable step profiling");
  93. }));
  94. } // namespace cpu
  95. } // namespace profiler
  96. } // namespace mindspore