From patchwork Fri Dec 15 15:05:07 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: 179370 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:7300:3b04:b0:fb:cd0c:d3e with SMTP id c4csp9347756dys; Fri, 15 Dec 2023 07:14:02 -0800 (PST) X-Google-Smtp-Source: AGHT+IF82I+x/6enqW80mX1FNDycMmLExd+tenIywfKnH3mfx+/2Em8GPdgsTxz1EVRDRmBqwXO0 X-Received: by 2002:a17:90a:65c5:b0:286:76e6:7b26 with SMTP id i5-20020a17090a65c500b0028676e67b26mr6278222pjs.36.1702653242071; Fri, 15 Dec 2023 07:14:02 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1702653242; cv=none; d=google.com; s=arc-20160816; b=a9vpDz4JqeKykwgY5CtLEtqmqDcNxV4bw8slnnfe32o00OXznordrWtTwlLu6th9vW CYuCgiGlkwXxMh/EeOPLB/tZuCmBl2dUzZRqy6a0W1+x/EpZoJ5cbQyAQOMUm/VP85ho UoveSJHrprmOq9U7bpzYtt4ACxSGLbsI4Ay7EIIXspJfJLrNhuxFA9TKPJTmyzq2d+/C 6x+Ur/0JVmLTeidkFi2uwLrEzXZRT/0Vn0Bswz2EjfFXoub9Hd0nyjpZyTWGkeKhFBkQ qWm+cxEXaPek0XQmISw0fJyD8dIyFgAVeSDJ7h/QI4HeJSORiFV2DQfUV8G78mI8OUTY 6wYA== 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=n14K3vpsWiDVqYnMHD9QXAkYa683LqFs6d58henc3YI=; fh=gPQ6jqLSfsDb5bE3yrtO+AlT5R4d75RXkjC5xckz7Dk=; b=rh4qmVuMDD5kOMpTsjOSUR5wBx1SIodbGssKAj3Ni/1kvIgbON009dlqYAZaY3USqS P6Mo8ecS2LI7g4wp0dkYbGNZAAwbGq6wsWk5S5tgCzWgdj2/YcMwMQKx3/jDs8zLSDc9 +qgd/kwTCoBL9adiRjtyySsaAJA82Htsr13P4lw23j7CQ7JjH2YEMPj+inI7+L9aG7s+ Xx27FFXAI3vJJ5RNAYh5BNPgArjIskAbJLSYrqCnOF8HbtPyXDe+/ZMhp08Zi/nKbidE LHqJSGv/DbJfHIQ6ZqyrJ9BkjhTFXK0IGLKXihD5txZSSrLHDCQm7+ndf37YWHROJirx 2OfA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=oFaDRqQi; spf=pass (google.com: domain of linux-kernel+bounces-1172-ouuuleilei=gmail.com@vger.kernel.org designates 147.75.48.161 as permitted sender) smtp.mailfrom="linux-kernel+bounces-1172-ouuuleilei=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=intel.com Received: from sy.mirrors.kernel.org (sy.mirrors.kernel.org. [147.75.48.161]) by mx.google.com with ESMTPS id e6-20020a17090a804600b0028b31f41235si928172pjw.182.2023.12.15.07.14.01 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 15 Dec 2023 07:14:02 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-1172-ouuuleilei=gmail.com@vger.kernel.org designates 147.75.48.161 as permitted sender) client-ip=147.75.48.161; Authentication-Results: mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=oFaDRqQi; spf=pass (google.com: domain of linux-kernel+bounces-1172-ouuuleilei=gmail.com@vger.kernel.org designates 147.75.48.161 as permitted sender) smtp.mailfrom="linux-kernel+bounces-1172-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 sy.mirrors.kernel.org (Postfix) with ESMTPS id CE394B20C5D for ; Fri, 15 Dec 2023 15:11:34 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 00CC83FE32; Fri, 15 Dec 2023 15:08:55 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=intel.com header.i=@intel.com header.b="oFaDRqQi" X-Original-To: linux-kernel@vger.kernel.org Received: from mgamail.intel.com (mgamail.intel.com [192.55.52.151]) (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 27D2D3FB20; Fri, 15 Dec 2023 15:08:48 +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=1702652929; x=1734188929; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=Vf+YhSbbVKKBX9ioybzBZ0uA7UfvYFjvulm8AvtaAIE=; b=oFaDRqQi4hy2AnlgBDiMeiBxMyBhiQxNZHefCUrb1ATJ9IwcYt5UDy9Z sI7kjXMdr607Na+eKEQSBuYXKJMxBeYCqq56+0zo6JOk1dTN86xCQVWyI 3a22oI8KAxTfsmvbwIDB2dnoOP7uUFFFOPXUb2THIAU1Cjg1vWCL47mao X0WVdUjqm2+4j5RpIDNXtGxtAMGOsFqwOq1a2qq1pLFqX24EElfVP8G3e wDHja+WzgxGJJFSp8ZUX86LYbeGVWIGpgGKk61oWTnrRJqSwYs9bZje8i o9pj4i2r4dspLkVgCynahJaJulYbykb7jtrZwVazm2FwkmRttO+fKbiCR w==; X-IronPort-AV: E=McAfee;i="6600,9927,10924"; a="375433510" X-IronPort-AV: E=Sophos;i="6.04,279,1695711600"; d="scan'208";a="375433510" Received: from fmsmga007.fm.intel.com ([10.253.24.52]) by fmsmga107.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 15 Dec 2023 07:08:47 -0800 X-ExtLoop1: 1 X-IronPort-AV: E=McAfee;i="6600,9927,10924"; a="778289242" X-IronPort-AV: E=Sophos;i="6.04,279,1695711600"; d="scan'208";a="778289242" Received: from ijarvine-desk1.ger.corp.intel.com (HELO localhost) ([10.246.49.116]) by fmsmga007-auth.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 15 Dec 2023 07:08:44 -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 21/29] selftests/resctrl: Read in less obvious order to defeat prefetch optimizations Date: Fri, 15 Dec 2023 17:05:07 +0200 Message-Id: <20231215150515.36983-22-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: 1785361326189006119 X-GMAIL-MSGID: 1785361326189006119 When reading memory in order, HW prefetching optimizations will interfere with measuring how caches and memory are being accessed. This adds noise into the results. Change the fill_buf reading loop to not use an obvious in-order access using multiply by a prime and modulo. Using a prime multiplier with modulo ensures the entire buffer is eventually read. 23 is small enough that the reads are spread out but wrapping does not occur very frequently (wrapping too often can trigger L2 hits more frequently which causes noise to the test because getting the data from LLC is not required). It was discovered that not all primes work equally well and some can cause wildly unstable results (e.g., in an earlier version of this patch, the reads were done in reversed order and 59 was used as the prime resulting in unacceptably high and unstable results in MBA and MBM test on some architectures). Link: https://lore.kernel.org/linux-kselftest/TYAPR01MB6330025B5E6537F94DA49ACB8B499@TYAPR01MB6330.jpnprd01.prod.outlook.com/ Signed-off-by: Ilpo Järvinen Reviewed-by: Reinette Chatre --- tools/testing/selftests/resctrl/fill_buf.c | 38 +++++++++++++++++----- 1 file changed, 30 insertions(+), 8 deletions(-) diff --git a/tools/testing/selftests/resctrl/fill_buf.c b/tools/testing/selftests/resctrl/fill_buf.c index 8fe9574db9d8..93a3d408339c 100644 --- a/tools/testing/selftests/resctrl/fill_buf.c +++ b/tools/testing/selftests/resctrl/fill_buf.c @@ -51,16 +51,38 @@ static void mem_flush(unsigned char *buf, size_t buf_size) sb(); } +/* + * Buffer index step advance to workaround HW prefetching interfering with + * the measurements. + * + * Must be a prime to step through all indexes of the buffer. + * + * Some primes work better than others on some architectures (from MBA/MBM + * result stability point of view). + */ +#define FILL_IDX_MULT 23 + static int fill_one_span_read(unsigned char *buf, size_t buf_size) { - unsigned char *end_ptr = buf + buf_size; - unsigned char sum, *p; - - sum = 0; - p = buf; - while (p < end_ptr) { - sum += *p; - p += (CL_SIZE / 2); + unsigned int size = buf_size / (CL_SIZE / 2); + unsigned int i, idx = 0; + unsigned char sum = 0; + + /* + * Read the buffer in an order that is unexpected by HW prefetching + * optimizations to prevent them interfering with the caching pattern. + * + * The read order is (in terms of halves of cachelines): + * i * FILL_IDX_MULT % size + * The formula is open-coded below to avoiding modulo inside the loop + * as it improves MBA/MBM result stability on some architectures. + */ + for (i = 0; i < size; i++) { + sum += buf[idx * (CL_SIZE / 2)]; + + idx += FILL_IDX_MULT; + while (idx >= size) + idx -= size; } return sum;