.net - How to get accurate CPU usage for a process on a multi-processor / multi-core system -
or maybe question more "what doing blatantly wrong here?"
i have test app nothing watch own cpu usage. looks little this:
protected performancetrace() { process currentprocess = process.getcurrentprocess(); this.cpucounter = new performancecounter("process", "% processor time", currentprocess.processname); this.corecount = environment.processorcount; } private int corecount; private datetime lastcpuread = datetime.minvalue; private float _cpuusage; private float cpuusage { { if ((datetime.now - this.lastcpuread) > timespan.fromseconds(1.0)) { this._cpuusage = this.cpucounter.nextvalue(); } return this._cpuusage / this.corecount; } }
the cpuusage property read frequently. here's thing:
on machine, environment.processorcount
produces value of 2. however, value coming counter 800. assuming has multiple cores , hyperthreading. can actual value i'm looking for? (the total % processor time particular process)
you rigt - have divide number performance counters number of cpus times number of real threads per cpu - 1 per core, plus 1 per core if hyperthreading factor.
in c# don't know how determine this. in native code can use getlogicalprocessorinformation , its associated structure count logical processors, including share core.
Comments
Post a Comment