ITraceActivity.h 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. #if !defined(TORCH_STABLE_ONLY) && !defined(TORCH_TARGET_VERSION)
  2. /*
  3. * Copyright (c) Meta Platforms, Inc. and affiliates.
  4. * All rights reserved.
  5. *
  6. * This source code is licensed under the BSD-style license found in the
  7. * LICENSE file in the root directory of this source tree.
  8. */
  9. #pragma once
  10. #include <string>
  11. #include "ActivityType.h"
  12. namespace libkineto {
  13. class ActivityLogger;
  14. struct TraceSpan;
  15. // Generic activity interface is borrowed from tensorboard protobuf format.
  16. struct ITraceActivity {
  17. virtual ~ITraceActivity() = default;
  18. // Device is a physical or logical entity, e.g. CPU, GPU or process
  19. [[nodiscard]] virtual int64_t deviceId() const = 0;
  20. // A resource is something on the device, h/w thread,
  21. // functional units etc.
  22. [[nodiscard]] virtual int64_t resourceId() const = 0;
  23. // s/w thread
  24. [[nodiscard]] virtual int32_t getThreadId() const = 0;
  25. // Start timestamp in nanoseconds
  26. [[nodiscard]] virtual int64_t timestamp() const = 0;
  27. // Duration in nanoseconds
  28. [[nodiscard]] virtual int64_t duration() const = 0;
  29. // Used to link up async activities
  30. [[nodiscard]] virtual int64_t correlationId() const = 0;
  31. // Part of a flow, identified by flow id and type
  32. [[nodiscard]] virtual int flowType() const = 0;
  33. [[nodiscard]] virtual int64_t flowId() const = 0;
  34. [[nodiscard]] virtual bool flowStart() const = 0;
  35. [[nodiscard]] virtual ActivityType type() const = 0;
  36. [[nodiscard]] virtual const std::string name() const = 0;
  37. // Optional linked activity
  38. [[nodiscard]] virtual const ITraceActivity* linkedActivity() const = 0;
  39. // Optional containing trace object
  40. [[nodiscard]] virtual const TraceSpan* traceSpan() const = 0;
  41. // Log activity
  42. virtual void log(ActivityLogger& logger) const = 0;
  43. // Return json formatted metadata
  44. // FIXME: Return iterator to dynamic type map here instead
  45. [[nodiscard]] virtual const std::string metadataJson() const = 0;
  46. // Return the metadata value in string format with key
  47. // @lint-ignore CLANGTIDY: clang-diagnostic-unused-parameter
  48. [[nodiscard]] virtual const std::string getMetadataValue(
  49. const std::string& key) const {
  50. return "";
  51. }
  52. static int64_t nsToUs(int64_t ns) {
  53. // It's important that this conversion is the same everywhere.
  54. // No rounding!
  55. return ns / 1000;
  56. }
  57. };
  58. } // namespace libkineto
  59. #else
  60. #error "This file should not be included when either TORCH_STABLE_ONLY or TORCH_TARGET_VERSION is defined."
  61. #endif // !defined(TORCH_STABLE_ONLY) && !defined(TORCH_TARGET_VERSION)