Received: by 2002:a05:6902:102b:0:0:0:0 with SMTP id x11csp2111554ybt; Fri, 3 Jul 2020 00:47:15 -0700 (PDT) X-Google-Smtp-Source: ABdhPJy8ryyLLs6GEanmJU+6YpqRjqe5o/TnF0CTT+uGSK3/4lMCq8SC7xBKzaCmcXndbpH86pRn X-Received: by 2002:a05:6402:947:: with SMTP id h7mr39282138edz.213.1593762434867; Fri, 03 Jul 2020 00:47:14 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1593762434; cv=none; d=google.com; s=arc-20160816; b=GTcZePmJEEgBbSIygC3zwmvL/Au+5+piGl/+IZQ9nViHstFubhDxZAt2reDbNusPvX 8m7d8CMlj3pCvpw7Dm4KbHdaZ6UjZ601VBKDiynhyTQu6DBT6D/w++h7qSQuIV/9awnJ O4A3PhmDgOVA5BdcjlL6AiK5WzxUydP0LPeTBXB+0FJpbhz10R4pFFjsOrN9HdYBG0j8 TBTfHyR5/4CmZ6IDmTj39XWT1OALFA6rZXqd/PacN54IWIui8o7R5oLdHLTrTG0eRaAk 5usOFczsFNGs8DkV3pV8JO+VmuZ8PEzb5Xyf4NB2dGZC4nGpOtjtvxh8yDV2XUrOtmWI H4+g== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:content-transfer-encoding :content-language:in-reply-to:mime-version:user-agent:date :message-id:organization:references:cc:to:from:subject:ironport-sdr :ironport-sdr; bh=Q/hXlcotvmwY5ALVIvDB7KIpxetXyCnawy45M+9V/1w=; b=U+82vVdUoYEEjCtojLKVXIF+G6GMPZfywxWfguvYmptTouZdHwQWKmDZqZDX5/bmdw /HwSb4x+fJ/vxi61T/uQWak3QM1sBGM8R/FcTaVhZQ8ksjbClSQLxmaFYq/vEkolB7NT NwD0YaDo/H5rG2eZOvIPSwow15zWTp2DaMY30ER0d+/jS3NPf+fEkFKuuGcE7GcjW3f6 SOsnyvNPMqEfSJgINTngp+cSFgH1kt5uADd0JNh0mogjVD4L75tmoaDzZekUORaCnl8c fJniHTXwLRuldyEwO4QhS/+ckHHCHYCcXepL2nkdYrF5G/ErS2/G9Wdc2MsZ7Hnf41vy iCkQ== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=intel.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id e1si5151641edy.464.2020.07.03.00.46.51; Fri, 03 Jul 2020 00:47:14 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=intel.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726415AbgGCHqV (ORCPT + 99 others); Fri, 3 Jul 2020 03:46:21 -0400 Received: from mga18.intel.com ([134.134.136.126]:2292 "EHLO mga18.intel.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1725779AbgGCHqU (ORCPT ); Fri, 3 Jul 2020 03:46:20 -0400 IronPort-SDR: 8cGbqs2XFj6IfoeqPJ9AU8ky3wsZmyLxSfooB2LtKgCyWVWAu1qXVPUvwUt3uRxRlhS5zAB4pO uByQGlpa8Ctg== X-IronPort-AV: E=McAfee;i="6000,8403,9670"; a="134568478" X-IronPort-AV: E=Sophos;i="5.75,307,1589266800"; d="scan'208";a="134568478" X-Amp-Result: SKIPPED(no attachment in message) X-Amp-File-Uploaded: False Received: from orsmga005.jf.intel.com ([10.7.209.41]) by orsmga106.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 03 Jul 2020 00:46:20 -0700 IronPort-SDR: gKkmDHpbpUqOLFeLsfdUKfUHV7hwEQ+pMuYmdgnYHV4hnpVYka7+SEbAwnMrzptEOm9KkIB4GZ yPCOAIGxS7gQ== X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="5.75,307,1589266800"; d="scan'208";a="455813534" Received: from linux.intel.com ([10.54.29.200]) by orsmga005.jf.intel.com with ESMTP; 03 Jul 2020 00:46:20 -0700 Received: from [10.249.231.67] (abudanko-mobl.ccr.corp.intel.com [10.249.231.67]) by linux.intel.com (Postfix) with ESMTP id 5CEDD580781; Fri, 3 Jul 2020 00:46:17 -0700 (PDT) Subject: [PATCH v9 09/15] perf stat: factor out event handling loop into dispatch_events() From: Alexey Budankov To: Arnaldo Carvalho de Melo , Jiri Olsa Cc: Namhyung Kim , Alexander Shishkin , Peter Zijlstra , Ingo Molnar , Andi Kleen , linux-kernel References: Organization: Intel Corp. Message-ID: Date: Fri, 3 Jul 2020 10:46:15 +0300 User-Agent: Mozilla/5.0 (Windows NT 10.0; WOW64; rv:68.0) Gecko/20100101 Thunderbird/68.10.0 MIME-Version: 1.0 In-Reply-To: Content-Type: text/plain; charset=utf-8 Content-Language: en-US Content-Transfer-Encoding: 7bit Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Consolidate event dispatching loops for fork, attach and system wide monitoring use cases into common dispatch_events() function. Signed-off-by: Alexey Budankov --- tools/perf/builtin-stat.c | 42 +++++++++++++++++++++++++-------------- 1 file changed, 27 insertions(+), 15 deletions(-) diff --git a/tools/perf/builtin-stat.c b/tools/perf/builtin-stat.c index 3e11f854ffc8..723f1fe27d63 100644 --- a/tools/perf/builtin-stat.c +++ b/tools/perf/builtin-stat.c @@ -550,6 +550,30 @@ static bool is_target_alive(struct target *_target, return false; } +static int dispatch_events(bool forks, int timeout, int interval, int *times, struct timespec *ts) +{ + bool stop = false; + int child_exited = 0, status = 0; + + while (1) { + if (forks) + child_exited = waitpid(child_pid, &status, WNOHANG); + else + child_exited = !is_target_alive(&target, evsel_list->core.threads) ? 1 : 0; + + if (done || stop || child_exited) + break; + + nanosleep(ts, NULL); + if (timeout) + stop = true; + else + stop = handle_interval(interval, times); + } + + return status; +} + enum counter_recovery { COUNTER_SKIP, COUNTER_RETRY, @@ -621,7 +645,6 @@ static int __run_perf_stat(int argc, const char **argv, int run_idx) struct affinity affinity; int i, cpu; bool second_pass = false; - bool stop = false; if (interval) { ts.tv_sec = interval / USEC_PER_MSEC; @@ -790,14 +813,8 @@ static int __run_perf_stat(int argc, const char **argv, int run_idx) perf_evlist__start_workload(evsel_list); enable_counters(); - if (interval || timeout) { - while (!stop && !waitpid(child_pid, &status, WNOHANG)) { - nanosleep(&ts, NULL); - if (timeout) - break; - stop = handle_interval(interval, ×); - } - } + if (interval || timeout) + status = dispatch_events(forks, timeout, interval, ×, &ts); if (child_pid != -1) { if (timeout) kill(child_pid, SIGTERM); @@ -814,12 +831,7 @@ static int __run_perf_stat(int argc, const char **argv, int run_idx) psignal(WTERMSIG(status), argv[0]); } else { enable_counters(); - while (!done && !stop && is_target_alive(&target, evsel_list->core.threads)) { - nanosleep(&ts, NULL); - if (timeout) - break; - stop = handle_interval(interval, ×); - } + status = dispatch_events(forks, timeout, interval, ×, &ts); } disable_counters(); -- 2.24.1