ITraceActivity.h 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  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() {}
  18. // Device is a physical or logical entity, e.g. CPU, GPU or process
  19. virtual int64_t deviceId() const = 0;
  20. // A resource is something on the device, h/w thread,
  21. // functional units etc.
  22. virtual int64_t resourceId() const = 0;
  23. // s/w thread
  24. virtual int32_t getThreadId() const = 0;
  25. // Start timestamp in nanoseconds
  26. virtual int64_t timestamp() const = 0;
  27. // Duration in nanoseconds
  28. virtual int64_t duration() const = 0;
  29. // Used to link up async activities
  30. virtual int64_t correlationId() const = 0;
  31. // Part of a flow, identified by flow id and type
  32. virtual int flowType() const = 0;
  33. virtual int64_t flowId() const = 0;
  34. virtual bool flowStart() const = 0;
  35. virtual ActivityType type() const = 0;
  36. virtual const std::string name() const = 0;
  37. // Optional linked activity
  38. virtual const ITraceActivity* linkedActivity() const = 0;
  39. // Optional containing trace object
  40. 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. 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. virtual const std::string getMetadataValue(const std::string& key) const {
  49. return "";
  50. }
  51. static int64_t nsToUs(int64_t ns) {
  52. // It's important that this conversion is the same everywhere.
  53. // No rounding!
  54. return ns / 1000;
  55. }
  56. };
  57. } // namespace libkineto
  58. #else
  59. #error "This file should not be included when either TORCH_STABLE_ONLY or TORCH_TARGET_VERSION is defined."
  60. #endif // !defined(TORCH_STABLE_ONLY) && !defined(TORCH_TARGET_VERSION)