flat assembler
Message board for the users of flat assembler.

Index > Windows > Windows 10 get CPU usage percent ?

Author
Thread Post new topic Reply to topic
Roman



Joined: 21 Apr 2012
Posts: 1792
Roman 05 Dec 2020, 16:31
I found this.
Code:
#include <windows.h>
#include <psapi.h>
#include <pdh.h>

.................................................... 
    HQUERY Query = NULL;
    PDH_STATUS Status = PdhOpenQuery(NULL, 0, &Query);
    HCOUNTER Counter;
 
    CHAR CounterPathBuffer[PDH_MAX_COUNTER_PATH] = "\\Thread(_Total/_Total)\\Context Switches/sec";
    if (Status != ERROR_SUCCESS) ...ERROR...    
    
 Status = PdhAddCounter(Query, CounterPathBuffer, 0, &Counter);
 if (Status != ERROR_SUCCESS) ...ERROR...     
 
 Status = PdhCollectQueryData(Query);
    if (Status != ERROR_SUCCESS) ...ERROR...    

 ....................Timed.code................
 
    Status = PdhCollectQueryData(Query);
    if (Status != ERROR_SUCCESS) ...ERROR...    
 
    DWORD CounterType;
    PDH_FMT_COUNTERVALUE DisplayValue;
    Status = PdhGetFormattedCounterValue(Counter,
                                         PDH_FMT_DOUBLE,
                                         &CounterType,
                                         &DisplayValue);
    cout << "Counter: " << DisplayValue.doubleValue << endl;
 
    if (Query){
       PdhCloseQuery(Query);
    } 
    

But get DisplayValue.doubleValue big value.
What do this DisplayValue.doubleValue , how convert or calculate ?
Post 05 Dec 2020, 16:31
View user's profile Send private message Reply with quote
Roman



Joined: 21 Apr 2012
Posts: 1792
Roman 05 Dec 2020, 16:43
Post 05 Dec 2020, 16:43
View user's profile Send private message Reply with quote
ProMiNick



Joined: 24 Mar 2012
Posts: 799
Location: Russian Federation, Sochi
ProMiNick 05 Dec 2020, 17:56
PDH_FMT_COUNTERVALUE
https://docs.microsoft.com/en-us/windows/win32/api/pdh/ns-pdh-pdh_fmt_countervalue
for 64bit I guess DisplayValue.doubleValue located at DisplayValue+8
how convert double to text?
MS provided something like this
cinvoke wsprintf,szBuffer,szFormatStr,[DisplayValue+8]

szFormatStr TCHAR 'Counter: %f',0
one moment wsprintf can`t convert floats to strings, so thou will have to realize it manualy (But community will never forget thou if thou share it)
Post 05 Dec 2020, 17:56
View user's profile Send private message Send e-mail Reply with quote
Display posts from previous:
Post new topic Reply to topic

Jump to:  


< Last Thread | Next Thread >
Forum Rules:
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You can download files in this forum


Copyright © 1999-2024, Tomasz Grysztar. Also on GitHub, YouTube.

Website powered by rwasa.