如何监视.NET TaskSchedulers(跨AppDomain)中的任务队列 [英] How can I monitor the Task queues in the .NET TaskSchedulers (across AppDomain)
问题描述
作为开发人员,我想监视TaskSchedulers中任务队列"中的工作量(和进度),以便我可以评估生产负荷下经历的速度下降是否是由于计划任务的规模或停滞造成的
As a developer, I would like to monitor the size (and progress) of work in the Task Queues in the TaskSchedulers so that I can evaluate whether an experienced slowdown under production load is due to the magnitude or perhaps stall of tasks scheduled.
通常,我只是简单地附加调试器并检查任务大小,但是:
Normally I would simply attach the debugger and inspect task sizes, but:
- 该应用程序可以在Mono下运行并在生产环境中运行,因此我无法使用Visual Studio附加到该应用程序上
- 我想提供对此数据的分析输出作为服务健康监视的监视输入
我已经浏览了文档并找到了
I have been through the docs and found
- TaskScheduler.GetScheduledTasks ,它将此信息传递给调试器.它受到保护(我可以绕过它),但似乎还需要一些关于冻结线程的保证,而这些保证是我无法兑现的.但是,我愿意使用不一致的数据.
- 如何获取列表.net 4.0中运行的任务.重点放在运行任务上,这些任务对我而言并不那么有趣.我对积压的数量以及工作是否在进行中很感兴趣.
- TaskScheduler.GetScheduledTasks, which delivers this information to the debugger. It is protected (which I could circumvent), but also seem to require some guarantees about frozen threads which I cannot honor. However, I am willing to use inconsistent data.
- how to get a list of running tasks in .net 4.0. Which focuses on the running Tasks, which are not that interesting to me. I am interested in the size of the backlog and whether work is progressing.
我愿意:
- 使用为其他目的(例如调试器)设计的代码
- 接受我得到的数据不一致(这是用于统计和分析)
我不想要做的事情是:
- 向创建的每个任务添加跟踪.
- 某些代码是第三方,无法重构
- 有很多 代码,将这些代码注入其中比无法知道代码的进展情况更糟.
- Add tracking to each task created.
- Some of the code is 3rd party and cannot be refactored
- There is lots of code, and injecting this into that is worse than not being able to tell how it is progressing.
- 某些代码是第三方,无法重构
- 某些代码使用TaskSchedulers来设置线程优先级和最大并发性.将监控方面的因素考虑在内会很糟糕.
推荐答案
不幸的是,TaskScheduler.GetScheduledTasks
仅用于调试器,没有直接的方法来获取计划任务.
Unfortunately the TaskScheduler.GetScheduledTasks
is designed for debuggers only and there is no direct way to get scheduled tasks.
But MSDN says (source):
实现自定义调试器的开发人员不应直接调用此方法,而应使用内部包装器方法
GetScheduledTasksForDebugger
代替:internal Task[] GetScheduledTasksForDebugger()
.这个包装方法返回一个任务数组,而不是一个可枚举的数组.要检索活动调度程序的列表,请使用内部方法internal static TaskScheduler[] GetTaskSchedulersForDebugger()
.此静态方法返回所有活动TaskScheduler实例的数组.然后,您可以在每个调度程序实例上使用GetScheduledTasksForDebugger
来检索其调度任务列表.
Developers who are implementing custom debuggers shouldn't call this method directly, but should use the internal wrapper method
GetScheduledTasksForDebugger
instead:internal Task[] GetScheduledTasksForDebugger()
. This wrapper method returns an array of tasks instead of an enumerable. To retrieve a list of active schedulers, use the internal methodinternal static TaskScheduler[] GetTaskSchedulersForDebugger()
. This static method returns an array of all active TaskScheduler instances. You can then useGetScheduledTasksForDebugger
on each scheduler instance to retrieve its list of scheduled tasks.
那么为什么不使用反射呢?这是一种安全轻松的方法,可以实现您想要的目标,因为GetScheduledTasksForDebugger
将来会存在,并且它是为调试和概要分析而设计的.
So why not use reflection? This is a safe and easy way to achieve what you want because GetScheduledTasksForDebugger
will exist in future and it's designed for debugging and probably for profiling.
这篇关于如何监视.NET TaskSchedulers(跨AppDomain)中的任务队列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!