.net - C# performance counter data is "all over the map" -
i need calculate cpu , ram usage overall system specific process. i've never done in c#. able come following code (that took samples on web site):
try { process proc = process.getcurrentprocess(); string strprocname = proc.processname; console.writeline("process: " + strprocname); using (performancecounter total_cpu = new performancecounter("process", "% processor time", "_total", true)) { using (performancecounter process_cpu = new performancecounter("process", "% processor time", strprocname, true)) { (; ; ) { console.cursortop = 1; console.cursorleft = 0; float t = total_cpu.nextvalue() / environment.processorcount; float p = process_cpu.nextvalue() / environment.processorcount; console.writeline(string.format("total cpu (%) = {0}\t\t\napp cpu (%) = {1}\t\t\napp ram (kb) = {2}", t, p, process.getcurrentprocess().workingset64 / (1024) )); system.threading.thread.sleep(100); } } } } catch(exception ex) { console.writeline("exception: " + ex); }
but gives me data "all on map". take look:
so can answer these points:
it seems running such performance counter pretty costly operation -- raises cpu usage 5%. did ton of cpu counters c++ , take pretty no cpu time run.
besides said above, first time loop above runs 2 second delay! literally hangs 2 seconds. normal? if is, can't believe had audacity call performance counter :)
even though ram reading pretty close 1 reported task manager, cpu output totally off. can tell me doing wrong here?
moreover, can't seem find documentation performancecounter class, can explain of these:
% processor time
,_total
, etc? , importantly, english only? supposed localized?the process there specified name. if have more 1 process under same name running. then?
here's came with, using unmanaged apis:
[dllimport("kernel32.dll")] [return: marshalas(unmanagedtype.bool)] static extern bool getprocesstimes(intptr hprocess, 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")] [return: marshalas(unmanagedtype.u4)] static extern uint32 gettickcount(); static bool gbsetolddata = false; static uint32 gmsoldtickcount = 0; static ulong gnsoldkerneltime = 0; static ulong gnsoldusertime = 0; public static double getcpuusageforprocess(int nprocid = 0) { //get cpu usage process in id in 'nprocid' //'nprocid' = process id, or 0 current process //return: // = cpu usage: [0.0 - 1.0] // = negative if error double fcpuusage = -1.0; try { intptr hprocess = nprocid != 0 ? process.getprocessbyid(nprocid).handle : process.getcurrentprocess().handle; system.runtime.interopservices.comtypes.filetime ftcreated, ftexit, ftkernel, ftuser; if (getprocesstimes(hprocess, out ftcreated, out ftexit, out ftkernel, out ftuser)) { uint32 dwmsnewtickcount = gettickcount(); ulong nsnewkerneltime = (ulong)ftkernel.dwhighdatetime; nsnewkerneltime <<= 32; nsnewkerneltime |= (ulong)(uint)ftkernel.dwlowdatetime; ulong nsnewusertime = (ulong)ftuser.dwhighdatetime; nsnewusertime <<= 32; nsnewusertime |= (ulong)(uint)ftuser.dwlowdatetime; if (gbsetolddata) { //adjust 100-nanosecond intervals milliseconds //100-nanosecond intervals = 100 * 10^-9 = 10^-7 //1ms = 10^-3 fcpuusage = (double)((nsnewkerneltime - gnsoldkerneltime) + (nsnewusertime - gnsoldusertime)) / (double)((dwmsnewtickcount - gmsoldtickcount) * 10000); //account multiprocessor architecture fcpuusage /= environment.processorcount; //in case timer api report inaccurate if (fcpuusage > 1.0) fcpuusage = 1.0; } else { //for first run, assume no cpu usage fcpuusage = 0.0; } //remember data gnsoldkerneltime = nsnewkerneltime; gnsoldusertime = nsnewusertime; gmsoldtickcount = dwmsnewtickcount; gbsetolddata = true; } } catch { //failed fcpuusage = -1.0; } return fcpuusage; }
and how you'd call it:
int ndummy = 1; (; ; ) { double fcpu = getcpuusageforprocess(); console.cursortop = 1; console.cursorleft = 0; int ncpu = (int)(fcpu * 100); console.writeline("cpu%: {0}\t\t", ncpu); //time filler long j = 0; (; j < 1000000; j++) { ndummy += (int)math.cos(1 / (j + 1)); } //don't hog cpu time! system.threading.thread.sleep(500); }
i'm not sure if .net has similar method. if there's 1 (that doesn't have limitations posted above) i'd hear it?
Comments
Post a Comment