GenericTraceActivity.h 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. #if !defined(TORCH_STABLE_ONLY) && !defined(TORCH_TARGET_VERSION)
  2. /*
  3. * Copyright (c) Meta Platforms, Inc. and affiliates.
  4. * All rights reserved.
  5. *
  6. * This source code is licensed under the BSD-style license found in the
  7. * LICENSE file in the root directory of this source tree.
  8. */
  9. #pragma once
  10. #include <fmt/format.h>
  11. #include <sstream>
  12. #include <string>
  13. #include <thread>
  14. #include <unordered_map>
  15. #include <vector>
  16. #include "ITraceActivity.h"
  17. #include "ThreadUtil.h"
  18. #include "TraceSpan.h"
  19. namespace libkineto {
  20. // Link type, used in GenericTraceActivity.flow.type
  21. constexpr unsigned int kLinkFwdBwd = 1;
  22. constexpr unsigned int kLinkAsyncCpuGpu = 2;
  23. // @lint-ignore-every CLANGTIDY
  24. // cppcoreguidelines-non-private-member-variables-in-classes
  25. // @lint-ignore-every CLANGTIDY cppcoreguidelines-pro-type-member-init
  26. class GenericTraceActivity : public ITraceActivity {
  27. public:
  28. GenericTraceActivity()
  29. : activityType(ActivityType::ENUM_COUNT), traceSpan_(nullptr) {}
  30. GenericTraceActivity(
  31. const TraceSpan& trace,
  32. ActivityType type,
  33. const std::string& name)
  34. : activityType(type), activityName(name), traceSpan_(&trace) {}
  35. int64_t deviceId() const override {
  36. return device;
  37. }
  38. int64_t resourceId() const override {
  39. return resource;
  40. }
  41. void setDevice(int32_t newDevice) {
  42. device = newDevice;
  43. }
  44. int32_t getThreadId() const override {
  45. return threadId;
  46. }
  47. int64_t timestamp() const override {
  48. return startTime;
  49. }
  50. int64_t duration() const override {
  51. return endTime - startTime;
  52. }
  53. int64_t correlationId() const override {
  54. return id;
  55. }
  56. ActivityType type() const override {
  57. return activityType;
  58. }
  59. const ITraceActivity* linkedActivity() const override {
  60. return linked;
  61. }
  62. int flowType() const override {
  63. return flow.type;
  64. }
  65. int64_t flowId() const override {
  66. return flow.id;
  67. }
  68. bool flowStart() const override {
  69. return flow.start;
  70. }
  71. const std::string name() const override {
  72. return activityName;
  73. }
  74. const TraceSpan* traceSpan() const override {
  75. return traceSpan_;
  76. }
  77. void log(ActivityLogger& logger) const override;
  78. // Encode client side metadata as a key/value
  79. template <typename ValType>
  80. void addMetadata(const std::string& key, const ValType& value) {
  81. metadataMap_.emplace(key, std::make_pair(fmt::format("{}", value), false));
  82. }
  83. void addMetadataQuoted(const std::string& key, const std::string& value) {
  84. metadataMap_.emplace(key, std::make_pair(value, true));
  85. }
  86. const std::string getMetadataValue(const std::string& key) const override {
  87. if (auto it = metadataMap_.find(key); it != metadataMap_.end()) {
  88. return it->second.first;
  89. }
  90. return "";
  91. }
  92. const std::string metadataJson() const override {
  93. std::stringstream json;
  94. bool first = true;
  95. for (const auto& [key, val] : metadataMap_) {
  96. if (!first) {
  97. json << ", ";
  98. }
  99. // Ok to use fmt::format here as we are not logging
  100. val.second ? json << fmt::format("\"{}\": \"{}\"", key, val.first)
  101. : json << fmt::format("\"{}\": {}", key, val.first);
  102. first = false;
  103. }
  104. return json.str();
  105. }
  106. virtual ~GenericTraceActivity() override {}
  107. int64_t startTime{0};
  108. int64_t endTime{0};
  109. int32_t id{0};
  110. int32_t device{0};
  111. int32_t resource{0};
  112. int32_t threadId{0};
  113. ActivityType activityType;
  114. std::string activityName;
  115. struct Flow {
  116. Flow() : id(0), type(0), start(0) {}
  117. // Ids must be unique within each type
  118. uint32_t id;
  119. // Type will be used to connect flows between profilers, as
  120. // well as look up flow information (name etc)
  121. uint32_t type : 4;
  122. uint32_t start : 1;
  123. } flow;
  124. const ITraceActivity* linked{nullptr};
  125. private:
  126. const TraceSpan* traceSpan_;
  127. // Metadata map: { key: (value, quoted)}
  128. std::unordered_map<std::string, std::pair<std::string, bool>> metadataMap_;
  129. };
  130. } // namespace libkineto
  131. #else
  132. #error "This file should not be included when either TORCH_STABLE_ONLY or TORCH_TARGET_VERSION is defined."
  133. #endif // !defined(TORCH_STABLE_ONLY) && !defined(TORCH_TARGET_VERSION)