Received: by 2002:ac0:a582:0:0:0:0:0 with SMTP id m2-v6csp5386451imm; Tue, 16 Oct 2018 09:26:36 -0700 (PDT) X-Google-Smtp-Source: ACcGV62kbC1fK0R3Q+hXjYuSoHdFzGxIfqOP7ju8v/U/IsaNKoMak/5jg5sqHY5JaSesKWdQbuk2 X-Received: by 2002:a17:902:509:: with SMTP id 9-v6mr22293178plf.155.1539707196596; Tue, 16 Oct 2018 09:26:36 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1539707196; cv=none; d=google.com; s=arc-20160816; b=CetnIwAJ9FFp/Kq40X3pD+FjpdJOKStnpe4b/uvvZfr3mxwW51IkFHe1ejES2EVYra wqsTRcW3My54LiMnYtMPxSkAN1K86hKDjCRYplezW1rPM0cfT6Vi/AABH9p/tlAWgEU4 zt6FR8ud5rKhAs/g1ru6skqrODCPk8GV2Cl82HWLljljDhdTiQRzM20oe8juoCDfd4Bs BvUFJtHZFeEnHTk/Klpi+0ye7hPz76FjlvbPgdM12dLxdjAHXJ9pB2yYnL/Osr6+GsJ5 8KgIN3AOaJwbUwi7z+y0k3e1nODXz2fl993H5iscsD7VOsjW7CPfxNQo7xArvv/dB9xw mkrw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:user-agent:in-reply-to :content-disposition:mime-version:references:message-id:subject:cc :to:from:date; bh=srKoyqmTfH7TtPZVBTq1Ox6FEy5FOrPUQGcTXIddkAk=; b=fn4j3kxfxm77o3Ai7akVLToWAYaGtG4gxetaQ9pRFC+BhG0d6rMgNOXRmIQLrbRrFN tubMbU+upaDOH4E4Daxja7h8KfcP8nAXuNy6RlqE9XCGhgFQCM7Ry88ipfuwLhiXIN7P Qq+p7u2p21SCtIZdKmeSUFMv7WQyZPrtUX1pMJnnLRyYwJ2iUBRTgk+WQ7deD7DiMMIV do/0FM5SD9VDrcdHXsV8IwfaEOq6FfBWJ+FIKzkSFvLPsq/0NgBDycA3fgaxohzhrIXa kywB3OnC6w/3sE2GDNpo1rKApdq2l9VZEeHZP+Rdp8apwpjjF6/xJNNURzUkoiADAI4X /szQ== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=redhat.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id n7-v6si14283253pgb.171.2018.10.16.09.26.20; Tue, 16 Oct 2018 09:26:36 -0700 (PDT) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=redhat.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1727165AbeJQARI (ORCPT + 99 others); Tue, 16 Oct 2018 20:17:08 -0400 Received: from mx1.redhat.com ([209.132.183.28]:54740 "EHLO mx1.redhat.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1727016AbeJQARI (ORCPT ); Tue, 16 Oct 2018 20:17:08 -0400 Received: from smtp.corp.redhat.com (int-mx06.intmail.prod.int.phx2.redhat.com [10.5.11.16]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id 808D19F726; Tue, 16 Oct 2018 16:25:57 +0000 (UTC) Received: from krava (unknown [10.43.17.150]) by smtp.corp.redhat.com (Postfix) with SMTP id C11E871CA9; Tue, 16 Oct 2018 16:25:55 +0000 (UTC) Date: Tue, 16 Oct 2018 18:25:54 +0200 From: Jiri Olsa To: Stephane Eranian Cc: Arnaldo Carvalho de Melo , Jiri Olsa , LKML , Peter Zijlstra , Ingo Molnar , Namhyung Kim , Alexander Shishkin Subject: Re: [BUG] perf stat: hangs with -p and process completes Message-ID: <20181016162554.GD6631@krava> References: <20181016110341.GB18450@krava> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20181016110341.GB18450@krava> User-Agent: Mutt/1.10.1 (2018-07-13) X-Scanned-By: MIMEDefang 2.79 on 10.5.11.16 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.39]); Tue, 16 Oct 2018 16:25:57 +0000 (UTC) Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Tue, Oct 16, 2018 at 01:03:41PM +0200, Jiri Olsa wrote: > On Fri, Oct 12, 2018 at 02:26:09PM -0700, Stephane Eranian wrote: > > Hi, > > > > I am running into a perf stat issue with the -p option which allows you > > to attach to a running process. If that process happens to terminate > > while under monitoring > > perf hangs in there and never terminates. The proper behavior would be to stop. > > I can see the issue in that the attached process is not a child, so > > wait() would not work. > > > > To reproduce: > > $ sleep 10 & > > $ perf stat -p $! > > > > doing the same with perf record works, so there is a solution to this problem. > > yea, we don't poll for the event state change in perf stat, > but we do that in perf record.. also because the perf poll > code in kernel is originaly meant for tracking the ring > buffer state > > maybe we could return EPOLLIN for alive events without ring > buffer.. like below (totaly untested) and add polling for > event state into perf stat > > cc-ing perf folks > on the second thought attached patch works as well without kernel change jirka --- diff --git a/tools/perf/builtin-stat.c b/tools/perf/builtin-stat.c index b86aba1c8028..d1028d7755bb 100644 --- a/tools/perf/builtin-stat.c +++ b/tools/perf/builtin-stat.c @@ -409,6 +409,28 @@ static struct perf_evsel *perf_evsel__reset_weak_group(struct perf_evsel *evsel) return leader; } +static bool is_target_alive(struct target *_target, + struct thread_map *threads) +{ + struct stat st; + int i; + + if (!target__has_task(_target)) + return true; + + for (i = 0; i < threads->nr; i++) { + char path[PATH_MAX]; + + scnprintf(path, PATH_MAX, "%s/%d", procfs__mountpoint(), + threads->map[i].pid); + + if (!stat(path, &st)) + return true; + } + + return false; +} + static int __run_perf_stat(int argc, const char **argv, int run_idx) { int interval = stat_config.interval; @@ -579,6 +601,8 @@ static int __run_perf_stat(int argc, const char **argv, int run_idx) enable_counters(); while (!done) { nanosleep(&ts, NULL); + if (!is_target_alive(&target, evsel_list->threads)) + break; if (timeout) break; if (interval) {