Cyril Hrubis | 9dee405 | 2015-02-24 13:31:14 +0100 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright (C) 2015 Cyril Hrubis <chrubis@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 "lapi/posix_clocks.h" |
| 29 | |
| 30 | static struct timespec start_time, stop_time; |
| 31 | static clockid_t clock_id; |
| 32 | |
| 33 | static const char *clock_name(clockid_t clk_id) |
| 34 | { |
| 35 | switch (clk_id) { |
| 36 | case CLOCK_REALTIME: |
| 37 | return "CLOCK_REALTIME"; |
| 38 | case CLOCK_REALTIME_COARSE: |
| 39 | return "CLOCK_REALTIME_COARSE"; |
| 40 | case CLOCK_MONOTONIC: |
| 41 | return "CLOCK_MONOTONIC"; |
| 42 | case CLOCK_MONOTONIC_COARSE: |
| 43 | return "CLOCK_MONOTONIC_COARSE"; |
| 44 | case CLOCK_MONOTONIC_RAW: |
| 45 | return "CLOCK_MONOTONIC_RAW"; |
| 46 | case CLOCK_BOOTTIME: |
| 47 | return "CLOCK_BOOTTIME"; |
| 48 | case CLOCK_PROCESS_CPUTIME_ID: |
| 49 | return "CLOCK_PROCESS_CPUTIME_ID"; |
| 50 | case CLOCK_THREAD_CPUTIME_ID: |
| 51 | return "CLOCK_THREAD_CPUTIME_ID"; |
| 52 | default: |
| 53 | return "UNKNOWN/INVALID"; |
| 54 | } |
| 55 | } |
| 56 | |
| 57 | void tst_timer_check(clockid_t clk_id) |
| 58 | { |
| 59 | if (clock_gettime(clk_id, &start_time)) { |
| 60 | if (errno == EINVAL) { |
| 61 | tst_brkm(TCONF, NULL, |
| 62 | "Clock id %s(%u) not supported by kernel", |
| 63 | clock_name(clk_id), clk_id); |
| 64 | } |
| 65 | |
| 66 | tst_brkm(TBROK | TERRNO, NULL, "clock_gettime() failed"); |
| 67 | } |
| 68 | } |
| 69 | |
| 70 | void tst_timer_start(clockid_t clk_id) |
| 71 | { |
| 72 | clock_id = clk_id; |
| 73 | |
| 74 | if (clock_gettime(clock_id, &start_time)) |
| 75 | tst_resm(TWARN | TERRNO, "clock_gettime() failed"); |
| 76 | } |
| 77 | |
| 78 | void tst_timer_stop(void) |
| 79 | { |
| 80 | if (clock_gettime(clock_id, &stop_time)) |
| 81 | tst_resm(TWARN | TERRNO, "clock_gettime() failed"); |
| 82 | } |
| 83 | |
| 84 | struct timespec tst_timer_elapsed(void) |
| 85 | { |
| 86 | return tst_timespec_diff(stop_time, start_time); |
| 87 | } |