Received: by 2002:a25:ad19:0:0:0:0:0 with SMTP id y25csp4139598ybi; Mon, 29 Jul 2019 20:07:29 -0700 (PDT) X-Google-Smtp-Source: APXvYqyUjNg2oqkuas3P0173ijzxl9wMGRZ1s+2+EO1mD++BY0krWSFX3TQ9y6JTQ3+i9HT1/HBG X-Received: by 2002:a62:e910:: with SMTP id j16mr40806269pfh.123.1564456049272; Mon, 29 Jul 2019 20:07:29 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1564456049; cv=none; d=google.com; s=arc-20160816; b=CLkiiKRJmSS9TLjbiYaKLMCioBNlxtqFdCSjzF/kJks47B0o/e6Rzt8bUjRLH6scDL TFKx5Ih2Ko6MI+wlasc/oxGoAhuvYCqBqMBenIF7Gt9ywAkNIq9HbBP8rmWXwRQiTI7y OE5efQZNuy0tmfP0i56GqwRxT3EH1MovPWxAqYxGgiQVPXgfG89uIO1Fm1pSLDtvPI27 YBV1CqfalY4FRwn0me+pVkAbmTt5JubYphtwcILZO584u05Et5JFbFBboLZb9PBOstEW yb5ibFxFo6UG3y41mS+Q0RzsvLqEZqbFT6OBvqfOP5eA0Ln8ZnkEvSMuc7y9xH8oB7Zv lbxw== 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=KNzlBhr0Ob2l0AvvzXZp7FfZ03no19mEjcZV68SiC50=; b=QvZInVKeQ+XxWGQ3vUupVUW7PFZ68ICOacYjxfkJQyGe/hY36PIWaM4IP2Kh1nqpOq Wd3KdV2pms9rfhKgGQ0cRQESx7WMFv7DgRiErw24ts6c8b/3mwSWwy8r2MiuhL+IX+Rc ptJ9/v/JKAZwnYosmZg4fyjSs1auoUjL3Xpnm0F5TEfcaBD21sS4frbH40a1msiE3Bhg ONrbE4SfFO8I0jFAU8L65C7o0jILwTffbemaEU8ZttpBZnmwVJMciAeg5vQLrAsu1mCk zZN8meyJ78OkfqN2jgyrHGH33GZd8HozHovfSp+XJWW7iOSnb84VwLPfVsmt6wMwiHCw C5cw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@arista.com header.s=googlenew header.b=EFGoFRol; 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 p44si27432018pjp.0.2019.07.29.20.07.14; Mon, 29 Jul 2019 20:07:29 -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=EFGoFRol; 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 S1729176AbfG2WAM (ORCPT + 99 others); Mon, 29 Jul 2019 18:00:12 -0400 Received: from mail-wm1-f65.google.com ([209.85.128.65]:51319 "EHLO mail-wm1-f65.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S2389331AbfG2V6x (ORCPT ); Mon, 29 Jul 2019 17:58:53 -0400 Received: by mail-wm1-f65.google.com with SMTP id 207so55202052wma.1 for ; Mon, 29 Jul 2019 14:58:51 -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 :mime-version:content-transfer-encoding; bh=KNzlBhr0Ob2l0AvvzXZp7FfZ03no19mEjcZV68SiC50=; b=EFGoFRolRgMSHSfkVg6shq2tfnoDbHJyLg8LLbwY/04x713ZXmiFgW/iC0QXgb+rku 7Y7MPa6cliqPRtfHD3zAd8KrgynwGagQIeY/uXSwITDUPq6Lw9Sxy8c9VvTmi0Zvbziq xih1+j0PLHo0SSxzG6asdTolt8OCDTuJJ/f3r6IywQJ/lPaPciq7IPyyaC+KybBSzgVl 0j96H5D6m+uKRa4hZRCFHotU6MOPQhqkXrrCv4r9ruPCESPFA9U7hqgi/aTyQMjCGgb9 gjeyHwJ4lvALPQxwbEG7ztSw2Y70f3VIEwunrXZYicpoaODxKLCaLPBiBHSKH+cvQTEx fakA== 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=KNzlBhr0Ob2l0AvvzXZp7FfZ03no19mEjcZV68SiC50=; b=U1ei1ptHssXvPWFcEeHuwPq/vsc2u5mM0FHjveU0NoIKA2twwpSUwWPuPkLauF2hl/ wtNwStTLrjgbQ4hSxEoPoD8FVD0Knfl6RLtoMAWWQU6iAaed5vLx/aXxOirkR/nlhPNg DFVYMNScK+S/NmhkpfE2YV2QNiVLl0Cecq41Gx4KBd+12gvXeKbSwWLLkc3pHQNuMeV2 H+L77HChtdyZSmjsKjqJ8kOzj9oZkaeOZMl/1a6V1IIR8RthT11f41q3wDjQuM6L+D+5 cTqS4EjrwOU6iMN3UeYItLFT360rM/5KIgPstNHZxDJbY457gr+Jao0SOVzH/dJ1QAaH M/IA== X-Gm-Message-State: APjAAAWre8FEi+h4qHebUFCjL0/cseanZ60zDurXVL2u/hYwze4bpiRH J+wHpIKJmMEuI2ifAW+Wq+DUtqD5mw9swGJRbRP1jZ0+HejT17xrkz9vBOE6Tr+HP/6gt7QsTVw cmLunleHBU3aQXZZWVOdPPow1sJSQEHuod/FT8W3dvi2vip3WRWVqeBaDcGXGohcw6jglwO16+8 KTnbUaI1y5/rejvq5iDEs1OpDELdqGhOOFVZrHmVA= X-Received: by 2002:a1c:e710:: with SMTP id e16mr103936443wmh.38.1564437530322; Mon, 29 Jul 2019 14:58:50 -0700 (PDT) Received: from Mindolluin.ire.aristanetworks.com ([217.173.96.166]) by smtp.gmail.com with ESMTPSA id x20sm49230728wmc.1.2019.07.29.14.58.49 (version=TLS1_3 cipher=AEAD-AES256-GCM-SHA384 bits=256/256); Mon, 29 Jul 2019 14:58:49 -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 , Arnd Bergmann , Christian Brauner , Cyrill Gorcunov , "Eric W. Biederman" , "H. Peter Anvin" , Ingo Molnar , Jann Horn , Jeff Dike , Oleg Nesterov , Pavel Emelyanov , Shuah Khan , Thomas Gleixner , Vincenzo Frascino , containers@lists.linux-foundation.org, criu@openvz.org, linux-api@vger.kernel.org, x86@kernel.org Subject: [PATCHv5 36/37] selftests/timens: Add a simple perf test for clock_gettime() Date: Mon, 29 Jul 2019 22:57:18 +0100 Message-Id: <20190729215758.28405-37-dima@arista.com> X-Mailer: git-send-email 2.22.0 In-Reply-To: <20190729215758.28405-1-dima@arista.com> References: <20190729215758.28405-1-dima@arista.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-CLOUD-SEC-AV-Info: arista,google_mail,monitor X-CLOUD-SEC-AV-Sent: true X-Gm-Spam: 0 X-Gm-Phishy: 0 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 Co-developed-by: Dmitry Safonov Signed-off-by: Dmitry Safonov --- tools/testing/selftests/timens/.gitignore | 2 + tools/testing/selftests/timens/Makefile | 10 +- tools/testing/selftests/timens/gettime_perf.c | 101 +++++++++++ .../selftests/timens/gettime_perf_cold.c | 160 ++++++++++++++++++ 4 files changed, 271 insertions(+), 2 deletions(-) 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..16292e4d08a5 100644 --- a/tools/testing/selftests/timens/.gitignore +++ b/tools/testing/selftests/timens/.gitignore @@ -1,4 +1,6 @@ clock_nanosleep +gettime_perf +gettime_perf_cold procfs timens timer diff --git a/tools/testing/selftests/timens/Makefile b/tools/testing/selftests/timens/Makefile index ae1ffd24cc43..97e0460eaf48 100644 --- a/tools/testing/selftests/timens/Makefile +++ b/tools/testing/selftests/timens/Makefile @@ -1,6 +1,12 @@ -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 +LDFLAGS := -lrt -ldl include ../lib.mk diff --git a/tools/testing/selftests/timens/gettime_perf.c b/tools/testing/selftests/timens/gettime_perf.c new file mode 100644 index 000000000000..f7d7832c0293 --- /dev/null +++ b/tools/testing/selftests/timens/gettime_perf.c @@ -0,0 +1,101 @@ +// 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" + +//#define TEST_SYSCALL + +typedef int (*vgettime_t)(clockid_t, struct timespec *); + +vgettime_t vdso_clock_gettime; + +static void fill_function_pointers(void) +{ + void *vdso = dlopen("linux-vdso.so.1", + RTLD_LAZY | RTLD_LOCAL | RTLD_NOLOAD); + if (!vdso) + vdso = dlopen("linux-gate.so.1", + RTLD_LAZY | RTLD_LOCAL | RTLD_NOLOAD); + if (!vdso) { + pr_err("[WARN]\tfailed to find vDSO\n"); + return; + } + + vdso_clock_gettime = (vgettime_t)dlsym(vdso, "__vdso_clock_gettime"); + if (!vdso_clock_gettime) + pr_err("Warning: failed to find clock_gettime in vDSO\n"); + +} + +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 + vdso_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 + vdso_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; + + ksft_set_plan(4); + + fill_function_pointers(); + + 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..2ab0869744a6 --- /dev/null +++ b/tools/testing/selftests/timens/gettime_perf_cold.c @@ -0,0 +1,160 @@ +// SPDX-License-Identifier: GPL-2.0 +#define _GNU_SOURCE +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include "log.h" +#include "timens.h" + +#define PAGE_SIZE 4096 +#define CACHE_LINE_SIZE 64 + +typedef int (*vgettime_t)(clockid_t, struct timespec *); + +vgettime_t vdso_clock_gettime; + +static void fill_function_pointers(void) +{ + void *vdso = dlopen("linux-vdso.so.1", + RTLD_LAZY | RTLD_LOCAL | RTLD_NOLOAD); + if (!vdso) + vdso = dlopen("linux-gate.so.1", + RTLD_LAZY | RTLD_LOCAL | RTLD_NOLOAD); + if (!vdso) { + pr_err("[WARN]\tfailed to find vDSO\n"); + return; + } + + vdso_clock_gettime = (vgettime_t)dlsym(vdso, "__vdso_clock_gettime"); + if (!vdso_clock_gettime) + pr_err("Warning: failed to find clock_gettime in vDSO\n"); + +} + +static inline __attribute__((always_inline)) unsigned long long rdtsc(void) +{ + unsigned int hi, lo; + + __asm__ __volatile__ ("rdtsc" : "=a"(lo), "=d"(hi)); + return ((unsigned long long) lo) | (((unsigned long long)hi) << 32); +} + +static inline __attribute__((always_inline)) void test(clock_t clockid, char *clockstr) +{ + struct timespec tp; + long long s, e; + + s = rdtsc(); + vdso_clock_gettime(clockid, &tp); + e = rdtsc(); + printf("%lld\n", e - s); +} + +static inline void clflush(volatile void *__p) +{ + asm volatile("clflush %0" : "+m"(*(volatile char *)__p)); +} + +void *pg_addr; +void sigh(int sig) +{ + void *addr; + + addr = mmap(pg_addr, PAGE_SIZE, PROT_READ, + MAP_PRIVATE | MAP_ANONYMOUS | MAP_FIXED, -1, 0); + if (addr != pg_addr) { + pr_perror("Unable to map %lx", (long) pg_addr); + exit(1); + } +} + +int main(int argc, char **argv) +{ + time_t offset = 10; + void *vdso_start = 0, *vdso_end = 0; + void *vvar_start = 0, *vvar_end = 0; + char buf[PAGE_SIZE]; + int nsfd, i; + FILE *maps; + + fill_function_pointers(); + if (argc == 1) + goto out; + 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"); + +out: + maps = fopen("/proc/self/maps", "r"); + if (!maps) { + pr_perror("Unable to open /proc/self/maps"); + return 1; + } + + while (fgets(buf, sizeof(buf), maps)) { + unsigned long start, end; + char tail[PAGE_SIZE]; + int r; + + r = sscanf(buf, "%lx-%lx %*s %*s %*s %*s %s\n", &start, &end, tail); + + if (r < 3) + continue; + + if (strcmp(tail, "[vdso]") == 0) { + vdso_start = (void *)start; + vdso_end = (void *)end; + } + if (strcmp(tail, "[vvar]") == 0) { + vvar_start = (void *)start; + vvar_end = (void *)end; + } + } + if (!vvar_start || !vdso_start) { + pr_err("Unable to find vdso\n"); + return 1; + } + + /* Map zero pages instead of unreadable vdso pages. */ + signal(SIGSEGV, sigh); + signal(SIGBUS, sigh); + for (pg_addr = vdso_start; pg_addr < vdso_end; pg_addr += PAGE_SIZE) + buf[0] += *(char *)pg_addr; + for (pg_addr = vvar_start; pg_addr < vvar_end; pg_addr += PAGE_SIZE) + buf[0] += *(char *)pg_addr; + signal(SIGSEGV, SIG_DFL); + signal(SIGBUS, SIG_DFL); + + for (i = 0; i < 10240; i++) { + void *p; + + for (p = vdso_start; p < vdso_end; p += CACHE_LINE_SIZE) + clflush(p); + for (p = vvar_start; p < vvar_end; p += CACHE_LINE_SIZE) + clflush(p); + test(CLOCK_MONOTONIC, "monotonic"); + } + return 0; +} -- 2.22.0