Received: by 2002:ac0:8c9a:0:0:0:0:0 with SMTP id r26csp5663568ima; Tue, 5 Feb 2019 16:12:44 -0800 (PST) X-Google-Smtp-Source: AHgI3IZuO7DaqxN0OF56d96m0Qruxm2q1fapbe/wKquGLzhTwdYlOD5ekI9mWU+BmBoBF7Wm3MDT X-Received: by 2002:a63:cd4c:: with SMTP id a12mr7100519pgj.252.1549411964386; Tue, 05 Feb 2019 16:12:44 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1549411964; cv=none; d=google.com; s=arc-20160816; b=POKoIlyEBlhZJSbVGd04/BfxExfyBzfFimvK726kZodEy0fiMgMYGolT23dnUgitQh 01WreOVICoKgUlK0VyV1LEqjCZI3jmJnl0+u5RT48ZRO2onaKDAJKZfLlneRnrz93Qeo LJatWxQ1LBq0PPaCGgCwIj3hW/aUtg5XIoTxqY9yTVC6ZS4jQJyMcoyL1CzEeYfe3Yws dSdbAf5Ca2+lvYtTLNdhKRqMu+REsRCppLmf6VfzPWHMz5EFOrAX8yV6wo9A/YuqKAVg pMJgogpdkhehz1mEA5gYKJwSVPNLhREjOUJSYypH02r85OfFF21p2eC+xnFqKxeHhZ0p o0Iw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=y864ignTHXAZ4Cw9/Z8wdmVJElW5ggoP8Y5xizXA7FQ=; b=bTSLs6zDuBL312W4BbbM1TbVeSbJs/IvyPkC6xSayF8byi67NfmP+7kg2fbwKjoOge XghSxvW4TMDrryJS1Wrc2t3ZgSRVsHcGA5APcUIsvrPIEK2hi6bb/isEoHZFR/jfzxx0 DHrj8PdnHcLWykp1cmoA7GkGaEMWPHR8Ra4xrxnViL/2FizfEnKU2SpTwmgFBso5s47S IV003ffZZxmILV10ccdwHvBn6R4ir1aI+RdL4PbpRh2fx9uyPWZXIdREn5JQDy2jIEt+ vYu92alKtrOV+m8FM3qOQghh1m2+mh8hq5AejdHmyjGKCxi3+mD0BH0ar0iYL7ZVcVLW yWXw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@arista.com header.s=googlenew header.b=Nx3OJPKq; 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 r18si4645013pfc.199.2019.02.05.16.12.29; Tue, 05 Feb 2019 16:12:44 -0800 (PST) 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=Nx3OJPKq; 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 S1727173AbfBFALz (ORCPT + 99 others); Tue, 5 Feb 2019 19:11:55 -0500 Received: from mail-ed1-f65.google.com ([209.85.208.65]:36964 "EHLO mail-ed1-f65.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1729498AbfBFALu (ORCPT ); Tue, 5 Feb 2019 19:11:50 -0500 Received: by mail-ed1-f65.google.com with SMTP id h15so4480480edb.4 for ; Tue, 05 Feb 2019 16:11:49 -0800 (PST) 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 :mime-version:content-transfer-encoding; bh=y864ignTHXAZ4Cw9/Z8wdmVJElW5ggoP8Y5xizXA7FQ=; b=Nx3OJPKqDACUIKy0WpLDwAOLesOuJol8rRduBmyTcgrQi5G80mY/152sqsZWp/YbOA rdCI0mo5lChAwrqiMsWQXyVZZBavoWmjtKH/PY0++GvpjJygaZiokD8E12qcVkaYoco+ UeGNDttfv6wfj31KVXCdEA0Vm4XXONLeRIuXsEZbHD0VXO1/c2jhWu1i8L/F2QmlR+wf 0lF4V8GSyzeb4n/MxfnC+kd5QU93Y0M+eifEe49p6ar0GiKKSJZZBaOO54HJxMycI4yV Eeu1qYCak5ozqTusFviiquSWKa+QWxqct5+2AzWYytSdtwGjk6fVV+f3/TTiNUpYVBwp g0UA== 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=y864ignTHXAZ4Cw9/Z8wdmVJElW5ggoP8Y5xizXA7FQ=; b=RKiiy+pfRKkZo5WySf6enAFxc/aCX1Y75APPdlwsWFhLAe+17Gmok6MRp7Z38HYU87 jKHlJkIZka0JDymTqtkZqT+0n5+BzZCYvJxZDWYJvuIt3gAog/oVnRADyG8s0lFBSrOF wcf3PLvV400f7guNJ59/c2Ot2m86pS6ckV49lzlr+M5kZLKTbaRea6HQH2mlDfIPB+Aq PRGvgfRBnRdBIcxi+53rfLFwJrNSD8PtTupPIWffuUkuWi9UkUxLq/p8GdmoT9AXSMGp YqagyP3bN4Iwo/HrVGohGR8W90jtwO6F4qzSdJ0Wq9TuoGlceLuY5jvMkOCt/WCZEk+Y 7dHg== X-Gm-Message-State: AHQUAuYpMm0o/12F++VLhiAcCe+hJ464oDl5H51mm61CxV7o/s92Yr9X it+O2OznYEyyTsz2mvriIufQxNyyAVw= X-Received: by 2002:aa7:d602:: with SMTP id c2mr5919710edr.203.1549411908384; Tue, 05 Feb 2019 16:11:48 -0800 (PST) Received: from Mindolluin.ire.aristanetworks.com ([217.173.96.166]) by smtp.gmail.com with ESMTPSA id p30sm5489594eda.68.2019.02.05.16.11.47 (version=TLS1_3 cipher=AEAD-AES256-GCM-SHA384 bits=256/256); Tue, 05 Feb 2019 16:11:47 -0800 (PST) From: Dmitry Safonov To: linux-kernel@vger.kernel.org Cc: Andrei Vagin , Dmitry Safonov , Adrian Reber , Andrei Vagin , Andy Lutomirski , Andy Tucker , Arnd Bergmann , Christian Brauner , Cyrill Gorcunov , Dmitry Safonov <0x7f454c46@gmail.com>, "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 Subject: [PATCH 29/32] selftests: Add a simple perf test for clock_gettime() Date: Wed, 6 Feb 2019 00:11:03 +0000 Message-Id: <20190206001107.16488-30-dima@arista.com> X-Mailer: git-send-email 2.20.1 In-Reply-To: <20190206001107.16488-1-dima@arista.com> References: <20190206001107.16488-1-dima@arista.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Andrei Vagin Signed-off-by: Andrei Vagin Signed-off-by: Dmitry Safonov --- tools/testing/selftests/timens/.gitignore | 1 + tools/testing/selftests/timens/Makefile | 8 +- tools/testing/selftests/timens/gettime_perf.c | 74 +++++++++++++++++++ .../selftests/timens/gettime_perf_cold.c | 63 ++++++++++++++++ 4 files changed, 145 insertions(+), 1 deletion(-) create mode 100644 tools/testing/selftests/timens/gettime_perf.c create mode 100644 tools/testing/selftests/timens/gettime_perf_cold.c diff --git a/tools/testing/selftests/timens/.gitignore b/tools/testing/selftests/timens/.gitignore index 3b7eda8f35ce..6bb90fdb4519 100644 --- a/tools/testing/selftests/timens/.gitignore +++ b/tools/testing/selftests/timens/.gitignore @@ -1,4 +1,5 @@ clock_nanosleep +gettime_perf procfs timens timer diff --git a/tools/testing/selftests/timens/Makefile b/tools/testing/selftests/timens/Makefile index ae1ffd24cc43..ef65bf96b55c 100644 --- a/tools/testing/selftests/timens/Makefile +++ b/tools/testing/selftests/timens/Makefile @@ -1,4 +1,10 @@ -TEST_GEN_PROGS := timens timerfd timer clock_nanosleep procfs +TEST_GEN_PROGS := timens timerfd timer clock_nanosleep procfs gettime_perf + +uname_M := $(shell uname -m 2>/dev/null || echo not) +ARCH ?= $(shell echo $(uname_M) | sed -e s/i.86/i386/) +ifeq ($(ARCH),x86_64) +TEST_GEN_PROGS += gettime_perf_cold +endif CFLAGS := -Wall -Werror LDFLAGS := -lrt diff --git a/tools/testing/selftests/timens/gettime_perf.c b/tools/testing/selftests/timens/gettime_perf.c new file mode 100644 index 000000000000..510d77a941d9 --- /dev/null +++ b/tools/testing/selftests/timens/gettime_perf.c @@ -0,0 +1,74 @@ +// SPDX-License-Identifier: GPL-2.0 +#define _GNU_SOURCE +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include "log.h" +#include "timens.h" + +//#define TEST_SYSCALL + +static void test(clock_t clockid, char *clockstr, bool in_ns) +{ + struct timespec tp, start; + long i = 0; + const int timeout = 3; + +#ifndef TEST_SYSCALL + clock_gettime(clockid, &start); +#else + syscall(__NR_clock_gettime, clockid, &start); +#endif + tp = start; + for (tp = start; start.tv_sec + timeout > tp.tv_sec || + (start.tv_sec + timeout == tp.tv_sec && + start.tv_nsec > tp.tv_nsec); i++) { +#ifndef TEST_SYSCALL + clock_gettime(clockid, &tp); +#else + syscall(__NR_clock_gettime, clockid, &tp); +#endif + } + + ksft_test_result_pass("%s:\tclock: %10s\tcycles:\t%10ld\n", + in_ns ? "ns" : "host", clockstr, i); +} + +int main(int argc, char *argv[]) +{ + time_t offset = 10; + int nsfd; + + test(CLOCK_MONOTONIC, "monotonic", false); + test(CLOCK_BOOTTIME, "boottime", false); + + nscheck(); + + if (unshare(CLONE_NEWTIME)) + return pr_perror("Can't unshare() timens"); + + nsfd = open("/proc/self/ns/time_for_children", O_RDONLY); + if (nsfd < 0) + return pr_perror("Can't open a time namespace"); + + if (_settime(CLOCK_MONOTONIC, offset)) + return 1; + if (_settime(CLOCK_BOOTTIME, offset)) + return 1; + + if (setns(nsfd, CLONE_NEWTIME)) + return pr_perror("setns"); + + test(CLOCK_MONOTONIC, "monotonic", true); + test(CLOCK_BOOTTIME, "boottime", true); + + ksft_exit_pass(); + return 0; +} diff --git a/tools/testing/selftests/timens/gettime_perf_cold.c b/tools/testing/selftests/timens/gettime_perf_cold.c new file mode 100644 index 000000000000..f72db8a4c903 --- /dev/null +++ b/tools/testing/selftests/timens/gettime_perf_cold.c @@ -0,0 +1,63 @@ +// SPDX-License-Identifier: GPL-2.0 +#define _GNU_SOURCE +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include "log.h" +#include "timens.h" + +static __inline__ unsigned long long rdtsc(void) +{ + unsigned hi, lo; + + __asm__ __volatile__ ("rdtsc" : "=a"(lo), "=d"(hi)); + return ((unsigned long long) lo) | (((unsigned long long)hi) << 32); +} + +static void test(clock_t clockid, char *clockstr) +{ + struct timespec tp; + long long s, e; + + s = rdtsc(); + clock_gettime(clockid, &tp); + e = rdtsc(); + printf("%lld\n", e - s); + return; +} + +int main(int argc, char **argv) +{ + time_t offset = 10; + int nsfd; + + if (argc == 1) { + test(CLOCK_MONOTONIC, "monotonic"); + return 0; + } + nscheck(); + + if (unshare(CLONE_NEWTIME)) + return pr_perror("Can't unshare() timens"); + + nsfd = open("/proc/self/ns/time_for_children", O_RDONLY); + if (nsfd < 0) + return pr_perror("Can't open a time namespace"); + + if (_settime(CLOCK_MONOTONIC, offset)) + return 1; + + if (setns(nsfd, CLONE_NEWTIME)) + return pr_perror("setns"); + + test(CLOCK_MONOTONIC, "monotonic"); + return 0; +} -- 2.20.1