| 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 GetThreadTimes() function |
| 12 | ** |
| 13 | ** |
| 14 | **=========================================================*/ |
| 15 | |
| 16 | #include <palsuite.h> |
| 17 | |
| 18 | int __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 | FILETIME kernelTime1, userTime1, kernelTime2, userTime2; |
| 28 | /* Delta = .01 sec */ |
| 29 | LONG64 Actual, Expected, Delta = 850000000; |
| 30 | Actual = 0; |
| 31 | Expected = 0; |
| 32 | const ULONG64 MSEC_TO_NSEC = 1000000; |
| 33 | |
| 34 | /* |
| 35 | * Initialize the PAL and return FAILURE if this fails |
| 36 | */ |
| 37 | |
| 38 | if(0 != (PAL_Initialize(argc, argv))) |
| 39 | { |
| 40 | return FAIL; |
| 41 | } |
| 42 | |
| 43 | HANDLE cThread = GetCurrentThread(); |
| 44 | |
| 45 | int i; |
| 46 | /* Take 2000 tiny measurements */ |
| 47 | for (i = 0; i < 2000; i++){ |
| 48 | ULONG64 Time1, Time2; |
| 49 | |
| 50 | Sleep(1); |
| 51 | |
| 52 | /* Grab a FirstCount, then loop for a bit to make the clock increase */ |
| 53 | if (!GetThreadTimes(cThread, NULL, NULL, &kernelTime1, &userTime1)) |
| 54 | { |
| 55 | Fail("ERROR: GetThreadTimes returned failure.\n" ); |
| 56 | } |
| 57 | LONG64 x, Init; |
| 58 | /* Init is in milliseconds, so we will convert later */ |
| 59 | Init = (ULONG64)GetTickCount(); |
| 60 | /* Spin for < 1 Quantum so we don't get interrupted */ |
| 61 | x = Init + 3; |
| 62 | volatile int counter; |
| 63 | do { |
| 64 | for (counter = 0; counter < 100000; counter++) |
| 65 | { |
| 66 | // spin to consume CPU time |
| 67 | } |
| 68 | |
| 69 | } while (x > GetTickCount()); |
| 70 | Expected += (GetTickCount() - Init) * MSEC_TO_NSEC; |
| 71 | /* Get a second count */ |
| 72 | if (!GetThreadTimes(cThread, NULL, NULL, &kernelTime2, &userTime2)) |
| 73 | { |
| 74 | Fail("ERROR: GetThreadTimes returned failure.\n" ); |
| 75 | } |
| 76 | |
| 77 | Time1 = ((ULONG64)kernelTime1.dwHighDateTime << 32); |
| 78 | Time1 += (ULONG64)kernelTime1.dwLowDateTime; |
| 79 | Time1 += ((ULONG64)userTime1.dwHighDateTime << 32); |
| 80 | Time1 += (ULONG64)userTime1.dwLowDateTime; |
| 81 | |
| 82 | Time2 = ((ULONG64)kernelTime2.dwHighDateTime << 32); |
| 83 | Time2 += (ULONG64)kernelTime2.dwLowDateTime; |
| 84 | Time2 += ((ULONG64)userTime2.dwHighDateTime << 32); |
| 85 | Time2 += (ULONG64)userTime2.dwLowDateTime; |
| 86 | |
| 87 | Actual += (Time2 - Time1) * 100; |
| 88 | } |
| 89 | |
| 90 | if(labs(Expected - Actual) > Delta) |
| 91 | { |
| 92 | Fail("ERROR: The measured time (%llu millisecs) was not within Delta %llu " |
| 93 | "of the expected time (%llu millisecs).\n" , |
| 94 | (Actual / MSEC_TO_NSEC), (Delta / MSEC_TO_NSEC), (Expected / MSEC_TO_NSEC)); |
| 95 | } |
| 96 | //printf("%llu, %llu\n", Expected, Actual); |
| 97 | PAL_Terminate(); |
| 98 | ret = PASS; |
| 99 | } |
| 100 | EXIT: |
| 101 | return ret; |
| 102 | } |
| 103 | |