Received: by 2002:a05:6a10:206:0:0:0:0 with SMTP id 6csp5237503pxj; Wed, 26 May 2021 06:10:37 -0700 (PDT) X-Google-Smtp-Source: ABdhPJxhVEP004+bmSPo916t+VNzNhOzY5Ou8IHDQCuaK6GkzFRtxNomnxoTEpTaYXgN4PqC6xu/ X-Received: by 2002:a05:6402:22fa:: with SMTP id dn26mr36570178edb.5.1622034636765; Wed, 26 May 2021 06:10:36 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1622034636; cv=none; d=google.com; s=arc-20160816; b=w2cnX9fZtOrSUNFx81i+M3rrbXBDA5BVsA6lpUYkV1RadEpp1vwhcKeE8AJUbKT3Ql 2PK6tp2Slp8xqLUIQxxlWFZm1MU91jyMlREv7QoPX2TTIF8eAml0EEJhPup/Q4R9yfR3 Efvg15FglqiAOr4iHLtwxfEiTCCS6xUs4F9jYKyZB4yt+6HNCtiIMBsyfp2PQNedXqkz 0VQeD5sBqElEVtdnCF4qK3b+VwDKOJxIbw7Zm72bG6oYm5k34F8G2fVlQM5Vj3RuYFEZ +15iaV0PnorrfX6Voct8ElrTRW7aZ3aLvoYJWg9hR+ee/rKuX5RMgvYbq9cFzxDHokue wWvQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:in-reply-to:content-disposition:mime-version :references:message-id:subject:cc:to:from:date:dkim-signature; bh=5/M4Zi1UD0F4y7iC9n27EXxdTv0jPB/LjVTE/VRiZvQ=; b=kv9OIQmiVYHsQ8QnxD+FD//HnTUgGql+SKNqSy3Lnjgt6WcfUMQ/JSekfb837apYhP q1yRx70o+iq2cz/IcgF4Lezq2hWTWakkVneCXCp6TWUFY5ngsoho1BTFtE4L2Wi5tzol v/6OfZQoA2UDURaIXMvFQ4hUdyI8yLSdq1+pNzOUf8rsBpWczMI0DZz5GOBOsFMQYsHp watycQFTpS302FWtDO/tv4/2FZVR8DnSEsau0KLAJIaSBOaoJUhTutHPD7pQScNk33SH 31qvbY/lBVv/18DmVFWpkUVgueDF8fyVxBYDr/cqc/OUdTY0Yg5L8DQU3coi8yONF4LG c9xA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=HCXsFUGc; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id p24si17275539edr.329.2021.05.26.06.10.12; Wed, 26 May 2021 06:10:36 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=HCXsFUGc; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233354AbhEZMfj (ORCPT + 99 others); Wed, 26 May 2021 08:35:39 -0400 Received: from us-smtp-delivery-124.mimecast.com ([170.10.133.124]:55365 "EHLO us-smtp-delivery-124.mimecast.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233221AbhEZMfg (ORCPT ); Wed, 26 May 2021 08:35:36 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1622032444; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: in-reply-to:in-reply-to:references:references; bh=5/M4Zi1UD0F4y7iC9n27EXxdTv0jPB/LjVTE/VRiZvQ=; b=HCXsFUGcj5Jht333LzpXZwJFvw32TIBigCE9xpV8tz6q/ABisXiVnh7cUVdQYyzolHcrue 5DwRrl+6IX+I3hIADpR3saXkLaWR9Bt1ZbF9rUlc87gzCXw6oS+m6zNzmmW3ZppSIb2y69 HYH+Sq5mTmgE8XZckl1qayVlZrljhYU= Received: from mimecast-mx01.redhat.com (mimecast-mx01.redhat.com [209.132.183.4]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-390-twY8WkrSOACUVVWzQT69EQ-1; Wed, 26 May 2021 08:34:02 -0400 X-MC-Unique: twY8WkrSOACUVVWzQT69EQ-1 Received: from smtp.corp.redhat.com (int-mx02.intmail.prod.int.phx2.redhat.com [10.5.11.12]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id 7675B80ED8E; Wed, 26 May 2021 12:33:59 +0000 (UTC) Received: from krava (unknown [10.40.195.164]) by smtp.corp.redhat.com (Postfix) with SMTP id 5AFC45044F; Wed, 26 May 2021 12:33:56 +0000 (UTC) Date: Wed, 26 May 2021 14:33:55 +0200 From: Jiri Olsa To: Riccardo Mancini Cc: Arnaldo Carvalho de Melo , Namhyung Kim , Ian Rogers , Peter Zijlstra , Ingo Molnar , Mark Rutland , Alexander Shishkin , Leo Yan , Fabian Hemmer , Tommi Rantala , Stephane Eranian , linux-perf-users@vger.kernel.org, linux-kernel@vger.kernel.org Subject: Re: [PATCH] perf test: iterate over shell tests in alphabetical order Message-ID: References: <20210525230521.244553-1-rickyman7@gmail.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20210525230521.244553-1-rickyman7@gmail.com> X-Scanned-By: MIMEDefang 2.79 on 10.5.11.12 Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Wed, May 26, 2021 at 01:05:17AM +0200, Riccardo Mancini wrote: > for_each_shell_test macro iterated over all shell tests in the directory > using readdir, which does not guarantee any ordering, causing > problems on certain fs. However, the order in which they are visited > determines the id of the test, in case one wants to run a single test. > > This patch replaces readdir with scandir using alphabetical sorting. > This guarantees that, given the same set of tests, all machines will > see the tests in the same order, and, thus, that test ids are > consistent. > > Signed-off-by: Riccardo Mancini > Cc: Arnaldo Carvalho de Melo > Cc: Namhyung Kim > Cc: Ian Rogers Acked-by: Jiri Olsa thanks, jirka > --- > tools/perf/tests/builtin-test.c | 38 ++++++++++++++++++--------------- > 1 file changed, 21 insertions(+), 17 deletions(-) > > diff --git a/tools/perf/tests/builtin-test.c b/tools/perf/tests/builtin-test.c > index c4b888f18e9ca..cbbfe48ab8029 100644 > --- a/tools/perf/tests/builtin-test.c > +++ b/tools/perf/tests/builtin-test.c > @@ -510,8 +510,8 @@ static const char *shell_test__description(char *description, size_t size, > return description ? strim(description + 1) : NULL; > } > > -#define for_each_shell_test(dir, base, ent) \ > - while ((ent = readdir(dir)) != NULL) \ > +#define for_each_shell_test(entlist, nr, base, ent) \ > + for (int __i = 0; __i < nr && (ent = entlist[__i]); __i++) \ > if (!is_directory(base, ent) && ent->d_name[0] != '.') > > static const char *shell_tests__dir(char *path, size_t size) > @@ -538,8 +538,9 @@ static const char *shell_tests__dir(char *path, size_t size) > > static int shell_tests__max_desc_width(void) > { > - DIR *dir; > + struct dirent **entlist; > struct dirent *ent; > + int n_dirs; > char path_dir[PATH_MAX]; > const char *path = shell_tests__dir(path_dir, sizeof(path_dir)); > int width = 0; > @@ -547,11 +548,11 @@ static int shell_tests__max_desc_width(void) > if (path == NULL) > return -1; > > - dir = opendir(path); > - if (!dir) > + n_dirs = scandir(path, &entlist, NULL, alphasort); > + if (n_dirs == -1) > return -1; > > - for_each_shell_test(dir, path, ent) { > + for_each_shell_test(entlist, n_dirs, path, ent) { > char bf[256]; > const char *desc = shell_test__description(bf, sizeof(bf), path, ent->d_name); > > @@ -563,7 +564,8 @@ static int shell_tests__max_desc_width(void) > } > } > > - closedir(dir); > + free(entlist); > + > return width; > } > > @@ -589,8 +591,9 @@ static int shell_test__run(struct test *test, int subdir __maybe_unused) > > static int run_shell_tests(int argc, const char *argv[], int i, int width) > { > - DIR *dir; > + struct dirent **entlist; > struct dirent *ent; > + int n_dirs; > char path_dir[PATH_MAX]; > struct shell_test st = { > .dir = shell_tests__dir(path_dir, sizeof(path_dir)), > @@ -599,14 +602,14 @@ static int run_shell_tests(int argc, const char *argv[], int i, int width) > if (st.dir == NULL) > return -1; > > - dir = opendir(st.dir); > - if (!dir) { > + n_dirs = scandir(st.dir, &entlist, NULL, alphasort); > + if (n_dirs == -1) { > pr_err("failed to open shell test directory: %s\n", > st.dir); > return -1; > } > > - for_each_shell_test(dir, st.dir, ent) { > + for_each_shell_test(entlist, n_dirs, st.dir, ent) { > int curr = i++; > char desc[256]; > struct test test = { > @@ -623,7 +626,7 @@ static int run_shell_tests(int argc, const char *argv[], int i, int width) > test_and_print(&test, false, -1); > } > > - closedir(dir); > + free(entlist); > return 0; > } > > @@ -722,19 +725,20 @@ static int __cmd_test(int argc, const char *argv[], struct intlist *skiplist) > > static int perf_test__list_shell(int argc, const char **argv, int i) > { > - DIR *dir; > + struct dirent **entlist; > struct dirent *ent; > + int n_dirs; > char path_dir[PATH_MAX]; > const char *path = shell_tests__dir(path_dir, sizeof(path_dir)); > > if (path == NULL) > return -1; > > - dir = opendir(path); > - if (!dir) > + n_dirs = scandir(path, &entlist, NULL, alphasort); > + if (n_dirs == -1) > return -1; > > - for_each_shell_test(dir, path, ent) { > + for_each_shell_test(entlist, n_dirs, path, ent) { > int curr = i++; > char bf[256]; > struct test t = { > @@ -747,7 +751,7 @@ static int perf_test__list_shell(int argc, const char **argv, int i) > pr_info("%2d: %s\n", i, t.desc); > } > > - closedir(dir); > + free(entlist); > return 0; > } > > -- > 2.31.1 >