InfluxDB 运行时
InfluxDB 提供 Go 运行时的配置文件、跟踪和其他有助于分析和调试服务器运行时执行的信息。
Go运行时概况
A Go runtime profile 是一组堆栈跟踪,显示导致特定事件实例的调用序列。InfluxDB 提供以下事件的配置文件数据:
- 区块
- CPU 使用率
- 内存分配
- 互斥 (mutex)
- 操作系统线程创建
当您向 InfluxDB 发送配置文件请求时,Golang 运行时 pprof 包 会对运行时事件进行采样,以收集堆栈跟踪和统计信息(例如,堆分配事件的内存字节数)。对于某些配置文件,您可以设置 InfluxDB 收集配置文件数据的秒数。
一旦数据收集完成,InfluxDB将返回配置文件数据。默认响应格式是压缩的协议缓冲区,格式为profile.proto。profile.proto文件与pprof和go tool pprof分析工具兼容。对于某些配置,InfluxDB提供了另一种可读性更高的纯文本格式,其中包含注释,翻译为函数调用和行号,但pprof工具和profile.proto格式提供了以下优点:
- 从磁盘或HTTP读取配置文件。
- 汇总并比较多个相同类型的配置文件。
- 分析和筛选个人资料数据。
- 生成可视化和报告。
分析 Go 运行时配置文件
使用 /debug/pprof InfluxDB 端点一次性下载所有配置文件或单独请求它们。
获取所有运行时配置文件
要一次性下载所有运行时性能剖析,使用HTTP客户端发送一个 GET 请求到 /debug/pprof/all 端点。 go tool pprof 无法直接从 /debug/pprof/all 获取剖析。
GET http://localhost:8086/debug/pprof/allInfluxDB 返回一个 gzip 压缩的 tar 文件,该文件包含以下以 profile.proto 格式的配置文件:
profiles/allocs.pb.gz: 概况所有内存分配profiles/block.pb.gz: 配置剔除操作profiles/cpu.pb.gz: (可选) profile CPU.profiles/goroutine.pb.gz: 分析 goroutinesprofiles/heap.pb.gz: 分析堆内存分配profiles/mutex.pb.gz: 配置互斥profiles/threadcreate.pb.gz: 配置线程创建
| 选项 | 包含方式 |
|---|---|
| 个人资料 CPU | 在您的请求 URL 中使用 cpu 查询参数传递一个 持续时间(以秒为单位) |
使用像 curl 或 wget 的 HTTP 客户端从 /debug/pprof/all 下载配置文件。
示例
# Use `curl` to download a `.tar.gz` of all profiles after 10 seconds of CPU sampling.
# Use `tar` to extract the profiles folder.
curl "http://localhost:8086/debug/pprof/all?cpu=10s" | tar -xz
# Analyze an extracted profile.
go tool pprof profiles/heap.pb.gz
分析所有内存分配
分析内存分配并将默认的配置文件显示设置为 alloc_space,自程序开始以来分配的总字节数(包括被垃圾收集的字节)。
GET http://localhost:8086/debug/pprof/allocs| 选项 | 包含方式 |
|---|---|
| 采样秒数 | 在您的请求URL中传递一个 无符号整数,使用 seconds 查询参数 |
输出纯文本(与 seconds 互斥) | 在您的请求 URL 中使用 debug 查询参数传递 1 |
# Analyze the profile in interactive mode.
go tool pprof http://localhost:8086/debug/pprof/allocs
# `pprof` returns the following prompt:
# Entering interactive mode (type "help" for commands, "o" for options)
# (pprof)
# At the prompt, get the top N memory allocations.
(pprof) top10
个人资料阻止操作
导致在同步原语上阻塞的配置操作,并导致Go暂停goroutine的执行。
GET http://localhost:8086/debug/pprof/block| 选项 | 包含方式 |
|---|---|
| 输出纯文本 | 在您的请求URL中使用debug查询参数传递1 |
# Analyze the profile in interactive mode.
go tool pprof http://localhost:8086/debug/pprof/block
# `pprof` returns the following prompt:
# Entering interactive mode (type "help" for commands, "o" for options)
# (pprof)
# At the prompt, get the top N entries.
(pprof) top10
配置CPU
从执行栈采样的程序计数器。要下载分析数据,请使用HTTP客户端向/debug/pprof/profile端点发送GET请求。go tool pprof无法直接获取CPU分析数据。
GET http://localhost:8086/debug/pprof/profile| 选项 | 包含方式 |
|---|---|
采样的秒数(默认 30) | 在您的请求URL中使用 seconds 查询参数传递一个 无符号整数 |
使用像 curl 或 wget 这样的HTTP客户端来下载配置文件。
示例
# Get the profile.
curl http://localhost:8086/debug/pprof/profile -o cpu
# Analyze the profile in interactive mode.
go tool pprof ./cpu
# At the prompt, get the top N functions most often running
# or waiting during the sample period.
(pprof) top10
使用 seconds 查询参数来控制采样持续时间。
/debug/pprof/profile?seconds=SECONDS 返回与 /debug/pprof/all?cpu=DURATION 相同的 CPU 配置文件。
示例
# Get the CPU profile after 10 seconds of sampling.
curl "http://localhost:8086/debug/pprof/profile?seconds=10" -o cpu
# Get all profiles after 10 seconds of CPU sampling.
curl "http://localhost:8086/debug/pprof/all?cpu=10s" -o all.tar.gz
配置goroutines
分析所有当前的 goroutines。
GET http://localhost:8086/debug/pprof/goroutine| 选项 | 包含方式 |
|---|---|
| 采样秒数 | 在您的请求URL中传递一个 无符号整数,使用 seconds 查询参数 |
输出纯文本(与 seconds 互斥) | 在您的请求 URL 中使用 debug 查询参数传递 1 |
示例
# Analyze the profile in interactive mode.
go tool pprof http://localhost:8086/debug/pprof/goroutine
# `pprof` returns the following prompt:
# Entering interactive mode (type "help" for commands, "o" for options)
# (pprof)
# At the prompt, get the top N entries.
(pprof) top10
个人资料堆内存分配
配置堆或用于活动对象的内存分配。
GET http://localhost:8086/debug/pprof/heap| 选项 | 包含方式 |
|---|---|
| 在采样之前运行垃圾控制 | 在您的请求 URL 中使用 gc 查询参数传递 1 |
| 采样秒数 | 在您的请求URL中传递一个 无符号整数,使用 seconds 查询参数 |
输出纯文本(与 seconds 互斥) | 在您的请求 URL 中使用 debug 查询参数传递 1 |
示例
# Analyze the profile in interactive mode.
go tool pprof http://localhost:8086/debug/pprof/heap
# `pprof` returns the following prompt:
# Entering interactive mode (type "help" for commands, "o" for options)
# (pprof)
# At the prompt, get the top N memory-intensive nodes.
(pprof) top10
# pprof displays the list:
# Showing nodes accounting for 142.46MB, 85.43% of 166.75MB total
# Dropped 895 nodes (cum <= 0.83MB)
# Showing top 10 nodes out of 143
配置互斥(互斥体)
档案持有者的争用互斥锁(mutexes)。
GET http://localhost:8086/debug/pprof/mutex| 选项 | 包含方式 |
|---|---|
| 采样秒数 | 在您的请求URL中传递一个 无符号整数,使用 seconds 查询参数 |
输出纯文本(与 seconds 互斥) | 在您的请求 URL 中使用 debug 查询参数传递 1 |
示例
# Analyze the profile in interactive mode.
go tool pprof http://localhost:8086/debug/pprof/mutex
# `pprof` returns the following prompt:
# Entering interactive mode (type "help" for commands, "o" for options)
# (pprof)
# At the prompt, get the top N entries.
(pprof) top10
配置线程创建
导致创建操作系统线程的配置文件操作。
GET http://localhost:8086/debug/pprof/threadcreate| 选项 | 包含方式 |
|---|---|
| 采样秒数 | 在您的请求URL中传递一个 无符号整数,使用 seconds 查询参数 |
输出纯文本(与 seconds 互斥) | 在您的请求 URL 中使用 debug 查询参数传递 1 |
示例
# Analyze the profile in interactive mode.
go tool pprof http://localhost:8086/debug/pprof/threadcreate
# `pprof` returns the following prompt:
# Entering interactive mode (type "help" for commands, "o" for options)
# (pprof)
# At the prompt, get the top N entries.
(pprof) top10
分析 Go 运行时追踪
要跟踪InfluxDB的执行事件,请使用/debug/pprof/trace端点与go tool trace。
GET http://localhost:8086/debug/pprof/trace示例
# Download the trace file.
curl http://localhost:8086/debug/pprof/trace -o trace.out
# Analyze the trace.
go tool trace ./trace.out
从 trace 生成类似 pprof 的性能分析
您可以使用 go tool trace 从跟踪文件生成 pprof样 的配置文件,然后使用 go tool pprof 进行分析。
示例
# Generate a profile from the downloaded trace file.
go tool trace -pprof=PROFILE_TYPE ./trace.out > PROFILE_TYPE.pprof
将 PROFILE_TYPE 替换为以下 Golang 配置文件类型 之一:
net: 网络阻塞配置文件sync: 同步阻塞配置文件syscall: 系统调用阻塞分析sched: 调度程序延迟概况
查看调用InfluxDB的命令行
要查看调用InfluxDB的命令、参数和命令行变量,请使用 /debug/pprof/cmdline 端点。
GET http://localhost:8086/debug/pprof/cmdline/debug/pprof/cmdline 返回以纯文本形式的命令行调用。
查看运行时配置
在 InfluxDB v2.3+ 中,您可以查看您的活动运行时配置,包括标志和环境变量。查看如何查看您的运行时服务器配置。