From patchwork Wed Feb 14 04:53:49 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ian Rogers X-Patchwork-Id: 200805 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:7300:bc8a:b0:106:860b:bbdd with SMTP id dn10csp991423dyb; Tue, 13 Feb 2024 20:57:32 -0800 (PST) X-Forwarded-Encrypted: i=3; AJvYcCXUnCkOnsP9PJRATMq6qG84NnGsq3mNujXGik1kM13aw1NpxI0sAPEIaDDt4faqoa3CR4T5Bki976j2IwJAitvRg/vEJA== X-Google-Smtp-Source: AGHT+IEJS6XmNISiVXTZ/Ggb2sFoUCFZA+DdsTM+crgPeenbEBUF80oxdv1iKWF7bQlHYq8Nzk1o X-Received: by 2002:a05:6358:708:b0:176:99f3:8d11 with SMTP id e8-20020a056358070800b0017699f38d11mr1177639rwj.32.1707886651707; Tue, 13 Feb 2024 20:57:31 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1707886651; cv=pass; d=google.com; s=arc-20160816; b=rN/GeV+1DmDTA7u4/UPk9ydqVlj0LItpDdxeo06vDQxIPOkP/BONalW2DWrznuwGux 24Bwlg9KZhrDDx6qTPClvLsSnY5wOAC6P1Jt1dHBtn1SDwhFoS18p53xvXqw0ch9NTMQ rIeklZ3v9kVxUiYBw0wjmNUxTVT5RgK6trK1ZZ59cMOTBU33jonMiJ6clCM4iRhkGPDX Fzyw2nlDUOgSWNrlZsEUyC5w6Lw1ZBaplLu0kiaYnzVuKkQzqI1bpjhaFN1UA5RJQxCm c6f/WYLdag3WtKyPAH2NzS5nWxsYM5BzX9uFT7DlS9PF1vdNV7aQJS2SR7CHyWA6SeW1 DNBw== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=to:from:subject:references:mime-version:list-unsubscribe :list-subscribe:list-id:precedence:message-id:in-reply-to:date :dkim-signature; bh=F6UUwQ6uU0gh1AgBT5LAdlyWuLVQW/eRWP9wNVATd0w=; fh=VU2gOBkHnSAjjQAmdtXiZzDWQs0EKB7DH3lRv/VNdNY=; b=YL0WOB58GiNFVR2oajfCuk14Kr7d4pD79WD2+F608qxBXl/VoavVj/D9ElizjTBKm/ 254aM3BioE+BjdWL3LCJsQsWqMW0KLz9HV576I4DEDDPF/KSMemrYqjnFb9Y5TPY9Eh0 jBKnPOLasdlk2KivECSan7usRQndNDW2f1NK1+5TVcIlpR7jMNNjExO0SplJ0Bf2isSo u5GFmK/GWewkdl5cxgkAY1KEAnjNBRK1Kkmeg75nFLPi0SOEaPbUAiQCrU+z2krwbx03 0oF7yHdVFRLAfB/NsTcit/cy20b96H93zwFxAzYVBolHN7DlCAkitFhRATTbN4GhuQNJ vLow==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=xUNo3MuT; arc=pass (i=1 spf=pass spfdomain=flex--irogers.bounces.google.com dkim=pass dkdomain=google.com dmarc=pass fromdomain=google.com); spf=pass (google.com: domain of linux-kernel+bounces-64737-ouuuleilei=gmail.com@vger.kernel.org designates 139.178.88.99 as permitted sender) smtp.mailfrom="linux-kernel+bounces-64737-ouuuleilei=gmail.com@vger.kernel.org"; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com X-Forwarded-Encrypted: i=2; AJvYcCXFClow+MH6DtWQTw3U8VQOAcrH7TmA+yjtCJ/OY/704s0WB0aj5E8NoIgpkKmLekn+fj7ho/kdqAXxuFog5ujzXiUEWA== Received: from sv.mirrors.kernel.org (sv.mirrors.kernel.org. [139.178.88.99]) by mx.google.com with ESMTPS id l10-20020a65680a000000b005dc34078517si3110087pgt.538.2024.02.13.20.57.31 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 13 Feb 2024 20:57:31 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-64737-ouuuleilei=gmail.com@vger.kernel.org designates 139.178.88.99 as permitted sender) client-ip=139.178.88.99; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=xUNo3MuT; arc=pass (i=1 spf=pass spfdomain=flex--irogers.bounces.google.com dkim=pass dkdomain=google.com dmarc=pass fromdomain=google.com); spf=pass (google.com: domain of linux-kernel+bounces-64737-ouuuleilei=gmail.com@vger.kernel.org designates 139.178.88.99 as permitted sender) smtp.mailfrom="linux-kernel+bounces-64737-ouuuleilei=gmail.com@vger.kernel.org"; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by sv.mirrors.kernel.org (Postfix) with ESMTPS id B20D028B9E1 for ; Wed, 14 Feb 2024 04:55:01 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 0333710A28; Wed, 14 Feb 2024 04:54:39 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="xUNo3MuT" 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 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-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 X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1790848953794318390 X-GMAIL-MSGID: 1790848953794318390 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_cpus(uid_t uid) snprintf(path, sizeof(path), "/proc/%d/task", pid); items = scandir(path, &namelist, filter, NULL); - if (items <= 0) - goto out_free_closedir; - + if (items <= 0) { + pr_debug("scandir for %d returned empty, skipping\n", pid); + continue; + } while (threads->nr + items >= max_threads) { max_threads *= 2; grow = true; @@ -152,8 +153,6 @@ static struct perf_thread_map *__thread_map__new_all_cpus(uid_t uid) for (i = 0; i < items; i++) zfree(&namelist[i]); free(namelist); - -out_free_closedir: zfree(&threads); goto out_closedir; } From patchwork Wed Feb 14 04:53:50 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ian Rogers X-Patchwork-Id: 200806 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:7300:bc8a:b0:106:860b:bbdd with SMTP id dn10csp991517dyb; Tue, 13 Feb 2024 20:57:44 -0800 (PST) X-Forwarded-Encrypted: i=3; AJvYcCXXfTjzXzNhfDXIU/Dr722eeen0iHoE6KCoPAnUbLdrap7nx+IGxkwUFFRzH3m2/3GEAG80jRlsEYRk9U79GOEjOQKZxA== X-Google-Smtp-Source: AGHT+IHwoFJPFaBvt2/1sm7BTWaJcg4aybl1ALe37uUfuDRDYbUqk7u2AVJMt5msN0bTm8f+I/Cr X-Received: by 2002:a17:902:d315:b0:1d9:70b8:8c4b with SMTP id b21-20020a170902d31500b001d970b88c4bmr1619028plc.68.1707886663958; Tue, 13 Feb 2024 20:57:43 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1707886663; cv=pass; d=google.com; s=arc-20160816; b=urxcy6Nkoc+cHEAuf4aUA4LwHxebfH5r0+wP0iBaUa4erKH60QS76fvKG5pTqTio8p lEe7eyVKsrUKvHx4s+ZU+ytZ8FpEmze6dxkDBqSujfmJ3/eB8eyKsKUb5C7CFmbOR1Z4 wlxaZ6JQrAVjhxHn/h3nL5u6I1CuHVMLC6xhHE9I7AcSp0Pb2If9Szwt09W6lvt64vKx tWBLIlN1WcpNmP4Y6ZAK+wvaTDdqYgp2iOo49m2b+joV3hG4K4WK9jNuvxvqEUtkNM52 wXbIS7MqerLfN8OfbcmXk6doEPwUzI3EdFkhuoTedUEwLj6ICcyfl9mJa+wfD4Xw0peI uUnw== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=to:from:subject:references:mime-version:list-unsubscribe :list-subscribe:list-id:precedence:message-id:in-reply-to:date :dkim-signature; bh=YmSZNQ356QtM9WesWCMinXmK9k2yULQ13GYKx7NLJhc=; fh=JW9InCv+dfDSj03e8lDTVNNKQSfwNZ4bEtkodjX4uBA=; b=pyqoaR8mF2lTS1Jeg0WVzgeqedexsTRDp1PSsbF1Pn9R0qZ4d+mn7ubdsd0oqUxFxD Ov+3M6ZZ+oc6HlJ75jrV7E2XTXY8ps0WFPWcC4pe4QBJnIOk+LpLEEwv9NqCBN0IZfbX ol0u9jEyYH0bqR9ZvYFscQ9FZkF1f/zF6Z6GQfGFRxxgG1PHMtzWB7qQkxQTYJgL7cH2 3QgBKksM4sflYFvrLE2+mzVn7dsj3GSi+ZFe22roMaAFBkp8TYxxo+aMQhkyW8EoA3SG IzDzIG7dHo8USLQHuZS8UijRF1Iz4zPhrPYGQ32+6aoDnIQbzCl00W+/32it8ZFhWfT6 kMYg==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=1xGd8ewx; arc=pass (i=1 spf=pass spfdomain=flex--irogers.bounces.google.com dkim=pass dkdomain=google.com dmarc=pass fromdomain=google.com); spf=pass (google.com: domain of linux-kernel+bounces-64738-ouuuleilei=gmail.com@vger.kernel.org designates 139.178.88.99 as permitted sender) smtp.mailfrom="linux-kernel+bounces-64738-ouuuleilei=gmail.com@vger.kernel.org"; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com X-Forwarded-Encrypted: i=2; AJvYcCVOCKgY++sfTcABdHf27fFkrz4GfWdLGciu0AoxPdswhInQMYQZBq8KfzZ3RKuuHekhtBOyRF6YOky+ZRHtVM0bMNiXZQ== Received: from sv.mirrors.kernel.org (sv.mirrors.kernel.org. [139.178.88.99]) by mx.google.com with ESMTPS id y2-20020a17090264c200b001db55c24a30si748941pli.236.2024.02.13.20.57.43 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 13 Feb 2024 20:57:43 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-64738-ouuuleilei=gmail.com@vger.kernel.org designates 139.178.88.99 as permitted sender) client-ip=139.178.88.99; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=1xGd8ewx; arc=pass (i=1 spf=pass spfdomain=flex--irogers.bounces.google.com dkim=pass dkdomain=google.com dmarc=pass fromdomain=google.com); spf=pass (google.com: domain of linux-kernel+bounces-64738-ouuuleilei=gmail.com@vger.kernel.org designates 139.178.88.99 as permitted sender) smtp.mailfrom="linux-kernel+bounces-64738-ouuuleilei=gmail.com@vger.kernel.org"; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by sv.mirrors.kernel.org (Postfix) with ESMTPS id 4D51328D380 for ; Wed, 14 Feb 2024 04:55:13 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 7BEA31119B; Wed, 14 Feb 2024 04:54:41 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="1xGd8ewx" 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 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-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 X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1790848966655584657 X-GMAIL-MSGID: 1790848966655584657 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 | 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 = get_tracing_file("events"); int events_fd = open(events_path, O_PATH); + struct dirent **sys_namelist = NULL; + int sys_items; put_tracing_file(events_path); if (events_fd < 0) { @@ -70,10 +72,7 @@ void print_tracepoint_events(const struct print_callbacks *print_cb __maybe_unus return; } -#ifdef HAVE_SCANDIRAT_SUPPORT -{ - struct dirent **sys_namelist = NULL; - int sys_items = tracing_events__scandir_alphasort(&sys_namelist); + sys_items = tracing_events__scandir_alphasort(&sys_namelist); for (int i = 0; i < sys_items; i++) { struct dirent *sys_dirent = sys_namelist[i]; @@ -130,11 +129,6 @@ void print_tracepoint_events(const struct print_callbacks *print_cb __maybe_unus } 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 available tracepoint events.\n", events_path); -#endif close(events_fd); } 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 = openat(dirfd, dirp, O_PATH); + + if (fd < 0) + return fd; + + snprintf(path, sizeof(path), "/proc/%d/fd/%d", getpid(), fd); + err = 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 +#include #include #include #include @@ -56,6 +57,13 @@ int perf_tip(char **strp, const char *dirpath); int sched_getcpu(void); #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 **)); +#endif + extern bool perf_singlethreaded; void perf_set_singlethreaded(void); From patchwork Wed Feb 14 04:53:51 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ian Rogers X-Patchwork-Id: 200801 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:7300:bc8a:b0:106:860b:bbdd with SMTP id dn10csp990856dyb; Tue, 13 Feb 2024 20:55:50 -0800 (PST) X-Forwarded-Encrypted: i=3; AJvYcCVkSUhwDCl+9qDZcpPE9sCv9YnlZttu+i9B9u1Ih+8BoCHTV0+8wEwLprYcIOYHMeSxNotTBT0wyPHsUIysX6tjA6uOlA== X-Google-Smtp-Source: AGHT+IHPeY9WJ2e6guaZ9vHqUIARtzAjdzvdNbDkEUfIguQXh03YM20JxLZkB7TcbYlJcFIM3hhJ X-Received: by 2002:a05:620a:b86:b0:787:24e6:baa8 with SMTP id k6-20020a05620a0b8600b0078724e6baa8mr1602297qkh.56.1707886550196; Tue, 13 Feb 2024 20:55:50 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1707886550; cv=pass; d=google.com; s=arc-20160816; b=TAEun4wEJO/zKRRoQR7cC0iyPVlxOKYM67COMcUgV12Cfa0Uu89FZX7hNcH1Urwhdf KPwjiwdnHs5BP+BWipHyUaH5Phu5a0GU5hfqgCtOWb0TOT2pJfkYDNcuj6etd0zi2ZTC d3KC2ASp2MvSmR+brpH2iSvt/0yLjbNwvZtFCe36uuVWZT5Yu2K7sJlTZ3QBKhUHC0BY vUbYO5zfZzt7BPW7cr0AEKFud8MCd3Jr0mBcxuL6HqsBYKN8HSb71Ben1QlPmlgUP+pz 6WMTHqyuMnyyHDtWuBXK43GTKg6O8PHPYmNcQj0NQHurYPuwO3q9sbki+8acME06YT7/ eWEw== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=to:from:subject:references:mime-version:list-unsubscribe :list-subscribe:list-id:precedence:message-id:in-reply-to:date :dkim-signature; bh=WQsEsQdYL+F7rq54Mfa+75HgoPY/TwxmW9rhD6V78kU=; fh=/zT6VuKwH6/WkhoOjhSKKEgeusqx5z4m2zHD8xtWrK4=; b=Jfe/5j/uoCQ0kN7E1vuJjRVI4c3FJXZ8ccx7ibVj8kS8DO0N1DCqYeQQJ34T6J4CQU yXW5ELo0iu9+BYe5RSo/9HW2nDMYXZl0IWGUNajUesqoY5Z6Yka0i7MXraCOnWriZESj S/RJGxGqlM8ilolNXnRRBjoGW9/mIWmTEU3d+ixJaQFsu9rWgOKLpfRdZW1y+QGt/g4k +TlwTeHIDRCxGtO7CUAlw2v/MtbLlGqCYsbCUUBjyNCor5Vtx9mM51KmFt8RilRp6hoq WDRlllKl7kSzXmgNM8il788XwkYtSYMeGE7f7V4+vSI8ULSpskXLOH4NKTVGH3m2/ni/ D43Q==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=ka1HKHnw; arc=pass (i=1 spf=pass spfdomain=flex--irogers.bounces.google.com dkim=pass dkdomain=google.com dmarc=pass fromdomain=google.com); spf=pass (google.com: domain of linux-kernel+bounces-64739-ouuuleilei=gmail.com@vger.kernel.org designates 147.75.199.223 as permitted sender) smtp.mailfrom="linux-kernel+bounces-64739-ouuuleilei=gmail.com@vger.kernel.org"; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com X-Forwarded-Encrypted: i=2; AJvYcCUlbieo3f918yce44/VOFgpGtBU0pM3MxvmyFAduAIG6yPzG+XcQeyG1kT5Tlxi4BbBLLy0Aa0eX9iH6PgS6RN/S3R/LQ== Received: from ny.mirrors.kernel.org (ny.mirrors.kernel.org. [147.75.199.223]) by mx.google.com with ESMTPS id a12-20020a05620a102c00b00785b33ee81fsi9868900qkk.623.2024.02.13.20.55.50 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 13 Feb 2024 20:55:50 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-64739-ouuuleilei=gmail.com@vger.kernel.org designates 147.75.199.223 as permitted sender) client-ip=147.75.199.223; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=ka1HKHnw; arc=pass (i=1 spf=pass spfdomain=flex--irogers.bounces.google.com dkim=pass dkdomain=google.com dmarc=pass fromdomain=google.com); spf=pass (google.com: domain of linux-kernel+bounces-64739-ouuuleilei=gmail.com@vger.kernel.org designates 147.75.199.223 as permitted sender) smtp.mailfrom="linux-kernel+bounces-64739-ouuuleilei=gmail.com@vger.kernel.org"; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ny.mirrors.kernel.org (Postfix) with ESMTPS id C12551C2288C for ; Wed, 14 Feb 2024 04:55:30 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 947B3125A5; Wed, 14 Feb 2024 04:54:44 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="ka1HKHnw" 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 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-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 X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1790848847111497328 X-GMAIL-MSGID: 1790848847111497328 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 --- 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/tests/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 @@ 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 From patchwork Wed Feb 14 04:53:52 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ian Rogers X-Patchwork-Id: 200800 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:7300:bc8a:b0:106:860b:bbdd with SMTP id dn10csp990801dyb; Tue, 13 Feb 2024 20:55:40 -0800 (PST) X-Forwarded-Encrypted: i=3; AJvYcCVcoagfZunpX0Huy/uD3UYeejO6+tyEYUGscSdcddxDXIEhKsx8SQ4uVsK9g2pQaTpF2SSOWm6U3/jPjZiOwV3XONBJDw== X-Google-Smtp-Source: AGHT+IGMCGWkuOtBvYv6T2aaN2Z6n0KAOWp+kCwrCq78pz/E/ov6r5TBZQL8OK2DVBF4NQyeG6gi X-Received: by 2002:aa7:cc0f:0:b0:562:f25:b125 with SMTP id q15-20020aa7cc0f000000b005620f25b125mr1085086edt.36.1707886540669; Tue, 13 Feb 2024 20:55:40 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1707886540; cv=pass; d=google.com; s=arc-20160816; b=knMyOAHVs9H0+7FgbMNJypqM2OPwPoiCrjYxL6GXxmBovD6eQkraqym6XwbX1YSHPc WXybVu4ajZyz3xSSCJlM05O9H6EdwyXAZF30C3gBNun44BsCW9xKyWF0yUOzAWt9flCV k0nPOH0GDDOWzaGN6vd0qbdiaBlk/Iv0n6KHC2OWFGS4Pul8bh2yCXw0uttT30TUTFme RxGhi+9v2ACJFNN97LqFoz6Qa6DcrE2pldcsgq11jKP8bltPcpF0dHvRBrGeJiRtymQm ZEJ21R6wkDf/yjy2RCQm/1cin82T53T2VskL5nFKugCfuo45vyY1tTnIPih86k4DWWc5 r7Nw== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=to:from:subject:references:mime-version:list-unsubscribe :list-subscribe:list-id:precedence:message-id:in-reply-to:date :dkim-signature; bh=FAs57lgcOMUxLgf079L8itwe0kfFXp2CsT7fDYu4PDI=; fh=CWJaSDwmSsKDjtJ8m5dKhqfhzFxeolZoGSXfPK9sibI=; b=JbiaTYCw6eI3yvPgZw8vr7vSUGT2PCRzjxUuyUxCQynzuYPJKUpY05Z8vI6gsxK5Pu veebmUDSBVUuMTngs/sbNpETT80K+UjeXs97Of/S+1DL8alnvPUa05VYPWJGBi5BVKJT uv3hwHLsGoA4C28om4RXRXrGOxiAgD7y2u+lbfbCvsX1ka+Wc5kfjDA9kpCB6ytmZ0nm L1cFY+t9fA/kaEKz4XSsYPfze/h8+cr4d/np8vhKTU3FAByQdb5ADpBep+eE11AwcfT+ YJ/c8g+e3NqEu5O/wsnysj5b3SeRYvxLGqmVvWfoR22kQUguN6AAzt1boY0Y0o38Xf49 d/NQ==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=PuZiel8+; arc=pass (i=1 spf=pass spfdomain=flex--irogers.bounces.google.com dkim=pass dkdomain=google.com dmarc=pass fromdomain=google.com); spf=pass (google.com: domain of linux-kernel+bounces-64740-ouuuleilei=gmail.com@vger.kernel.org designates 2604:1380:4601:e00::3 as permitted sender) smtp.mailfrom="linux-kernel+bounces-64740-ouuuleilei=gmail.com@vger.kernel.org"; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com X-Forwarded-Encrypted: i=2; AJvYcCXwZmiN6Hu6OFKVAPOF4Whv0kLMVCJInkqE4OnRJPQbIwlsEWEFTigd2HiD6gmP8Vjdpa0b2pLc+7TG7nRZk24jbCEdWw== Received: from am.mirrors.kernel.org (am.mirrors.kernel.org. [2604:1380:4601:e00::3]) by mx.google.com with ESMTPS id s16-20020aa7c550000000b0055eef1ceb3dsi4431459edr.197.2024.02.13.20.55.40 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 13 Feb 2024 20:55:40 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-64740-ouuuleilei=gmail.com@vger.kernel.org designates 2604:1380:4601:e00::3 as permitted sender) client-ip=2604:1380:4601:e00::3; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=PuZiel8+; arc=pass (i=1 spf=pass spfdomain=flex--irogers.bounces.google.com dkim=pass dkdomain=google.com dmarc=pass fromdomain=google.com); spf=pass (google.com: domain of linux-kernel+bounces-64740-ouuuleilei=gmail.com@vger.kernel.org designates 2604:1380:4601:e00::3 as permitted sender) smtp.mailfrom="linux-kernel+bounces-64740-ouuuleilei=gmail.com@vger.kernel.org"; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by am.mirrors.kernel.org (Postfix) with ESMTPS id 4C5EB1F29153 for ; Wed, 14 Feb 2024 04:55:40 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id EB090125C2; Wed, 14 Feb 2024 04:54:45 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="PuZiel8+" 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 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-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 X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1790848837262205970 X-GMAIL-MSGID: 1790848837262205970 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); }; int start_command(struct child_process *); From patchwork Wed Feb 14 04:53:53 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ian Rogers X-Patchwork-Id: 200802 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:7300:bc8a:b0:106:860b:bbdd with SMTP id dn10csp990914dyb; Tue, 13 Feb 2024 20:56:03 -0800 (PST) X-Forwarded-Encrypted: i=3; AJvYcCXBnChdrQlqp+8NcohWojVNMsFNTwPRpNd+q7cWXHRu3qU7+UKNrEw2pX0FhCe8Fkx3CB13t8GIM7lUNmKIhPbMWhEYeg== X-Google-Smtp-Source: AGHT+IHBDZK3RsOMUh8cZPahdgzZVNPqDvAwh5qwh2H4/qH+o2yCarEbLjvS7AXtEGW2g0pXkToD X-Received: by 2002:ac8:5bd1:0:b0:42c:7bb1:20f8 with SMTP id b17-20020ac85bd1000000b0042c7bb120f8mr1841817qtb.59.1707886563037; Tue, 13 Feb 2024 20:56:03 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1707886563; cv=pass; d=google.com; s=arc-20160816; b=p+y4b12kRhOjek85c80F9IYy+/W1/J64bEv6xt1Jchugq9yNwUG+dtL3EA/W6udh3O 3LT0Az0YVOZ9JcDHFwgPbb3qpnrN3l0ZpCbLdaLgZ5LRFWc6KT3UMv63OcWuhxVkKw6t nZgrivVeiNe/pfUFX8j/gpZnCQxSWwtNxbPz5mqO04gp/ifC6r0A9K8ZMnrHm/BrttV8 DChMacdOIRhttoIONDY5+aFGsUirbZ+Gf4PgOLQHkTOyMx6znslNVN71CY3C5epTDYjm nNQ28AT+DO2o6ySS5gfZO1U69ExXBXfrmLQGgNz2x/zVgM97kywXyUHDueMEPIaAjN8e W/hQ== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=to:from:subject:references:mime-version:list-unsubscribe :list-subscribe:list-id:precedence:message-id:in-reply-to:date :dkim-signature; bh=ORLaSbqJ7XxCjGmPCGfASaJCafSHFkQjULJrCBkVFqo=; fh=i4KY6UP7bZxmOoz5TSZpwoaM2C8LnFkgGJAPyBgVNWY=; b=O5ZQPp0IiW3uPFBcvjCxDDaPPYDWUgpalmjAi1KGZqQvnwevCKKAQzxEiueFaV0g2W dViW84YrO63379++p8mcDFrLCqrqh37DGcYaAZ19d5mNvdU8dbjn8smAFCxZq3vy3Rvm +ORh15O9Y1JFcvJjnO0/7q7KvBjHWkLdL+6BCjcZY9nS0iwSPsRsu160uUu1295Y4YKk 28QUVkaCh/u17ObrqvYSLtxOJp6T+1o4+eWKWKGgvLn67U+pOOaLP3uy4Ahl8j+Ucf/q XdTd10MhQgREaowQFVCHLry6zT4Pg3HuQv0hwcBXgHunz8FzF83cTr6iEwR0Qs0Avu9r ihNQ==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=OKiYzusp; arc=pass (i=1 spf=pass spfdomain=flex--irogers.bounces.google.com dkim=pass dkdomain=google.com dmarc=pass fromdomain=google.com); spf=pass (google.com: domain of linux-kernel+bounces-64741-ouuuleilei=gmail.com@vger.kernel.org designates 147.75.199.223 as permitted sender) smtp.mailfrom="linux-kernel+bounces-64741-ouuuleilei=gmail.com@vger.kernel.org"; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com X-Forwarded-Encrypted: i=2; AJvYcCWlLhG6t2j0duDqjRuwMbjcVfbfp98jAlDFjeDbLsI+ILUwpUIOCEgBgUX1LIzljZVeG+s3uSNY6vrWTgRo3DQOVsfVbw== Received: from ny.mirrors.kernel.org (ny.mirrors.kernel.org. [147.75.199.223]) by mx.google.com with ESMTPS id z23-20020a05620a08d700b00787158bd882si4371818qkz.238.2024.02.13.20.56.02 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 13 Feb 2024 20:56:03 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-64741-ouuuleilei=gmail.com@vger.kernel.org designates 147.75.199.223 as permitted sender) client-ip=147.75.199.223; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=OKiYzusp; arc=pass (i=1 spf=pass spfdomain=flex--irogers.bounces.google.com dkim=pass dkdomain=google.com dmarc=pass fromdomain=google.com); spf=pass (google.com: domain of linux-kernel+bounces-64741-ouuuleilei=gmail.com@vger.kernel.org designates 147.75.199.223 as permitted sender) smtp.mailfrom="linux-kernel+bounces-64741-ouuuleilei=gmail.com@vger.kernel.org"; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ny.mirrors.kernel.org (Postfix) with ESMTPS id 804531C28391 for ; Wed, 14 Feb 2024 04:55:51 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 9F11BD30B; Wed, 14 Feb 2024 04:54:48 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="OKiYzusp" 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 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-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 X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1790848860975749765 X-GMAIL-MSGID: 1790848860975749765 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 => tests-scripts.c} | 2 +- tools/perf/tests/{builtin-test-list.h => tests-scripts.h} | 4 ++++ 4 files changed, 7 insertions(+), 3 deletions(-) rename tools/perf/tests/{builtin-test-list.c => tests-scripts.c} (99%) rename tools/perf/tests/{builtin-test-list.h => 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 perf-y += builtin-test.o -perf-y += builtin-test-list.o +perf-y += tests-scripts.o perf-y += parse-events.o perf-y += dso-data.o perf-y += attr.o diff --git a/tools/perf/tests/builtin-test.c b/tools/perf/tests/builtin-test.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 -#include "builtin-test-list.h" +#include "tests-scripts.h" 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 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 */ From patchwork Wed Feb 14 04:53:54 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ian Rogers X-Patchwork-Id: 200803 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:7300:bc8a:b0:106:860b:bbdd with SMTP id dn10csp990948dyb; Tue, 13 Feb 2024 20:56:09 -0800 (PST) X-Forwarded-Encrypted: i=3; AJvYcCXktVYlShvmoH2CKYFOPcFHe69OpXEZKzzdxTpDZf0dDr7siujxmDcwQH5deU35DIx4MCyV1Eymn7csvFtCafvvH/Cl4w== X-Google-Smtp-Source: AGHT+IHzXPgbkoeXEV1E5AGeLMCSwRhzygJKRbFjzhwBQl8/RyxWJz0kVFroTyggX3wvZld773Jw X-Received: by 2002:ac2:456a:0:b0:511:4860:425f with SMTP id k10-20020ac2456a000000b005114860425fmr963008lfm.17.1707886568903; Tue, 13 Feb 2024 20:56:08 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1707886568; cv=pass; d=google.com; s=arc-20160816; b=pOw7n3B8bAlmUauv2efkfpEuNX9hg+/8HRueOj1ehzrtdB4zGIPL4IfKxuQbHn7GZ/ xzbQFJXOT5xinHDLA+jP3vPxx8q6sOdHZUIJIVRDHRnXBWjkL0Yq1r4I/8qO36ZCBTmM ItVZQ9v6widm2Cgd7BtA/4KD2/QqMcTE+MLMKQ6hElsPgOC7PZLhZoiEyk74gvyQivgc 9BR4bIh9WI97jZFSafSac48yPB3EfU8JzqhHUbBt5wGJRtBlvia4dcUadW2mzWg8c7uq 8pqJLZ4BAVrKcVMlzabkMFWR+lv003vAblm/k9CqCzrEdIML3+2MMSYVzepk4zxSCGUU 3yJQ== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=to:from:subject:references:mime-version:list-unsubscribe :list-subscribe:list-id:precedence:message-id:in-reply-to:date :dkim-signature; bh=mEeFv0uon5WQC9cFYvgD6Kwx90gDUxQUuXbCDfN9dJU=; fh=7tLEiN/fDB5JnMAwzh/kHkYr2MTV+/7/BGKawelYOZg=; b=SONHY6xaD0dOJTkN4nXTb4ETZWS0ZN/Y4UoKjqbGmC0QMONej1HjyT9L2d0frDz/Iw smkrjWU2tEDJljBEwQnIGzzi2Yc0PUjHlbFD2eAoq6nn84Xcow0n+8ubeSJ+cP5zipv/ j1s8d7gyGQNAAADZIaJY8wvn/7x5JcSBHEOHqDhK8hNX64tXrATbkI+WZ514w4Wy5ptY P83qOnuiHIYRnbimSygc0Mq3MWgbOvJuhKyT/Ib2FPTDmuQVwOLkIxDeqlk9+BE2Tanj ns248fD2kycZA9AU2+/XVZeti3vMCxJqQxBXKjHGi6rGYIk61EAwzXb2gWHRiTwinhyp qnog==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=ACbHuTxZ; arc=pass (i=1 spf=pass spfdomain=flex--irogers.bounces.google.com dkim=pass dkdomain=google.com dmarc=pass fromdomain=google.com); spf=pass (google.com: domain of linux-kernel+bounces-64742-ouuuleilei=gmail.com@vger.kernel.org designates 2604:1380:4601:e00::3 as permitted sender) smtp.mailfrom="linux-kernel+bounces-64742-ouuuleilei=gmail.com@vger.kernel.org"; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com X-Forwarded-Encrypted: i=2; AJvYcCUlFzy74AKN6a7GsUiAu4O8R/nqSxn8wtPutszJ7j0CBSayerb4JArL3RNAFMjuVdOyFDpv3zkUUEQ1oaxyUB71kJ9+EQ== Received: from am.mirrors.kernel.org (am.mirrors.kernel.org. [2604:1380:4601:e00::3]) by mx.google.com with ESMTPS id mf22-20020a170906cb9600b00a3cf1f110b2si1523952ejb.142.2024.02.13.20.56.08 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 13 Feb 2024 20:56:08 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-64742-ouuuleilei=gmail.com@vger.kernel.org designates 2604:1380:4601:e00::3 as permitted sender) client-ip=2604:1380:4601:e00::3; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=ACbHuTxZ; arc=pass (i=1 spf=pass spfdomain=flex--irogers.bounces.google.com dkim=pass dkdomain=google.com dmarc=pass fromdomain=google.com); spf=pass (google.com: domain of linux-kernel+bounces-64742-ouuuleilei=gmail.com@vger.kernel.org designates 2604:1380:4601:e00::3 as permitted sender) smtp.mailfrom="linux-kernel+bounces-64742-ouuuleilei=gmail.com@vger.kernel.org"; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by am.mirrors.kernel.org (Postfix) with ESMTPS id 5779F1F291F9 for ; Wed, 14 Feb 2024 04:56:08 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id D5D7311709; Wed, 14 Feb 2024 04:54:52 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="ACbHuTxZ" 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 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-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 X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1790848866591800980 X-GMAIL-MSGID: 1790848866591800980 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 | 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-test.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 subtest) } struct shell_test { - const char *dir; const char *file; }; static int shell_test__run(struct test_suite *test, int subdir __maybe_unused) { int err; - char script[PATH_MAX]; struct shell_test *st = test->priv; + char *cmd; - path__join(script, sizeof(script) - 3, st->dir, st->file); - - if (verbose > 0) - strncat(script, " -v", sizeof(script) - strlen(script) - 1); - - err = system(script); + asprintf(&cmd, "%s%s", st->file, verbose ? " -v" : ""); + if (!cmd) + return TEST_FAIL; + err = system(cmd); + free(cmd); if (!err) return TEST_OK; @@ -331,7 +329,7 @@ static int run_shell_tests(int argc, const char *argv[], int i, int width, files = list_script_files(); if (!files) return 0; - for (file = files; file->dir; file++) { + for (file = files; file->file; file++) { int curr = i++; struct test_case test_cases[] = { { @@ -345,13 +343,12 @@ static int run_shell_tests(int argc, const char *argv[], int i, int width, .test_cases = test_cases, .priv = &st, }; - st.dir = file->dir; + st.file = file->file; if (test_suite.desc == NULL || !perf_test__matches(test_suite.desc, curr, argc, argv)) continue; - st.file = file->file; pr_info("%3d: %-*s:", i, width, test_suite.desc); if (intlist__find(skiplist, i)) { @@ -455,7 +452,7 @@ static int perf_test__list_shell(int argc, const char **argv, int i) files = list_script_files(); if (!files) return 0; - for (file = files; file->dir; file++) { + for (file = files; file->file; file++) { int curr = i++; struct test_suite t = { .desc = file->desc diff --git a/tools/perf/tests/tests-scripts.c b/tools/perf/tests/tests-scripts.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 = 0; static struct script_file *files = NULL; static int files_max_width = 0; -static const char *shell_tests__dir(char *path, size_t size) +static int shell_tests__dir_fd(void) { - const char *devel_dirs[] = { "./tools/perf/tests", "./tests", }; - char *exec_path; - unsigned int i; + char path[PATH_MAX], *exec_path; + static const char * const devel_dirs[] = { "./tools/perf/tests/shell", "./tests/shell", }; - for (i = 0; i < ARRAY_SIZE(devel_dirs); ++i) { - struct stat st; + for (size_t i = 0; i < ARRAY_SIZE(devel_dirs); ++i) { + int fd = open(devel_dirs[i], O_PATH); - if (!lstat(devel_dirs[i], &st)) { - scnprintf(path, size, "%s/shell", devel_dirs[i]); - if (!lstat(devel_dirs[i], &st)) - return path; - } + if (fd >= 0) + return fd; } /* Then installed path. */ exec_path = 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); } -static const char *shell_test__description(char *description, size_t size, - const char *path, const char *name) +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 = 0; - path__join(filename, sizeof(filename), path, name); - fp = fopen(filename, "r"); - if (!fp) + io__init(&io, openat(dir_fd, name, O_RDONLY), buf, sizeof(buf)); + if (io.fd < 0) return NULL; /* Skip first line - should be #!/bin/sh Shebang */ + if (io__get_char(&io) != '#') + goto err_out; + if (io__get_char(&io) != '!') + goto err_out; do { - ch = fgetc(fp); - } while (ch != EOF && ch != '\n'); - - description = fgets(description, size, fp); - fclose(fp); + ch = io__get_char(&io); + if (ch < 0) + goto err_out; + } while (ch != '\n'); - /* Assume first char on line is omment everything after that desc */ - return description ? strim(description + 1) : NULL; + do { + ch = io__get_char(&io); + if (ch < 0) + goto err_out; + } while (ch == '#' || isspace(ch)); + while (ch > 0 && ch != '\n') { + desc[pos++] = ch; + if (pos >= (int)sizeof(desc) - 1) + break; + ch = io__get_char(&io); + } + while (pos > 0 && isspace(desc[--pos])) + ; + desc[++pos] = '\0'; + close(io.fd); + return strdup(desc); +err_out: + close(io.fd); + return NULL; } /* 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; @@ -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) == 0) /* Is executable */ + if (faccessat(dir_fd, path, R_OK | X_OK, 0) == 0) /* Is executable */ return true; } return false; } /* 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); } /* 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; } -static void append_script(const char *dir, const char *file, const char *desc) +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; + snprintf(link, sizeof(link), "/proc/%d/fd/%d", getpid(), dir_fd); + len = readlink(link, filename, sizeof(filename)); + if (len < 0) { + pr_err("Failed to readlink %s", link); + return; + } + filename[len++] = '/'; + strcpy(&filename[len], name); files_num_tmp = files_num + 1; if (files_num_tmp >= 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 = files_tmp; files_num = files_num_tmp; - files[files_num - 1].dir = strdup_check(dir); - files[files_num - 1].file = strdup_check(file); - files[files_num - 1].desc = strdup_check(desc); - files[files_num].dir = NULL; + files[files_num - 1].file = strdup_check(filename); + files[files_num - 1].desc = desc; files[files_num].file = NULL; files[files_num].desc = NULL; @@ -154,32 +172,39 @@ static void append_script(const char *dir, const char *file, const char *desc) files_max_width = width; } -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]; /* List files, sorted by alpha */ - n_dirs = scandir(path, &entlist, NULL, alphasort); + n_dirs = scandirat(dir_fd, ".", &entlist, NULL, alphasort); if (n_dirs == -1) return; for (i = 0; i < n_dirs && (ent = entlist[i]); i++) { + int fd; + if (ent->d_name[0] == '.') continue; /* Skip hidden files */ - if (is_test_script(path, ent->d_name)) { /* It's a test */ - char bf[256]; - const char *desc = 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 = shell_test__description(dir_fd, ent->d_name); 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 != DT_DIR) { + struct stat st; + + if (ent->d_type != DT_UNKNOWN) + continue; + fstatat(dir_fd, ent->d_name, &st, 0); + if (!S_ISDIR(st.st_mode)) + continue; } + fd = openat(dir_fd, ent->d_name, O_PATH); + append_scripts_in_dir(fd); } for (i = 0; i < n_dirs; i++) /* Clean up */ zfree(&entlist[i]); @@ -188,14 +213,17 @@ static void append_scripts_in_dir(const char *path) const struct script_file *list_script_files(void) { - char path_dir[PATH_MAX]; - const char *path; + int dir_fd; if (files) return files; /* Singleton - we already know our list */ - path = shell_tests__dir(path_dir, sizeof(path_dir)); /* Walk dir */ - append_scripts_in_dir(path); + dir_fd = shell_tests__dir_fd(); /* Walk dir */ + if (dir_fd < 0) + return NULL; + + append_scripts_in_dir(dir_fd); + close(dir_fd); return files; } diff --git a/tools/perf/tests/tests-scripts.h b/tools/perf/tests/tests-scripts.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 struct script_file { - char *dir; char *file; char *desc; }; From patchwork Wed Feb 14 04:53:55 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ian Rogers X-Patchwork-Id: 200804 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:7300:bc8a:b0:106:860b:bbdd with SMTP id dn10csp991016dyb; Tue, 13 Feb 2024 20:56:21 -0800 (PST) X-Forwarded-Encrypted: i=3; AJvYcCUKilE9SVBhgn+PujfivyyvpXutyui0nXTBjmDakDrfP3dt2e09IcFFuRL22rEcnfLtKCvCIhfrZEPlfJuIfyLSIHDn+Q== X-Google-Smtp-Source: AGHT+IGYZHfznAkbv/quTFclnmkAG1yVlka1ZQaru5Iv1P82InRvKZ6EzhGI185KmE8slvVJNNbA X-Received: by 2002:a05:622a:1012:b0:42d:a6f3:9e78 with SMTP id d18-20020a05622a101200b0042da6f39e78mr1703632qte.68.1707886581304; Tue, 13 Feb 2024 20:56:21 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1707886581; cv=pass; d=google.com; s=arc-20160816; b=rbDd5pJZTD76Or1Oxe2vlc2doN1e8ce4jJC5lP8tYFTJRlRY4b8pNkJ4q+KkHLVhqo ufEjUzTUw3Sc0hLVKifPhI8qaB4c8pt6GMGxSdf3piP5SI1uEii5eQL1o63qYz/AqTle 7lWhwz3fEqYirUj72WE/U59k7g3O7snzWP7qz6SebD7jhZvq2z30hJcm6S/EuP1Evh3t KlkxTPeD4ZH30lG84e8IEi4ptgpuzV4KW3MqiEAPpU6DsoEpF78rQqBvyGWnK3BDrgb9 1KYcutO1t2yFfS3vW2aMnhsPJquoK6J+ch3dy5cgMm0IjMwUBk0Wli+NbfJ+BaoUDcPQ BQzA== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=to:from:subject:references:mime-version:list-unsubscribe :list-subscribe:list-id:precedence:message-id:in-reply-to:date :dkim-signature; bh=pKUjhtJivPO8JCDFJHJoi47s/FDcERs+KA6j2+NQ9Pw=; fh=Os67VkssL9vQTtG234R6gVJ4up0GX/4OfS0Xl5TyRR8=; b=gTItQ4Rvoy717qtg606TduwjQkeU9scx4cOrtZa5joFgTCsm2MaYxNolqybc9R2K79 9loKmc/PaT+CdC2G0iZKJfJ5NoFxvea4xxA2atNa/b8ywAp9+z5Q7UxGu/DXJpL67QKF VbVXUhgpmtYDVo7Y8f6g/9BYnSfFmDBuWPxPg/vEqhcV50BYvaowbFflh6PoaOoT1Cqt uYsFDQ/KKw5ZskvcjsE6TFglzURf2ukorm9cvL8Io/Y2uctSRtoK+Y6rxkePloitzspF rhHUgbtiZEtDFt5OBT3k9P8zU2BdRHOyCt0OBk3yaHQRfFEnXyhTirUzHrbnnvF5w6LV +AgQ==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=oItx5nx3; arc=pass (i=1 spf=pass spfdomain=flex--irogers.bounces.google.com dkim=pass dkdomain=google.com dmarc=pass fromdomain=google.com); spf=pass (google.com: domain of linux-kernel+bounces-64743-ouuuleilei=gmail.com@vger.kernel.org designates 2604:1380:45d1:ec00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-64743-ouuuleilei=gmail.com@vger.kernel.org"; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com X-Forwarded-Encrypted: i=2; AJvYcCXQIlavCs4X3dGU1b9jcNx+i/AXTrf2q0CPTXdL4FjOAaMQzvIAiXPBSdM+PbwIwZixGlsxWFaxnF5TTe6DMmBTsLj9jg== Received: from ny.mirrors.kernel.org (ny.mirrors.kernel.org. [2604:1380:45d1:ec00::1]) by mx.google.com with ESMTPS id vw20-20020a05620a565400b00785920ee1ecsi4407502qkn.197.2024.02.13.20.56.21 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 13 Feb 2024 20:56:21 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-64743-ouuuleilei=gmail.com@vger.kernel.org designates 2604:1380:45d1:ec00::1 as permitted sender) client-ip=2604:1380:45d1:ec00::1; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=oItx5nx3; arc=pass (i=1 spf=pass spfdomain=flex--irogers.bounces.google.com dkim=pass dkdomain=google.com dmarc=pass fromdomain=google.com); spf=pass (google.com: domain of linux-kernel+bounces-64743-ouuuleilei=gmail.com@vger.kernel.org designates 2604:1380:45d1:ec00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-64743-ouuuleilei=gmail.com@vger.kernel.org"; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ny.mirrors.kernel.org (Postfix) with ESMTPS id 0F5A91C20F8F for ; Wed, 14 Feb 2024 04:56:21 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id E726D14297; Wed, 14 Feb 2024 04:54:55 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="oItx5nx3" 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 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-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 X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1790848880209046483 X-GMAIL-MSGID: 1790848880209046483 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 | 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-test.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[] = { static struct test_suite **tests[] = { generic_tests, arch_tests, + NULL, /* shell tests created at runtime. */ }; static struct test_workload *workloads[] = { @@ -299,74 +300,12 @@ static int test_and_print(struct test_suite *t, int subtest) return err; } -struct shell_test { - const char *file; -}; - -static int shell_test__run(struct test_suite *test, int subdir __maybe_unused) -{ - int err; - struct shell_test *st = test->priv; - char *cmd; - - asprintf(&cmd, "%s%s", st->file, verbose ? " -v" : ""); - if (!cmd) - return TEST_FAIL; - err = system(cmd); - free(cmd); - if (!err) - return TEST_OK; - - return WEXITSTATUS(err) == 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 = list_script_files(); - if (!files) - return 0; - for (file = files; file->file; file++) { - int curr = i++; - struct test_case test_cases[] = { - { - .desc = file->desc, - .run_case = shell_test__run, - }, - { .name = NULL, } - }; - struct test_suite test_suite = { - .desc = test_cases[0].desc, - .test_cases = test_cases, - .priv = &st, - }; - st.file = file->file; - - if (test_suite.desc == 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 *skiplist) { struct test_suite *t; unsigned int j, k; int i = 0; - int width = list_script_max_width(); + int width = 0; for_each_test(j, k, t) { int len = strlen(test_description(t, -1)); @@ -441,28 +380,6 @@ static int __cmd_test(int argc, const char *argv[], struct 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 = list_script_files(); - if (!files) - return 0; - for (file = files; file->file; file++) { - int curr = i++; - struct test_suite t = { - .desc = file->desc - }; - - if (!perf_test__matches(t.desc, curr, argc, argv)) - continue; - - pr_info("%3d: %s\n", i, t.desc); - } return 0; } @@ -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; } @@ -551,6 +465,7 @@ int cmd_test(int argc, const char **argv) /* Unbuffered output */ setvbuf(stdout, NULL, _IONBF, 0); + tests[2] = create_script_test_suites(); argc = parse_options_subcommand(argc, argv, test_options, test_subcommands, test_usage, 0); if (argc >= 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-scripts.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" - -/* - * 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 = 0; -static struct script_file *files = NULL; -static int files_max_width = 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; } -static void append_script(int dir_fd, const char *name, char *desc) +static int shell_test__run(struct test_suite *test, int subtest __maybe_unused) +{ + const char *file = test->priv; + int err; + char *cmd; + + asprintf(&cmd, "%s%s", file, verbose ? " -v" : ""); + if (!cmd) + return TEST_FAIL; + err = system(cmd); + free(cmd); + if (!err) + return TEST_OK; + + return WEXITSTATUS(err) == 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; snprintf(link, sizeof(link), "/proc/%d/fd/%d", getpid(), dir_fd); len = readlink(link, filename, sizeof(filename)); @@ -146,33 +155,43 @@ static void append_script(int dir_fd, const char *name, char *desc) } filename[len++] = '/'; strcpy(&filename[len], name); - files_num_tmp = files_num + 1; - if (files_num_tmp >= SIZE_MAX) { - pr_err("Too many script files\n"); - abort(); + + tests = calloc(2, sizeof(*tests)); + if (!tests) { + pr_err("Out of memory while building script test suite list\n"); + return; } + tests[0].name = strdup_check(name); + tests[0].desc = strdup_check(desc); + tests[0].run_case = shell_test__run; + + test_suite = 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 = desc; + test_suite->test_cases = tests; + test_suite->priv = strdup_check(filename); /* Realloc is good enough, though we could realloc by chunks, not that * anyone will ever measure performance here */ - files_tmp = realloc(files, - (files_num_tmp + 1) * sizeof(struct script_file)); - if (files_tmp == NULL) { - pr_err("Out of memory while building test list\n"); - abort(); + result_tmp = realloc(*result, (*result_sz + 1) * sizeof(*result_tmp)); + if (result_tmp == 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 = files_tmp; - files_num = files_num_tmp; - files[files_num - 1].file = strdup_check(filename); - files[files_num - 1].desc = desc; - files[files_num].file = NULL; - files[files_num].desc = NULL; - - width = strlen(desc); /* Track max width of desc */ - if (width > files_max_width) - files_max_width = width; + *result = result_tmp; + (*result)[*result_sz] = test_suite; + (*result_sz)++; } -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 = shell_test__description(dir_fd, ent->d_name); 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 != DT_DIR) { @@ -204,32 +223,35 @@ static void append_scripts_in_dir(int dir_fd) continue; } fd = openat(dir_fd, ent->d_name, O_PATH); - append_scripts_in_dir(fd); + append_scripts_in_dir(fd, result, result_sz); } for (i = 0; i < n_dirs; i++) /* Clean up */ zfree(&entlist[i]); free(entlist); } -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 = shell_tests__dir_fd(); /* Walk dir */ - if (dir_fd < 0) - return NULL; + struct test_suite **result = NULL, **result_tmp; + size_t result_sz = 0; + int dir_fd = shell_tests__dir_fd(); /* Walk dir */ - 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 >= 0) + append_scripts_in_dir(dir_fd, &result, &result_sz); - return files; -} - -int list_script_max_width(void) -{ - list_script_files(); /* Ensure we have scanned all scripts */ - return files_max_width; + result_tmp = realloc(result, (result_sz + 1) * sizeof(*result_tmp)); + if (result_tmp == NULL) { + pr_err("Out of memory while building script test suite list\n"); + abort(); + } + /* NULL terminate the test suite array. */ + result = result_tmp; + result[result_sz] = NULL; + if (dir_fd >= 0) + close(dir_fd); + return result; } diff --git a/tools/perf/tests/tests-scripts.h b/tools/perf/tests/tests-scripts.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 -struct script_file { - char *file; - char *desc; -}; +#include "tests.h" -/* 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); #endif /* TESTS_SCRIPTS_H */ From patchwork Wed Feb 14 04:53:56 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ian Rogers X-Patchwork-Id: 200807 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:7300:bc8a:b0:106:860b:bbdd with SMTP id dn10csp991941dyb; Tue, 13 Feb 2024 20:58:48 -0800 (PST) X-Forwarded-Encrypted: i=3; AJvYcCUTfKHhUvxYtFhV1pAi3u2HVs2vZhul9ciD+m6RvKjO1c6CrA+zuVQ4kodF/xUYgw21c7QKddgXiz8QDsfafJwPBWSRtw== X-Google-Smtp-Source: AGHT+IEe1Ezg7JkpTjrTo81VY4Ar7dyzh2N7phfuZeSxCmZPkuDm5SN5cMlv8nXPfNmIVUVyCwHy X-Received: by 2002:a05:6a00:4e4f:b0:6e0:6734:a9f8 with SMTP id gu15-20020a056a004e4f00b006e06734a9f8mr1525244pfb.14.1707886728126; Tue, 13 Feb 2024 20:58:48 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1707886728; cv=pass; d=google.com; s=arc-20160816; b=twE/5G6AJ5Ycamb3XEGK5XVo9aZVegXy8J8pj6pDysFB9y9xMV2GoBTu1dCvedRXpi NuRk+PGFX6cg0ws+vIPW7flegRuYertwXFB5Xr9xsNwuKONu3KFhWcKEYg1shEqwaGeV ERbvV3jH3G2UGbmi2UEkt6Jq8y+9hFxCeGsK5BSqHyPKtL8zJ4ka1ZBX7l+ATxDZE9mv 9w5kcWsY83lJuBJQFJWo8h7GaYB6i3scIeQ/wp/UzWI4JO78cFjUDLliBP3hWXnAf1Ee KyUMACy27OG2iqhMNkhNsoPlaiEIU+O3Let0Gx4Jr5YyZC6vNXQ2pupNNwnwxHs5wWcd Rj+A== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=to:from:subject:references:mime-version:list-unsubscribe :list-subscribe:list-id:precedence:message-id:in-reply-to:date :dkim-signature; bh=Uq1mVmEDpbr91BLlFdUce3FbPa9YnIbaer6ngXT0cLo=; fh=4Q4wbvo14SSxugyH9ep7kgVvs2CUTx4JzGWCwti8h8I=; b=TseMU+ODy+L71DRm1qQTLKmhoghQFQxYHqEvsUnYmL4SPvKs4qIgb+TNG2tUoHf64l M0xgYMrtyyhMllQEJtfEbGEqtY3yimTrMjf90jL4iH/DJ/4KA89sPooG3JuC7ivhNrCO AtRk+776DThN5SCM54+F1SxfK8wpj/K6xgt+2Oyj+jPL5vETmuGgq3V9mpukQl9Df0ZH OgJTUUl21VwZMZiaVkucbPrgPxiU05/ZuzION/LVBIRlYHdC3VoEd57pdKOK9QJeXLms qv9MojkdgvBCD3kFN0cDN7PmKBBXz/8bUU8pQQSrsfjL0zBfwBrBfHWk6lu5S+IPy5Ek nAuA==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=QxYNxEo4; arc=pass (i=1 spf=pass spfdomain=flex--irogers.bounces.google.com dkim=pass dkdomain=google.com dmarc=pass fromdomain=google.com); spf=pass (google.com: domain of linux-kernel+bounces-64744-ouuuleilei=gmail.com@vger.kernel.org designates 147.75.48.161 as permitted sender) smtp.mailfrom="linux-kernel+bounces-64744-ouuuleilei=gmail.com@vger.kernel.org"; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com X-Forwarded-Encrypted: i=2; AJvYcCV/dAd06eZllWIZL2v+WU9SLJ68HY0W4qpU2EdGBYMtdSXwm55AYImjp0GIVXU92/GwYgH0iX4gLADpECYdatuTaUdGSQ== Received: from sy.mirrors.kernel.org (sy.mirrors.kernel.org. [147.75.48.161]) by mx.google.com with ESMTPS id a16-20020a056a000c9000b006e0f491e027si2922660pfv.106.2024.02.13.20.58.47 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 13 Feb 2024 20:58:48 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-64744-ouuuleilei=gmail.com@vger.kernel.org designates 147.75.48.161 as permitted sender) client-ip=147.75.48.161; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=QxYNxEo4; arc=pass (i=1 spf=pass spfdomain=flex--irogers.bounces.google.com dkim=pass dkdomain=google.com dmarc=pass fromdomain=google.com); spf=pass (google.com: domain of linux-kernel+bounces-64744-ouuuleilei=gmail.com@vger.kernel.org designates 147.75.48.161 as permitted sender) smtp.mailfrom="linux-kernel+bounces-64744-ouuuleilei=gmail.com@vger.kernel.org"; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by sy.mirrors.kernel.org (Postfix) with ESMTPS id 196B6B27509 for ; Wed, 14 Feb 2024 04:56:31 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 0559616436; Wed, 14 Feb 2024 04:54:59 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="QxYNxEo4" 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 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-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 X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1790849033718959811 X-GMAIL-MSGID: 1790849033718959811 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 --- 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-test.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 @@ #include "tests-scripts.h" +/* + * 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 = "objdump"; @@ -209,76 +218,36 @@ static bool perf_test__matches(const char *desc, int curr, int argc, const char return false; } -static int run_test(struct test_suite *test, int subtest) -{ - int status, err = -1, child = 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 <= 0) { - int nullfd = open("/dev/null", O_WRONLY); - - if (nullfd >= 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 = 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; +}; - if (WIFEXITED(status)) { - err = (signed char)WEXITSTATUS(status); - pr_debug("test child finished with %d\n", err); - } else if (WIFSIGNALED(status)) { - err = -1; - pr_debug("test child interrupted\n"); - } - } +static int run_test_child(struct child_process *process) +{ + struct child_test *child = container_of(process, struct child_test, process); + int err; - return err; + pr_debug("--- start ---\n"); + pr_debug("test child forked, pid %d\n", getpid()); + err = test_function(child->test, child->subtest)(child->test, child->subtest); + pr_debug("---- end(%d) ----\n", err); + fflush(NULL); + return -err; } -#define for_each_test(j, k, t) \ - for (j = 0, k = 0; j < ARRAY_SIZE(tests); j++, k = 0) \ - while ((t = tests[j][k++]) != 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 = width > 2 ? width - 2 : width; - pr_debug("\n--- start ---\n"); - err = 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)); - 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; } - return err; + return 0; +} + +static int finish_test(struct child_test *child_test, int width) +{ + struct test_suite *t = child_test->test; + int i = child_test->test_num; + int subi = child_test->subtest; + int out = child_test->process.out; + int err = child_test->process.err; + bool out_done = out <= 0; + bool err_done = err <= 0; + struct strbuf out_output = STRBUF_INIT; + struct strbuf err_output = STRBUF_INIT; + int ret; + + /* + * For test suites with subtests, display the suite name ahead of the + * sub test names. + */ + if (has_subtests(t) && subi == 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] = { + { .fd = out, + .events = POLLIN | POLLERR | POLLHUP | POLLNVAL, + }, + { .fd = err, + .events = 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=*/1000); + if (!out_done && pfds[0].revents) { + errno = 0; + len = read(out, buf, sizeof(buf) - 1); + + if (len <= 0) { + out_done = errno != EAGAIN; + } else { + buf[len] = '\0'; + if (verbose > 1) + fprintf(stdout, "%s", buf); + else + strbuf_addstr(&out_output, buf); + } + } + if (!err_done && pfds[1].revents) { + errno = 0; + len = read(err, buf, sizeof(buf) - 1); + + if (len <= 0) { + err_done = errno != EAGAIN; + } else { + buf[len] = '\0'; + if (verbose > 1) + fprintf(stdout, "%s", buf); + else + strbuf_addstr(&err_output, buf); + } + } + } + /* Clean up child process. */ + ret = finish_command(&child_test->process); + if (verbose == 1 && ret == 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 child_test **child, + int width) +{ + int err; + + *child = NULL; + if (dont_fork) { + pr_debug("--- start ---\n"); + err = test_function(test, subi)(test, subi); + pr_debug("---- end ----\n"); + print_test_result(test, i, subi, err, width); + return 0; + } + + *child = zalloc(sizeof(**child)); + if (!*child) + return -ENOMEM; + + (*child)->test = test; + (*child)->test_num = i; + (*child)->subtest = subi; + (*child)->process.pid = -1; + (*child)->process.no_stdin = 1; + if (verbose <= 0) { + (*child)->process.no_stdout = 1; + (*child)->process.no_stderr = 1; + } else { + (*child)->process.out = -1; + (*child)->process.err = -1; + } + (*child)->process.no_exec_cmd = run_test_child; + err = start_command(&(*child)->process); + if (err || parallel) + return err; + return finish_test(*child, width); } +#define for_each_test(j, k, t) \ + for (j = 0, k = 0; j < ARRAY_SIZE(tests); j++, k = 0) \ + while ((t = tests[j][k++]) != NULL) + static int __cmd_test(int argc, const char *argv[], struct intlist *skiplist) { struct test_suite *t; unsigned int j, k; int i = 0; int width = 0; + size_t num_tests = 0; + struct child_test **child_tests; + int child_test_num = 0; for_each_test(j, k, t) { int len = strlen(test_description(t, -1)); if (width < len) width = len; + + if (has_subtests(t)) { + for (int subi = 0, subn = num_subtests(t); subi < subn; subi++) { + len = strlen(test_description(t, subi)); + if (width < len) + width = len; + num_tests++; + } + } else { + num_tests++; + } } + child_tests = calloc(num_tests, sizeof(*child_tests)); + if (!child_tests) + return -ENOMEM; for_each_test(j, k, t) { int curr = i++; - int subi; if (!perf_test__matches(test_description(t, -1), curr, argc, argv)) { bool skip = true; - int subn; - - subn = num_subtests(t); - for (subi = 0; subi < subn; subi++) { + for (int subi = 0, subn = num_subtests(t); subi < subn; subi++) { if (perf_test__matches(test_description(t, subi), curr, argc, argv)) skip = false; @@ -334,52 +455,45 @@ static int __cmd_test(int argc, const char *argv[], struct intlist *skiplist) continue; } - 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; } if (!has_subtests(t)) { - test_and_print(t, -1); - } else { - int subn = 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 = width > 2 ? width - 2 : width; - - if (subn <= 0) { - color_fprintf(stderr, PERF_COLOR_YELLOW, - " Skip (not compiled in)\n"); - continue; - } - pr_info("\n"); + int err = start_test(t, curr, -1, &child_tests[child_test_num++], width); - for (subi = 0; subi < subn; subi++) { - int len = strlen(test_description(t, subi)); - - if (subw < len) - subw = len; + if (err) { + /* TODO: if parallel waitpid the already forked children. */ + free(child_tests); + return err; } + } else { + for (int subi = 0, subn = num_subtests(t); subi < subn; subi++) { + int err; - for (subi = 0; subi < subn; subi++) { if (!perf_test__matches(test_description(t, subi), curr, argc, argv)) continue; - pr_info("%3d.%1d: %-*s:", i, subi + 1, subw, - test_description(t, subi)); - test_and_print(t, subi); + err = start_test(t, curr, subi, &child_tests[child_test_num++], + width); + if (err) + return err; } } } + for (i = 0; i < child_test_num; i++) { + if (parallel) { + int ret = finish_test(child_tests[i], width); + + if (ret) + return ret; + } + free(child_tests[i]); + } + free(child_tests); return 0; } @@ -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 testing"), OPT_STRING(0, "dso", &dso_to_test, "dso", "dso to test"), OPT_STRING(0, "objdump", &test_objdump_path, "path",