GenericTraceActivity.h 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  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 <string>
  11. #include <thread>
  12. #include <unordered_map>
  13. #include <vector>
  14. #include <sstream>
  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 cppcoreguidelines-non-private-member-variables-in-classes
  23. // @lint-ignore-every CLANGTIDY cppcoreguidelines-pro-type-member-init
  24. class GenericTraceActivity : public ITraceActivity {
  25. public:
  26. GenericTraceActivity()
  27. : activityType(ActivityType::ENUM_COUNT), traceSpan_(nullptr) {}
  28. GenericTraceActivity(
  29. const TraceSpan& trace, ActivityType type, const std::string& name)
  30. : activityType(type), activityName(name), traceSpan_(&trace) {}
  31. int64_t deviceId() const override {
  32. return device;
  33. }
  34. int64_t resourceId() const override {
  35. return resource;
  36. }
  37. int32_t getThreadId() const override {
  38. return threadId;
  39. }
  40. int64_t timestamp() const override {
  41. return startTime;
  42. }
  43. int64_t duration() const override {
  44. return endTime - startTime;
  45. }
  46. int64_t correlationId() const override {
  47. return id;
  48. }
  49. ActivityType type() const override {
  50. return activityType;
  51. }
  52. const ITraceActivity* linkedActivity() const override {
  53. return linked;
  54. }
  55. int flowType() const override {
  56. return flow.type;
  57. }
  58. int flowId() const override {
  59. return flow.id;
  60. }
  61. bool flowStart() const override {
  62. return flow.start;
  63. }
  64. const std::string name() const override {
  65. return activityName;
  66. }
  67. const TraceSpan* traceSpan() const override {
  68. return traceSpan_;
  69. }
  70. void log(ActivityLogger& logger) const override;
  71. // Encode client side metadata as a key/value
  72. template <typename ValType>
  73. void addMetadata(const std::string& key, const ValType& value) {
  74. metadataMap_.emplace(key, std::make_pair(fmt::format("{}", value), false));
  75. }
  76. void addMetadataQuoted(const std::string& key, const std::string& value) {
  77. metadataMap_.emplace(key, std::make_pair(value, true));
  78. }
  79. const std::string getMetadataValue(const std::string& key) const override {
  80. if (auto it = metadataMap_.find(key); it != metadataMap_.end()) {
  81. return it->second.first;
  82. }
  83. return "";
  84. }
  85. const std::string metadataJson() const override {
  86. std::stringstream json;
  87. bool first = true;
  88. for (const auto& [key, val] : metadataMap_) {
  89. if (!first) {
  90. json << ", ";
  91. }
  92. val.second ? json << fmt::format("\"{}\": \"{}\"", key, val.first)
  93. : json << fmt::format("\"{}\": {}", key, val.first);
  94. first = false;
  95. }
  96. return json.str();
  97. }
  98. virtual ~GenericTraceActivity() override {}
  99. int64_t startTime{0};
  100. int64_t endTime{0};
  101. int32_t id{0};
  102. int32_t device{0};
  103. int32_t resource{0};
  104. int32_t threadId{0};
  105. ActivityType activityType;
  106. std::string activityName;
  107. struct Flow {
  108. Flow(): id(0), type(0), start(0) {}
  109. // Ids must be unique within each type
  110. uint32_t id : 27;
  111. // Type will be used to connect flows between profilers, as
  112. // well as look up flow information (name etc)
  113. uint32_t type : 4;
  114. uint32_t start : 1;
  115. } flow;
  116. const ITraceActivity* linked{nullptr};
  117. private:
  118. const TraceSpan* traceSpan_;
  119. // Metadata map: { key: (value, quoted)}
  120. std::unordered_map<std::string, std::pair<std::string, bool>> metadataMap_;
  121. };
  122. } // namespace libkineto