470,571 Members | 2,358 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 470,571 developers. It's quick & easy.

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 1791

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

2 posts views Thread by Marc Melancon | last post: by
reply views Thread by jimiz | last post: by
reply views Thread by Salvador | last post: by
6 posts views Thread by sony.m.2007 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.