2012-04-16 12 views
4

C#を使用して2つのプロセスを起動するテストを実行しています。自分のプロセスで使用されるトップメモリ​​とCPUを取得する必要があります。管理コードを使用して行う方法についてのガイドラインを教えてください。 (私はまた、モノを使用してLinux上で実行します)。 A.exeB.exeC#でメモリとCPUの使用状況をプロファイルする方法

プロセスtest.exeは、2つのプロセスを起動します:

アーキテクチャは次のようです。私はそれを行うことは可能ですtest.exe

から、プロセスAとBのための meausre最大メモリとCPUを測定する必要がありますか?事前に感謝

+0

トップCPUを試すことができますか?パーセントを意味しますか?実行中は、コアの100%です。ブロックされているときは0%です。プロセスによるCPU使用率は、これらの2つの実行平均です。それは本当にあなたが知りたいことですか? –

答えて

7

System.Diagnostics.Processクラスを使用してプロセスを開始することができます。 UserProcessorTimeTotalProcessorTimePeakWorkingSet64などのプロパティをチェックして、プロセッサの使用状況とメモリ使用量を確認できます。これをチェックしてくださいMSDN Article for System.Diagnostics.Process

+0

これはプロセッサの使用量を示すものではなく、CPU時間のみを示します。 – Joey

2

てみ使用このGet CPU Info

PerformanceCounter cpuCounter; 
PerformanceCounter ramCounter; 

cpuCounter = new PerformanceCounter(); 

cpuCounter.CategoryName = "Processor"; 
cpuCounter.CounterName = "% Processor Time"; 
cpuCounter.InstanceName = "_Total"; 

ramCounter = new PerformanceCounter("Memory", "Available MBytes"); 


public string getCurrentCpuUsage(){ 
     return cpuCounter.NextValue()+"%"; 
} 

public string getAvailableRAM(){ 
     return ramCounter.NextValue()+"MB"; 
} 

EDIT: あなたのプロセスを開始した後、前にあなたは違いを確認することができます。

0

それとも、使用されるこの

private ulong GetCPU(ThreadEntry[] thList) 
{ 
    ulong result = 0; 
    for (int i = 0; i < thList.Length; i++) 
    { 
    ulong NewThC = 0; 
    ulong NewThE = 0; 
    ulong NewThK = 0; 
    ulong NewThU = 0; 
    if(GetThreadTimes(thList[i].ThreadID, ref NewThC, ref NewThE, ref NewThK, ref NewThU)) 
     result = result + NewThK + NewThU; 
    int error = Marshal.GetLastWin32Error(); 
//often ERROR == 6 or ERROR == 18 
    } 
    return result; 
} 

//GET ALL PROCESS CPU USAGE 
private void timer1_Tick(object sender, EventArgs e) 
{ 
//reset results 
    this.textBox1.Text = string.Empty; 
//turn of timer 
    this.Enabled = false; 
    uint perm = GetCurrentPermissions(); 


    //SET PERMISION SO I CAN RECEIVE INFO ABOUT THREADS 
    SetProcPermissions(0xFFFFFFFF); 


//get all running process (OPENNETCF)  
    List<ProcessEntry> processList = new List<ProcessEntry>(ProcessEntry.GetProcesses()); 

//OLD Variables stored in list 
    if (OldResList == null || (OldResList != null && OldResList.Length != processList.Count)) 
    OldResList = new ulong[processList.Count]; 

//SORT by ID only for testing  
    processList.Sort(CompareProcessEntry); 
//GET ALL CPU USAGE  
    for(int i=0;i<processList.Count;i++) 
    { 
    //new value 
    ulong newRes = GetCPU(processList[i].GetThreads()); 
    //result 
    ulong result = (newRes - OldResList[i]); 
    //valid result 
    result = result/(ulong)this.timer1.Interval; 
    //set result to the thexbox 
    this.textBox1.Text += processList[i].ExeFile + " " + result.ToString() + " ;"; 
    //change old to new 
    OldResList[i] = newRes; 
    } 
    //sleep 
    Thread.Sleep(1000); 
    SetProcPermissions(0xFFFFFFFF); 
    //start again 

    timer1.Enabled = true; 
    } 
    public static int CompareProcessEntry(ProcessEntry p1, ProcessEntry p2) 
    { 
    return p1.ProcessID.CompareTo(p2.ProcessID); 
    } 

    [DllImport("coredll")] 
    private static extern bool GetThreadTimes(uint p, ref ulong NewThC, ref ulong NewThE, ref ulong NewThK, ref ulong NewThU); 
関連する問題