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

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