1// Licensed to the .NET Foundation under one or more agreements.
2// The .NET Foundation licenses this file to you under the MIT license.
3// See the LICENSE file in the project root for more information.
4
5/*============================================================
6**
7** Source:
8**
9** Source : test1.c
10**
11** Purpose: Test for QueryThreadCycleTime() function
12**
13**
14**=========================================================*/
15
16#include <palsuite.h>
17
18int __cdecl main(int argc, char *argv[]) {
19 int ret = FAIL;
20
21 //Test is failing unreliably, so for now we always return pass.
22 if (TRUE){
23 ret = PASS;
24 goto EXIT;
25 }
26 {
27 LONG64 Actual, Expected, Delta = 850000000;
28 Actual = 0;
29 Expected = 0;
30 const LONG64 MSEC_TO_NSEC = 1000000;
31
32 /*
33 * Initialize the PAL and return FAILURE if this fails
34 */
35
36 if(0 != (PAL_Initialize(argc, argv)))
37 {
38 return FAIL;
39 }
40
41 HANDLE cThread = GetCurrentThread();
42
43 int i;
44 /* Take 2000 tiny measurements */
45 for (i = 0; i < 2000; i++){
46 ULONG64 FirstCount, SecondCount;
47 LONG64 Init;
48
49 Sleep(1);
50
51 /* Grab a FirstCount, then loop for a bit to make the clock increase */
52 if (!QueryThreadCycleTime(cThread, (PULONG64)&FirstCount))
53 {
54 Fail("ERROR: QueryThreadCycleTime returned failure.\n");
55 }
56
57 LONG64 x;
58 /* Init is in milliseconds, so we will convert later */
59 Init = (LONG64)GetTickCount();
60 x = Init + 3;
61 volatile int counter;
62 do {
63 for (counter = 0; counter < 100000; counter++)
64 {
65 // spin to consume CPU time
66 }
67
68 } while (x > GetTickCount());
69 Expected += (GetTickCount() - Init) * MSEC_TO_NSEC;
70 /* Get a second count */
71 if (!QueryThreadCycleTime(cThread, (PULONG64)&SecondCount))
72 {
73 Fail("ERROR: QueryThreadCycleTime returned failure.\n");
74 }
75
76 LONG64 trial = (LONG64)SecondCount - (LONG64)FirstCount;
77 if (trial < 0){
78 printf("Negative value %llu measured", trial);
79 }
80 Actual += (trial);
81
82 }
83
84
85
86 if(labs(Expected - Actual) > Delta)
87 {
88 Fail("ERROR: The measured time (%llu millisecs) was not within Delta %llu "
89 "of the expected time (%llu millisecs).\n",
90 (Actual / MSEC_TO_NSEC), (Delta / MSEC_TO_NSEC), (Expected / MSEC_TO_NSEC));
91 }
92 //printf("%llu, %llu\n", Expected, Actual);
93 PAL_Terminate();
94 ret = PASS;
95 }
96EXIT:
97 return ret;
98}
99