InfluxDB 运行时
InfluxDB 提供了 Go 运行时 profile、trace 和其他信息,这些信息对于分析和调试服务器运行时执行非常有用。
Go 运行时 profile 概述
Go 运行时 profile 是堆栈跟踪的集合,显示了导致特定事件实例的调用序列。 InfluxDB 为以下事件提供 profile 数据
- 阻塞
- CPU 使用率
- 内存分配
- 互斥 (mutex)
- OS 线程创建
当您向 InfluxDB 发送 profile 请求时,Golang 运行时 pprof 包 对运行时上的事件进行采样,以收集堆栈跟踪和统计信息(例如,堆分配事件的内存字节数)。对于某些 profile,您可以设置 InfluxDB 将收集 profile 数据的秒数。
数据收集完成后,InfluxDB 返回 profile 数据。默认响应格式是 profile.proto 格式的压缩协议缓冲区。 profile.proto 文件与 pprof 和 go tool pprof
分析工具兼容。对于某些 profile,InfluxDB 提供了一种替代的人类可读纯文本格式,其中包含转换为函数调用和行号的注释,但 pprof
工具和 profile.proto 格式具有以下优点
- 从磁盘或 HTTP 读取 profile。
- 聚合和比较相同类型的多个 profile。
- 分析和过滤 profile 数据。
- 生成可视化和报告。
分析 Go 运行时 profile
使用 /debug/pprof
InfluxDB 端点一次下载所有 profile 或单独请求它们。
- 获取所有运行时 profile
- Profile 所有内存分配
- Profile 阻塞操作
- Profile CPU
- Profile Goroutine
- Profile 堆内存分配
- Profile 互斥锁
- Profile 线程创建
获取所有运行时 profile
要一次下载所有运行时 profile,请使用 HTTP 客户端向 /debug/pprof/all
端点发送 GET
请求。 go tool pprof
无法直接从 /debug/pprof/all
获取 profile。
GET http://localhost:8086/debug/pprof/all
InfluxDB 返回一个 gzipped tar 文件,其中包含以下 profile.proto 格式的 profile
profiles/allocs.pb.gz
: profile 所有内存分配profiles/block.pb.gz
: profile 阻塞操作profiles/cpu.pb.gz
: (可选) profile CPU。profiles/goroutine.pb.gz
: profile Goroutineprofiles/heap.pb.gz
: profile 堆内存分配profiles/mutex.pb.gz
: profile 互斥锁profiles/threadcreate.pb.gz
: profile 线程创建
选项 | 包含方式 |
---|---|
Profile CPU | 在您的请求 URL 中使用 cpu 查询参数传递 秒持续时间 |
使用 HTTP 客户端(如 curl
或 wget
)从 /debug/pprof/all
下载 profile。
示例
# 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
Profile 所有内存分配
Profile 内存分配并将默认 profile 显示设置为 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
Profile 阻塞操作
Profile 导致阻塞同步原语并导致 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
Profile CPU
Profile 从执行堆栈采样的程序计数器。要下载 profile,请使用 HTTP 客户端向 /debug/pprof/profile
端点发送 GET
请求。 go tool pprof
无法直接获取 CPU profile。
GET http://localhost:8086/debug/pprof/profile
选项 | 包含方式 |
---|---|
采样秒数(默认为 30 ) | 在您的请求 URL 中使用 seconds 查询参数传递 无符号整数 |
使用 HTTP 客户端(如 curl
或 wget
)下载 profile。
示例
# 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 profile。
示例
# 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
Profile Goroutine
Profile 所有当前 Goroutine。
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
Profile 堆内存分配
Profile 堆或活动对象的内存分配。
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
Profile 互斥锁
Profile 争用互斥锁的持有者。
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
Profile 线程创建
Profile 导致创建 OS 线程的操作。
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 运行时 trace
要跟踪 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 的 profile
您可以使用 go tool trace
从 trace 文件生成类似 pprof 的 profile,然后使用 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 profile 类型 之一
net
:网络阻塞 profilesync
:同步阻塞 profilesyscall
:系统调用阻塞 profilesched
:调度器延迟 profile
查看调用 InfluxDB 的命令行
要查看调用 InfluxDB 的命令、参数和命令行变量,请使用 /debug/pprof/cmdline
端点。
GET http://localhost:8086/debug/pprof/cmdline
/debug/pprof/cmdline
以纯文本形式返回命令行调用。
查看运行时配置
在 InfluxDB v2.3+ 中,您可以查看您的活动运行时配置,包括标志和环境变量。请参阅如何查看您的运行时服务器配置。
此页内容对您有帮助吗?
感谢您的反馈!
支持和反馈
感谢您成为我们社区的一份子!我们欢迎并鼓励您提供关于 InfluxDB 和本文档的反馈和错误报告。要获得支持,请使用以下资源
拥有年度合同或支持合同的客户 可以联系 InfluxData 支持。