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: test2.c |
8 | ** |
9 | ** Purpose: Test to ensure GetProcessTimes works properly. |
10 | ** |
11 | ** Dependencies: PAL_Initialize |
12 | ** PAL_Terminate |
13 | ** Fail |
14 | ** ZeroMemory |
15 | ** CompareFileTime |
16 | ** GetLastError |
17 | ** |
18 | |
19 | ** |
20 | **===========================================================================*/ |
21 | #include <palsuite.h> |
22 | |
23 | |
24 | int __cdecl main( int argc, char **argv ) |
25 | |
26 | { |
27 | int i, j, k; |
28 | int *total; |
29 | |
30 | HANDLE hProcess; |
31 | FILETIME createTime; |
32 | FILETIME exitTime; |
33 | FILETIME kernelTime1; |
34 | FILETIME userTime1; |
35 | FILETIME kernelTime2; |
36 | FILETIME userTime2; |
37 | |
38 | DWORD dwError; |
39 | |
40 | /* initialize the PAL */ |
41 | if( PAL_Initialize(argc, argv) != 0 ) |
42 | { |
43 | return( FAIL ); |
44 | } |
45 | |
46 | /* get our own process handle */ |
47 | hProcess = GetCurrentProcess(); |
48 | if( hProcess == NULL ) |
49 | { |
50 | Fail( "GetCurrentProcess() returned a NULL handle.\n" ); |
51 | } |
52 | |
53 | /* zero our time structures */ |
54 | ZeroMemory( &createTime, sizeof(createTime) ); |
55 | ZeroMemory( &exitTime, sizeof(exitTime) ); |
56 | ZeroMemory( &kernelTime1, sizeof(kernelTime1) ); |
57 | ZeroMemory( &userTime1, sizeof(userTime1) ); |
58 | ZeroMemory( &kernelTime2, sizeof(kernelTime2) ); |
59 | ZeroMemory( &userTime2, sizeof(userTime2) ); |
60 | |
61 | /* check the process times for the child process */ |
62 | if( ! GetProcessTimes( hProcess, |
63 | &createTime, |
64 | &exitTime, |
65 | &kernelTime1, |
66 | &userTime1 ) ) |
67 | { |
68 | dwError = GetLastError(); |
69 | Fail( "GetProcessTimes() call failed with error code %d\n" , |
70 | dwError ); |
71 | } |
72 | |
73 | |
74 | /* simulate some activity */ |
75 | for( i=0; i<1000; i++ ) |
76 | { |
77 | for( j=0; j<1000; j++ ) |
78 | { |
79 | /* do kernel work to increase system usage counters */ |
80 | total = (int*)malloc(1024 * 1024); |
81 | |
82 | *total = j * i; |
83 | for( k=0; k<1000; k++ ) |
84 | { |
85 | *total += k + i; |
86 | } |
87 | |
88 | free(total); |
89 | } |
90 | } |
91 | |
92 | /* check the process times for the child process */ |
93 | if( ! GetProcessTimes( hProcess, |
94 | &createTime, |
95 | &exitTime, |
96 | &kernelTime2, |
97 | &userTime2 ) ) |
98 | { |
99 | dwError = GetLastError(); |
100 | Fail( "GetProcessTimes() call failed with error code %d\n" , |
101 | dwError ); |
102 | } |
103 | |
104 | |
105 | /* very simple logical checking of the results */ |
106 | if( CompareFileTime( &kernelTime1, &kernelTime2 ) > 0 ) |
107 | { |
108 | Fail( "Unexpected kernel time value reported.\n" ); |
109 | } |
110 | |
111 | if( CompareFileTime( &userTime1, &userTime2 ) > 0 ) |
112 | { |
113 | Fail( "Unexpected user time value reported.\n" ); |
114 | } |
115 | |
116 | |
117 | /* terminate the PAL */ |
118 | PAL_Terminate(); |
119 | |
120 | /* return success */ |
121 | return PASS; |
122 | } |
123 | |