1/**
2 * Copyright (c) Glow Contributors. See CONTRIBUTORS file.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16#ifndef GLOW_RUNTIME_STATSEXPORTER_H
17#define GLOW_RUNTIME_STATSEXPORTER_H
18
19#include "llvm/ADT/StringRef.h"
20
21#include <vector>
22
23namespace glow {
24
25/// Interface for exporting runtime statistics. The base implementation
26/// delegates to any subclass registered via `registerStatsExporter`.
27class StatsExporter {
28public:
29 /// Dtor.
30 virtual ~StatsExporter() = default;
31
32 /// Add value to a time series. May be called concurrently.
33 virtual void addTimeSeriesValue(llvm::StringRef key, double value) = 0;
34
35 /// Increment a counter. May be called concurrently.
36 virtual void incrementCounter(llvm::StringRef key, int64_t value = 1) = 0;
37
38 /// Set a counter. May be called concurrently.
39 virtual void setCounter(llvm::StringRef key, int64_t value) = 0;
40};
41
42/// Registry of StatsExporters.
43class StatsExporterRegistry final {
44public:
45 /// Add value to a time series for all registered StatsExporters.
46 void addTimeSeriesValue(llvm::StringRef key, double value);
47
48 /// Increment a counter for all registered StatsExporters.
49 void incrementCounter(llvm::StringRef key, int64_t value = 1);
50
51 /// Set a counter for all registered StatsExporters.
52 void setCounter(llvm::StringRef key, int64_t value);
53
54 /// Register a StatsExporter.
55 void registerStatsExporter(StatsExporter *exporter);
56
57 /// Revoke a StatsExporter.
58 void revokeStatsExporter(StatsExporter *exporter);
59
60 /// Static singleton StatsExporter.
61 static std::shared_ptr<StatsExporterRegistry> Stats();
62
63private:
64 /// Registered StatsExporters.
65 std::vector<StatsExporter *> exporters_;
66};
67
68} // namespace glow
69
70#endif // GLOW_RUNTIME_STATSEXPORTER_H
71