Gauge.h 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. #if !defined(TORCH_STABLE_ONLY) && !defined(TORCH_TARGET_VERSION)
  2. #pragma once
  3. #include <memory>
  4. #include <string_view>
  5. #include <c10/macros/Macros.h>
  6. #include <c10/util/SmallVector.h>
  7. namespace c10::monitor {
  8. namespace detail {
  9. class GaugeImpl;
  10. class GaugeBackendIf {
  11. public:
  12. virtual ~GaugeBackendIf() = default;
  13. virtual void record(int64_t value) noexcept = 0;
  14. };
  15. class GaugeBackendFactoryIf {
  16. public:
  17. virtual ~GaugeBackendFactoryIf() = default;
  18. // May return nullptr if the gauge will be ignored by the given backend.
  19. virtual std::unique_ptr<GaugeBackendIf> create(
  20. std::string_view key) noexcept = 0;
  21. };
  22. void C10_API
  23. registerGaugeBackend(std::unique_ptr<GaugeBackendFactoryIf> /*backend*/);
  24. } // namespace detail
  25. // A handle to a Gauge.
  26. class C10_API GaugeHandle {
  27. public:
  28. explicit GaugeHandle(std::string_view key);
  29. void record(int64_t value);
  30. private:
  31. // NOLINTNEXTLINE(cppcoreguidelines-avoid-const-or-ref-data-members)
  32. detail::GaugeImpl& impl_;
  33. };
  34. } // namespace c10::monitor
  35. #define STATIC_GAUGE(_key) \
  36. []() -> ::c10::monitor::GaugeHandle& { \
  37. static ::c10::monitor::GaugeHandle handle(#_key); \
  38. return handle; \
  39. }()
  40. #else
  41. #error "This file should not be included when either TORCH_STABLE_ONLY or TORCH_TARGET_VERSION is defined."
  42. #endif // !defined(TORCH_STABLE_ONLY) && !defined(TORCH_TARGET_VERSION)