|
|
不开心的皮带 · surface ...· 4 月前 · |
|
|
害羞的饭卡 · 阿米莉亚·华生 - 萌娘百科 万物皆可萌的百科全书· 10 月前 · |
|
|
飞翔的枕头 · 广东海洋大学硕士生拟调剂专业及联系方式(更新 ...· 1 年前 · |
|
|
活泼的香槟 · fcm模糊聚类matlab实例 - CSDN文库· 1 年前 · |
|
|
烦恼的哑铃 · 宇宙来的双性吸血鬼X地球的科研壮攻!!!早古 ...· 2 年前 · |
This article is a brief explanation of how to use the
GetProcessTimes
API. There are times when knowing how long a process has been running might be useful.
The time values returned from
GetProcessTimes
are fairly easy to convert into something useful/readable. Let's operate on this code snippet:
HANDLE hProcess; </CODE>FILETIME ftCreation, ftExit, ftKernel, ftUser; GetProcessTimes(hProcess, &ftCreation, &ftExit, &ftKernel, &ftUser);
A processes' running-time is the amount of time that has elapsed between the current time and the processes' creation time. This is stored in a
FILETIME
structure.
Once the elapsed time is calculated, then it's a matter of converting it to hours, minutes, seconds, etc. Luckily, the
COleDateTime
class makes this a painless process.
COleDateTime timeNow = COleDateTime::GetCurrentTime(), timeCreation = ftCreation; COleDateTimeSpan timeDiff = timeNow - timeCreation;
From here, you can use the different methods of
COleDateTimeSpan
to get the elapsed hours, minutes, etc.
Per the documentation, the kernel and user times are
amounts of time
rather than an actual
time period
. The value in the
FILETIME
structure is expressed in 100-nanosecond units. To convert that to something useful, let's look at two methods.
We can convert that to seconds with some basic arithmetic. A nanosecond is one billionth of a second, but since the time is already expressed in 100-nanosecond units, we'll only divide by 10 million:
__int64 i64Kernel = *((__int64 *) &ftKernel); DWORD dwKernel = (DWORD) (i64Kernel / 10000000U);
As an alternative to the casting used above, a union could have just as easily been employed:
union FILETIME ftKernel; __int64 i64Kernel; } timeKernel; timeKernel.ftKernel = ftKernel; DWORD dwKernel = (DWORD) (timeKernel.i64Kernel / 10000000U);
Either way,
dwKernel
now represents the number of elapsed seconds that the process has been in kernel mode. Converting seconds to hours, minutes, and seconds is a straightforward process.
An alternative method that does not require anything other than a function call is to use the
FileTimeToSystemTime
API. This stores the result in a
SYSTEMTIME
structure, where we then have access to the
wHour
,
wMinute
, and
wSecond
members.
SYSTEMTIME stKernel; FileTimeToSystemTime(&ftKernel, &stKernel);
The user-mode time is handled in the same way as kernel-mode time.
That's all there is to it. Looking at all of this together yields:
GetProcessTimes(hProcess, &ftCreation, &ftExit, &ftKernel, &ftUser); timeCreation = ftCreation; strData.Format("Created at %02d:%02d:%02d", timeCreation.GetHour(), timeCreation.GetMinute(), timeCreation.GetSecond()); timeDiff = timeNow - timeCreation; strData.Format("Elapsed time = %ud %uh %um %us", timeDiff.GetDays(), timeDiff.GetHours(), timeDiff.GetMinutes(), timeDiff.GetSeconds()); FileTimeToSystemTime(&ftKernel, &stKernel); strData.Format("Time in kernel mode = %uh %um %us", stKernel.wHour, stKernel.wMinute, stKernel.wSecond);
The way the demo code is currently written, some system-level processes did not allow their name and time-information to be retrieved.
I want to check memory usage by a process running in a remote system. Here is the code snippet I am using. I am specifying the system name/ IP, but call to PdhAddCounter()is failing. Any suggestion?
Are you using Windows XP or Vista?
Aryan S wrote:pdhStatus = PdhAddCounter(hQuery, szWorkingSet, 0, &hWorkingSet); // failing here :: Access denied
"A good athlete is the result of a good and worthy opponent." - David Crow
"To have a respect for ourselves guides our morals; to have deference for others governs our manners." - Laurence Sterne
Sir,with all due respect.
TCHAR szAvailBytes[256] = TEXT(
"
"
);
TCHAR szCacheBytes[256] = TEXT(
"
"
);
TCHAR szWorkingSet[256] = TEXT(
"
"
);
TCHAR szBuffer[256] = TEXT(
"
"
);
DWORD dwBufferSize =
sizeof
(szAvailBytes);
HCOUNTER hAvailBytes,
hCacheBytes,
hWorkingSet;
HQUERY hQuery = NULL;
PDH_COUNTER_PATH_ELEMENTS pdhCpe;
PDH_STATUS pdhStatus;
PDH_FMT_COUNTERVALUE pdhfmtAvail,
pdhfmtCache,
pdhfmtWorking;
__try
pdhStatus = PdhOpenQuery(NULL,
0
, &hQuery);
if
(pdhStatus != ERROR_SUCCESS)
__leave;
//
Make Counter Path
pdhCpe.szMachineName = TEXT(
"
your machine"
);
pdhCpe.szObjectName = TEXT(
"
Memory"
);
pdhCpe.szInstanceName = NULL;
pdhCpe.szParentInstance = NULL;
pdhCpe.dwInstanceIndex = -1;
pdhCpe.szCounterName = TEXT(
"
Available Bytes"
);
pdhStatus = PdhMakeCounterPath(&pdhCpe, szAvailBytes, &dwBufferSize,
0
);
if
(pdhStatus != ERROR_SUCCESS)
__leave;
pdhCpe.szCounterName = TEXT(
"
Cache Bytes"
);
dwBufferSize =
sizeof
(szCacheBytes);
pdhStatus = PdhMakeCounterPath(&pdhCpe, szCacheBytes, &dwBufferSize,
0
);
if
(pdhStatus != ERROR_SUCCESS)
__leave;
pdhCpe.szObjectName = TEXT(
"
Process"
);
pdhCpe.szInstanceName = TEXT(
"
_Total"
);
pdhCpe.szCounterName = TEXT(
"
Working Set"
);
dwBufferSize =
sizeof
(szWorkingSet);
pdhStatus = PdhMakeCounterPath(&pdhCpe, szWorkingSet, &dwBufferSize,
0
);
if
(pdhStatus != ERROR_SUCCESS)
__leave;
//
Add counters.
pdhStatus = PdhAddCounter(hQuery, szAvailBytes,
0
, &hAvailBytes);
if
(pdhStatus != ERROR_SUCCESS)
__leave;
pdhStatus = PdhAddCounter(hQuery, szCacheBytes,
0
, &hCacheBytes);
if
(pdhStatus != ERROR_SUCCESS)
__leave;
pdhStatus = PdhAddCounter(hQuery, szWorkingSet,
0
, &hWorkingSet);
if
(pdhStatus != ERROR_SUCCESS)
__leave;
//
Get the data.
pdhStatus = PdhCollectQueryData(hQuery);
if
(pdhStatus != ERROR_SUCCESS)
__leave;
//
Format counter values.
pdhStatus = PdhGetFormattedCounterValue(hAvailBytes, PDH_FMT_LONG | PDH_FMT_NOSCALE, NULL, &pdhfmtAvail);
if
(pdhStatus != ERROR_SUCCESS)
__leave;
pdhStatus = PdhGetFormattedCounterValue(hCacheBytes, PDH_FMT_LONG | PDH_FMT_NOSCALE, NULL, &pdhfmtCache);
if
(pdhStatus != ERROR_SUCCESS)
__leave;
pdhStatus = PdhGetFormattedCounterValue(hWorkingSet, PDH_FMT_LONG | PDH_FMT_NOSCALE, NULL, &pdhfmtWorking);
if
(pdhStatus != ERROR_SUCCESS)
__leave;
wsprintf(szBuffer, TEXT(
"
Avail Bytes = %ld\n"
), pdhfmtAvail.longValue);
OutputDebugString(szBuffer);
wsprintf(szBuffer, TEXT(
"
Cache Bytes = %ld\n"
), pdhfmtCache.longValue);
OutputDebugString(szBuffer);
wsprintf(szBuffer, TEXT(
"
Working Set = %ld\n"
), pdhfmtWorking.longValue);
OutputDebugString(szBuffer);
wsprintf(szBuffer, TEXT(
"
Physical Mem = %ldMB\n"
), (pdhfmtAvail.longValue + pdhfmtCache.longValue + pdhfmtWorking.longValue) / (
1024
*
1024
));
OutputDebugString(szBuffer);
__finally
if
(hQuery != NULL)
pdhStatus = PdhCloseQuery(hQuery);
if
(pdhStatus != ERROR_SUCCESS)
}
"When I was born I was so surprised that I didn't talk for a year and a half." - Gracie Allen
Is it possible to obtain the dll module called by the specific process.
Hi DavidCrow,
i downloaded your code. it gives a compilin error sayin
The file is in the zip. I suspect you have unzipped all of the files into a single folder. If this is the case, there are 2 solutions:
It is now, but wasn't when pranay posted.
"The pointy end goes in the other man." - Antonio Banderas (Zorro, 1998)
How to get CPU usage per process? Shall be possible to calculate trough GetProcessTimes, but how?
PDH_STATUS pdhStatus;
HQUERY hQuery = NULL;
HCOUNTER hProcessorTime;
PDH_COUNTER_PATH_ELEMENTS pdhCpe;
PDH_FMT_COUNTERVALUE pdhfmtProcessorTime;
TCHAR szProcessorTime[256],
szBuffer[256];
DWORD dwBufferSize;
__try
pdhStatus = PdhOpenQuery(NULL,
0
, &hQuery);
if
(pdhStatus != ERROR_SUCCESS)
__leave;
//
make counter path
pdhCpe.szMachineName = TEXT(
"
crow-nt"
);
pdhCpe.szObjectName = TEXT(
"
Process"
);
pdhCpe.szInstanceName = TEXT(
"
Idle"
);
pdhCpe.szParentInstance = NULL;
pdhCpe.dwInstanceIndex = -1;
pdhCpe.szCounterName = TEXT(
"
% Processor Time"
);
dwBufferSize =
sizeof
(szProcessorTime);
pdhStatus = PdhMakeCounterPath(&pdhCpe, szProcessorTime, &dwBufferSize,
0
);
if
(pdhStatus != ERROR_SUCCESS)
__leave;
//
add counter
pdhStatus = PdhAddCounter(hQuery, szProcessorTime,
0
, &hProcessorTime);
if
(pdhStatus != ERROR_SUCCESS)
__leave;
//
get the data
pdhStatus = PdhCollectQueryData(hQuery);
if
(pdhStatus != ERROR_SUCCESS)
__leave;
//
format counter value
pdhStatus = PdhGetFormattedCounterValue(hProcessorTime, PDH_FMT_LONG, NULL, &pdhfmtProcessorTime);
if
(pdhStatus != ERROR_SUCCESS)
__leave;
if
(ERROR_SUCCESS == pdhStatus)
wsprintf(szBuffer, TEXT(
"
Processor Time = %ld\n"
), pdhfmtProcessorTime.longValue);
__finally
if
(hQuery != NULL)
pdhStatus = PdhCloseQuery(hQuery);
"The pointy end goes in the other man." - Antonio Banderas (Zorro, 1998)
Hello,
,
termal wrote:My program crash with Access violation after:
pdhStatus = PdhCollectQueryData(hQuery);
hQuery
valid?
"A good athlete is the result of a good and worthy opponent." - David Crow
"To have a respect for ourselves guides our morals; to have deference for others governs our manners." - Laurence Sterne
__try{
pdhStatus = PdhOpenQuery(NULL,
0
, &hQuery);
//
hQuery is not NULL
if
(pdhStatus != ERROR_SUCCESS)
__leave;
//
make counter path
pdhCpe.szMachineName = TEXT(
"
crow-nt"
);
pdhCpe.szObjectName = TEXT(
"
Process"
);
pdhCpe.szInstanceName = TEXT(
"
Idle"
);
pdhCpe.szParentInstance = NULL;
pdhCpe.dwInstanceIndex = -1;
pdhCpe.szCounterName = TEXT(
"
% Processor Time"
);
dwBufferSize =
sizeof
(szProcessorTime);
pdhStatus = PdhMakeCounterPath(&pdhCpe, szProcessorTime, &dwBufferSize,
0
);
if
(pdhStatus != ERROR_SUCCESS)
//
this is ok
__leave;
//
add counter
pdhStatus = PdhAddCounter(hQuery, szProcessorTime,
0
, &hProcessorTime);
//
on this line i see in a output window there is an exception, but program stops in next call for data
if
(pdhStatus != ERROR_SUCCESS)
//
__leave;
//
get the data
pdhStatus = PdhCollectQueryData(hQuery);
if
(pdhStatus != ERROR_SUCCESS)
//
here not, unhandled exception! :(
__leave;
......
//
follow rest of code
This is what i try to read current cpu usage
"A good athlete is the result of a good and worthy opponent." - David Crow
"To have a respect for ourselves guides our morals; to have deference for others governs our manners." - Laurence Sterne
termal wrote:here to set own machine name!?
szMachineName
.
"A good athlete is the result of a good and worthy opponent." - David Crow
"To have a respect for ourselves guides our morals; to have deference for others governs our manners." - Laurence Sterne
Airport1 wrote:
Why are you quoting
Airport1
?
"When I was born I was so surprised that I didn't talk for a year and a half." - Gracie Allen
By using above example we are getting kernel,user,creation times of process.
See my response to
Airport1
.
"The pointy end goes in the other man." - Antonio Banderas (Zorro, 1998)
I noticed that GetProcessTimes returns the same user time and kernel time if the application is idle; that is, if I have an application which has been idle for eight hours, the user time at the end of that eight hours is the same as the beginning of the eight hours. Same goes for the kernel time.
m.bergman wrote:
SendMessageTimeout(..., WM_NULL, ..., SMTO_ABORTIFHUNG, 4000, ...)
, which waits 4 seconds for the window to respond.