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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  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 ~Sampling() = default;
  57. };
  58. // Tracing is class of profiling which record samples upon request.
  59. class Tracing : public Profiling {
  60. // Tracing does not define a fixed interface to provide flexible on data recording.
  61. };
  62. // ProfilingManager is a class manages all profiling infrastructure
  63. // It serves the following purposes:
  64. // 1) Fetch profiling configs from global contexts
  65. // 2) Setup all profiling node based on config
  66. // 3) Provide access of profiling nodes for profiling actions
  67. // 4) Manage profiling data serialization process
  68. class ProfilingManager {
  69. public:
  70. explicit ProfilingManager(ExecutionTree *tree);
  71. ~ProfilingManager() = default;
  72. Status Initialize();
  73. // Save profile data to file
  74. // @return Status The status code returned
  75. Status SaveProfilingData();
  76. // Sampling node getter
  77. // @param name - The name of the requested node
  78. // @param node - Pointer to the shared pointer for the Sampling node
  79. // @return Status The status code returned
  80. Status GetSamplingNode(const std::string &name, std::shared_ptr<Sampling> *node);
  81. // Tracing node getter
  82. // @param name - The name of the requested node
  83. // @param node - Pointer to the shared pointer for the Tracing node
  84. // @return Status The status code returned
  85. Status GetTracingNode(const std::string &name, std::shared_ptr<Tracing> *node);
  86. // return true if env variable has profiling enabled and enabled_ is set to true.
  87. bool IsProfilingEnable() const;
  88. // Calling this would disable Profiling functionality for the entire duration of ExecutionTree. It cannot be
  89. // re-enabled. Each execution_tree is associated with a unique profiling_manager which will start when tree is
  90. // launched. This is the master off switch, once called, it won't start profiler even if env variable says so.
  91. void DisableProfiling() { enabled_ = false; }
  92. const std::unordered_map<std::string, std::shared_ptr<Sampling>> &GetSamplingNodes() { return sampling_nodes_; }
  93. // Launch monitoring thread.
  94. Status LaunchMonitor();
  95. Status ChangeFileMode();
  96. private:
  97. std::unique_ptr<Monitor> perf_monitor_;
  98. bool enabled_;
  99. std::unordered_map<std::string, std::shared_ptr<Tracing>> tracing_nodes_;
  100. std::unordered_map<std::string, std::shared_ptr<Sampling>> sampling_nodes_;
  101. // Register profile node to tree
  102. // @param node - Profiling node
  103. // @return Status The status code returned
  104. Status RegisterTracingNode(std::shared_ptr<Tracing> node);
  105. // Register profile node to tree
  106. // @param node - Profiling node
  107. // @return Status The status code returned
  108. Status RegisterSamplingNode(std::shared_ptr<Sampling> node);
  109. ExecutionTree *tree_; // ExecutionTree pointer
  110. std::string dir_path_; // where to create profiling file
  111. std::string device_id_; // used when create profiling file,filename_device_id.suffix
  112. };
  113. enum ProfilingType { TIME, CONNECTOR_DEPTH };
  114. enum ProfilingTimeSubType {
  115. PIPELINE_TIME,
  116. TDT_PUSH_TIME,
  117. BATCH_TIME,
  118. INVALID_TIME,
  119. };
  120. class ProfilingTime {
  121. public:
  122. static uint64_t GetCurMilliSecond();
  123. };
  124. } // namespace dataset
  125. } // namespace mindspore
  126. #endif