Home | History | Annotate | Download | only in gettimeofday
      1 /*
      2  *   This program is free software;  you can redistribute it and/or modify
      3  *   it under the terms of the GNU General Public License as published by
      4  *   the Free Software Foundation; either version 2 of the License, or
      5  *   (at your option) any later version.
      6  *
      7  *   This program is distributed in the hope that it will be useful,
      8  *   but WITHOUT ANY WARRANTY;  without even the implied warranty of
      9  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See
     10  *   the GNU General Public License for more details.
     11  *
     12  *   You should have received a copy of the GNU General Public License
     13  *   along with this program;  if not, write to the Free Software
     14  *   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
     15  */
     16 
     17 /*
     18  * NAME
     19  *	gettimeofday02.c
     20  *
     21  * DESCRIPTION
     22  *	Check if gettimeofday is monotonous
     23  *
     24  * ALGORITHM
     25  *	Call gettimeofday() to get a t1 (fist value)
     26  *	call it again to get t2, see if t2 < t1, set t2 = t1, repeat for 30 sec
     27  *
     28  * USAGE:  <for command-line>
     29  *  gettimeofday02 [-c n] [-e] [-i n] [-I x] [-P x] [-t]
     30  *     where,  -c n : Run n copies concurrently.
     31  *             -e   : Turn on errno logging.
     32  *             -i n : Execute test n times.
     33  *             -I x : Execute test for x seconds.
     34  *             -P x : Pause for x seconds between iterations.
     35  *             -t   : Turn on syscall timing.
     36  *             -T   : Seconds to test gettimeofday (default 30)
     37  *
     38  * HISTORY
     39  *	05/2002 Written by Andi Kleen
     40  *
     41  */
     42 
     43 #include <stdint.h>
     44 #include <stdio.h>
     45 #include <sys/time.h>
     46 #include <signal.h>
     47 #include <stdlib.h>
     48 #include "test.h"
     49 #include <sys/syscall.h>
     50 #include <unistd.h>
     51 #include <time.h>
     52 #include <errno.h>
     53 
     54 #define gettimeofday(a,b)  syscall(__NR_gettimeofday,a,b)
     55 
     56 char *TCID = "gettimeofday02";
     57 int TST_TOTAL = 1;
     58 
     59 int Tflag;
     60 char *tlen = "30";
     61 
     62 sig_atomic_t done;
     63 
     64 option_t opts[] = { {"T:", &Tflag, &tlen}, {} };
     65 
     66 void breakout(int sig)
     67 {
     68 	done = 1;
     69 }
     70 
     71 void cleanup(void)
     72 {
     73 }
     74 
     75 void help(void)
     76 {
     77 	printf("  -T len  seconds to test gettimeofday (default %s)\n", tlen);
     78 }
     79 
     80 int main(int ac, char **av)
     81 {
     82 	struct timeval tv1, tv2;
     83 
     84 	tst_parse_opts(ac, av, opts, help);
     85 
     86 	tst_sig(NOFORK, DEF_HANDLER, cleanup);
     87 	TEST_PAUSE;
     88 
     89 	tst_resm(TINFO, "checking if gettimeofday is monotonous, takes %ss",
     90 		 tlen);
     91 	signal(SIGALRM, breakout);
     92 	alarm(atoi(tlen));
     93 
     94 	if (gettimeofday(&tv1, NULL) != 0)
     95 		tst_brkm(TBROK, cleanup, "first gettimeofday() failed: %s\n",
     96 			 strerror(errno));
     97 	while (!done) {
     98 		if (gettimeofday(&tv2, NULL) != 0)
     99 			tst_brkm(TBROK, cleanup,
    100 				 "loop gettimeofday() failed: %s\n",
    101 				 strerror(errno));
    102 
    103 		if (tv2.tv_sec < tv1.tv_sec ||
    104 		    (tv2.tv_sec == tv1.tv_sec && tv2.tv_usec < tv1.tv_usec)) {
    105 			tst_resm(TFAIL,
    106 				 "Time is going backwards: old %jd.%jd vs new %jd.%jd!",
    107 				 (intmax_t) tv1.tv_sec, (intmax_t) tv1.tv_usec,
    108 				 (intmax_t) tv2.tv_sec, (intmax_t) tv2.tv_usec);
    109 			cleanup();
    110 			return 1;
    111 		}
    112 
    113 		tv1 = tv2;
    114 	}
    115 
    116 	tst_resm(TPASS, "gettimeofday monotonous in %s seconds", tlen);
    117 
    118 	cleanup();
    119 	tst_exit();
    120 }
    121