Home | History | Annotate | Download | only in lib
      1 /*
      2  * Copyright (C) 2015 Cyril Hrubis <chrubis (at) suse.cz>
      3  *
      4  * This program is free software; you can redistribute it and/or modify it
      5  * under the terms of version 2 of the GNU General Public License as
      6  * published by the Free Software Foundation.
      7  *
      8  * This program is distributed in the hope that it would be useful, but
      9  * WITHOUT ANY WARRANTY; without even the implied warranty of
     10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
     11  *
     12  * Further, this software is distributed without any warranty that it is
     13  * free of the rightful claim of any third person regarding infringement
     14  * or the like.  Any license provided herein, whether implied or
     15  * otherwise, applies only to this software file.  Patent licenses, if
     16  * any, provided herein do not apply to combinations of this program with
     17  * other software, or any other product whatsoever.
     18  *
     19  * You should have received a copy of the GNU General Public License along
     20  * with this program; if not, write the Free Software Foundation, Inc.,
     21  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
     22  */
     23 
     24 #include <errno.h>
     25 
     26 #include "test.h"
     27 #include "tst_timer.h"
     28 #include "tst_clocks.h"
     29 #include "lapi/posix_clocks.h"
     30 
     31 static struct timespec start_time, stop_time;
     32 static clockid_t clock_id;
     33 
     34 static const char *clock_name(clockid_t clk_id)
     35 {
     36 	switch (clk_id) {
     37 	case CLOCK_REALTIME:
     38 		return "CLOCK_REALTIME";
     39 	case CLOCK_REALTIME_COARSE:
     40 		return "CLOCK_REALTIME_COARSE";
     41 	case CLOCK_MONOTONIC:
     42 		return "CLOCK_MONOTONIC";
     43 	case CLOCK_MONOTONIC_COARSE:
     44 		return "CLOCK_MONOTONIC_COARSE";
     45 	case CLOCK_MONOTONIC_RAW:
     46 		return "CLOCK_MONOTONIC_RAW";
     47 	case CLOCK_BOOTTIME:
     48 		return "CLOCK_BOOTTIME";
     49 	case CLOCK_PROCESS_CPUTIME_ID:
     50 		return "CLOCK_PROCESS_CPUTIME_ID";
     51 	case CLOCK_THREAD_CPUTIME_ID:
     52 		return "CLOCK_THREAD_CPUTIME_ID";
     53 	default:
     54 		return "UNKNOWN/INVALID";
     55 	}
     56 }
     57 
     58 void tst_timer_check(clockid_t clk_id)
     59 {
     60 	if (tst_clock_gettime(clk_id, &start_time)) {
     61 		if (errno == EINVAL) {
     62 			tst_brkm(TCONF, NULL,
     63 			         "Clock id %s(%u) not supported by kernel",
     64 				 clock_name(clk_id), clk_id);
     65 			return;
     66 		}
     67 
     68 		tst_brkm(TBROK | TERRNO, NULL, "tst_clock_gettime() failed");
     69 	}
     70 }
     71 
     72 void tst_timer_start(clockid_t clk_id)
     73 {
     74 	clock_id = clk_id;
     75 
     76 	if (tst_clock_gettime(clock_id, &start_time))
     77 		tst_resm(TWARN | TERRNO, "tst_clock_gettime() failed");
     78 }
     79 
     80 void tst_timer_stop(void)
     81 {
     82 	if (tst_clock_gettime(clock_id, &stop_time))
     83 		tst_resm(TWARN | TERRNO, "tst_clock_gettime() failed");
     84 }
     85 
     86 struct timespec tst_timer_elapsed(void)
     87 {
     88 	return tst_timespec_diff(stop_time, start_time);
     89 }
     90