You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用lttng追踪.NET Core 6的EventSource时如何获取ThreadId

原理说明

cpu_id是事件触发时线程所在的CPU核心编号,线程运行过程中会被操作系统调度切换到不同核心执行,因此同一个函数的Enter、Leave事件cpu_id不同属于正常现象。

采集线程ID的配置修改

你只需要在原有采集脚本中添加上下文采集规则,即可获取线程ID信息:
在lttng enable-channel --userspace DotNetCoreChannel命令执行后,添加如下命令:

lttng add-context --userspace --channel=DotNetCoreChannel --type vtid --type vpid

其中vtid就是你需要的进程内操作系统级线程ID,vpid是进程ID。

完整修改后的采集脚本
export COMPlus_PerfMapEnabled=1
export COMPlus_EnableEventLog=1

lttng create my-session -o ./my-trace
lttng enable-channel --userspace DotNetCoreChannel
# 新增上下文采集配置
lttng add-context --userspace --channel=DotNetCoreChannel --type vtid --type vpid
lttng enable-event --userspace --tracepoint DotNETRuntime:EventSource --channel=DotNetCoreChannel --filter "EventSourceName=='Trace'"
lttng start
dotnet run 
lttng stop
lttng view
lttng destroy my-session
结果验证

修改配置后重新采集,输出的trace数据会新增vtid字段即为对应事件的线程ID,示例如下:

[22:05:00.402124499] (+?.?????????) userVM DotNETRuntime:EventSource: { cpu_id = 1, vpid = 12345, vtid = 12347 }, { EventID = 1, EventName = "Enter", EventSourceName = "Trace", Payload = "" }

如果需要批量解析trace数据提取线程ID,可使用babeltrace 2命令指定输出字段筛选即可。

补充说明

如果你需要.NET 托管线程ID和操作系统线程ID的对应关系,可以在你自定义的Trace EventSource的事件Payload中手动添加Thread.ManagedThreadId输出,即可实现两种ID的映射。

内容的提问来源于stack exchange,提问作者Фёдор Надуткин

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.09.23 20:54:02