• 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_PLUGIN_DEVICE_CPU_HAL_PROFILER_CPU_PROFILING_H
18 #define MINDSPORE_CCSRC_PLUGIN_DEVICE_CPU_HAL_PROFILER_CPU_PROFILING_H
19 #include <algorithm>
20 #include <cstdio>
21 #include <map>
22 #include <memory>
23 #include <string>
24 #include <unordered_map>
25 #include <utility>
26 #include <vector>
27 #include <optional>
28 #include "include/backend/debug/profiler/profiling.h"
29 #include "include/backend/debug/profiler/data_saver.h"
30 #include "actor/actormgr.h"
31 #include "include/backend/kernel_graph.h"
32 
33 namespace mindspore {
34 namespace profiler {
35 namespace cpu {
36 constexpr float kNanosecondToMillisecond = 1000000;
37 class CPUProfiler : public Profiler {
38  public:
39   static std::shared_ptr<CPUProfiler> GetInstance();
40 
41   CPUProfiler() = default;
42   ~CPUProfiler() = default;
43   CPUProfiler(const CPUProfiler &) = delete;
44   CPUProfiler &operator=(const CPUProfiler &) = delete;
45 
46   void Init(const std::string &profiling_path, uint32_t device_id, const std::string &profiling_options) override;
Finalize()47   void Finalize() override {}
Start()48   void Start() override {}
49   void Stop() override;
50   void StepProfilingEnable(const bool enable_flag) override;
51   void OpDataProducerBegin(const std::string op_name, const uint32_t pid);
52   void OpDataProducerEnd() override;
53   void OpDataProducerEndParallel(const std::string op_name);
54   void OpDataProducerBeginParallel(const std::string op_name, const uint32_t pid);
55   float SetRuntimeEnd(const std::string op_name, const uint64_t stop_timestamp);
56   void SetRuntimeStart(const std::string op_name, const uint64_t start_timestamp);
57   void RecordFrameWorkInfo(const CNodePtr &kernel);
58   void RecordFrameWorkInfo(const std::string &op_name, const std::vector<BaseShapePtr> &input_shapes);
59   void RecordMemoryPoolInfo(const size_t total_allocated, const size_t total_reserved, const size_t total_active);
60   std::vector<CurKernelInfo> all_kernel_info_;
61   std::mutex kernel_mutex_;
62 
63  private:
64   void SetRunTimeData(const std::string &op_name, const uint32_t pid, bool is_parallel = false);
65   void SaveProfileData() override;
66   void ClearInst() override;
67   void SetGpuHeteroStatus();
68   void RecordGpuOneStepStartEndInfo();
69 
70   uint64_t base_time_;
71   std::string op_name_;
72   uint32_t pid_;
73 
74   uint64_t op_time_start_;
75   uint64_t op_time_mono_start_;
76   uint64_t op_time_stop_;
77 
78   std::optional<bool> is_gpu_hetero_ = {};
79 };
80 }  // namespace cpu
81 }  // namespace profiler
82 }  // namespace mindspore
83 
84 #endif  // MINDSPORE_CCSRC_PLUGIN_DEVICE_CPU_HAL_PROFILER_CPU_PROFILING_H
85