From patchwork Thu Nov 19 11:00:07 2015 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Joonas Lahtinen X-Patchwork-Id: 7656471 Return-Path: X-Original-To: patchwork-intel-gfx@patchwork.kernel.org Delivered-To: patchwork-parsemail@patchwork1.web.kernel.org Received: from mail.kernel.org (mail.kernel.org [198.145.29.136]) by patchwork1.web.kernel.org (Postfix) with ESMTP id 0F4199F2E2 for ; Thu, 19 Nov 2015 11:00:21 +0000 (UTC) Received: from mail.kernel.org (localhost [127.0.0.1]) by mail.kernel.org (Postfix) with ESMTP id 1D2812053E for ; Thu, 19 Nov 2015 11:00:20 +0000 (UTC) Received: from gabe.freedesktop.org (gabe.freedesktop.org [131.252.210.177]) by mail.kernel.org (Postfix) with ESMTP id 244872052A for ; Thu, 19 Nov 2015 11:00:19 +0000 (UTC) Received: from gabe.freedesktop.org (localhost [127.0.0.1]) by gabe.freedesktop.org (Postfix) with ESMTP id 2D86C6E046; Thu, 19 Nov 2015 03:00:18 -0800 (PST) X-Original-To: intel-gfx@lists.freedesktop.org Delivered-To: intel-gfx@lists.freedesktop.org Received: from mga01.intel.com (mga01.intel.com [192.55.52.88]) by gabe.freedesktop.org (Postfix) with ESMTP id 58F736E046 for ; Thu, 19 Nov 2015 03:00:17 -0800 (PST) Received: from orsmga002.jf.intel.com ([10.7.209.21]) by fmsmga101.fm.intel.com with ESMTP; 19 Nov 2015 03:00:16 -0800 X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="5.20,317,1444719600"; d="scan'208";a="854490451" Received: from jlahtine-mobl1.ger.corp.intel.com ([10.252.25.175]) by orsmga002.jf.intel.com with ESMTP; 19 Nov 2015 03:00:15 -0800 From: Joonas Lahtinen To: Intel graphics driver community testing & development Date: Thu, 19 Nov 2015 13:00:07 +0200 Message-Id: <1447930807-7533-1-git-send-email-joonas.lahtinen@linux.intel.com> X-Mailer: git-send-email 2.4.3 In-Reply-To: <20151118130739.GD30605@nuc-i3427.alporthouse.com> References: <20151118130739.GD30605@nuc-i3427.alporthouse.com> Cc: Thomas Wood Subject: [Intel-gfx] [PATCH i-g-t v4] lib/igt_core: Prefer CLOCK_MONOTONIC_RAW X-BeenThere: intel-gfx@lists.freedesktop.org X-Mailman-Version: 2.1.18 Precedence: list List-Id: Intel graphics driver community testing & development List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , MIME-Version: 1.0 Errors-To: intel-gfx-bounces@lists.freedesktop.org Sender: "Intel-gfx" X-Spam-Status: No, score=-4.8 required=5.0 tests=BAYES_00, RCVD_IN_DNSWL_MED, RP_MATCHES_RCVD, UNPARSEABLE_RELAY autolearn=unavailable version=3.3.1 X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on mail.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP CLOCK_MONOTONIC_RAW is not affected by NTP, so it should be THE clock used for timing execution of tests. When fetching either the starting or ending time of a test, show the time as -1.000s. v4: - Introduce time_valid macro (Chris) - Reduce amount of boilerplate code for calculating elapsed time v3: - Do not exit directly from handler (Chris) - Show elapsed time as -1 if it is not calculable v2: - Cache the used clock (Chris) - Do not change the clock during execution - Spit out and error if monotonic time can not be read Cc: Thomas Wood Cc: Chris Wilson Signed-off-by: Joonas Lahtinen Reviewed-by: Chris Wilson --- lib/igt_core.c | 53 ++++++++++++++++++++++++++++++++++++++++------------- 1 file changed, 40 insertions(+), 13 deletions(-) diff --git a/lib/igt_core.c b/lib/igt_core.c index 04a0ab2..3269804 100644 --- a/lib/igt_core.c +++ b/lib/igt_core.c @@ -220,6 +220,7 @@ static char *run_single_subtest = NULL; static bool run_single_subtest_found = false; static const char *in_subtest = NULL; static struct timespec subtest_time; +static clockid_t igt_clock = (clockid_t)-1; static bool in_fixture = false; static bool test_with_subtests = false; static bool in_atexit_handler = false; @@ -337,14 +338,48 @@ static void kmsg(const char *format, ...) fclose(file); } -static void gettime(struct timespec *ts) +#define time_valid(ts) ((ts)->tv_sec || (ts)->tv_nsec) + +static double +time_elapsed(struct timespec *then, + struct timespec* now) { + double elapsed = -1.; + + if (time_valid(then) && time_valid(now)) { + elapsed = now->tv_sec - then->tv_sec; + elapsed += (now->tv_nsec - then->tv_nsec) * 1e-9; + } + + return elapsed; +} + +static int gettime(struct timespec *ts) { memset(ts, 0, sizeof(*ts)); + errno = 0; + // Stay on the same clock for consistency. + if (igt_clock != (clockid_t)-1) { + if (clock_gettime(igt_clock, ts)) + goto error; + return 0; + } + +#ifdef CLOCK_MONOTONIC_RAW + if (!clock_gettime(igt_clock = CLOCK_MONOTONIC_RAW, ts)) + return 0; +#endif #ifdef CLOCK_MONOTONIC_COARSE - if (clock_gettime(CLOCK_MONOTONIC_COARSE, ts)) + if (!clock_gettime(igt_clock = CLOCK_MONOTONIC_COARSE, ts)) + return 0; #endif - clock_gettime(CLOCK_MONOTONIC, ts); + if (!clock_gettime(igt_clock = CLOCK_MONOTONIC, ts)) + return 0; +error: + igt_warn("Could not read monotonic time: %s\n", + strerror(errno)); + + return -errno; } bool __igt_fixture(void) @@ -831,15 +866,11 @@ static void exit_subtest(const char *) __attribute__((noreturn)); static void exit_subtest(const char *result) { struct timespec now; - double elapsed; gettime(&now); - elapsed = now.tv_sec - subtest_time.tv_sec; - elapsed += (now.tv_nsec - subtest_time.tv_nsec) * 1e-9; - printf("%sSubtest %s: %s (%.3fs)%s\n", (!__igt_plain_output) ? "\x1b[1m" : "", - in_subtest, result, elapsed, + in_subtest, result, time_elapsed(&subtest_time, &now), (!__igt_plain_output) ? "\x1b[0m" : ""); fflush(stdout); @@ -1088,12 +1119,9 @@ void igt_exit(void) if (!test_with_subtests) { struct timespec now; - double elapsed; const char *result; gettime(&now); - elapsed = now.tv_sec - subtest_time.tv_sec; - elapsed += (now.tv_nsec - subtest_time.tv_nsec) * 1e-9; switch (igt_exitcode) { case IGT_EXIT_SUCCESS: @@ -1109,8 +1137,7 @@ void igt_exit(void) result = "FAIL"; } - - printf("%s (%.3fs)\n", result, elapsed); + printf("%s (%.3fs)\n", result, time_elapsed(&subtest_time, &now)); exit(igt_exitcode); }