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

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