From patchwork Fri Dec 15 15:05:06 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: =?utf-8?q?Ilpo_J=C3=A4rvinen?= X-Patchwork-Id: 179361 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:7300:3b04:b0:fb:cd0c:d3e with SMTP id c4csp9345897dys; Fri, 15 Dec 2023 07:11:43 -0800 (PST) X-Google-Smtp-Source: AGHT+IFwXTp6Y371PohhXV1ApelEIAsfIYLSnFOJngbY6+CGkUchIdcyn8y8YQcP+PqssSRopU3Y X-Received: by 2002:a17:907:1ca0:b0:a0c:fe2f:c445 with SMTP id nb32-20020a1709071ca000b00a0cfe2fc445mr6294526ejc.69.1702653103083; Fri, 15 Dec 2023 07:11:43 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1702653103; cv=none; d=google.com; s=arc-20160816; b=IXb1hamRQxmFkAw2wvFWvHEwJdwJfBCIRQcoN5UnjtnPOvTucYwLSOwB9lswx5kHcD c8Rc7T6UvQ65jcQ97tXWOzMiPtkevsRGAU07FQ9Rkwrc9A29lwH3zD8wk1wJSypAz3uj o7527FMtCOB755qYpIIcmdm4KLDTxakkZ8n8pp5YQnRgeYvsHkiCuXV6ACQAK0N6NJbD uPt+0h2P2QtJBSOOQ/wEcNPtOiDl4nKwlsOC7x49dd/yXtGXgpCizIRYocVgfI/SP13O vyVgm8zVNmRHW08eP51tQGJ89Ns87X1FAoOJ2cg4MzvAE2Z8XIsYjbvXOvhlOqIrG+Ic jY+Q== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:mime-version:list-unsubscribe :list-subscribe:list-id:precedence:references:in-reply-to:message-id :date:subject:cc:to:from:dkim-signature; bh=t0f72qFXyQp9KkVGO/E2AVT5RMgwbtlT3v7gvtJ6qi4=; fh=gPQ6jqLSfsDb5bE3yrtO+AlT5R4d75RXkjC5xckz7Dk=; b=rfAAbehFRiuWTTm7L4Po6pYDXjBa3pHMLFN3MxqJq478CZ0FGLB4ZHlOO62oeFMoEG NAtAdzRKMubRdfHZOVEibu4Vy02MhxQiCjVGWHVbC1PNZURBEITbRrIVFyt7woBHcE8J z+O0KilSPX1bIxV/wud47XuHSOXhyeVW4HMRvCq3i+buyMDrbVGBqpMshaO6YqwnlsYx ZdOVCYL70jBNICcHUHjjjN9jzYwlcmdBGzTxr1xazn1y6jBERxm8sprVHq13AIpfjOe3 dCjZ/8+m7l7ipFGQhoNtdjIxlafij7JFGtXzbppIG8AU9O9JiFjV1oaq0XaCCohcllXh q4gQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=Cdbvvknn; spf=pass (google.com: domain of linux-kernel+bounces-1171-ouuuleilei=gmail.com@vger.kernel.org designates 147.75.80.249 as permitted sender) smtp.mailfrom="linux-kernel+bounces-1171-ouuuleilei=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=intel.com Received: from am.mirrors.kernel.org (am.mirrors.kernel.org. [147.75.80.249]) by mx.google.com with ESMTPS id lh26-20020a170906f8da00b00a1d8cacbfd5si7192931ejb.1001.2023.12.15.07.11.42 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 15 Dec 2023 07:11:43 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-1171-ouuuleilei=gmail.com@vger.kernel.org designates 147.75.80.249 as permitted sender) client-ip=147.75.80.249; Authentication-Results: mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=Cdbvvknn; spf=pass (google.com: domain of linux-kernel+bounces-1171-ouuuleilei=gmail.com@vger.kernel.org designates 147.75.80.249 as permitted sender) smtp.mailfrom="linux-kernel+bounces-1171-ouuuleilei=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=intel.com Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by am.mirrors.kernel.org (Postfix) with ESMTPS id 2450F1F250C4 for ; Fri, 15 Dec 2023 15:11:19 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 72D803F8FE; Fri, 15 Dec 2023 15:08:43 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=intel.com header.i=@intel.com header.b="Cdbvvknn" X-Original-To: linux-kernel@vger.kernel.org Received: from mgamail.intel.com (mgamail.intel.com [198.175.65.12]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 726DD3EA8E; Fri, 15 Dec 2023 15:08:39 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.intel.com Authentication-Results: smtp.subspace.kernel.org; spf=none smtp.mailfrom=linux.intel.com DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1702652920; x=1734188920; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=c56pSutKyBL/+RSgpalm2vDnnz/5q/Fld2dkSwdn0Jk=; b=CdbvvknnhcTxeFLKzOr6+zJk3q6dOn7PR47UUqyhS1n4rAuoEDkXlS1T +JNhxMXeolUg8FXrC2r0pm3M/mM2r/S058+4bVeCDjodhv+t3biG2ht9n TOzxS55b46ZHNcVLQQaGZC+c2YDrTkv3PydPhPd1kCA2cBOxGzCeMKxWG 1RhJ7jT4Qo7Cdfa9C7oGfXpco3UL4yK2p0KqME+C6q+KSYig8TElFvBvd PDW5O2qaZPeAZGg5gluQSsIbCVtYxA5K++K9TkvDxTXrmFTsz00BF8GmE ln1rBQRBHflPFUVUV/5aTx6lla+h28ONSegQ3SYmc/AwNp6VucU+o32Vi Q==; X-IronPort-AV: E=McAfee;i="6600,9927,10924"; a="2453898" X-IronPort-AV: E=Sophos;i="6.04,279,1695711600"; d="scan'208";a="2453898" Received: from orsmga001.jf.intel.com ([10.7.209.18]) by orvoesa104.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 15 Dec 2023 07:08:39 -0800 X-ExtLoop1: 1 X-IronPort-AV: E=McAfee;i="6600,9927,10924"; a="809001431" X-IronPort-AV: E=Sophos;i="6.04,279,1695711600"; d="scan'208";a="809001431" Received: from ijarvine-desk1.ger.corp.intel.com (HELO localhost) ([10.246.49.116]) by orsmga001-auth.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 15 Dec 2023 07:08:35 -0800 From: =?utf-8?q?Ilpo_J=C3=A4rvinen?= To: linux-kselftest@vger.kernel.org, Reinette Chatre , Shuah Khan , Shaopeng Tan , =?utf-8?q?Maciej_Wiecz=C3=B3r-R?= =?utf-8?q?etman?= , Fenghua Yu Cc: linux-kernel@vger.kernel.org, =?utf-8?q?Ilpo_J=C3=A4rvinen?= Subject: [PATCH v4 20/29] selftests/resctrl: Replace file write with volatile variable Date: Fri, 15 Dec 2023 17:05:06 +0200 Message-Id: <20231215150515.36983-21-ilpo.jarvinen@linux.intel.com> X-Mailer: git-send-email 2.30.2 In-Reply-To: <20231215150515.36983-1-ilpo.jarvinen@linux.intel.com> References: <20231215150515.36983-1-ilpo.jarvinen@linux.intel.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1785361179926085760 X-GMAIL-MSGID: 1785361179926085760 The fill_buf code prevents compiler optimizating the entire read loop away by writing the final value of the variable into a file. While it achieves the goal, writing into a file requires significant amount of work within the innermost test loop and also error handling. A simpler approach is to take advantage of volatile. Writing through a pointer to a volatile variable is enough to prevent compiler from optimizing the write away, and therefore compiler cannot remove the read loop either. Add a volatile 'value_sink' into resctrl_tests.c and make fill_buf to write into it. As a result, the error handling in fill_buf.c can be simplified. Signed-off-by: Ilpo Järvinen Reviewed-by: Reinette Chatre --- v3: - Altered changelog's wording to not say "a volatile pointer" --- tools/testing/selftests/resctrl/fill_buf.c | 26 ++++--------------- tools/testing/selftests/resctrl/resctrl.h | 7 +++++ .../testing/selftests/resctrl/resctrl_tests.c | 4 +++ 3 files changed, 16 insertions(+), 21 deletions(-) diff --git a/tools/testing/selftests/resctrl/fill_buf.c b/tools/testing/selftests/resctrl/fill_buf.c index 635f938b11f0..8fe9574db9d8 100644 --- a/tools/testing/selftests/resctrl/fill_buf.c +++ b/tools/testing/selftests/resctrl/fill_buf.c @@ -78,10 +78,9 @@ static void fill_one_span_write(unsigned char *buf, size_t buf_size) } } -static int fill_cache_read(unsigned char *buf, size_t buf_size, bool once) +static void fill_cache_read(unsigned char *buf, size_t buf_size, bool once) { int ret = 0; - FILE *fp; while (1) { ret = fill_one_span_read(buf, buf_size); @@ -90,26 +89,16 @@ static int fill_cache_read(unsigned char *buf, size_t buf_size, bool once) } /* Consume read result so that reading memory is not optimized out. */ - fp = fopen("/dev/null", "w"); - if (!fp) { - ksft_perror("Unable to write to /dev/null"); - return -1; - } - fprintf(fp, "Sum: %d ", ret); - fclose(fp); - - return 0; + *value_sink = ret; } -static int fill_cache_write(unsigned char *buf, size_t buf_size, bool once) +static void fill_cache_write(unsigned char *buf, size_t buf_size, bool once) { while (1) { fill_one_span_write(buf, buf_size); if (once) break; } - - return 0; } static unsigned char *alloc_buffer(size_t buf_size, int memflush) @@ -143,21 +132,16 @@ static unsigned char *alloc_buffer(size_t buf_size, int memflush) int run_fill_buf(size_t buf_size, int memflush, int op, bool once) { unsigned char *buf; - int ret; buf = alloc_buffer(buf_size, memflush); if (!buf) return -1; if (op == 0) - ret = fill_cache_read(buf, buf_size, once); + fill_cache_read(buf, buf_size, once); else - ret = fill_cache_write(buf, buf_size, once); + fill_cache_write(buf, buf_size, once); free(buf); - if (ret) { - printf("\n Error in fill cache\n"); - return -1; - } return 0; } diff --git a/tools/testing/selftests/resctrl/resctrl.h b/tools/testing/selftests/resctrl/resctrl.h index f22b7897251e..5b9bea505120 100644 --- a/tools/testing/selftests/resctrl/resctrl.h +++ b/tools/testing/selftests/resctrl/resctrl.h @@ -78,6 +78,13 @@ struct perf_event_read { #define CMT_STR "cmt" #define CAT_STR "cat" +/* + * Memory location that consumes values compiler must not optimize away. + * Volatile ensures writes to this location cannot be optimized away by + * compiler. + */ +extern volatile int *value_sink; + extern pid_t bm_pid, ppid; extern char llc_occup_path[1024]; diff --git a/tools/testing/selftests/resctrl/resctrl_tests.c b/tools/testing/selftests/resctrl/resctrl_tests.c index 2bbe3045a018..f0d573aa363b 100644 --- a/tools/testing/selftests/resctrl/resctrl_tests.c +++ b/tools/testing/selftests/resctrl/resctrl_tests.c @@ -10,6 +10,10 @@ */ #include "resctrl.h" +/* Volatile memory sink to prevent compiler optimizations */ +static volatile int sink_target; +volatile int *value_sink = &sink_target; + static int detect_vendor(void) { FILE *inf = fopen("/proc/cpuinfo", "r");