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.4 kB

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