有没有办法记录 IIS 线程池线程的使用情况?
我想看看池中的线程何时投入使用以及何时释放。
我想要的是探索第三方库的行为,以便决定我是否应该打扰它的异步方法。如果这些方法阻止公共池中的线程,那么使用它们不会为 ASP.NET 应用程序带来任何好处。该库已被混淆,因此我不想使用反编译器来搜索答案。
I want to see when exactly a thread from the pool is brought into play and when it is freed.
What I want is to explore the behaviour of a third party library in order to decide whether I should bother with its async methods. If these methods block threads from the common pool then using of them wouldn't provide any benefits for ASP.NET apps. The library is obfuscated, so I don't want to search for an answer with a decompiler.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果您希望某个事件或某种公共挂钩能够告诉您 ThreadPool 线程正在做什么,那么不幸的是没有这样的事情。
我可以想到另外两种可能有帮助的方法:
首先,您可以在调用库之前和之后检查 ThreadPool.GetAvailableThreads() 的workerThreads 结果的值。如果值发生变化,库可能就是罪魁祸首。这在您仅处理单个请求的测试环境中很可能是有意义的。您还可以将workerThreads 数量保存在自定义Windows 性能计数器中,并随时间跟踪它——也许可以用它来比较一种方法与另一种方法。
以下是描述性能计数器方法的文章的链接:
http://msdn。 microsoft.com/en-us/library/ff650682.aspx
如果您使用的是 IIS 7+,另一种可能性是禁用
MaxConcurrentRequestsPerCPU
(通过.NET 4.0+ 中的HostingEnvironment
,或通过 .NET 3.5 或更高版本中的 aspnet.config 文件)将其设置为零,并将MaxConcurrentThreadsPerCPU
设置为相对较小的数字,并使用库中的一个界面与另一个界面来衡量应用程序在负载下的性能。如果由于没有足够的线程而导致请求排队,响应时间将相应地跳跃。遗憾的是,如何执行此操作以及进行哪些测量的详细信息取决于您的库和 Web 请求的结构。If you're hoping for an event or a public hook of some kind that will tell you what the ThreadPool threads are doing, unfortunately there is no such thing.
I can think of two other approaches that might help:
First, you can check the value of the workerThreads result from
ThreadPool.GetAvailableThreads()
, before and after calling into your library. If the value changes, the library may be the culprit. This is most likely to be meaningful in a test environment where you're only processing a single request. You can also save the workerThreads number in a custom Windows performance counter, and track it over time -- perhaps using it to compare one approach vs. the other.Here's a link to an article that describes the performance counter approach:
http://msdn.microsoft.com/en-us/library/ff650682.aspx
If you're using IIS 7+, another possibility is to disable
MaxConcurrentRequestsPerCPU
(viaHostingEnvironment
in .NET 4.0+, or via the aspnet.config file in .NET 3.5 or later) by setting it to zero, and setMaxConcurrentThreadsPerCPU
to a relatively small number, and measure the performance of your app under load, using one interface in your library vs. the other. If requests are getting queued due to not having enough threads, response times will jump accordingly. The details of how to do this and what measurements to make unfortunately depend on the structure of your library and web requests.您提出问题的方式就像您想要一个监视程序的调试器一样。
现在,在您的程序中,您知道何时从池中调用线程以及何时释放它 - 不是吗?
如果您不控制程序,您想查看和跟踪其他程序,那么它听起来就像一个通用的调试器。这可以通过使用 c/c++ 的钩子函数和大量代码来完成。
如果您单击池并查看属性,sysinternals 中的进程资源管理器可以实时为您提供打开的线程。
另一方面,我认为 ThreadPool.GetAvailableThreads 可以在您需要时为您提供您所要求的信息。
http://msdn.microsoft.com/en- us/library/system.threading.threadpool.getavailablethreads.aspx
如果您可以提供一些有关您真正尝试归档和缩小问题范围的更多信息,从一般的调试程序到更多有针对性的需求。
The way you ask the question is like you want a debugger that monitor your program.
Now inside your program you know when you call a thread from the pool and when you release it - don't you ?
If you do not control the program, you want to see and trace other programs then its sound like a general debugger. This can be done by hook functions using c/c++ and a lot of code.
The process explorer from sysinternals can give you the open threads on real time, if you click on pool and see the properties.
From the other hand I think that
ThreadPool.GetAvailableThreads
can give you the information's you ask, at the moment that you needed.http://msdn.microsoft.com/en-us/library/system.threading.threadpool.getavailablethreads.aspx
If you can give some more information's on what you really try to archive and narrow your question, from a general debug program to a more targeted need.