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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  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. #include "minddata/dataset/engine/perf/profiling.h"
  17. #include <cstdlib>
  18. #include <fstream>
  19. #include "utils/ms_utils.h"
  20. #include "minddata/dataset/util/path.h"
  21. #ifdef ENABLE_GPUQUE
  22. #include "minddata/dataset/core/config_manager.h"
  23. #include "minddata/dataset/core/global_context.h"
  24. #endif
  25. #include "minddata/dataset/engine/perf/monitor.h"
  26. #include "minddata/dataset/engine/perf/device_queue_tracing.h"
  27. #include "minddata/dataset/engine/perf/connector_size.h"
  28. #include "minddata/dataset/engine/perf/connector_throughput.h"
  29. #include "minddata/dataset/engine/perf/cpu_sampling.h"
  30. #include "minddata/dataset/engine/perf/dataset_iterator_tracing.h"
  31. #include "minddata/dataset/util/log_adapter.h"
  32. namespace mindspore {
  33. namespace dataset {
  34. // Constructor
  35. ProfilingManager::ProfilingManager(ExecutionTree *tree) : tree_(tree), enabled_(true) {
  36. perf_monitor_ = std::make_unique<Monitor>(tree_);
  37. }
  38. bool ProfilingManager::IsProfilingEnable() const { return common::GetEnv("PROFILING_MODE") == "true" && enabled_; }
  39. Status ProfilingManager::Initialize() {
  40. // Register nodes based on config
  41. std::string dir = common::GetEnv("MINDDATA_PROFILING_DIR");
  42. if (dir.empty()) {
  43. RETURN_STATUS_UNEXPECTED("Profiling dir is not set.");
  44. }
  45. char real_path[PATH_MAX] = {0};
  46. if (dir.size() >= PATH_MAX) {
  47. RETURN_STATUS_UNEXPECTED("Profiling dir is invalid.");
  48. }
  49. #if defined(_WIN32) || defined(_WIN64)
  50. if (_fullpath(real_path, common::SafeCStr(dir), PATH_MAX) == nullptr) {
  51. RETURN_STATUS_UNEXPECTED("Profiling dir is invalid.");
  52. }
  53. #else
  54. if (realpath(common::SafeCStr(dir), real_path) == nullptr) {
  55. RETURN_STATUS_UNEXPECTED("Profiling dir is invalid.");
  56. }
  57. #endif
  58. dir_path_ = real_path;
  59. #ifdef ENABLE_GPUQUE
  60. std::shared_ptr<ConfigManager> cfg = GlobalContext::config_manager();
  61. int32_t rank_id = cfg->rank_id();
  62. // If DEVICE_ID is not set, default value is 0
  63. if (rank_id < 0) {
  64. device_id_ = common::GetEnv("DEVICE_ID");
  65. // If DEVICE_ID is not set, default value is 0
  66. if (device_id_.empty()) {
  67. device_id_ = "0";
  68. }
  69. } else {
  70. device_id_ = std::to_string(rank_id);
  71. }
  72. #else
  73. device_id_ = common::GetEnv("DEVICE_ID");
  74. // If DEVICE_ID is not set, default value is 0
  75. if (device_id_.empty()) {
  76. device_id_ = "0";
  77. }
  78. #endif
  79. // Register all profiling node.
  80. // device_queue node is used for graph mode
  81. std::shared_ptr<Tracing> device_queue_tracing = std::make_shared<DeviceQueueTracing>();
  82. RETURN_IF_NOT_OK(RegisterTracingNode(device_queue_tracing));
  83. // dataset_iterator node is used for graph mode
  84. std::shared_ptr<Tracing> dataset_iterator_tracing = std::make_shared<DatasetIteratorTracing>();
  85. RETURN_IF_NOT_OK(RegisterTracingNode(dataset_iterator_tracing));
  86. std::shared_ptr<Sampling> connector_size_sampling = std::make_shared<ConnectorSize>(tree_);
  87. RETURN_IF_NOT_OK(RegisterSamplingNode(connector_size_sampling));
  88. std::shared_ptr<Sampling> connector_thr_sampling = std::make_shared<ConnectorThroughput>(tree_);
  89. RETURN_IF_NOT_OK(RegisterSamplingNode(connector_thr_sampling));
  90. #ifndef ENABLE_ANDROID
  91. std::shared_ptr<Sampling> cpu_sampling = std::make_shared<CpuSampling>(tree_);
  92. RETURN_IF_NOT_OK(RegisterSamplingNode(cpu_sampling));
  93. #endif
  94. return Status::OK();
  95. }
  96. // Launch monitoring thread.
  97. Status ProfilingManager::LaunchMonitor() {
  98. RETURN_IF_NOT_OK(tree_->AllTasks()->CreateAsyncTask("Monitor Thread launched", std::ref(*perf_monitor_)));
  99. return Status::OK();
  100. }
  101. // Profiling node registration
  102. Status ProfilingManager::RegisterTracingNode(std::shared_ptr<Tracing> node) {
  103. // Check if node with the same name has already been registered.
  104. auto exist = tracing_nodes_.find(node->Name());
  105. if (exist != tracing_nodes_.end()) {
  106. return Status(StatusCode::kMDProfilingError, "Profiling node already exist: " + node->Name());
  107. }
  108. // Register the node with its name as key.
  109. RETURN_IF_NOT_OK(node->Init(dir_path_, device_id_));
  110. tracing_nodes_[node->Name()] = node;
  111. return Status::OK();
  112. }
  113. // Profiling node getter
  114. Status ProfilingManager::GetTracingNode(const std::string &name, std::shared_ptr<Tracing> *node) {
  115. // Check if node with the same name has already been registered.
  116. auto exist = tracing_nodes_.find(name);
  117. if (exist == tracing_nodes_.end()) {
  118. return Status(StatusCode::kMDProfilingError, "Profiling node does not exist: " + name);
  119. }
  120. // Fetch node.
  121. *node = tracing_nodes_[name];
  122. return Status::OK();
  123. }
  124. // Profiling node registration
  125. Status ProfilingManager::RegisterSamplingNode(std::shared_ptr<Sampling> node) {
  126. // Check if node with the same name has already been registered.
  127. auto exist = sampling_nodes_.find(node->Name());
  128. if (exist != sampling_nodes_.end()) {
  129. return Status(StatusCode::kMDProfilingError, "Profiling node already exist: " + node->Name());
  130. }
  131. // Register the node with its name as key.
  132. RETURN_IF_NOT_OK(node->Init(dir_path_, device_id_));
  133. sampling_nodes_[node->Name()] = node;
  134. return Status::OK();
  135. }
  136. // Profiling node getter
  137. Status ProfilingManager::GetSamplingNode(const std::string &name, std::shared_ptr<Sampling> *node) {
  138. // Check if node with the same name has already been registered.
  139. auto exist = sampling_nodes_.find(name);
  140. if (exist == sampling_nodes_.end()) {
  141. return Status(StatusCode::kMDProfilingError, "Profiling node does not exist: " + name);
  142. }
  143. // Fetch node.
  144. *node = sampling_nodes_[name];
  145. return Status::OK();
  146. }
  147. Status ProfilingManager::SaveProfilingData() {
  148. if (!IsProfilingEnable()) {
  149. return Status::OK();
  150. }
  151. MS_LOG(INFO) << "Start to save profiling data.";
  152. for (auto node : tracing_nodes_) {
  153. RETURN_IF_NOT_OK(node.second->SaveToFile());
  154. }
  155. for (auto node : sampling_nodes_) {
  156. RETURN_IF_NOT_OK(node.second->SaveToFile());
  157. }
  158. MS_LOG(INFO) << "Save profiling data end.";
  159. return Status::OK();
  160. }
  161. Status ProfilingManager::Analyze() {
  162. if (!IsProfilingEnable()) {
  163. return Status::OK();
  164. }
  165. MS_LOG(INFO) << "Start to analyze profiling data.";
  166. for (auto node : sampling_nodes_) {
  167. RETURN_IF_NOT_OK(node.second->Analyze());
  168. }
  169. return Status::OK();
  170. }
  171. Status ProfilingManager::ChangeFileMode() {
  172. if (!IsProfilingEnable()) {
  173. return Status::OK();
  174. }
  175. MS_LOG(INFO) << "Start to change file mode.";
  176. for (auto node : tracing_nodes_) {
  177. RETURN_IF_NOT_OK(node.second->ChangeFileMode());
  178. }
  179. for (auto node : sampling_nodes_) {
  180. RETURN_IF_NOT_OK(node.second->ChangeFileMode());
  181. }
  182. MS_LOG(INFO) << "Change file mode end.";
  183. return Status::OK();
  184. }
  185. uint64_t ProfilingTime::GetCurMilliSecond() {
  186. // because cpplint does not allow using namespace
  187. using std::chrono::duration_cast;
  188. using std::chrono::milliseconds;
  189. using std::chrono::steady_clock;
  190. return static_cast<uint64_t>(duration_cast<milliseconds>(steady_clock::now().time_since_epoch()).count());
  191. }
  192. } // namespace dataset
  193. } // namespace mindspore