ActivityProfilerInterface.h 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  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 <thread>
  12. #include <vector>
  13. #include "ActivityType.h"
  14. #include "ActivityTraceInterface.h"
  15. #include "IActivityProfiler.h"
  16. namespace libkineto {
  17. class ActivityProfilerController;
  18. struct CpuTraceBuffer;
  19. class Config;
  20. class ActivityProfilerInterface {
  21. public:
  22. virtual ~ActivityProfilerInterface() {}
  23. virtual void init() {}
  24. virtual bool isInitialized() {
  25. return false;
  26. }
  27. virtual bool isActive(){
  28. return false;
  29. }
  30. // *** Asynchronous API ***
  31. // Instead of starting and stopping the trace manually, provide a start time
  32. // and duration and / or iteration stop criterion.
  33. // Tracing terminates when either condition is met.
  34. virtual void scheduleTrace(const std::string& configStr) {}
  35. // *** Synchronous API ***
  36. // These must be called in order:
  37. // prepareTrace -> startTrace -> stopTrace.
  38. // Many tracing structures are lazily initialized during trace collection,
  39. // with potentially high overhead.
  40. // Call prepareTrace to enable tracing, then run the region to trace
  41. // at least once (and ideally run the same code that is to be traced) to
  42. // allow tracing structures to be initialized.
  43. virtual void prepareTrace(
  44. const std::set<ActivityType>& activityTypes,
  45. const std::string& configStr = "") {}
  46. // Start recording, potentially reusing any buffers allocated since
  47. // prepareTrace was called.
  48. virtual void startTrace() {}
  49. // Stop and process trace, producing an in-memory list of trace records.
  50. // The processing will be done synchronously (using the calling thread.)
  51. virtual std::unique_ptr<ActivityTraceInterface> stopTrace() {
  52. return nullptr;
  53. }
  54. // Re-evaluate internal state to allow for triggering operations based
  55. // on number of iteration. each implicitly increments the iteration count
  56. virtual void step() {}
  57. // *** TraceActivity API ***
  58. // FIXME: Pass activityProfiler interface into clientInterface?
  59. virtual void pushCorrelationId(uint64_t id){}
  60. virtual void popCorrelationId(){}
  61. virtual void transferCpuTrace(
  62. std::unique_ptr<CpuTraceBuffer> traceBuffer){}
  63. // Correlation ids for user defined spans
  64. virtual void pushUserCorrelationId(uint64_t){}
  65. virtual void popUserCorrelationId(){}
  66. // Saves information for the current thread to be used in profiler output
  67. // Client must record any new kernel thread where the activity has occured.
  68. virtual void recordThreadInfo() {}
  69. // Record trace metadata, currently supporting only string key and values,
  70. // values with the same key are overwritten
  71. virtual void addMetadata(const std::string& key, const std::string& value) = 0;
  72. // Add a child activity profiler, this enables frameworks in the application
  73. // to enable custom framework events.
  74. virtual void addChildActivityProfiler(
  75. std::unique_ptr<IActivityProfiler> profiler) {}
  76. // Log Invariant Violation to factories enabled. This helps record
  77. // instances when the profiler behaves unexpectedly.
  78. virtual void logInvariantViolation(
  79. const std::string&,
  80. const std::string&,
  81. const std::string&,
  82. const std::string& = "") {}
  83. };
  84. } // namespace libkineto