From patchwork Wed Feb 21 03:41:48 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ian Rogers X-Patchwork-Id: 203885 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:693c:2685:b0:108:e6aa:91d0 with SMTP id mn5csp813078dyc; Tue, 20 Feb 2024 19:43:00 -0800 (PST) X-Forwarded-Encrypted: i=3; AJvYcCWOuLcNj41gzKQiH7AToYcnp7IwMscqRrPBElByf8Hpiufg72EXiI/eD3hIYJ3CpL2sO4QOs5bEPxG+5JjIQQEcqBL0YQ== X-Google-Smtp-Source: AGHT+IFQJuxo9i5lC3BU2GQZ0yZe68rzHHlVsvbEgGbBIW8yHG1QDvnrNqDUMoEVOg9iMNC5f7pK X-Received: by 2002:a05:6a00:9393:b0:6e4:71c1:cc4c with SMTP id ka19-20020a056a00939300b006e471c1cc4cmr7152910pfb.34.1708486980602; Tue, 20 Feb 2024 19:43:00 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1708486980; cv=pass; d=google.com; s=arc-20160816; b=R952o3ha+CIEEwkBPEFv/JOQyabMtUQUBS3LCsxBfhWLAF0t3pzRI2jzVugQRFCVpB CM/08NfFEKTzDJ6OYaxSR1/rrMxnZW4optTttwvgEi+QIsrK92/mmmBPUy0Oo6+zuiry 9XVbGCpDDbQf8EPzzusE3Htu7wMi1ID2VCZt7uHMD2vMFCjWq/QadpOpChQOMKdVJD0K hr1yIH90rLLb/kD3smKBKAsSmXCpvna9bqzewh2+had1YwSuQfmDT3Qz5orpBK1Gk3We mg2WWBhwupFE0W6X9OJuzocCfD9yIeL1L8N2ouYEsVPrcRC6j+Gr4ZXOI8EPuaLcMFO6 qRlA== 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=uv/9NgKdLz0z52mqtvy0vTSbbBgaPJcb0Vk69+L979Q=; fh=0u1l5NUOKyleDkk51CMo2hSU5OQ7hFsid2V+kG+D3ZE=; b=cSWBkJUlLyV2EFuqMWhVESL66mBGWXA5EtDUVMOfTHDOrUJUgoFbmRhwXO7aZU9kmk h5fCklfIrRqrgLc77qw9SsOJ6GPgBROs5WeVt8ma3ItOENqLv1K0yzwn3wxq4o1C8lVz AKufupB9ehJhUy4yanqvzQ2gs4EFHGKyxMosoeh3dD/WCeBdreWc8yI82W9s3+mYx0V+ 7wqLVLLVi6CIlTWIxWgrlGpPs89wS5ya016rhizw7FaK4968I0eAYfA4uv8q2pgbGRA/ 9/a8HeE1AWTdZF9NPTbqeNN2iu7rMFkbJtsSWivOZJQ0GzBSSAQG4sK4mmYE828AQye0 VZWQ==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=bvs0jjKY; 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-74007-ouuuleilei=gmail.com@vger.kernel.org designates 2604:1380:40f1:3f00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-74007-ouuuleilei=gmail.com@vger.kernel.org"; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Received: from sy.mirrors.kernel.org (sy.mirrors.kernel.org. [2604:1380:40f1:3f00::1]) by mx.google.com with ESMTPS id i193-20020a636dca000000b005dc82a6d761si7327937pgc.123.2024.02.20.19.43.00 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 20 Feb 2024 19:43:00 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-74007-ouuuleilei=gmail.com@vger.kernel.org designates 2604:1380:40f1:3f00::1 as permitted sender) client-ip=2604:1380:40f1:3f00::1; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=bvs0jjKY; 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-74007-ouuuleilei=gmail.com@vger.kernel.org designates 2604:1380:40f1:3f00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-74007-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 65440B23839 for ; Wed, 21 Feb 2024 03:42:46 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 4651339AF4; Wed, 21 Feb 2024 03:42:10 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="bvs0jjKY" Received: from mail-yw1-f202.google.com (mail-yw1-f202.google.com [209.85.128.202]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id BB6ED38DE6 for ; Wed, 21 Feb 2024 03:42:05 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.128.202 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1708486927; cv=none; b=eCv+nGSQBmQYOk6Na9xrPmffHZJ4SO9/Qo3b2dcdx3se+bRv7yg7gchcvu4PF3zK8TLG3SlB7ydi5AnPMWIZA7zhCFJ1korA3o7A2hylsjQal9C69/7hVqWHMmtoP+r6ldsvvQ4rnDz6t5pntLybOduqiR4VotioTtuACgULSfM= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1708486927; c=relaxed/simple; bh=o/osGCI+hvlD14sD3OCM5ESdLYEEWx1V7jgoXENiCKc=; h=Date:In-Reply-To:Message-Id:Mime-Version:References:Subject:From: To:Content-Type; b=bZ9mz9mnXPRTFPibPRgfUKv6IlyzWnFf29chPDpm7EF3IOO6BDuZJlC9AeTslrevU2RiDRoNqg1Lv/PMkzjsqMyCNPr22lCVyBV/ilTEckBdRt4weoydtQ7+sT29JTAuf0hMmVSzXkbvVU9hHlPe33ImFXpkM00AOafUb3cHyuI= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--irogers.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=bvs0jjKY; arc=none smtp.client-ip=209.85.128.202 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--irogers.bounces.google.com Received: by mail-yw1-f202.google.com with SMTP id 00721157ae682-607cd6c11d7so89979957b3.0 for ; Tue, 20 Feb 2024 19:42:05 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1708486925; x=1709091725; darn=vger.kernel.org; h=to:from:subject:references:mime-version:message-id:in-reply-to:date :from:to:cc:subject:date:message-id:reply-to; bh=uv/9NgKdLz0z52mqtvy0vTSbbBgaPJcb0Vk69+L979Q=; b=bvs0jjKYG21ZkB+YgfE5BE740UXuO77Aq3aGQVuxlIvR4Api1Wd4UkAzo8tzSYpyxu qgIcKEqtQo9kzYmWGUOYEzQqHm3T419K97jcCG6JD54rA6kt9TW3725qnQ91FWLCSKld DkO2R3m1FoX6F5W9P+Lz18oSTagIl5/cO6lUWg1G7Uw/eXm39MRhHadPE9CjR24Tt3FS w8g6c+vm1zP/XJcMZJ9rTkmPOW5IHsHgB9NA5KU0rMlj3wY9F4rKgehtcnq40to/jKhT GaZmDyaiksQFqofW5FfXSwirlu2KlyFgPt0WQcQmCX1WZ6nZR/aWrE31NPuKo38UqC21 W9Og== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1708486925; x=1709091725; h=to:from:subject:references:mime-version:message-id:in-reply-to:date :x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=uv/9NgKdLz0z52mqtvy0vTSbbBgaPJcb0Vk69+L979Q=; b=FiSE3G/wbQeeZHE2Yyxj96B+pbzEA9YdCXNDZhjv1mXCpy13cdQ+ooVhKlLHC5zIXO SREMRVAR78iWfzQOuPAfflkbbILjJi/MtKvLVo5xHQo+E9CRUmbnx0cpD8JeIiwsuOi7 jIPh8byUHMVhkVxlt7aEPY/uiTYWkphKYDbh/lYQR+jVcoUHu4ja9UaHox6zmpIPUF9a 1RHoujmP1XC0xuU0DBLZs2MHQo+jPfUJQAKUNuDk9agDRWeW3Z/Tz48BWwsbMHunsoki yrxPlxZLGSwzCsNoLTtSSyHnuzsV848hL2AwN4W9pgL3DUh4uB1Cwm0bn18hjsUg51rj Y9AQ== X-Forwarded-Encrypted: i=1; AJvYcCUtQxHnKdxUa5ft6FxY0TE8f9c4FkSX46hJ51HrvpG/g+zKh6acWPeYzBiKCh7sXjdsPEPwvnW+wlpQN+hox5QWocmgjiN7SqDDX5je X-Gm-Message-State: AOJu0YxhNijls3m6aAbKPZQEQVBVT9XXSF9NUyo1+qN5F13VgRpvZkcZ LCwJX1iAZjqBZgmW1gfSif08HEPEsi1PL1Z5eWYuDdIg1P1hgLaEA0RzWUqvUrJ7FXw4kqWshkB BtBnN+Q== X-Received: from irogers.svl.corp.google.com ([2620:15c:2a3:200:3c02:f882:5876:b7ab]) (user=irogers job=sendgmr) by 2002:a25:db93:0:b0:dc7:9218:df3b with SMTP id g141-20020a25db93000000b00dc79218df3bmr708882ybf.10.1708486924814; Tue, 20 Feb 2024 19:42:04 -0800 (PST) Date: Tue, 20 Feb 2024 19:41:48 -0800 In-Reply-To: <20240221034155.1500118-1-irogers@google.com> Message-Id: <20240221034155.1500118-2-irogers@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20240221034155.1500118-1-irogers@google.com> X-Mailer: git-send-email 2.44.0.rc0.258.g7320e95886-goog Subject: [PATCH v6 1/8] perf thread_map: Skip exited threads when scanning /proc From: Ian Rogers To: Peter Zijlstra , Ingo Molnar , Arnaldo Carvalho de Melo , Namhyung Kim , Mark Rutland , Alexander Shishkin , Jiri Olsa , Ian Rogers , Adrian Hunter , Nathan Chancellor , Nick Desaulniers , Bill Wendling , Justin Stitt , Athira Jajeev , James Clark , Kan Liang , Yang Jihong , linux-kernel@vger.kernel.org, linux-perf-users@vger.kernel.org, llvm@lists.linux.dev X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1791478443831616716 X-GMAIL-MSGID: 1791478443831616716 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 21 03:41: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: 203886 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:693c:2685:b0:108:e6aa:91d0 with SMTP id mn5csp813156dyc; Tue, 20 Feb 2024 19:43:18 -0800 (PST) X-Forwarded-Encrypted: i=3; AJvYcCVgRhY1svIJBEq2NCK3BxGjJHSPEzvYr4oYBLIam0GS4YJzOlY+Z2cPc3dV+ALazTpy4PoEV43zADXiDyLjMWsnueLtkw== X-Google-Smtp-Source: AGHT+IGAnZQ2xoJy0UF9zfhI2S2+oqLVFXlKpdnTkqVvzerPTO6mP4wRYy3/4t5Eiwi5OWBGau13 X-Received: by 2002:a05:620a:31a4:b0:787:7844:d50b with SMTP id bi36-20020a05620a31a400b007877844d50bmr5054218qkb.49.1708486998155; Tue, 20 Feb 2024 19:43:18 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1708486998; cv=pass; d=google.com; s=arc-20160816; b=FhS79tRiI3d10z6I5MRrjgoJ5CfYnILxCsDnT4bqKUDgI/ztotCmVmGKQgfjnTBm3m xfaU+cfBGzXyP1MmZ4DME4brcv3mItHTAih28/DPwUvOVS60UHPW1t5IpXPlwWI8fE73 1KthcDRInnpmOnLyjmK4qUmF1pTFmk6SUHVYeervZDTAD6ogwIWygWo3sMF4S78szqJo q27z/G/+FW5IPDRK2d5oWdAAmQmQsK099qV5/aQ1h/C0cPUU5rzFkWxgGycnKvvSKYVI yEp/vmbUBcBopjs2jLAgr1VYUn4Fh+1BYr4QA3P35sHtHq2cesKWuBLmJCE2cDA7fOHq 9D3Q== 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=GN8DooxqnWYMX4Lx9QAzw7baLGn2b+1JMBP0il1xEM0=; fh=0fMRXu6fb19wc1ls5/A6XgmrIZ8VOCjNytU+xlwEpzM=; b=uxSPzM/ZFw61HGD3HnU++QqFdwQeESEYoJahImKo2qq9W44JMuYRS0wTkxBnIG20aJ /K5DHCXqzUUJWYiwMeWeWox4AW3d8jxwydVa7fpaaq1lzovsLCJoqEYfbqRJzbIyfMtY kpM0FngR4faFnwat7Pat+cn7s2V4DODxw2zgcZvJLP8ZgyT/6ScMCIkUJLniY4BFlmhv Pafj3D7oVsbHkJrvitCp1z1rnZ4QNLf7ZqrUKXbexTyJIXgA0APq3FrVEDuqyKkRfC2D 7XFeyCV9Aft2tx7rQcSe5KpaS+wnxtRGKBveTR8UA6trB6jHWZjQ6YA6LRkrz/Swm3tt xmIw==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=F2QQAfnN; 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-74009-ouuuleilei=gmail.com@vger.kernel.org designates 2604:1380:45d1:ec00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-74009-ouuuleilei=gmail.com@vger.kernel.org"; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Received: from ny.mirrors.kernel.org (ny.mirrors.kernel.org. [2604:1380:45d1:ec00::1]) by mx.google.com with ESMTPS id de16-20020a05620a371000b0078720177d08si11381362qkb.86.2024.02.20.19.43.18 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 20 Feb 2024 19:43:18 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-74009-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=F2QQAfnN; 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-74009-ouuuleilei=gmail.com@vger.kernel.org designates 2604:1380:45d1:ec00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-74009-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 EA8731C21E81 for ; Wed, 21 Feb 2024 03:43:17 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 8F7BD3A1C2; Wed, 21 Feb 2024 03:42:13 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="F2QQAfnN" Received: from mail-yw1-f201.google.com (mail-yw1-f201.google.com [209.85.128.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 2714438FAF for ; Wed, 21 Feb 2024 03:42:07 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.128.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1708486929; cv=none; b=pS4pvSBVoFzDsUPC4jPcLszMg9oAM56pZoyZBcnRlPAzb9ZItRvpNz+9AyF/5XFPmJV3m8TQpDos0EM/R4UFDqECSbOC5iT8x/0GolT0eEKF1AwRnb/zWSl+6Gyh9YxikvCIvVUb1uiJBIJiC7+KyYct09GoLRUrTCs1tKN1vY4= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1708486929; c=relaxed/simple; bh=3dph1oOgPNiMARTIk1TUv0cnoXVZ3w2YcrnwEMmaGXE=; h=Date:In-Reply-To:Message-Id:Mime-Version:References:Subject:From: To:Content-Type; b=BHoQL74pAz8x207E14WUgn1vr6ByYCCaRImYUR1ce4NiUD97eO/25CWdx778zOFKe5m4LpJOAdJj0Wd+XEHXLRSTLVNlPDkCt212/ApytOrG3i2/CyK0tI5MxujB0DgOuLxC5GCQyb2pFTRPqlbE+T6rornkCB+KrlQUCobe0WQ= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--irogers.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=F2QQAfnN; arc=none smtp.client-ip=209.85.128.201 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--irogers.bounces.google.com Received: by mail-yw1-f201.google.com with SMTP id 00721157ae682-60802b0afd2so42498567b3.1 for ; Tue, 20 Feb 2024 19:42:07 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1708486927; x=1709091727; darn=vger.kernel.org; h=to:from:subject:references:mime-version:message-id:in-reply-to:date :from:to:cc:subject:date:message-id:reply-to; bh=GN8DooxqnWYMX4Lx9QAzw7baLGn2b+1JMBP0il1xEM0=; b=F2QQAfnNm3m+i8aHhWZVTGpzA2iBwyCjcb5CFKPzWgFZkzC4a3ty6VDa69vefd2k7Y Jq7P9oPJ2I3F0bRiMt6J330w6k1AifoFp5VS3fKE/diZOEip0lcp9Bh8GgRJFDwpDHnx ySWs600npH2jgmotkLzox7uWtUGwiFFtyBUkAhR0TwyJpWwZbu3Uw958rgOpxsJDD1O5 m+T7IE+btMN0xdlkuJnjcIBngdHU6AFgagmEhfDtZDPauBCx1EJEGDLi51pqhyJrplFv hgzDFSpYFoYndNGW33yrEVDIHP1JYd9eMTkgIML2DQy3gwx2FWfKVSVbcQokYH8NLvKM 15Eg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1708486927; x=1709091727; h=to:from:subject:references:mime-version:message-id:in-reply-to:date :x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=GN8DooxqnWYMX4Lx9QAzw7baLGn2b+1JMBP0il1xEM0=; b=omPr6rhUtX1iJ3vpOrIqZVhC/peBvTplZwyhfLVvBTXqpTFzDTUcRf55gqafmuX+Oe ZinGTFhfWyxF/RPLLMKwz9lSfKD+z/30PmhqTk4trGmSR9SvfJagi9U0Yhz8IdBTFPbz vj0ND4PXjIUFeQuBEklVY5D3qQijg9akHBY4OqnNhwvFncT0j4aGzGmVweb7+Oa0G84Y JlI4B5qW/U2GuIWB08eD5hT0IN86g6VfsMhklwT1ztYOgQNqWNVMIfWb1Ek+QIhaR4GV 5xzWvHNwoX/hn0v1d1PM4C/893/HmaMDGd9ibnf+rcoc1mhoPRUyHLULDv1RqDqU3VBE p3Fw== X-Forwarded-Encrypted: i=1; AJvYcCXQirE8AdTPktxsNuTtin/Ik1GHCig8Ew8+Md4LNpSObIP5L13EHKhjQp1z+33loExEFQWlniu60W2OW8h86KZwC2xZJHrX9LSQM+ii X-Gm-Message-State: AOJu0YzUV/vr7YQaqtxsKUuhIlmIQHuZ3+4oRoaC8yC4/IfrA0juYDEv vosmPQ1jH+5NXHlNcIi0rVAvkxevnurCGTMpQDvmvwVsa5HulUtVW34soYn9PDQWRtJA1kS4auI ye/6etw== X-Received: from irogers.svl.corp.google.com ([2620:15c:2a3:200:3c02:f882:5876:b7ab]) (user=irogers job=sendgmr) by 2002:a05:690c:338e:b0:608:28cf:3592 with SMTP id fl14-20020a05690c338e00b0060828cf3592mr2065532ywb.1.1708486927165; Tue, 20 Feb 2024 19:42:07 -0800 (PST) Date: Tue, 20 Feb 2024 19:41:49 -0800 In-Reply-To: <20240221034155.1500118-1-irogers@google.com> Message-Id: <20240221034155.1500118-3-irogers@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20240221034155.1500118-1-irogers@google.com> X-Mailer: git-send-email 2.44.0.rc0.258.g7320e95886-goog Subject: [PATCH v6 2/8] perf list: Add scandirat compatibility function From: Ian Rogers To: Peter Zijlstra , Ingo Molnar , Arnaldo Carvalho de Melo , Namhyung Kim , Mark Rutland , Alexander Shishkin , Jiri Olsa , Ian Rogers , Adrian Hunter , Nathan Chancellor , Nick Desaulniers , Bill Wendling , Justin Stitt , Athira Jajeev , James Clark , Kan Liang , Yang Jihong , linux-kernel@vger.kernel.org, linux-perf-users@vger.kernel.org, llvm@lists.linux.dev X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1791478462550133565 X-GMAIL-MSGID: 1791478462550133565 scandirat is used during the printing of tracepoint events but may be missing from certain libcs. Add a compatibility implementation that uses the symlink of an fd in /proc as a path for the reliably present scandir. Signed-off-by: Ian Rogers --- tools/perf/util/print-events.c | 13 ++++--------- tools/perf/util/util.c | 19 +++++++++++++++++++ tools/perf/util/util.h | 8 ++++++++ 3 files changed, 31 insertions(+), 9 deletions(-) diff --git a/tools/perf/util/print-events.c b/tools/perf/util/print-events.c index 9e47712507cc..0dc70b87d609 100644 --- a/tools/perf/util/print-events.c +++ b/tools/perf/util/print-events.c @@ -28,6 +28,7 @@ #include "tracepoint.h" #include "pfm.h" #include "thread_map.h" +#include "util.h" #define MAX_NAME_LEN 100 @@ -63,6 +64,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 +73,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 +130,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 21 03:41: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: 203887 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:693c:2685:b0:108:e6aa:91d0 with SMTP id mn5csp813218dyc; Tue, 20 Feb 2024 19:43:34 -0800 (PST) X-Forwarded-Encrypted: i=3; AJvYcCX3aTu7nYM8+3isVc8oDouTPXs7dxhbc5KjDPSLMRtCCRVnWYESo71bR9WS5G7e7coXo7qE/HKTrfvHkkpzZtTPJ7JZgg== X-Google-Smtp-Source: AGHT+IEZnTz4NYyv1FX749hIFZeNKse2CnZkGjiElAkSScNFSntw/M38d5rMWqxGo8azdCVqVLGu X-Received: by 2002:a17:902:a3cf:b0:1dc:2f2e:139f with SMTP id q15-20020a170902a3cf00b001dc2f2e139fmr546478plb.39.1708487014178; Tue, 20 Feb 2024 19:43:34 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1708487014; cv=pass; d=google.com; s=arc-20160816; b=sN+Qbo3/jDt0RCyhGVTXe3wD1iNHXonHXRShKLrfPCjSP51c1EPLRqLjljhTrK4K0i 9dgdYaXvOoJ0wRR+WvvYjLy1idgxRPhxKRaJ2SWsczr0HbWu3v6F2MFOyb6khlSj68+i g3fzJTWgONdREZ7A7jLkqRk5VKOl4Hsrq6Mn15v8Qk12YaNp9xD7T2IPqMQX5NDzvMlo d9BDuDShPotLrDzzwG1Crm/JyqdhXPoFmB2ZPD7875bnmJZavrHGTwQmeV6lVYhrgMEc 5zBsAQy24Z3lYS6jp7txVtEreaW2o74Y//VZu+vg+vsNjmCGgdkeXjI3AbH0FGfFiTgY vDuQ== 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=OwHudnCNl5wfpV2IMol1tp79ctLuVP/i5n4Lz8j3mXY=; fh=PuuaUpTTaYkreygZPyIWKH4b3UUAmD/njXc0MwgnD6o=; b=jZFcIiUi71qYvEA65xjGYyyPAkkwwUDlltyRXAukmHIRY9NY05Y5klVzyxUbQQh1dK zSnmU9rhH/GMHdZ7tLXiIGEU9QA3kroPZEmM2OSwv/TcvUkMAVyvFws2IyG+vvrP9Qud 19cktpHnZ8xjuVWUBAoSGBGo20ucqRmuP/gSlfzE5Dhsxa6WjofM8xgOey8PRAm2kdKJ DAD24jk0mXVMXaAYtO07EUbwGVXTaOZXjTZVwrErazSCfYdCgpKWNGdEi2zC9WlvLkhx NgENHP2bsrooDl2eqTr5kP7YcSJ3SrSHEDVP+aAbMGs8f6oZhAYdv+YZgLa8UK6ZBZf+ swpg==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b="cz5q/b+E"; 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-74010-ouuuleilei=gmail.com@vger.kernel.org designates 2604:1380:45e3:2400::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-74010-ouuuleilei=gmail.com@vger.kernel.org"; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Received: from sv.mirrors.kernel.org (sv.mirrors.kernel.org. [2604:1380:45e3:2400::1]) by mx.google.com with ESMTPS id jb18-20020a170903259200b001dba356b96fsi7177190plb.420.2024.02.20.19.43.34 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 20 Feb 2024 19:43:34 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-74010-ouuuleilei=gmail.com@vger.kernel.org designates 2604:1380:45e3:2400::1 as permitted sender) client-ip=2604:1380:45e3:2400::1; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b="cz5q/b+E"; 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-74010-ouuuleilei=gmail.com@vger.kernel.org designates 2604:1380:45e3:2400::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-74010-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 06488282DC9 for ; Wed, 21 Feb 2024 03:43:34 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 060043A28B; Wed, 21 Feb 2024 03:42:15 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="cz5q/b+E" Received: from mail-yb1-f202.google.com (mail-yb1-f202.google.com [209.85.219.202]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 9E3F439FC3 for ; Wed, 21 Feb 2024 03:42:10 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.219.202 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1708486932; cv=none; b=f4OQThmN6RTDL/9ZHP/1dZFo3et4A++MZ0hayvepMVb3Fq77nxI2xXyQO67x/I65NIP44gsEqKDseomDEUetDUOCrM81oYbAmeteLUufjqGtLDDJ9l5oBrspzdCAcgpFvudap3eD5sbdcrGdBQZKR3VkPPhIDtW1qsJ3rp1tyOs= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1708486932; c=relaxed/simple; bh=9QHqj0EQvOX6+a6MNptism09KshtnOJS+VQ0pDYpZf8=; h=Date:In-Reply-To:Message-Id:Mime-Version:References:Subject:From: To:Content-Type; b=OGoH5kyoRQmIqt4NeTT9JpJxTf4O1e3Szb4DrnNuz637LzCuFNxvKlrVXfHT0qdjZ0Q4Ap4/KGV+tJ0W36CH8151rlXXn5UOaMCN7KfRtgfGW9LlDvmb1wEw4uZNm8YgU8vBOZ0ko7A6ytSPgV32Hl0B4MOrFI4WHbpyAb2L4Gg= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--irogers.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=cz5q/b+E; arc=none smtp.client-ip=209.85.219.202 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--irogers.bounces.google.com Received: by mail-yb1-f202.google.com with SMTP id 3f1490d57ef6-dced704f17cso2109165276.1 for ; Tue, 20 Feb 2024 19:42:10 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1708486929; x=1709091729; darn=vger.kernel.org; h=to:from:subject:references:mime-version:message-id:in-reply-to:date :from:to:cc:subject:date:message-id:reply-to; bh=OwHudnCNl5wfpV2IMol1tp79ctLuVP/i5n4Lz8j3mXY=; b=cz5q/b+EfWRt2txHILY61V/gXJBUL2CSnvB009BhTjTdw0aWlisPpCaFRExuFxZ1uF Y3WlZN5mnjzsqRFTaNoGjelMHbxXk+4n7L7BvT7JRhEUYrdpSbjP7rERkiRlJBezQSTp XcHIOqPVFIR6HR9+Wudknqxh2oWHF0fUfqUCXIVCQK7Ql8dbeuopjnFCa2R52wevmNZO aq/+IaFdflieT2tcK/0j6Ui0b7DHwu78N8hGKTzTll8emsj9rD2uinslvX8g+CoTELyA 2IXklu85A8AW3tqVTP9f+EXf+jHh4WlNSstn3f6Bpe4yS4ph436vFtCEvylHz56y65US S/0w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1708486929; x=1709091729; h=to:from:subject:references:mime-version:message-id:in-reply-to:date :x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=OwHudnCNl5wfpV2IMol1tp79ctLuVP/i5n4Lz8j3mXY=; b=d6b2pc4NKCziSyOokbwLaznP79NpF1ifjHXUHul2TNjBiSgQS4oyXDGb64BwVU8616 Zn/5frpSVA/7J+j0+ZHtXzkAMld2Y4wvYVxSLyGldrjajVKPPuBYQGm0ulqGASKGbTqw B3DbbbZbzbLw1GRHUhQTfYYWPl6CAJaTPvnDl/0f1pfmMWXprSahSDiDPKSlj6pP+BOw i2vwNElhVupEm2POajYILEwmvWi1pNy0OvrYsUgV1UQp628xhLmi1Asxyvu3pGbYqlSc S3O8z8Vu90n+U+pNvdCeHuddVRd4iNz2FE9RA3TyY5c2a7MnqsX0+MU/UP+VANFdEGcm ADlQ== X-Forwarded-Encrypted: i=1; AJvYcCW+f8c8Yj6aU2LXWzLOFm0DPeHJMw62bRzfVCO2V4va1h5ls4CcKM2eHUT1faoo9TRlOa/UmFPxl0wpzxHAxdYSr4ikclBvCTfN4vwY X-Gm-Message-State: AOJu0YwObbLaZzXj6TvJrn+j+ZPqjHRXCjdfJ5FLeMKs1xB2ljbPMAaU fJme0yaH2HHCqpCillWV7/xvo7iWhHaRHNmQNUE2BSIuuPmYT1gRFtOOHLOfJVBHhzhgv5Dm4T4 Qrrj1Aw== X-Received: from irogers.svl.corp.google.com ([2620:15c:2a3:200:3c02:f882:5876:b7ab]) (user=irogers job=sendgmr) by 2002:a25:a447:0:b0:dcc:41ad:fb3b with SMTP id f65-20020a25a447000000b00dcc41adfb3bmr602386ybi.10.1708486929422; Tue, 20 Feb 2024 19:42:09 -0800 (PST) Date: Tue, 20 Feb 2024 19:41:50 -0800 In-Reply-To: <20240221034155.1500118-1-irogers@google.com> Message-Id: <20240221034155.1500118-4-irogers@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20240221034155.1500118-1-irogers@google.com> X-Mailer: git-send-email 2.44.0.rc0.258.g7320e95886-goog Subject: [PATCH v6 3/8] perf tests: Avoid fork in perf_has_symbol test From: Ian Rogers To: Peter Zijlstra , Ingo Molnar , Arnaldo Carvalho de Melo , Namhyung Kim , Mark Rutland , Alexander Shishkin , Jiri Olsa , Ian Rogers , Adrian Hunter , Nathan Chancellor , Nick Desaulniers , Bill Wendling , Justin Stitt , Athira Jajeev , James Clark , Kan Liang , Yang Jihong , linux-kernel@vger.kernel.org, linux-perf-users@vger.kernel.org, llvm@lists.linux.dev X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1791478479178446344 X-GMAIL-MSGID: 1791478479178446344 perf test -vv Symbols is used to indentify symbols within the perf binary. Add the -F flag so that the test command doesn't fork the test before running. This removes a little overhead. Acked-by: Adrian Hunter Signed-off-by: Ian Rogers --- tools/perf/tests/shell/lib/perf_has_symbol.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tools/perf/tests/shell/lib/perf_has_symbol.sh b/tools/perf/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 21 03:41: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: 203889 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:693c:2685:b0:108:e6aa:91d0 with SMTP id mn5csp813313dyc; Tue, 20 Feb 2024 19:43:59 -0800 (PST) X-Forwarded-Encrypted: i=3; AJvYcCUudoOyjwc0+e1a/3COGvuL+9mSgvFD/UFKniytDVkqwdoQGABwzpvr9ncwrB3/l4IPENTNPQ4p8vRCF1rhLx7VQIcnOg== X-Google-Smtp-Source: AGHT+IGcIbRZmkVt6OOWwMTq5DBmes5iocaCjaK+prcqEoxIxEY3EwVm2UoGQr9qn9DOlcJvWm+r X-Received: by 2002:a05:620a:220c:b0:785:d95d:dda0 with SMTP id m12-20020a05620a220c00b00785d95ddda0mr15897579qkh.41.1708487039306; Tue, 20 Feb 2024 19:43:59 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1708487039; cv=pass; d=google.com; s=arc-20160816; b=b1MRHckFKGww2ngBs3PCB9LykzQ4jFPFtxEp98CRGkPxJ+NYi2T+fuE+71KIP885Oj DY8zhZAcFkYOkiifk8buf0781+Kckd5r5x34gMoGHcYpqRpaoSG0XRG4Z25uHYZ+Gwfz y6Ho2KF6GyDuJnZIP3siwLk5R8rtgRbbegjzLmu93K2RsS4wsSyQkO39lQSleUgNDzor 745MWV8Qmqzj46Wd2RsayCfSjhYqRwtB2FBkaU20nCX7SzRgLbV8ybOpStBXB2JT/eX8 JkZQeBefLELx8DacBItSz9TG8sEtsApwpCUOdhsYPchwQ3ILxJPQ5zvNZRWydZGgDfDn 0KbQ== 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=eF36kSIEMlGOGF+Ru4Ee+ArAeIU+1BDlU+YfXMm89FI=; fh=UBG9/7/sylHE6ePT41H0WnpEepxf9r/VO/DpCHetBus=; b=OWOTnmMBgKeXwhtDLdvQzfS5Z7asej7wgtN7dTauF36EtuzOEyvuNBj0HtKaJd0jLU bnruqJCR7pcTvUbZ3zKZ97pT/oW953fVAQV0+f+1i2IOTmnBpu8Q4BSy0EHUEjb1fYpU swCuNOHHbqh5cg3r8yRxnPblY9clw6ArJBnFE/wblGRmVBlHbzL6meJTU5zduDkFegqm TAQj7QBuWQAPeq1KEUn1ern5nUPg+4+/TlKMOGivTkbLN0aEbVRHXHhbW0bJ0Zv+7j4Z VcZHQP8kwqHwNcJmNCCr7H3CgBDArIsAYbBpmUToDi2x1P29lMXwbeqYuBCr/iLq533b CTOw==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=h3bnIIED; 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-74012-ouuuleilei=gmail.com@vger.kernel.org designates 2604:1380:45d1:ec00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-74012-ouuuleilei=gmail.com@vger.kernel.org"; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Received: from ny.mirrors.kernel.org (ny.mirrors.kernel.org. [2604:1380:45d1:ec00::1]) by mx.google.com with ESMTPS id x17-20020a05620a0ed100b007873ca4eaaesi9601784qkm.660.2024.02.20.19.43.59 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 20 Feb 2024 19:43:59 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-74012-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=h3bnIIED; 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-74012-ouuuleilei=gmail.com@vger.kernel.org designates 2604:1380:45d1:ec00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-74012-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 1BE3A1C22041 for ; Wed, 21 Feb 2024 03:43:59 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id D663D3A8ED; Wed, 21 Feb 2024 03:42:17 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="h3bnIIED" Received: from mail-yw1-f201.google.com (mail-yw1-f201.google.com [209.85.128.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 094F438DCC for ; Wed, 21 Feb 2024 03:42:12 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.128.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1708486934; cv=none; b=SyzAYYWGtdqww8T1QvZEH96pfbgoaNfYxKcGWzhQAj+Bx+5y8ouHdlYs8QtRqqApIvDXcbIfq4KmVpqLem4hCxuchAs6I4y+KibgUhYmpTXESuI5lE/kMOTxajt6SmfHFhP7XsuaZ4oHOYs4Ux7RGSjR48PP5mgd/4Ouz6ZPAK4= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1708486934; c=relaxed/simple; bh=nIwADbFncYymlWUg/R60jRadSx8+xe1bkn0KEBii9rU=; h=Date:In-Reply-To:Message-Id:Mime-Version:References:Subject:From: To:Content-Type; b=aU05VRF3cspvmfriS9PWeSYJTXvqcdQy5N4yVrZoVEDqB7+2/Ikr5M3utOAuYFEZj4M4+0d1te64tUim3uCASocKtReGx72PKL5AAjhT2oBigNmBObFZIFGbZOnxJXeNNw9e9k84GfrecVLWcHI3v8Zd+OjFj4GalTJ+ZvDNEJA= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--irogers.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=h3bnIIED; arc=none smtp.client-ip=209.85.128.201 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--irogers.bounces.google.com Received: by mail-yw1-f201.google.com with SMTP id 00721157ae682-6081639fecfso41735927b3.1 for ; Tue, 20 Feb 2024 19:42:12 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1708486932; x=1709091732; darn=vger.kernel.org; h=to:from:subject:references:mime-version:message-id:in-reply-to:date :from:to:cc:subject:date:message-id:reply-to; bh=eF36kSIEMlGOGF+Ru4Ee+ArAeIU+1BDlU+YfXMm89FI=; b=h3bnIIEDWRDfKD/YMR00cbHb9S5+to8IdxcG+rp2/cD33J5Rqai7CqWB3pkNS+9ILI +1fMJ3foXETnFYJhMICrxS6uy3cYGIPqwvb92Mv1ZkXgN734XZP4igLGLSUANfrxcus4 hOovDO3gRWuoMCNEdivgzGzsJM9FBUPaugc0fTcnSRtSItvvg7Dla6Qxm22LLl7MYa2N fegE7djwCJP418WFzc4M75C2H7QwTPg/Dk4E/MtBGY0prZUdeOndGof3+GtyTCXp6JXO ICmMHxT33+SOgDb933qi0nzWrW4UMK9W3Sai9royTTQ5ko7A6SxjyC6CJ+BqPoTNKuAr re4Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1708486932; x=1709091732; h=to:from:subject:references:mime-version:message-id:in-reply-to:date :x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=eF36kSIEMlGOGF+Ru4Ee+ArAeIU+1BDlU+YfXMm89FI=; b=FdOnP9PGLF+4sQg90fgnxhgLDxUbBVtBQxnIXW4sPHIltvGzrxKCibtDyFU2TUnJ3p BTTC6xaehr5Lz+odXcX1s8Dz9MpaDcg5XLHEKgjsi5J97ubn+OQB+BJljVfuw2eHBo4F YTnRZPNSnp7drKgcD13qEhzI4TvGH09MNTm8KKl0PDk5I0WPedTeYsio2UVy0sZSBNuN vnZr0mHq2EYOt5wH75Tocc4xKX77RmY8ghRl0LqKywXTdH9oyB1gFzsVq2Y75J71SQOi PyFHFrQAhNFu+XH66rSczt68txYv+XMjxoFB8M90bHsp1HSE5rxCg8VwDRRpuFkA2NGo uV3Q== X-Forwarded-Encrypted: i=1; AJvYcCXEpGu1RdLgJYVqSQyQnNlMLjlbJPebtgDHJgQMbuAo4TUAriQz/SDfrXfBDoHj/C/yNWzu/iLC9GIHDIzlWXUiJz85xL4Rn//yNWHf X-Gm-Message-State: AOJu0YwHwJ6v7h0mD/bu3MfB/vWwa4BGvqvmZQScvE2ShJjo4UzFcOOC UIsWi3qN8pwnentRxQb057k4ssgUcu1eMxk95E5nY6awYvlsxpHT0BQYXYaFPLXDnLGTNJcGAeh 5lIl8xQ== X-Received: from irogers.svl.corp.google.com ([2620:15c:2a3:200:3c02:f882:5876:b7ab]) (user=irogers job=sendgmr) by 2002:a05:6902:704:b0:dc7:6f13:61d9 with SMTP id k4-20020a056902070400b00dc76f1361d9mr660785ybt.4.1708486932038; Tue, 20 Feb 2024 19:42:12 -0800 (PST) Date: Tue, 20 Feb 2024 19:41:51 -0800 In-Reply-To: <20240221034155.1500118-1-irogers@google.com> Message-Id: <20240221034155.1500118-5-irogers@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20240221034155.1500118-1-irogers@google.com> X-Mailer: git-send-email 2.44.0.rc0.258.g7320e95886-goog Subject: [PATCH v6 4/8] tools subcmd: Add a no exec function call option From: Ian Rogers To: Peter Zijlstra , Ingo Molnar , Arnaldo Carvalho de Melo , Namhyung Kim , Mark Rutland , Alexander Shishkin , Jiri Olsa , Ian Rogers , Adrian Hunter , Nathan Chancellor , Nick Desaulniers , Bill Wendling , Justin Stitt , Athira Jajeev , James Clark , Kan Liang , Yang Jihong , linux-kernel@vger.kernel.org, linux-perf-users@vger.kernel.org, llvm@lists.linux.dev X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1791478505549623878 X-GMAIL-MSGID: 1791478505549623878 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 21 03:41: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: 203892 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:693c:2685:b0:108:e6aa:91d0 with SMTP id mn5csp813736dyc; Tue, 20 Feb 2024 19:45:29 -0800 (PST) X-Forwarded-Encrypted: i=3; AJvYcCXvh157YaKm/8ZP6ayCkUBTTyeS+R3/eamntjY3m+WCgGVmA08JPLor78Ok+Kg+XTyuWCQ7fsLoyT70b7AQTyZuzI6iZQ== X-Google-Smtp-Source: AGHT+IF5BMm8iWw2DsOn6h1Jjg9BUC9WRg9O/Sg1pXuVbe9P5RX45DsfyC0hUA12P8xwTGKp+c51 X-Received: by 2002:a05:620a:e03:b0:787:1b11:d93b with SMTP id y3-20020a05620a0e0300b007871b11d93bmr18401280qkm.61.1708487129596; Tue, 20 Feb 2024 19:45:29 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1708487129; cv=pass; d=google.com; s=arc-20160816; b=xXRg+3dDk1loegcf1TR7gdgyAgS4ZywfdIJLV+deJxmwmxRkDT1wDRS6SgRhlKpEEw wFJUyLFGE1l0/nqWdIyjpomo0BZ5D1JliH4kj6lhtSobNqch3MiNwF0esug5EO3cWPar AUKt9KJqN8ObH/+5JK4R4ZpVvx5RP7JMMK2dDgUqAl5AzVo7afDknMHjjnOEra/3y2rj 9DruhRLtauKqyJERLGhe+8HOdSuldYaUGQU6Nzsvm8Qo2UdY/sslMG0iE6P6D1xn2aC8 YE+HvTtgy4zKN1Jm8HrLb9X450idfZIFVD++BXloPdddqWXfbs8MmKZ2Pt42QjcMd/Xa gZnA== 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=o0/Grl7GyHkKiOIRnltfxI/fRhIGN1eYIXPhWGdq1Pc=; fh=Mzioz07oQFL4UXSqnZgnLIo9XOKmgmVrdE7o/j6rgN4=; b=tV6LVNny2WNo+taXmehej4lSZ2teH5h5h5vrTCX1Abe6tEbtBsHB8O7UlJSC4MQ938 uUKErm2/I3O5jDmVc9tmNArXvbSRT/7V+CvPdD3VXG5IVxVEX+3sgR6IU1UUuL+4mhmc ewkX78LbOy8PXj8dvmUJBsJbstQGaB9MrVHYWTBT6R2emouZr6WdHxX694aRhBtmQ02b bTwY20pVOp+yN/aFP0UFfd96NEx7+gOVbQUmUkIU7BzK4s0CAeMjWSQQ5r8dY3N6nLb3 jl+OK2j2a0yquZc8mJKr6rltWOrOXOwyeaLsyCUOJodZ+ta6omY/6wiNqgbSDXIfcTQb hrXw==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=otD3bOGC; 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-74015-ouuuleilei=gmail.com@vger.kernel.org designates 147.75.199.223 as permitted sender) smtp.mailfrom="linux-kernel+bounces-74015-ouuuleilei=gmail.com@vger.kernel.org"; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Received: from ny.mirrors.kernel.org (ny.mirrors.kernel.org. [147.75.199.223]) by mx.google.com with ESMTPS id o27-20020a05620a22db00b00783da601714si9814370qki.574.2024.02.20.19.45.29 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 20 Feb 2024 19:45:29 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-74015-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=otD3bOGC; 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-74015-ouuuleilei=gmail.com@vger.kernel.org designates 147.75.199.223 as permitted sender) smtp.mailfrom="linux-kernel+bounces-74015-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 D958C1C21E43 for ; Wed, 21 Feb 2024 03:45:12 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 674AB3C461; Wed, 21 Feb 2024 03:42:26 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="otD3bOGC" Received: from mail-yb1-f202.google.com (mail-yb1-f202.google.com [209.85.219.202]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id ACD4E3AC24 for ; Wed, 21 Feb 2024 03:42:18 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.219.202 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1708486941; cv=none; b=e5ayDNHQ7cgTLBCUfvw9H5udlx0+AOgbjxyj+zO58isrBU1r/XI8jLkLOX1Wxv8RCnplz02+TlbYnPOM7HNG/l66UkgwiFKH6k4VsNuBp+WZEg2LK8TzmssYDDaYIrqBrJlfd8H/K3WUsreLxtfZXkXUxjrfFycIuEwscoGMuno= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1708486941; c=relaxed/simple; bh=qUA5FgB5jKzfSy9eC0WQ2ZcQSsUjX037xNJTVpjXWhQ=; h=Date:In-Reply-To:Message-Id:Mime-Version:References:Subject:From: To:Content-Type; b=dwQq4/4U8Mg0MSjjE99lOIZOy8MttWLpFw5JMlGWyvLDZyoupP31Qwd6V4xypjM3+5WZ9nLipM1SHZup3OA73OURdJk5Va5pAqxmpspe6OOYDQ44HhXaE1rD4/Em1+emfO093TSgzuYGGC+N13OotxO663oH8rtc9NClCKb9K7k= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--irogers.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=otD3bOGC; arc=none smtp.client-ip=209.85.219.202 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--irogers.bounces.google.com Received: by mail-yb1-f202.google.com with SMTP id 3f1490d57ef6-dc743cc50a6so8447909276.2 for ; Tue, 20 Feb 2024 19:42:18 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1708486938; x=1709091738; darn=vger.kernel.org; h=to:from:subject:references:mime-version:message-id:in-reply-to:date :from:to:cc:subject:date:message-id:reply-to; bh=o0/Grl7GyHkKiOIRnltfxI/fRhIGN1eYIXPhWGdq1Pc=; b=otD3bOGCDWeBoipbjDFK7uTEbhxBm636UDYzVJm/8xXNkU1RoGr6wW433TAjxKuo1i kksppxBOlIG9vnzsXOeEd9cxzvb6899nq3XbX2Lf/JU18uvWNdvPEEoSIkqMePYmE+RW 2Lmcy0pV3Yv/DrYQty9hKjqBVoB04+E1ZTm6Gp6vFMf6IILi2MeynyOSt0lJ0Fj55z51 i+IoZTe1IzV7zRREcjVSkNwqrdzNJfFewj02314vV7OJBf/y9MvN62uM3DW6AiTM0hNq 1iXCkYM66kIh8HDcggg/FARusyQoMAYEnF76J/tB9jSzks0GqE73l+PyXlUe0MafFVnE QrlQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1708486938; x=1709091738; h=to:from:subject:references:mime-version:message-id:in-reply-to:date :x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=o0/Grl7GyHkKiOIRnltfxI/fRhIGN1eYIXPhWGdq1Pc=; b=snn+iMIrShp079vqOhMV3Ieu6VZpg+GFTLEzHsjmSAXPieohYqeT38X2QruJQ/Z/NQ LthWxn1aCQPBbm1ZG209wWyJmv8xzjIGCSRdcP/ZI7zCijYhBFAvuX9rGgzn57YPkVDf V7EOkYMQc4HgD6bHHD0Xf5RRt9FspyxVbK3Pc+9brxXFM24SxaUiZfrwAz5wBWN6Tv2N LBIgjakOE3I7baMd3tEUqUyui7KKlydTRuwas2KeJ64lvogYZ1j09MGHq6Uq9I8DAQ/b rJ6kM23ZibP9XnupuY3zRsj6GaBT0Xz2jT6f28km4NtsI/ncR9fpAqxuBI8Ivry074lX QD8A== X-Forwarded-Encrypted: i=1; AJvYcCVuIIjVxNK0xckS//fM+jVp1g/cWm3UmugV0AU/rJQqsXrT4/kr0PJ1JDfjoVQBtGGswVjZOQ+ebVfNJirQTOFWjSv25Tumn1mklZgQ X-Gm-Message-State: AOJu0YxcdgqBhU7NNngwwt2VowYEzX14FrJraXwlNtU3SmtrfE/rxqsn mJddl8iR3M6c3Tcf2e0R4uTUKS/Fe5AJJ9QenIepgPW9aQX67W5UQULo5GBFfbUqNL0IFK18upp Z+u0/RA== X-Received: from irogers.svl.corp.google.com ([2620:15c:2a3:200:3c02:f882:5876:b7ab]) (user=irogers job=sendgmr) by 2002:a05:6902:18d4:b0:dcd:2f2d:7a0f with SMTP id ck20-20020a05690218d400b00dcd2f2d7a0fmr605043ybb.9.1708486937727; Tue, 20 Feb 2024 19:42:17 -0800 (PST) Date: Tue, 20 Feb 2024 19:41:53 -0800 In-Reply-To: <20240221034155.1500118-1-irogers@google.com> Message-Id: <20240221034155.1500118-7-irogers@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20240221034155.1500118-1-irogers@google.com> X-Mailer: git-send-email 2.44.0.rc0.258.g7320e95886-goog Subject: [PATCH v6 6/8] perf tests: Use scandirat for shell script finding From: Ian Rogers To: Peter Zijlstra , Ingo Molnar , Arnaldo Carvalho de Melo , Namhyung Kim , Mark Rutland , Alexander Shishkin , Jiri Olsa , Ian Rogers , Adrian Hunter , Nathan Chancellor , Nick Desaulniers , Bill Wendling , Justin Stitt , Athira Jajeev , James Clark , Kan Liang , Yang Jihong , linux-kernel@vger.kernel.org, linux-perf-users@vger.kernel.org, llvm@lists.linux.dev X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1791478600056716752 X-GMAIL-MSGID: 1791478600056716752 Avoid filename appending buffers by using openat, faccessat and scandirat more widely. Turn the script's path back to a file name using readlink from /proc//fd/. Read the script's description using api/io.h to avoid fdopen conversions. Whilst reading perform additional sanity checks on the script's contents. Signed-off-by: Ian Rogers --- tools/perf/tests/builtin-test.c | 20 ++--- tools/perf/tests/tests-scripts.c | 145 ++++++++++++++++++------------- tools/perf/tests/tests-scripts.h | 1 - 3 files changed, 95 insertions(+), 71 deletions(-) diff --git a/tools/perf/tests/builtin-test.c b/tools/perf/tests/builtin-test.c index eff3c62e9b47..162f9eb090ac 100644 --- a/tools/perf/tests/builtin-test.c +++ b/tools/perf/tests/builtin-test.c @@ -300,22 +300,19 @@ static int test_and_print(struct test_suite *t, int 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 = NULL; - path__join(script, sizeof(script) - 3, st->dir, st->file); - - if (verbose > 0) - strncat(script, " -v", sizeof(script) - strlen(script) - 1); - - err = system(script); + if (asprintf(&cmd, "%s%s", st->file, verbose ? " -v" : "") < 0) + return TEST_FAIL; + err = system(cmd); + free(cmd); if (!err) return TEST_OK; @@ -331,7 +328,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 +342,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 +451,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..c21f7a425da9 100644 --- a/tools/perf/tests/tests-scripts.c +++ b/tools/perf/tests/tests-scripts.c @@ -14,6 +14,7 @@ #include #include #include +#include #include "builtin.h" #include "tests-scripts.h" #include "color.h" @@ -24,6 +25,7 @@ #include "symbol.h" #include "tests.h" #include "util/rlimit.h" +#include "util/util.h" /* @@ -35,55 +37,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 +107,16 @@ static bool is_shell_script(const char *path) if (!ext) return false; if (!strcmp(ext, ".sh")) { /* Has .sh extension */ - if (access(path, R_OK | X_OK) == 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 +132,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 +163,8 @@ static void append_script(const char *dir, const char *file, const char *desc) /* Add file to end and NULL terminate the struct array */ files = 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 +173,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 +214,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 21 03:41: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: 203894 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:693c:2685:b0:108:e6aa:91d0 with SMTP id mn5csp813792dyc; Tue, 20 Feb 2024 19:45:40 -0800 (PST) X-Forwarded-Encrypted: i=3; AJvYcCXQ5XK6B4c2YuxlgX6Ze5XE3p+dN/L2G4TlZQ+8yVIKoydGsjliHYCdDJkIvHERXzT2QBBbm4SN64F3gM/RMBITa1r9HQ== X-Google-Smtp-Source: AGHT+IF110fIvm6FXQ9Z0WfulhQYtVFsDQBnAQMXvgTtwWRH/ecjmKLYOfZugQyIjk8AoMwxdX1e X-Received: by 2002:a05:620a:408a:b0:787:888e:2b2d with SMTP id f10-20020a05620a408a00b00787888e2b2dmr400416qko.69.1708487140685; Tue, 20 Feb 2024 19:45:40 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1708487140; cv=pass; d=google.com; s=arc-20160816; b=UbXK1VzrJBzU3CB8PeS31HcP5FWJCA439S/O7+UHOEY6631dw+sZ++b5Yl+MrWYzJ5 2SzIXQEuJsNCZ3s/e9zKqh/okkUatAHqW0/JE4mbFXaVPMU8ufd8e5mW6tVxCc3+DqSC ngq2hCPJFiqDiJje/L97TFmYWRtPcl9DTpt7WlBWtkRen+viBKkY8FRTHtqnPkG8qFhw WLAa6gAB4PWzrWdgjd05Bbh4QSBaXG+y1M2ACIbktPVOENyF4mLsd7VXISvLDLtq+03+ 4ZOiT9vFlKIfie3J5sCRmuTv+WLNIKQYfqV5+Rv+m8uGxnZuS47Ta3x+NxXkffz1CYWa EaKg== 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=Bs0vGCr4d3+lgKm3VULbLfJ1W710iiXQYeVwqOAeIjg=; fh=tCe1mQGsTw+DkikmA5+PfHNnJmJz9yeZi6S9nLFk4VQ=; b=nIR2XnuzSlrwRRERDQvyMpkBTApq7i06+q0CvufyZHJ7EiwEV31ODqIsDBTuz/W1iZ naeRdS3NSm105quwodmGZyRGbeqKxASYWQki+7+ujNBOPASzcOQNQQFClGdXOEvTJ5iz EjUTFBQARkMPSSHbW1gi8g5iIyKKiIkSiAGMUsjjEB5lmz3o7YWPYXJFm0LH4DuLNUdF KBCC9PWnnZIOp6vFhVBRbU/BAla0nYuGkaesT16ib+/q5bdBiG6Xo0tcIpOxUwSnDuYe 34JBEV1b9JRTYEHsdpQTuPcOzA2ZxrGPC72KCiF3+j7O5m0bmStqYAHsBmKimslYmz2I Bu0w==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=qFNIFl67; 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-74017-ouuuleilei=gmail.com@vger.kernel.org designates 147.75.199.223 as permitted sender) smtp.mailfrom="linux-kernel+bounces-74017-ouuuleilei=gmail.com@vger.kernel.org"; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Received: from ny.mirrors.kernel.org (ny.mirrors.kernel.org. [147.75.199.223]) by mx.google.com with ESMTPS id f3-20020ae9ea03000000b0078770fb0835si4337766qkg.542.2024.02.20.19.45.40 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 20 Feb 2024 19:45:40 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-74017-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=qFNIFl67; 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-74017-ouuuleilei=gmail.com@vger.kernel.org designates 147.75.199.223 as permitted sender) smtp.mailfrom="linux-kernel+bounces-74017-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 014AE1C22665 for ; Wed, 21 Feb 2024 03:45:34 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 88EC03C68A; Wed, 21 Feb 2024 03:42:28 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="qFNIFl67" Received: from mail-yw1-f202.google.com (mail-yw1-f202.google.com [209.85.128.202]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 2A5B03A8CA for ; Wed, 21 Feb 2024 03:42:21 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.128.202 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1708486943; cv=none; b=Db6aW8wgoHAOBcbxD5IySZyCTpV2vE21za4ePD6uf1T1Turt8F4YBYAK/rKWbkHWlQk7Upuebh9L1svKNribJ7RT2mAetU/64J+UmsCV0ax+fWRvm1cZo5loZRtiM3IN3uqHm/4/VZsYoVgMY1BHGV+7NDy2DOYPGOp7i+TSTVE= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1708486943; c=relaxed/simple; bh=53jT037aEfk5XcFmEFkxjxpT8RKj6m5/qirKoy031Rc=; h=Date:In-Reply-To:Message-Id:Mime-Version:References:Subject:From: To:Content-Type; b=j/1uDokjK+O7enlNgsGcz2eMBenPXu+WPzA5/jn0ZjUlRjcXXUbC+x4fOGrgFsnK6u/zFl93HewZOj4KZRCuzXKfBLnn6eJFslgp3TEGMQD6N4ZW+jFCB3emttUaHL6i2CdAx7l5PhfeBTfzyxW/CVhr1Xqnfyp23A5NexX/H2A= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--irogers.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=qFNIFl67; arc=none smtp.client-ip=209.85.128.202 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--irogers.bounces.google.com Received: by mail-yw1-f202.google.com with SMTP id 00721157ae682-6047a047f4cso4483147b3.3 for ; Tue, 20 Feb 2024 19:42:21 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1708486940; x=1709091740; darn=vger.kernel.org; h=to:from:subject:references:mime-version:message-id:in-reply-to:date :from:to:cc:subject:date:message-id:reply-to; bh=Bs0vGCr4d3+lgKm3VULbLfJ1W710iiXQYeVwqOAeIjg=; b=qFNIFl67sWIGunz/LqHFOBvJ3wMYQemxA2fTt2hzhRmAfMfWk4ytAqDXrFM6v2PV5G NNE7iR4hAeOR/lD2lmzzC1CiQKkhRUzWVKMoguhIh2aN1+UUELT+7Ic/e3kGS390ndOk 8T29Cz8XtLFcD8LmKWJRxocwpkbOPjsA4iCjLFiwlNKFuXjY+gi6fBZzQ+7a7vJXYwmo 6a9vTBWKjUso6ncUWOXQJ8DePrfMZtsGWQpFxZH/AEkyHpqY/Q0pw1PwJ7CNXsvyQKnQ sxC5zMNGhdFDRycDsqVmjoiFyQpOHSGVV94qA408kEWnQgIT22ONlBzmG4pMH52vwmpZ eP+w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1708486940; x=1709091740; h=to:from:subject:references:mime-version:message-id:in-reply-to:date :x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=Bs0vGCr4d3+lgKm3VULbLfJ1W710iiXQYeVwqOAeIjg=; b=rrvT/9JfJGCbqM8fc1GB+8282PuhbLhgYKiiCUZUByMhPA3VA8Ed+IENHRiG5TW22L O81GQpe6faHBgZAeLqlemIKVkUaQHbto24IVCuRsVX9WMx3Otj3WaaeJEuTxky62pdCf TPUgpl+v64ZQMfGsPncVrVFoSNJgcqxFmAxFxdkHG/WAaIqpqofu0Once3v3+jVqzcRe P5ngEBPa0upcrOHtNXRhjwdfaQFq2ZAPKkvUfl9zOCwZrTE7WwAFe1zJOrm6u06wjviL KmXC1CeRoVCIhTkRW8GZa3IQi0DQhx+N/tD052BOA7Nk15Q0/5BNrD16B3CTm59YIcKN IQ2A== X-Forwarded-Encrypted: i=1; AJvYcCXryZgwzv3BjpbpVg2s40kJN7UWnICT4DiloQn2vtTZM5tX2o8MVTe9k/bOsm7PUVyfdahSonGDS3keCosuU0pA/e5sfFpYO6MqJrpK X-Gm-Message-State: AOJu0YyJLuWf2Y+UZwD40gz72BXpa5r9yOSy2vL0n58onouml/fYMo54 h0A4UO3nGzzsmFXskilGtmcolfwciuaRv30Q8/8XXI8U0KnyP+Uc7QJvp4aOqCxkVv75nPqHCQ4 butPB1A== X-Received: from irogers.svl.corp.google.com ([2620:15c:2a3:200:3c02:f882:5876:b7ab]) (user=irogers job=sendgmr) by 2002:a81:6dd0:0:b0:608:3329:2f13 with SMTP id i199-20020a816dd0000000b0060833292f13mr1885586ywc.8.1708486940220; Tue, 20 Feb 2024 19:42:20 -0800 (PST) Date: Tue, 20 Feb 2024 19:41:54 -0800 In-Reply-To: <20240221034155.1500118-1-irogers@google.com> Message-Id: <20240221034155.1500118-8-irogers@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20240221034155.1500118-1-irogers@google.com> X-Mailer: git-send-email 2.44.0.rc0.258.g7320e95886-goog Subject: [PATCH v6 7/8] perf tests: Run time generate shell test suites From: Ian Rogers To: Peter Zijlstra , Ingo Molnar , Arnaldo Carvalho de Melo , Namhyung Kim , Mark Rutland , Alexander Shishkin , Jiri Olsa , Ian Rogers , Adrian Hunter , Nathan Chancellor , Nick Desaulniers , Bill Wendling , Justin Stitt , Athira Jajeev , James Clark , Kan Liang , Yang Jihong , linux-kernel@vger.kernel.org, linux-perf-users@vger.kernel.org, llvm@lists.linux.dev X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1791478612182873414 X-GMAIL-MSGID: 1791478612182873414 Rather than special shell test logic, do a single pass to create an array of test suites. Hold the shell test file name in the test suite priv field. This makes the special shell test logic in builtin-test.c redundant so remove it. Signed-off-by: Ian Rogers --- tools/perf/tests/builtin-test.c | 90 +-------------------- tools/perf/tests/tests-scripts.c | 129 ++++++++++++++++++------------- tools/perf/tests/tests-scripts.h | 10 +-- 3 files changed, 80 insertions(+), 149 deletions(-) diff --git a/tools/perf/tests/builtin-test.c b/tools/perf/tests/builtin-test.c index 162f9eb090ac..c42cb40fc242 100644 --- a/tools/perf/tests/builtin-test.c +++ b/tools/perf/tests/builtin-test.c @@ -130,6 +130,7 @@ static struct test_suite *generic_tests[] = { static struct test_suite **tests[] = { generic_tests, arch_tests, + NULL, /* shell tests created at runtime. */ }; static struct test_workload *workloads[] = { @@ -299,73 +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 = NULL; - - if (asprintf(&cmd, "%s%s", st->file, verbose ? " -v" : "") < 0) - 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)); @@ -440,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; } @@ -488,9 +406,6 @@ static int perf_test__list(int argc, const char **argv) test_description(t, subi)); } } - - perf_test__list_shell(argc, argv, i); - return 0; } @@ -550,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 c21f7a425da9..e2042b368269 100644 --- a/tools/perf/tests/tests-scripts.c +++ b/tools/perf/tests/tests-scripts.c @@ -27,16 +27,6 @@ #include "util/rlimit.h" #include "util/util.h" - -/* - * 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; @@ -132,12 +122,30 @@ 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 = NULL; + + if (asprintf(&cmd, "%s%s", file, verbose ? " -v" : "") < 0) + 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)); @@ -147,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; @@ -192,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) { @@ -205,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 21 03:41: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: 203895 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:693c:2685:b0:108:e6aa:91d0 with SMTP id mn5csp813888dyc; Tue, 20 Feb 2024 19:46:03 -0800 (PST) X-Forwarded-Encrypted: i=3; AJvYcCUV3yJHWiRIw5rs2Lj1MqdPAHnbXYtX6c3t8aBjU627U2Vv9pVX3XcHW8H+QQPLSI6bID96VRK4RUGgNgGJtHwlBUU6Sg== X-Google-Smtp-Source: AGHT+IE3StnN1Hm6vp+WK40dg2dug6rbJOcorMPLhQIXcAeh04kfLkkLDZHJErNeWiGGURlr3I5q X-Received: by 2002:a05:6214:224e:b0:68f:61c6:db84 with SMTP id c14-20020a056214224e00b0068f61c6db84mr11088449qvc.31.1708487162911; Tue, 20 Feb 2024 19:46:02 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1708487162; cv=pass; d=google.com; s=arc-20160816; b=T8g/dF7ysHZf9Ex+uxDsS+L1hbl8UKQs5HSvTFVCNqwWggllnpfS2vqzuPwfBnBlgl Mcx+IDKIEJAef7JC+suv4trDSNEqQP7vu9WJ0VBxbg+lVmMVui1mwZ7hP2kjjBvqL1vy 23rIT4dPoC1Ki2WbVMxVHZhhxNtL6RCZ+90ePAay+0IJIPgbv+4Up2jAce+UbzgVSwto 8JAW1uhZrCwJrWTeBoJ813RrZbyzzAxe+M57pTDdmBHwLOS39tkh7K9HPrEbdhaa+XC4 KKFDWvwsP5xN41MiSyU33NZJUPqsc9JedEv2/LxScejZ0VO4ssKBMqS5Fuu6GKAvginE xrHg== 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=46NbERsw+rX6VfHxglAqwzl8LfYw4J5J2BPeonbB4cQ=; fh=nrD4p4b1D67ivkaCNQFVDezfy3oNac3DwpSFuSGiHHw=; b=lVHvUSq9acl9+xJQFLk/vnyHUzc8z7F+kbySP90eJr0lgf9QxIEt0QK4mSmEtM8/aq uZLG13UfHlrM28p5YIF60gsGkAbVxO3hYOS+8VrZoddx1BJ5Ub6hiXRKUNMOoOX7F5iF HfEUiFTvDRu45llNeih8OZHns894+XleKkNde908dcIq89Z1oabUbgYmMdpUqbzAH/Rq Hz7lbDd3TujGKCUYb0eoIcSJKiGvTqIorSa2NZnB7xjsqTTR0i5YdVJEQV0QcEO2Z0EQ VFpD9rq1rFWBjhQ3CXyuuWy9Fdj39vL/4jUFHhqLM6JbeUrbDkwLhAZab4SzQKa8Vihu Mnrg==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=bDP546u8; 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-74019-ouuuleilei=gmail.com@vger.kernel.org designates 147.75.199.223 as permitted sender) smtp.mailfrom="linux-kernel+bounces-74019-ouuuleilei=gmail.com@vger.kernel.org"; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Received: from ny.mirrors.kernel.org (ny.mirrors.kernel.org. [147.75.199.223]) by mx.google.com with ESMTPS id jf15-20020a0562142a4f00b0068f6ee1e8e7si6117947qvb.217.2024.02.20.19.46.02 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 20 Feb 2024 19:46:02 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-74019-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=bDP546u8; 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-74019-ouuuleilei=gmail.com@vger.kernel.org designates 147.75.199.223 as permitted sender) smtp.mailfrom="linux-kernel+bounces-74019-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 9F6DE1C20D2C for ; Wed, 21 Feb 2024 03:46:02 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id D06FC3CF60; Wed, 21 Feb 2024 03:42:30 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="bDP546u8" Received: from mail-yw1-f202.google.com (mail-yw1-f202.google.com [209.85.128.202]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id B2E5C39859 for ; Wed, 21 Feb 2024 03:42:23 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.128.202 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1708486945; cv=none; b=faVCkXOz2e5ybYdYiNlzb3d/ewS4rm/YWFXUSLsLVYul4djj2HSQR2m1U6FYiK178NqpfoRa01AcVCp8OJp6P5V0P9K6San634R+YEQBk/oRsSe/1eSyEH4w3fh23hvV3VhJ/oAlz3AcSb1/sl1XLsUGkzEPMDCXmiAcB3BUfdI= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1708486945; c=relaxed/simple; bh=SV8Q2XP3AEzUCdEScST86iV7qnjMAWaIiYJHWAwVraA=; h=Date:In-Reply-To:Message-Id:Mime-Version:References:Subject:From: To:Content-Type; b=Vim6kKAzyGOIm4AKTBWfMcScvqneen3rqSgCZBkReOIzKclloYEYy/b/G1xxJB6RSoeUrZZJHDuQp9gu1UZVKzZJFbULNKBzXh6JQVM2k6KUWb61llAzbUmySmgoLxaz40AJSObypyHA3/Tgpdq2KyfIcjzeS5vlzHaYG60t4J0= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--irogers.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=bDP546u8; arc=none smtp.client-ip=209.85.128.202 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--irogers.bounces.google.com Received: by mail-yw1-f202.google.com with SMTP id 00721157ae682-60810219282so42976777b3.0 for ; Tue, 20 Feb 2024 19:42:23 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1708486943; x=1709091743; darn=vger.kernel.org; h=to:from:subject:references:mime-version:message-id:in-reply-to:date :from:to:cc:subject:date:message-id:reply-to; bh=46NbERsw+rX6VfHxglAqwzl8LfYw4J5J2BPeonbB4cQ=; b=bDP546u8vC4zpylxVQlXWnWOqiRNTzOLPp7wHjt9tq3M9goHm7W7F2bw2GEqJdCNXc uONh6tVlKsxas3WZkKqD90CkSaV7r8VtRzlpC0aA+qbg1NtIbwoqv5fTZTDluWfSX3QZ ZYvWfaCtef+c4q796dHEUXByE8zuQwG804jjgbtFJalVkSbMrakwQZgc0NilDyY6aeIu du54+MqOjDvqDFMPWiqyPYzaboPNmYfOshRcSHzqVrDAdsaaYFO7YGljUeSv+Gxu5fO5 3m/jTuVWEK6IV74WQNw9AG35FH+drOluBEkNWWALKQJEvLdninu4crih7IoSXMrtkI7+ JNvg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1708486943; x=1709091743; h=to:from:subject:references:mime-version:message-id:in-reply-to:date :x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=46NbERsw+rX6VfHxglAqwzl8LfYw4J5J2BPeonbB4cQ=; b=TeNUJAdJGKYXsDJjV8JbkFG0DKqPkG53V3Bd6gWxbkyUf6I2oklM3YJvu30Uv+uNFt LbHQbce/hnxzUfPSazYbBG3GOVK/xRoedUHZA9nEVjkUdtLO1Xv7l9xnY1S8nkfrJ6FH TJj1KiscAwwA40OU9NStTCsaQdIKmGYG7A0IIwVYOk38pHFUWHUMXmN/av0n01zNylbg ohwjcRoVY55TWdG+UjFnAMgVrA1i6F85eC9tvK+iaahm8uc2WhhPHbGOr4Y9fPGdf11v aVVX1IzCTqENyoyRNFVzXeJNDS0bpT30MgalUk7+4QaUcyKKhrEMWonZQTEefWy0LaeN ZNLw== X-Forwarded-Encrypted: i=1; AJvYcCXPFpT4HncJFA/Vk6fsI187f0Alz8XzxbRJKlApAA9XNVMUu9Hcq0fIfuoW9xMJE0L9rMJcA6XOAfDAA8McrKrwhSenW66QpzX+wcbO X-Gm-Message-State: AOJu0Yw+yLZpINbvYxcoS//tY7o7DpwxrYWw3RSKq2u/FWpyp7o349eS QsssYgxSiigtb3fcnCoWk8bfd0KLYv6hDxVtUkE6GFzRSeDuzy93AaEVUNVUplf0mHTqkvUNP7B LP11fhw== X-Received: from irogers.svl.corp.google.com ([2620:15c:2a3:200:3c02:f882:5876:b7ab]) (user=irogers job=sendgmr) by 2002:a0d:db4b:0:b0:607:81e1:8b83 with SMTP id d72-20020a0ddb4b000000b0060781e18b83mr3197747ywe.8.1708486942813; Tue, 20 Feb 2024 19:42:22 -0800 (PST) Date: Tue, 20 Feb 2024 19:41:55 -0800 In-Reply-To: <20240221034155.1500118-1-irogers@google.com> Message-Id: <20240221034155.1500118-9-irogers@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20240221034155.1500118-1-irogers@google.com> X-Mailer: git-send-email 2.44.0.rc0.258.g7320e95886-goog Subject: [PATCH v6 8/8] perf tests: Add option to run tests in parallel From: Ian Rogers To: Peter Zijlstra , Ingo Molnar , Arnaldo Carvalho de Melo , Namhyung Kim , Mark Rutland , Alexander Shishkin , Jiri Olsa , Ian Rogers , Adrian Hunter , Nathan Chancellor , Nick Desaulniers , Bill Wendling , Justin Stitt , Athira Jajeev , James Clark , Kan Liang , Yang Jihong , linux-kernel@vger.kernel.org, linux-perf-users@vger.kernel.org, llvm@lists.linux.dev X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1791478635813711696 X-GMAIL-MSGID: 1791478635813711696 By default tests are forked, add an option (-p or --parallel) so that the forked tests are all started in parallel and then their output gathered serially. This is opt-in as running in parallel can cause test flakes. Rather than fork within the code, the start_command/finish_command from libsubcmd are used. This changes how stderr and stdout are handled. The child stderr and stdout are always read to avoid the child blocking. If verbose is 1 (-v) then if the test fails the child stdout and stderr are displayed. If the verbose is >1 (e.g. -vv) then the stdout and stderr from the child are immediately displayed. An unscientific test on my laptop shows the wall clock time for perf test without parallel being 5 minutes 21 seconds and with parallel (-p) being 1 minute 50 seconds. Signed-off-by: Ian Rogers --- tools/perf/tests/builtin-test.c | 314 ++++++++++++++++++++++---------- 1 file changed, 215 insertions(+), 99 deletions(-) diff --git a/tools/perf/tests/builtin-test.c b/tools/perf/tests/builtin-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",