From patchwork Tue Jan 29 17:36:55 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Rafael David Tinoco X-Patchwork-Id: 157004 Delivered-To: patch@linaro.org Received: by 2002:a02:48:0:0:0:0:0 with SMTP id 69csp4935563jaa; Tue, 29 Jan 2019 09:37:35 -0800 (PST) X-Google-Smtp-Source: ALg8bN6YMvqXDIq9myyP7BJ/oOxEGGjKxV1fkk0zGG93kUGoajE5YrpirtKxEqPQHOpzB3L42c1v X-Received: by 2002:a1c:5dd1:: with SMTP id r200mr22845404wmb.93.1548783455655; Tue, 29 Jan 2019 09:37:35 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1548783455; cv=none; d=google.com; s=arc-20160816; b=lJ3bk5K8G+E0dALgJwgY7Qw+cpDsdd+FrzRMl1TW2RL2ctKRateDP1FoJ/xR5kNUwQ JsZI617+g/BSSWuwiOWifRogkA8b5+Z7TpNiASkkIFI92FEZ7wKZWE/nT2i76n97LZtI Bl9fUZEkQTap4rw6mSn/Ilhc/XldNAuOMTEPQ85EcJXsFIphz4/ZcXV4SBuMfdNAUFm4 okBb/b51reBYwQFzx6AjHB9HMtxVQACRassjKXtHbGwKLXzZScBS1LOLScvf6OAsrPFS 7Avct9K1Zb8bD/yubv9obO5G7QrrCk+pF83ZgQHN3e1t9m8Vas4rxOW86Y1ajFW24vzh n9pQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:content-transfer-encoding:list-subscribe:list-help :list-post:list-archive:list-unsubscribe:list-id:precedence:subject :mime-version:references:in-reply-to:message-id:date:to:from :dkim-signature:delivered-to; bh=yHGqupB5dXRc2wE1zQ4DNrsfCAIgMHcJ92Rd21V7nS4=; b=Y/1gexyzF8Av2yKdVSBIatYW6whh5F3GdUwV31suZisRbDWEfaG6mjzTM6bxJ8kLyd kjnoXTinIQBlxkjMCdlcR8puyWT2pEnP7CE+yBOOBe3ndz/Z9rB4qZ7cHBw5VPTKFY6e zgU16cPGOUb+gjOZb0MrisisjuIUk//1HJVflkmF3l3gasaAqZx3ROZ8pFHysEF2pS0o ZC+FGiFC//oF+Dl3GktvwTd/dVVd+G2l4OwgszK5VBNtlRJA10S5c76a2ra9k0J5eDW4 xvPVCDICo3FnJ2+wfMbL0/yfpH117OmV1DZ7iTdwxxng0yHnR9+LvpUPz1Z782jk8/1k p/Wg== ARC-Authentication-Results: i=1; mx.google.com; dkim=neutral (body hash did not verify) header.i=@linaro.org header.s=google header.b=RlSCJu8i; spf=pass (google.com: domain of ltp-bounces+patch=linaro.org@lists.linux.it designates 213.254.12.146 as permitted sender) smtp.mailfrom="ltp-bounces+patch=linaro.org@lists.linux.it"; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=linaro.org Return-Path: Received: from picard.linux.it (picard.linux.it. [213.254.12.146]) by mx.google.com with ESMTPS id b8si2349699wmd.74.2019.01.29.09.37.35 for (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Tue, 29 Jan 2019 09:37:35 -0800 (PST) Received-SPF: pass (google.com: domain of ltp-bounces+patch=linaro.org@lists.linux.it designates 213.254.12.146 as permitted sender) client-ip=213.254.12.146; Authentication-Results: mx.google.com; dkim=neutral (body hash did not verify) header.i=@linaro.org header.s=google header.b=RlSCJu8i; spf=pass (google.com: domain of ltp-bounces+patch=linaro.org@lists.linux.it designates 213.254.12.146 as permitted sender) smtp.mailfrom="ltp-bounces+patch=linaro.org@lists.linux.it"; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=linaro.org Received: from picard.linux.it (localhost [IPv6:::1]) by picard.linux.it (Postfix) with ESMTP id 2D7993EAD14 for ; Tue, 29 Jan 2019 18:37:35 +0100 (CET) X-Original-To: ltp@lists.linux.it Delivered-To: ltp@picard.linux.it Received: from in-2.smtp.seeweb.it (in-2.smtp.seeweb.it [217.194.8.2]) by picard.linux.it (Postfix) with ESMTP id 83E9F3EAD54 for ; Tue, 29 Jan 2019 18:37:11 +0100 (CET) Received: from mail-qk1-x741.google.com (mail-qk1-x741.google.com [IPv6:2607:f8b0:4864:20::741]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by in-2.smtp.seeweb.it (Postfix) with ESMTPS id AC4C8600701 for ; Tue, 29 Jan 2019 18:37:10 +0100 (CET) Received: by mail-qk1-x741.google.com with SMTP id 189so12004456qkj.8 for ; Tue, 29 Jan 2019 09:37:10 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=K/bsbfubyS7N3hRl+G5c90yvZbCJrRE/2SG7sgH16rc=; b=RlSCJu8ik0VDJTON1rZoloqhbQ3CE0ycb59c30HfMcw+gLF15q3bDrdE9PPOvUfsy9 Limw5aFo5UA78E/3d90mgRRG9O5YYeR9Bh0it3DxxSE7WjDr3jQm3KpPjWTYIe7Mj33U Qv2O5gqp+20/v68YzBL/nRMNadTZQbHYoDvMk= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=K/bsbfubyS7N3hRl+G5c90yvZbCJrRE/2SG7sgH16rc=; b=UgrwIcFmMrUquGE8krnPg2a/NGE3hkBTgBdx7J8oa8OxE+wajufTVhJXw1Bf5jeEid ux0aqBQVvEvyX0RG+8xTibPIf+EDQOmh0mbg60oIR01s7ld/drxiN2ibVEvfeZacRrnX 7XGJtXoAOS579xLfB2wtmAZPL4yPa7Hu86BQyBoFO7ryrWWpkPyYEArwghL6ZM1CuSTn n9sN2EzE1iGGGaWOjoiOfxv6uuwqSq9rGA7hrybjQ6wslbQWYxy1DztB9AvTSLCD+8Fd 2k4AWNKZc7K3zaeBgSaUWrBmrA1nfiebiV9iUGYioHyGn6Nkihs35MOJvuPAi7YkqIW2 7YXQ== X-Gm-Message-State: AJcUukeEbwml7vz+CSUVhVpcRd3ZM9jSPzcd+NkxyLmvpimw9/uIzEjc 1W7c13kPBlSU1baAj9+01bB2j9aiYUY= X-Received: by 2002:a37:8107:: with SMTP id c7mr23330703qkd.77.1548783429017; Tue, 29 Jan 2019 09:37:09 -0800 (PST) Received: from workstation.celeiro.br. ([168.194.163.64]) by smtp.gmail.com with ESMTPSA id a20sm56890855qkj.28.2019.01.29.09.37.06 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Tue, 29 Jan 2019 09:37:08 -0800 (PST) From: Rafael David Tinoco To: ltp@lists.linux.it Date: Tue, 29 Jan 2019 15:36:55 -0200 Message-Id: <20190129173659.27901-4-rafael.tinoco@linaro.org> X-Mailer: git-send-email 2.20.1 In-Reply-To: <20190129173659.27901-1-rafael.tinoco@linaro.org> References: <20190124161159.GC16804@rei.lan> <20190129173659.27901-1-rafael.tinoco@linaro.org> MIME-Version: 1.0 X-Virus-Scanned: clamav-milter 0.99.2 at in-2.smtp.seeweb.it X-Virus-Status: Clean X-Spam-Status: No, score=0.1 required=7.0 tests=DKIM_SIGNED,DKIM_VALID, DKIM_VALID_AU,SPF_PASS autolearn=disabled version=3.4.0 X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on in-2.smtp.seeweb.it Subject: [LTP] [PATCH v4 4/8] lib: new restore_wallclock field to restore realtime clock X-BeenThere: ltp@lists.linux.it X-Mailman-Version: 2.1.18 Precedence: list List-Id: Linux Test Project List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: ltp-bounces+patch=linaro.org@lists.linux.it Sender: "ltp" Some tests that change the system-wide clock need to have a way to restore the correct time after their execution. This commit introduces a new field to tst_test struct called "restore_wallclock": it makes the test to save current realtime clock during setup phase, and, later, during cleanup, restore it to the appropriate time using a monotonic raw clock difference. Signed-off-by: Rafael David Tinoco --- doc/test-writing-guidelines.txt | 35 +++++++++++++++++++++++++ include/tst_test.h | 1 + include/tst_wallclock.h | 15 +++++++++++ lib/tst_test.c | 7 +++++ lib/tst_wallclock.c | 45 +++++++++++++++++++++++++++++++++ 5 files changed, 103 insertions(+) create mode 100644 include/tst_wallclock.h create mode 100644 lib/tst_wallclock.c diff --git a/doc/test-writing-guidelines.txt b/doc/test-writing-guidelines.txt index 731be7692..f2f72c4d6 100644 --- a/doc/test-writing-guidelines.txt +++ b/doc/test-writing-guidelines.txt @@ -1539,6 +1539,41 @@ static struct tst_test test = { }; ------------------------------------------------------------------------------- +2.2.29 Changing the Wall Clock Time during test execution +^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + +There are some tests that, for different reasons, might need to change the +system-wide clock time. Whenever this happens, it is imperative that the clock +is restored, at the end of test's execution, taking in consideration the amount +of time elapsed during that test. + +In order for that to happen, struct tst_test has a variable called +"restore_wallclock" that should be set to "1" so LTP knows it should: (1) +initialize a monotonic clock during test setup phase and (2) use that monotonic +clock to fix the system-wide clock time at the test cleanup phase. + +[source,c] +------------------------------------------------------------------------------- +#include "tst_test.h" + +static void setup(void) +{ + ... +} + +static void run(void) +{ + ... +} + +sturct tst_test test = { + ... + .setup = setup, + .test_all = run, + .restore_wallclock = 1, + ... +}; +------------------------------------------------------------------------------- 2.3 Writing a testcase in shell ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ diff --git a/include/tst_test.h b/include/tst_test.h index b37890959..12dda2e79 100644 --- a/include/tst_test.h +++ b/include/tst_test.h @@ -135,6 +135,7 @@ struct tst_test { int needs_rofs:1; int child_needs_reinit:1; int needs_devfs:1; + int restore_wallclock:1; /* * If set the test function will be executed for all available * filesystems and the current filesytem type would be set in the diff --git a/include/tst_wallclock.h b/include/tst_wallclock.h new file mode 100644 index 000000000..7d6723a7a --- /dev/null +++ b/include/tst_wallclock.h @@ -0,0 +1,15 @@ +// SPDX-License-Identifier: GPL-2.0-or-later +/* + * Copyright (c) 2019 Linaro Limited. All rights reserved. + * Author: Rafael David Tinoco + */ + + +#ifndef TST_WALLCLK_H__ +#define TST_WALLCLK_H__ + +void tst_wallclock_save(void); + +void tst_wallclock_restore(void); + +#endif /* TST_WALLCLK_H__ */ diff --git a/lib/tst_test.c b/lib/tst_test.c index da3e0c8a0..7dd890b8d 100644 --- a/lib/tst_test.c +++ b/lib/tst_test.c @@ -35,6 +35,7 @@ #include "tst_timer_test.h" #include "tst_clocks.h" #include "tst_timer.h" +#include "tst_wallclock.h" #include "tst_sys_conf.h" #include "tst_kconfig.h" @@ -872,6 +873,9 @@ static void do_setup(int argc, char *argv[]) if (tst_test->resource_files) copy_resources(); + + if (tst_test->restore_wallclock) + tst_wallclock_save(); } static void do_test_setup(void) @@ -903,6 +907,9 @@ static void do_cleanup(void) tst_sys_conf_restore(0); cleanup_ipc(); + + if (tst_test->restore_wallclock) + tst_wallclock_restore(); } static void run_tests(void) diff --git a/lib/tst_wallclock.c b/lib/tst_wallclock.c new file mode 100644 index 000000000..1513882f4 --- /dev/null +++ b/lib/tst_wallclock.c @@ -0,0 +1,45 @@ +// SPDX-License-Identifier: GPL-2.0-or-later +/* + * Copyright (c) 2019 Linaro Limited. All rights reserved. + * Author: Rafael David Tinoco + */ + +#include + +#define TST_NO_DEFAULT_MAIN + +#include "tst_test.h" +#include "tst_timer.h" +#include "tst_clocks.h" +#include "tst_wallclock.h" +#include "lapi/posix_clocks.h" + +static struct timespec real_begin, mono_begin; + +void tst_wallclock_save(void) +{ + /* save initial monotonic time to restore it when needed */ + + if (tst_clock_gettime(CLOCK_REALTIME, &real_begin)) + tst_brk(TBROK | TERRNO, "tst_clock_gettime() realtime failed"); + + if (tst_clock_gettime(CLOCK_MONOTONIC_RAW, &mono_begin)) + tst_brk(TBROK | TERRNO, "tst_clock_gettime() monotonic failed"); +} + +void tst_wallclock_restore(void) +{ + static struct timespec mono_end, elapsed, adjust; + + if (tst_clock_gettime(CLOCK_MONOTONIC_RAW, &mono_end)) + tst_brk(TBROK | TERRNO, "tst_clock_gettime() monotonic failed"); + + elapsed = tst_timespec_diff(mono_end, mono_begin); + + adjust = tst_timespec_add(real_begin, elapsed); + + /* restore realtime clock based on monotonic delta */ + + if (tst_clock_settime(CLOCK_REALTIME, &adjust)) + tst_brk(TBROK | TERRNO, "tst_clock_settime() realtime failed"); +}