From patchwork Wed Feb 22 14:46:49 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Frederic Weisbecker X-Patchwork-Id: 60568 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a5d:5915:0:0:0:0:0 with SMTP id v21csp619085wrd; Wed, 22 Feb 2023 06:54:20 -0800 (PST) X-Google-Smtp-Source: AK7set+sZicC9YhDcGXSupO6m1HGo5DNqgiLoADJ+xR4Rw7PNxo4K7VNCGaH2Y2pvG4gY2bOeqk3 X-Received: by 2002:a17:906:489:b0:879:ab3:93d1 with SMTP id f9-20020a170906048900b008790ab393d1mr16618088eja.4.1677077660004; Wed, 22 Feb 2023 06:54:20 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1677077659; cv=none; d=google.com; s=arc-20160816; b=wDDyHuX3UyEVTivIgtMvFq/XolXYl7xD15hcxOADfiLtJSuFbRoaLGdQESfXm3Zbxn czUXbHi4kz3OxH25wQbgBdTVLsjA2Aztm9rbuNCGSgIFzLqeiCYAxVOdvHrfRKjlwo/7 D0tYwlayTIrByxWMcEFe81kElzF3sQwSnEE/NXzI6XAPLlbUZO7mA5+XHqwcxqnNgEIh KRg+2eSbgTBiOWY60tsEEMS4BZQEu7wD47MCOnAKoDNedxQ5I2aNQYef3CFtDXF/TZRz OqKsB6HIYj+PHz94tohx0isFVrP70fiucMHQSTrJY7OfzLuu1W40z5Pipl8Hkw7V889l SfHQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=D2JmexIW+UHfXP2WtCzOlQ001Sk+aLFj/Twgy2DczE0=; b=HT6VAt5XjwQaUPeRM9SsvKBWpsA52lceJNhn4aI2Qg7eLR3IV6edjIIHPnyLCv8AZa z76bKJAUB/q2fWyzC2hoHUCBefBIpn4j/kLWkqjEDTPeChg/Z5sDSsLU1cD2uNGjQ7+C 0irsHh2ZUeBSfe+fSsm82LB5HTODpDOV+1/bcmSXkg1vt3OAimnR3BHImGF0E1jjBvVA VzBc7MYuAygkeA+4hFaZGKlO4QN77OUJ/kpzSuwFHQWFM8Ebz9Mk05hU+8EcXlsgFOSU IvvJu52MKweJpEZ6YvCFtUfLz5x3NfiIMY3H2UVb2vRY3cw+fs1DhNpfDoNx7XeTEhiK qlbw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b="O/SXFOf0"; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id gj19-20020a170907741300b008b84c44d603si14773349ejc.388.2023.02.22.06.53.45; Wed, 22 Feb 2023 06:54:19 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b="O/SXFOf0"; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232302AbjBVOr7 (ORCPT + 99 others); Wed, 22 Feb 2023 09:47:59 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:59326 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232201AbjBVOrh (ORCPT ); Wed, 22 Feb 2023 09:47:37 -0500 Received: from ams.source.kernel.org (ams.source.kernel.org [IPv6:2604:1380:4601:e00::1]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 8249F38EB6 for ; Wed, 22 Feb 2023 06:47:21 -0800 (PST) Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ams.source.kernel.org (Postfix) with ESMTPS id BC6C2B815C9 for ; Wed, 22 Feb 2023 14:47:18 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 60642C4339E; Wed, 22 Feb 2023 14:47:15 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1677077237; bh=3ds3/cTbh0yU84XZ4nwDnOjFVf7ivtfK68suy9ql4Kw=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=O/SXFOf0UVWM/2lQuF9pRCQJD7/Qjp1ltLC72UF7I04QPqLkTNXiYIUbh83lE8D6P qI/1/V3Rz/i9ea+1Q7TIPhxLJC0epNbZysQD2oisZBi73sQYcCq8mBgRma8Mv4S8KI 79IkEWklYyfvR/pp5se81yl7XOoFJZ50LTqpYdgtEHNd4klwuarw0kxpeDfNnr3zmJ LqBU2SnACBrx2yZ5SKNg3m9UjybeHnfUTlc8GatJcYbBnJE3YsVNSgvPMSJLS7FHg/ M9dIgk/6Kf7kqCTGT7u45sBjDL//mMflCmQpOjNH+k/XnbdQf2cyCbbY1Jg6OGcI9E FF1NHlqN4bxfw== From: Frederic Weisbecker To: Thomas Gleixner Cc: LKML , Frederic Weisbecker , Alexey Dobriyan , Wei Li , Peter Zijlstra , Mirsad Goran Todorovac , Yu Liao , Hillf Danton , Ingo Molnar Subject: [PATCH 8/8] selftests/proc: Assert clock_gettime(CLOCK_BOOTTIME) VS /proc/uptime monotonicity Date: Wed, 22 Feb 2023 15:46:49 +0100 Message-Id: <20230222144649.624380-9-frederic@kernel.org> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20230222144649.624380-1-frederic@kernel.org> References: <20230222144649.624380-1-frederic@kernel.org> MIME-Version: 1.0 X-Spam-Status: No, score=-4.4 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_MED, SPF_HELO_NONE,SPF_PASS autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1758543384300603834?= X-GMAIL-MSGID: =?utf-8?q?1758543384300603834?= The first field of /proc/uptime relies on the CLOCK_BOOTTIME clock which can also be fetched from clock_gettime() API. Improve the test coverage while verifying the monotonicity of CLOCK_BOOTTIME accross both interfaces. Suggested-by: Thomas Gleixner Cc: Yu Liao Cc: Hillf Danton Cc: Ingo Molnar Cc: Wei Li Cc: Alexey Dobriyan Cc: Mirsad Goran Todorovac Cc: Peter Zijlstra Signed-off-by: Frederic Weisbecker --- .../testing/selftests/proc/proc-uptime-001.c | 21 ++++++++++++++---- .../testing/selftests/proc/proc-uptime-002.c | 22 +++++++++++++++---- tools/testing/selftests/proc/proc-uptime.h | 12 ++++++++++ 3 files changed, 47 insertions(+), 8 deletions(-) diff --git a/tools/testing/selftests/proc/proc-uptime-001.c b/tools/testing/selftests/proc/proc-uptime-001.c index 35bddd9dd60b..f335eec5067e 100644 --- a/tools/testing/selftests/proc/proc-uptime-001.c +++ b/tools/testing/selftests/proc/proc-uptime-001.c @@ -13,9 +13,9 @@ * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. */ -// Test that boottime value in /proc/uptime increments monotonically. -// We don't test idle time monotonicity due to broken iowait task -// counting, cf: comment above get_cpu_idle_time_us() +// Test that boottime value in /proc/uptime and CLOCK_BOOTTIME increment +// monotonically. We don't test idle time monotonicity due to broken iowait +// task counting, cf: comment above get_cpu_idle_time_us() #undef NDEBUG #include #include @@ -27,7 +27,7 @@ int main(void) { - uint64_t start, u0, u1; + uint64_t start, u0, u1, c0, c1; int fd; fd = open("/proc/uptime", O_RDONLY); @@ -35,10 +35,23 @@ int main(void) u0 = proc_uptime(fd); start = u0; + c0 = clock_boottime(); + do { u1 = proc_uptime(fd); + c1 = clock_boottime(); + + /* Is /proc/uptime monotonic ? */ assert(u1 >= u0); + + /* Is CLOCK_BOOTTIME monotonic ? */ + assert(c1 >= c0); + + /* Is CLOCK_BOOTTIME VS /proc/uptime monotonic ? */ + assert(c0 >= u0); + u0 = u1; + c0 = c1; } while (u1 - start < 100); return 0; diff --git a/tools/testing/selftests/proc/proc-uptime-002.c b/tools/testing/selftests/proc/proc-uptime-002.c index 7ad79d5eaa84..ae453daa96c1 100644 --- a/tools/testing/selftests/proc/proc-uptime-002.c +++ b/tools/testing/selftests/proc/proc-uptime-002.c @@ -13,9 +13,10 @@ * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. */ -// Test that boottime value in /proc/uptime increments monotonically -// while shifting across CPUs. We don't test idle time monotonicity -// due to broken iowait task counting, cf: comment above get_cpu_idle_time_us() +// Test that boottime value in /proc/uptime and CLOCK_BOOTTIME increment +// monotonically while shifting across CPUs. We don't test idle time +// monotonicity due to broken iowait task counting, cf: comment above +// get_cpu_idle_time_us() #undef NDEBUG #include #include @@ -43,10 +44,10 @@ static inline int sys_sched_setaffinity(pid_t pid, unsigned int len, unsigned lo int main(void) { + uint64_t u0, u1, c0, c1; unsigned int len; unsigned long *m; unsigned int cpu; - uint64_t u0, u1; int fd; /* find out "nr_cpu_ids" */ @@ -62,6 +63,8 @@ int main(void) assert(fd >= 0); u0 = proc_uptime(fd); + c0 = clock_boottime(); + for (cpu = 0; cpu < len * 8; cpu++) { memset(m, 0, len); m[cpu / (8 * sizeof(unsigned long))] |= 1UL << (cpu % (8 * sizeof(unsigned long))); @@ -70,8 +73,19 @@ int main(void) sys_sched_setaffinity(0, len, m); u1 = proc_uptime(fd); + c1 = clock_boottime(); + + /* Is /proc/uptime monotonic ? */ assert(u1 >= u0); + + /* Is CLOCK_BOOTTIME monotonic ? */ + assert(c1 >= c0); + + /* Is CLOCK_BOOTTIME VS /proc/uptime monotonic ? */ + assert(c0 >= u0); + u0 = u1; + c0 = c1; } return 0; diff --git a/tools/testing/selftests/proc/proc-uptime.h b/tools/testing/selftests/proc/proc-uptime.h index ca55abeb0ccc..730cce4a3d73 100644 --- a/tools/testing/selftests/proc/proc-uptime.h +++ b/tools/testing/selftests/proc/proc-uptime.h @@ -19,9 +19,21 @@ #include #include #include +#include #include "proc.h" +static uint64_t clock_boottime(void) +{ + struct timespec ts; + int err; + + err = clock_gettime(CLOCK_BOOTTIME, &ts); + assert(err >= 0); + + return (ts.tv_sec * 100) + (ts.tv_nsec / 10000000); +} + static uint64_t proc_uptime(int fd) { uint64_t val1, val2;