GenericTraceActivity.h 3.8 KB

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