首页 > 解决方案 > 为什么在 Linux 中跟踪 C# 代码在导入 speedscope 时会失败?

问题描述

背景:

我有一些运行非常缓慢的代码,因此建议我分析代码以找出原因。

下图显示了在 Linux 上运行的 Visual Studio Code,我在下面使用了一些 dotnet 命令来创建trace.nettracetrace.speedscope.json文件:

在此处输入图像描述

然后我尝试将trace.speedscope.json文件导入https://www.speedscope.app/并收到以下错误:

在此处输入图像描述


命令:

我使用以下页面作为创建跟踪 speedscope 文件的指南:

https://github.com/jlfwong/speedscope/wiki/Importing-from-.NET-Core

具体来说,我使用的命令是:

dotnet trace collect -p <process id> --format speedscope


例子:

可以在此处查看生成的示例 speedscope 跟踪文件:

https://pastebin.com/4WW2tbiF


问题:

如何创建适用于https://www.speedscope.app/的 speedscope 跟踪文件?

标签: c#linux.net-corevisual-studio-codeprofiling

解决方案


您的命令(基于手册屏幕截图

$ dotnet tool install --global dotnet-trace
$ dotnet trace collect -p <process id> --format speedscope
No profile or providers specified, defaulting to trace profile 'cpu-sampling' 
Provider Name Level Enabled by 
Microsoft-DotNETCore-SampleProfiler Informational(4) --profile 
Microsoft-Windows-DotNETRuntime Informational(4) --profile 

Process: /Code/mysql-insert-1234/bin/Debug/netcoreapp3.1/mysql-insert-1234 
Output File: /Code/mysql-insert-1234/trace.nettrace

[00:00:00:02] Recording trace 2.0604 (MB) 

Press <Enter> or <Ctrl+C> to exit... Trace completed.
$ dotnet trace convert --format speedscope trace.nettrace 
Writing: /Code/mysql-insert-1234/trace.speedscope.json 
Conversion complete

您来自屏幕截图的错误消息(从您的原始 json 文件中复制的类似错误):

speedscope v1.8.0  
Importing as speedscope json file 
Failed to load format error: Tried to leave frame "AppendExtraBuffer" while frame "TryGetValue" was at the top at 31.8
 at n._leaveFrame (160)
 at n.leaveFrame (160)
 at speedscope.js:166
 at a (166)
 at speedscope.js:166
 at Array.map (<anonymous>)
 at s (166)
 at import.js:111
 at Generator.next (<anonymous>)
 at s (import.js:111)

基于“错误:试图离开框架”错误片段,我能够找到两个类似错误的报告:

https://github.com/dotnet/diagnostics/issues/1206

dotnet-trace:speedscope.app 显示错误,显示加载格式失败错误:试图离开框架 #1206 已关闭 mcraiha 于 2020 年 6 月 6 日打开此问题 ... josalem 于 2020 年 6 月 17 日发表评论

看起来这是由 Speedscope (jlfwong/speedscope#273) 的最近更改导致的错误,该更改决定更改行为并在某些排序约束上失败。我将看看我们在 Microsoft/PerfView 中的转换逻辑,看看我是否能找出导致排序问题的原因。

Microsoft/PerfView 中的问题:microsoft/perfview#1178。根据这里的评论,看起来我们应该能够通过选择最新的 TraceEvent 版本来解决这个问题。

adamsitnik 于 2020 年 6 月 18 日发表评论 我提供了一个修复microsoft/perfview#1212

noahfalk 在 #1313 中关闭了这个 2020 年 7 月 6 日

https://github.com/microsoft/perfview/issues/1178

导出到 speedscope 有时会出现不对称的打开/关闭帧事件顺序 #1178 Open jlfwong 打开了这个问题 2020 年 5 月 23 日 · 8 条评论

因此,据说该错误与速度范围的https://github.com/jlfwong/speedscope/pull/273更新有关(“jlfwong 将 2 个提交从 jlfwong/fail-unmatched-loudly 2020 年 5 月 23 日合并到 master”),已包含在 speedscope.app 的 v1.6.0(2020 年 5 月 31 日)版本中。我可以建议您尝试使用早期版本的 speedscope 来解析此文件(从 2020 年 1 月 16 日开始的 v1.5.3)或获取更新版本的 perfview(应用https://github.com/microsoft/perfview/pull/1212提交)。

speedscope 应用程序有预编译离线版本:https ://github.com/jlfwong/speedscope/releases - 从 https://github.com/jlfwong/speedscope/releases/download/v1 获取 pull/273 更新之前的版本。 5.3/speedscope-1.5.3.zip

解压它,用浏览器打开 index.html 并加载你的 json 跟踪文件。我能够使用旧版本的 speedscope从https://pastebin.com/4WW2tbiF呈现您的示例

不知道如何使用 npm 或更新 microsoft/perfview 或使用未发布版本的 microsoft/perfview。


推荐阅读