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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  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. // Profiling is a class of basic unit of profiling action
  33. // This base class encapsulate the serialization output logic
  34. class Profiling : std::enable_shared_from_this<Profiling> {
  35. public:
  36. // Constructor
  37. Profiling() = default;
  38. // Destructor
  39. virtual ~Profiling() = default;
  40. virtual Status Init(const std::string &dir_path, const std::string &device_id) = 0;
  41. // Default serialization file generator
  42. virtual Status SaveToFile() = 0;
  43. // Profiling name
  44. virtual std::string Name() const = 0;
  45. virtual Status ChangeFileMode() = 0;
  46. protected:
  47. std::string file_path_;
  48. };
  49. // Sampling is a class of profiling which generate samples periodically.
  50. class Sampling : public Profiling {
  51. public:
  52. // Sampling action function. This function will be invoked by performance monitor thread.
  53. virtual Status Sample() = 0;
  54. // virtual Status TestPrint() = 0;
  55. virtual ~Sampling() = default;
  56. };
  57. // Tracing is class of profiling which record samples upon request.
  58. class Tracing : public Profiling {
  59. // Tracing does not define a fixed interface to provide flexible on data recording.
  60. };
  61. // ProfilingManager is a class manages all profiling infrastructure
  62. // It serves the following purposes:
  63. // 1) Fetch profiling configs from global contexts
  64. // 2) Setup all profiling node based on config
  65. // 3) Provide access of profiling nodes for profiling actions
  66. // 4) Manage profiling data serialization process
  67. class ProfilingManager {
  68. public:
  69. explicit ProfilingManager(ExecutionTree *tree);
  70. ~ProfilingManager() = default;
  71. Status Initialize();
  72. // Save profile data to file
  73. // @return Status - The error code return
  74. Status SaveProfilingData();
  75. // Sampling node getter
  76. // @param name - The name of the requested node
  77. // @param node - Pointer to the shared pointer for the Sampling node
  78. // @return Status - The error code return
  79. Status GetSamplingNode(const std::string &name, std::shared_ptr<Sampling> *node);
  80. // Tracing node getter
  81. // @param name - The name of the requested node
  82. // @param node - Pointer to the shared pointer for the Tracing node
  83. // @return Status - The error code return
  84. Status GetTracingNode(const std::string &name, std::shared_ptr<Tracing> *node);
  85. // If profiling is enabled.
  86. bool IsProfilingEnable() const;
  87. const std::unordered_map<std::string, std::shared_ptr<Sampling>> &GetSamplingNodes() { return sampling_nodes_; }
  88. // Launch monitoring thread.
  89. Status LaunchMonitor();
  90. Status ChangeFileMode();
  91. private:
  92. std::unique_ptr<Monitor> perf_monitor_;
  93. std::unordered_map<std::string, std::shared_ptr<Tracing>> tracing_nodes_;
  94. std::unordered_map<std::string, std::shared_ptr<Sampling>> sampling_nodes_;
  95. // Register profile node to tree
  96. // @param node - Profiling node
  97. // @return Status - The error code return
  98. Status RegisterTracingNode(std::shared_ptr<Tracing> node);
  99. // Register profile node to tree
  100. // @param node - Profiling node
  101. // @return Status - The error code return
  102. Status RegisterSamplingNode(std::shared_ptr<Sampling> node);
  103. ExecutionTree *tree_ = nullptr; // ExecutionTree pointer
  104. std::string dir_path_; // where to create profiling file
  105. std::string device_id_; // used when create profiling file,filename_deviceid.suffix
  106. };
  107. enum ProfilingType { TIME, CONNECTOR_DEPTH };
  108. enum ProfilingTimeSubType {
  109. PIPELINE_TIME,
  110. TDT_PUSH_TIME,
  111. BATCH_TIME,
  112. INVALID_TIME,
  113. };
  114. class ProfilingTime {
  115. public:
  116. static int64_t GetCurMilliSecond();
  117. };
  118. } // namespace dataset
  119. } // namespace mindspore
  120. #endif