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.h 3.4 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  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. #ifndef MINDSPORE_CCSRC_PROFILER_DEVICE_PROFILING_H
  17. #define MINDSPORE_CCSRC_PROFILER_DEVICE_PROFILING_H
  18. #include <algorithm>
  19. #include <cstdio>
  20. #include <map>
  21. #include <memory>
  22. #include <mutex>
  23. #include <string>
  24. #include <unordered_map>
  25. #include <utility>
  26. #include <vector>
  27. #include "utils/hash_map.h"
  28. namespace mindspore {
  29. namespace profiler {
  30. struct StartDuration {
  31. uint64_t start_timestamp = 0l;
  32. float duration = 0l;
  33. };
  34. struct OneStepStartEndInfo {
  35. std::string iter_start_op_name;
  36. std::string fp_start_op_name;
  37. std::string iter_end_op_name;
  38. };
  39. struct OpInfo {
  40. std::string op_name;
  41. float cupti_api_call_time = 0l;
  42. float cupti_activity_time = 0l;
  43. float op_host_cost_time = 0;
  44. int op_kernel_api_count = 0;
  45. int op_kernel_count = 0;
  46. int op_count = 0;
  47. std::vector<StartDuration> start_duration;
  48. void *stream;
  49. uint32_t pid;
  50. };
  51. class ProfilerManager {
  52. public:
  53. static std::shared_ptr<ProfilerManager> &GetInstance();
  54. ProfilerManager() = default;
  55. ~ProfilerManager() = default;
  56. ProfilerManager(const ProfilerManager &) = delete;
  57. ProfilerManager &operator=(const ProfilerManager &) = delete;
  58. bool GetProfilingEnableFlag() const;
  59. void RecordOneStepStartEndInfo() const;
  60. std::string GetProfilingOptions() const;
  61. private:
  62. static std::shared_ptr<ProfilerManager> profiler_manager_inst_;
  63. };
  64. class Profiler {
  65. public:
  66. Profiler() = default;
  67. virtual ~Profiler() = default;
  68. virtual void Init(const std::string &profileDataPath) = 0;
  69. virtual void Stop() = 0;
  70. virtual void StepProfilingEnable(const bool enable_flag) = 0;
  71. virtual void OpDataProducerEnd() = 0;
  72. void RecordOneStepStartEndInfo();
  73. bool GetEnableFlag() const { return enable_flag_; }
  74. std::string ProfileDataPath() const { return profile_data_path_; }
  75. void RecordOneStepStartEndInfo(std::string op_name);
  76. std::pair<double, double> GetSingleOpLaunchTime() { return single_op_launch_start_time_end_time_; }
  77. void SetSingleOpLaunchTime(const std::pair<double, double> &launch_start_end) {
  78. single_op_launch_start_time_end_time_ = launch_start_end;
  79. }
  80. protected:
  81. void SetRunTimeData(const std::string &op_name, const float time_elapsed);
  82. void SetRunTimeData(const std::string &op_name, const uint64_t start, const float duration);
  83. uint64_t GetHostMonoTimeStamp() const;
  84. virtual void SaveProfileData() = 0;
  85. virtual void ClearInst() = 0;
  86. std::pair<double, double> single_op_launch_start_time_end_time_;
  87. bool enable_flag_ = false;
  88. std::string profile_data_path_;
  89. std::unordered_map<std::string, OpInfo> op_info_map_;
  90. OneStepStartEndInfo step_start_end_info_;
  91. std::vector<OneStepStartEndInfo> all_step_start_end_info_;
  92. std::mutex record_mutex_;
  93. };
  94. } // namespace profiler
  95. } // namespace mindspore
  96. #endif // MINDSPORE_CCSRC_PROFILER_DEVICE_PROFILING_H