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.

profiling.cc 4.2 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  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/profiling.h"
  17. #include <cxxabi.h>
  18. #include <cmath>
  19. #include <ctime>
  20. #include "pybind_api/api_register.h"
  21. #include "utils/log_adapter.h"
  22. #include "utils/utils.h"
  23. #if ENABLE_GPU
  24. #include "profiler/device/gpu/gpu_profiling.h"
  25. #endif
  26. #if ENABLE_D
  27. #include "profiler/device/ascend/ascend_profiling.h"
  28. #endif
  29. namespace mindspore {
  30. namespace profiler {
  31. std::shared_ptr<ProfilerManager> ProfilerManager::profiler_manager_inst_ = std::make_shared<ProfilerManager>();
  32. uint64_t Profiler::GetHostMonoTimeStamp() const {
  33. struct timespec ts;
  34. #if defined(_WIN32) || defined(_WIN64)
  35. if (clock_gettime(CLOCK_MONOTONIC, &ts) != 0) {
  36. MS_LOG(ERROR) << "Get host timestamp failed";
  37. return 0;
  38. }
  39. #else
  40. if (clock_gettime(CLOCK_MONOTONIC_RAW, &ts) != 0) {
  41. MS_LOG(ERROR) << "Get host timestamp failed";
  42. return 0;
  43. }
  44. #endif
  45. constexpr uint64_t kNSecondInSecond = 1000000000;
  46. uint64_t cur_time_stamp = ts.tv_sec * kNSecondInSecond + ts.tv_nsec;
  47. return cur_time_stamp;
  48. }
  49. void Profiler::SetRunTimeData(const std::string &op_name, const float time_elapsed) {
  50. auto iter = op_info_map_.find(op_name);
  51. if (iter != op_info_map_.end()) {
  52. iter->second.op_host_cost_time += time_elapsed;
  53. }
  54. }
  55. void Profiler::SetRunTimeData(const std::string &op_name, const uint64_t start, const float duration) {
  56. auto iter = op_info_map_.find(op_name);
  57. if (iter != op_info_map_.end()) {
  58. iter->second.start_duration.emplace_back(StartDuration({start, duration}));
  59. }
  60. }
  61. void Profiler::RecordOneStepStartEndInfo() {
  62. std::lock_guard<std::mutex> locker(record_mutex_);
  63. all_step_start_end_info_.push_back(step_start_end_info_);
  64. step_start_end_info_.iter_start_op_name = "";
  65. step_start_end_info_.fp_start_op_name = "";
  66. }
  67. void Profiler::RecordOneStepStartEndInfo(const std::string op_name) {
  68. std::lock_guard<std::mutex> locker(record_mutex_);
  69. if (step_start_end_info_.iter_start_op_name.empty()) {
  70. step_start_end_info_.iter_start_op_name = op_name;
  71. step_start_end_info_.fp_start_op_name = op_name;
  72. }
  73. std::string fp_start_op_name = step_start_end_info_.fp_start_op_name;
  74. auto op_type_begin_iter = fp_start_op_name.rfind('/') + 1;
  75. auto op_type_end_iter = fp_start_op_name.rfind('-');
  76. auto op_type = fp_start_op_name.substr(op_type_begin_iter, op_type_end_iter - op_type_begin_iter);
  77. if (op_type == "InitDataSetQueue" || op_type == "GetNext") {
  78. step_start_end_info_.fp_start_op_name = op_name;
  79. }
  80. step_start_end_info_.iter_end_op_name = op_name;
  81. }
  82. std::shared_ptr<ProfilerManager> &ProfilerManager::GetInstance() {
  83. MS_EXCEPTION_IF_NULL(profiler_manager_inst_);
  84. return profiler_manager_inst_;
  85. }
  86. bool ProfilerManager::GetProfilingEnableFlag() const {
  87. #if ENABLE_GPU
  88. return profiler::gpu::GPUProfiler::GetInstance()->GetEnableFlag();
  89. #endif
  90. #if ENABLE_D
  91. auto ascend_instance = profiler::ascend::AscendProfiler::GetInstance();
  92. MS_EXCEPTION_IF_NULL(ascend_instance);
  93. return ascend_instance->GetProfilingEnableFlag();
  94. #endif
  95. return false;
  96. }
  97. void ProfilerManager::RecordOneStepStartEndInfo() const {
  98. #if ENABLE_GPU
  99. auto gpu_profiler_inst = profiler::gpu::GPUProfiler::GetInstance();
  100. if (gpu_profiler_inst->GetEnableFlag()) {
  101. gpu_profiler_inst->RecordOneStepStartEndInfo();
  102. }
  103. #endif
  104. }
  105. std::string ProfilerManager::GetProfilingOptions() const {
  106. #if ENABLE_D
  107. auto ascend_instance = profiler::ascend::AscendProfiler::GetInstance();
  108. MS_EXCEPTION_IF_NULL(ascend_instance);
  109. return ascend_instance->GetProfilingOptions();
  110. #endif
  111. return "";
  112. }
  113. } // namespace profiler
  114. } // namespace mindspore