VirtualBox

Ticket #12076: QPC_Check.cpp

File QPC_Check.cpp, 2.9 KB (added by asdel, 12 years ago)

Source for Windows app demonstrating QueryPerformanceCounter anomaly

Line 
1// QPC_Check.cpp : Defines the entry point for the console application.
2//
3#include "stdafx.h"
4#include <stdio.h>
5#include <conio.h> // _kbhit()
6#include <windows.h> // Sleep()
7
8// This constant is used to check for forward jumps of the performance counter
9// It is dependent upon the frequency of the counter (determined via
10// QueryPerformanceFrequency() -- not used in this test app for brevity).
11#define FWD_LIMIT (0x1000000)
12
13int _tmain(int argc, _TCHAR* argv[])
14{
15 BOOL bDone = FALSE ;
16 char cUserInput ;
17 LARGE_INTEGER liLastCount, liNewCount ;
18 int nBackEventTally = 0 ;
19 int nFwdEventTally = 0 ;
20
21 (void)printf("QPC Check - v1.0 Hit 'c' to display current count, 'x' to exit\n") ;
22
23 // Read counter to prime the LastCount value
24 if (TRUE != QueryPerformanceCounter ( &liLastCount ))
25 {
26 printf("QPC read error\n") ;
27 bDone = TRUE ;
28 }
29
30 while (FALSE == bDone)
31 {
32 // Respond to user input, if any
33 if (0 != _kbhit()) // _kbhit() returns 0 or "non-zero"
34 {
35 // Get the key
36 cUserInput = _getch() ;
37
38 if (('x' == cUserInput) || ('X' == cUserInput))
39 {
40 printf("Exiting QPC_Check\n") ;
41 bDone = TRUE ;
42 }
43 // Print status and last count
44 else if (('c' == cUserInput) || ('C' == cUserInput))
45 {
46 printf("Backward events: %d Forward events: %d", nBackEventTally, nFwdEventTally ) ;
47 printf(" Last Count = 0x%08x:%08x\n", liLastCount.u.HighPart, liLastCount.u.LowPart ) ;
48 }
49 else if ('b' == cUserInput)
50 {
51 printf("Testing backward event...\n") ;
52 liLastCount.QuadPart += FWD_LIMIT ;
53 }
54 else if ('f' == cUserInput)
55 {
56 printf("Testing forward event...\n") ;
57 liLastCount.QuadPart -= (FWD_LIMIT + 1);
58 }
59 }
60
61 // Read & check counter
62 if (TRUE == QueryPerformanceCounter ( &liNewCount ))
63 {
64 // Check for new < last
65 if (liNewCount.QuadPart < liLastCount.QuadPart)
66 {
67 nBackEventTally++ ;
68 printf("Jump backward event: Last = 0x%08x:%08x, New = 0x%08x:%08x\n",
69 liLastCount.u.HighPart, liLastCount.u.LowPart,
70 liNewCount.u.HighPart, liNewCount.u.LowPart ) ;
71 }
72
73 // Check for new >> last
74 if (liNewCount.QuadPart > (liLastCount.QuadPart + FWD_LIMIT))
75 {
76 nFwdEventTally++ ;
77 printf("Jump forward event: Last = 0x%08x:%08x, New = 0x%08x:%08x\n",
78 liLastCount.u.HighPart, liLastCount.u.LowPart,
79 liNewCount.u.HighPart, liNewCount.u.LowPart ) ;
80 }
81
82 // Update LastCount with new value
83 liLastCount.QuadPart = liNewCount.QuadPart ;
84 }
85 else
86 {
87 printf("QPC read error\n" ) ;
88 bDone = TRUE ;
89 }
90 Sleep( 50 ) ; // wait 50 millisecs then check counter again
91 }
92
93 return 0;
94}
95

© 2025 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette