From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=74.125.82.199 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796334768; b=fWreJk1Of6KNVilNRS5ZFLU5AWgqvtVmiwdAqefv0a8IdF2ofDorQ+rcCRtC06Hu8g7C iB1tn2j8k6pr+ExdPCEm60X3DBSLI7qSU3VhgUA7BTQMb4xZvzC8qEiHHADJKMSAolT3C 9BTMENBO5A7jATLL6xVUSdwaC4PevFa/fe6PMPk06hn4FBOZEqjdN858xfY6hjOc82g4F xZWbwGrzHu9cX9n/RdT3J0jxTguS6sBugfA5w2O7LE2FqSNDjSyM6lb7eZsIkjq0ZeOmd WXbClowN0d2oPCzsdDaVvvNzU/ly6xNF3vsd9hLoITUPfQ/mFUVpcOe5OqFf4irGdyg== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796334768; h=DMARC-Filter:Received:DKIM-Signature:X-Google-DKIM-Signature: X-Forwarded-Encrypted:X-Gm-Message-State:X-Received:Date:In-Reply-To: Mime-Version:References:X-Mailer:Message-ID:Subject:From:To:Cc: Content-Type; bh=H5z2lBmhXxuSBMDOBEZXLL9pvXpiapsnMzhXr5rCH/s=; b=e0sgRaLaOMPxi94txjtpTbQtcM57RInvx0un1xCepKGHPBKnGmaC2hrfgVj2JIE/zR1a AG4Gpsix3ei6lYxgfHUT3+I7HDDn6LPB7ztPzm4jyZqkK8Yca2MpOzUDnZBFg9wsCPpCe XdyNljm2tnBIpuHLlYcZDmk8dYpB++4MMgWCb23aWrC4IzE2JrUGydRieYTtbs0Wo4nbQ s6ovnMGbBOT2dSUlKQMd9Rq5RkTXoEda/lPgOTGMliVWhamnYclMORWfqOFvU7683azIe qn8ut2DdGc4vWSXxI28WkQN3CwmVZY8YbfvS2xAdOXDw8L//rJHm/i6HGjBUFfkwdTA== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass header.from=google.com; spf=pass smtp.mailfrom=flex--irogers.bounces.google.com; dkim=pass header.d=google.com header.i=@google.com header.a=rsa-sha256 header.s=20260707 header.b=N64FnqNV; arc=none smtp.remote-ip=74.125.82.199 Received: from mail-dy1-f199.google.com (mail-dy1-f199.google.com [74.125.82.199]) (using TLSv1.3 with cipher TLS_AES_128_GCM_SHA256 (128/128 bits) key-exchange X25519 server-signature ECDSA (prime256v1) server-digest SHA256) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 21E61606A6A5 for ; Thu, 3 Dec 2026 21:52:47 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org 21E61606A6A5 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 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.a=rsa-sha256 header.s=20260707 header.b=N64FnqNV Received: by mail-dy1-f199.google.com with SMTP id 5a478bee46e88-38751f8cc15so196624eec.1 for ; Thu, 03 Dec 2026 13:52:47 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20260707; t=1796334767; x=1796939567; darn=vger.kernel.org; h=content-type:cc:to:from:subject:message-id:references:mime-version :in-reply-to:date:from:to:cc:subject:date:message-id:reply-to :content-type; bh=H5z2lBmhXxuSBMDOBEZXLL9pvXpiapsnMzhXr5rCH/s=; b=N64FnqNVefpTIrZWjg0Nu6cvLpp+mcpoCTccQVOwfyuOrZ9qZIjWEac24PQ8+m1MHp S3j6yaaAdOOvZ+W3b6nMtePdr4wHGPQqRjSv/0DaMfgK6oMI52TaPSnGA+a89oANxf35 gA3pApiQy2rtry1nBqeKvtol6QHtZ/cjktRun2ZbQeTKhBF1PMxdLRmAAviG+uOZWaYx CoDfLtz1T/j+E24FmBIs46e0EPX2uOC3p8jrQIPZ0cFep/N+8vmhCxfmNixxuVcz6emC 2FgxeWVpGNByVEd4EspEg48/q2TNZrEFLCsn/uV4dX7OFNNXzg7hyia9MYfvC/eqJ3x+ QTeQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1796334767; x=1796939567; h=content-type:cc:to:from:subject:message-id:references:mime-version :in-reply-to:date:x-gm-message-state:from:to:cc:subject:date :message-id:reply-to:content-type; bh=H5z2lBmhXxuSBMDOBEZXLL9pvXpiapsnMzhXr5rCH/s=; b=CpzBx74tPfWpa1JDWtltJc5ftARpZePgz/h/8512nW7uGGOn5fWfHciy8TzD4C9TCd utqUYtk1i4UDHg7FsBIR/YbLkf5eRmrUUxud4pYaGSix9tzUscKPpR19GCmOJ4Z5Y+Tv b7B4KqbEE4xFRPIE2Zuu0b5UvO97Rei3XzUUQtyWg8AsXfoHWnXt5suUBlbdprT2l9BZ u7MDS1cYr0Wfwikb6Cqwu5guTQF0DVzKEIE71kDFwu6MkJix6WHe4whshInr1G1cJuOq g2h5D0olk63cFJ0h6f/06S8Bbnc2/RF832o4RC8NY5V+V5OtM6QY9IjbiKCTuDF4Hpg8 iaOA== X-Forwarded-Encrypted: i=1; AL+hErEwOZl2oQ29RUgIOCeB4WmZqN8ySyskxLIo9zl5RYKJMKH09hJO1Dl8laPlP4qjZ+cIjjE8K8L2jvpDxZc=@vger.kernel.org X-Gm-Message-State: APuU4Z8lv/vuN6lB+IEXt4CYflB994zX61c1GJrtE6zikl/rVObyUkDM bGcu3HtuV9lIMdhRWbH2QSti8FLylPxoSAnpg2WZ1EtGndeixuUJIKnBq6gzSrjbx7VQtkeI5jR Tfy1kw0PktQ== X-Received: from dybpj8.prod.google.com ([2002:a05:7301:4248:b0:36c:a092:ca28]) (user=irogers job=prod-delivery.src-stubby-dispatcher) by 2002:a05:7301:6198:b0:386:954f:7c18 with SMTP id 5a478bee46e88-387521e5467mr95845eec.31.1796334766650; Thu, 03 Dec 2026 13:52:46 -0800 (PST) Date: Thu, 3 Dec 2026 13:52:17 -0800 In-Reply-To: <20261203215222.1392035-1-irogers@google.com> X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list Mime-Version: 1.0 References: <20261203215222.1392035-1-irogers@google.com> X-Mailer: git-send-email 2.56.1.915.g69d5ec7298-goog Message-ID: <20261203215222.1392035-9-irogers@google.com> Subject: [PATCH v2 08/13] perf stat: Respect the enabled time of exited user_time/system_time events From: Ian Rogers To: Peter Zijlstra , Ingo Molnar , Arnaldo Carvalho de Melo , Namhyung Kim , Jiri Olsa , Ian Rogers , Adrian Hunter , James Clark Cc: Athira Rajeev , TAKANO Mitsuhiro , Hang Zhengyang , Howard Chu , Thomas Falcon , Song Liu , linux-perf-users@vger.kernel.org, linux-kernel@vger.kernel.org Content-Type: text/plain; charset="UTF-8" When a workload has exited and been reaped by wait4, user_time and system_time can no longer be read from /proc//stat and perf stat falls back to using the rusage values from wait4. These are the totals for the whole run of the workload and ignore when the event was enabled, such as after a delay. For example: $ perf stat -D 500 -e duration_time,user_time,task-clock -- perf test -w noploop 524,466,208 duration_time 1,004,578,000 user_time 522.86 msec task-clock Add evsel__tool_pmu_exited_process_time() to compute the time the event was enabled for from the process's total time, the start time when it was last enabled and the accumulated time, and use it for the rusage fallback. Fixes: 0b18bced5444 ("perf tool_pmu: Make tool PMU events respect enable/disable") Signed-off-by: Ian Rogers --- tools/perf/builtin-stat.c | 3 +++ tools/perf/util/tool_pmu.c | 24 ++++++++++++++++++++++++ tools/perf/util/tool_pmu.h | 2 ++ 3 files changed, 29 insertions(+) diff --git a/tools/perf/builtin-stat.c b/tools/perf/builtin-stat.c index a470e2c0f392..e87954453d70 100644 --- a/tools/perf/builtin-stat.c +++ b/tools/perf/builtin-stat.c @@ -302,6 +302,9 @@ static int read_single_counter(struct evsel *counter, int cpu_map_idx, int threa else val = ru_stats.ru_stime_usec_stat.mean; + /* Only count the time the event was enabled, such as after a delay. */ + val = evsel__tool_pmu_exited_process_time(counter, cpu_map_idx, thread, val); + count->val = val; if (old_count) { count->run = old_count->run + 1; diff --git a/tools/perf/util/tool_pmu.c b/tools/perf/util/tool_pmu.c index a62a7cb390ae..24ff27099d32 100644 --- a/tools/perf/util/tool_pmu.c +++ b/tools/perf/util/tool_pmu.c @@ -13,9 +13,11 @@ #include #include #include +#include #include #include #include +#include #define INVALID_START_TIME ~0ULL @@ -709,6 +711,28 @@ int evsel__tool_pmu_read(struct evsel *evsel, int cpu_map_idx, int thread) return 0; } +/* + * When the process has exited its user or system time can no longer be read + * from /proc. Given the process's total time, such as from wait4, compute the + * time the event was enabled for. + */ +u64 evsel__tool_pmu_exited_process_time(struct evsel *evsel, int cpu_map_idx, int thread, + u64 total_ns) +{ + u64 ns_per_tick = 1000000000ULL / sysconf(_SC_CLK_TCK); + __u64 start_time = *(__u64 *)xyarray__entry(evsel->process_time.start_times, + cpu_map_idx, thread); + __u64 accumulated = *(__u64 *)xyarray__entry(evsel->process_time.accumulated_times, + cpu_map_idx, thread); + u64 val = accumulated * ns_per_tick; + + if (!evsel->disabled && start_time != INVALID_START_TIME && + total_ns >= start_time * ns_per_tick) + val += total_ns - start_time * ns_per_tick; + + return val; +} + struct perf_pmu *tool_pmu__new(void) { struct perf_pmu *tool = zalloc(sizeof(struct perf_pmu)); diff --git a/tools/perf/util/tool_pmu.h b/tools/perf/util/tool_pmu.h index f66d24cf3502..137b0a06a852 100644 --- a/tools/perf/util/tool_pmu.h +++ b/tools/perf/util/tool_pmu.h @@ -61,6 +61,8 @@ int evsel__tool_pmu_enable(struct evsel *evsel); int evsel__tool_pmu_disable_cpu(struct evsel *evsel, int cpu_map_idx); int evsel__tool_pmu_disable(struct evsel *evsel); int evsel__tool_pmu_read(struct evsel *evsel, int cpu_map_idx, int thread); +u64 evsel__tool_pmu_exited_process_time(struct evsel *evsel, int cpu_map_idx, int thread, + u64 total_ns); struct perf_pmu *tool_pmu__new(void); -- 2.56.1.915.g69d5ec7298-goog