Skip to content

Commit 676135f

Browse files
author
Brian Coutinho
committed
add the device info API to trace builder
1 parent 85acbd1 commit 676135f

File tree

3 files changed

+65
-12
lines changed

3 files changed

+65
-12
lines changed

libkineto/include/KinetoDynamicPluginInterface.h

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,21 @@ struct KinetoPlugin_ProfileEventFlow {
111111
KINETO_PLUGIN_UNPADDED_STRUCT_SIZE(struct KinetoPlugin_ProfileEventFlow, \
112112
isStartPoint)
113113

114+
struct KinetoPlugin_ProfileDeviceInfo {
115+
// Always set to KINETO_PLUGIN_PROFILE_DEVICE_INFO_UNPADDED_STRUCT_SIZE
116+
size_t unpaddedStructSize;
117+
// Device ID
118+
int64_t deviceId;
119+
// Device sort index
120+
int64_t sortIndex;
121+
// Device name
122+
const char *pName;
123+
// Device label
124+
const char *pLabel;
125+
};
126+
#define KINETO_PLUGIN_PROFILE_DEVICE_INFO_UNPADDED_STRUCT_SIZE \
127+
KINETO_PLUGIN_UNPADDED_STRUCT_SIZE(struct KinetoPlugin_ProfileDeviceInfo, \
128+
pLabel)
114129
struct KinetoPlugin_ProfileResourceInfo {
115130
// Always set to KINETO_PLUGIN_PROFILE_RESOURCE_INFO_UNPADDED_STRUCT_SIZE
116131
size_t unpaddedStructSize;
@@ -161,6 +176,10 @@ struct KinetoPlugin_TraceBuilder {
161176
KinetoPlugin_TraceBuilderHandle *pTraceBuilderHandle, const char *pKey,
162177
const char *pValue);
163178

179+
int (*addDeviceInfo)(
180+
KinetoPlugin_TraceBuilderHandle *pTraceBuilderHandle,
181+
const struct KinetoPlugin_ProfileDeviceInfo *pProfileDeviceInfo);
182+
164183
int (*addResourceInfo)(
165184
KinetoPlugin_TraceBuilderHandle *pTraceBuilderHandle,
166185
const struct KinetoPlugin_ProfileResourceInfo *pProfileResourceInfo);

libkineto/src/dynamic_plugin/PluginProfiler.h

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -191,6 +191,7 @@ class PluginProfilerSession : public IActivityProfilerSession {
191191

192192
// Take ownership of trace buffer from builder
193193
traceBuffer_ = pluginTraceBuilder.getTraceBuffer();
194+
deviceInfos_ = pluginTraceBuilder.getDeviceInfos();
194195
resourceInfos_ = pluginTraceBuilder.getResourceInfos();
195196

196197
// Log events
@@ -207,7 +208,12 @@ class PluginProfilerSession : public IActivityProfilerSession {
207208
}
208209

209210
// returns device info used in this trace, could be nullptr
210-
std::unique_ptr<DeviceInfo> getDeviceInfo() override { return {}; }
211+
std::unique_ptr<DeviceInfo> getDeviceInfo() override {
212+
if (deviceInfos_.empty()) {
213+
return {};
214+
}
215+
return std::make_unique<DeviceInfo>(deviceInfos_[0]);
216+
}
211217

212218
// returns resource info used in this trace, could be empty
213219
std::vector<ResourceInfo> getResourceInfos() override {
@@ -221,6 +227,7 @@ class PluginProfilerSession : public IActivityProfilerSession {
221227

222228
private:
223229
std::unique_ptr<CpuTraceBuffer> traceBuffer_;
230+
std::vector<DeviceInfo> deviceInfos_;
224231
std::vector<ResourceInfo> resourceInfos_;
225232
const KinetoPlugin_ProfilerInterface profiler_;
226233
KinetoPlugin_ProfilerHandle *pProfilerHandle_ = nullptr;

libkineto/src/dynamic_plugin/PluginTraceBuilder.h

Lines changed: 38 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@ class PluginTraceBuilder {
2929

3030
if (pProfileEvent == nullptr) {
3131
LOG(ERROR) << "Failed to add event of nullptr";
32-
3332
return -1;
3433
}
3534

@@ -38,7 +37,6 @@ class PluginTraceBuilder {
3837
if (pProfileEvent->unpaddedStructSize <
3938
KINETO_PLUGIN_PROFILER_PROCESS_EVENTS_PARAMS_UNPADDED_STRUCT_SIZE) {
4039
LOG(ERROR) << "Profile event has an incompatible version";
41-
4240
return -1;
4341
}
4442

@@ -66,18 +64,15 @@ class PluginTraceBuilder {
6664

6765
if (pName == nullptr) {
6866
LOG(ERROR) << "Failed to set last event name of nullptr";
69-
7067
return -1;
7168
}
7269

7370
if (buffer_->activities.empty()) {
7471
LOG(ERROR) << "Failed to set last event flow as there is no last event";
75-
7672
return -1;
7773
}
7874

7975
buffer_->activities.back()->activityName.assign(pName);
80-
8176
return 0;
8277
}
8378

@@ -97,13 +92,11 @@ class PluginTraceBuilder {
9792
if (pProfileEventFlow->unpaddedStructSize <
9893
KINETO_PLUGIN_PROFILE_EVENT_FLOW_UNPADDED_STRUCT_SIZE) {
9994
LOG(ERROR) << "Profile event flow has an incompatible version";
100-
10195
return -1;
10296
}
10397

10498
if (buffer_->activities.empty()) {
10599
LOG(ERROR) << "Failed to set last event flow as there is no last event";
106-
107100
return -1;
108101
}
109102

@@ -123,19 +116,43 @@ class PluginTraceBuilder {
123116

124117
if (pKey == nullptr || pValue == nullptr) {
125118
LOG(ERROR) << "Failed to set last event metadata of nullptr";
126-
127119
return -1;
128120
}
129121

130122
if (buffer_->activities.empty()) {
131123
LOG(ERROR)
132124
<< "Failed to set last event metadata as there is no last event";
133-
134125
return -1;
135126
}
136127

137128
buffer_->activities.back()->addMetadata(std::string{pKey},
138129
std::string{pValue});
130+
return 0;
131+
}
132+
133+
int addDeviceInfo(const KinetoPlugin_ProfileDeviceInfo *pProfileDeviceInfo) {
134+
if (pProfileDeviceInfo == nullptr) {
135+
LOG(ERROR) << "Failed to add device info of nullptr";
136+
return -1;
137+
}
138+
139+
// Handle versioning
140+
// Currently expect the exact same version
141+
if (pProfileDeviceInfo->unpaddedStructSize <
142+
KINETO_PLUGIN_PROFILE_DEVICE_INFO_UNPADDED_STRUCT_SIZE) {
143+
LOG(ERROR) << "Profile device info has an incompatible version";
144+
return -1;
145+
}
146+
147+
DeviceInfo deviceInfo(
148+
pProfileDeviceInfo->deviceId, pProfileDeviceInfo->sortIndex,
149+
pProfileDeviceInfo->pName
150+
? std::string(pProfileDeviceInfo->pName)
151+
: std::to_string(pProfileDeviceInfo->deviceId),
152+
pProfileDeviceInfo->pLabel ? std::string(pProfileDeviceInfo->pLabel)
153+
: "");
154+
155+
deviceInfos_.push_back(deviceInfo);
139156

140157
return 0;
141158
}
@@ -144,7 +161,6 @@ class PluginTraceBuilder {
144161
const KinetoPlugin_ProfileResourceInfo *pProfileResourceInfo) {
145162
if (pProfileResourceInfo == nullptr) {
146163
LOG(ERROR) << "Failed to add resource info of nullptr";
147-
148164
return -1;
149165
}
150166

@@ -153,7 +169,6 @@ class PluginTraceBuilder {
153169
if (pProfileResourceInfo->unpaddedStructSize <
154170
KINETO_PLUGIN_PROFILE_RESOURCE_INFO_UNPADDED_STRUCT_SIZE) {
155171
LOG(ERROR) << "Profile resource info has an incompatible version";
156-
157172
return -1;
158173
}
159174

@@ -178,6 +193,7 @@ class PluginTraceBuilder {
178193
.setLastEventName = cSetLastEventName,
179194
.setLastEventFlow = cSetLastEventFlow,
180195
.addLastEventMetadata = cAddLastEventMetadata,
196+
.addDeviceInfo = cAddDeviceInfo,
181197
.addResourceInfo = cAddResourceInfo};
182198
}
183199

@@ -186,6 +202,8 @@ class PluginTraceBuilder {
186202
return std::move(buffer_);
187203
}
188204

205+
std::vector<DeviceInfo> getDeviceInfos() { return deviceInfos_; }
206+
189207
std::vector<ResourceInfo> getResourceInfos() { return resourceInfos_; }
190208

191209
private:
@@ -220,6 +238,14 @@ class PluginTraceBuilder {
220238
return pPluginTraceBuilder->addLastEventMetadata(pKey, pValue);
221239
}
222240

241+
static int
242+
cAddDeviceInfo(KinetoPlugin_TraceBuilderHandle *pTraceBuilderHandle,
243+
const KinetoPlugin_ProfileDeviceInfo *pProfileDeviceInfo) {
244+
auto pPluginTraceBuilder =
245+
reinterpret_cast<PluginTraceBuilder *>(pTraceBuilderHandle);
246+
return pPluginTraceBuilder->addDeviceInfo(pProfileDeviceInfo);
247+
}
248+
223249
static int cAddResourceInfo(
224250
KinetoPlugin_TraceBuilderHandle *pTraceBuilderHandle,
225251
const KinetoPlugin_ProfileResourceInfo *pProfileResourceInfo) {
@@ -229,6 +255,7 @@ class PluginTraceBuilder {
229255
}
230256

231257
std::unique_ptr<CpuTraceBuffer> buffer_;
258+
std::vector<DeviceInfo> deviceInfos_;
232259
std::vector<ResourceInfo> resourceInfos_;
233260
};
234261

0 commit comments

Comments
 (0)