Home | History | Annotate | Download | only in fork
      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.,
     15 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
     16 
     17 * This sample test aims to check the following assertion:
     18 *
     19 * The CPU-time clock of the new process/ new process's thread is initialized to 0.
     20 
     21 * The steps are:
     22 * -> compute until the parent process CPU-time clock is greater than 1 sec.
     23 * -> fork
     24 * -> check the child process process CPU time and thread CPU time clocks.
     25 
     26 * The test fails if any of these clocks are > 1sec.
     27 
     28 */
     29 
     30 #include <pthread.h>
     31 #include <stdarg.h>
     32 #include <stdio.h>
     33 #include <stdlib.h>
     34 #include <string.h>
     35 #include <unistd.h>
     36 
     37 #include <sys/wait.h>
     38 #include <errno.h>
     39 
     40 #include <time.h>
     41 
     42 #include "../testfrmw/testfrmw.h"
     43 #include "../testfrmw/testfrmw.c"
     44 
     45 #ifndef VERBOSE
     46 #define VERBOSE 1
     47 #endif
     48 
     49 int main(void)
     50 {
     51 	int ret, status;
     52 	pid_t child, ctl;
     53 
     54 	long ctp, ctt;
     55 	clockid_t clp, clt;
     56 
     57 	struct timespec tp;
     58 
     59 	output_init();
     60 
     61 	ctp = sysconf(_SC_CPUTIME);
     62 	ctt = sysconf(_SC_THREAD_CPUTIME);
     63 
     64 	if ((ctp == -1) && (ctt == -1)) {
     65 		UNTESTED
     66 		    ("The testcase needs CPUTIME or THREAD_CPUTIME support");
     67 	}
     68 #if VERBOSE > 0
     69 	output("System abilities:\n");
     70 
     71 	output("  _POSIX_CPUTIME        : %ld\n", ctp);
     72 
     73 	output("  _POSIX_THREAD_CPUTIME : %ld\n", ctt);
     74 
     75 #endif
     76 	if (ctp > 0) {
     77 		ret = clock_getcpuclockid(0, &clp);
     78 
     79 		if (ret != 0) {
     80 			UNRESOLVED(ret,
     81 				   "Unable to get cpu-time clock id of the process");
     82 		}
     83 
     84 		do {
     85 			ret = clock_gettime(clp, &tp);
     86 
     87 			if (ret != 0) {
     88 				UNRESOLVED(errno,
     89 					   "Failed to read CPU time clock");
     90 			}
     91 		}
     92 		while (tp.tv_sec < 1);
     93 	}
     94 
     95 	if (ctt > 0) {
     96 		ret = pthread_getcpuclockid(pthread_self(), &clt);
     97 
     98 		if (ret != 0) {
     99 			UNRESOLVED(ret,
    100 				   "Unable to get cpu-time clock id of the thread");
    101 		}
    102 
    103 		do {
    104 			ret = clock_gettime(clt, &tp);
    105 
    106 			if (ret != 0) {
    107 				UNRESOLVED(errno,
    108 					   "Failed to read thread CPU time clock");
    109 			}
    110 		}
    111 		while (tp.tv_sec < 1);
    112 	}
    113 
    114 	/* Create the child */
    115 	child = fork();
    116 
    117 	if (child == -1) {
    118 		UNRESOLVED(errno, "Failed to fork");
    119 	}
    120 
    121 	/* child */
    122 	if (child == 0) {
    123 		if (ctp > 0) {
    124 			ret = clock_getcpuclockid(0, &clp);
    125 
    126 			if (ret != 0) {
    127 				UNRESOLVED(ret,
    128 					   "Unable to get cpu-time clock id of the process");
    129 			}
    130 
    131 			ret = clock_gettime(clp, &tp);
    132 
    133 			if (ret != 0) {
    134 				UNRESOLVED(errno,
    135 					   "Failed to read CPU time clock");
    136 			}
    137 
    138 			if (tp.tv_sec > 0) {
    139 				FAILED
    140 				    ("The process CPU-time clock was not reset in child\n");
    141 			}
    142 		}
    143 
    144 		if (ctt > 0) {
    145 			ret = pthread_getcpuclockid(pthread_self(), &clt);
    146 
    147 			if (ret != 0) {
    148 				UNRESOLVED(ret,
    149 					   "Unable to get cpu-time clock id of the thread");
    150 			}
    151 
    152 			ret = clock_gettime(clt, &tp);
    153 
    154 			if (ret != 0) {
    155 				UNRESOLVED(errno,
    156 					   "Failed to read thread CPU time clock");
    157 			}
    158 
    159 			if (tp.tv_sec > 0) {
    160 				FAILED
    161 				    ("The thread CPU-time clock was not reset in child\n");
    162 			}
    163 		}
    164 
    165 		exit(PTS_PASS);
    166 	}
    167 
    168 	/* Parent joins the child */
    169 	ctl = waitpid(child, &status, 0);
    170 
    171 	if (ctl != child) {
    172 		UNRESOLVED(errno, "Waitpid returned the wrong PID");
    173 	}
    174 
    175 	if (!WIFEXITED(status) || (WEXITSTATUS(status) != PTS_PASS)) {
    176 		FAILED("Child exited abnormally");
    177 	}
    178 
    179 #if VERBOSE > 0
    180 	output("Test passed\n");
    181 #endif
    182 	PASSED;
    183 }
    184