1 /** 2 * Copyright 2020 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_MINDDATA_DATASET_UTIL_PROFILE_H_ 17 #define MINDSPORE_CCSRC_MINDDATA_DATASET_UTIL_PROFILE_H_ 18 19 #include <string> 20 #include <vector> 21 #include <unordered_map> 22 #include <memory> 23 #include <chrono> 24 #include <nlohmann/json.hpp> 25 #include "minddata/dataset/util/status.h" 26 27 namespace mindspore { 28 namespace dataset { 29 30 class Monitor; 31 class ExecutionTree; 32 33 const char kDeviceQueueTracingName[] = "Device_Queue_Tracing"; 34 const char kDatasetIteratorTracingName[] = "Dataset_Iterator_Tracing"; 35 const char kConnectorSizeSamplingName[] = "Connector_Size_Sampling"; 36 const char kConnectorThroughputSamplingName[] = "Connector_Throughput_Sampling"; 37 const char kCpuSamplingName[] = "Cpu_Sampling"; 38 39 // Profiling is a class of basic unit of profiling action 40 // This base class encapsulate the serialization output logic 41 class Profiling : std::enable_shared_from_this<Profiling> { 42 public: 43 // Constructor 44 Profiling() = default; 45 46 // Destructor 47 virtual ~Profiling() = default; 48 49 virtual Status Init(const std::string &dir_path, const std::string &device_id) = 0; 50 51 // Default serialization file generator 52 virtual Status SaveToFile() = 0; 53 54 // Profiling name 55 virtual std::string Name() const = 0; 56 57 virtual Status ChangeFileMode() = 0; 58 59 protected: 60 std::string file_path_; 61 }; 62 63 // Sampling is a class of profiling which generate samples periodically. 64 class Sampling : public Profiling { 65 public: 66 // Sampling action function. This function will be invoked by performance monitor thread. 67 virtual Status Sample() = 0; 68 // virtual Status TestPrint() = 0; 69 virtual Status Analyze() = 0; 70 virtual ~Sampling() = default; 71 Status ReadJson(nlohmann::json *output); 72 }; 73 74 // Tracing is class of profiling which record samples upon request. 75 class Tracing : public Profiling { 76 public: 77 // Tracing has minimal interface to provide flexible on data recording. 78 // It only includes some common routines. 79 Status SaveToFile(); 80 81 protected: 82 std::vector<std::string> value_; 83 }; 84 85 // ProfilingManager is a class manages all profiling infrastructure 86 // It serves the following purposes: 87 // 1) Fetch profiling configs from global contexts 88 // 2) Setup all profiling node based on config 89 // 3) Provide access of profiling nodes for profiling actions 90 // 4) Manage profiling data serialization process 91 class ProfilingManager { 92 public: 93 explicit ProfilingManager(ExecutionTree *tree); 94 95 ~ProfilingManager() = default; 96 97 Status Initialize(); 98 99 // Save profile data to file 100 // @return Status The status code returned 101 Status SaveProfilingData(); 102 103 // Sampling node getter 104 // @param name - The name of the requested node 105 // @param node - Pointer to the shared pointer for the Sampling node 106 // @return Status The status code returned 107 Status GetSamplingNode(const std::string &name, std::shared_ptr<Sampling> *node); 108 109 // Tracing node getter 110 // @param name - The name of the requested node 111 // @param node - Pointer to the shared pointer for the Tracing node 112 // @return Status The status code returned 113 Status GetTracingNode(const std::string &name, std::shared_ptr<Tracing> *node); 114 115 // return true if env variable has profiling enabled and enabled_ is set to true. 116 bool IsProfilingEnable() const; 117 118 // Calling this would disable Profiling functionality for the entire duration of ExecutionTree. It cannot be 119 // re-enabled. Each execution_tree is associated with a unique profiling_manager which will start when tree is 120 // launched. This is the master off switch, once called, it won't start profiler even if env variable says so. DisableProfiling()121 void DisableProfiling() { enabled_ = false; } 122 GetSamplingNodes()123 const std::unordered_map<std::string, std::shared_ptr<Sampling>> &GetSamplingNodes() { return sampling_nodes_; } 124 125 // Launch monitoring thread. 126 Status LaunchMonitor(); 127 128 Status ChangeFileMode(); 129 130 // Analyze profile data and print warning messages 131 Status Analyze(); 132 133 private: 134 std::unique_ptr<Monitor> perf_monitor_; 135 bool enabled_; 136 std::unordered_map<std::string, std::shared_ptr<Tracing>> tracing_nodes_; 137 138 std::unordered_map<std::string, std::shared_ptr<Sampling>> sampling_nodes_; 139 140 // Register profile node to tree 141 // @param node - Profiling node 142 // @return Status The status code returned 143 Status RegisterTracingNode(std::shared_ptr<Tracing> node); 144 145 // Register profile node to tree 146 // @param node - Profiling node 147 // @return Status The status code returned 148 Status RegisterSamplingNode(std::shared_ptr<Sampling> node); 149 150 ExecutionTree *tree_; // ExecutionTree pointer 151 std::string dir_path_; // where to create profiling file 152 std::string device_id_; // used when create profiling file,filename_device_id.suffix 153 }; 154 155 enum ProfilingType { TIME, CONNECTOR_DEPTH }; 156 157 enum ProfilingTimeSubType { 158 PIPELINE_TIME, 159 TDT_PUSH_TIME, 160 BATCH_TIME, 161 INVALID_TIME, 162 }; 163 164 class ProfilingTime { 165 public: 166 static uint64_t GetCurMilliSecond(); 167 }; 168 } // namespace dataset 169 } // namespace mindspore 170 #endif 171