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.

ascend_profiling.cc 2.5 kB

5 years ago
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. /**
  2. * Copyright 2019-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 "profiler/device/ascend/ascend_profiling.h"
  17. #include <cstdarg>
  18. #include <iomanip>
  19. #include <utility>
  20. #include "utils/log_adapter.h"
  21. #include "./securec.h"
  22. namespace mindspore {
  23. namespace profiler {
  24. namespace ascend {
  25. const int kMaxEvents = 10000;
  26. const int kEventDescMax = 256;
  27. const int kMaxEventTypes = 8;
  28. const int kIndent = 8;
  29. AscendProfiler::AscendProfiler() : counter_(0) { Reset(); }
  30. void AscendProfiler::RecordEvent(EventType event_type, const char *fmt, ...) {
  31. va_list args;
  32. va_start(args, fmt);
  33. char buf[kEventDescMax];
  34. if (vsnprintf_s(buf, kEventDescMax, kEventDescMax - 1, fmt, args) == -1) {
  35. MS_LOG(ERROR) << "format failed:" << fmt;
  36. va_end(args);
  37. return;
  38. }
  39. va_end(args);
  40. std::string event = buf;
  41. auto index = counter_++;
  42. auto &evt = events_[index];
  43. evt.timestamp = std::chrono::system_clock::now();
  44. evt.desc = std::move(event);
  45. evt.event_type = event_type;
  46. }
  47. void AscendProfiler::Dump(std::ostream &output_stream) {
  48. MS_LOG(INFO) << "start dump async profiling info";
  49. if (events_.empty()) {
  50. return;
  51. }
  52. auto first_evt = events_[0];
  53. auto start = first_evt.timestamp;
  54. std::vector<decltype(start)> prev_timestamps;
  55. prev_timestamps.resize(kMaxEventTypes, start);
  56. for (int i = 0; i < counter_; ++i) {
  57. auto &evt = events_[i];
  58. auto elapsed = std::chrono::duration_cast<std::chrono::microseconds>(evt.timestamp - start).count();
  59. auto &prev_ts = prev_timestamps[evt.event_type];
  60. auto cost = std::chrono::duration_cast<std::chrono::microseconds>(evt.timestamp - prev_ts).count();
  61. prev_ts = evt.timestamp;
  62. output_stream << std::setw(kIndent) << elapsed << "\t\t" << cost << "\t\t" << evt.desc << std::endl;
  63. }
  64. events_.clear();
  65. MS_LOG(INFO) << "end";
  66. }
  67. void AscendProfiler::Reset() {
  68. counter_ = 0;
  69. events_.clear();
  70. events_.resize(kMaxEvents);
  71. }
  72. } // namespace ascend
  73. } // namespace profiler
  74. } // namespace mindspore