Received: by 2002:ac0:a5a6:0:0:0:0:0 with SMTP id m35-v6csp1193925imm; Wed, 19 Sep 2018 13:52:03 -0700 (PDT) X-Google-Smtp-Source: ANB0VdZ467/Iw928fSQ7p2MrjVVFBHHM7nsqUv7VYPN+I4cyZ2z3FQR6trwxF9N0ShlqevAAyplE X-Received: by 2002:a17:902:4381:: with SMTP id j1-v6mr35045577pld.227.1537390323165; Wed, 19 Sep 2018 13:52:03 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1537390323; cv=none; d=google.com; s=arc-20160816; b=E2iZc3npPGQJntSw+LFMAkxyJPLVS+VolhYYdGQMCYlBL/MjOGJbcynoJEpG7EPUzG 79yjNa9/G1wBJUnT3Q0hjuuMa8zrP8/3wzQdyWCmb0bwVhM79oLf1aOhDWX2l9xJmV+n 6NCGhIHkILZiaGIt71IoElvabnRAnfM8LQAjeBXdTGUR+6iQIw+wO1Bk90AacIj1x28N RXpUzAGOMWS35lyQQiaXZwW5s5VVvUe4HqcK6RAoMuqTrx7uURNTJkOiix+2486TFZjN aYRUDl1XDfIC0LciCrpKp45UekcPWm90BK5eJzkehX6JiU2YaXX2aSpLpsM8E7UCdH0C MbUQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:references:in-reply-to:message-id:date :subject:cc:to:from:dkim-signature; bh=pi9FAVaiInjT79/JwXdoUp3uJgJi1NJmkooIr7Bn2DI=; b=xoLCas96+9SEmphHL/4Si0sg4LL8uciTF39HpH0xaDys61Mw+hPxU/zsZh7mQNJQ19 koXCaecnID+xi0vOMXixKJgdMrjkcwPYwGw4Je7PkrekqAJtuNWx1mPMJm2QBKu5g9U5 aDp7CEnRBtSPnXxL7tWnQND0Ty0nVbMrPq8EvSPkTTa94QOa+fL5ctUHKDWGt68mL9/M c+ILiRuxjNHHdKrQjB0jC94chJxU93pB21i0PHq05rkK5vRlatj1McNzs4PUxDqf4Fw7 ILrXBFUZIpBGPB5xBVDEJyjShrkZVzm0u04NrXDWZbFFOmpCy32CDv+4E6+t/PEtqfnN Y+TQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@arista.com header.s=googlenew header.b=H56SVmnG; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=QUARANTINE sp=REJECT dis=NONE) header.from=arista.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id u1-v6si25106731pfc.337.2018.09.19.13.51.47; Wed, 19 Sep 2018 13:52:03 -0700 (PDT) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; dkim=pass header.i=@arista.com header.s=googlenew header.b=H56SVmnG; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=QUARANTINE sp=REJECT dis=NONE) header.from=arista.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1733143AbeITCas (ORCPT + 99 others); Wed, 19 Sep 2018 22:30:48 -0400 Received: from mail-ed1-f66.google.com ([209.85.208.66]:37429 "EHLO mail-ed1-f66.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1733099AbeITCar (ORCPT ); Wed, 19 Sep 2018 22:30:47 -0400 Received: by mail-ed1-f66.google.com with SMTP id a20-v6so6041807edd.4 for ; Wed, 19 Sep 2018 13:51:07 -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=pi9FAVaiInjT79/JwXdoUp3uJgJi1NJmkooIr7Bn2DI=; b=H56SVmnGBbZQaDsQ8FctnByLb0AITxSptuEFN9bR6t+bG8ajE76dWXjdIsfRB8GEcJ wiqffgqrCjlz9YXnmzPoQbajRrKswXNVIQ38CfL94K3Rlsk5Azs3YN3bfZUvB6t7arqQ NtgH8Tpqs9lqlBO7kfRhlN/3vk2wI5OhnnQfPTD0qt7xUPa1YyHHloue2MvfEfWYrOZE NknEnPwbDThhWuOjAZpC24Cn9bUHUHCklHz7Kc2dFXEdCsNKteE3z2Beq0e+F3bLqCip vFX41ntGKpPaWZiyjeZ8lVRUmPkzqd/4ZNtjNKw/u2snKi3k5X+4/lHsju4lAr+IgHfZ GeVQ== 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=pi9FAVaiInjT79/JwXdoUp3uJgJi1NJmkooIr7Bn2DI=; b=l8Dx6zIAFylrs5h7muUYVXVz+xFdOOWmuLOIy0+3q1tlstYabUzBq44PeqxSuR4kxn lIfSbBJgL/UuXskGvr4M2KWtpkAj/7k+lmNb3K9nqfA+2NumJzpaxA+sBobCY6XZhg8g 3AGbrKAeKnLSMr4ig4aTXo7pb6gm5R3GZpvBwBbRM++t4YNrGbQ45QESnIeeDr1UEm4U WBjx61SmBgcNNcAUdTjBJ6q6uurU0EsdT1OrY7BNVEUNXQAgppbFGs3t1FcNGeC6NXdc yGhx8d8P/RLUEcWH3bXGe8LNRzB5OgfNdkqr4NN8G+/w578EuGs6nea8oRzaDlZC5CDy xXbw== X-Gm-Message-State: APzg51BIWSlU0onOOjPwNloiJM9/AqT6Uv38UuucANepbpqwkeUPnLpW EJUekyos2R3teArZxo/TxGg8E5KJIZk= X-Received: by 2002:aa7:c441:: with SMTP id n1-v6mr58385073edr.55.1537390266123; Wed, 19 Sep 2018 13:51:06 -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.04 (version=TLS1_2 cipher=ECDHE-RSA-CHACHA20-POLY1305 bits=256/256); Wed, 19 Sep 2018 13:51:05 -0700 (PDT) From: Dmitry Safonov To: linux-kernel@vger.kernel.org Cc: Dmitry Safonov <0x7f454c46@gmail.com>, Andrei Vagin , Dmitry Safonov , Adrian Reber , Andrei Vagin , 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 18/20] selftest/timens: Add test for clock_nanosleep Date: Wed, 19 Sep 2018 21:50:35 +0100 Message-Id: <20180919205037.9574-19-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-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Andrei Vagin Check that clock_nanosleep() 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 | 2 +- tools/testing/selftests/timens/clock_nanosleep.c | 98 ++++++++++++++++++++++++ 3 files changed, 100 insertions(+), 1 deletion(-) create mode 100644 tools/testing/selftests/timens/clock_nanosleep.c diff --git a/tools/testing/selftests/timens/.gitignore b/tools/testing/selftests/timens/.gitignore index b609f6ee9fb9..9b6c8ddac2c8 100644 --- a/tools/testing/selftests/timens/.gitignore +++ b/tools/testing/selftests/timens/.gitignore @@ -1,2 +1,3 @@ +clock_nanosleep timens timerfd diff --git a/tools/testing/selftests/timens/Makefile b/tools/testing/selftests/timens/Makefile index 66b90cd28e5c..76a1dc891184 100644 --- a/tools/testing/selftests/timens/Makefile +++ b/tools/testing/selftests/timens/Makefile @@ -1,4 +1,4 @@ -TEST_GEN_PROGS := timens timerfd +TEST_GEN_PROGS := timens timerfd clock_nanosleep CFLAGS := -Wall -Werror diff --git a/tools/testing/selftests/timens/clock_nanosleep.c b/tools/testing/selftests/timens/clock_nanosleep.c new file mode 100644 index 000000000000..5af780b4cfe0 --- /dev/null +++ b/tools/testing/selftests/timens/clock_nanosleep.c @@ -0,0 +1,98 @@ +#define _GNU_SOURCE +#include + +#include +#include +#include +#include +#include +#include +#include + +#include "log.h" + +#ifndef CLONE_NEWTIME +#define CLONE_NEWTIME 0x00001000 +#endif + +static long long get_elapsed_time(int clockid, struct timespec *start) +{ + struct timespec curr; + long long secs, nsecs; + + if (clock_gettime(clockid, &curr) == -1) + return pr_perror("clock_gettime"); + + secs = curr.tv_sec - start->tv_sec; + nsecs = curr.tv_nsec - start->tv_nsec; + if (nsecs < 0) { + secs--; + nsecs += 1000000000; + } + if (nsecs > 1000000000) { + secs++; + nsecs -= 1000000000; + } + return secs * 1000 + nsecs / 1000000; +} + +int run_test(int clockid) +{ + long long elapsed; + int i; + + for (i = 0; i < 2; i++) { + struct timespec now = {}; + struct timespec start; + + if (clock_gettime(clockid, &start) == -1) + return pr_perror("clock_gettime"); + + + if (i == 1) { + now.tv_sec = start.tv_sec; + now.tv_nsec = start.tv_nsec; + } + + printf("clock_nanosleep: %d\n", clockid); + now.tv_sec += 2; + clock_nanosleep(clockid, i ? TIMER_ABSTIME : 0, &now, NULL); + + elapsed = get_elapsed_time(clockid, &start); + if (elapsed < 1900 || elapsed > 2100) { + 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 += 7 * 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_MONOTONIC); + return ret; +} + -- 2.13.6