You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
55 lines
1.5 KiB
55 lines
1.5 KiB
|
6 days ago
|
#if !defined(TORCH_STABLE_ONLY) && !defined(TORCH_TARGET_VERSION)
|
||
|
|
#pragma once
|
||
|
|
|
||
|
|
#include <functional>
|
||
|
|
#include <memory>
|
||
|
|
#include <string_view>
|
||
|
|
|
||
|
|
#include <c10/macros/Macros.h>
|
||
|
|
|
||
|
|
namespace c10::monitor {
|
||
|
|
|
||
|
|
class C10_API DynamicCounter {
|
||
|
|
public:
|
||
|
|
using Callback = std::function<int64_t()>;
|
||
|
|
|
||
|
|
// Creates a dynamic counter that can be queried at any point in time by
|
||
|
|
// multiple backends. Only one counter with a given key can exist at any point
|
||
|
|
// in time.
|
||
|
|
//
|
||
|
|
// The callback is invoked every time the counter is queried.
|
||
|
|
// The callback must be thread-safe.
|
||
|
|
// The callback must not throw.
|
||
|
|
// The callback must not block.
|
||
|
|
DynamicCounter(std::string_view key, Callback getCounterCallback);
|
||
|
|
|
||
|
|
// Unregisters the callback.
|
||
|
|
// Waits for all ongoing callback invocations to finish.
|
||
|
|
~DynamicCounter();
|
||
|
|
|
||
|
|
private:
|
||
|
|
struct Guard;
|
||
|
|
std::unique_ptr<Guard> guard_;
|
||
|
|
};
|
||
|
|
|
||
|
|
namespace detail {
|
||
|
|
class DynamicCounterBackendIf {
|
||
|
|
public:
|
||
|
|
virtual ~DynamicCounterBackendIf() = default;
|
||
|
|
|
||
|
|
virtual void registerCounter(
|
||
|
|
std::string_view key,
|
||
|
|
DynamicCounter::Callback getCounterCallback) = 0;
|
||
|
|
// MUST wait for all ongoing callback invocations to finish
|
||
|
|
virtual void unregisterCounter(std::string_view key) = 0;
|
||
|
|
};
|
||
|
|
|
||
|
|
void C10_API registerDynamicCounterBackend(
|
||
|
|
std::unique_ptr<DynamicCounterBackendIf> /*backend*/);
|
||
|
|
} // namespace detail
|
||
|
|
} // namespace c10::monitor
|
||
|
|
|
||
|
|
#else
|
||
|
|
#error "This file should not be included when either TORCH_STABLE_ONLY or TORCH_TARGET_VERSION is defined."
|
||
|
|
#endif // !defined(TORCH_STABLE_ONLY) && !defined(TORCH_TARGET_VERSION)
|