472,796 Members | 2,258 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,796 software developers and data experts.

Bug of performance counter API or perfmon tool

Hello everyone,
I found using performance counter API and using perfmon counter will
result in different numeric result. Here is an example, and in the
example code, virtual bytes is always larger than working set, but in
perfmon, working set is always larger than working set.

Any ideas?

Expand|Select|Wrap|Line Numbers
  1. #include <windows.h>
  2. #include <stdio.h>
  3. #include <pdh.h>
  4. #include <atltime.h>
  5.  
  6. #pragma comment(lib, "pdh")
  7.  
  8. PDH_STATUS ps;
  9. CHAR wsPath[256];
  10. CHAR vbPath[256];
  11. DWORD wsItems = 0;
  12. DWORD wsSize  = 0;
  13. DWORD vbItems = 0;
  14. DWORD vbSize  = 0;
  15.  
  16. FILE* fLog = fopen("perfcount.log", "w+");
  17.  
  18. CHAR processModule[MAX_PATH] = {0};
  19.  
  20. CHAR* GetProcModuleName()
  21. {
  22. if (GetModuleFileName(GetModuleHandle(NULL), processModule,
  23. sizeof(processModule)))
  24. {
  25. CHAR* p = strrchr(processModule, '.');
  26. if (p)
  27. *p = '\0';
  28. else
  29. return NULL;
  30. p = strrchr(processModule, '\\');
  31. if (p)
  32. return (p + 1);
  33. }
  34.  
  35. return NULL;
  36. }
  37.  
  38. void GetCounters(DWORD sectionIndex)
  39. {
  40. HQUERY hQuery;
  41. HCOUNTER hWsCount;
  42. HCOUNTER hVbCount;
  43.  
  44. ps = PdhOpenQuery(NULL, 0, &hQuery);
  45. ps = PdhAddCounter(hQuery, wsPath, 0, &hWsCount);
  46. ps = PdhAddCounter(hQuery, vbPath, 0, &hVbCount);
  47.  
  48. ps = PdhCollectQueryData(hQuery);
  49.  
  50. wsItems = 0;
  51. wsSize  = 0;
  52. vbItems = 0;
  53. vbSize  = 0;
  54. PDH_RAW_COUNTER_ITEM* pWS = NULL;
  55. PDH_RAW_COUNTER_ITEM* pVB = NULL;
  56.  
  57. ps = PdhGetRawCounterArray(hWsCount, &wsSize, &wsItems, NULL);
  58. pWS = (PDH_RAW_COUNTER_ITEM*)LocalAlloc(LPTR, wsSize);
  59. ps = PdhGetRawCounterArray(hVbCount, &vbSize, &vbItems, NULL);
  60. pVB = (PDH_RAW_COUNTER_ITEM*)LocalAlloc(LPTR, vbSize);
  61. ps = PdhGetRawCounterArray(hWsCount, &wsSize, &wsItems, pWS);
  62. ps = PdhGetRawCounterArray(hVbCount, &vbSize, &vbItems, pVB);
  63. ps = PdhCloseQuery(hQuery);
  64.  
  65. CTime t = pWS->RawValue.TimeStamp;
  66. fprintf(fLog, "[%s] SI: %6d\t WS: %.2f Mb\tVB: %.2f Mb\n",
  67. t.Format("%c"),
  68. sectionIndex,
  69. double(pWS->RawValue.FirstValue) /1024 /1024,
  70. double(pVB->RawValue.FirstValue) /1024 /1024);
  71. LocalFree(pWS);
  72. LocalFree(pVB);
  73. fflush(fLog);
  74. }
  75.  
  76.  
  77. int main(int argc, char* argv[])
  78. {
  79. LARGE_INTEGER start,end;
  80. LARGE_INTEGER freq;
  81. QueryPerformanceCounter(&start);
  82. QueryPerformanceFrequency(&freq);
  83.  
  84. MEMORYSTATUS memstat;
  85. void** map;
  86. int sectionIndex = 0;
  87. memstat.dwLength = sizeof(memstat);
  88. GlobalMemoryStatus(&memstat);
  89.  
  90. // basic file mapping test (512 MB)
  91. long long size = 512*1024*1024;
  92.  
  93.  
  94. DWORD nsize = 0;
  95. CHAR poName[256];
  96. CHAR wsName[256];
  97. CHAR vbName[256];
  98.  
  99. nsize = sizeof(poName);
  100. ps = PdhLookupPerfNameByIndex(NULL, 230, poName, &nsize);  // Process
  101. object
  102. nsize = sizeof(wsName);
  103. ps = PdhLookupPerfNameByIndex(NULL, 180, wsName, &nsize);  // Working
  104. Set counter
  105. nsize = sizeof(vbName);
  106. ps = PdhLookupPerfNameByIndex(NULL, 174, vbName, &nsize);  // Virtual
  107. Bytes counter
  108.  
  109. PDH_COUNTER_PATH_ELEMENTS pcpe = {0};
  110. pcpe.szObjectName = poName;
  111. pcpe.szInstanceName = GetProcModuleName();
  112.  
  113. pcpe.szCounterName = wsName;
  114. nsize = sizeof(wsPath);
  115. ps = PdhMakeCounterPath(&pcpe, wsPath, &nsize, 0);
  116.  
  117. pcpe.szCounterName = vbName;
  118. nsize = sizeof(vbPath);
  119. ps = PdhMakeCounterPath(&pcpe, vbPath, &nsize, 0);
  120.  
  121. HANDLE mapping =
  122. CreateFileMapping(INVALID_HANDLE_VALUE,NULL,PAGE_READWRITE|SEC_COMMIT,
  123. (DWORD)(size>>32),DWORD(size),NULL);
  124. if (mapping)
  125. {
  126. // create and destroy temporary views
  127. SYSTEM_INFO sysInfo;
  128. GetSystemInfo(&sysInfo);
  129. const int allocSize = sysInfo.dwAllocationGranularity;
  130.  
  131. GlobalMemoryStatus(&memstat);
  132.  
  133. void *mem = new char[allocSize];
  134. memset(mem,0x11,allocSize);
  135.  
  136. map = (void**) new char [sizeof(void*) * size / allocSize];
  137.  
  138. for (int i=0; i<1; i++)
  139. {
  140.  
  141. sectionIndex = 0;
  142. for (long long offset=0; offset<=size-allocSize; offset
  143. +=allocSize)
  144. {
  145. map [sectionIndex] =
  146. MapViewOfFile(mapping,FILE_MAP_WRITE,(DWORD)(offset>>32),
  147. (DWORD)offset,allocSize);
  148. if (map [sectionIndex])
  149. {
  150. memcpy(map [sectionIndex],mem,allocSize);
  151.  
  152. GetCounters(sectionIndex);
  153.  
  154. // UnmapViewOfFile(map);
  155. }
  156.  
  157. sectionIndex++;
  158. } // for (long long offset=0; offset<=size-allocSize; offset
  159. +=allocSize)
  160.  
  161. // close mapped files to avoid leak
  162. for (sectionIndex = 0; sectionIndex < size/allocSize; sectionIndex+
  163. +)
  164. {
  165. if (map [sectionIndex])
  166. {
  167. UnmapViewOfFile(map [sectionIndex]);
  168. }
  169. }
  170.  
  171. GlobalMemoryStatus(&memstat);
  172.  
  173. sectionIndex = 0;
  174. for (long long offset=0; offset<=size-allocSize; offset
  175. +=allocSize)
  176. {
  177. map [sectionIndex] =
  178. MapViewOfFile(mapping,FILE_MAP_READ,(DWORD)(offset>>32),
  179. (DWORD)offset,allocSize);
  180. if (map [sectionIndex])
  181. {
  182. for (int t=0; t<allocSize; t++)
  183. {
  184. if (((char *)(map [sectionIndex]))[t]!=0x11)
  185. {
  186. OutputDebugString("Memory read failed\n");
  187. }
  188. }
  189.  
  190. GetCounters(sectionIndex);
  191. }
  192.  
  193. UnmapViewOfFile(map [sectionIndex]);
  194. sectionIndex++;
  195. }
  196.  
  197. // close mapped files to avoid leak
  198. /*
  199. for (sectionIndex = 0; sectionIndex < size/allocSize; sectionIndex+
  200. +)
  201. {
  202. if (map [sectionIndex])
  203. {
  204. UnmapViewOfFile(map [sectionIndex]);
  205. }
  206. }
  207. */
  208.  
  209. GlobalMemoryStatus(&memstat);
  210. } // for (int i=0; i<10; i++)
  211.  
  212. QueryPerformanceCounter(&end);
  213.  
  214. GlobalMemoryStatus(&memstat);
  215.  
  216. printf("Time %.3f\n",
  217. double(end.QuadPart-start.QuadPart)/double(freq.QuadPart));
  218. CloseHandle(mapping);
  219. delete[] mem;
  220. GlobalMemoryStatus(&memstat);
  221. } //if (mapping)
  222.  
  223. fclose(fLog);
  224.  
  225. return 0;
  226. }
  227.  

thanks in advance,
George
Jan 25 '08 #1
0 1902

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: william | last post by:
Hi, I'm playing windows service using vb.net. I added a performance counter to my windows service, but I couldn't set MachineName property of the performance counter to blank. After I set it to...
0
by: Val Mazur (MVP) | last post by:
Hi guys, My application creates custom performance counter and it works fine. Now I am creating multiple instances of the same counter and increment value for each instance. This part work fine...
2
by: Marc Melancon | last post by:
Will the next release of SQL Server 2000 64bit sp provide performance counter? MarcM
0
by: jimiz | last post by:
I have a question on how the performance counter instance returns values. I have an app that uses the performance counter for Category name = "Web Service" Counter name = "Current Connections"...
0
by: Salvador | last post by:
Hi, I have created a service, which installer installs 5 performance counters as well. Then the service instances these counters and interact with them. If I check the event viewer I receive...
1
by: jvn | last post by:
I am experiencing a particular problem with performance counters. I have created a set of classes, that uses System.Diagnostics.PerformanceCounter to increment custom performance counters (using...
1
by: JSha | last post by:
Hello, The application I am using works on most machines except one where in it throws the following exception... Error: Process Performance Counter is Disabled, so requested operation cannot...
0
by: JSha | last post by:
Hello, The application I am using works on most machines except one where in it throws the following exception... Error: Process Performance Counter is Disabled, so requested operation cannot...
6
by: sony.m.2007 | last post by:
Hi, I'm using VSTS2008. I tried to create a performance counter as below. A simple example of monitoring processor usage (similar to what's seen in task manager): Code Snippet
0
by: erikbower65 | last post by:
Using CodiumAI's pr-agent is simple and powerful. Follow these steps: 1. Install CodiumAI CLI: Ensure Node.js is installed, then run 'npm install -g codiumai' in the terminal. 2. Connect to...
0
linyimin
by: linyimin | last post by:
Spring Startup Analyzer generates an interactive Spring application startup report that lets you understand what contributes to the application startup time and helps to optimize it. Support for...
0
by: kcodez | last post by:
As a H5 game development enthusiast, I recently wrote a very interesting little game - Toy Claw ((http://claw.kjeek.com/))。Here I will summarize and share the development experience here, and hope it...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Sept 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
14
DJRhino1175
by: DJRhino1175 | last post by:
When I run this code I get an error, its Run-time error# 424 Object required...This is my first attempt at doing something like this. I test the entire code and it worked until I added this - If...
0
by: Rina0 | last post by:
I am looking for a Python code to find the longest common subsequence of two strings. I found this blog post that describes the length of longest common subsequence problem and provides a solution in...
5
by: DJRhino | last post by:
Private Sub CboDrawingID_BeforeUpdate(Cancel As Integer) If = 310029923 Or 310030138 Or 310030152 Or 310030346 Or 310030348 Or _ 310030356 Or 310030359 Or 310030362 Or...
0
by: lllomh | last post by:
Define the method first this.state = { buttonBackgroundColor: 'green', isBlinking: false, // A new status is added to identify whether the button is blinking or not } autoStart=()=>{
0
by: Mushico | last post by:
How to calculate date of retirement from date of birth

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.