From patchwork Wed Dec 12 20:37:21 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Rafael David Tinoco X-Patchwork-Id: 1012266 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: ozlabs.org; spf=pass (mailfrom) smtp.mailfrom=lists.linux.it (client-ip=213.254.12.146; helo=picard.linux.it; envelope-from=ltp-bounces+incoming=patchwork.ozlabs.org@lists.linux.it; receiver=) Authentication-Results: ozlabs.org; dmarc=fail (p=none dis=none) header.from=linaro.org Authentication-Results: ozlabs.org; dkim=fail reason="signature verification failed" (1024-bit key; unprotected) header.d=linaro.org header.i=@linaro.org header.b="HaDkETPx"; dkim-atps=neutral Received: from picard.linux.it (picard.linux.it [213.254.12.146]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id 43FTCP65Vhz9s0t for ; Thu, 13 Dec 2018 07:37:53 +1100 (AEDT) Received: from picard.linux.it (localhost [IPv6:::1]) by picard.linux.it (Postfix) with ESMTP id 4D1343E6F25 for ; Wed, 12 Dec 2018 21:37:51 +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 412C83E6F94 for ; Wed, 12 Dec 2018 21:37:36 +0100 (CET) Received: from mail-qk1-x743.google.com (mail-qk1-x743.google.com [IPv6:2607:f8b0:4864:20::743]) (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 92019601C64 for ; Wed, 12 Dec 2018 21:37:35 +0100 (CET) Received: by mail-qk1-x743.google.com with SMTP id o125so11626286qkf.3 for ; Wed, 12 Dec 2018 12:37:35 -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=SC8/tFdtnpu12EZuucE2jE2icThtUYh/4MUtk5kxKQE=; b=HaDkETPxBqbd0+QTTq0A22XFNbCSohyyOcAptrtxFmGEJPtqkCf/uGFEjMn1BVykQJ MYk7A3mQRLrX9zEUX45eFXlc2cWXWzeeCd6lrlqYxXE3uS51i6CXL2sshSbBRVt9wD1h hceTVU4QdcPzZtgTs2s0HcMMwuhrTZEYORH54= 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=SC8/tFdtnpu12EZuucE2jE2icThtUYh/4MUtk5kxKQE=; b=SsQIYLXOxJ/LMyEqD30Zow9F5uWjpnkNNjOH/ngmD+CZ/RfUMBJQqbQlKbxUpTYb/y mE8wZGbd9rlCXh0WqIgtoftEpIwpnaqArVp+5yzp0oyUvwX8fXy4gZ7dZ4PIGABHLGFa xYZHW8xJDLA9H2uYmkSTgQCaJqpcMIf12LEQbhS15OqQXvISzQ8i/9/+Oq0pYyqAb+td VoGLrTtrPryss2Y96mN06TGKw+w5D1yi+mI9Mu7yIvF5Invitju2mmblBih7Hxcgmrtl lLtkPkhYL9G/4yDrirhA+VtLnXcgl9BLArAVwdmv/jc07KgAXu0YCfsGqHvsBaQ9QndL DOTQ== X-Gm-Message-State: AA+aEWao/RQSaCBGjZPQAQjZH7jQ2IPMhpslIAOySI6cFtLK9a63AeNG OHgNjmAoBW041yeRCVyEINTxxfIX6YEBhw== X-Google-Smtp-Source: AFSGD/V0V+ivjXbfO3ALudSlQv9pY82KswyPwquyzQS/5bWi8jc0exBzuGUV4w//twtVANw9kLJo5g== X-Received: by 2002:a37:2bcf:: with SMTP id r76mr19007464qkr.218.1544647054229; Wed, 12 Dec 2018 12:37:34 -0800 (PST) Received: from workstation.celeiro.br ([138.204.25.7]) by smtp.gmail.com with ESMTPSA id n3sm11846872qtc.81.2018.12.12.12.37.32 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Wed, 12 Dec 2018 12:37:33 -0800 (PST) From: Rafael David Tinoco To: ltp@lists.linux.it Date: Wed, 12 Dec 2018 18:37:21 -0200 Message-Id: <20181212203723.18810-4-rafael.tinoco@linaro.org> X-Mailer: git-send-email 2.20.0.rc1 In-Reply-To: <20181212203723.18810-1-rafael.tinoco@linaro.org> References: <20181211142750.GA27159@rei> <20181212203723.18810-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 v3 4/6] 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+incoming=patchwork.ozlabs.org@lists.linux.it Sender: "ltp" Some tests around clocks need to restore the correct date and time after the tests, including possible iterations, run. This commit introduces a new field to tst_test called "restore_wallclock", which 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 --- include/tst_test.h | 1 + lib/tst_test.c | 6 ++++++ lib/tst_wallclock.c | 44 ++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 51 insertions(+) create mode 100644 lib/tst_wallclock.c diff --git a/include/tst_test.h b/include/tst_test.h index 2ebf746eb..170bddc21 100644 --- a/include/tst_test.h +++ b/include/tst_test.h @@ -131,6 +131,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/lib/tst_test.c b/lib/tst_test.c index 661fbbfce..aa3d674f0 100644 --- a/lib/tst_test.c +++ b/lib/tst_test.c @@ -868,6 +868,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) @@ -899,6 +902,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..ef08e1dba --- /dev/null +++ b/lib/tst_wallclock.c @@ -0,0 +1,44 @@ +// SPDX-License-Identifier: GPL-2.0-or-later +/* + * Copyright (c) 2018 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 "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_us(real_begin, tst_timespec_to_us(elapsed)); + + /* restore realtime clock based on monotonic delta */ + + if (tst_clock_settime(CLOCK_REALTIME, &adjust)) + tst_brk(TBROK | TERRNO, "tst_clock_settime() realtime failed"); +}