13

プロセスのCPU使用率とメモリ使用率を取得する方法は知っていますが、スレッドごとのレベルで取得する方法を考えていました。最善の解決策がP呼び出しを行うことである場合、それも問題ありません。

必要なものの例:

Thread myThread = Thread.CurrentThread;

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

Console.WriteLine(GetThreadSpecificCpuUsage(myThread));
4

4 に答える 4

11

前述のように、メモリ使用量はプロセス全体の属性であるため回答できませんが、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
}
于 2009-06-01T12:34:34.117 に答える
7

メモリはプロセス内のすべてのスレッド間で共有されるため、スレッドごとのメモリ使用量を取得できません。あるスレッドでメモリを割り当て、別のスレッドで使用したかどうかを OS が知るにはどうすればよいでしょうか。そして、それはどういう意味ですか?

于 2009-06-01T12:19:21.183 に答える
3

これはあなたが望むことをする例です http://www.codeproject.com/KB/system/processescpuusage.aspx

于 2009-06-01T12:07:11.023 に答える
2

これは、異なる量の 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 によって変更される可能性があることに注意してください。ただし、実際には、これが実際にどのくらいの頻度で発生するかはわかりません。

于 2017-01-10T20:56:19.337 に答える