Skip to content

Commit e9d5cf1

Browse files
author
Brian Coutinho
committed
address comments, add pushpop corr interface, better test, complete enums
1 parent 96de1ec commit e9d5cf1

File tree

4 files changed

+344
-10
lines changed

4 files changed

+344
-10
lines changed

libkineto/include/KinetoDynamicPluginInterface.h

Lines changed: 94 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
1-
#ifndef KINETO_PLUGIN_FORMAT_H
2-
#define KINETO_PLUGIN_FORMAT_H
1+
#ifndef KINETO_DYNAMIC_PLUGIN_FORMAT_H
2+
#define KINETO_DYNAMIC_PLUGIN_FORMAT_H
33

44
#include <stdint.h>
55

66
#define KINETO_PLUGIN_LIB_DIR_PATH_ENV_VARIABLE "KINETO_PLUGIN_LIB_DIR_PATH"
77

8-
// This file uses unpadded struct size for version control
8+
// NOTE: This file uses unpadded struct size for version control
99
// To support backward compatibility, do NOT remove, reorder, or resize the
1010
// existing fields For details, visit
1111
// https://github.com/annarev/community/commit/09e231324c3b2b1f653c3f385fd2120edd460815
@@ -155,7 +155,7 @@ struct KinetoPlugin_TraceBuilder {
155155
const struct KinetoPlugin_ProfileEventFlow *pProfileEventFlow);
156156
// If metadata value is a string itself (e.g., "metadata value"), MUST add
157157
// additional quote around it (e.g., "\"metadata value\"") If metadata value
158-
// is a list (e.g., 1, 2, 3), MUST add squre bracket around it (e.g., "[1, 2,
158+
// is a list (e.g., 1, 2, 3), MUST add square bracket around it (e.g., "[1, 2,
159159
// 3]")
160160
int (*addLastEventMetadata)(
161161
KinetoPlugin_TraceBuilderHandle *pTraceBuilderHandle, const char *pKey,
@@ -214,7 +214,7 @@ struct KinetoPlugin_ProfilerQuery_Params {
214214
};
215215
#define KINETO_PLUGIN_PROFILER_QUERY_PARAMS_UNPADDED_STRUCT_SIZE \
216216
KINETO_PLUGIN_UNPADDED_STRUCT_SIZE(struct KinetoPlugin_ProfilerQuery_Params, \
217-
profilerNameMaxLen)
217+
supportedActivityTypesMaxLen)
218218

219219
struct KinetoPlugin_ProfilerStart_Params {
220220
// Always set to KINETO_PLUGIN_PROFILER_START_PARAMS_UNPADDED_STRUCT_SIZE
@@ -253,25 +253,113 @@ struct KinetoPlugin_ProfilerProcessEvents_Params {
253253
KINETO_PLUGIN_UNPADDED_STRUCT_SIZE( \
254254
struct KinetoPlugin_ProfilerProcessEvents_Params, pTraceBuilder)
255255

256+
struct KinetoPlugin_ProfilerPushCorrelationId_Params {
257+
// Always set to
258+
// KINETO_PLUGIN_PROFILER_PUSH_CORRELATION_ID_PARAMS_UNPADDED_STRUCT_SIZE
259+
size_t unpaddedStructSize;
260+
261+
// [in] Correlation ID
262+
uint64_t correlationId;
263+
264+
// [in] An instance created via profilerCreate()
265+
KinetoPlugin_ProfilerHandle *pProfilerHandle;
266+
};
267+
#define KINETO_PLUGIN_PROFILER_PUSH_CORRELATION_ID_PARAMS_UNPADDED_STRUCT_SIZE \
268+
KINETO_PLUGIN_UNPADDED_STRUCT_SIZE( \
269+
struct KinetoPlugin_ProfilerPushCorrelationId_Params, pProfilerHandle)
270+
271+
struct KinetoPlugin_ProfilerPopCorrelationId_Params {
272+
// Always set to
273+
// KINETO_PLUGIN_PROFILER_POP_CORRELATION_ID_PARAMS_UNPADDED_STRUCT_SIZE
274+
size_t unpaddedStructSize;
275+
276+
// [in] An instance created via profilerCreate()
277+
KinetoPlugin_ProfilerHandle *pProfilerHandle;
278+
};
279+
#define KINETO_PLUGIN_PROFILER_POP_CORRELATION_ID_PARAMS_UNPADDED_STRUCT_SIZE \
280+
KINETO_PLUGIN_UNPADDED_STRUCT_SIZE( \
281+
struct KinetoPlugin_ProfilerPopCorrelationId_Params, pProfilerHandle)
282+
283+
struct KinetoPlugin_ProfilerPushUserCorrelationId_Params {
284+
// Always set to
285+
// KINETO_PLUGIN_PROFILER_PUSH_USER_CORRELATION_ID_PARAMS_UNPADDED_STRUCT_SIZE
286+
size_t unpaddedStructSize;
287+
288+
// [in] User correlation ID
289+
uint64_t userCorrelationId;
290+
291+
// [in] An instance created via profilerCreate()
292+
KinetoPlugin_ProfilerHandle *pProfilerHandle;
293+
};
294+
#define KINETO_PLUGIN_PROFILER_PUSH_USER_CORRELATION_ID_PARAMS_UNPADDED_STRUCT_SIZE \
295+
KINETO_PLUGIN_UNPADDED_STRUCT_SIZE( \
296+
struct KinetoPlugin_ProfilerPushUserCorrelationId_Params, \
297+
pProfilerHandle)
298+
299+
struct KinetoPlugin_ProfilerPopUserCorrelationId_Params {
300+
// Always set to
301+
// KINETO_PLUGIN_PROFILER_POP_USER_CORRELATION_ID_PARAMS_UNPADDED_STRUCT_SIZE
302+
size_t unpaddedStructSize;
303+
304+
// [in] An instance created via profilerCreate()
305+
KinetoPlugin_ProfilerHandle *pProfilerHandle;
306+
};
307+
#define KINETO_PLUGIN_PROFILER_POP_USER_CORRELATION_ID_PARAMS_UNPADDED_STRUCT_SIZE \
308+
KINETO_PLUGIN_UNPADDED_STRUCT_SIZE( \
309+
struct KinetoPlugin_ProfilerPopUserCorrelationId_Params, \
310+
pProfilerHandle)
311+
256312
struct KinetoPlugin_ProfilerInterface {
257313
// Always set to KINETO_PLUGIN_PROFILER_INTERFACE_UNPADDED_STRUCT_SIZE
258314
size_t unpaddedStructSize;
259315

316+
// Create an instance of the profiler session
260317
int (*profilerCreate)(
261318
struct KinetoPlugin_ProfilerCreate_Params *pProfilerCreateParams);
262319

320+
// Destroy an instance of the profiler session
263321
int (*profilerDestroy)(
264322
struct KinetoPlugin_ProfilerDestroy_Params *pProfilerDestroyParams);
265323

324+
// Query additional information about the profile such as its name, supported
325+
// activity types, etc.
266326
int (*profilerQuery)(
267327
struct KinetoPlugin_ProfilerQuery_Params *pProfilerQueryParams);
268328

329+
// Start the trace collection
269330
int (*profilerStart)(
270331
struct KinetoPlugin_ProfilerStart_Params *pProfilerStartParams);
271332

333+
// Stop the trace collection
272334
int (*profilerStop)(
273335
struct KinetoPlugin_ProfilerStop_Params *pProfilerStopParams);
274336

337+
// The following interfaces are optional and can be used to track correlation
338+
// IDs If not implemented, the correlation IDs will not be tracked through the
339+
// profiler session.
340+
341+
// Push a correlation ID to the profiler session
342+
int (*profilerPushCorrelationId)(
343+
struct KinetoPlugin_ProfilerPushCorrelationId_Params
344+
*pProfilerPushCorrelationIdParams);
345+
346+
// Pop a correlation ID from the profiler session
347+
int (*profilerPopCorrelationId)(
348+
struct KinetoPlugin_ProfilerPopCorrelationId_Params
349+
*pProfilerPopCorrelationIdParams);
350+
351+
// Push a user correlation ID to the profiler session
352+
int (*profilerPushUserCorrelationId)(
353+
struct KinetoPlugin_ProfilerPushUserCorrelationId_Params
354+
*pProfilerPushUserCorrelationIdParams);
355+
356+
// Pop a user correlation ID from the profiler session
357+
int (*profilerPopUserCorrelationId)(
358+
struct KinetoPlugin_ProfilerPopUserCorrelationId_Params
359+
*pProfilerPopUserCorrelationIdParams);
360+
361+
// Process the events collected by the profiler session to the central
362+
// profiler. This leverages the trace builder to enqueue events.
275363
int (*profilerProcessEvents)(struct KinetoPlugin_ProfilerProcessEvents_Params
276364
*pProfilerProcessEventsParams);
277365
};
@@ -304,4 +392,4 @@ int KinetoPlugin_register(const struct KinetoPlugin_Registry *pRegistry);
304392
}
305393
#endif
306394

307-
#endif
395+
#endif // KINETO_DYNAMIC_PLUGIN_FORMAT_H

libkineto/src/dynamic_plugin/PluginProfiler.h

Lines changed: 188 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,63 @@ class PluginProfilerSession : public IActivityProfilerSession {
9595
}
9696
}
9797

98+
void pushCorrelationId(uint64_t id) override {
99+
KinetoPlugin_ProfilerPushCorrelationId_Params pushCorrelationIdParams{
100+
KINETO_PLUGIN_PROFILER_PUSH_CORRELATION_ID_PARAMS_UNPADDED_STRUCT_SIZE};
101+
pushCorrelationIdParams.pProfilerHandle = pProfilerHandle_;
102+
pushCorrelationIdParams.correlationId = id;
103+
104+
int errorCode =
105+
profiler_.profilerPushCorrelationId(&pushCorrelationIdParams);
106+
if (errorCode != 0) {
107+
LOG(ERROR) << "Plugin profiler " << name_
108+
<< " failed at profilerPushCorrelationId() with error "
109+
<< errorCode;
110+
}
111+
}
112+
113+
void popCorrelationId() override {
114+
KinetoPlugin_ProfilerPopCorrelationId_Params popCorrelationIdParams{
115+
KINETO_PLUGIN_PROFILER_POP_CORRELATION_ID_PARAMS_UNPADDED_STRUCT_SIZE};
116+
popCorrelationIdParams.pProfilerHandle = pProfilerHandle_;
117+
118+
int errorCode = profiler_.profilerPopCorrelationId(&popCorrelationIdParams);
119+
if (errorCode != 0) {
120+
LOG(ERROR) << "Plugin profiler " << name_
121+
<< " failed at profilerPopCorrelationId() with error "
122+
<< errorCode;
123+
}
124+
}
125+
126+
void pushUserCorrelationId(uint64_t id) override {
127+
KinetoPlugin_ProfilerPushUserCorrelationId_Params pushUserCorrelationIdParams{
128+
KINETO_PLUGIN_PROFILER_PUSH_USER_CORRELATION_ID_PARAMS_UNPADDED_STRUCT_SIZE};
129+
pushUserCorrelationIdParams.pProfilerHandle = pProfilerHandle_;
130+
pushUserCorrelationIdParams.userCorrelationId = id;
131+
132+
int errorCode =
133+
profiler_.profilerPushUserCorrelationId(&pushUserCorrelationIdParams);
134+
if (errorCode != 0) {
135+
LOG(ERROR) << "Plugin profiler " << name_
136+
<< " failed at profilerPushUserCorrelationId() with error "
137+
<< errorCode;
138+
}
139+
}
140+
141+
void popUserCorrelationId() override {
142+
KinetoPlugin_ProfilerPopUserCorrelationId_Params popUserCorrelationIdParams{
143+
KINETO_PLUGIN_PROFILER_POP_USER_CORRELATION_ID_PARAMS_UNPADDED_STRUCT_SIZE};
144+
popUserCorrelationIdParams.pProfilerHandle = pProfilerHandle_;
145+
146+
int errorCode =
147+
profiler_.profilerPopUserCorrelationId(&popUserCorrelationIdParams);
148+
if (errorCode != 0) {
149+
LOG(ERROR) << "Plugin profiler " << name_
150+
<< " failed at profilerPopUserCorrelationId() with error "
151+
<< errorCode;
152+
}
153+
}
154+
98155
TraceStatus status() { return status_; }
99156

100157
// returns errors with this trace
@@ -169,8 +226,13 @@ class PluginProfiler : public IActivityProfiler {
169226

170227
public:
171228
PluginProfiler(const KinetoPlugin_ProfilerInterface &profiler)
172-
: profiler_(profiler) {
173-
validateProfiler();
229+
: profiler_(profiler), isValid_(true) {
230+
isValid_ = validateProfiler();
231+
if (!isValid_) {
232+
LOG(ERROR) << "Plugin profiler " << name_
233+
<< " is not valid; skipping registration";
234+
return;
235+
}
174236

175237
char profilerName[32];
176238

@@ -216,6 +278,13 @@ class PluginProfiler : public IActivityProfiler {
216278
std::unique_ptr<IActivityProfilerSession>
217279
configure(const std::set<ActivityType> &activity_types,
218280
const Config & /*config*/) override {
281+
// Check if profiler is valid
282+
if (!isValid_) {
283+
LOG(ERROR) << "Plugin profiler " << name_
284+
<< " is not valid, cannot configure";
285+
return nullptr;
286+
}
287+
219288
// Check if the plugin supports ANY of the requested activity types
220289
bool hasSupported = false;
221290
for (const auto &activity_type : activity_types) {
@@ -245,7 +314,9 @@ class PluginProfiler : public IActivityProfiler {
245314
}
246315

247316
private:
248-
void validateProfiler() {
317+
bool validateProfiler() {
318+
bool isValid = true;
319+
249320
// Handle versioning
250321
// Currently expect the exact same version
251322
if (profiler_.unpaddedStructSize <
@@ -265,14 +336,128 @@ class PluginProfiler : public IActivityProfiler {
265336
profiler_.profilerStop = [](struct KinetoPlugin_ProfilerStop_Params *) {
266337
return -1;
267338
};
339+
profiler_.profilerPushCorrelationId =
340+
[](struct KinetoPlugin_ProfilerPushCorrelationId_Params *) {
341+
return -1;
342+
};
343+
profiler_.profilerPopCorrelationId =
344+
[](struct KinetoPlugin_ProfilerPopCorrelationId_Params *) {
345+
return -1;
346+
};
347+
profiler_.profilerPushUserCorrelationId =
348+
[](struct KinetoPlugin_ProfilerPushUserCorrelationId_Params *) {
349+
return -1;
350+
};
351+
profiler_.profilerPopUserCorrelationId =
352+
[](struct KinetoPlugin_ProfilerPopUserCorrelationId_Params *) {
353+
return -1;
354+
};
268355
profiler_.profilerProcessEvents =
269356
[](struct KinetoPlugin_ProfilerProcessEvents_Params *) { return -1; };
357+
return false;
358+
}
359+
360+
// Check if individual function pointers are implemented
361+
// For critical functions, set them to error-returning stubs if nullptr and
362+
// mark as invalid
363+
if (profiler_.profilerCreate == nullptr) {
364+
LOG(ERROR) << "Plugin profiler profilerCreate is not implemented";
365+
profiler_.profilerCreate =
366+
[](struct KinetoPlugin_ProfilerCreate_Params *) { return -1; };
367+
isValid = false;
368+
}
369+
370+
if (profiler_.profilerDestroy == nullptr) {
371+
LOG(ERROR) << "Plugin profiler profilerDestroy is not implemented";
372+
profiler_.profilerDestroy =
373+
[](struct KinetoPlugin_ProfilerDestroy_Params *) { return -1; };
374+
isValid = false;
375+
}
376+
377+
if (profiler_.profilerQuery == nullptr) {
378+
LOG(ERROR) << "Plugin profiler profilerQuery is not implemented";
379+
profiler_.profilerQuery = [](struct KinetoPlugin_ProfilerQuery_Params *) {
380+
return -1;
381+
};
382+
isValid = false;
383+
}
384+
385+
if (profiler_.profilerStart == nullptr) {
386+
LOG(ERROR) << "Plugin profiler profilerStart is not implemented";
387+
profiler_.profilerStart = [](struct KinetoPlugin_ProfilerStart_Params *) {
388+
return -1;
389+
};
390+
isValid = false;
270391
}
392+
393+
if (profiler_.profilerStop == nullptr) {
394+
LOG(ERROR) << "Plugin profiler profilerStop is not implemented";
395+
profiler_.profilerStop = [](struct KinetoPlugin_ProfilerStop_Params *) {
396+
return -1;
397+
};
398+
isValid = false;
399+
}
400+
401+
if (profiler_.profilerProcessEvents == nullptr) {
402+
LOG(ERROR) << "Plugin profiler profilerProcessEvents is not implemented";
403+
profiler_.profilerProcessEvents =
404+
[](struct KinetoPlugin_ProfilerProcessEvents_Params *) { return -1; };
405+
isValid = false;
406+
}
407+
408+
// For correlation functions, provide default warning implementations if not
409+
// provided These don't affect validity
410+
if (profiler_.profilerPushCorrelationId == nullptr) {
411+
LOG(INFO) << "Plugin profiler profilerPushCorrelationId is not "
412+
"implemented, using default stub";
413+
profiler_.profilerPushCorrelationId =
414+
[](struct KinetoPlugin_ProfilerPushCorrelationId_Params *) {
415+
LOG(WARNING) << "profilerPushCorrelationId called but not "
416+
"implemented by plugin";
417+
return 0;
418+
};
419+
}
420+
421+
if (profiler_.profilerPopCorrelationId == nullptr) {
422+
LOG(INFO) << "Plugin profiler profilerPopCorrelationId is not "
423+
"implemented, using default stub";
424+
profiler_.profilerPopCorrelationId =
425+
[](struct KinetoPlugin_ProfilerPopCorrelationId_Params *) {
426+
LOG(WARNING) << "profilerPopCorrelationId called but not "
427+
"implemented by plugin";
428+
return 0;
429+
};
430+
}
431+
432+
if (profiler_.profilerPushUserCorrelationId == nullptr) {
433+
LOG(INFO) << "Plugin profiler profilerPushUserCorrelationId is not "
434+
"implemented, using default stub";
435+
profiler_.profilerPushUserCorrelationId =
436+
[](struct KinetoPlugin_ProfilerPushUserCorrelationId_Params *) {
437+
LOG(WARNING) << "profilerPushUserCorrelationId called but not "
438+
"implemented by plugin";
439+
return 0;
440+
};
441+
}
442+
443+
if (profiler_.profilerPopUserCorrelationId == nullptr) {
444+
LOG(INFO) << "Plugin profiler profilerPopUserCorrelationId is not "
445+
"implemented, using default stub";
446+
profiler_.profilerPopUserCorrelationId =
447+
[](struct KinetoPlugin_ProfilerPopUserCorrelationId_Params *) {
448+
LOG(WARNING) << "profilerPopUserCorrelationId called but not "
449+
"implemented by plugin";
450+
return 0;
451+
};
452+
}
453+
454+
return isValid;
271455
}
272456

273457
KinetoPlugin_ProfilerInterface profiler_;
274458
std::string name_;
275459
std::set<ActivityType> supportedActivities_;
460+
bool isValid_;
276461
};
277462

278463
} // namespace libkineto

0 commit comments

Comments
 (0)