Home | History | Annotate | Download | only in tests-m32
      1 /*
      2  * Copyright (c) 2015 Eugene Syromyatnikov <evgsyr (at) gmail.com>
      3  * Copyright (c) 2015-2016 Dmitry V. Levin <ldv (at) altlinux.org>
      4  * All rights reserved.
      5  *
      6  * Redistribution and use in source and binary forms, with or without
      7  * modification, are permitted provided that the following conditions
      8  * are met:
      9  * 1. Redistributions of source code must retain the above copyright
     10  *    notice, this list of conditions and the following disclaimer.
     11  * 2. Redistributions in binary form must reproduce the above copyright
     12  *    notice, this list of conditions and the following disclaimer in the
     13  *    documentation and/or other materials provided with the distribution.
     14  * 3. The name of the author may not be used to endorse or promote products
     15  *    derived from this software without specific prior written permission.
     16  *
     17  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     18  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     19  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     20  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     21  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     22  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     23  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     24  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     25  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     26  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     27  */
     28 
     29 /**
     30  * @file
     31  * This test burns some CPU cycles in user space and kernel space in order to
     32  * get some non-zero values returned by times(2).
     33  */
     34 
     35 #include "tests.h"
     36 #include <sched.h>
     37 #include <stdio.h>
     38 #include <time.h>
     39 #include <unistd.h>
     40 
     41 #include <asm/unistd.h>
     42 #include <sys/times.h>
     43 #include <sys/wait.h>
     44 
     45 enum {
     46 	NUM_USER_ITERS = 1000000,
     47 	PARENT_CPUTIME_LIMIT_NSEC = 200000000,
     48 	CHILD_CPUTIME_LIMIT_NSEC = 300000000
     49 };
     50 
     51 int
     52 main (void)
     53 {
     54 	struct timespec ts;
     55 	volatile int dummy = 0;
     56 	int i = 0;
     57 
     58 	pid_t pid = fork();
     59 	if (pid < 0)
     60 		perror_msg_and_fail("fork");
     61 
     62 	const long cputime_limit =
     63 		pid ? PARENT_CPUTIME_LIMIT_NSEC : CHILD_CPUTIME_LIMIT_NSEC;
     64 
     65 	/* Enjoying my user time */
     66 	while (clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &ts) == 0) {
     67 		if (ts.tv_sec || ts.tv_nsec >= cputime_limit)
     68 			break;
     69 
     70 		if (i && !(ts.tv_sec || ts.tv_nsec))
     71 			error_msg_and_skip("clock_gettime(CLOCK_PROCESS_CPUTIME_ID, {0, 0})");
     72 
     73 		for (i = 0; i < NUM_USER_ITERS; ++i)
     74 			++dummy;
     75 	}
     76 
     77 	/* Enjoying my system time */
     78 	while (clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &ts) == 0) {
     79 		if (ts.tv_sec || ts.tv_nsec >= cputime_limit * 2)
     80 			break;
     81 
     82 		sched_yield();
     83 	}
     84 
     85 	if (pid == 0) {
     86 		return 0;
     87 	} else {
     88 		wait(NULL);
     89 	}
     90 
     91 	struct tms tbuf;
     92 	unsigned long long llres;
     93 
     94 	/*
     95 	 * On systems where user's and kernel's long types are the same,
     96 	 * prefer direct times syscall over libc's times function because
     97 	 * the latter is more prone to return value truncation.
     98 	 */
     99 #undef USE_LIBC_SYSCALL
    100 #if defined __NR_times && \
    101    !defined(LINUX_MIPSN32) && \
    102    !(defined __x86_64__ && defined __ILP32__)
    103 # define USE_LIBC_SYSCALL 1
    104 #endif
    105 
    106 #if defined USE_LIBC_SYSCALL
    107 	long res = syscall(__NR_times, &tbuf);
    108 
    109 	if (-1L == res)
    110 		perror_msg_and_skip("times");
    111 	else
    112 		llres = (unsigned long) res;
    113 #elif defined __NR_times && defined __x86_64__ && defined __ILP32__
    114 	register long arg asm("rdi") = (long) &tbuf;
    115 	asm volatile("syscall\n\t"
    116 		     : "=a"(llres)
    117 		     : "0"(__NR_times), "r"(arg)
    118 		     : "memory", "cc", "r11", "cx");
    119 	if (llres > 0xfffffffffffff000)
    120 		return 77;
    121 #else
    122 	clock_t res = times(&tbuf);
    123 
    124 	if ((clock_t) -1 == res)
    125 		perror_msg_and_skip("times");
    126 	if (sizeof(res) < sizeof(unsigned long long))
    127 		llres = (unsigned long) res;
    128 	else
    129 		llres = res;
    130 #endif
    131 
    132 	printf("times({tms_utime=%llu, tms_stime=%llu, ",
    133 		(unsigned long long) tbuf.tms_utime,
    134 		(unsigned long long) tbuf.tms_stime);
    135 	printf("tms_cutime=%llu, tms_cstime=%llu}) = %llu\n",
    136 		(unsigned long long) tbuf.tms_cutime,
    137 		(unsigned long long) tbuf.tms_cstime,
    138 		llres);
    139 	puts("+++ exited with 0 +++");
    140 
    141 	return 0;
    142 }
    143