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 <vector>
26
27namespace llvm {
28namespace telemetry {
29
31public:
32 virtual ~Serializer() = default;
33
34 virtual Error init() = 0;
35 virtual void write(StringRef KeyName, bool Value) = 0;
36 virtual void write(StringRef KeyName, StringRef Value) = 0;
37 virtual void write(StringRef KeyName, int Value) = 0;
38 virtual void write(StringRef KeyName, long Value) = 0;
39 virtual void write(StringRef KeyName, long long Value) = 0;
40 virtual void write(StringRef KeyName, unsigned int Value) = 0;
41 virtual void write(StringRef KeyName, unsigned long Value) = 0;
42 virtual void write(StringRef KeyName, unsigned long long Value) = 0;
43 virtual void beginObject(StringRef KeyName) = 0;
44 virtual void endObject() = 0;
45 virtual Error finalize() = 0;
46
47 template <typename T, typename = typename T::mapped_type>
48 void write(StringRef KeyName, const T &Map) {
49 static_assert(std::is_convertible_v<typename T::key_type, StringRef>,
50 "KeyType must be convertible to string");
51 beginObject(KeyName);
52 for (const auto &KeyVal : Map)
53 write(KeyVal.first, KeyVal.second);
54 endObject();
55 }
56};
57
58/// Configuration for the Manager class.
59/// This stores configurations from both users and vendors and is passed
60/// to the Manager upon construction. (Any changes to the config after
61/// the Manager's construction will not have any effect on it).
62///
63/// This struct can be extended as needed to add additional configuration
64/// points specific to a vendor's implementation.
65struct Config {
66 static constexpr bool BuildTimeEnableTelemetry = LLVM_ENABLE_TELEMETRY;
67
68 // If true, telemetry will be enabled.
69 const bool EnableTelemetry;
70
72
73 virtual ~Config() = default;
74
75 // Telemetry can only be enabled if both the runtime and buildtime flag
76 // are set.
78
79 virtual std::optional<std::string> makeSessionId() { return std::nullopt; }
80};
81
82/// For isa, dyn_cast, etc operations on TelemetryInfo.
83typedef unsigned KindType;
84/// This struct is used by TelemetryInfo to support isa<>, dyn_cast<>
85/// operations.
86/// It is defined as a struct (rather than an enum) because it is
87/// expected to be extended by subclasses which may have
88/// additional TelemetryInfo types defined to describe different events.
89struct EntryKind {
90 static const KindType Base = 0;
91};
92
93/// TelemetryInfo is the data courier, used to move instrumented data
94/// from the tool being monitored to the Telemetry framework.
95///
96/// This base class contains only the basic set of telemetry data.
97/// Downstream implementations can define more subclasses with
98/// additional fields to describe different events and concepts.
99///
100/// For example, The LLDB debugger can define a DebugCommandInfo subclass
101/// which has additional fields about the debug-command being instrumented,
102/// such as `CommandArguments` or `CommandName`.
104 // This represents a unique-id, conventionally corresponding to
105 // a tool's session - i.e., every time the tool starts until it exits.
106 //
107 // Note: a tool could have multiple sessions running at once, in which
108 // case, these shall be multiple sets of TelemetryInfo with multiple unique
109 // IDs.
110 //
111 // Different usages can assign different types of IDs to this field.
112 std::string SessionId;
113
114 TelemetryInfo() = default;
115 virtual ~TelemetryInfo() = default;
116
117 virtual void serialize(Serializer &serializer) const;
118
119 // For isa, dyn_cast, etc, operations.
120 virtual KindType getKind() const { return EntryKind::Base; }
121 static bool classof(const TelemetryInfo *T) {
122 return T->getKind() == EntryKind::Base;
123 }
124};
125
126/// This class presents a data sink to which the Telemetry framework
127/// sends data.
128///
129/// Its implementation is transparent to the framework.
130/// It is up to the vendor to decide which pieces of data to forward
131/// and where to forward them.
133public:
134 virtual ~Destination() = default;
135 virtual Error receiveEntry(const TelemetryInfo *Entry) = 0;
136 virtual StringLiteral name() const = 0;
137};
138
139/// This class is the main interaction point between any LLVM tool
140/// and this framework.
141/// It is responsible for collecting telemetry data from the tool being
142/// monitored and transmitting the data elsewhere.
144public:
145 Manager() = default;
146 virtual ~Manager() = default;
147
148 // Explicitly non-copyable.
149 Manager(Manager const &) = delete;
150 Manager &operator=(Manager const &) = delete;
151
152 // Dispatch Telemetry data to the Destination(s).
153 // The argument is non-const because the Manager may add or remove
154 // data from the entry.
155 virtual Error dispatch(TelemetryInfo *Entry);
156
157 // Register a Destination.
158 void addDestination(std::unique_ptr<Destination> Destination);
159
160protected:
161 // Optional callback for subclasses to perform additional tasks before
162 // dispatching to Destinations.
163 virtual Error preDispatch(TelemetryInfo *Entry);
164
165private:
166 std::vector<std::unique_ptr<Destination>> Destinations;
167};
168
169} // namespace telemetry
170} // namespace llvm
171
172#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:132
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:48
virtual void endObject()=0
unsigned KindType
For isa, dyn_cast, etc operations on TelemetryInfo.
Definition Telemetry.h:83
This is an optimization pass for GlobalISel generic memory operations.
static constexpr bool BuildTimeEnableTelemetry
Definition Telemetry.h:66
const bool EnableTelemetry
Definition Telemetry.h:69
virtual ~Config()=default
virtual std::optional< std::string > makeSessionId()
Definition Telemetry.h:79
This struct is used by TelemetryInfo to support isa<>, dyn_cast<> operations.
Definition Telemetry.h:89
static const KindType Base
Definition Telemetry.h:90
TelemetryInfo is the data courier, used to move instrumented data from the tool being monitored to th...
Definition Telemetry.h:103
virtual void serialize(Serializer &serializer) const
Definition Telemetry.cpp:19
virtual KindType getKind() const
Definition Telemetry.h:120
static bool classof(const TelemetryInfo *T)
Definition Telemetry.h:121
virtual ~TelemetryInfo()=default