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 5.5 kB

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