forked from open-telemetry/opentelemetry-ebpf-profiler
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
357 lines (289 loc) · 11.2 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0
package main
import (
"context"
"fmt"
"net/http"
//nolint:gosec
_ "net/http/pprof"
"os"
"os/signal"
"runtime"
"time"
"github.com/tklauser/numcpus"
"golang.org/x/sys/unix"
"go.opentelemetry.io/ebpf-profiler/times"
tracertypes "go.opentelemetry.io/ebpf-profiler/tracer/types"
"go.opentelemetry.io/ebpf-profiler/util"
"go.opentelemetry.io/ebpf-profiler/vc"
"go.opentelemetry.io/ebpf-profiler/host"
"go.opentelemetry.io/ebpf-profiler/tracehandler"
"go.opentelemetry.io/ebpf-profiler/hostmetadata"
"go.opentelemetry.io/ebpf-profiler/metrics"
"go.opentelemetry.io/ebpf-profiler/reporter"
"go.opentelemetry.io/ebpf-profiler/tracer"
log "github.com/sirupsen/logrus"
)
// Short copyright / license text for eBPF code
var copyright = `Copyright The OpenTelemetry Authors.
For the eBPF code loaded by Universal Profiling Agent into the kernel,
the following license applies (GPLv2 only). You can obtain a copy of the GPLv2 code at:
https://go.opentelemetry.io/ebpf-profiler/tree/main/support/ebpf
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License version 2 only,
as published by the Free Software Foundation;
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details:
https://www.gnu.org/licenses/old-licenses/gpl-2.0.en.html
`
type exitCode int
const (
exitSuccess exitCode = 0
exitFailure exitCode = 1
// Go 'flag' package calls os.Exit(2) on flag parse errors, if ExitOnError is set
exitParseError exitCode = 2
)
func startTraceHandling(ctx context.Context, rep reporter.TraceReporter,
intervals *times.Times, trc *tracer.Tracer, cacheSize uint32) error {
// Spawn monitors for the various result maps
traceCh := make(chan *host.Trace)
if err := trc.StartMapMonitors(ctx, traceCh); err != nil {
return fmt.Errorf("failed to start map monitors: %v", err)
}
_, err := tracehandler.Start(ctx, rep, trc.TraceProcessor(),
traceCh, intervals, cacheSize)
return err
}
func main() {
os.Exit(int(mainWithExitCode()))
}
func mainWithExitCode() exitCode {
args, err := parseArgs()
if err != nil {
return parseError("Failure to parse arguments: %v", err)
}
if args.copyright {
fmt.Print(copyright)
return exitSuccess
}
if args.version {
fmt.Printf("%s\n", vc.Version())
return exitSuccess
}
if args.verboseMode {
log.SetLevel(log.DebugLevel)
// Dump the arguments in debug mode.
args.dump()
}
if code := sanityCheck(args); code != exitSuccess {
return code
}
// Context to drive main goroutine and the Tracer monitors.
mainCtx, mainCancel := signal.NotifyContext(context.Background(),
unix.SIGINT, unix.SIGTERM, unix.SIGABRT)
defer mainCancel()
if args.pprofAddr != "" {
go func() {
//nolint:gosec
if err = http.ListenAndServe(args.pprofAddr, nil); err != nil {
log.Errorf("Serving pprof on %s failed: %s", args.pprofAddr, err)
}
}()
}
log.Infof("Starting OTEL profiling agent %s (revision %s, build timestamp %s)",
vc.Version(), vc.Revision(), vc.BuildTimestamp())
if err = tracer.ProbeBPFSyscall(); err != nil {
return failure("Failed to probe eBPF syscall: %v", err)
}
if err = tracer.ProbeTracepoint(); err != nil {
return failure("Failed to probe tracepoint: %v", err)
}
presentCores, err := numcpus.GetPresent()
if err != nil {
return failure("Failed to read CPU file: %v", err)
}
traceHandlerCacheSize :=
traceCacheSize(args.monitorInterval, args.samplesPerSecond, uint16(presentCores))
intervals := times.New(args.monitorInterval,
args.reporterInterval, args.probabilisticInterval)
// Start periodic synchronization with the realtime clock
times.StartRealtimeSync(mainCtx, args.clockSyncInterval)
log.Debugf("Determining tracers to include")
includeTracers, err := tracertypes.Parse(args.tracers)
if err != nil {
return failure("Failed to parse the included tracers: %v", err)
}
metadataCollector := hostmetadata.NewCollector(args.collAgentAddr)
metadataCollector.AddCustomData("os.type", "linux")
kernelVersion, err := getKernelVersion()
if err != nil {
return failure("Failed to get Linux kernel version: %v", err)
}
// OTel semantic introduced in https://github.com/open-telemetry/semantic-conventions/issues/66
metadataCollector.AddCustomData("os.kernel.release", kernelVersion)
// hostname and sourceIP will be populated from the root namespace.
hostname, sourceIP, err := getHostnameAndSourceIP(args.collAgentAddr)
if err != nil {
log.Warnf("Failed to fetch metadata information in the root namespace: %v", err)
}
metadataCollector.AddCustomData("host.name", hostname)
metadataCollector.AddCustomData("host.ip", sourceIP)
// Network operations to CA start here
var rep reporter.Reporter
// Connect to the collection agent
rep, err = reporter.Start(mainCtx, &reporter.Config{
CollAgentAddr: args.collAgentAddr,
DisableTLS: args.disableTLS,
MaxRPCMsgSize: 32 << 20, // 32 MiB
MaxGRPCRetries: 5,
GRPCOperationTimeout: intervals.GRPCOperationTimeout(),
GRPCStartupBackoffTime: intervals.GRPCStartupBackoffTime(),
GRPCConnectionTimeout: intervals.GRPCConnectionTimeout(),
ReportInterval: intervals.ReportInterval(),
CacheSize: traceHandlerCacheSize,
SamplesPerSecond: args.samplesPerSecond,
KernelVersion: kernelVersion,
HostName: hostname,
IPAddress: sourceIP,
})
if err != nil {
return failure("Failed to start reporting: %v", err)
}
metrics.SetReporter(rep)
// Now that set the initial host metadata, start a goroutine to keep sending updates regularly.
metadataCollector.StartMetadataCollection(mainCtx, rep)
// Load the eBPF code and map definitions
trc, err := tracer.NewTracer(mainCtx, &tracer.Config{
Reporter: rep,
Intervals: intervals,
IncludeTracers: includeTracers,
FilterErrorFrames: !args.sendErrorFrames,
SamplesPerSecond: args.samplesPerSecond,
MapScaleFactor: int(args.mapScaleFactor),
KernelVersionCheck: !args.noKernelVersionCheck,
BPFVerifierLogLevel: uint32(args.bpfVerifierLogLevel),
ProbabilisticInterval: args.probabilisticInterval,
ProbabilisticThreshold: args.probabilisticThreshold,
})
if err != nil {
return failure("Failed to load eBPF tracer: %v", err)
}
log.Printf("eBPF tracer loaded")
defer trc.Close()
now := time.Now()
// Initial scan of /proc filesystem to list currently active PIDs and have them processed.
if err = trc.StartPIDEventProcessor(mainCtx); err != nil {
log.Errorf("Failed to list processes from /proc: %v", err)
}
metrics.Add(metrics.IDProcPIDStartupMs, metrics.MetricValue(time.Since(now).Milliseconds()))
log.Debug("Completed initial PID listing")
// Attach our tracer to the perf event
if err := trc.AttachTracer(); err != nil {
return failure("Failed to attach to perf event: %v", err)
}
log.Info("Attached tracer program")
if args.probabilisticThreshold < tracer.ProbabilisticThresholdMax {
trc.StartProbabilisticProfiling(mainCtx)
log.Printf("Enabled probabilistic profiling")
} else {
if err := trc.EnableProfiling(); err != nil {
return failure("Failed to enable perf events: %v", err)
}
}
if err := trc.AttachSchedMonitor(); err != nil {
return failure("Failed to attach scheduler monitor: %v", err)
}
// This log line is used in our system tests to verify if that the agent has started. So if you
// change this log line update also the system test.
log.Printf("Attached sched monitor")
if err := startTraceHandling(mainCtx, rep, intervals, trc, traceHandlerCacheSize); err != nil {
return failure("Failed to start trace handling: %v", err)
}
// Block waiting for a signal to indicate the program should terminate
<-mainCtx.Done()
log.Info("Stop processing ...")
rep.Stop()
log.Info("Exiting ...")
return exitSuccess
}
// traceCacheSize defines the maximum number of elements for the caches in tracehandler.
//
// The caches in tracehandler have a size-"processing overhead" trade-off: Every cache miss will
// trigger additional processing for that trace in userspace (Go). For most maps, we use
// maxElementsPerInterval as a base sizing factor. For the tracehandler caches, we also multiply
// with traceCacheIntervals. For typical/small values of maxElementsPerInterval, this can lead to
// non-optimal map sizing (reduced cache_hit:cache_miss ratio and increased processing overhead).
// Simply increasing traceCacheIntervals is problematic when maxElementsPerInterval is large
// (e.g. too many CPU cores present) as we end up using too much memory. A minimum size is
// therefore used here.
func traceCacheSize(monitorInterval time.Duration, samplesPerSecond int,
presentCPUCores uint16) uint32 {
const (
traceCacheIntervals = 6
traceCacheMinSize = 65536
)
maxElements := maxElementsPerInterval(monitorInterval, samplesPerSecond, presentCPUCores)
size := maxElements * uint32(traceCacheIntervals)
if size < traceCacheMinSize {
size = traceCacheMinSize
}
return util.NextPowerOfTwo(size)
}
func maxElementsPerInterval(monitorInterval time.Duration, samplesPerSecond int,
presentCPUCores uint16) uint32 {
return uint32(uint16(samplesPerSecond) * uint16(monitorInterval.Seconds()) * presentCPUCores)
}
func sanityCheck(args *arguments) exitCode {
if args.samplesPerSecond < 1 {
return parseError("Invalid sampling frequency: %d", args.samplesPerSecond)
}
if args.mapScaleFactor > 8 {
return parseError("eBPF map scaling factor %d exceeds limit (max: %d)",
args.mapScaleFactor, maxArgMapScaleFactor)
}
if args.bpfVerifierLogLevel > 2 {
return parseError("Invalid eBPF verifier log level: %d", args.bpfVerifierLogLevel)
}
if args.probabilisticInterval < 1*time.Minute || args.probabilisticInterval > 5*time.Minute {
return parseError("Invalid argument for probabilistic-interval: use " +
"a duration between 1 and 5 minutes")
}
if args.probabilisticThreshold < 1 ||
args.probabilisticThreshold > tracer.ProbabilisticThresholdMax {
return parseError("Invalid argument for probabilistic-threshold. Value "+
"should be between 1 and %d", tracer.ProbabilisticThresholdMax)
}
if !args.noKernelVersionCheck {
major, minor, patch, err := tracer.GetCurrentKernelVersion()
if err != nil {
return failure("Failed to get kernel version: %v", err)
}
var minMajor, minMinor uint32
switch runtime.GOARCH {
case "amd64":
minMajor, minMinor = 4, 19
case "arm64":
// Older ARM64 kernel versions have broken bpf_probe_read.
// https://github.com/torvalds/linux/commit/6ae08ae3dea2cfa03dd3665a3c8475c2d429ef47
minMajor, minMinor = 5, 5
default:
return failure("Unsupported architecture: %s", runtime.GOARCH)
}
if major < minMajor || (major == minMajor && minor < minMinor) {
return failure("Host Agent requires kernel version "+
"%d.%d or newer but got %d.%d.%d", minMajor, minMinor, major, minor, patch)
}
}
return exitSuccess
}
func parseError(msg string, args ...interface{}) exitCode {
log.Errorf(msg, args...)
return exitParseError
}
func failure(msg string, args ...interface{}) exitCode {
log.Errorf(msg, args...)
return exitFailure
}