IActivityProfiler.h 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  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 <memory>
  10. #include <set>
  11. #include <vector>
  12. #include "Config.h"
  13. #include "GenericTraceActivity.h"
  14. /* This file includes an abstract base class for an activity profiler
  15. * that can be implemented by multiple tracing agents in the application.
  16. * The high level Kineto profiler can co-ordinate start and end of tracing
  17. * and combine together events from multiple such activity profilers.
  18. */
  19. namespace libkineto {
  20. struct CpuTraceBuffer;
  21. #ifdef _MSC_VER
  22. // workaround for the predefined ERROR macro on Windows
  23. #undef ERROR
  24. #endif // _MSC_VER
  25. enum class TraceStatus {
  26. READY, // Accepting trace requests
  27. WARMUP, // Performing trace warmup
  28. RECORDING, // Actively collecting activities
  29. PROCESSING, // Recording is complete, preparing results
  30. ERROR, // One or more errors (and possibly also warnings) occurred.
  31. WARNING, // One or more warnings occurred.
  32. };
  33. /* DeviceInfo:
  34. * Can be used to specify process name, PID and device label
  35. */
  36. struct DeviceInfo {
  37. DeviceInfo(int64_t id, const std::string& name, const std::string& label)
  38. : id(id), name(name), label(label) {}
  39. int64_t id; // process id
  40. const std::string name; // process name
  41. const std::string label; // device label
  42. };
  43. /* ResourceInfo:
  44. * Can be used to specify resource inside device
  45. */
  46. struct ResourceInfo {
  47. ResourceInfo(
  48. int64_t deviceId,
  49. int64_t id,
  50. int64_t sortIndex,
  51. const std::string& name)
  52. : id(id), sortIndex(sortIndex), deviceId(deviceId), name(name) {}
  53. int64_t id; // resource id
  54. int64_t sortIndex; // position in trace view
  55. int64_t deviceId; // id of device which owns this resource (specified in DeviceInfo.id)
  56. const std::string name; // resource name
  57. };
  58. using getLinkedActivityCallback =
  59. std::function<const ITraceActivity*(int32_t)>;
  60. /* IActivityProfilerSession:
  61. * an opaque object that can be used by a high level profiler to
  62. * start/stop and return trace events.
  63. */
  64. class IActivityProfilerSession {
  65. public:
  66. virtual ~IActivityProfilerSession() {}
  67. // start the trace collection synchronously
  68. virtual void start() = 0;
  69. // stop the trace collection synchronously
  70. virtual void stop() = 0;
  71. TraceStatus status() {
  72. return status_;
  73. }
  74. // returns errors with this trace
  75. virtual std::vector<std::string> errors() = 0;
  76. // processes trace activities using logger
  77. virtual void processTrace(ActivityLogger& logger) = 0;
  78. virtual void processTrace(ActivityLogger& logger,
  79. getLinkedActivityCallback /*getLinkedActivity*/,
  80. int64_t /*startTime*/, int64_t /*endTime*/) {
  81. processTrace(logger);
  82. }
  83. // returns device info used in this trace, could be nullptr
  84. virtual std::unique_ptr<DeviceInfo> getDeviceInfo() = 0;
  85. // returns resource info used in this trace, could be empty
  86. virtual std::vector<ResourceInfo> getResourceInfos() = 0;
  87. // release ownership of the trace events and metadata
  88. virtual std::unique_ptr<CpuTraceBuffer> getTraceBuffer() = 0;
  89. // XXX define trace formats
  90. // virtual save(string name, TraceFormat format)
  91. virtual void pushCorrelationId(uint64_t /*id*/) {}
  92. virtual void popCorrelationId() {}
  93. virtual void pushUserCorrelationId(uint64_t /*id*/) {}
  94. virtual void popUserCorrelationId() {}
  95. protected:
  96. TraceStatus status_ = TraceStatus::READY;
  97. };
  98. /* Activity Profiler Plugins:
  99. * These allow other frameworks to integrate into Kineto's primariy
  100. * activity profiler. While the primary activity profiler handles
  101. * timing the trace collections and correlating events the plugins
  102. * can become source of new trace activity types.
  103. */
  104. class IActivityProfiler {
  105. public:
  106. virtual ~IActivityProfiler() {}
  107. // name of profiler
  108. virtual const std::string& name() const = 0;
  109. // returns activity types this profiler supports
  110. virtual const std::set<ActivityType>& availableActivities() const = 0;
  111. // Calls prepare() on registered tracer providers passing in the relevant
  112. // activity types. Returns a profiler session handle
  113. virtual std::unique_ptr<IActivityProfilerSession> configure(
  114. const std::set<ActivityType>& activity_types,
  115. const Config& config) = 0;
  116. // asynchronous version of the above with future timestamp and duration.
  117. virtual std::unique_ptr<IActivityProfilerSession> configure(
  118. int64_t ts_ms,
  119. int64_t duration_ms,
  120. const std::set<ActivityType>& activity_types,
  121. const Config& config) = 0;
  122. };
  123. } // namespace libkineto