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

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