LLVM 22.0.0git
Telemetry.h
Go to the documentation of this file.
1//===----------------------------------------------------------------------===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8///
9/// \file
10/// This file provides the basic framework for Telemetry.
11/// Refer to its documentation at llvm/docs/Telemetry.rst for more details.
12//===---------------------------------------------------------------------===//
13
14#ifndef LLVM_TELEMETRY_TELEMETRY_H
15#define LLVM_TELEMETRY_TELEMETRY_H
16
17#include "llvm/ADT/DenseMap.h"
19#include "llvm/ADT/StringRef.h"
21#include "llvm/Support/Error.h"
22#include <memory>
23#include <optional>
24#include <string>
25#include <type_traits>
26#include <vector>
27
28namespace llvm {
29namespace telemetry {
30
32public:
33 virtual ~Serializer() = default;
34
35 virtual Error init() = 0;
36 virtual void write(StringRef KeyName, bool Value) = 0;
37 virtual void write(StringRef KeyName, StringRef Value) = 0;
38 virtual void write(StringRef KeyName, int Value) = 0;
39 virtual void write(StringRef KeyName, long Value) = 0;
40 virtual void write(StringRef KeyName, long long Value) = 0;
41 virtual void write(StringRef KeyName, unsigned int Value) = 0;
42 virtual void write(StringRef KeyName, unsigned long Value) = 0;
43 virtual void write(StringRef KeyName, unsigned long long Value) = 0;
44 virtual void beginObject(StringRef KeyName) = 0;
45 virtual void endObject() = 0;
46 virtual Error finalize() = 0;
47
48 template <typename T, typename = typename T::mapped_type>
49 void write(StringRef KeyName, const T &Map) {
50 static_assert(std::is_convertible_v<typename T::key_type, StringRef>,
51 "KeyType must be convertible to string");
52 beginObject(KeyName);
53 for (const auto &KeyVal : Map)
54 write(KeyVal.first, KeyVal.second);
55 endObject();
56 }
57};
58
59/// Configuration for the Manager class.
60/// This stores configurations from both users and vendors and is passed
61/// to the Manager upon construction. (Any changes to the config after
62/// the Manager's construction will not have any effect on it).
63///
64/// This struct can be extended as needed to add additional configuration
65/// points specific to a vendor's implementation.
66struct Config {
67 static constexpr bool BuildTimeEnableTelemetry = LLVM_ENABLE_TELEMETRY;
68
69 // If true, telemetry will be enabled.
70 const bool EnableTelemetry;
71
73
74 virtual ~Config() = default;
75
76 // Telemetry can only be enabled if both the runtime and buildtime flag
77 // are set.
79
80 virtual std::optional<std::string> makeSessionId() { return std::nullopt; }
81};
82
83/// For isa, dyn_cast, etc operations on TelemetryInfo.
84typedef unsigned KindType;
85/// This struct is used by TelemetryInfo to support isa<>, dyn_cast<>
86/// operations.
87/// It is defined as a struct (rather than an enum) because it is
88/// expected to be extended by subclasses which may have
89/// additional TelemetryInfo types defined to describe different events.
90struct EntryKind {
91 static const KindType Base = 0;
92};
93
94/// TelemetryInfo is the data courier, used to move instrumented data
95/// from the tool being monitored to the Telemetry framework.
96///
97/// This base class contains only the basic set of telemetry data.
98/// Downstream implementations can define more subclasses with
99/// additional fields to describe different events and concepts.
100///
101/// For example, The LLDB debugger can define a DebugCommandInfo subclass
102/// which has additional fields about the debug-command being instrumented,
103/// such as `CommandArguments` or `CommandName`.
105 // This represents a unique-id, conventionally corresponding to
106 // a tool's session - i.e., every time the tool starts until it exits.
107 //
108 // Note: a tool could have multiple sessions running at once, in which
109 // case, these shall be multiple sets of TelemetryInfo with multiple unique
110 // IDs.
111 //
112 // Different usages can assign different types of IDs to this field.
113 std::string SessionId;
114
115 TelemetryInfo() = default;
116 virtual ~TelemetryInfo() = default;
117
118 virtual void serialize(Serializer &serializer) const;
119
120 // For isa, dyn_cast, etc, operations.
121 virtual KindType getKind() const { return EntryKind::Base; }
122 static bool classof(const TelemetryInfo *T) {
123 return T->getKind() == EntryKind::Base;
124 }
125};
126
127/// This class presents a data sink to which the Telemetry framework
128/// sends data.
129///
130/// Its implementation is transparent to the framework.
131/// It is up to the vendor to decide which pieces of data to forward
132/// and where to forward them.
134public:
135 virtual ~Destination() = default;
136 virtual Error receiveEntry(const TelemetryInfo *Entry) = 0;
137 virtual StringLiteral name() const = 0;
138};
139
140/// This class is the main interaction point between any LLVM tool
141/// and this framework.
142/// It is responsible for collecting telemetry data from the tool being
143/// monitored and transmitting the data elsewhere.
145public:
146 Manager() = default;
147 virtual ~Manager() = default;
148
149 // Explicitly non-copyable.
150 Manager(Manager const &) = delete;
151 Manager &operator=(Manager const &) = delete;
152
153 // Dispatch Telemetry data to the Destination(s).
154 // The argument is non-const because the Manager may add or remove
155 // data from the entry.
156 virtual Error dispatch(TelemetryInfo *Entry);
157
158 // Register a Destination.
159 void addDestination(std::unique_ptr<Destination> Destination);
160
161protected:
162 // Optional callback for subclasses to perform additional tasks before
163 // dispatching to Destinations.
164 virtual Error preDispatch(TelemetryInfo *Entry);
165
166private:
167 std::vector<std::unique_ptr<Destination>> Destinations;
168};
169
170} // namespace telemetry
171} // namespace llvm
172
173#endif // LLVM_TELEMETRY_TELEMETRY_H
static GCRegistry::Add< CoreCLRGC > E("coreclr", "CoreCLR-compatible GC")
#define LLVM_ABI
Definition Compiler.h:213
This file defines the DenseMap class.
#define T
This file contains some functions that are useful when dealing with strings.
Lightweight error class with error context and mandatory checking.
Definition Error.h:159
A wrapper around a string literal that serves as a proxy for constructing global tables of StringRefs...
Definition StringRef.h:854
StringRef - Represent a constant reference to a string, i.e.
Definition StringRef.h:55
LLVM Value Representation.
Definition Value.h:75
This class presents a data sink to which the Telemetry framework sends data.
Definition Telemetry.h:133
virtual Error receiveEntry(const TelemetryInfo *Entry)=0
virtual StringLiteral name() const =0
virtual ~Destination()=default
Manager & operator=(Manager const &)=delete
virtual ~Manager()=default
virtual Error preDispatch(TelemetryInfo *Entry)
Definition Telemetry.cpp:40
void addDestination(std::unique_ptr< Destination > Destination)
Definition Telemetry.cpp:36
virtual Error dispatch(TelemetryInfo *Entry)
Definition Telemetry.cpp:23
Manager(Manager const &)=delete
virtual void write(StringRef KeyName, unsigned long Value)=0
virtual Error init()=0
virtual ~Serializer()=default
virtual void write(StringRef KeyName, unsigned int Value)=0
virtual void write(StringRef KeyName, bool Value)=0
virtual void write(StringRef KeyName, long long Value)=0
virtual Error finalize()=0
virtual void write(StringRef KeyName, int Value)=0
virtual void write(StringRef KeyName, long Value)=0
virtual void beginObject(StringRef KeyName)=0
virtual void write(StringRef KeyName, StringRef Value)=0
virtual void write(StringRef KeyName, unsigned long long Value)=0
void write(StringRef KeyName, const T &Map)
Definition Telemetry.h:49
virtual void endObject()=0
unsigned KindType
For isa, dyn_cast, etc operations on TelemetryInfo.
Definition Telemetry.h:84
This is an optimization pass for GlobalISel generic memory operations.
static constexpr bool BuildTimeEnableTelemetry
Definition Telemetry.h:67
const bool EnableTelemetry
Definition Telemetry.h:70
virtual ~Config()=default
virtual std::optional< std::string > makeSessionId()
Definition Telemetry.h:80
This struct is used by TelemetryInfo to support isa<>, dyn_cast<> operations.
Definition Telemetry.h:90
static const KindType Base
Definition Telemetry.h:91
TelemetryInfo is the data courier, used to move instrumented data from the tool being monitored to th...
Definition Telemetry.h:104
virtual void serialize(Serializer &serializer) const
Definition Telemetry.cpp:19
virtual KindType getKind() const
Definition Telemetry.h:121
static bool classof(const TelemetryInfo *T)
Definition Telemetry.h:122
virtual ~TelemetryInfo()=default