From nobody Sun Feb 8 06:00:06 2026 Received: from mail-yb1-f201.google.com (mail-yb1-f201.google.com [209.85.219.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 E8900C154 for ; Wed, 14 Feb 2024 04:54:34 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.219.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1707886476; cv=none; b=qay91gEqLcet8LBdX0RznmUP0bLKoGOIppzCH1WOVAyh4qT7rHFCHvbT2dw8FiTqaOkPHvLJpRULZgRKX1PK7SdIItJOsR2GyItm5KNao7tGO5Te+m3XIMO99dDtJjrUTNGD7uOR7Mczv0vGSPXu9t32tzkJUtJsCglc9jkX6TQ= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1707886476; c=relaxed/simple; bh=9GvaQ8U/ciyGf0POxeefEusSW20t+DHMSsugj/N6ETY=; h=Date:In-Reply-To:Message-Id:Mime-Version:References:Subject:From: To:Content-Type; b=KXd0EB6GbFEiBbX/ZcW3RVpfO8sBARiA/4vB7pD0PqmW32R2ecgqqn2XK5HXQz5sUkZ/1Ct2pK4ypd6YtSMuqsuFGn2SQ5Xpvgwzo4qT6vypE6uLEhaOEpvSKp2PvP6vAL3pY0M9bccWlsLlvaZ7grMjodTa3EH/cG5C5e7tYr0= 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=xUNo3MuT; arc=none smtp.client-ip=209.85.219.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="xUNo3MuT" Received: by mail-yb1-f201.google.com with SMTP id 3f1490d57ef6-dc74645bfa8so2588395276.1 for ; Tue, 13 Feb 2024 20:54:34 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1707886474; x=1708491274; 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=F6UUwQ6uU0gh1AgBT5LAdlyWuLVQW/eRWP9wNVATd0w=; b=xUNo3MuTCHNPNBQSi0iOeCYRhkZNpLcWuhCz+RXhzVfhzxK+TFj5UCTqB1yjMiEgf4 Y2IG/TsNqIuLvuXhVgwCfrFqkulqkxzpGVoKkvlnkvgmtXb2fvm49DMi+Cx4GsUWHQrZ sWYmkj0jSbpPEK9d36KD+H81PqEqLB+Dy2jKJeYw3fZYvAS1ASJvzI4cLcAKXNtG/yUE TWjImoL4OLfJtmjCA1uNDwXgoX/RJOZzGnIhuSWn0e/QVWX2zxnobM7lxur5JaxWHXVI Q5X5Zf41wd99vLJfhXQ4rztXfxNuf+epH4qJaFXBRbgLreCLKQGyL2o/vWpZP1VeA/E9 3aAw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1707886474; x=1708491274; 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=F6UUwQ6uU0gh1AgBT5LAdlyWuLVQW/eRWP9wNVATd0w=; b=EVrpg7TZ7ydQjUykt3RJKokphZVTT/hi+i318IJJc+H5AhxtqXvCxWXVX0Pp2SsuaD GPIYRT62ID5n8BXhq9vYoRuzPLMSHPJPVstNQ/Hs1KvfVu2UHP7ewsRI0gYMXsU03s2v Dsjst4rClrj2WMSwfI5fi83f3UsuPj1Gh+Crkjisscmb/WyN8NtmLT92WXYcc3KKN68C JD/vzGltgKcWepqSmhHQU63/VylN/xwqH5rgxB4ewPL26tGHUe0pU4/wfDpwksvKwkEv Fka+txiiKAW2i4I2dXAI2aPAh80tZB+ENUib9uYJHCkmKze8bJP9qRjcVGbZeH2rX0Ct UGVA== X-Forwarded-Encrypted: i=1; AJvYcCXd5/Pp6ZXramDNMiZw1c3sLrdrnjnm1TTeILrSe6Q9NBrnHPiXLovtC6hRoz8CMgJzkicz9nI3mRpFJj2ZHwjhwe/S3rKXrvzZnf74 X-Gm-Message-State: AOJu0Yw7EmPzVEiZ/B6jdZciohlbrWXBUax8v89gsepi8YnY4bb2+VQr ePHlPdrpMWfVbbBIM5M6MSo4R5BlJkLY1EoYSOATNQljaMwiu0Znmf1/oU+759ZlZAzSVnxFiRl kWPzVBw== X-Google-Smtp-Source: AGHT+IEYhgWjbXFx0sxYnQQnVr7GNh7ZLD+INkja+l1mji2uo3Azkx4WVasjvNd44POf3VLkrK39N5w37xYF X-Received: from irogers.svl.corp.google.com ([2620:15c:2a3:200:6d92:85eb:9adc:66dd]) (user=irogers job=sendgmr) by 2002:a25:d3c6:0:b0:dc7:6cfa:dc59 with SMTP id e189-20020a25d3c6000000b00dc76cfadc59mr50124ybf.4.1707886473857; Tue, 13 Feb 2024 20:54:33 -0800 (PST) Date: Tue, 13 Feb 2024 20:53:49 -0800 In-Reply-To: <20240214045356.748330-1-irogers@google.com> Message-Id: <20240214045356.748330-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: <20240214045356.748330-1-irogers@google.com> X-Mailer: git-send-email 2.43.0.687.g38aa6559b0-goog Subject: [PATCH v4 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 , James Clark , Athira Jajeev , 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 Acked-by: Namhyung Kim --- 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.43.0.687.g38aa6559b0-goog From nobody Sun Feb 8 06:00:06 2026 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 F3DD210A25 for ; Wed, 14 Feb 2024 04:54:36 +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=1707886478; cv=none; b=Bd1c4UUg3LZJmDYpDWECBoP/UFsBh2UnaL5lnxJOdghYbQpXw+BQwxSykvETl3Xlzum8MWp8/SuddrHzVTJ0tKinac38Yi8ICdJKgvBpyDcVl5gF+IPE70IdaMShtwMI+dciUL+RTcf/G3AmG63b4y+Rbs2KoZjohyRQcNhUM7M= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1707886478; c=relaxed/simple; bh=NhiwdSNsgGhanFThf3Y8+0MhiUWCknICtVTm6IyvRmw=; h=Date:In-Reply-To:Message-Id:Mime-Version:References:Subject:From: To:Content-Type; b=TAaxskLtKMsEGgjIOXEIwKqsrSnWjPh/YbR8TP9B03LEGKsoIXQ+Ujg1Ly4wiRrhNqWa/XZYHZxSi2myjYg0cODWL3XTfm9abYYy9CAgRFnz0ovqT2Bae8GXu8mLY/NBYROBKxQN6CPNsONjKt/usKymneeJ7UaVI8nYZ0AWGjY= 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=1xGd8ewx; 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="1xGd8ewx" Received: by mail-yw1-f201.google.com with SMTP id 00721157ae682-6077e9dd249so25754347b3.3 for ; Tue, 13 Feb 2024 20:54:36 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1707886476; x=1708491276; 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=YmSZNQ356QtM9WesWCMinXmK9k2yULQ13GYKx7NLJhc=; b=1xGd8ewx+6QMWMCOnsEdqMLKF2N81vwpOJk7nUzwCgdEYXsLrTJ+FvcIBrAaaK0lR8 OdMY2SIRkPKDvbaN3kpV+gyiHeUQ3yRTZoz1mjG8gYUEJ17CBxBR9rVa0lrF7+uUqODs 62fDERGEZcArpHvYVrZLkGPkirMca7cqqdND83MY4HlQSdnKDWM66nzIBG5gGTuH1Fbz zrQoakOgLq3ugaAcRwzYXOfXAkTkzM1IfveEWAJkjZUx/XMnXjS+xUXFVy/EZTRV5Mpm m4n2rf6GgWIWvxIc4gY8sGqF30Bw/bt+//CYY9KPgYhwRkj5wF/ddt9cikvbcpISDtvA xpyQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1707886476; x=1708491276; 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=YmSZNQ356QtM9WesWCMinXmK9k2yULQ13GYKx7NLJhc=; b=Qu3OCMITEMOw1j1tjIMzR1KElREa3r9DDbyNUNHqA/mzx1ha1A5HVzsDott7iamsq0 DCsD3k2++gHGHUyNh5FoR0liJm3vkpu9uyYKlS3TuEupicZIY1BdwI6XpR0bF9RwObRr J8UXRWuEMY9Kpx8Na2ViIiom/q3eP33VwSyl3lAFmXBE3d8LG35qyXd1DPUlBOsF9NT9 E4Y/AEjXcEChCWIJ0Ogty97UGKk6I63g1mVgMuBjqsuJmDg32Q4MGFPD/uiSkTRU4IwV m9TXO3y1Ge/VR5doaxfNc1oVR0wqbitpBbskZLb99TPP8JCbvLR4XJ4Gv6/CYBRQp4uq tCdw== X-Forwarded-Encrypted: i=1; AJvYcCVVVps3OS8fC79db+H8fqwVTKep0law2u5lUpxpAhGWyIBstJm+66VxvIzOfi04y4WnUkAkjEkxCDAVjg79vSjR1r5wIQL8jmcNtNFT X-Gm-Message-State: AOJu0YzoxKS+VjQON5zCE0ZTq50XldNk29/utQepkwOce2BeIIg3ybfG NJxLYqAftKKdY8gfrY+svNnqTd/B1me7Jljvt18mbz7phJGKvQBe4SPMBVGA1FHTNM5gJeSZ3Hu kjxl0cA== X-Google-Smtp-Source: AGHT+IEFuvHaqaekRipzgHS56dLK9shPHnKIKYxXCRw4dh0QDC9cp+ued3K17AtYGH6E73IBBe9HedDMpOlG X-Received: from irogers.svl.corp.google.com ([2620:15c:2a3:200:6d92:85eb:9adc:66dd]) (user=irogers job=sendgmr) by 2002:a0d:eb15:0:b0:602:b7ce:f382 with SMTP id u21-20020a0deb15000000b00602b7cef382mr240294ywe.7.1707886476058; Tue, 13 Feb 2024 20:54:36 -0800 (PST) Date: Tue, 13 Feb 2024 20:53:50 -0800 In-Reply-To: <20240214045356.748330-1-irogers@google.com> Message-Id: <20240214045356.748330-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: <20240214045356.748330-1-irogers@google.com> X-Mailer: git-send-email 2.43.0.687.g38aa6559b0-goog Subject: [PATCH v4 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 , James Clark , Athira Jajeev , 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 Acked-by: Namhyung Kim --- tools/perf/util/print-events.c | 12 +++--------- tools/perf/util/util.c | 19 +++++++++++++++++++ tools/perf/util/util.h | 8 ++++++++ 3 files changed, 30 insertions(+), 9 deletions(-) diff --git a/tools/perf/util/print-events.c b/tools/perf/util/print-events.c index 9e47712507cc..bf79dd366e2e 100644 --- a/tools/perf/util/print-events.c +++ b/tools/perf/util/print-events.c @@ -63,6 +63,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 +72,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 +129,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.43.0.687.g38aa6559b0-goog From nobody Sun Feb 8 06:00:06 2026 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 513CA1119E for ; Wed, 14 Feb 2024 04:54:39 +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=1707886481; cv=none; b=F9iJ1Av0O8Duy8PjK43igjhCAy0K0VBP5NEJXc7Ys7JW7i4vq05Pv3fxkdVbTy7ua5M8hWMl3sgyxeVhjQWpoQ2dVN9e/0CzIT2J6MWYXZfNvczzkLA4ljkuNzFmLtwzY7LClqL2mHa4dv1KHQPMDEqfeAdSibmlVa5rArmd9ME= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1707886481; c=relaxed/simple; bh=cEoquvI2Ge+v6/Ow4yXCwb905NVQF3PwLLtFQnXzKG0=; h=Date:In-Reply-To:Message-Id:Mime-Version:References:Subject:From: To:Content-Type; b=gzogAVducOJe0CAVqsrMwN6+cEF9uRPaPWsbGxEkE+9vmF3iwsnATDjbp5zfYgfVi3CORvdHYsCLFDPVkz81fJ0fFNHqFsUoY8a7Y2XWlY4Xp5Dy0k3CyVGWzlgi15XCghhtFjHjdS3xSCP35lD89x/tUBTU9ZRYDFpbuwaZNZQ= 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=ka1HKHnw; 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="ka1HKHnw" Received: by mail-yw1-f202.google.com with SMTP id 00721157ae682-6077fa2fa9bso30720987b3.2 for ; Tue, 13 Feb 2024 20:54:39 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1707886478; x=1708491278; 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=WQsEsQdYL+F7rq54Mfa+75HgoPY/TwxmW9rhD6V78kU=; b=ka1HKHnwUZf/VtZmg13XfpuL356/CJEiYDRvGxg8or1taZebth0hH+3EF/v2bsJ1K/ T6HJTlUU9Xj9GUPg4+Jh0hiaxXlC53GVqNGHmIhMOOXKpWEMQiSvm5P/WfCWaNItYR/i c2N6g4fgB6wDiWqFCQjUNHfWzYwLtpCzVVJLyKo2oFfUieIF1vlPOU1VTYfI8RVC/1Rx WmNflBbWD0iBrarDoOFTfkuuH5WEDHNhQbFC4ehOGJIepaez3U+7mVMY6+Ut7lXRTCWv O0e39iaA27ZfMz1noSy6ESsD2RsnuCm2L9do1rqEHImu0PYWzBCHXfeW/2sPgYWVvzRG XNbg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1707886478; x=1708491278; 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=WQsEsQdYL+F7rq54Mfa+75HgoPY/TwxmW9rhD6V78kU=; b=UHbaT4BYkgu8P/ZncBxiOPqGXwobjXLGVTKHEzzcmuhmi8a0jefzbm2Xu7FNesErjD zig/9Bz5gQPtWmLcEdthA2mwMmsvPh3LbpbDRa+DJvVlUvLpTRmkTyeH1Vn6GBFBylzh AzJkBli4lMb2JrgbDCx8YpLfhAJzGT4TpmEG1NtTHm0WYXgo3eRB0Em00cwxYXWRbBd+ 6fEku3G2P59sDKCdXYGJOuQ8ajMmrJu0SZa/mh5sCvFtaiEmVSpexz3IFj9B1pRNOH2D x5SP23pCEe4+EIj0ceEpFwvt8uNHBECF4/Q60VGKIU5DrZHX3vX+yI3LrJE+oYQZSmpC FlZQ== X-Forwarded-Encrypted: i=1; AJvYcCWV/N6qlcwf/iCsP/6PpeczyAeyT7knV6BRIKzZnxqJVpjUUfSM4SX6d7JkDUM+/pfkH1Mfgx7YEkR5bEXc1zputzreOAEICV27+E0Y X-Gm-Message-State: AOJu0YyoQRzse9KaQzb5NXd4XD4wl4965mI5iA+1j1RGdciwbYASAZLj QCFtIJ6ZA1JR4iEfGhVR/Zap7nAwGMX+TjDL4joWNzFEvwlPd+FslUlFTD7UJjLGiYgocOW86bf nn4CmSw== X-Google-Smtp-Source: AGHT+IEyHbP+pckfFvabgFHrAtPTPFgzZnnrvWiKaPJToQmUliMu4+e+EktFDuhWE7n5StUlVnHejJ0MRW+a X-Received: from irogers.svl.corp.google.com ([2620:15c:2a3:200:6d92:85eb:9adc:66dd]) (user=irogers job=sendgmr) by 2002:a0d:cb4a:0:b0:607:83bd:bea3 with SMTP id n71-20020a0dcb4a000000b0060783bdbea3mr244497ywd.10.1707886478458; Tue, 13 Feb 2024 20:54:38 -0800 (PST) Date: Tue, 13 Feb 2024 20:53:51 -0800 In-Reply-To: <20240214045356.748330-1-irogers@google.com> Message-Id: <20240214045356.748330-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: <20240214045356.748330-1-irogers@google.com> X-Mailer: git-send-email 2.43.0.687.g38aa6559b0-goog Subject: [PATCH v4 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 , James Clark , Athira Jajeev , 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. Signed-off-by: Ian Rogers Acked-by: Adrian Hunter Acked-by: Namhyung Kim --- 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.43.0.687.g38aa6559b0-goog From nobody Sun Feb 8 06:00:06 2026 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 AB35711709 for ; Wed, 14 Feb 2024 04:54:41 +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=1707886483; cv=none; b=Q/vwXzbWVxe5gUmksNEUWu4wAxlH57pbfgdm4Yb3pfs6DKzsMNoEGMOQYYDhFi7un70Uias2ShIfDFWYXOUz7ZGtCua30jsrW4LJU2kp0aL6iNSgWvuUsX/pKOzdlm74KncVFsqOq91LqSg3tqJD5h458Tc9AtmFBfT/d6+IJ5M= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1707886483; c=relaxed/simple; bh=LBou+XLQ8QDVF7i9zNrb0c+TC2HDLKGD9kRdzvqDd8M=; h=Date:In-Reply-To:Message-Id:Mime-Version:References:Subject:From: To:Content-Type; b=BviUaiLw9UHSSA+L67UDA2tIuitYCCvVl2xolN63fzOHwL5dSRIzqTBrQi3bELoH0DV4n/AJ2EJMTYgZ6qG2y1osc2LbAjPk630g46gWLjYPttw/ZKtR509zqi+XmcLU0u5CGpT1VXSEiY2mSHeLTY19I0e3eejJeKk3lMgN3bM= 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=PuZiel8+; 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="PuZiel8+" Received: by mail-yb1-f202.google.com with SMTP id 3f1490d57ef6-dc74645bfa8so2588486276.1 for ; Tue, 13 Feb 2024 20:54:41 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1707886481; x=1708491281; 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=FAs57lgcOMUxLgf079L8itwe0kfFXp2CsT7fDYu4PDI=; b=PuZiel8+TVj6wama+WrB9HM90X5Z/P77Cp0XHMEPoUS3FzSAP487pn1JBAe+Qbk7q9 AGqiX7uvF4qt2aaFpZ0Ygyg1GHJh8Z95b9gsnsBazYHW+RKr9I0a3SDrkavJcnIsQGb3 l/h3oP5JUoFEShkEp1/hb5ZxLU2dIGbqS/+SOrfFscREVyJkzTncK89zcUc6g91kVLG/ gzdPcZP0s+pgQRv33pZJlj1poghUMHsvFrA6+D8haXRsbI3tTop66F9QVaqhQMyMadi5 hCP0pHkOgOf8Z67zYXDLVvbYCYhdwQxvHDOk2FeC85JElX0aWSrojrciAXtb4ttsk8Xw IbcA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1707886481; x=1708491281; 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=FAs57lgcOMUxLgf079L8itwe0kfFXp2CsT7fDYu4PDI=; b=nygAXT44abcE8hFlh2liUVMFC3DBu53SGhPv3gFA+LQtz7MWnw9U5QuYerSqKZfTwl mueVjv6JOwkkgA24V6bmiTBwTd0qrSiDxPeGLW3CAovz97JyFQpxckrkxfx6jdml11gA 827NX3q6GC7RWkKXkewc3ui0xEA2V3CHFAOCUNVRhRG+oKjEUZ0K+fgoIayd4Lp2ZY8q QvZmhWTcimP3usFmMIyY1i1XOMZUL40kf9STA6A0QyyIVlppERRecYPDuQeFOSmrbLxk YLsvnqhL3v+gR/hD+lOmkF7alQ7fswOpA2YDJe9pBJde5ann1i5d7YIg9gJp5TzgdvoE awdA== X-Forwarded-Encrypted: i=1; AJvYcCV2n6CD3kEneuMf5oIjBx7FwKdXRXgB2phowd0hDqB0UfPIW7fbq/L9uadAClwr+IIw36jfH+scy9cKLlRJ8m3QnwnGWm52s3ejfs6H X-Gm-Message-State: AOJu0Yx+5zS9C4J7lD99ygvCE3ySXhXbdlBnzKrsGMTsGSmXa14Pd6Kt fnI/GTrnr0dOMaDQwlPtSbLw+TlBQ6sNwcc0euwJMWe4d9gvYHMmQrG4n7jO1XPmnHcSdZmg3Wc WS9NTvA== X-Google-Smtp-Source: AGHT+IFpwgld5BYgEW1Kl/VAFXVUauMsroX4LSe1K6ry/SOqA3PEmypBd2ntS4b9iIcZUKdQtHBwgISl4ugR X-Received: from irogers.svl.corp.google.com ([2620:15c:2a3:200:6d92:85eb:9adc:66dd]) (user=irogers job=sendgmr) by 2002:a05:6902:1102:b0:dc2:5456:d9ac with SMTP id o2-20020a056902110200b00dc25456d9acmr50401ybu.5.1707886480735; Tue, 13 Feb 2024 20:54:40 -0800 (PST) Date: Tue, 13 Feb 2024 20:53:52 -0800 In-Reply-To: <20240214045356.748330-1-irogers@google.com> Message-Id: <20240214045356.748330-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: <20240214045356.748330-1-irogers@google.com> X-Mailer: git-send-email 2.43.0.687.g38aa6559b0-goog Subject: [PATCH v4 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 , James Clark , Athira Jajeev , 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 Acked-by: Namhyung Kim --- 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.43.0.687.g38aa6559b0-goog From nobody Sun Feb 8 06:00:06 2026 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 4184D11CBD for ; Wed, 14 Feb 2024 04:54:44 +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=1707886485; cv=none; b=AyArDKpLXY7ek2csYA9qof/cRxKJeHMiu3tdk4WIkBFgpuX86BbsFQYELg/ZWRo3Pz8njKuauK+hIvnF8xqzR8tpVXDyOgF7bGBRO52cejPpRnTsd4rh4023UDnSHqRCjqBzN5VRD5zybhBSmP89dMajWtkaiocHjyAk03bFlbM= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1707886485; c=relaxed/simple; bh=d/hQVr7HLwaHgpbOrtRFt5ktwlU8Fbwq9EyeSqxfkuQ=; h=Date:In-Reply-To:Message-Id:Mime-Version:References:Subject:From: To:Content-Type; b=ZkTNWAW9RrFdFeGKjgAgUjBcJGZah56EPedNLNbUtWXGPbfyxR2uVLmL/1c+LiRhR9XPXQkModbmEYV1n7E03cfllYzLzC/qpT26SdGRsla3lhLn4gyDVzvIpmbonyELMSkMt4RlmY2U1qBlOAj9/nzj8DNB8N1pLHbM4QU/r24= 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=OKiYzusp; 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="OKiYzusp" Received: by mail-yb1-f202.google.com with SMTP id 3f1490d57ef6-dc74ac7d015so7382819276.0 for ; Tue, 13 Feb 2024 20:54:44 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1707886483; x=1708491283; 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=ORLaSbqJ7XxCjGmPCGfASaJCafSHFkQjULJrCBkVFqo=; b=OKiYzusp+vQXupBh2m/3KN//hxorBgcscjkzKzQJ5bkmyNZygNryMDqGsJ+i+ju/dd wsC71guPBN0S2TyPcpDxqKq8Dq6636tWxTWk+69/yQZ9svs2ey0W9vy+TmVMxLwrIRZ0 7XE19/ZT1sTF3REKfppx1B17JU5nad5o0g/JlvrUiR/j51h4CsW+a76JCIOlSFeTkVSL 0D+GdEqq6DwIej/+pxcAgBx4PVmMpjU/AXJuJsjR6hbUVzmzY8z51NGji9G8zbEF5DbJ BjlNpH3HdDa0l1rBzkIVauvQqcGwxcj5dUHJbtlT4uSGRR8g/G+/wxyu6xqouJPP3SHV Cggw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1707886483; x=1708491283; 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=ORLaSbqJ7XxCjGmPCGfASaJCafSHFkQjULJrCBkVFqo=; b=ewOqDfIuQa/LEuD1sfR/j5dKxoKCGPpb28n9/rRPZ7luZqWra//dAxPjoL0MRyelzP koIq6mhkeJoKRKakQKFDDU5VVp2S0sAmWJMOUr61R/Pn4got/p+1CFF8goDHbh0MKUE2 NSt6300PUMXlFMXvbfaDFEmZi4vZbIDdphBXTaq2sgo4KYJ1rVoXVVAQ6lDxVZAIIrtx FLxb+lqy3sLejf1pf4aVgj+SMd5YhgHPf3FfkwzSWg90bA3yGdyO9G5Q4rMyUppNgsVf HUabWbyN4+v9NpRPo5or+vHm9NKp5tgElkdOVF8PcnPxXP3+M1d8EtQeg42yOUIBjYq3 ckGw== X-Forwarded-Encrypted: i=1; AJvYcCVlJRQ2yYlOl16YEvYE4y6bQ7JHW87975fXKoN+szNcSmtmbgvAz7Q+JwKgysEHkGZ1mdbRCopopub1tXTRUlXLIMegOV6KONKjunt2 X-Gm-Message-State: AOJu0Yw0QBRAX3MvK2+KqykQRGjiua1Dn3lXbvDOI3BKw7XvGVcZjmwq rOorzcyxk/SYTglABbN9gye3pT4/PsiJrGjKWyQcBeZ3VyFE1++sv7ltuH0CWIcwEnZB4MmAtf3 DYDk2kg== X-Google-Smtp-Source: AGHT+IHtwgvbCf4Maez+fUH4FaS0TqvACXGv8O6/zRPo17W4j+pBYYEsBChdTYkPGjc1iiVxLuFBwvet1Wli X-Received: from irogers.svl.corp.google.com ([2620:15c:2a3:200:6d92:85eb:9adc:66dd]) (user=irogers job=sendgmr) by 2002:a05:6902:82:b0:dc7:9218:df3b with SMTP id h2-20020a056902008200b00dc79218df3bmr61457ybs.10.1707886483182; Tue, 13 Feb 2024 20:54:43 -0800 (PST) Date: Tue, 13 Feb 2024 20:53:53 -0800 In-Reply-To: <20240214045356.748330-1-irogers@google.com> Message-Id: <20240214045356.748330-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: <20240214045356.748330-1-irogers@google.com> X-Mailer: git-send-email 2.43.0.687.g38aa6559b0-goog Subject: [PATCH v4 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 , James Clark , Athira Jajeev , 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 Acked-by: Namhyung Kim --- 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.43.0.687.g38aa6559b0-goog From nobody Sun Feb 8 06:00:06 2026 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 919D612B60 for ; Wed, 14 Feb 2024 04:54:46 +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=1707886488; cv=none; b=tVyAMaQ05xeqht6AvOy2PW5IdSmchgRNO8kJI+g2ULDZcq0gCF3ZC3sgyF4hOHpVilE53lPQ5qaz+GqiMgt24E692fbCILcmjmqxCowZqiPEipwi7Kt4Gy5ay5WI6c8TFyZWXbDCWoHPmTZBk4oCFA5ZBCcOGTAJt3tDKYpHNVw= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1707886488; c=relaxed/simple; bh=pokSEbOrag3uWdO1RhTKTOPvPSFp8ZMJI6bQo7DbnNA=; h=Date:In-Reply-To:Message-Id:Mime-Version:References:Subject:From: To:Content-Type; b=l37XI9jufytinyp5ogtWh3U56l7/X4gmBnpf6YD/srZWZxGbpRMIzv3MziKK1CP28n5CobLNkqihn9WlSeaWR6AX83lsWaaidrsilyWv3tWuhJ184Xgwxu34OyKTvH0sxXbTgYoXhkZy37koRTJqTgCukfwYN9BltkQkyeZwL5U= 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=ACbHuTxZ; 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="ACbHuTxZ" Received: by mail-yb1-f202.google.com with SMTP id 3f1490d57ef6-dcd703b721dso1231488276.1 for ; Tue, 13 Feb 2024 20:54:46 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1707886485; x=1708491285; 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=mEeFv0uon5WQC9cFYvgD6Kwx90gDUxQUuXbCDfN9dJU=; b=ACbHuTxZthtoTd3nlDYDquxbufe2cI9F5DQ5//GukYFAaBixZOoHUFcUW96QapMyk/ ExlAX3e/p1iLUUVQqBhzWUrpzMC264ZWNcMCNht60ypZnBZFojMsAtrbes5k8s/iJpTa CAXmxjchT1drT0DcYEiLT1useD6vL8vHqNH9WcXY7NBpTjUf5nLu/tCoywQKU2rVpd59 S5AhBglynZ1heMcnXQUJhSB4v3XjKRZeAO8eG0+FCVtEDpzDVKMZsQ6D76s+GkpHu/W2 L1HvTtX9pZcd8pwRdt+2LnDKciSGY5Fo+fnmtHEe6AA4z5rPdM40AY/kNKyXn/U/UMO2 wNxw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1707886485; x=1708491285; 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=mEeFv0uon5WQC9cFYvgD6Kwx90gDUxQUuXbCDfN9dJU=; b=DTYFVfES1XZivrueaslLkOIW0lLxDtoGciaOXFscMaYxMyOHPMHrg+x/jUr5r//haC ktKE2mYfk/g+UHZmwq44j5YGlUSJ7qAi1eEcIkGQmmTvFwkde68LuEQbZQ7Bqjs6iGa7 l407jFMmqBOK0JLbExxJOdJ6OvnbXeODHf1JHySw/ylyGjV/Tyx34TMZvmjHcJbS3Ugi yGW6cULSEHQRGnLJVKb56qHpmlUeXkhB75/GMvTOLMPKPbCdkFo31QiijbEeFWDkp0Ff qACjbQb9SZaU0QfXvepblIhXhDRBM7nHT8K2MUWqLR3KWWWrGeY3LBC901DR7xltZg2K 8vJQ== X-Forwarded-Encrypted: i=1; AJvYcCU9a4dVXnzfjvM6rvodiztwPfA5+ykQ9FITOmpCntr2+hHpoUYME1J5p8heubfQnMNZt4c3rhABIKUHL34bLkUXf4MGZ3FFa+GacihH X-Gm-Message-State: AOJu0YwNsEgXQzCRVTg7uumZo7EvxCyAguxA6cOUzymeN/KhitEgoQLm QHm/+OspL9rVlIY6ivhxMbqAFqR2EGC0y8Vx4N6wIVCpuvzRKIt+EhUbRR+LDRWkTRiDUY0NH5C LMGmzdw== X-Google-Smtp-Source: AGHT+IHSet7A96qWebh2Ep2XW2Sl/AHHnGwI4zz+x2s/cywd8H/BYo3Mbauk3ICX2IgxKf7iHq+hE3T25yGQ X-Received: from irogers.svl.corp.google.com ([2620:15c:2a3:200:6d92:85eb:9adc:66dd]) (user=irogers job=sendgmr) by 2002:a25:2e4c:0:b0:dc6:44d4:bee0 with SMTP id b12-20020a252e4c000000b00dc644d4bee0mr74284ybn.7.1707886485640; Tue, 13 Feb 2024 20:54:45 -0800 (PST) Date: Tue, 13 Feb 2024 20:53:54 -0800 In-Reply-To: <20240214045356.748330-1-irogers@google.com> Message-Id: <20240214045356.748330-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: <20240214045356.748330-1-irogers@google.com> X-Mailer: git-send-email 2.43.0.687.g38aa6559b0-goog Subject: [PATCH v4 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 , James Clark , Athira Jajeev , 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 Acked-by: Namhyung Kim --- tools/perf/tests/builtin-test.c | 21 ++--- tools/perf/tests/tests-scripts.c | 144 ++++++++++++++++++------------- 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..6d5001daaf63 100644 --- a/tools/perf/tests/builtin-test.c +++ b/tools/perf/tests/builtin-test.c @@ -300,22 +300,20 @@ 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; =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); + asprintf(&cmd, "%s%s", st->file, verbose ? " -v" : ""); + if (!cmd) + return TEST_FAIL; + err =3D system(cmd); + free(cmd); if (!err) return TEST_OK; =20 @@ -331,7 +329,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 +343,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 +452,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..9b3b66dd5508 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" @@ -35,55 +36,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 +106,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 +131,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 +162,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 +172,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 +213,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.43.0.687.g38aa6559b0-goog From nobody Sun Feb 8 06:00:06 2026 Received: from mail-yb1-f201.google.com (mail-yb1-f201.google.com [209.85.219.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 2765212B85 for ; Wed, 14 Feb 2024 04:54:48 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.219.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1707886491; cv=none; b=C6lngtGn56IDuQlUhcBSgXKXMgiiDrpmoit4dEh25OPMaqj/CQ6AcG15AiCM1FICF8FCBTJhq/vaQmih+WqCSGQYX3XHYxAEh8WKMedzJbnO4HycCMnlxjTBUEuuQ76dYu/jz0JA43PmofKsMO937wMyqH+BVMCq4She8b2SeYk= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1707886491; c=relaxed/simple; bh=e3Zo3j0Y+lDByuniGWZetsyjr0YP2URaOB3bhuKdkPg=; h=Date:In-Reply-To:Message-Id:Mime-Version:References:Subject:From: To:Content-Type; b=f6nTSmrz62rHbGmKrpgwzwpnaqjd2Tw6ePe8TDb60oxK8yWJynULD/5Q34FFsaonIunlo43DH35J0QUGmqJ7pt+TnUjP0ZK1syrZ38YLOYVH1dORddujvgP1Kvk2ouOcfwnL+laAruEpbc/9dBKurdze5lCr+ZL6GZNhDkdPleA= 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=oItx5nx3; arc=none smtp.client-ip=209.85.219.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="oItx5nx3" Received: by mail-yb1-f201.google.com with SMTP id 3f1490d57ef6-dcbee93a3e1so3237316276.3 for ; Tue, 13 Feb 2024 20:54:48 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1707886488; x=1708491288; 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=pKUjhtJivPO8JCDFJHJoi47s/FDcERs+KA6j2+NQ9Pw=; b=oItx5nx3UIe6WZ75ARaLb5jj4Xbv4VfdHC43b4fI3I+lG+GhyZyF/p0jGSRj+nKEkk k6oMKgnLF40fVd+qow+fPsDwE/oiGMkCHns5El/TNCfecDJFL0TldL+A303VdGiBUJzs hnQtrtUVJQGFXXM+PqcSRVd8Bky3fTQ7n1ZPkB0ff4ruKypAyji0ux/m327nhcTR9ldW RpGyD6uEmmKkidi3jU6JQ8/ukskqHRnz5tUyctDcMtZFeLjfa7MiltiM+KIk91C76r4o FeZRh2QzmDEaK0za6I5iNRvc0Hhux4HTHtPWV59+yEfinJjp/4spnsHe8nKtV/GJjSMj k9Ig== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1707886488; x=1708491288; 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=pKUjhtJivPO8JCDFJHJoi47s/FDcERs+KA6j2+NQ9Pw=; b=fQlBrLISW9j1fLeFUdrg2KyPO5xd5cbsd9n8grucjjH3KCW4fNCmLTfVwBo3UgcseZ +sKxtHFx5cymKwH+UtPTYyUF0HWT4huvLz+B6fvkzFziPi6c9JdQjTTWSqFUXALHxXqv FK7qNzG6ZC9XAQribCqoVTfiM/t+Pll3IPMHunjoZRSx3FGRZlzI5uUkN32f5BUgY2TR JMpQs0EbhwqE5SKVPFiV5fBzez7IaNuGTNhGJmvNNFdyWWVGnJ2htFBs5qXS2uw6ZOMB g8cFVAohJSEdQSvSWM+UvbPNIjmmt4S40S2hWgLBkKxthj79Ibj3ybCJl/p8OHwNGcFK CBDg== X-Forwarded-Encrypted: i=1; AJvYcCVv7nnPeiT0sfmEyJcREp8Lia1qFkNuxKrNumsQXLV/V5qo6nBJhdTc+xAGfOtEGQB8wS6EHelQOldjx76GM3tCOZjtaQxtbjqiCwHr X-Gm-Message-State: AOJu0YxR+mDzFu4kT/UO4x0uQF53r2of1kZe/g0Vkn9tuMcIKGUiTQV9 H4JMAPcfrhEhBxqEsIgdKQAyi7UlqZo8z3Y9nciEDncPZKd00LcdiRNarmHm/AgUN22BMjMMjxd aiYp5sw== X-Google-Smtp-Source: AGHT+IFaN3HW0664XFpfWjVh+YIsZwjCWaY/qcAJfYwFw79REvzT+YK/wfYBlfPVn+iiGWFoQcPfD5tRp/xV X-Received: from irogers.svl.corp.google.com ([2620:15c:2a3:200:6d92:85eb:9adc:66dd]) (user=irogers job=sendgmr) by 2002:a05:6902:1505:b0:dcc:8be2:7cb0 with SMTP id q5-20020a056902150500b00dcc8be27cb0mr77852ybu.0.1707886488084; Tue, 13 Feb 2024 20:54:48 -0800 (PST) Date: Tue, 13 Feb 2024 20:53:55 -0800 In-Reply-To: <20240214045356.748330-1-irogers@google.com> Message-Id: <20240214045356.748330-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: <20240214045356.748330-1-irogers@google.com> X-Mailer: git-send-email 2.43.0.687.g38aa6559b0-goog Subject: [PATCH v4 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 , James Clark , Athira Jajeev , 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 Acked-by: Namhyung Kim --- tools/perf/tests/builtin-test.c | 91 +--------------------- tools/perf/tests/tests-scripts.c | 130 ++++++++++++++++++------------- tools/perf/tests/tests-scripts.h | 10 +-- 3 files changed, 81 insertions(+), 150 deletions(-) diff --git a/tools/perf/tests/builtin-test.c b/tools/perf/tests/builtin-tes= t.c index 6d5001daaf63..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,74 +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; - - asprintf(&cmd, "%s%s", st->file, verbose ? " -v" : ""); - if (!cmd) - 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)); @@ -441,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 @@ -489,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 @@ -551,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 9b3b66dd5508..e60867b1e5ce 100644 --- a/tools/perf/tests/tests-scripts.c +++ b/tools/perf/tests/tests-scripts.c @@ -26,16 +26,6 @@ #include "tests.h" #include "util/rlimit.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; @@ -131,12 +121,31 @@ 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; + + asprintf(&cmd, "%s%s", file, verbose ? " -v" : ""); + if (!cmd) + 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)); @@ -146,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; @@ -191,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) { @@ -204,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.43.0.687.g38aa6559b0-goog From nobody Sun Feb 8 06:00:06 2026 Received: from mail-yb1-f201.google.com (mail-yb1-f201.google.com [209.85.219.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 599FD12E5E for ; Wed, 14 Feb 2024 04:54:51 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.219.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1707886494; cv=none; b=fTiQoF8rmpeBHM5ozlKhuXcJEncT5lxAnlSL9rePMGqfWwkQcG08UDcEgoC4gq8XrEZIcGE7w80RyLgMLDNwBw6IGfA+Obyon7ACTmI1whqsFouQLpcU50DHRCR2IaNEtn8XNH/I5UGXwykHCEWWtu3pE5GF5VEXsZ1f7LoXhWA= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1707886494; c=relaxed/simple; bh=UFSCVd8UNIe1fnhvUnJjcNYyn7YM/8/N7szI4Y04bpY=; h=Date:In-Reply-To:Message-Id:Mime-Version:References:Subject:From: To:Content-Type; b=DkpSd3JO1FglC11IDoDdM+4V4PqYOez6J2lZlaOsF3ZUemVZYU9s+KNuTF+sHWnPg7J1XlaAjW8y1j2Y7FW91or7DOGdqIcJXUfm8du1rAV170OaA15H1xkvrr07rotZlOTUvhP4FYx213X2M/akDrbMbADHiKt8RizUwN+verI= 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=QxYNxEo4; arc=none smtp.client-ip=209.85.219.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="QxYNxEo4" Received: by mail-yb1-f201.google.com with SMTP id 3f1490d57ef6-dcc05887ee9so3363513276.1 for ; Tue, 13 Feb 2024 20:54:51 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1707886490; x=1708491290; 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=Uq1mVmEDpbr91BLlFdUce3FbPa9YnIbaer6ngXT0cLo=; b=QxYNxEo4mZtjER21jEKL33r4k8NUKaAVd/XJmUvpXISgRJM6b8NzwD51AcwnUNAT6U e/6k/z7Xuw52To5linEKqzOkUERyqDz6hosn4X0u0nKfnmO1tG4YWHMr37UtYAw+cZlW hqzuljw3E02iF0OYN6a0HcPuuSZxWkOj3lyU/yW7GkCg01B4WvK7S5I5CKiBFvTtbNaO DeGhjHtRwhf8xaN1YeFk1vJyPqsy4oxdwK1hgxsPkRKdJJWU/rAZtbZTMdHYjO5BFqYE 5EtEOzbVEPExkESfNuLRoqJI23i2QzVCai374Zfr6fQ92XEn7aZlJgkSW+GuYXcWQSSf K1pQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1707886490; x=1708491290; 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=Uq1mVmEDpbr91BLlFdUce3FbPa9YnIbaer6ngXT0cLo=; b=osHzJ094UAAKmDmB8wT/uM2EGYLbJSSxUATFy9mnvnczrLJqni5af5GGXPzKCC2qIz Mu/zMTJZftyj4Sj8Y3LF+e8wZsBQnHfnOJMGjC88vHYpjUTow0h+3sMZMM4b9BUkoiMR WIMejuy5B/Vmnbidy7NnAnfvaQ/S/kcBu974c/Qq1D+lwGVkrrxlQOLoubBDFToUFp9p dRkUQueIczANooEeEi81R4fSj7n0DT15Ueax3D0JT96endq9nrMZH0zGjz0MwmJa1has zWjbrSdDidcwDWmW0fBVdKKe0OqFkpFWU3YlTAjPnf4NjJRnBs4RvMoEL8xsxrY0aZe+ DZPA== X-Forwarded-Encrypted: i=1; AJvYcCW93R/uUwCSJ9VYT9CLMh7ntXJZJIT91HA5Wfq4I2vinIGOjF+mUyn57Rmc1vEpLH0PM+prqpMG8+ohROr3ejnUHGTrzGD0xfp4LvQL X-Gm-Message-State: AOJu0YzlvpoXNcKE3ICS2cW56N3YDyTratqeXJNhcJ112EXUVK8h39jq oBYDeyTOfLhdSVFsRrxVdXb8PMmz2xt9Q1Mf7zrUXpKbBQ3GOjVhGPi3sau0qV5Pw9MIZHpzcno vdOeCqw== X-Google-Smtp-Source: AGHT+IFq2sY6TpPL4nsNdVBUPnRcAjii8+2j6P/VnCMM5FBh9m2ONzSfdyD2AYaGkdsQF1LAJDURePJkZBO8 X-Received: from irogers.svl.corp.google.com ([2620:15c:2a3:200:6d92:85eb:9adc:66dd]) (user=irogers job=sendgmr) by 2002:a05:6902:100f:b0:dc7:3189:4e75 with SMTP id w15-20020a056902100f00b00dc731894e75mr44337ybt.3.1707886490408; Tue, 13 Feb 2024 20:54:50 -0800 (PST) Date: Tue, 13 Feb 2024 20:53:56 -0800 In-Reply-To: <20240214045356.748330-1-irogers@google.com> Message-Id: <20240214045356.748330-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: <20240214045356.748330-1-irogers@google.com> X-Mailer: git-send-email 2.43.0.687.g38aa6559b0-goog Subject: [PATCH v4 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 , James Clark , Athira Jajeev , 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 Acked-by: Namhyung Kim --- v4 fix width computation with subtests bug caught by Namhyung. v1 of this code had a bug where stdout/stderr weren't read fully. This and additional issues/improvements are dealt with in v2. 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.43.0.687.g38aa6559b0-goog