GetThreadTimes为什么返回

问题描述:

我试图测量线程中用于进度报告目的的时间,但是我从GetThreadTimes系统调用中得到了非常奇怪的结果。考虑下面的程序(在VS 2013编译,针对.NET 4.5):GetThreadTimes为什么返回

using System; 
using System.Diagnostics; 
using System.Runtime.InteropServices; 
using System.Threading; 

namespace ThreadTimingTest 
{ 
    class Program 
    { 
     static Stopwatch _wallClockTimer; 

     static System.Timers.Timer _timer = new System.Timers.Timer(); 

     private static Thread _thread; 
     private static IntPtr _threadHandle; 

     static void Main(string[] args) 
     { 
      _timer = new System.Timers.Timer(); 
      _timer.Elapsed += (s, e) => 
      { 
       System.Runtime.InteropServices.ComTypes.FILETIME start, end, rawKernelTime, rawUserTime; 

       GetThreadTimes(_threadHandle, out start, out end, out rawKernelTime, out rawUserTime); 

       //ref: http://*.com/a/6083846 
       ulong uLow = (ulong)rawKernelTime.dwLowDateTime; 
       ulong uHigh = (uint)rawKernelTime.dwHighDateTime; 
       uHigh = uHigh << 32; 
       long kernelTime = (long)(uHigh | uLow); 

       uLow = (ulong)rawUserTime.dwLowDateTime; 
       uHigh = (uint)rawUserTime.dwHighDateTime; 
       uHigh = uHigh << 32; 
       long userTime = (long)(uHigh | uLow); 

       Debug.WriteLine("Kernel time: " + kernelTime); 
       Debug.WriteLine("User time: " + userTime); 
       Debug.WriteLine("Combined raw execution time: " + (kernelTime + userTime)); 

       long elapsedMilliseconds = (kernelTime + userTime)/10000; //convert to milliseconds: raw timing unit is 100 nanoseconds 
       Debug.WriteLine("Elapsed thread time: " + elapsedMilliseconds + " milliseconds"); 

       Debug.WriteLine("Wall Clock Time: " + _wallClockTimer.ElapsedMilliseconds + " milliseconds"); 

      }; 

      _timer.Interval = 1000; 

      _wallClockTimer = new Stopwatch(); 

      Debug.WriteLine("Starting..."); 
      RunTest(); 

      Debug.WriteLine("Ended."); 
     } 

     public static void RunTest() 
     { 
      _thread = 
       new Thread 
       (
        () => 
        { 
         _threadHandle = GetCurrentThread(); 
         Stopwatch sw = Stopwatch.StartNew(); 

         while (sw.ElapsedMilliseconds < 3000) 
         { 
          int i = 1 + 2; 
         } //do busy-work for 3 seconds 

         sw.Stop(); 
        } 
       ); 

      _timer.Start(); 

      _thread.Start();a 

      _wallClockTimer.Start(); 

      _thread.Join(); 
     } 

     [DllImport("kernel32.dll", SetLastError = true)] 
     static extern bool GetThreadTimes(IntPtr hThread, 
      out System.Runtime.InteropServices.ComTypes.FILETIME lpCreationTime, out System.Runtime.InteropServices.ComTypes.FILETIME lpExitTime, 
      out System.Runtime.InteropServices.ComTypes.FILETIME lpKernelTime, out System.Runtime.InteropServices.ComTypes.FILETIME lpUserTime); 

     [DllImport("kernel32.dll")] 
     private static extern IntPtr GetCurrentThread(); 
    } 
} 

我得到以下输出:

Starting... 
Kernel time: 0 
User time: 0 
Combined raw execution time: 0 
Elapsed thread time: 0 milliseconds 
Wall Clock Time: 1036 milliseconds 
Kernel time: 0 
User time: 0 
Combined raw execution time: 0 
Elapsed thread time: 0 milliseconds 
Wall Clock Time: 2036 milliseconds 
The thread '<No Name>' (0x191c) has exited with code 0 (0x0). 
Ended. 

我希望GetThreadTimes为螺纹次报告为零以外的东西:为什么零报告?

+1

[多个托管线程可以在同一个操作系统线程上运行](http://msdn.microsoft.com/en-us/library/74169f59.aspx)。假设:.NET运行时在完成其时间片之前正在中断OS线程,因此其时间不会被记录(参见[this](http://blog.kalmbachnet.de/?postid=28)文章)。当然,时间不会准确无论如何,因为你没有测量在* managed *线程中花费的时间,你正在测量底层的OS线程。 – Blorgbeard 2014-10-20 19:31:41

+4

因为GetCurrentThread()不会返回您认为它的作用。让你衡量错误线程的时间。刚刚在[此博客文章]中报道(http://blogs.msdn.com/b/oldnewthing/archive/2014/10/15/10564700.aspx)。 – 2014-10-20 19:48:28

在根据Hans提供的链接对代码进行几个简单的修改后,将显示有效时间。

添加几个互操作的声明:

[DllImport("kernel32.dll", SetLastError = true)] 
[return: MarshalAs(UnmanagedType.Bool)] 
static extern bool DuplicateHandle(IntPtr hSourceProcessHandle, 
    IntPtr hSourceHandle, IntPtr hTargetProcessHandle, out IntPtr lpTargetHandle, 
    uint dwDesiredAccess, [MarshalAs(UnmanagedType.Bool)] bool bInheritHandle, uint dwOptions); 

[Flags] 
public enum DuplicateOptions : uint 
{ 
    DUPLICATE_CLOSE_SOURCE = (0x00000001), // Closes the source handle. This occurs regardless of any error status returned. 
    DUPLICATE_SAME_ACCESS = (0x00000002), // Ignores the dwDesiredAccess parameter. The duplicate handle has the same access as the source handle. 
} 

[DllImport("kernel32.dll")] 
static extern IntPtr GetCurrentProcess(); 

然后修改手柄如何分配:

//_threadHandle = GetCurrentThread();  <-- previous assignment 
IntPtr processHandle = GetCurrentProcess(); 
bool result = DuplicateHandle(processHandle, GetCurrentThread(), processHandle, out _threadHandle, 0, false, (uint) DuplicateOptions.DUPLICATE_SAME_ACCESS); 

产生以下结果:

Starting... 
Kernel time: 0 
User time: 10000000 
Combined raw execution time: 10000000 
Elapsed thread time: 1000 milliseconds 
Wall Clock Time: 1006 milliseconds 
Kernel time: 0 
User time: 20000000 
Combined raw execution time: 20000000 
Elapsed thread time: 2000 milliseconds 
Wall Clock Time: 2004 milliseconds 
Kernel time: 0 
User time: 30000000 
Combined raw execution time: 30000000 
Ended. 
Elapsed thread time: 3000 milliseconds 
Wall Clock Time: 3045 milliseconds 

编辑:

最近,我们付出了很大的努力来处理为给定系统创建的太多线程。假设你有一个四核处理器,并且有20多个线程都想运行。线程在启动,内核管理,内存(他们有自己的堆栈)等方面的成本相当高。与减少线程数量相比,系统实际上可能更慢(杂乱的上下文和调度)。所以在.NET中,创建了类似TPL的库(允许开发人员管理任务,而不是线程)。这使得CLR能够将适当的线程数量平衡到目标系统。但在你的情况下(你明确地创建一个托管线程),与本地线程几乎总是有1对1的关系。

希望这会有所帮助。

+0

感谢您的回答!但我不清楚一件事:这个解决方案如何解决@ Blorgbeard的观察:多个托管线程可以在同一个OS线程上运行? – Caleb 2014-10-22 17:59:43

+0

@Caleb - 我会编辑我的答案(因为解释有点长)。 – Jeff 2014-10-22 19:18:00