From nobody Mon Sep 16 19:32:04 2024 Received: from mail-yw1-f202.google.com (mail-yw1-f202.google.com [209.85.128.202]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id BB6ED38DE6 for ; Wed, 21 Feb 2024 03:42:05 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.128.202 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1708486927; cv=none; b=eCv+nGSQBmQYOk6Na9xrPmffHZJ4SO9/Qo3b2dcdx3se+bRv7yg7gchcvu4PF3zK8TLG3SlB7ydi5AnPMWIZA7zhCFJ1korA3o7A2hylsjQal9C69/7hVqWHMmtoP+r6ldsvvQ4rnDz6t5pntLybOduqiR4VotioTtuACgULSfM= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1708486927; c=relaxed/simple; bh=o/osGCI+hvlD14sD3OCM5ESdLYEEWx1V7jgoXENiCKc=; h=Date:In-Reply-To:Message-Id:Mime-Version:References:Subject:From: To:Content-Type; b=bZ9mz9mnXPRTFPibPRgfUKv6IlyzWnFf29chPDpm7EF3IOO6BDuZJlC9AeTslrevU2RiDRoNqg1Lv/PMkzjsqMyCNPr22lCVyBV/ilTEckBdRt4weoydtQ7+sT29JTAuf0hMmVSzXkbvVU9hHlPe33ImFXpkM00AOafUb3cHyuI= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--irogers.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=bvs0jjKY; arc=none smtp.client-ip=209.85.128.202 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--irogers.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="bvs0jjKY" Received: by mail-yw1-f202.google.com with SMTP id 00721157ae682-607cd6c11d7so89979957b3.0 for ; Tue, 20 Feb 2024 19:42:05 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1708486925; x=1709091725; darn=vger.kernel.org; h=to:from:subject:references:mime-version:message-id:in-reply-to:date :from:to:cc:subject:date:message-id:reply-to; bh=uv/9NgKdLz0z52mqtvy0vTSbbBgaPJcb0Vk69+L979Q=; b=bvs0jjKYG21ZkB+YgfE5BE740UXuO77Aq3aGQVuxlIvR4Api1Wd4UkAzo8tzSYpyxu qgIcKEqtQo9kzYmWGUOYEzQqHm3T419K97jcCG6JD54rA6kt9TW3725qnQ91FWLCSKld DkO2R3m1FoX6F5W9P+Lz18oSTagIl5/cO6lUWg1G7Uw/eXm39MRhHadPE9CjR24Tt3FS w8g6c+vm1zP/XJcMZJ9rTkmPOW5IHsHgB9NA5KU0rMlj3wY9F4rKgehtcnq40to/jKhT GaZmDyaiksQFqofW5FfXSwirlu2KlyFgPt0WQcQmCX1WZ6nZR/aWrE31NPuKo38UqC21 W9Og== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1708486925; x=1709091725; h=to:from:subject:references:mime-version:message-id:in-reply-to:date :x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=uv/9NgKdLz0z52mqtvy0vTSbbBgaPJcb0Vk69+L979Q=; b=FiSE3G/wbQeeZHE2Yyxj96B+pbzEA9YdCXNDZhjv1mXCpy13cdQ+ooVhKlLHC5zIXO SREMRVAR78iWfzQOuPAfflkbbILjJi/MtKvLVo5xHQo+E9CRUmbnx0cpD8JeIiwsuOi7 jIPh8byUHMVhkVxlt7aEPY/uiTYWkphKYDbh/lYQR+jVcoUHu4ja9UaHox6zmpIPUF9a 1RHoujmP1XC0xuU0DBLZs2MHQo+jPfUJQAKUNuDk9agDRWeW3Z/Tz48BWwsbMHunsoki yrxPlxZLGSwzCsNoLTtSSyHnuzsV848hL2AwN4W9pgL3DUh4uB1Cwm0bn18hjsUg51rj Y9AQ== X-Forwarded-Encrypted: i=1; AJvYcCUtQxHnKdxUa5ft6FxY0TE8f9c4FkSX46hJ51HrvpG/g+zKh6acWPeYzBiKCh7sXjdsPEPwvnW+wlpQN+hox5QWocmgjiN7SqDDX5je X-Gm-Message-State: AOJu0YxhNijls3m6aAbKPZQEQVBVT9XXSF9NUyo1+qN5F13VgRpvZkcZ LCwJX1iAZjqBZgmW1gfSif08HEPEsi1PL1Z5eWYuDdIg1P1hgLaEA0RzWUqvUrJ7FXw4kqWshkB BtBnN+Q== X-Google-Smtp-Source: AGHT+IGqHOfbFeUMm04fgBII6BGPdq0HLJ7wcSG61rdgw+C9Xjypqm77xVwuEi9AZKTTTzR7Hwymw6gQ+OLN X-Received: from irogers.svl.corp.google.com ([2620:15c:2a3:200:3c02:f882:5876:b7ab]) (user=irogers job=sendgmr) by 2002:a25:db93:0:b0:dc7:9218:df3b with SMTP id g141-20020a25db93000000b00dc79218df3bmr708882ybf.10.1708486924814; Tue, 20 Feb 2024 19:42:04 -0800 (PST) Date: Tue, 20 Feb 2024 19:41:48 -0800 In-Reply-To: <20240221034155.1500118-1-irogers@google.com> Message-Id: <20240221034155.1500118-2-irogers@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20240221034155.1500118-1-irogers@google.com> X-Mailer: git-send-email 2.44.0.rc0.258.g7320e95886-goog Subject: [PATCH v6 1/8] perf thread_map: Skip exited threads when scanning /proc From: Ian Rogers To: Peter Zijlstra , Ingo Molnar , Arnaldo Carvalho de Melo , Namhyung Kim , Mark Rutland , Alexander Shishkin , Jiri Olsa , Ian Rogers , Adrian Hunter , Nathan Chancellor , Nick Desaulniers , Bill Wendling , Justin Stitt , Athira Jajeev , James Clark , Kan Liang , Yang Jihong , linux-kernel@vger.kernel.org, linux-perf-users@vger.kernel.org, llvm@lists.linux.dev Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Scanning /proc is inherently racy. Scanning /proc/pid/task within that is also racy as the pid can terminate. Rather than failing in __thread_map__new_all_cpus, skip pids for such failures. Signed-off-by: Ian Rogers --- tools/perf/util/thread_map.c | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/tools/perf/util/thread_map.c b/tools/perf/util/thread_map.c index ea3b431b9783..b5f12390c355 100644 --- a/tools/perf/util/thread_map.c +++ b/tools/perf/util/thread_map.c @@ -109,9 +109,10 @@ static struct perf_thread_map *__thread_map__new_all_c= pus(uid_t uid) =20 snprintf(path, sizeof(path), "/proc/%d/task", pid); items =3D scandir(path, &namelist, filter, NULL); - if (items <=3D 0) - goto out_free_closedir; - + if (items <=3D 0) { + pr_debug("scandir for %d returned empty, skipping\n", pid); + continue; + } while (threads->nr + items >=3D max_threads) { max_threads *=3D 2; grow =3D true; @@ -152,8 +153,6 @@ static struct perf_thread_map *__thread_map__new_all_cp= us(uid_t uid) for (i =3D 0; i < items; i++) zfree(&namelist[i]); free(namelist); - -out_free_closedir: zfree(&threads); goto out_closedir; } --=20 2.44.0.rc0.258.g7320e95886-goog From nobody Mon Sep 16 19:32:04 2024 Received: from mail-yw1-f201.google.com (mail-yw1-f201.google.com [209.85.128.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 2714438FAF for ; Wed, 21 Feb 2024 03:42:07 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.128.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1708486929; cv=none; b=pS4pvSBVoFzDsUPC4jPcLszMg9oAM56pZoyZBcnRlPAzb9ZItRvpNz+9AyF/5XFPmJV3m8TQpDos0EM/R4UFDqECSbOC5iT8x/0GolT0eEKF1AwRnb/zWSl+6Gyh9YxikvCIvVUb1uiJBIJiC7+KyYct09GoLRUrTCs1tKN1vY4= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1708486929; c=relaxed/simple; bh=3dph1oOgPNiMARTIk1TUv0cnoXVZ3w2YcrnwEMmaGXE=; h=Date:In-Reply-To:Message-Id:Mime-Version:References:Subject:From: To:Content-Type; b=BHoQL74pAz8x207E14WUgn1vr6ByYCCaRImYUR1ce4NiUD97eO/25CWdx778zOFKe5m4LpJOAdJj0Wd+XEHXLRSTLVNlPDkCt212/ApytOrG3i2/CyK0tI5MxujB0DgOuLxC5GCQyb2pFTRPqlbE+T6rornkCB+KrlQUCobe0WQ= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--irogers.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=F2QQAfnN; arc=none smtp.client-ip=209.85.128.201 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--irogers.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="F2QQAfnN" Received: by mail-yw1-f201.google.com with SMTP id 00721157ae682-60802b0afd2so42498567b3.1 for ; Tue, 20 Feb 2024 19:42:07 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1708486927; x=1709091727; darn=vger.kernel.org; h=to:from:subject:references:mime-version:message-id:in-reply-to:date :from:to:cc:subject:date:message-id:reply-to; bh=GN8DooxqnWYMX4Lx9QAzw7baLGn2b+1JMBP0il1xEM0=; b=F2QQAfnNm3m+i8aHhWZVTGpzA2iBwyCjcb5CFKPzWgFZkzC4a3ty6VDa69vefd2k7Y Jq7P9oPJ2I3F0bRiMt6J330w6k1AifoFp5VS3fKE/diZOEip0lcp9Bh8GgRJFDwpDHnx ySWs600npH2jgmotkLzox7uWtUGwiFFtyBUkAhR0TwyJpWwZbu3Uw958rgOpxsJDD1O5 m+T7IE+btMN0xdlkuJnjcIBngdHU6AFgagmEhfDtZDPauBCx1EJEGDLi51pqhyJrplFv hgzDFSpYFoYndNGW33yrEVDIHP1JYd9eMTkgIML2DQy3gwx2FWfKVSVbcQokYH8NLvKM 15Eg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1708486927; x=1709091727; h=to:from:subject:references:mime-version:message-id:in-reply-to:date :x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=GN8DooxqnWYMX4Lx9QAzw7baLGn2b+1JMBP0il1xEM0=; b=omPr6rhUtX1iJ3vpOrIqZVhC/peBvTplZwyhfLVvBTXqpTFzDTUcRf55gqafmuX+Oe ZinGTFhfWyxF/RPLLMKwz9lSfKD+z/30PmhqTk4trGmSR9SvfJagi9U0Yhz8IdBTFPbz vj0ND4PXjIUFeQuBEklVY5D3qQijg9akHBY4OqnNhwvFncT0j4aGzGmVweb7+Oa0G84Y JlI4B5qW/U2GuIWB08eD5hT0IN86g6VfsMhklwT1ztYOgQNqWNVMIfWb1Ek+QIhaR4GV 5xzWvHNwoX/hn0v1d1PM4C/893/HmaMDGd9ibnf+rcoc1mhoPRUyHLULDv1RqDqU3VBE p3Fw== X-Forwarded-Encrypted: i=1; AJvYcCXQirE8AdTPktxsNuTtin/Ik1GHCig8Ew8+Md4LNpSObIP5L13EHKhjQp1z+33loExEFQWlniu60W2OW8h86KZwC2xZJHrX9LSQM+ii X-Gm-Message-State: AOJu0YzUV/vr7YQaqtxsKUuhIlmIQHuZ3+4oRoaC8yC4/IfrA0juYDEv vosmPQ1jH+5NXHlNcIi0rVAvkxevnurCGTMpQDvmvwVsa5HulUtVW34soYn9PDQWRtJA1kS4auI ye/6etw== X-Google-Smtp-Source: AGHT+IHcimSGjtXP/J/UxCh+R6kQfYuW768ba/K0H1tyN90UXsV/9dElbyr75/bcNmmISHcycP36jLk6uHUp X-Received: from irogers.svl.corp.google.com ([2620:15c:2a3:200:3c02:f882:5876:b7ab]) (user=irogers job=sendgmr) by 2002:a05:690c:338e:b0:608:28cf:3592 with SMTP id fl14-20020a05690c338e00b0060828cf3592mr2065532ywb.1.1708486927165; Tue, 20 Feb 2024 19:42:07 -0800 (PST) Date: Tue, 20 Feb 2024 19:41:49 -0800 In-Reply-To: <20240221034155.1500118-1-irogers@google.com> Message-Id: <20240221034155.1500118-3-irogers@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20240221034155.1500118-1-irogers@google.com> X-Mailer: git-send-email 2.44.0.rc0.258.g7320e95886-goog Subject: [PATCH v6 2/8] perf list: Add scandirat compatibility function From: Ian Rogers To: Peter Zijlstra , Ingo Molnar , Arnaldo Carvalho de Melo , Namhyung Kim , Mark Rutland , Alexander Shishkin , Jiri Olsa , Ian Rogers , Adrian Hunter , Nathan Chancellor , Nick Desaulniers , Bill Wendling , Justin Stitt , Athira Jajeev , James Clark , Kan Liang , Yang Jihong , linux-kernel@vger.kernel.org, linux-perf-users@vger.kernel.org, llvm@lists.linux.dev Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" scandirat is used during the printing of tracepoint events but may be missing from certain libcs. Add a compatibility implementation that uses the symlink of an fd in /proc as a path for the reliably present scandir. Signed-off-by: Ian Rogers --- tools/perf/util/print-events.c | 13 ++++--------- tools/perf/util/util.c | 19 +++++++++++++++++++ tools/perf/util/util.h | 8 ++++++++ 3 files changed, 31 insertions(+), 9 deletions(-) diff --git a/tools/perf/util/print-events.c b/tools/perf/util/print-events.c index 9e47712507cc..0dc70b87d609 100644 --- a/tools/perf/util/print-events.c +++ b/tools/perf/util/print-events.c @@ -28,6 +28,7 @@ #include "tracepoint.h" #include "pfm.h" #include "thread_map.h" +#include "util.h" =20 #define MAX_NAME_LEN 100 =20 @@ -63,6 +64,8 @@ void print_tracepoint_events(const struct print_callbacks= *print_cb __maybe_unus { char *events_path =3D get_tracing_file("events"); int events_fd =3D open(events_path, O_PATH); + struct dirent **sys_namelist =3D NULL; + int sys_items; =20 put_tracing_file(events_path); if (events_fd < 0) { @@ -70,10 +73,7 @@ void print_tracepoint_events(const struct print_callback= s *print_cb __maybe_unus return; } =20 -#ifdef HAVE_SCANDIRAT_SUPPORT -{ - struct dirent **sys_namelist =3D NULL; - int sys_items =3D tracing_events__scandir_alphasort(&sys_namelist); + sys_items =3D tracing_events__scandir_alphasort(&sys_namelist); =20 for (int i =3D 0; i < sys_items; i++) { struct dirent *sys_dirent =3D sys_namelist[i]; @@ -130,11 +130,6 @@ void print_tracepoint_events(const struct print_callba= cks *print_cb __maybe_unus } =20 free(sys_namelist); -} -#else - printf("\nWARNING: Your libc doesn't have the scandirat function, please = ask its maintainers to implement it.\n" - " As a rough fallback, please do 'ls %s' to see the availa= ble tracepoint events.\n", events_path); -#endif close(events_fd); } =20 diff --git a/tools/perf/util/util.c b/tools/perf/util/util.c index c1fd9ba6d697..4f561e5e4162 100644 --- a/tools/perf/util/util.c +++ b/tools/perf/util/util.c @@ -552,3 +552,22 @@ int sched_getcpu(void) return -1; } #endif + +#ifndef HAVE_SCANDIRAT_SUPPORT +int scandirat(int dirfd, const char *dirp, + struct dirent ***namelist, + int (*filter)(const struct dirent *), + int (*compar)(const struct dirent **, const struct dirent **)) +{ + char path[PATH_MAX]; + int err, fd =3D openat(dirfd, dirp, O_PATH); + + if (fd < 0) + return fd; + + snprintf(path, sizeof(path), "/proc/%d/fd/%d", getpid(), fd); + err =3D scandir(path, namelist, filter, compar); + close(fd); + return err; +} +#endif diff --git a/tools/perf/util/util.h b/tools/perf/util/util.h index 7c8915d92dca..9966c21aaf04 100644 --- a/tools/perf/util/util.h +++ b/tools/perf/util/util.h @@ -6,6 +6,7 @@ /* glibc 2.20 deprecates _BSD_SOURCE in favour of _DEFAULT_SOURCE */ #define _DEFAULT_SOURCE 1 =20 +#include #include #include #include @@ -56,6 +57,13 @@ int perf_tip(char **strp, const char *dirpath); int sched_getcpu(void); #endif =20 +#ifndef HAVE_SCANDIRAT_SUPPORT +int scandirat(int dirfd, const char *dirp, + struct dirent ***namelist, + int (*filter)(const struct dirent *), + int (*compar)(const struct dirent **, const struct dirent **)); +#endif + extern bool perf_singlethreaded; =20 void perf_set_singlethreaded(void); --=20 2.44.0.rc0.258.g7320e95886-goog From nobody Mon Sep 16 19:32:04 2024 Received: from mail-yb1-f202.google.com (mail-yb1-f202.google.com [209.85.219.202]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 9E3F439FC3 for ; Wed, 21 Feb 2024 03:42:10 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.219.202 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1708486932; cv=none; b=f4OQThmN6RTDL/9ZHP/1dZFo3et4A++MZ0hayvepMVb3Fq77nxI2xXyQO67x/I65NIP44gsEqKDseomDEUetDUOCrM81oYbAmeteLUufjqGtLDDJ9l5oBrspzdCAcgpFvudap3eD5sbdcrGdBQZKR3VkPPhIDtW1qsJ3rp1tyOs= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1708486932; c=relaxed/simple; bh=9QHqj0EQvOX6+a6MNptism09KshtnOJS+VQ0pDYpZf8=; h=Date:In-Reply-To:Message-Id:Mime-Version:References:Subject:From: To:Content-Type; b=OGoH5kyoRQmIqt4NeTT9JpJxTf4O1e3Szb4DrnNuz637LzCuFNxvKlrVXfHT0qdjZ0Q4Ap4/KGV+tJ0W36CH8151rlXXn5UOaMCN7KfRtgfGW9LlDvmb1wEw4uZNm8YgU8vBOZ0ko7A6ytSPgV32Hl0B4MOrFI4WHbpyAb2L4Gg= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--irogers.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=cz5q/b+E; arc=none smtp.client-ip=209.85.219.202 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--irogers.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="cz5q/b+E" Received: by mail-yb1-f202.google.com with SMTP id 3f1490d57ef6-dced704f17cso2109165276.1 for ; Tue, 20 Feb 2024 19:42:10 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1708486929; x=1709091729; darn=vger.kernel.org; h=to:from:subject:references:mime-version:message-id:in-reply-to:date :from:to:cc:subject:date:message-id:reply-to; bh=OwHudnCNl5wfpV2IMol1tp79ctLuVP/i5n4Lz8j3mXY=; b=cz5q/b+EfWRt2txHILY61V/gXJBUL2CSnvB009BhTjTdw0aWlisPpCaFRExuFxZ1uF Y3WlZN5mnjzsqRFTaNoGjelMHbxXk+4n7L7BvT7JRhEUYrdpSbjP7rERkiRlJBezQSTp XcHIOqPVFIR6HR9+Wudknqxh2oWHF0fUfqUCXIVCQK7Ql8dbeuopjnFCa2R52wevmNZO aq/+IaFdflieT2tcK/0j6Ui0b7DHwu78N8hGKTzTll8emsj9rD2uinslvX8g+CoTELyA 2IXklu85A8AW3tqVTP9f+EXf+jHh4WlNSstn3f6Bpe4yS4ph436vFtCEvylHz56y65US S/0w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1708486929; x=1709091729; h=to:from:subject:references:mime-version:message-id:in-reply-to:date :x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=OwHudnCNl5wfpV2IMol1tp79ctLuVP/i5n4Lz8j3mXY=; b=d6b2pc4NKCziSyOokbwLaznP79NpF1ifjHXUHul2TNjBiSgQS4oyXDGb64BwVU8616 Zn/5frpSVA/7J+j0+ZHtXzkAMld2Y4wvYVxSLyGldrjajVKPPuBYQGm0ulqGASKGbTqw B3DbbbZbzbLw1GRHUhQTfYYWPl6CAJaTPvnDl/0f1pfmMWXprSahSDiDPKSlj6pP+BOw i2vwNElhVupEm2POajYILEwmvWi1pNy0OvrYsUgV1UQp628xhLmi1Asxyvu3pGbYqlSc S3O8z8Vu90n+U+pNvdCeHuddVRd4iNz2FE9RA3TyY5c2a7MnqsX0+MU/UP+VANFdEGcm ADlQ== X-Forwarded-Encrypted: i=1; AJvYcCW+f8c8Yj6aU2LXWzLOFm0DPeHJMw62bRzfVCO2V4va1h5ls4CcKM2eHUT1faoo9TRlOa/UmFPxl0wpzxHAxdYSr4ikclBvCTfN4vwY X-Gm-Message-State: AOJu0YwObbLaZzXj6TvJrn+j+ZPqjHRXCjdfJ5FLeMKs1xB2ljbPMAaU fJme0yaH2HHCqpCillWV7/xvo7iWhHaRHNmQNUE2BSIuuPmYT1gRFtOOHLOfJVBHhzhgv5Dm4T4 Qrrj1Aw== X-Google-Smtp-Source: AGHT+IGpVA7if143B0lJXVmaSTTYwpcK6AvkiAGNahJ11h9vd94Ww2nCxr5eJbFyGeAX/DDo5vhoCpkHB36Q X-Received: from irogers.svl.corp.google.com ([2620:15c:2a3:200:3c02:f882:5876:b7ab]) (user=irogers job=sendgmr) by 2002:a25:a447:0:b0:dcc:41ad:fb3b with SMTP id f65-20020a25a447000000b00dcc41adfb3bmr602386ybi.10.1708486929422; Tue, 20 Feb 2024 19:42:09 -0800 (PST) Date: Tue, 20 Feb 2024 19:41:50 -0800 In-Reply-To: <20240221034155.1500118-1-irogers@google.com> Message-Id: <20240221034155.1500118-4-irogers@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20240221034155.1500118-1-irogers@google.com> X-Mailer: git-send-email 2.44.0.rc0.258.g7320e95886-goog Subject: [PATCH v6 3/8] perf tests: Avoid fork in perf_has_symbol test From: Ian Rogers To: Peter Zijlstra , Ingo Molnar , Arnaldo Carvalho de Melo , Namhyung Kim , Mark Rutland , Alexander Shishkin , Jiri Olsa , Ian Rogers , Adrian Hunter , Nathan Chancellor , Nick Desaulniers , Bill Wendling , Justin Stitt , Athira Jajeev , James Clark , Kan Liang , Yang Jihong , linux-kernel@vger.kernel.org, linux-perf-users@vger.kernel.org, llvm@lists.linux.dev Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" perf test -vv Symbols is used to indentify symbols within the perf binary. Add the -F flag so that the test command doesn't fork the test before running. This removes a little overhead. Acked-by: Adrian Hunter Signed-off-by: Ian Rogers --- tools/perf/tests/shell/lib/perf_has_symbol.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tools/perf/tests/shell/lib/perf_has_symbol.sh b/tools/perf/tes= ts/shell/lib/perf_has_symbol.sh index 5d59c32ae3e7..561c93b75d77 100644 --- a/tools/perf/tests/shell/lib/perf_has_symbol.sh +++ b/tools/perf/tests/shell/lib/perf_has_symbol.sh @@ -3,7 +3,7 @@ =20 perf_has_symbol() { - if perf test -vv "Symbols" 2>&1 | grep "[[:space:]]$1$"; then + if perf test -vv -F "Symbols" 2>&1 | grep "[[:space:]]$1$"; then echo "perf does have symbol '$1'" return 0 fi --=20 2.44.0.rc0.258.g7320e95886-goog From nobody Mon Sep 16 19:32:04 2024 Received: from mail-yw1-f201.google.com (mail-yw1-f201.google.com [209.85.128.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 094F438DCC for ; Wed, 21 Feb 2024 03:42:12 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.128.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1708486934; cv=none; b=SyzAYYWGtdqww8T1QvZEH96pfbgoaNfYxKcGWzhQAj+Bx+5y8ouHdlYs8QtRqqApIvDXcbIfq4KmVpqLem4hCxuchAs6I4y+KibgUhYmpTXESuI5lE/kMOTxajt6SmfHFhP7XsuaZ4oHOYs4Ux7RGSjR48PP5mgd/4Ouz6ZPAK4= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1708486934; c=relaxed/simple; bh=nIwADbFncYymlWUg/R60jRadSx8+xe1bkn0KEBii9rU=; h=Date:In-Reply-To:Message-Id:Mime-Version:References:Subject:From: To:Content-Type; b=aU05VRF3cspvmfriS9PWeSYJTXvqcdQy5N4yVrZoVEDqB7+2/Ikr5M3utOAuYFEZj4M4+0d1te64tUim3uCASocKtReGx72PKL5AAjhT2oBigNmBObFZIFGbZOnxJXeNNw9e9k84GfrecVLWcHI3v8Zd+OjFj4GalTJ+ZvDNEJA= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--irogers.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=h3bnIIED; arc=none smtp.client-ip=209.85.128.201 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--irogers.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="h3bnIIED" Received: by mail-yw1-f201.google.com with SMTP id 00721157ae682-6081639fecfso41735927b3.1 for ; Tue, 20 Feb 2024 19:42:12 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1708486932; x=1709091732; darn=vger.kernel.org; h=to:from:subject:references:mime-version:message-id:in-reply-to:date :from:to:cc:subject:date:message-id:reply-to; bh=eF36kSIEMlGOGF+Ru4Ee+ArAeIU+1BDlU+YfXMm89FI=; b=h3bnIIEDWRDfKD/YMR00cbHb9S5+to8IdxcG+rp2/cD33J5Rqai7CqWB3pkNS+9ILI +1fMJ3foXETnFYJhMICrxS6uy3cYGIPqwvb92Mv1ZkXgN734XZP4igLGLSUANfrxcus4 hOovDO3gRWuoMCNEdivgzGzsJM9FBUPaugc0fTcnSRtSItvvg7Dla6Qxm22LLl7MYa2N fegE7djwCJP418WFzc4M75C2H7QwTPg/Dk4E/MtBGY0prZUdeOndGof3+GtyTCXp6JXO ICmMHxT33+SOgDb933qi0nzWrW4UMK9W3Sai9royTTQ5ko7A6SxjyC6CJ+BqPoTNKuAr re4Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1708486932; x=1709091732; h=to:from:subject:references:mime-version:message-id:in-reply-to:date :x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=eF36kSIEMlGOGF+Ru4Ee+ArAeIU+1BDlU+YfXMm89FI=; b=FdOnP9PGLF+4sQg90fgnxhgLDxUbBVtBQxnIXW4sPHIltvGzrxKCibtDyFU2TUnJ3p BTTC6xaehr5Lz+odXcX1s8Dz9MpaDcg5XLHEKgjsi5J97ubn+OQB+BJljVfuw2eHBo4F YTnRZPNSnp7drKgcD13qEhzI4TvGH09MNTm8KKl0PDk5I0WPedTeYsio2UVy0sZSBNuN vnZr0mHq2EYOt5wH75Tocc4xKX77RmY8ghRl0LqKywXTdH9oyB1gFzsVq2Y75J71SQOi PyFHFrQAhNFu+XH66rSczt68txYv+XMjxoFB8M90bHsp1HSE5rxCg8VwDRRpuFkA2NGo uV3Q== X-Forwarded-Encrypted: i=1; AJvYcCXEpGu1RdLgJYVqSQyQnNlMLjlbJPebtgDHJgQMbuAo4TUAriQz/SDfrXfBDoHj/C/yNWzu/iLC9GIHDIzlWXUiJz85xL4Rn//yNWHf X-Gm-Message-State: AOJu0YwHwJ6v7h0mD/bu3MfB/vWwa4BGvqvmZQScvE2ShJjo4UzFcOOC UIsWi3qN8pwnentRxQb057k4ssgUcu1eMxk95E5nY6awYvlsxpHT0BQYXYaFPLXDnLGTNJcGAeh 5lIl8xQ== X-Google-Smtp-Source: AGHT+IE/on57IS2xafUtNQKU7p+VrGTBQkCYynjxRso5yRXY1KPrBnnB3HTy5IyNKzr/8fPp2V2mhgp0xx+K X-Received: from irogers.svl.corp.google.com ([2620:15c:2a3:200:3c02:f882:5876:b7ab]) (user=irogers job=sendgmr) by 2002:a05:6902:704:b0:dc7:6f13:61d9 with SMTP id k4-20020a056902070400b00dc76f1361d9mr660785ybt.4.1708486932038; Tue, 20 Feb 2024 19:42:12 -0800 (PST) Date: Tue, 20 Feb 2024 19:41:51 -0800 In-Reply-To: <20240221034155.1500118-1-irogers@google.com> Message-Id: <20240221034155.1500118-5-irogers@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20240221034155.1500118-1-irogers@google.com> X-Mailer: git-send-email 2.44.0.rc0.258.g7320e95886-goog Subject: [PATCH v6 4/8] tools subcmd: Add a no exec function call option From: Ian Rogers To: Peter Zijlstra , Ingo Molnar , Arnaldo Carvalho de Melo , Namhyung Kim , Mark Rutland , Alexander Shishkin , Jiri Olsa , Ian Rogers , Adrian Hunter , Nathan Chancellor , Nick Desaulniers , Bill Wendling , Justin Stitt , Athira Jajeev , James Clark , Kan Liang , Yang Jihong , linux-kernel@vger.kernel.org, linux-perf-users@vger.kernel.org, llvm@lists.linux.dev Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Tools like perf fork tests in case they crash, but they don't want to exec a full binary. Add an option to call a function rather than do an exec. The child process exits with the result of the function call and is passed the struct of the run_command, things like container_of can then allow the child process function to determine additional arguments. Signed-off-by: Ian Rogers --- tools/lib/subcmd/run-command.c | 2 ++ tools/lib/subcmd/run-command.h | 2 ++ 2 files changed, 4 insertions(+) diff --git a/tools/lib/subcmd/run-command.c b/tools/lib/subcmd/run-command.c index 5cdac2162532..d435eb42354b 100644 --- a/tools/lib/subcmd/run-command.c +++ b/tools/lib/subcmd/run-command.c @@ -122,6 +122,8 @@ int start_command(struct child_process *cmd) } if (cmd->preexec_cb) cmd->preexec_cb(); + if (cmd->no_exec_cmd) + exit(cmd->no_exec_cmd(cmd)); if (cmd->exec_cmd) { execv_cmd(cmd->argv); } else { diff --git a/tools/lib/subcmd/run-command.h b/tools/lib/subcmd/run-command.h index 17d969c6add3..d794138a797f 100644 --- a/tools/lib/subcmd/run-command.h +++ b/tools/lib/subcmd/run-command.h @@ -47,6 +47,8 @@ struct child_process { unsigned exec_cmd:1; /* if this is to be external sub-command */ unsigned stdout_to_stderr:1; void (*preexec_cb)(void); + /* If set, call function in child rather than doing an exec. */ + int (*no_exec_cmd)(struct child_process *process); }; =20 int start_command(struct child_process *); --=20 2.44.0.rc0.258.g7320e95886-goog From nobody Mon Sep 16 19:32:04 2024 Received: from mail-yw1-f201.google.com (mail-yw1-f201.google.com [209.85.128.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 799443A8CA for ; Wed, 21 Feb 2024 03:42:15 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.128.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1708486937; cv=none; b=a3TFLH+sv+QdGpm0abBGmB7MokPhSPEW9SJ7YgXL6zfi38ciDM/mRh4Hg4xZi0TLMZWZLkwi240gkebcyWqyO8IxmoFIkzYskhNbmEGz3O46WULw+0UHu027pgdq3Q26UzCgm1r0oF57Ep3hLg1oeRU8uBGx8i360/dE2WSRM8c= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1708486937; c=relaxed/simple; bh=I+UE93RRTXaqRw/ye3XEqbtXPztsO4QkZJrrM8rzem4=; h=Date:In-Reply-To:Message-Id:Mime-Version:References:Subject:From: To:Content-Type; b=Rp3lxFwEwfDnNAF18kkoQ1q1KGKS4AlslL3tm8NfpVwd53srPca6qqgZmXXdxcWIQaSdU4OQgO2aOYPVMQWrPkuGQCETrqm4oJzmpzj2G4xC1sIbLcA6J+KBihzS33FhUBMG4jiQBGaoUgGKouJ4BabbqA2qJgdyOux9rVJJOTk= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--irogers.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=G7s7dV87; arc=none smtp.client-ip=209.85.128.201 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--irogers.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="G7s7dV87" Received: by mail-yw1-f201.google.com with SMTP id 00721157ae682-602dae507caso101484457b3.0 for ; Tue, 20 Feb 2024 19:42:15 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1708486934; x=1709091734; darn=vger.kernel.org; h=to:from:subject:references:mime-version:message-id:in-reply-to:date :from:to:cc:subject:date:message-id:reply-to; bh=BDoXWFUllq5r9qvxHaqqdOtetQC7wmYhYSm0N03NFTM=; b=G7s7dV87iO7rxKFJv9uTv0T1850IFJiqgrvwCcDPdq0FC+X0wep23+WCGp0dPGoVbM 01jMp13UL+SxmzadnLAuXPcPAeBtizvqRdz/5i2kLfbU4g2EoOP3RXgIHxfiRnMT9sHX gJqY6toDXfXHwLqtY52TdO/m5vzeuTXF4RJMzo5lp6JDykqY5FSE+ZqkXauIpG4AEFG4 6ceRYgOHwcESECTb15RJ6Q/L7z9iOsKbjSaANfEw069U42R/qfk4Z9P+mKI+i5uelsgA edgVtFCit60f9F8yUdJyfqifUhJssCpu+G5RHjlXAbPkK235MPWTnSiuIfAJEHsAEDlb 3mYQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1708486934; x=1709091734; h=to:from:subject:references:mime-version:message-id:in-reply-to:date :x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=BDoXWFUllq5r9qvxHaqqdOtetQC7wmYhYSm0N03NFTM=; b=UhnvGM5/uB0t966AObf+mlGttFvlraOKw7WJFKt5tNH+bL7b4E5KHXejB2pyGTZeoK 5lUEkbyQcjw9+qnzBsdsXLyNT7cdj3iVWAqJVsphYmof9cfN+Zk4OYTWCY+okkHBwcLj H7hHMcw7Jz4OSjrsL9FAutESiEk5I6mE6M7vuaKTWDLxeSNgxmT5veV+Vajja2FBtLCa dwH8HNPOLs2PGP/tZrIbz6SnV0FxmfYOGbrQzo04DI8Ucfs6MUy30mfXzWCaiwDd2Ab6 3+nJ2g3jLwPw86VUiEOPfMnDxwORR8p/aEtZqkcBNfrGKIKjBNch0QY+VTYtw2ZJZ+qk BbKA== X-Forwarded-Encrypted: i=1; AJvYcCXbFzug7755cEULLGJfmPvLFw+/WspA7BdtJDQpZZ0WCeMyzMfoEM1WhP71MmDYKHNkyUyfppJozNC9z8+UMNlO6vNlNsJe1uW9GJ+i X-Gm-Message-State: AOJu0Yz7+vrOFBVBFecXIowFeJvJsJXOktQY69K8qrTTQBOgDP50/NsH 4KwPuIHJ69HVCB5/zyjvBYBiDhSwpTvcHL/0sHHOtxCCzAOLf775efoP4sB3QvwPegOHBo/RArc yLsOppQ== X-Google-Smtp-Source: AGHT+IEKcmdKTjHr2xWWJkj/LRomPZ1pY+pgf9xS7sna0/bb350+idRqAo6Uz+mbnsKjTAEDSZ8CywccCPNe X-Received: from irogers.svl.corp.google.com ([2620:15c:2a3:200:3c02:f882:5876:b7ab]) (user=irogers job=sendgmr) by 2002:a81:f008:0:b0:607:f6f0:bdca with SMTP id p8-20020a81f008000000b00607f6f0bdcamr3613089ywm.7.1708486934605; Tue, 20 Feb 2024 19:42:14 -0800 (PST) Date: Tue, 20 Feb 2024 19:41:52 -0800 In-Reply-To: <20240221034155.1500118-1-irogers@google.com> Message-Id: <20240221034155.1500118-6-irogers@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20240221034155.1500118-1-irogers@google.com> X-Mailer: git-send-email 2.44.0.rc0.258.g7320e95886-goog Subject: [PATCH v6 5/8] perf test: Rename builtin-test-list and add missed header guard From: Ian Rogers To: Peter Zijlstra , Ingo Molnar , Arnaldo Carvalho de Melo , Namhyung Kim , Mark Rutland , Alexander Shishkin , Jiri Olsa , Ian Rogers , Adrian Hunter , Nathan Chancellor , Nick Desaulniers , Bill Wendling , Justin Stitt , Athira Jajeev , James Clark , Kan Liang , Yang Jihong , linux-kernel@vger.kernel.org, linux-perf-users@vger.kernel.org, llvm@lists.linux.dev Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" builtin-test-list is primarily concerned with shell script tests. Rename the file to better reflect this and add a missed header guard. Signed-off-by: Ian Rogers --- tools/perf/tests/Build | 2 +- tools/perf/tests/builtin-test.c | 2 +- tools/perf/tests/{builtin-test-list.c =3D> tests-scripts.c} | 2 +- tools/perf/tests/{builtin-test-list.h =3D> tests-scripts.h} | 4 ++++ 4 files changed, 7 insertions(+), 3 deletions(-) rename tools/perf/tests/{builtin-test-list.c =3D> tests-scripts.c} (99%) rename tools/perf/tests/{builtin-test-list.h =3D> tests-scripts.h} (79%) diff --git a/tools/perf/tests/Build b/tools/perf/tests/Build index 53ba9c3e20e0..c7f9d9676095 100644 --- a/tools/perf/tests/Build +++ b/tools/perf/tests/Build @@ -1,7 +1,7 @@ # SPDX-License-Identifier: GPL-2.0 =20 perf-y +=3D builtin-test.o -perf-y +=3D builtin-test-list.o +perf-y +=3D tests-scripts.o perf-y +=3D parse-events.o perf-y +=3D dso-data.o perf-y +=3D attr.o diff --git a/tools/perf/tests/builtin-test.c b/tools/perf/tests/builtin-tes= t.c index 4a5973f9bb9b..eff3c62e9b47 100644 --- a/tools/perf/tests/builtin-test.c +++ b/tools/perf/tests/builtin-test.c @@ -29,7 +29,7 @@ #include #include =20 -#include "builtin-test-list.h" +#include "tests-scripts.h" =20 static bool dont_fork; const char *dso_to_test; diff --git a/tools/perf/tests/builtin-test-list.c b/tools/perf/tests/tests-= scripts.c similarity index 99% rename from tools/perf/tests/builtin-test-list.c rename to tools/perf/tests/tests-scripts.c index a65b9e547d82..4ebd841da05b 100644 --- a/tools/perf/tests/builtin-test-list.c +++ b/tools/perf/tests/tests-scripts.c @@ -15,7 +15,7 @@ #include #include #include "builtin.h" -#include "builtin-test-list.h" +#include "tests-scripts.h" #include "color.h" #include "debug.h" #include "hist.h" diff --git a/tools/perf/tests/builtin-test-list.h b/tools/perf/tests/tests-= scripts.h similarity index 79% rename from tools/perf/tests/builtin-test-list.h rename to tools/perf/tests/tests-scripts.h index eb81f3aa6683..3a3ec6191848 100644 --- a/tools/perf/tests/builtin-test-list.h +++ b/tools/perf/tests/tests-scripts.h @@ -1,4 +1,6 @@ /* SPDX-License-Identifier: GPL-2.0 */ +#ifndef TESTS_SCRIPTS_H +#define TESTS_SCRIPTS_H =20 struct script_file { char *dir; @@ -10,3 +12,5 @@ struct script_file { const struct script_file *list_script_files(void); /* Get maximum width of description string */ int list_script_max_width(void); + +#endif /* TESTS_SCRIPTS_H */ --=20 2.44.0.rc0.258.g7320e95886-goog From nobody Mon Sep 16 19:32:04 2024 Received: from mail-yb1-f202.google.com (mail-yb1-f202.google.com [209.85.219.202]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id ACD4E3AC24 for ; Wed, 21 Feb 2024 03:42:18 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.219.202 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1708486941; cv=none; b=e5ayDNHQ7cgTLBCUfvw9H5udlx0+AOgbjxyj+zO58isrBU1r/XI8jLkLOX1Wxv8RCnplz02+TlbYnPOM7HNG/l66UkgwiFKH6k4VsNuBp+WZEg2LK8TzmssYDDaYIrqBrJlfd8H/K3WUsreLxtfZXkXUxjrfFycIuEwscoGMuno= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1708486941; c=relaxed/simple; bh=qUA5FgB5jKzfSy9eC0WQ2ZcQSsUjX037xNJTVpjXWhQ=; h=Date:In-Reply-To:Message-Id:Mime-Version:References:Subject:From: To:Content-Type; b=dwQq4/4U8Mg0MSjjE99lOIZOy8MttWLpFw5JMlGWyvLDZyoupP31Qwd6V4xypjM3+5WZ9nLipM1SHZup3OA73OURdJk5Va5pAqxmpspe6OOYDQ44HhXaE1rD4/Em1+emfO093TSgzuYGGC+N13OotxO663oH8rtc9NClCKb9K7k= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--irogers.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=otD3bOGC; arc=none smtp.client-ip=209.85.219.202 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--irogers.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="otD3bOGC" Received: by mail-yb1-f202.google.com with SMTP id 3f1490d57ef6-dc743cc50a6so8447909276.2 for ; Tue, 20 Feb 2024 19:42:18 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1708486938; x=1709091738; darn=vger.kernel.org; h=to:from:subject:references:mime-version:message-id:in-reply-to:date :from:to:cc:subject:date:message-id:reply-to; bh=o0/Grl7GyHkKiOIRnltfxI/fRhIGN1eYIXPhWGdq1Pc=; b=otD3bOGCDWeBoipbjDFK7uTEbhxBm636UDYzVJm/8xXNkU1RoGr6wW433TAjxKuo1i kksppxBOlIG9vnzsXOeEd9cxzvb6899nq3XbX2Lf/JU18uvWNdvPEEoSIkqMePYmE+RW 2Lmcy0pV3Yv/DrYQty9hKjqBVoB04+E1ZTm6Gp6vFMf6IILi2MeynyOSt0lJ0Fj55z51 i+IoZTe1IzV7zRREcjVSkNwqrdzNJfFewj02314vV7OJBf/y9MvN62uM3DW6AiTM0hNq 1iXCkYM66kIh8HDcggg/FARusyQoMAYEnF76J/tB9jSzks0GqE73l+PyXlUe0MafFVnE QrlQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1708486938; x=1709091738; h=to:from:subject:references:mime-version:message-id:in-reply-to:date :x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=o0/Grl7GyHkKiOIRnltfxI/fRhIGN1eYIXPhWGdq1Pc=; b=snn+iMIrShp079vqOhMV3Ieu6VZpg+GFTLEzHsjmSAXPieohYqeT38X2QruJQ/Z/NQ LthWxn1aCQPBbm1ZG209wWyJmv8xzjIGCSRdcP/ZI7zCijYhBFAvuX9rGgzn57YPkVDf V7EOkYMQc4HgD6bHHD0Xf5RRt9FspyxVbK3Pc+9brxXFM24SxaUiZfrwAz5wBWN6Tv2N LBIgjakOE3I7baMd3tEUqUyui7KKlydTRuwas2KeJ64lvogYZ1j09MGHq6Uq9I8DAQ/b rJ6kM23ZibP9XnupuY3zRsj6GaBT0Xz2jT6f28km4NtsI/ncR9fpAqxuBI8Ivry074lX QD8A== X-Forwarded-Encrypted: i=1; AJvYcCVuIIjVxNK0xckS//fM+jVp1g/cWm3UmugV0AU/rJQqsXrT4/kr0PJ1JDfjoVQBtGGswVjZOQ+ebVfNJirQTOFWjSv25Tumn1mklZgQ X-Gm-Message-State: AOJu0YxcdgqBhU7NNngwwt2VowYEzX14FrJraXwlNtU3SmtrfE/rxqsn mJddl8iR3M6c3Tcf2e0R4uTUKS/Fe5AJJ9QenIepgPW9aQX67W5UQULo5GBFfbUqNL0IFK18upp Z+u0/RA== X-Google-Smtp-Source: AGHT+IGjW09WyJmx19DJMMPJcFp8ZN/kWIXhC7Wx1m2/fsfOH/Oon5EbuHZGxLl9Vax9I5wDRGDMDMA3WEZE X-Received: from irogers.svl.corp.google.com ([2620:15c:2a3:200:3c02:f882:5876:b7ab]) (user=irogers job=sendgmr) by 2002:a05:6902:18d4:b0:dcd:2f2d:7a0f with SMTP id ck20-20020a05690218d400b00dcd2f2d7a0fmr605043ybb.9.1708486937727; Tue, 20 Feb 2024 19:42:17 -0800 (PST) Date: Tue, 20 Feb 2024 19:41:53 -0800 In-Reply-To: <20240221034155.1500118-1-irogers@google.com> Message-Id: <20240221034155.1500118-7-irogers@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20240221034155.1500118-1-irogers@google.com> X-Mailer: git-send-email 2.44.0.rc0.258.g7320e95886-goog Subject: [PATCH v6 6/8] perf tests: Use scandirat for shell script finding From: Ian Rogers To: Peter Zijlstra , Ingo Molnar , Arnaldo Carvalho de Melo , Namhyung Kim , Mark Rutland , Alexander Shishkin , Jiri Olsa , Ian Rogers , Adrian Hunter , Nathan Chancellor , Nick Desaulniers , Bill Wendling , Justin Stitt , Athira Jajeev , James Clark , Kan Liang , Yang Jihong , linux-kernel@vger.kernel.org, linux-perf-users@vger.kernel.org, llvm@lists.linux.dev Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Avoid filename appending buffers by using openat, faccessat and scandirat more widely. Turn the script's path back to a file name using readlink from /proc//fd/. Read the script's description using api/io.h to avoid fdopen conversions. Whilst reading perform additional sanity checks on the script's contents. Signed-off-by: Ian Rogers --- tools/perf/tests/builtin-test.c | 20 ++--- tools/perf/tests/tests-scripts.c | 145 ++++++++++++++++++------------- tools/perf/tests/tests-scripts.h | 1 - 3 files changed, 95 insertions(+), 71 deletions(-) diff --git a/tools/perf/tests/builtin-test.c b/tools/perf/tests/builtin-tes= t.c index eff3c62e9b47..162f9eb090ac 100644 --- a/tools/perf/tests/builtin-test.c +++ b/tools/perf/tests/builtin-test.c @@ -300,22 +300,19 @@ static int test_and_print(struct test_suite *t, int s= ubtest) } =20 struct shell_test { - const char *dir; const char *file; }; =20 static int shell_test__run(struct test_suite *test, int subdir __maybe_unu= sed) { int err; - char script[PATH_MAX]; struct shell_test *st =3D test->priv; + char *cmd =3D NULL; =20 - path__join(script, sizeof(script) - 3, st->dir, st->file); - - if (verbose > 0) - strncat(script, " -v", sizeof(script) - strlen(script) - 1); - - err =3D system(script); + if (asprintf(&cmd, "%s%s", st->file, verbose ? " -v" : "") < 0) + return TEST_FAIL; + err =3D system(cmd); + free(cmd); if (!err) return TEST_OK; =20 @@ -331,7 +328,7 @@ static int run_shell_tests(int argc, const char *argv[]= , int i, int width, files =3D list_script_files(); if (!files) return 0; - for (file =3D files; file->dir; file++) { + for (file =3D files; file->file; file++) { int curr =3D i++; struct test_case test_cases[] =3D { { @@ -345,13 +342,12 @@ static int run_shell_tests(int argc, const char *argv= [], int i, int width, .test_cases =3D test_cases, .priv =3D &st, }; - st.dir =3D file->dir; + st.file =3D file->file; =20 if (test_suite.desc =3D=3D NULL || !perf_test__matches(test_suite.desc, curr, argc, argv)) continue; =20 - st.file =3D file->file; pr_info("%3d: %-*s:", i, width, test_suite.desc); =20 if (intlist__find(skiplist, i)) { @@ -455,7 +451,7 @@ static int perf_test__list_shell(int argc, const char *= *argv, int i) files =3D list_script_files(); if (!files) return 0; - for (file =3D files; file->dir; file++) { + for (file =3D files; file->file; file++) { int curr =3D i++; struct test_suite t =3D { .desc =3D file->desc diff --git a/tools/perf/tests/tests-scripts.c b/tools/perf/tests/tests-scri= pts.c index 4ebd841da05b..c21f7a425da9 100644 --- a/tools/perf/tests/tests-scripts.c +++ b/tools/perf/tests/tests-scripts.c @@ -14,6 +14,7 @@ #include #include #include +#include #include "builtin.h" #include "tests-scripts.h" #include "color.h" @@ -24,6 +25,7 @@ #include "symbol.h" #include "tests.h" #include "util/rlimit.h" +#include "util/util.h" =20 =20 /* @@ -35,55 +37,69 @@ static size_t files_num =3D 0; static struct script_file *files =3D NULL; static int files_max_width =3D 0; =20 -static const char *shell_tests__dir(char *path, size_t size) +static int shell_tests__dir_fd(void) { - const char *devel_dirs[] =3D { "./tools/perf/tests", "./tests", }; - char *exec_path; - unsigned int i; + char path[PATH_MAX], *exec_path; + static const char * const devel_dirs[] =3D { "./tools/perf/tests/shell", = "./tests/shell", }; =20 - for (i =3D 0; i < ARRAY_SIZE(devel_dirs); ++i) { - struct stat st; + for (size_t i =3D 0; i < ARRAY_SIZE(devel_dirs); ++i) { + int fd =3D open(devel_dirs[i], O_PATH); =20 - if (!lstat(devel_dirs[i], &st)) { - scnprintf(path, size, "%s/shell", devel_dirs[i]); - if (!lstat(devel_dirs[i], &st)) - return path; - } + if (fd >=3D 0) + return fd; } =20 /* Then installed path. */ exec_path =3D get_argv_exec_path(); - scnprintf(path, size, "%s/tests/shell", exec_path); + scnprintf(path, sizeof(path), "%s/tests/shell", exec_path); free(exec_path); - return path; + return open(path, O_PATH); } =20 -static const char *shell_test__description(char *description, size_t size, - const char *path, const char *n= ame) +static char *shell_test__description(int dir_fd, const char *name) { - FILE *fp; - char filename[PATH_MAX]; - int ch; + struct io io; + char buf[128], desc[256]; + int ch, pos =3D 0; =20 - path__join(filename, sizeof(filename), path, name); - fp =3D fopen(filename, "r"); - if (!fp) + io__init(&io, openat(dir_fd, name, O_RDONLY), buf, sizeof(buf)); + if (io.fd < 0) return NULL; =20 /* Skip first line - should be #!/bin/sh Shebang */ + if (io__get_char(&io) !=3D '#') + goto err_out; + if (io__get_char(&io) !=3D '!') + goto err_out; do { - ch =3D fgetc(fp); - } while (ch !=3D EOF && ch !=3D '\n'); - - description =3D fgets(description, size, fp); - fclose(fp); + ch =3D io__get_char(&io); + if (ch < 0) + goto err_out; + } while (ch !=3D '\n'); =20 - /* Assume first char on line is omment everything after that desc */ - return description ? strim(description + 1) : NULL; + do { + ch =3D io__get_char(&io); + if (ch < 0) + goto err_out; + } while (ch =3D=3D '#' || isspace(ch)); + while (ch > 0 && ch !=3D '\n') { + desc[pos++] =3D ch; + if (pos >=3D (int)sizeof(desc) - 1) + break; + ch =3D io__get_char(&io); + } + while (pos > 0 && isspace(desc[--pos])) + ; + desc[++pos] =3D '\0'; + close(io.fd); + return strdup(desc); +err_out: + close(io.fd); + return NULL; } =20 /* Is this full file path a shell script */ -static bool is_shell_script(const char *path) +static bool is_shell_script(int dir_fd, const char *path) { const char *ext; =20 @@ -91,20 +107,16 @@ static bool is_shell_script(const char *path) if (!ext) return false; if (!strcmp(ext, ".sh")) { /* Has .sh extension */ - if (access(path, R_OK | X_OK) =3D=3D 0) /* Is executable */ + if (faccessat(dir_fd, path, R_OK | X_OK, 0) =3D=3D 0) /* Is executable */ return true; } return false; } =20 /* Is this file in this dir a shell script (for test purposes) */ -static bool is_test_script(const char *path, const char *name) +static bool is_test_script(int dir_fd, const char *name) { - char filename[PATH_MAX]; - - path__join(filename, sizeof(filename), path, name); - if (!is_shell_script(filename)) return false; - return true; + return is_shell_script(dir_fd, name); } =20 /* Duplicate a string and fall over and die if we run out of memory */ @@ -120,12 +132,21 @@ static char *strdup_check(const char *str) return newstr; } =20 -static void append_script(const char *dir, const char *file, const char *d= esc) +static void append_script(int dir_fd, const char *name, char *desc) { + char filename[PATH_MAX], link[128]; struct script_file *files_tmp; - size_t files_num_tmp; + size_t files_num_tmp, len; int width; =20 + snprintf(link, sizeof(link), "/proc/%d/fd/%d", getpid(), dir_fd); + len =3D readlink(link, filename, sizeof(filename)); + if (len < 0) { + pr_err("Failed to readlink %s", link); + return; + } + filename[len++] =3D '/'; + strcpy(&filename[len], name); files_num_tmp =3D files_num + 1; if (files_num_tmp >=3D SIZE_MAX) { pr_err("Too many script files\n"); @@ -142,10 +163,8 @@ static void append_script(const char *dir, const char = *file, const char *desc) /* Add file to end and NULL terminate the struct array */ files =3D files_tmp; files_num =3D files_num_tmp; - files[files_num - 1].dir =3D strdup_check(dir); - files[files_num - 1].file =3D strdup_check(file); - files[files_num - 1].desc =3D strdup_check(desc); - files[files_num].dir =3D NULL; + files[files_num - 1].file =3D strdup_check(filename); + files[files_num - 1].desc =3D desc; files[files_num].file =3D NULL; files[files_num].desc =3D NULL; =20 @@ -154,32 +173,39 @@ static void append_script(const char *dir, const char= *file, const char *desc) files_max_width =3D width; } =20 -static void append_scripts_in_dir(const char *path) +static void append_scripts_in_dir(int dir_fd) { struct dirent **entlist; struct dirent *ent; int n_dirs, i; - char filename[PATH_MAX]; =20 /* List files, sorted by alpha */ - n_dirs =3D scandir(path, &entlist, NULL, alphasort); + n_dirs =3D scandirat(dir_fd, ".", &entlist, NULL, alphasort); if (n_dirs =3D=3D -1) return; for (i =3D 0; i < n_dirs && (ent =3D entlist[i]); i++) { + int fd; + if (ent->d_name[0] =3D=3D '.') continue; /* Skip hidden files */ - if (is_test_script(path, ent->d_name)) { /* It's a test */ - char bf[256]; - const char *desc =3D shell_test__description - (bf, sizeof(bf), path, ent->d_name); + if (is_test_script(dir_fd, ent->d_name)) { /* It's a test */ + char *desc =3D shell_test__description(dir_fd, ent->d_name); =20 if (desc) /* It has a desc line - valid script */ - append_script(path, ent->d_name, desc); - } else if (is_directory(path, ent)) { /* Scan the subdir */ - path__join(filename, sizeof(filename), - path, ent->d_name); - append_scripts_in_dir(filename); + append_script(dir_fd, ent->d_name, desc); + continue; + } + if (ent->d_type !=3D DT_DIR) { + struct stat st; + + if (ent->d_type !=3D DT_UNKNOWN) + continue; + fstatat(dir_fd, ent->d_name, &st, 0); + if (!S_ISDIR(st.st_mode)) + continue; } + fd =3D openat(dir_fd, ent->d_name, O_PATH); + append_scripts_in_dir(fd); } for (i =3D 0; i < n_dirs; i++) /* Clean up */ zfree(&entlist[i]); @@ -188,14 +214,17 @@ static void append_scripts_in_dir(const char *path) =20 const struct script_file *list_script_files(void) { - char path_dir[PATH_MAX]; - const char *path; + int dir_fd; =20 if (files) return files; /* Singleton - we already know our list */ =20 - path =3D shell_tests__dir(path_dir, sizeof(path_dir)); /* Walk dir */ - append_scripts_in_dir(path); + dir_fd =3D shell_tests__dir_fd(); /* Walk dir */ + if (dir_fd < 0) + return NULL; + + append_scripts_in_dir(dir_fd); + close(dir_fd); =20 return files; } diff --git a/tools/perf/tests/tests-scripts.h b/tools/perf/tests/tests-scri= pts.h index 3a3ec6191848..3508a293aaf9 100644 --- a/tools/perf/tests/tests-scripts.h +++ b/tools/perf/tests/tests-scripts.h @@ -3,7 +3,6 @@ #define TESTS_SCRIPTS_H =20 struct script_file { - char *dir; char *file; char *desc; }; --=20 2.44.0.rc0.258.g7320e95886-goog From nobody Mon Sep 16 19:32:04 2024 Received: from mail-yw1-f202.google.com (mail-yw1-f202.google.com [209.85.128.202]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 2A5B03A8CA for ; Wed, 21 Feb 2024 03:42:21 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.128.202 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1708486943; cv=none; b=Db6aW8wgoHAOBcbxD5IySZyCTpV2vE21za4ePD6uf1T1Turt8F4YBYAK/rKWbkHWlQk7Upuebh9L1svKNribJ7RT2mAetU/64J+UmsCV0ax+fWRvm1cZo5loZRtiM3IN3uqHm/4/VZsYoVgMY1BHGV+7NDy2DOYPGOp7i+TSTVE= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1708486943; c=relaxed/simple; bh=53jT037aEfk5XcFmEFkxjxpT8RKj6m5/qirKoy031Rc=; h=Date:In-Reply-To:Message-Id:Mime-Version:References:Subject:From: To:Content-Type; b=j/1uDokjK+O7enlNgsGcz2eMBenPXu+WPzA5/jn0ZjUlRjcXXUbC+x4fOGrgFsnK6u/zFl93HewZOj4KZRCuzXKfBLnn6eJFslgp3TEGMQD6N4ZW+jFCB3emttUaHL6i2CdAx7l5PhfeBTfzyxW/CVhr1Xqnfyp23A5NexX/H2A= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--irogers.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=qFNIFl67; arc=none smtp.client-ip=209.85.128.202 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--irogers.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="qFNIFl67" Received: by mail-yw1-f202.google.com with SMTP id 00721157ae682-6047a047f4cso4483147b3.3 for ; Tue, 20 Feb 2024 19:42:21 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1708486940; x=1709091740; darn=vger.kernel.org; h=to:from:subject:references:mime-version:message-id:in-reply-to:date :from:to:cc:subject:date:message-id:reply-to; bh=Bs0vGCr4d3+lgKm3VULbLfJ1W710iiXQYeVwqOAeIjg=; b=qFNIFl67sWIGunz/LqHFOBvJ3wMYQemxA2fTt2hzhRmAfMfWk4ytAqDXrFM6v2PV5G NNE7iR4hAeOR/lD2lmzzC1CiQKkhRUzWVKMoguhIh2aN1+UUELT+7Ic/e3kGS390ndOk 8T29Cz8XtLFcD8LmKWJRxocwpkbOPjsA4iCjLFiwlNKFuXjY+gi6fBZzQ+7a7vJXYwmo 6a9vTBWKjUso6ncUWOXQJ8DePrfMZtsGWQpFxZH/AEkyHpqY/Q0pw1PwJ7CNXsvyQKnQ sxC5zMNGhdFDRycDsqVmjoiFyQpOHSGVV94qA408kEWnQgIT22ONlBzmG4pMH52vwmpZ eP+w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1708486940; x=1709091740; h=to:from:subject:references:mime-version:message-id:in-reply-to:date :x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=Bs0vGCr4d3+lgKm3VULbLfJ1W710iiXQYeVwqOAeIjg=; b=rrvT/9JfJGCbqM8fc1GB+8282PuhbLhgYKiiCUZUByMhPA3VA8Ed+IENHRiG5TW22L O81GQpe6faHBgZAeLqlemIKVkUaQHbto24IVCuRsVX9WMx3Otj3WaaeJEuTxky62pdCf TPUgpl+v64ZQMfGsPncVrVFoSNJgcqxFmAxFxdkHG/WAaIqpqofu0Once3v3+jVqzcRe P5ngEBPa0upcrOHtNXRhjwdfaQFq2ZAPKkvUfl9zOCwZrTE7WwAFe1zJOrm6u06wjviL KmXC1CeRoVCIhTkRW8GZa3IQi0DQhx+N/tD052BOA7Nk15Q0/5BNrD16B3CTm59YIcKN IQ2A== X-Forwarded-Encrypted: i=1; AJvYcCXryZgwzv3BjpbpVg2s40kJN7UWnICT4DiloQn2vtTZM5tX2o8MVTe9k/bOsm7PUVyfdahSonGDS3keCosuU0pA/e5sfFpYO6MqJrpK X-Gm-Message-State: AOJu0YyJLuWf2Y+UZwD40gz72BXpa5r9yOSy2vL0n58onouml/fYMo54 h0A4UO3nGzzsmFXskilGtmcolfwciuaRv30Q8/8XXI8U0KnyP+Uc7QJvp4aOqCxkVv75nPqHCQ4 butPB1A== X-Google-Smtp-Source: AGHT+IFkMkC4Wb84JVMKuT9ldEtCib5JdyOx/nCJnzKBMt6F39ZqX9PII+IJIak4m8bhYxA2ALhKqasXZb0t X-Received: from irogers.svl.corp.google.com ([2620:15c:2a3:200:3c02:f882:5876:b7ab]) (user=irogers job=sendgmr) by 2002:a81:6dd0:0:b0:608:3329:2f13 with SMTP id i199-20020a816dd0000000b0060833292f13mr1885586ywc.8.1708486940220; Tue, 20 Feb 2024 19:42:20 -0800 (PST) Date: Tue, 20 Feb 2024 19:41:54 -0800 In-Reply-To: <20240221034155.1500118-1-irogers@google.com> Message-Id: <20240221034155.1500118-8-irogers@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20240221034155.1500118-1-irogers@google.com> X-Mailer: git-send-email 2.44.0.rc0.258.g7320e95886-goog Subject: [PATCH v6 7/8] perf tests: Run time generate shell test suites From: Ian Rogers To: Peter Zijlstra , Ingo Molnar , Arnaldo Carvalho de Melo , Namhyung Kim , Mark Rutland , Alexander Shishkin , Jiri Olsa , Ian Rogers , Adrian Hunter , Nathan Chancellor , Nick Desaulniers , Bill Wendling , Justin Stitt , Athira Jajeev , James Clark , Kan Liang , Yang Jihong , linux-kernel@vger.kernel.org, linux-perf-users@vger.kernel.org, llvm@lists.linux.dev Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Rather than special shell test logic, do a single pass to create an array of test suites. Hold the shell test file name in the test suite priv field. This makes the special shell test logic in builtin-test.c redundant so remove it. Signed-off-by: Ian Rogers --- tools/perf/tests/builtin-test.c | 90 +-------------------- tools/perf/tests/tests-scripts.c | 129 ++++++++++++++++++------------- tools/perf/tests/tests-scripts.h | 10 +-- 3 files changed, 80 insertions(+), 149 deletions(-) diff --git a/tools/perf/tests/builtin-test.c b/tools/perf/tests/builtin-tes= t.c index 162f9eb090ac..c42cb40fc242 100644 --- a/tools/perf/tests/builtin-test.c +++ b/tools/perf/tests/builtin-test.c @@ -130,6 +130,7 @@ static struct test_suite *generic_tests[] =3D { static struct test_suite **tests[] =3D { generic_tests, arch_tests, + NULL, /* shell tests created at runtime. */ }; =20 static struct test_workload *workloads[] =3D { @@ -299,73 +300,12 @@ static int test_and_print(struct test_suite *t, int s= ubtest) return err; } =20 -struct shell_test { - const char *file; -}; - -static int shell_test__run(struct test_suite *test, int subdir __maybe_unu= sed) -{ - int err; - struct shell_test *st =3D test->priv; - char *cmd =3D NULL; - - if (asprintf(&cmd, "%s%s", st->file, verbose ? " -v" : "") < 0) - return TEST_FAIL; - err =3D system(cmd); - free(cmd); - if (!err) - return TEST_OK; - - return WEXITSTATUS(err) =3D=3D 2 ? TEST_SKIP : TEST_FAIL; -} - -static int run_shell_tests(int argc, const char *argv[], int i, int width, - struct intlist *skiplist) -{ - struct shell_test st; - const struct script_file *files, *file; - - files =3D list_script_files(); - if (!files) - return 0; - for (file =3D files; file->file; file++) { - int curr =3D i++; - struct test_case test_cases[] =3D { - { - .desc =3D file->desc, - .run_case =3D shell_test__run, - }, - { .name =3D NULL, } - }; - struct test_suite test_suite =3D { - .desc =3D test_cases[0].desc, - .test_cases =3D test_cases, - .priv =3D &st, - }; - st.file =3D file->file; - - if (test_suite.desc =3D=3D NULL || - !perf_test__matches(test_suite.desc, curr, argc, argv)) - continue; - - pr_info("%3d: %-*s:", i, width, test_suite.desc); - - if (intlist__find(skiplist, i)) { - color_fprintf(stderr, PERF_COLOR_YELLOW, " Skip (user override)\n"); - continue; - } - - test_and_print(&test_suite, 0); - } - return 0; -} - static int __cmd_test(int argc, const char *argv[], struct intlist *skipli= st) { struct test_suite *t; unsigned int j, k; int i =3D 0; - int width =3D list_script_max_width(); + int width =3D 0; =20 for_each_test(j, k, t) { int len =3D strlen(test_description(t, -1)); @@ -440,28 +380,6 @@ static int __cmd_test(int argc, const char *argv[], st= ruct intlist *skiplist) } } } - - return run_shell_tests(argc, argv, i, width, skiplist); -} - -static int perf_test__list_shell(int argc, const char **argv, int i) -{ - const struct script_file *files, *file; - - files =3D list_script_files(); - if (!files) - return 0; - for (file =3D files; file->file; file++) { - int curr =3D i++; - struct test_suite t =3D { - .desc =3D file->desc - }; - - if (!perf_test__matches(t.desc, curr, argc, argv)) - continue; - - pr_info("%3d: %s\n", i, t.desc); - } return 0; } =20 @@ -488,9 +406,6 @@ static int perf_test__list(int argc, const char **argv) test_description(t, subi)); } } - - perf_test__list_shell(argc, argv, i); - return 0; } =20 @@ -550,6 +465,7 @@ int cmd_test(int argc, const char **argv) /* Unbuffered output */ setvbuf(stdout, NULL, _IONBF, 0); =20 + tests[2] =3D create_script_test_suites(); argc =3D parse_options_subcommand(argc, argv, test_options, test_subcomma= nds, test_usage, 0); if (argc >=3D 1 && !strcmp(argv[0], "list")) return perf_test__list(argc - 1, argv + 1); diff --git a/tools/perf/tests/tests-scripts.c b/tools/perf/tests/tests-scri= pts.c index c21f7a425da9..e2042b368269 100644 --- a/tools/perf/tests/tests-scripts.c +++ b/tools/perf/tests/tests-scripts.c @@ -27,16 +27,6 @@ #include "util/rlimit.h" #include "util/util.h" =20 - -/* - * As this is a singleton built once for the run of the process, there is - * no value in trying to free it and just let it stay around until process - * exits when it's cleaned up. - */ -static size_t files_num =3D 0; -static struct script_file *files =3D NULL; -static int files_max_width =3D 0; - static int shell_tests__dir_fd(void) { char path[PATH_MAX], *exec_path; @@ -132,12 +122,30 @@ static char *strdup_check(const char *str) return newstr; } =20 -static void append_script(int dir_fd, const char *name, char *desc) +static int shell_test__run(struct test_suite *test, int subtest __maybe_un= used) +{ + const char *file =3D test->priv; + int err; + char *cmd =3D NULL; + + if (asprintf(&cmd, "%s%s", file, verbose ? " -v" : "") < 0) + return TEST_FAIL; + err =3D system(cmd); + free(cmd); + if (!err) + return TEST_OK; + + return WEXITSTATUS(err) =3D=3D 2 ? TEST_SKIP : TEST_FAIL; +} + +static void append_script(int dir_fd, const char *name, char *desc, + struct test_suite ***result, + size_t *result_sz) { char filename[PATH_MAX], link[128]; - struct script_file *files_tmp; - size_t files_num_tmp, len; - int width; + struct test_suite *test_suite, **result_tmp; + struct test_case *tests; + size_t len; =20 snprintf(link, sizeof(link), "/proc/%d/fd/%d", getpid(), dir_fd); len =3D readlink(link, filename, sizeof(filename)); @@ -147,33 +155,43 @@ static void append_script(int dir_fd, const char *nam= e, char *desc) } filename[len++] =3D '/'; strcpy(&filename[len], name); - files_num_tmp =3D files_num + 1; - if (files_num_tmp >=3D SIZE_MAX) { - pr_err("Too many script files\n"); - abort(); + + tests =3D calloc(2, sizeof(*tests)); + if (!tests) { + pr_err("Out of memory while building script test suite list\n"); + return; } + tests[0].name =3D strdup_check(name); + tests[0].desc =3D strdup_check(desc); + tests[0].run_case =3D shell_test__run; + + test_suite =3D zalloc(sizeof(*test_suite)); + if (!test_suite) { + pr_err("Out of memory while building script test suite list\n"); + free(tests); + return; + } + test_suite->desc =3D desc; + test_suite->test_cases =3D tests; + test_suite->priv =3D strdup_check(filename); /* Realloc is good enough, though we could realloc by chunks, not that * anyone will ever measure performance here */ - files_tmp =3D realloc(files, - (files_num_tmp + 1) * sizeof(struct script_file)); - if (files_tmp =3D=3D NULL) { - pr_err("Out of memory while building test list\n"); - abort(); + result_tmp =3D realloc(*result, (*result_sz + 1) * sizeof(*result_tmp)); + if (result_tmp =3D=3D NULL) { + pr_err("Out of memory while building script test suite list\n"); + free(tests); + free(test_suite); + return; } /* Add file to end and NULL terminate the struct array */ - files =3D files_tmp; - files_num =3D files_num_tmp; - files[files_num - 1].file =3D strdup_check(filename); - files[files_num - 1].desc =3D desc; - files[files_num].file =3D NULL; - files[files_num].desc =3D NULL; - - width =3D strlen(desc); /* Track max width of desc */ - if (width > files_max_width) - files_max_width =3D width; + *result =3D result_tmp; + (*result)[*result_sz] =3D test_suite; + (*result_sz)++; } =20 -static void append_scripts_in_dir(int dir_fd) +static void append_scripts_in_dir(int dir_fd, + struct test_suite ***result, + size_t *result_sz) { struct dirent **entlist; struct dirent *ent; @@ -192,7 +210,7 @@ static void append_scripts_in_dir(int dir_fd) char *desc =3D shell_test__description(dir_fd, ent->d_name); =20 if (desc) /* It has a desc line - valid script */ - append_script(dir_fd, ent->d_name, desc); + append_script(dir_fd, ent->d_name, desc, result, result_sz); continue; } if (ent->d_type !=3D DT_DIR) { @@ -205,32 +223,35 @@ static void append_scripts_in_dir(int dir_fd) continue; } fd =3D openat(dir_fd, ent->d_name, O_PATH); - append_scripts_in_dir(fd); + append_scripts_in_dir(fd, result, result_sz); } for (i =3D 0; i < n_dirs; i++) /* Clean up */ zfree(&entlist[i]); free(entlist); } =20 -const struct script_file *list_script_files(void) +struct test_suite **create_script_test_suites(void) { - int dir_fd; - - if (files) - return files; /* Singleton - we already know our list */ - - dir_fd =3D shell_tests__dir_fd(); /* Walk dir */ - if (dir_fd < 0) - return NULL; + struct test_suite **result =3D NULL, **result_tmp; + size_t result_sz =3D 0; + int dir_fd =3D shell_tests__dir_fd(); /* Walk dir */ =20 - append_scripts_in_dir(dir_fd); - close(dir_fd); + /* + * Append scripts if fd is good, otherwise return a NULL terminated zero + * length array. + */ + if (dir_fd >=3D 0) + append_scripts_in_dir(dir_fd, &result, &result_sz); =20 - return files; -} - -int list_script_max_width(void) -{ - list_script_files(); /* Ensure we have scanned all scripts */ - return files_max_width; + result_tmp =3D realloc(result, (result_sz + 1) * sizeof(*result_tmp)); + if (result_tmp =3D=3D NULL) { + pr_err("Out of memory while building script test suite list\n"); + abort(); + } + /* NULL terminate the test suite array. */ + result =3D result_tmp; + result[result_sz] =3D NULL; + if (dir_fd >=3D 0) + close(dir_fd); + return result; } diff --git a/tools/perf/tests/tests-scripts.h b/tools/perf/tests/tests-scri= pts.h index 3508a293aaf9..b553ad26ea17 100644 --- a/tools/perf/tests/tests-scripts.h +++ b/tools/perf/tests/tests-scripts.h @@ -2,14 +2,8 @@ #ifndef TESTS_SCRIPTS_H #define TESTS_SCRIPTS_H =20 -struct script_file { - char *file; - char *desc; -}; +#include "tests.h" =20 -/* List available script tests to run - singleton - never freed */ -const struct script_file *list_script_files(void); -/* Get maximum width of description string */ -int list_script_max_width(void); +struct test_suite **create_script_test_suites(void); =20 #endif /* TESTS_SCRIPTS_H */ --=20 2.44.0.rc0.258.g7320e95886-goog From nobody Mon Sep 16 19:32:04 2024 Received: from mail-yw1-f202.google.com (mail-yw1-f202.google.com [209.85.128.202]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id B2E5C39859 for ; Wed, 21 Feb 2024 03:42:23 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.128.202 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1708486945; cv=none; b=faVCkXOz2e5ybYdYiNlzb3d/ewS4rm/YWFXUSLsLVYul4djj2HSQR2m1U6FYiK178NqpfoRa01AcVCp8OJp6P5V0P9K6San634R+YEQBk/oRsSe/1eSyEH4w3fh23hvV3VhJ/oAlz3AcSb1/sl1XLsUGkzEPMDCXmiAcB3BUfdI= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1708486945; c=relaxed/simple; bh=SV8Q2XP3AEzUCdEScST86iV7qnjMAWaIiYJHWAwVraA=; h=Date:In-Reply-To:Message-Id:Mime-Version:References:Subject:From: To:Content-Type; b=Vim6kKAzyGOIm4AKTBWfMcScvqneen3rqSgCZBkReOIzKclloYEYy/b/G1xxJB6RSoeUrZZJHDuQp9gu1UZVKzZJFbULNKBzXh6JQVM2k6KUWb61llAzbUmySmgoLxaz40AJSObypyHA3/Tgpdq2KyfIcjzeS5vlzHaYG60t4J0= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--irogers.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=bDP546u8; arc=none smtp.client-ip=209.85.128.202 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--irogers.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="bDP546u8" Received: by mail-yw1-f202.google.com with SMTP id 00721157ae682-60810219282so42976777b3.0 for ; Tue, 20 Feb 2024 19:42:23 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1708486943; x=1709091743; darn=vger.kernel.org; h=to:from:subject:references:mime-version:message-id:in-reply-to:date :from:to:cc:subject:date:message-id:reply-to; bh=46NbERsw+rX6VfHxglAqwzl8LfYw4J5J2BPeonbB4cQ=; b=bDP546u8vC4zpylxVQlXWnWOqiRNTzOLPp7wHjt9tq3M9goHm7W7F2bw2GEqJdCNXc uONh6tVlKsxas3WZkKqD90CkSaV7r8VtRzlpC0aA+qbg1NtIbwoqv5fTZTDluWfSX3QZ ZYvWfaCtef+c4q796dHEUXByE8zuQwG804jjgbtFJalVkSbMrakwQZgc0NilDyY6aeIu du54+MqOjDvqDFMPWiqyPYzaboPNmYfOshRcSHzqVrDAdsaaYFO7YGljUeSv+Gxu5fO5 3m/jTuVWEK6IV74WQNw9AG35FH+drOluBEkNWWALKQJEvLdninu4crih7IoSXMrtkI7+ JNvg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1708486943; x=1709091743; h=to:from:subject:references:mime-version:message-id:in-reply-to:date :x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=46NbERsw+rX6VfHxglAqwzl8LfYw4J5J2BPeonbB4cQ=; b=TeNUJAdJGKYXsDJjV8JbkFG0DKqPkG53V3Bd6gWxbkyUf6I2oklM3YJvu30Uv+uNFt LbHQbce/hnxzUfPSazYbBG3GOVK/xRoedUHZA9nEVjkUdtLO1Xv7l9xnY1S8nkfrJ6FH TJj1KiscAwwA40OU9NStTCsaQdIKmGYG7A0IIwVYOk38pHFUWHUMXmN/av0n01zNylbg ohwjcRoVY55TWdG+UjFnAMgVrA1i6F85eC9tvK+iaahm8uc2WhhPHbGOr4Y9fPGdf11v aVVX1IzCTqENyoyRNFVzXeJNDS0bpT30MgalUk7+4QaUcyKKhrEMWonZQTEefWy0LaeN ZNLw== X-Forwarded-Encrypted: i=1; AJvYcCXPFpT4HncJFA/Vk6fsI187f0Alz8XzxbRJKlApAA9XNVMUu9Hcq0fIfuoW9xMJE0L9rMJcA6XOAfDAA8McrKrwhSenW66QpzX+wcbO X-Gm-Message-State: AOJu0Yw+yLZpINbvYxcoS//tY7o7DpwxrYWw3RSKq2u/FWpyp7o349eS QsssYgxSiigtb3fcnCoWk8bfd0KLYv6hDxVtUkE6GFzRSeDuzy93AaEVUNVUplf0mHTqkvUNP7B LP11fhw== X-Google-Smtp-Source: AGHT+IET82N9VxF3P0mhswunlFqpZ962xGtp3e+douDh41h855ffc3i/FuxyiiNEpPVBfAizCbtju+rDiPsA X-Received: from irogers.svl.corp.google.com ([2620:15c:2a3:200:3c02:f882:5876:b7ab]) (user=irogers job=sendgmr) by 2002:a0d:db4b:0:b0:607:81e1:8b83 with SMTP id d72-20020a0ddb4b000000b0060781e18b83mr3197747ywe.8.1708486942813; Tue, 20 Feb 2024 19:42:22 -0800 (PST) Date: Tue, 20 Feb 2024 19:41:55 -0800 In-Reply-To: <20240221034155.1500118-1-irogers@google.com> Message-Id: <20240221034155.1500118-9-irogers@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20240221034155.1500118-1-irogers@google.com> X-Mailer: git-send-email 2.44.0.rc0.258.g7320e95886-goog Subject: [PATCH v6 8/8] perf tests: Add option to run tests in parallel From: Ian Rogers To: Peter Zijlstra , Ingo Molnar , Arnaldo Carvalho de Melo , Namhyung Kim , Mark Rutland , Alexander Shishkin , Jiri Olsa , Ian Rogers , Adrian Hunter , Nathan Chancellor , Nick Desaulniers , Bill Wendling , Justin Stitt , Athira Jajeev , James Clark , Kan Liang , Yang Jihong , linux-kernel@vger.kernel.org, linux-perf-users@vger.kernel.org, llvm@lists.linux.dev Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" By default tests are forked, add an option (-p or --parallel) so that the forked tests are all started in parallel and then their output gathered serially. This is opt-in as running in parallel can cause test flakes. Rather than fork within the code, the start_command/finish_command from libsubcmd are used. This changes how stderr and stdout are handled. The child stderr and stdout are always read to avoid the child blocking. If verbose is 1 (-v) then if the test fails the child stdout and stderr are displayed. If the verbose is >1 (e.g. -vv) then the stdout and stderr from the child are immediately displayed. An unscientific test on my laptop shows the wall clock time for perf test without parallel being 5 minutes 21 seconds and with parallel (-p) being 1 minute 50 seconds. Signed-off-by: Ian Rogers --- tools/perf/tests/builtin-test.c | 314 ++++++++++++++++++++++---------- 1 file changed, 215 insertions(+), 99 deletions(-) diff --git a/tools/perf/tests/builtin-test.c b/tools/perf/tests/builtin-tes= t.c index c42cb40fc242..d13ee7683d9d 100644 --- a/tools/perf/tests/builtin-test.c +++ b/tools/perf/tests/builtin-test.c @@ -6,6 +6,7 @@ */ #include #include +#include #include #include #include @@ -21,9 +22,11 @@ #include "debug.h" #include "color.h" #include +#include #include "string2.h" #include "symbol.h" #include "util/rlimit.h" +#include "util/strbuf.h" #include #include #include @@ -31,7 +34,13 @@ =20 #include "tests-scripts.h" =20 +/* + * Command line option to not fork the test running in the same process and + * making them easier to debug. + */ static bool dont_fork; +/* Fork the tests in parallel and then wait for their completion. */ +static bool parallel; const char *dso_to_test; const char *test_objdump_path =3D "objdump"; =20 @@ -209,76 +218,36 @@ static bool perf_test__matches(const char *desc, int = curr, int argc, const char return false; } =20 -static int run_test(struct test_suite *test, int subtest) -{ - int status, err =3D -1, child =3D dont_fork ? 0 : fork(); - char sbuf[STRERR_BUFSIZE]; - - if (child < 0) { - pr_err("failed to fork test: %s\n", - str_error_r(errno, sbuf, sizeof(sbuf))); - return -1; - } - - if (!child) { - if (!dont_fork) { - pr_debug("test child forked, pid %d\n", getpid()); - - if (verbose <=3D 0) { - int nullfd =3D open("/dev/null", O_WRONLY); - - if (nullfd >=3D 0) { - close(STDERR_FILENO); - close(STDOUT_FILENO); - - dup2(nullfd, STDOUT_FILENO); - dup2(STDOUT_FILENO, STDERR_FILENO); - close(nullfd); - } - } else { - signal(SIGSEGV, sighandler_dump_stack); - signal(SIGFPE, sighandler_dump_stack); - } - } - - err =3D test_function(test, subtest)(test, subtest); - if (!dont_fork) - exit(err); - } - - if (!dont_fork) { - wait(&status); +struct child_test { + struct child_process process; + struct test_suite *test; + int test_num; + int subtest; +}; =20 - if (WIFEXITED(status)) { - err =3D (signed char)WEXITSTATUS(status); - pr_debug("test child finished with %d\n", err); - } else if (WIFSIGNALED(status)) { - err =3D -1; - pr_debug("test child interrupted\n"); - } - } +static int run_test_child(struct child_process *process) +{ + struct child_test *child =3D container_of(process, struct child_test, pro= cess); + int err; =20 - return err; + pr_debug("--- start ---\n"); + pr_debug("test child forked, pid %d\n", getpid()); + err =3D test_function(child->test, child->subtest)(child->test, child->su= btest); + pr_debug("---- end(%d) ----\n", err); + fflush(NULL); + return -err; } =20 -#define for_each_test(j, k, t) \ - for (j =3D 0, k =3D 0; j < ARRAY_SIZE(tests); j++, k =3D 0) \ - while ((t =3D tests[j][k++]) !=3D NULL) - -static int test_and_print(struct test_suite *t, int subtest) +static int print_test_result(struct test_suite *t, int i, int subtest, int= result, int width) { - int err; + if (has_subtests(t)) { + int subw =3D width > 2 ? width - 2 : width; =20 - pr_debug("\n--- start ---\n"); - err =3D run_test(t, subtest); - pr_debug("---- end ----\n"); + pr_info("%3d.%1d: %-*s:", i + 1, subtest + 1, subw, test_description(t, = subtest)); + } else + pr_info("%3d: %-*s:", i + 1, width, test_description(t, subtest)); =20 - if (!has_subtests(t)) - pr_debug("%s:", t->desc); - else - pr_debug("%s subtest %d:", t->desc, subtest + 1); - - switch (err) { + switch (result) { case TEST_OK: pr_info(" Ok\n"); break; @@ -297,34 +266,186 @@ static int test_and_print(struct test_suite *t, int = subtest) break; } =20 - return err; + return 0; +} + +static int finish_test(struct child_test *child_test, int width) +{ + struct test_suite *t =3D child_test->test; + int i =3D child_test->test_num; + int subi =3D child_test->subtest; + int out =3D child_test->process.out; + int err =3D child_test->process.err; + bool out_done =3D out <=3D 0; + bool err_done =3D err <=3D 0; + struct strbuf out_output =3D STRBUF_INIT; + struct strbuf err_output =3D STRBUF_INIT; + int ret; + + /* + * For test suites with subtests, display the suite name ahead of the + * sub test names. + */ + if (has_subtests(t) && subi =3D=3D 0) + pr_info("%3d: %-*s:\n", i + 1, width, test_description(t, -1)); + + /* + * Busy loop reading from the child's stdout and stderr that are set to + * be non-blocking until EOF. + */ + if (!out_done) + fcntl(out, F_SETFL, O_NONBLOCK); + if (!err_done) + fcntl(err, F_SETFL, O_NONBLOCK); + if (verbose > 1) { + if (has_subtests(t)) + pr_info("%3d.%1d: %s:\n", i + 1, subi + 1, test_description(t, subi)); + else + pr_info("%3d: %s:\n", i + 1, test_description(t, -1)); + } + while (!out_done || !err_done) { + struct pollfd pfds[2] =3D { + { .fd =3D out, + .events =3D POLLIN | POLLERR | POLLHUP | POLLNVAL, + }, + { .fd =3D err, + .events =3D POLLIN | POLLERR | POLLHUP | POLLNVAL, + }, + }; + char buf[512]; + ssize_t len; + + /* Poll to avoid excessive spinning, timeout set for 1000ms. */ + poll(pfds, ARRAY_SIZE(pfds), /*timeout=3D*/1000); + if (!out_done && pfds[0].revents) { + errno =3D 0; + len =3D read(out, buf, sizeof(buf) - 1); + + if (len <=3D 0) { + out_done =3D errno !=3D EAGAIN; + } else { + buf[len] =3D '\0'; + if (verbose > 1) + fprintf(stdout, "%s", buf); + else + strbuf_addstr(&out_output, buf); + } + } + if (!err_done && pfds[1].revents) { + errno =3D 0; + len =3D read(err, buf, sizeof(buf) - 1); + + if (len <=3D 0) { + err_done =3D errno !=3D EAGAIN; + } else { + buf[len] =3D '\0'; + if (verbose > 1) + fprintf(stdout, "%s", buf); + else + strbuf_addstr(&err_output, buf); + } + } + } + /* Clean up child process. */ + ret =3D finish_command(&child_test->process); + if (verbose =3D=3D 1 && ret =3D=3D TEST_FAIL) { + /* Add header for test that was skipped above. */ + if (has_subtests(t)) + pr_info("%3d.%1d: %s:\n", i + 1, subi + 1, test_description(t, subi)); + else + pr_info("%3d: %s:\n", i + 1, test_description(t, -1)); + fprintf(stdout, "%s", out_output.buf); + fprintf(stderr, "%s", err_output.buf); + } + strbuf_release(&out_output); + strbuf_release(&err_output); + print_test_result(t, i, subi, ret, width); + if (out > 0) + close(out); + if (err > 0) + close(err); + return 0; +} + +static int start_test(struct test_suite *test, int i, int subi, struct chi= ld_test **child, + int width) +{ + int err; + + *child =3D NULL; + if (dont_fork) { + pr_debug("--- start ---\n"); + err =3D test_function(test, subi)(test, subi); + pr_debug("---- end ----\n"); + print_test_result(test, i, subi, err, width); + return 0; + } + + *child =3D zalloc(sizeof(**child)); + if (!*child) + return -ENOMEM; + + (*child)->test =3D test; + (*child)->test_num =3D i; + (*child)->subtest =3D subi; + (*child)->process.pid =3D -1; + (*child)->process.no_stdin =3D 1; + if (verbose <=3D 0) { + (*child)->process.no_stdout =3D 1; + (*child)->process.no_stderr =3D 1; + } else { + (*child)->process.out =3D -1; + (*child)->process.err =3D -1; + } + (*child)->process.no_exec_cmd =3D run_test_child; + err =3D start_command(&(*child)->process); + if (err || parallel) + return err; + return finish_test(*child, width); } =20 +#define for_each_test(j, k, t) \ + for (j =3D 0, k =3D 0; j < ARRAY_SIZE(tests); j++, k =3D 0) \ + while ((t =3D tests[j][k++]) !=3D NULL) + static int __cmd_test(int argc, const char *argv[], struct intlist *skipli= st) { struct test_suite *t; unsigned int j, k; int i =3D 0; int width =3D 0; + size_t num_tests =3D 0; + struct child_test **child_tests; + int child_test_num =3D 0; =20 for_each_test(j, k, t) { int len =3D strlen(test_description(t, -1)); =20 if (width < len) width =3D len; + + if (has_subtests(t)) { + for (int subi =3D 0, subn =3D num_subtests(t); subi < subn; subi++) { + len =3D strlen(test_description(t, subi)); + if (width < len) + width =3D len; + num_tests++; + } + } else { + num_tests++; + } } + child_tests =3D calloc(num_tests, sizeof(*child_tests)); + if (!child_tests) + return -ENOMEM; =20 for_each_test(j, k, t) { int curr =3D i++; - int subi; =20 if (!perf_test__matches(test_description(t, -1), curr, argc, argv)) { bool skip =3D true; - int subn; - - subn =3D num_subtests(t); =20 - for (subi =3D 0; subi < subn; subi++) { + for (int subi =3D 0, subn =3D num_subtests(t); subi < subn; subi++) { if (perf_test__matches(test_description(t, subi), curr, argc, argv)) skip =3D false; @@ -334,52 +455,45 @@ static int __cmd_test(int argc, const char *argv[], s= truct intlist *skiplist) continue; } =20 - pr_info("%3d: %-*s:", i, width, test_description(t, -1)); - if (intlist__find(skiplist, i)) { + pr_info("%3d: %-*s:", curr + 1, width, test_description(t, -1)); color_fprintf(stderr, PERF_COLOR_YELLOW, " Skip (user override)\n"); continue; } =20 if (!has_subtests(t)) { - test_and_print(t, -1); - } else { - int subn =3D num_subtests(t); - /* - * minus 2 to align with normal testcases. - * For subtest we print additional '.x' in number. - * for example: - * - * 35: Test LLVM searching and compiling : - * 35.1: Basic BPF llvm compiling test : Ok - */ - int subw =3D width > 2 ? width - 2 : width; - - if (subn <=3D 0) { - color_fprintf(stderr, PERF_COLOR_YELLOW, - " Skip (not compiled in)\n"); - continue; - } - pr_info("\n"); + int err =3D start_test(t, curr, -1, &child_tests[child_test_num++], wid= th); =20 - for (subi =3D 0; subi < subn; subi++) { - int len =3D strlen(test_description(t, subi)); - - if (subw < len) - subw =3D len; + if (err) { + /* TODO: if parallel waitpid the already forked children. */ + free(child_tests); + return err; } + } else { + for (int subi =3D 0, subn =3D num_subtests(t); subi < subn; subi++) { + int err; =20 - for (subi =3D 0; subi < subn; subi++) { if (!perf_test__matches(test_description(t, subi), curr, argc, argv)) continue; =20 - pr_info("%3d.%1d: %-*s:", i, subi + 1, subw, - test_description(t, subi)); - test_and_print(t, subi); + err =3D start_test(t, curr, subi, &child_tests[child_test_num++], + width); + if (err) + return err; } } } + for (i =3D 0; i < child_test_num; i++) { + if (parallel) { + int ret =3D finish_test(child_tests[i], width); + + if (ret) + return ret; + } + free(child_tests[i]); + } + free(child_tests); return 0; } =20 @@ -447,6 +561,8 @@ int cmd_test(int argc, const char **argv) "be more verbose (show symbol address, etc)"), OPT_BOOLEAN('F', "dont-fork", &dont_fork, "Do not fork for testcase"), + OPT_BOOLEAN('p', "parallel", ¶llel, + "Run the tests altogether in parallel"), OPT_STRING('w', "workload", &workload, "work", "workload to run for testi= ng"), OPT_STRING(0, "dso", &dso_to_test, "dso", "dso to test"), OPT_STRING(0, "objdump", &test_objdump_path, "path", --=20 2.44.0.rc0.258.g7320e95886-goog