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

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