Received: by 2002:a05:7412:b130:b0:e2:908c:2ebd with SMTP id az48csp2142397rdb; Mon, 20 Nov 2023 03:19:21 -0800 (PST) X-Google-Smtp-Source: AGHT+IG3DcNFySdqlPvLKMEJWmD0Ddh92EBbEi80rDoJs16t9NR6VcCRw+o0XNrnyQixEpz38k0W X-Received: by 2002:a05:6830:3d12:b0:6be:fd51:cb6d with SMTP id eu18-20020a0568303d1200b006befd51cb6dmr4976762otb.31.1700479160741; Mon, 20 Nov 2023 03:19:20 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1700479160; cv=none; d=google.com; s=arc-20160816; b=DtVFTXhz25zzOmF7tppmyT5QTHnl02oXjN5vzGlAiKVQ4kZpXwoxPqT5sS4fCVZxeJ st84aw6detNmRH2swC0IsEq6fIdTcW4jKMPLh73/UQg4tceen8ARQHC+/TOuTIg5DzSH kEToKqn7trW0ltlOJd+qBBTrffwCIB0hfEs4tZnBUXiyiRpq6+/S944t8xe/wxUmf5ij BSTuCuhME+ChNh3e+Pp+v7viz/KButsw0BQyVkMXcDsDxaJKN+lyjC+5y2WYzaPoKnZs HXnYsKFjT1UbPlOjszPAwuM0CpPK2sZM2ofjNSQKY1ubl7k+v090hOe25rLvrskV6sQB 01+A== 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=cDh0+ZFKrV5oXMPsFY32yOJqjB9VA49TYG5ESimHrCU=; fh=gPQ6jqLSfsDb5bE3yrtO+AlT5R4d75RXkjC5xckz7Dk=; b=oW8roLPNakCUOVJEg43xvksLo4VueyCfNxAtBIDjHxPKwM7WrJRcQYL7cLF9G95nrN nYz9pYxlunc2aB7LbPCkqtSKaV7lpGHw9T0gBzvkIW7Cvwm55aYgyfaB5BkTXpHoSowq Lzl1p5GodZzUwhsxz6j/iuRg5YH1c8EbV/SYZKYhAMc7Le3LpkuMDYmiGB9/JmyNnm+h D3wZDzIGpNjbHCzIm3kOiBHfJTtsuUf/Jp/aDF5Teke4gYFQIdPfDjzwnj3rcZ6Qqtga NZiD/8A8BM6YgvjoRC4aK6SZcBoOWv/32bMC+cVt/bw0bPSUEdkt06bGlJaoMXGY4ejH Nbrg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=fPxMa+9p; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.37 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=intel.com Return-Path: Received: from snail.vger.email (snail.vger.email. [23.128.96.37]) by mx.google.com with ESMTPS id h71-20020a636c4a000000b005bd366fd5aasi8020578pgc.464.2023.11.20.03.19.16 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 20 Nov 2023 03:19:20 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.37 as permitted sender) client-ip=23.128.96.37; Authentication-Results: mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=fPxMa+9p; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.37 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=intel.com Received: from out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by snail.vger.email (Postfix) with ESMTP id 0BDB480A0559; Mon, 20 Nov 2023 03:17:28 -0800 (PST) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.11 at snail.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232789AbjKTLRM (ORCPT + 99 others); Mon, 20 Nov 2023 06:17:12 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:32788 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233513AbjKTLQu (ORCPT ); Mon, 20 Nov 2023 06:16:50 -0500 Received: from mgamail.intel.com (mgamail.intel.com [134.134.136.24]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 484371993; Mon, 20 Nov 2023 03:16:19 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1700478979; x=1732014979; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=DktzD/iLAjshXQ2RGQH6sZM3sLDUA4PpyDcphlSqMLI=; b=fPxMa+9psON1urpLzV7gia2YP0sDucc0d4OYzj6ij1r9SaVsB0MUhi5W AbS/kb16z9Cna/NIUu6AIiA/MyZZIsBTdWAjK43UTwZm1RP4GkeYuOzOh rAmm+jCpV96BEn1ku1Wbw2DneW00Kr3bSxN4rqSH+ourMhEVcjCth8vQu FUCu/80JPMBZ941UkMTudA+QmVRJ+N5iDx98vDByDt8TvUscjl/vOEZaK KPmTd2dxQsYTiJlqgrlV5Vn1yQdGR0ZL3FLLg8y3yzQouO4WmmColl+j8 O48QWsQmqXCF2l9senBChjQM+MKWdNkp3k75hzz9UfhzAm5vkXVRIqnJt g==; X-IronPort-AV: E=McAfee;i="6600,9927,10899"; a="394447253" X-IronPort-AV: E=Sophos;i="6.04,213,1695711600"; d="scan'208";a="394447253" Received: from fmviesa002.fm.intel.com ([10.60.135.142]) by orsmga102.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 20 Nov 2023 03:16:18 -0800 X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="6.04,213,1695711600"; d="scan'208";a="7681615" Received: from sc9itsct4906.amr.corp.intel.com (HELO localhost) ([10.249.46.107]) by fmviesa002-auth.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 20 Nov 2023 03:16:15 -0800 From: =?UTF-8?q?Ilpo=20J=C3=A4rvinen?= To: linux-kselftest@vger.kernel.org, Reinette Chatre , Shuah Khan , Shaopeng Tan , =?UTF-8?q?Maciej=20Wiecz=C3=B3r-Retman?= , Fenghua Yu Cc: linux-kernel@vger.kernel.org, =?UTF-8?q?Ilpo=20J=C3=A4rvinen?= Subject: [PATCH v2 17/26] selftests/resctrl: Replace file write with volatile variable Date: Mon, 20 Nov 2023 13:13:31 +0200 Message-Id: <20231120111340.7805-18-ilpo.jarvinen@linux.intel.com> X-Mailer: git-send-email 2.30.2 In-Reply-To: <20231120111340.7805-1-ilpo.jarvinen@linux.intel.com> References: <20231120111340.7805-1-ilpo.jarvinen@linux.intel.com> MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-4.3 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_EF,RCVD_IN_DNSWL_MED, RCVD_IN_MSPIKE_H3,RCVD_IN_MSPIKE_WL,SPF_HELO_NONE,SPF_NONE, T_SCC_BODY_TEXT_LINE 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-Greylist: Sender passed SPF test, not delayed by milter-greylist-4.6.4 (snail.vger.email [0.0.0.0]); Mon, 20 Nov 2023 03:17:28 -0800 (PST) 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 to a variable through a volatile pointer 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 --- 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 b9303a9d819b..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) { - 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 fe07b732cbd9..68eb1f1bac3a 100644 --- a/tools/testing/selftests/resctrl/resctrl.h +++ b/tools/testing/selftests/resctrl/resctrl.h @@ -79,6 +79,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"); -- 2.30.2