Received: by 2002:a25:683:0:0:0:0:0 with SMTP id 125csp584209ybg; Mon, 1 Jun 2020 09:00:52 -0700 (PDT) X-Google-Smtp-Source: ABdhPJxxy3kXs3YU8cB49zIF+3Pz3obD+aUVO1sWAaaeQmg9IH70FR37pksPGxxAlcVdA3QaHZ3/ X-Received: by 2002:a17:906:c837:: with SMTP id dd23mr19955567ejb.272.1591027251668; Mon, 01 Jun 2020 09:00:51 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1591027251; cv=none; d=google.com; s=arc-20160816; b=KKnXMyxqVITIp2INBtTGD7yzkTgXv3JpThk4vhOYJScJgvLPT37TM/jiOadoYgI9Wj o8bJ1PJM7B5YGZBurJ9ok6B1XE9ZRWge1oBJU3NBss3OfOS940+avkHbwajYZLorEym2 6sLi2xE2vbqdQTF6IAdQ3rJGVYxLPE9bogi9UqbLhRIx1oFfv0b+3IRQvD4wE57tIvGO c4KuDPeZD2P1TqBxYfQZ0vIx53VM+2x+qFZJI7FbROXntYNy0Avv0d9aF6jHDpnVTsCt 62VpWt+77NgfsyqJWm6Vz0HqD7Bin/tWMHQbAvXz6gbYTfDtRHCST3PtEZMk880ECeqo /zsQ== 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=4yqsED+W1dQEliaLIYgdBTmUbYCy3xq92LJb2ajUawI=; b=Jp206SZreonSOisWb+8nVzZFQmsq62sCTW1JZodMfWOTe9Qmmhk4gsLkRaIydsIZj7 0g9YyeltfJqS0nkjkBTKctmKT9DR+MJ42hrefB1Pr8Ll8t47GusUKrk7Hm5UuGTCzQIM 0KSwlbmifBs5nH+w9TjYeu9rnm+HiYoEuqMUpyVcl0fN2sRZgZ/FTmMu/6HHzIJBtrVn IVIaIJlpGmTOvQMR+xym0AtPwjDgki8+gWWHMujSOqwGLqDNMjSDV+vwaM8UiauSWb8w 6oDUS2JafGOPV3HCpTdNcK1iFa2y9mqUjUWYKGhTqs1+WdOipnx+N63QBo14F9TZOTyb FOyg== 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 u11si11728758edj.229.2020.06.01.09.00.28; Mon, 01 Jun 2020 09:00:51 -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 S1727113AbgFAP4h (ORCPT + 99 others); Mon, 1 Jun 2020 11:56:37 -0400 Received: from mga04.intel.com ([192.55.52.120]:44386 "EHLO mga04.intel.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726149AbgFAP4h (ORCPT ); Mon, 1 Jun 2020 11:56:37 -0400 IronPort-SDR: nOeT2MOAVWRhSKuRoKBKvKlV2Xa8P9uRzD2jN8omSMvZ4Dz8PBvVFGDsCkylZN09zLKQ4wdarn JIDkldfMw8Kw== X-Amp-Result: SKIPPED(no attachment in message) X-Amp-File-Uploaded: False Received: from orsmga005.jf.intel.com ([10.7.209.41]) by fmsmga104.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 01 Jun 2020 08:56:36 -0700 IronPort-SDR: Gs6lUyHRcMWpVloe4FJyoNOZ3Lco3qWhrHa9aVKNERuTZlSz7/eBcscZrC0u3C8jO6cSqvNvTH kiSfBH6XsLPw== X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="5.73,461,1583222400"; d="scan'208";a="444335590" Received: from linux.intel.com ([10.54.29.200]) by orsmga005.jf.intel.com with ESMTP; 01 Jun 2020 08:56:36 -0700 Received: from [10.249.230.65] (abudanko-mobl.ccr.corp.intel.com [10.249.230.65]) by linux.intel.com (Postfix) with ESMTP id 4FA25580378; Mon, 1 Jun 2020 08:56:34 -0700 (PDT) Subject: [PATCH v5 07/13] perf stat: factor out event handling loop into dispatch_events() From: Alexey Budankov To: Arnaldo Carvalho de Melo Cc: Jiri Olsa , Namhyung Kim , Alexander Shishkin , Peter Zijlstra , Ingo Molnar , Andi Kleen , linux-kernel References: Organization: Intel Corp. Message-ID: <7f200782-c0ad-16f2-e000-bb4743ea976d@linux.intel.com> Date: Mon, 1 Jun 2020 18:56:33 +0300 User-Agent: Mozilla/5.0 (Windows NT 10.0; WOW64; rv:68.0) Gecko/20100101 Thunderbird/68.8.1 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 launch, attach and system wide monitoring use cases into common dispatch_events() function. If passed pid contains valid pid i.e. doesn't equal to -1 then the function returns pid process exit status in case the process terminates during monitoring. Signed-off-by: Alexey Budankov --- tools/perf/builtin-stat.c | 35 ++++++++++++++++++++++++----------- 1 file changed, 24 insertions(+), 11 deletions(-) diff --git a/tools/perf/builtin-stat.c b/tools/perf/builtin-stat.c index dc7506be8bbd..8eeaf92912d8 100644 --- a/tools/perf/builtin-stat.c +++ b/tools/perf/builtin-stat.c @@ -504,6 +504,27 @@ static bool is_target_alive(struct target *_target, return false; } +static int dispatch_events(pid_t pid, bool timeout, int interval, int *times, struct timespec *ts) +{ + bool stop = false; + int child = 0, status = 0; + + while (1) { + if (pid != -1) + child = waitpid(pid, &status, WNOHANG); + else + child = is_target_alive(&target, evsel_list->core.threads) == false ? 1 : 0; + + if (done || stop || child) + break; + + nanosleep(ts, NULL); + stop = process_timeout(timeout, interval, times); + } + + return status; +} + enum counter_recovery { COUNTER_SKIP, COUNTER_RETRY, @@ -575,7 +596,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; @@ -744,12 +764,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); - stop = process_timeout(timeout, interval, ×); - } - } + if (interval || timeout) + status = dispatch_events(child_pid, timeout, interval, ×, &ts); if (child_pid != -1) { if (timeout) kill(child_pid, SIGTERM); @@ -766,10 +782,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); - stop = process_timeout(timeout, interval, ×); - } + dispatch_events(-1, timeout, interval, ×, &ts); } disable_counters(); -- 2.24.1