LLVM 20.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"
20#include "llvm/Support/Error.h"
21#include <map>
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 virtual ~Config() = default;
68
69 // If true, telemetry will be enabled.
70 const bool EnableTelemetry;
72
73 virtual std::optional<std::string> makeSessionId() { return std::nullopt; }
74};
75
76/// For isa, dyn_cast, etc operations on TelemetryInfo.
77typedef unsigned KindType;
78/// This struct is used by TelemetryInfo to support isa<>, dyn_cast<>
79/// operations.
80/// It is defined as a struct (rather than an enum) because it is
81/// expected to be extended by subclasses which may have
82/// additional TelemetryInfo types defined to describe different events.
83struct EntryKind {
84 static const KindType Base = 0;
85};
86
87/// TelemetryInfo is the data courier, used to move instrumented data
88/// from the tool being monitored to the Telemetry framework.
89///
90/// This base class contains only the basic set of telemetry data.
91/// Downstream implementations can define more subclasses with
92/// additional fields to describe different events and concepts.
93///
94/// For example, The LLDB debugger can define a DebugCommandInfo subclass
95/// which has additional fields about the debug-command being instrumented,
96/// such as `CommandArguments` or `CommandName`.
98 // This represents a unique-id, conventionally corresponding to
99 // a tool's session - i.e., every time the tool starts until it exits.
100 //
101 // Note: a tool could have multiple sessions running at once, in which
102 // case, these shall be multiple sets of TelemetryInfo with multiple unique
103 // IDs.
104 //
105 // Different usages can assign different types of IDs to this field.
106 std::string SessionId;
107
108 TelemetryInfo() = default;
109 virtual ~TelemetryInfo() = default;
110
111 virtual void serialize(Serializer &serializer) const;
112
113 // For isa, dyn_cast, etc, operations.
114 virtual KindType getKind() const { return EntryKind::Base; }
115 static bool classof(const TelemetryInfo *T) {
116 return T->getKind() == EntryKind::Base;
117 }
118};
119
120/// This class presents a data sink to which the Telemetry framework
121/// sends data.
122///
123/// Its implementation is transparent to the framework.
124/// It is up to the vendor to decide which pieces of data to forward
125/// and where to forward them.
127public:
128 virtual ~Destination() = default;
129 virtual Error receiveEntry(const TelemetryInfo *Entry) = 0;
130 virtual StringLiteral name() const = 0;
131};
132
133/// This class is the main interaction point between any LLVM tool
134/// and this framework.
135/// It is responsible for collecting telemetry data from the tool being
136/// monitored and transmitting the data elsewhere.
137class Manager {
138public:
139 virtual ~Manager() = default;
140
141 // Optional callback for subclasses to perform additional tasks before
142 // dispatching to Destinations.
143 virtual Error preDispatch(TelemetryInfo *Entry) = 0;
144
145 // Dispatch Telemetry data to the Destination(s).
146 // The argument is non-const because the Manager may add or remove
147 // data from the entry.
148 virtual Error dispatch(TelemetryInfo *Entry);
149
150 // Register a Destination.
151 void addDestination(std::unique_ptr<Destination> Destination);
152
153private:
154 std::vector<std::unique_ptr<Destination>> Destinations;
155};
156
157} // namespace telemetry
158} // namespace llvm
159
160#endif // LLVM_TELEMETRY_TELEMETRY_H
static GCRegistry::Add< CoreCLRGC > E("coreclr", "CoreCLR-compatible GC")
This file defines the DenseMap class.
This file contains some functions that are useful when dealing with strings.
Lightweight error class with error context and mandatory checking.
Definition: Error.h:160
A wrapper around a string literal that serves as a proxy for constructing global tables of StringRefs...
Definition: StringRef.h:853
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:51
LLVM Value Representation.
Definition: Value.h:74
This class presents a data sink to which the Telemetry framework sends data.
Definition: Telemetry.h:126
virtual Error receiveEntry(const TelemetryInfo *Entry)=0
virtual StringLiteral name() const =0
virtual ~Destination()=default
This class is the main interaction point between any LLVM tool and this framework.
Definition: Telemetry.h:137
virtual Error preDispatch(TelemetryInfo *Entry)=0
virtual ~Manager()=default
void addDestination(std::unique_ptr< Destination > Destination)
Definition: Telemetry.cpp:21
virtual Error dispatch(TelemetryInfo *Entry)
Definition: Telemetry.cpp:10
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:77
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
Configuration for the Manager class.
Definition: Telemetry.h:66
const bool EnableTelemetry
Definition: Telemetry.h:70
virtual ~Config()=default
virtual std::optional< std::string > makeSessionId()
Definition: Telemetry.h:73
This struct is used by TelemetryInfo to support isa<>, dyn_cast<> operations.
Definition: Telemetry.h:83
static const KindType Base
Definition: Telemetry.h:84
TelemetryInfo is the data courier, used to move instrumented data from the tool being monitored to th...
Definition: Telemetry.h:97
virtual void serialize(Serializer &serializer) const
Definition: Telemetry.cpp:6
virtual KindType getKind() const
Definition: Telemetry.h:114
static bool classof(const TelemetryInfo *T)
Definition: Telemetry.h:115
virtual ~TelemetryInfo()=default