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
9#pragma once
10
11#include <fmt/format.h>
12#include <string>
13#include <thread>
14#include <vector>
15
16#include "ThreadUtil.h"
17#include "ITraceActivity.h"
18#include "TraceSpan.h"
19
20namespace libkineto {
21
22// Link type, used in GenericTraceActivity.flow.type
23constexpr unsigned int kLinkFwdBwd = 1;
24constexpr unsigned int kLinkAsyncCpuGpu = 2;
25
26// @lint-ignore-every CLANGTIDY cppcoreguidelines-non-private-member-variables-in-classes
27// @lint-ignore-every CLANGTIDY cppcoreguidelines-pro-type-member-init
28class GenericTraceActivity : public ITraceActivity {
29
30 public:
31 GenericTraceActivity() : activityType(ActivityType::ENUM_COUNT), traceSpan_(NULL) {}
32
33 GenericTraceActivity(
34 const TraceSpan& trace, ActivityType type, const std::string& name)
35 : activityType(type), activityName(name), traceSpan_(&trace) {
36 }
37
38 int64_t deviceId() const override {
39 return device;
40 }
41
42 int64_t resourceId() const override {
43 return resource;
44 }
45
46 int32_t getThreadId() const override {
47 return threadId;
48 }
49
50 int64_t timestamp() const override {
51 return startTime;
52 }
53
54 int64_t duration() const override {
55 return endTime - startTime;
56 }
57
58 int64_t correlationId() const override {
59 return id;
60 }
61
62 ActivityType type() const override {
63 return activityType;
64 }
65
66 const ITraceActivity* linkedActivity() const override {
67 return linked;
68 }
69
70 int flowType() const override {
71 return flow.type;
72 }
73
74 int flowId() const override {
75 return flow.id;
76 }
77
78 bool flowStart() const override {
79 return flow.start;
80 }
81
82 const std::string name() const override {
83 return activityName;
84 }
85
86 const TraceSpan* traceSpan() const override {
87 return traceSpan_;
88 }
89
90 void log(ActivityLogger& logger) const override;
91
92 //Encode client side metadata as a key/value
93 template <typename ValType>
94 void addMetadata(const std::string& key, const ValType& value) {
95 metadata_.push_back(fmt::format("\"{}\": {}", key, value));
96 }
97
98 void addMetadataQuoted(const std::string& key, const std::string& value) {
99 metadata_.push_back(fmt::format("\"{}\": \"{}\"", key, value));
100 }
101
102 const std::string metadataJson() const override {
103 return fmt::format("{}", fmt::join(metadata_, ", "));
104 }
105
106 virtual ~GenericTraceActivity() {};
107
108 int64_t startTime{0};
109 int64_t endTime{0};
110 int32_t id{0};
111 int32_t device{0};
112 int32_t resource{0};
113 int32_t threadId{0};
114 ActivityType activityType;
115 std::string activityName;
116 struct Flow {
117 Flow(): id(0), type(0), start(0) {}
118 // Ids must be unique within each type
119 uint32_t id : 27;
120 // Type will be used to connect flows between profilers, as
121 // well as look up flow information (name etc)
122 uint32_t type : 4;
123 uint32_t start : 1;
124 } flow;
125 const ITraceActivity* linked{nullptr};
126
127 private:
128 const TraceSpan* traceSpan_;
129 std::vector<std::string> metadata_;
130};
131
132} // namespace libkineto
133