From patchwork Wed Sep 19 20:50:37 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Dmitry Safonov X-Patchwork-Id: 10606545 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id 007BF1508 for ; Wed, 19 Sep 2018 20:51:18 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id E4B0A2CC34 for ; Wed, 19 Sep 2018 20:51:17 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id E2D1D2CC44; Wed, 19 Sep 2018 20:51:17 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-8.0 required=2.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,MAILING_LIST_MULTI,RCVD_IN_DNSWL_HI autolearn=ham version=3.3.1 Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 5C85C2CC34 for ; Wed, 19 Sep 2018 20:51:17 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1733168AbeITCav (ORCPT ); Wed, 19 Sep 2018 22:30:51 -0400 Received: from mail-ed1-f67.google.com ([209.85.208.67]:45670 "EHLO mail-ed1-f67.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1733151AbeITCau (ORCPT ); Wed, 19 Sep 2018 22:30:50 -0400 Received: by mail-ed1-f67.google.com with SMTP id p52-v6so6011189eda.12 for ; Wed, 19 Sep 2018 13:51:10 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=arista.com; s=googlenew; h=from:to:cc:subject:date:message-id:in-reply-to:references; bh=6WS+ZgnYZgB9hS1UsjCp2BVypHFJoS2o6YaRbqPUoaw=; b=LAO4KzWRzjl8bjZRaVaTVBNLYet4WqJXSuEL4Lz1ys7IHx7bMLU6Ho+VQJUwgd8j2V gEsvgNSUrHBYEaDHcnmU43fIZL4GN/ey1c7BVkuyGYlPqfJqgDrIfG3aXooXgFzsxAHE gVCQZKohC3Us3I0j0rtqf7Q9+c92sOEIzy+ehP/yLhLla5XjbQbycSUsUjH2BvS2zfjv m9A37IHjYUJmjHrC4ul/Ssh3hy4JKj9upFmDEuKsset33DdqAViNUlmk+n2ecTE0DPFu x6gLJhcTvjweK97aUUBtbpE/Ub9+Yxj5c53WoGxBBKG+q+OZnD/Vwfk5/ojWNq/BoNJD XD7A== 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; bh=6WS+ZgnYZgB9hS1UsjCp2BVypHFJoS2o6YaRbqPUoaw=; b=fE2Q51AXcpmv8ji8FFncnwcosuoXe/4oHbtZ3ryrhuT5Fxal7prsv2KpcvLK4mgLJd D+oDSLeSh8f8V8jtjKicN5EvM7lAhxm00KDcxIWZzuGkldssfboMGemOoUrr958B2xQJ wc5f26Nab6UNPA+JeO5tgfe4C8x8iLudkYQp4bZhOI7TeUvm33+F97C0OkYkukIjRueM pj0bQjhQwTR4QeDoDYKWvH3fZ1BNKz21V5gnA71zkiw5/4WWDjT9DPHb22wSlO/CPTjl 7G1bmPGLeeAQH+sucez6qZ9ejHW96qGLxMIe1gIMgQHrYy6fgJC50erZ0SK5htywg+Ru yw3g== X-Gm-Message-State: APzg51AEAjzWpbedEKEC+eCa6ybcZUZYfzNusQbVs45ypeNDbH0Wk3S7 XHf8gjZsp/iy69u/7SiqFaLQDA== X-Google-Smtp-Source: ANB0VdaFODWpZFIKqe1QLhyn1jdD+jHxGzIuBcccyb7iVmcZHURRaVNX4CY/Ad6oMGH/h/K9SCBatw== X-Received: by 2002:a50:b3c8:: with SMTP id t8-v6mr61010739edd.95.1537390269440; Wed, 19 Sep 2018 13:51:09 -0700 (PDT) Received: from dhcp.ire.aristanetworks.com ([217.173.96.166]) by smtp.gmail.com with ESMTPSA id t17-v6sm1747729edb.27.2018.09.19.13.51.08 (version=TLS1_2 cipher=ECDHE-RSA-CHACHA20-POLY1305 bits=256/256); Wed, 19 Sep 2018 13:51:08 -0700 (PDT) From: Dmitry Safonov To: linux-kernel@vger.kernel.org Cc: Dmitry Safonov <0x7f454c46@gmail.com>, Andrei Vagin , Dmitry Safonov , Adrian Reber , Andy Lutomirski , Christian Brauner , Cyrill Gorcunov , "Eric W. Biederman" , "H. Peter Anvin" , Ingo Molnar , Jeff Dike , Oleg Nesterov , Pavel Emelyanov , Shuah Khan , Thomas Gleixner , containers@lists.linux-foundation.org, criu@openvz.org, linux-api@vger.kernel.org, x86@kernel.org, linux-kselftest@vger.kernel.org Subject: [RFC 20/20] timens/selftest: Add timer offsets test Date: Wed, 19 Sep 2018 21:50:37 +0100 Message-Id: <20180919205037.9574-21-dima@arista.com> X-Mailer: git-send-email 2.13.6 In-Reply-To: <20180919205037.9574-1-dima@arista.com> References: <20180919205037.9574-1-dima@arista.com> Sender: linux-kselftest-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kselftest@vger.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP From: Andrei Vagin Check that timer_create takes into account clock offsets. Cc: linux-kselftest@vger.kernel.org Signed-off-by: Andrei Vagin Signed-off-by: Dmitry Safonov --- tools/testing/selftests/timens/.gitignore | 1 + tools/testing/selftests/timens/Makefile | 3 +- tools/testing/selftests/timens/timer.c | 95 +++++++++++++++++++++++++++++++ 3 files changed, 98 insertions(+), 1 deletion(-) create mode 100644 tools/testing/selftests/timens/timer.c diff --git a/tools/testing/selftests/timens/.gitignore b/tools/testing/selftests/timens/.gitignore index 94ffdd9cead7..3b7eda8f35ce 100644 --- a/tools/testing/selftests/timens/.gitignore +++ b/tools/testing/selftests/timens/.gitignore @@ -1,4 +1,5 @@ clock_nanosleep procfs timens +timer timerfd diff --git a/tools/testing/selftests/timens/Makefile b/tools/testing/selftests/timens/Makefile index f96f50d1fef8..ae1ffd24cc43 100644 --- a/tools/testing/selftests/timens/Makefile +++ b/tools/testing/selftests/timens/Makefile @@ -1,5 +1,6 @@ -TEST_GEN_PROGS := timens timerfd clock_nanosleep procfs +TEST_GEN_PROGS := timens timerfd timer clock_nanosleep procfs CFLAGS := -Wall -Werror +LDFLAGS := -lrt include ../lib.mk diff --git a/tools/testing/selftests/timens/timer.c b/tools/testing/selftests/timens/timer.c new file mode 100644 index 000000000000..e3a0951aadc8 --- /dev/null +++ b/tools/testing/selftests/timens/timer.c @@ -0,0 +1,95 @@ +// SPDX-License-Identifier: GPL-2.0 +#define _GNU_SOURCE +#include + +#include +#include +#include +#include +#include +#include +#include +#include + +#include "log.h" + +#ifndef CLONE_NEWTIME +#define CLONE_NEWTIME 0x00001000 /* New time namespace */ +#endif + +int run_test(int clockid) +{ + struct itimerspec new_value; + struct timespec now; + long long elapsed; + timer_t fd; + int i; + + if (clock_gettime(clockid, &now) == -1) + return pr_perror("clock_gettime"); + + for (i = 0; i < 2; i++) { + struct sigevent sevp = {.sigev_notify = SIGEV_NONE}; + int flags = 0; + + pr_msg("timerfd_settime: %d", "INFO", clockid); + new_value.it_value.tv_sec = 3600; + new_value.it_value.tv_nsec = 0; + new_value.it_interval.tv_sec = 1; + new_value.it_interval.tv_nsec = 0; + + if (i == 1) { + new_value.it_value.tv_sec += now.tv_sec; + new_value.it_value.tv_nsec += now.tv_nsec; + } + + if (timer_create(clockid, &sevp, &fd) == -1) + return pr_perror("timerfd_create"); + + if (i == 1) + flags |= TIMER_ABSTIME; + if (timer_settime(fd, flags, &new_value, NULL) == -1) + return pr_perror("timerfd_settime"); + + if (timer_gettime(fd, &new_value) == -1) + return pr_perror("timerfd_gettime"); + + elapsed = new_value.it_value.tv_sec; + if (abs(elapsed - 3600) > 60) { + pr_fail("elapsed: %lld\n", elapsed); + return 1; + } + } + + printf("PASS\n"); + + return 0; +} + +int main(int argc, char *argv[]) +{ + struct timespec tp; + int ret; + + if (unshare(CLONE_NEWTIME)) + return pr_perror("unshare"); + + if (clock_gettime(CLOCK_MONOTONIC, &tp)) + return pr_perror("clock_gettime"); + tp.tv_sec -= 70 * 24 * 3600; + if (clock_settime(CLOCK_MONOTONIC, &tp)) + return pr_perror("clock_settime"); + + if (clock_gettime(CLOCK_BOOTTIME, &tp)) + return pr_perror("clock_gettime"); + tp.tv_sec -= 9 * 24 * 3600; + tp.tv_nsec = 0; + if (clock_settime(CLOCK_BOOTTIME, &tp)) + return pr_perror("clock_settime"); + + ret = 0; + ret |= run_test(CLOCK_BOOTTIME); + ret |= run_test(CLOCK_MONOTONIC); + return ret; +} +