2009-06-01 10 views
13

私はプロセスのCPU使用量とメモリ使用量を取得する方法を知っていますが、スレッドごとのレベルで取得する方法を知りました。最良の解決策がP-Invokingを行うことであるなら、それも良いことです。私は必要なもののC#(マネージコード)で* THREAD *のCPU使用量やRAM使用量を取得するにはどうすればよいですか?

例:ここでは

Thread myThread = Thread.CurrentThread; 

// some time later in some other function... 

Console.WriteLine(GetThreadSpecificCpuUsage(myThread)); 

答えて

7

何をしたいあなたは、スレッドごとのメモリ使用量を得ることができないん例です。 OSは、あるスレッドでメモリを割り当てて別のスレッドで使用したかどうかをどのように知っていますか?それはどういう意味ですか?

+0

スレッドローカルストレージがあります。 – CesarGon

+0

しかし、通常、TLSは共有メモリ内のオブジェクトへの参照を保持するためにのみ使用されます。 – erikkallen

+0

スレッドの使用を決定するのはOSではなく、フレームワーク自体です。 –

10

としては、それは全体としてプロセスの属性があるため、メモリの使用はお答えできません、と述べたが、CPUの使用:ここで

Process p = Process.GetCurrentProcess(); // getting current running process of the app 
foreach (ProcessThread pt in p.Threads) 
{ 
    // use pt.Id/pt.TotalProcessorTime/pt.UserProcessorTime/pt.PrivilegedProcessorTime 
} 
1

は、CPUの異なる量を消費5つのスレッドを起動する単純なプログラムですどの管理対象スレッドがどのCPU量を消費しているかを調べます。

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

class Program 
{ 
[DllImport("Kernel32", EntryPoint = "GetCurrentThreadId", ExactSpelling = true)] 
public static extern Int32 GetCurrentWin32ThreadId(); 

static void Main(string[] args) 
{ 
    Dictionary<int, Thread> threads = new Dictionary<int, Thread>(); 

    // Launch the threads 
    for (int i = 0; i < 5; i++) 
    { 
     Thread cpuThread = new Thread((start) => 
     { 
      lock (threads) 
      { 
       threads.Add(GetCurrentWin32ThreadId(), Thread.CurrentThread); 
      } 

      ConsumeCPU(20 * (int)start); 
     }); 
     cpuThread.Name = "T" + i; 
     cpuThread.Start(i); 
    } 

    // Every second wake up and see how much CPU each thread is using. 
    Thread monitoringThread = new Thread(() => 
     { 
      Stopwatch watch = new Stopwatch(); 
      watch.Start(); 

      while (true) 
      { 
       Thread.Sleep(1000); 
       Console.Write("\r"); 

       double totalTime = ((double)watch.ElapsedMilliseconds); 
       if (totalTime > 0) 
       { 
        Process p = Process.GetCurrentProcess(); 
        foreach (ProcessThread pt in p.Threads) 
        { 
         Thread managedThread; 
         if (threads.TryGetValue(pt.Id, out managedThread)) 
         { 
          double percent = (pt.TotalProcessorTime.TotalMilliseconds/totalTime); 
          Console.Write("{0}-{1:0.00} ", managedThread.Name, percent); 
         } 
        } 
       } 
      } 
     }); 
    monitoringThread.Start(); 
} 


// Helper function that generates a percentage of CPU usage 
public static void ConsumeCPU(int percentage) 
{ 
    Stopwatch watch = new Stopwatch(); 
    watch.Start(); 
    while (true) 
    { 
     if (watch.ElapsedMilliseconds > percentage) 
     { 
      Thread.Sleep(100 - percentage); 
      watch.Reset(); 
      watch.Start(); 
     } 
    } 
} 
} 

CLRがマネージスレッドの下で実行されていること、ネイティブスレッドを変更することが可能であることに注意してください。しかし、実際には、実際にどのくらいの頻度でこれが実際に発生するかはわかりません。

関連する問題