1/*
2* Copyright (c) 2004, Bull S.A..  All rights reserved.
3* Created by: Sebastien Decugis
4
5* This program is free software; you can redistribute it and/or modify it
6* under the terms of version 2 of the GNU General Public License as
7* published by the Free Software Foundation.
8*
9* This program is distributed in the hope that it would be useful, but
10* WITHOUT ANY WARRANTY; without even the implied warranty of
11* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
12*
13* You should have received a copy of the GNU General Public License along
14* with this program; if not, write the Free Software Foundation, Inc., 59
15* Temple Place - Suite 330, Boston MA 02111-1307, USA.
16
17
18* This sample test aims to check the following assertion:
19*
20* The CPU-time clock of the new process/ new process's thread is initialized to 0.
21
22* The steps are:
23* -> compute until the parent process CPU-time clock is greater than 1 sec.
24* -> fork
25* -> check the child process process CPU time and thread CPU time clocks.
26
27* The test fails if any of these clocks are > 1sec.
28
29*/
30
31
32/* We are testing conformance to IEEE Std 1003.1, 2003 Edition */
33#define _POSIX_C_SOURCE 200112L
34
35/********************************************************************************************/
36/****************************** standard includes *****************************************/
37/********************************************************************************************/
38#include <pthread.h>
39 #include <stdarg.h>
40 #include <stdio.h>
41 #include <stdlib.h>
42 #include <string.h>
43 #include <unistd.h>
44
45#include <sys/wait.h>
46 #include <errno.h>
47
48#include <time.h>
49/********************************************************************************************/
50/******************************   Test framework   *****************************************/
51/********************************************************************************************/
52#include "testfrmw.h"
53 #include "testfrmw.c"
54/* This header is responsible for defining the following macros:
55 * UNRESOLVED(ret, descr);
56 *    where descr is a description of the error and ret is an int (error code for example)
57 * FAILED(descr);
58 *    where descr is a short text saying why the test has failed.
59 * PASSED();
60 *    No parameter.
61 *
62 * Both three macros shall terminate the calling process.
63 * The testcase shall not terminate in any other maneer.
64 *
65 * The other file defines the functions
66 * void output_init()
67 * void output(char * string, ...)
68 *
69 * Those may be used to output information.
70 */
71
72/********************************************************************************************/
73/********************************** Configuration ******************************************/
74/********************************************************************************************/
75#ifndef VERBOSE
76#define VERBOSE 1
77#endif
78
79/********************************************************************************************/
80/***********************************    Test case   *****************************************/
81/********************************************************************************************/
82
83/* The main test function. */
84int main( int argc, char * argv[] )
85{
86	int ret, status;
87	pid_t child, ctl;
88
89	long ctp, ctt;
90	clockid_t clp, clt;
91
92	struct timespec tp;
93
94	/* Initialize output */
95	output_init();
96
97	ctp = sysconf( _SC_CPUTIME );
98	ctt = sysconf( _SC_THREAD_CPUTIME );
99
100	if ( ( ctp == -1 ) && ( ctt == -1 ) )
101	{
102		UNTESTED( "The testcase needs CPUTIME or THREAD_CPUTIME support" );
103	}
104
105#if VERBOSE > 0
106	output( "System abilities:\n" );
107
108	output( "  _POSIX_CPUTIME        : %ld\n", ctp );
109
110	output( "  _POSIX_THREAD_CPUTIME : %ld\n", ctt );
111
112#endif
113	if ( ctp > 0 )
114	{
115		ret = clock_getcpuclockid( 0, &clp );
116
117		if ( ret != 0 )
118		{
119			UNRESOLVED( ret, "Unable to get cpu-time clock id of the process" );
120		}
121
122		do
123		{
124			ret = clock_gettime( clp, &tp );
125
126			if ( ret != 0 )
127			{
128				UNRESOLVED( errno, "Failed to read CPU time clock" );
129			}
130		}
131		while ( tp.tv_sec < 1 );
132	}
133
134	if ( ctt > 0 )
135	{
136		ret = pthread_getcpuclockid( pthread_self(), &clt );
137
138		if ( ret != 0 )
139		{
140			UNRESOLVED( ret, "Unable to get cpu-time clock id of the thread" );
141		}
142
143		do
144		{
145			ret = clock_gettime( clt, &tp );
146
147			if ( ret != 0 )
148			{
149				UNRESOLVED( errno, "Failed to read thread CPU time clock" );
150			}
151		}
152		while ( tp.tv_sec < 1 );
153	}
154
155
156	/* Create the child */
157	child = fork();
158
159	if ( child == ( pid_t ) - 1 )
160	{
161		UNRESOLVED( errno, "Failed to fork" );
162	}
163
164	/* child */
165	if ( child == ( pid_t ) 0 )
166	{
167		if ( ctp > 0 )
168		{
169			ret = clock_getcpuclockid( 0, &clp );
170
171			if ( ret != 0 )
172			{
173				UNRESOLVED( ret, "Unable to get cpu-time clock id of the process" );
174			}
175
176			ret = clock_gettime( clp, &tp );
177
178			if ( ret != 0 )
179			{
180				UNRESOLVED( errno, "Failed to read CPU time clock" );
181			}
182
183			if ( tp.tv_sec > 0 )
184			{
185				FAILED( "The process CPU-time clock was not reset in child\n" );
186			}
187		}
188
189		if ( ctt > 0 )
190		{
191			ret = pthread_getcpuclockid( pthread_self(), &clt );
192
193			if ( ret != 0 )
194			{
195				UNRESOLVED( ret, "Unable to get cpu-time clock id of the thread" );
196			}
197
198			ret = clock_gettime( clt, &tp );
199
200			if ( ret != 0 )
201			{
202				UNRESOLVED( errno, "Failed to read thread CPU time clock" );
203			}
204
205			if ( tp.tv_sec > 0 )
206			{
207				FAILED( "The thread CPU-time clock was not reset in child\n" );
208			}
209		}
210
211		/* We're done */
212		exit( PTS_PASS );
213	}
214
215	/* Parent joins the child */
216	ctl = waitpid( child, &status, 0 );
217
218	if ( ctl != child )
219	{
220		UNRESOLVED( errno, "Waitpid returned the wrong PID" );
221	}
222
223	if ( ( !WIFEXITED( status ) ) || ( WEXITSTATUS( status ) != PTS_PASS ) )
224	{
225		FAILED( "Child exited abnormally" );
226	}
227
228
229	/* Test passed */
230#if VERBOSE > 0
231	output( "Test passed\n" );
232
233#endif
234	PASSED;
235}
236
237