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