From nobody Thu Dec 25 16:14:41 2025 Received: from mail-pf1-f175.google.com (mail-pf1-f175.google.com [209.85.210.175]) (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 63D765689; Mon, 15 Jan 2024 07:23:18 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="Zq4GRdCj" Received: by mail-pf1-f175.google.com with SMTP id d2e1a72fcca58-6db82a57c50so139084b3a.2; Sun, 14 Jan 2024 23:23:18 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1705303397; x=1705908197; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=8DlrDFe5Y9kinmI2EZideUfaxC8SBA39wYnKFZevMC4=; b=Zq4GRdCjLOm7sEchNmh0mcQHOS4A9aiqwbcgYazxifGMNe4YsFs8Y7J8SCqhpqhgSM FzLy7A1xWQw+Lh6WkH33B+eLZnUJ2O+buGTKOgCHsyMNaDYyc3pwxim8vv37rRGc4tRW nBZZuWrOqD/Bt6eOD5FjxpSWEeI45sPscSdOwh/U5BQ/HEAx3J+rDYOyZUu510gWNkqC BcwPhxSaZ3acYj/M7O6chnkWzntMPD70Zj6adZqGxjGR9W8JhgxO7br7rOyY3N3wYb/1 JsBpgim7mwjVCj7IZvAWby3H/FuJRtrnjjFPhnjIjBwCO+Voqg5ttahIVCzvyDBHltxb 2n9g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1705303397; x=1705908197; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=8DlrDFe5Y9kinmI2EZideUfaxC8SBA39wYnKFZevMC4=; b=XzHWUavCF26rToFBWs2WIfU5BlRPNk4C3biMj+Gh8D2otj9Otx3rQS0tjxUArLpWpX Y12PjUHaWolZYrpr0c5v7ME2kXoYdz4oP2YPTIB+NLO2+RfM1DhkNy2SHc35zau65BAL 6ismycnN6GrHXeIO6Z3JHHBR595zJgDbcnMpPyXiDjpKjMS5V9tWlC+qr9dkZV2BRQme xw48ig84MibxbKdNla5GtbTxylmfjUzei0tghqBIQZJOGv97glcqYrc0AVfXy7eaqoaL 8c6ehxMTExgaEsna7SaKE6BKraSNn9DmrIQyxmvxomb+j9O9GzpD48WMQfDKWzCXnB5A HfHw== X-Gm-Message-State: AOJu0YxZoCVVTQli7JnKqyPK9Ayp69SfnU3dcLzZwzdjr/F2r7ocxVbM m28f0zTmxstM1orBpyeILxE= X-Google-Smtp-Source: AGHT+IEULegCjFeES6wlLCswi+EwFRbBZqG4OuMyEzjUISfosAI2UtkBU7q70D6rgrdnpD/5+c/sDA== X-Received: by 2002:a05:6a21:1a4:b0:19a:757f:2db5 with SMTP id le36-20020a056a2101a400b0019a757f2db5mr2012329pzb.8.1705303397599; Sun, 14 Jan 2024 23:23:17 -0800 (PST) Received: from localhost.localdomain ([203.205.141.87]) by smtp.googlemail.com with ESMTPSA id mp13-20020a170902fd0d00b001d4ac8ac969sm6990545plb.275.2024.01.14.23.23.14 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 14 Jan 2024 23:23:17 -0800 (PST) From: Ze Gao X-Google-Original-From: Ze Gao To: Adrian Hunter , Alexander Shishkin , Arnaldo Carvalho de Melo , Ian Rogers , Ingo Molnar , Jiri Olsa , Mark Rutland , Namhyung Kim , Peter Zijlstra , Steven Rostedt Cc: linux-kernel@vger.kernel.org, linux-perf-users@vger.kernel.org, Ze Gao Subject: [PATCH 1/4] perf sched: Sync state char array with the kernel Date: Mon, 15 Jan 2024 02:23:03 -0500 Message-ID: <20240115072306.303993-2-zegao@tencent.com> X-Mailer: git-send-email 2.41.0 In-Reply-To: <20240115072306.303993-1-zegao@tencent.com> References: <20240115072306.303993-1-zegao@tencent.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Update state char array to match the latest kernel definitions and remove unused state mapping macros. Note this is the preparing patch for get rid of the way to parse process state from raw bitmask value. Instead we are going to parse it from the recorded tracepoint print format, and this change marks why we're doing it. Signed-off-by: Ze Gao --- tools/perf/builtin-sched.c | 13 +------------ 1 file changed, 1 insertion(+), 12 deletions(-) diff --git a/tools/perf/builtin-sched.c b/tools/perf/builtin-sched.c index dd6065afbbaf..ced6fffe8110 100644 --- a/tools/perf/builtin-sched.c +++ b/tools/perf/builtin-sched.c @@ -92,23 +92,12 @@ struct sched_atom { struct task_desc *wakee; }; =20 -#define TASK_STATE_TO_CHAR_STR "RSDTtZXxKWP" +#define TASK_STATE_TO_CHAR_STR "RSDTtXZPI" =20 /* task state bitmask, copied from include/linux/sched.h */ #define TASK_RUNNING 0 #define TASK_INTERRUPTIBLE 1 #define TASK_UNINTERRUPTIBLE 2 -#define __TASK_STOPPED 4 -#define __TASK_TRACED 8 -/* in tsk->exit_state */ -#define EXIT_DEAD 16 -#define EXIT_ZOMBIE 32 -#define EXIT_TRACE (EXIT_ZOMBIE | EXIT_DEAD) -/* in tsk->state again */ -#define TASK_DEAD 64 -#define TASK_WAKEKILL 128 -#define TASK_WAKING 256 -#define TASK_PARKED 512 =20 enum thread_state { THREAD_SLEEPING =3D 0, --=20 2.41.0 From nobody Thu Dec 25 16:14:41 2025 Received: from mail-pl1-f175.google.com (mail-pl1-f175.google.com [209.85.214.175]) (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 D23556FB6; Mon, 15 Jan 2024 07:23:21 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="kygU50m/" Received: by mail-pl1-f175.google.com with SMTP id d9443c01a7336-1d3e2972f65so33125475ad.3; Sun, 14 Jan 2024 23:23:21 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1705303401; x=1705908201; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=C/dKJoVMA2YV+Bmb+1jLCdgMm+0uodJZZ6nPFfWpQiA=; b=kygU50m/O/oVO6N4SF1cr6cQFBSP5ctQEVKQxO6pvU8y+4xe/V3vCsB0guEyUX2m2c IYGjqiQGDvymsXD6kXsXcV/SnWRCx5Zry0BfwAkY1glqpHYSG+Em0Lki0KseVg8bGwmE NLXi0Ks+f/pFB7QsfZFR/P3iopfJdrb4RwVip4vOrOjEg7Mz0WejadaE4GbM3eF//7Eb qqpNZnuYNsYa9Jj9mgAXtLE1fov9y4IYTGYtZFwNqtrF4yiL+q4PtFMqk2wALI4vhntM qhPa24hO5qDbl6Mwz23g4LsT9qGmNq+7FLorfRA6BXWij0QopuPMioFbTNAxpjljX5DJ iNXg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1705303401; x=1705908201; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=C/dKJoVMA2YV+Bmb+1jLCdgMm+0uodJZZ6nPFfWpQiA=; b=n8JAu7c06IUiA97cPKxQhlR3Ye7qkNH2n0SfSoPLonrB95XKPnvNJsfsz9P0bRDVuY Zjgasa92zIQ957A+RLOwYegboYqh8U+jou++dOW5ShvZXdDxLxzOXzXVxem5RMIt0kh9 moHbRJINkXbIZZ/xgDKYyCv9KOdhgtmq9LRIxSpkfXGOLfMp7p7OL9dVsCzTAV1qiSdT twam5F/LtwS3ZN4HZBVinxOqWiMsI9qSPYlusWqAXNTsknhXJSFI4RNWEUkfC1/JDEc2 iJltcuCp9h20x3umnhqlUdn8e77yrZIchYchNIJZrUFY7uQvrC0r9JJ1RlghCvxXIhIX hPlg== X-Gm-Message-State: AOJu0YwKZoFMFpQV0DHU6w10K9jL4xR+TWYaBEsP7kZys5hhfuJQGJHc SijI+/tS/y/6kUjjFliBjc8= X-Google-Smtp-Source: AGHT+IESNBSdRKLz2PpiPiv2wP97t4b5+prjsly6UTKlXqmWMYjiHC9lDtpA14gn75jllqGuruzmeA== X-Received: by 2002:a17:902:654a:b0:1d5:693a:8904 with SMTP id d10-20020a170902654a00b001d5693a8904mr2296318pln.5.1705303401048; Sun, 14 Jan 2024 23:23:21 -0800 (PST) Received: from localhost.localdomain ([203.205.141.87]) by smtp.googlemail.com with ESMTPSA id mp13-20020a170902fd0d00b001d4ac8ac969sm6990545plb.275.2024.01.14.23.23.17 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 14 Jan 2024 23:23:20 -0800 (PST) From: Ze Gao X-Google-Original-From: Ze Gao To: Adrian Hunter , Alexander Shishkin , Arnaldo Carvalho de Melo , Ian Rogers , Ingo Molnar , Jiri Olsa , Mark Rutland , Namhyung Kim , Peter Zijlstra , Steven Rostedt Cc: linux-kernel@vger.kernel.org, linux-perf-users@vger.kernel.org, Ze Gao Subject: [PATCH 2/4] perf util: Add helpers to parse task state string from libtraceevent Date: Mon, 15 Jan 2024 02:23:04 -0500 Message-ID: <20240115072306.303993-3-zegao@tencent.com> X-Mailer: git-send-email 2.41.0 In-Reply-To: <20240115072306.303993-1-zegao@tencent.com> References: <20240115072306.303993-1-zegao@tencent.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Perf uses a hard coded string "RSDTtXZPI" to index the sched_switch prev_state field raw bitmask value. This works well except for when the kernel changes this string, in which case this will break again. Instead we add a new way to parse task state string from tracepoint print format already recorded by perf, which eliminates the further dependencies with this hardcode and unmaintainable macro, and this is exactly what libtraceevent[1] does for now. So we borrow the print flags parsing logic from libtraceevent[1]. And in get_states(), we walk the print arguments until the __print_flags() for the target state field is found, and use that to build the states string for future parsing. [1]: https://lore.kernel.org/linux-trace-devel/20231224140732.7d41698d@rors= chach.local.home/ Cc: Steven Rostedt (Google) Signed-off-by: Steven Rostedt (Google) Signed-off-by: Ze Gao --- tools/perf/util/evsel.c | 112 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 112 insertions(+) diff --git a/tools/perf/util/evsel.c b/tools/perf/util/evsel.c index 72a5dfc38d38..98edf42fcf80 100644 --- a/tools/perf/util/evsel.c +++ b/tools/perf/util/evsel.c @@ -2818,6 +2818,118 @@ u64 evsel__intval_common(struct evsel *evsel, struc= t perf_sample *sample, const return field ? format_field__intval(field, sample, evsel->needs_swap) : 0; } =20 +/* + * prev_state is of size long, which is 32 bits on 32 bit architectures. + * As it needs to have the same bits for both 32 bit and 64 bit architectu= res + * we can just assume that the flags we care about will all be within + * the 32 bits. + */ +#define MAX_STATE_BITS 32 + +static const char *convert_sym(struct tep_print_flag_sym *sym) +{ + static char save_states[MAX_STATE_BITS + 1]; + + memset(save_states, 0, sizeof(save_states)); + + /* This is the flags for the prev_state_field, now make them into a strin= g */ + for (; sym; sym =3D sym->next) { + long bitmask =3D strtoul(sym->value, NULL, 0); + int i; + + for (i =3D 0; !(bitmask & 1); i++) + bitmask >>=3D 1; + + if (i >=3D MAX_STATE_BITS) + continue; + + save_states[i] =3D sym->str[0]; + } + + return save_states; +} + +static struct tep_print_arg_field * +find_arg_field(struct tep_format_field *prev_state_field, struct tep_print= _arg *arg) +{ + struct tep_print_arg_field *field; + + if (!arg) + return NULL; + + if (arg->type =3D=3D TEP_PRINT_FIELD) + return &arg->field; + + if (arg->type =3D=3D TEP_PRINT_OP) { + field =3D find_arg_field(prev_state_field, arg->op.left); + if (field && field->field =3D=3D prev_state_field) + return field; + field =3D find_arg_field(prev_state_field, arg->op.right); + if (field && field->field =3D=3D prev_state_field) + return field; + } + return NULL; +} + +static struct tep_print_flag_sym * +test_flags(struct tep_format_field *prev_state_field, struct tep_print_arg= *arg) +{ + struct tep_print_arg_field *field; + + field =3D find_arg_field(prev_state_field, arg->flags.field); + if (!field) + return NULL; + + return arg->flags.flags; +} + +static struct tep_print_flag_sym * +search_op(struct tep_format_field *prev_state_field, struct tep_print_arg = *arg) +{ + struct tep_print_flag_sym *sym =3D NULL; + + if (!arg) + return NULL; + + if (arg->type =3D=3D TEP_PRINT_OP) { + sym =3D search_op(prev_state_field, arg->op.left); + if (sym) + return sym; + + sym =3D search_op(prev_state_field, arg->op.right); + if (sym) + return sym; + } else if (arg->type =3D=3D TEP_PRINT_FLAGS) { + sym =3D test_flags(prev_state_field, arg); + } + + return sym; +} + +static __maybe_unused const char *get_states(struct tep_format_field *prev= _state_field) +{ + struct tep_print_flag_sym *sym; + struct tep_print_arg *arg; + struct tep_event *event; + + event =3D prev_state_field->event; + + /* + * Look at the event format fields, and search for where + * the prev_state is parsed via the format flags. + */ + for (arg =3D event->print_fmt.args; arg; arg =3D arg->next) { + /* + * Currently, the __print_flags() for the prev_state + * is embedded in operations, so they too must be + * searched. + */ + sym =3D search_op(prev_state_field, arg); + if (sym) + return convert_sym(sym); + } + return NULL; +} #endif =20 bool evsel__fallback(struct evsel *evsel, int err, char *msg, size_t msgsi= ze) --=20 2.41.0 From nobody Thu Dec 25 16:14:41 2025 Received: from mail-pl1-f179.google.com (mail-pl1-f179.google.com [209.85.214.179]) (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 142257494; Mon, 15 Jan 2024 07:23:24 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="NPT6SAj7" Received: by mail-pl1-f179.google.com with SMTP id d9443c01a7336-1d4ab4e65aeso58280325ad.0; Sun, 14 Jan 2024 23:23:24 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1705303404; x=1705908204; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=YO7169RMuikSZ+p+ScnKGybrX+FP44pJIaHfIqNNpC4=; b=NPT6SAj7tFpW8DVZt5J7KX1PzlFXZxp4eEOG7PfZno4D/xTDkw32prpj6fkypbEz6q W0EDED7VeYOH9+9VKk4pVCO4d5KNT5fVlsQkWqw1Syie95r0L/6WrzZofh9fJNJltqu7 2UCcSfN9RYkbAlhaRA7vpboPGBwnEI4CU8Vf/TGfuk8qbQbY3x62dM2lNRTzY0QHadu5 0AHqw7UG4ki2LS6J4HR3xZXr+h4AUT5spTEJqYH/B4qJr448mmqfS60gpbYpprHF8rwe RSyn5HwTTIYyjhSRvykZVGCYdoxXkvS8iF62SnLCVsEHEM9KmBZOnTYG8Am138PwyV32 WgUw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1705303404; x=1705908204; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=YO7169RMuikSZ+p+ScnKGybrX+FP44pJIaHfIqNNpC4=; b=OI48LYIzIVpe0FSQ8c9ciFR/HpQ0mE5/UTWLbmp/s/3h6hpyBJSkv/svaRBdnJyCvQ CKomc0rNrtWxHMeeLE1ZIQBVJBoqI8CJOeLGIglVgWJieLPWzvYasnJKuUjtMTIkqbLU 3dj2zls7ymEX1JvB8cXyBWFQy7hArRfwgaSihAnUT42thLrDZFIv802eCs7y9N2fsDv2 xWJfQu6upfURuql65dTVWxSulIdoOtQreQ0S8y4+fbFQ62djSidWz9ex/9TYmeRwrKBR OoMmJZ1Hz+01kEjKGtqNU7cva2WldvKf3XMJKdLFBOJjWJXKKqdo2hO9jb/FBB5OpB0/ xeIA== X-Gm-Message-State: AOJu0YwSpvFjVhohkKDO6WLSGtFd+AN5ewHP8TLFZfK5JtCzQ7y8JzP1 KfKcE2cuHWl3ROqz0ZZeK98= X-Google-Smtp-Source: AGHT+IErMqZfrJl+P1wGVUtmwxjvshgCpz+Duj1dr3bHpYG/pdCr8ESy+g/Gw51zQpXZEmoGclKSOw== X-Received: by 2002:a17:902:db0c:b0:1d4:2bae:49f with SMTP id m12-20020a170902db0c00b001d42bae049fmr8005525plx.26.1705303404449; Sun, 14 Jan 2024 23:23:24 -0800 (PST) Received: from localhost.localdomain ([203.205.141.87]) by smtp.googlemail.com with ESMTPSA id mp13-20020a170902fd0d00b001d4ac8ac969sm6990545plb.275.2024.01.14.23.23.21 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 14 Jan 2024 23:23:24 -0800 (PST) From: Ze Gao X-Google-Original-From: Ze Gao To: Adrian Hunter , Alexander Shishkin , Arnaldo Carvalho de Melo , Ian Rogers , Ingo Molnar , Jiri Olsa , Mark Rutland , Namhyung Kim , Peter Zijlstra , Steven Rostedt Cc: linux-kernel@vger.kernel.org, linux-perf-users@vger.kernel.org, Ze Gao Subject: [PATCH 3/4] perf util: Add evsel__taskstate() to parse the task state info instead Date: Mon, 15 Jan 2024 02:23:05 -0500 Message-ID: <20240115072306.303993-4-zegao@tencent.com> X-Mailer: git-send-email 2.41.0 In-Reply-To: <20240115072306.303993-1-zegao@tencent.com> References: <20240115072306.303993-1-zegao@tencent.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Now that we have the __prinf_flags() parsing routines, we add a new helper evsel__taskstate() to extract the task state info from the recorded data. Signed-off-by: Ze Gao --- tools/perf/util/evsel.c | 36 +++++++++++++++++++++++++++++++++++- tools/perf/util/evsel.h | 1 + 2 files changed, 36 insertions(+), 1 deletion(-) diff --git a/tools/perf/util/evsel.c b/tools/perf/util/evsel.c index 98edf42fcf80..bc256bdc58d9 100644 --- a/tools/perf/util/evsel.c +++ b/tools/perf/util/evsel.c @@ -2906,7 +2906,7 @@ search_op(struct tep_format_field *prev_state_field, = struct tep_print_arg *arg) return sym; } =20 -static __maybe_unused const char *get_states(struct tep_format_field *prev= _state_field) +static const char *get_states(struct tep_format_field *prev_state_field) { struct tep_print_flag_sym *sym; struct tep_print_arg *arg; @@ -2930,6 +2930,40 @@ static __maybe_unused const char *get_states(struct = tep_format_field *prev_state } return NULL; } + +char evsel__taskstate(struct evsel *evsel, struct perf_sample *sample, con= st char *name) +{ + static struct tep_format_field *prev_state_field; + static const char *states; + struct tep_format_field *field; + unsigned long long val; + unsigned int bit; + char state =3D '?'; /* '?' denotes unknown task state */ + + field =3D evsel__field(evsel, name); + + if (!field) + return state; + + if (!states || field !=3D prev_state_field) { + states =3D get_states(field); + if (!states) + return state; + prev_state_field =3D field; + } + + /* + * Note since the kernel exposes TASK_REPORT_MAX to userspace + * to denote the 'preempted' state, we might as welll report + * 'R' for this case, which make senses to users as well. + * + * We can change this if we have a good reason in the future. + */ + val =3D evsel__intval(evsel, sample, name); + bit =3D val ? ffs(val) : 0; + state =3D (!bit || bit > strlen(states)) ? 'R' : states[bit-1]; + return state; +} #endif =20 bool evsel__fallback(struct evsel *evsel, int err, char *msg, size_t msgsi= ze) diff --git a/tools/perf/util/evsel.h b/tools/perf/util/evsel.h index d791316a1792..55a7a6210513 100644 --- a/tools/perf/util/evsel.h +++ b/tools/perf/util/evsel.h @@ -338,6 +338,7 @@ struct perf_sample; void *evsel__rawptr(struct evsel *evsel, struct perf_sample *sample, const= char *name); u64 evsel__intval(struct evsel *evsel, struct perf_sample *sample, const c= har *name); u64 evsel__intval_common(struct evsel *evsel, struct perf_sample *sample, = const char *name); +char evsel__taskstate(struct evsel *evsel, struct perf_sample *sample, con= st char *name); =20 static inline char *evsel__strval(struct evsel *evsel, struct perf_sample = *sample, const char *name) { --=20 2.41.0 From nobody Thu Dec 25 16:14:41 2025 Received: from mail-pl1-f173.google.com (mail-pl1-f173.google.com [209.85.214.173]) (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 858C88814; Mon, 15 Jan 2024 07:23:28 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="a0Min/Bp" Received: by mail-pl1-f173.google.com with SMTP id d9443c01a7336-1d51ba18e1bso71735725ad.0; Sun, 14 Jan 2024 23:23:28 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1705303408; x=1705908208; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=kZ5LQrtGOef2RhCQexh0DzGghwnp1zT6hJb7zIaDJYU=; b=a0Min/BpgERqnBz4GkjZawDm1Tmtq7QwVUZqZp9TbVGFiLQ46BxC5h6oqGn4HxfC+x Pqz1yhNI6uptsKTplg10oBlA1rJPMnJpf9Y6eGThSxgdoADrC4v0JDgMmCDiHAatzpZI iIfo+wm0vSfR9J+Jx4G465RiVx4ToJ7OF1zyaJ3AWc+R282Oh8orfRBbXWddF/3tncJK e84eZfw4dttpMMbCQ72YB6SS85wipxc37dABfuEJ7EAIuFtLR/oi+cVdi/2YLw3h8eqQ A720EKQT7jfOPSIles8MBeYQpts+o/9Suq0GYBjTALuTbr8B65mPIa4bkav43ifVwFzK dIQA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1705303408; x=1705908208; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=kZ5LQrtGOef2RhCQexh0DzGghwnp1zT6hJb7zIaDJYU=; b=lMb9/GC/tp8/xMvYLlBBGaQkc1w1RSa5mfmP72oVESn7g+LmqiuJWRH+mBeiclZB4g zreJvnu0z2I5VGPxBX1mHFLFOn+4iajAsff9ePOtZhoZDa7uiV1TPDAgZrRDdAYcN6+9 Mg6xgY8LNOlIJxqRWfh2zIEkJelNN5yi4hy/g/HsJxcCR45kRAKeRUjad1b33lRmjUv2 PuF5RoXm8J0RE4ba56KEAOk3evO6ZQ+h+UmMi+zsGgEW1cW3//fvXlPUeHfnq1RLVVRO o2qcsWysKEQutGYeh5IyiTkxOqg+IOrBbDVfgcEUhOX5tEcLnKBK+B/c9inhsFenPpuJ PdPg== X-Gm-Message-State: AOJu0YzOcqDClTzG9m8+aCuCMw6y0q0g9KUIgMrUQIe/nOgSMzYX88Or 82HxkSYCtliB8ptqVnzkdFY= X-Google-Smtp-Source: AGHT+IE7XD6hPXg4Mob0qJPGLGIM9saQIso5Je8CuomGJn7KGPsIOjR+XO9RI88DsLW7kxGQ8P7EbA== X-Received: by 2002:a17:902:e78a:b0:1d3:ee1f:ce54 with SMTP id cp10-20020a170902e78a00b001d3ee1fce54mr6201200plb.89.1705303407872; Sun, 14 Jan 2024 23:23:27 -0800 (PST) Received: from localhost.localdomain ([203.205.141.87]) by smtp.googlemail.com with ESMTPSA id mp13-20020a170902fd0d00b001d4ac8ac969sm6990545plb.275.2024.01.14.23.23.24 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 14 Jan 2024 23:23:27 -0800 (PST) From: Ze Gao X-Google-Original-From: Ze Gao To: Adrian Hunter , Alexander Shishkin , Arnaldo Carvalho de Melo , Ian Rogers , Ingo Molnar , Jiri Olsa , Mark Rutland , Namhyung Kim , Peter Zijlstra , Steven Rostedt Cc: linux-kernel@vger.kernel.org, linux-perf-users@vger.kernel.org, Ze Gao Subject: [PATCH 4/4] perf sched: Commit to evsel__taskstate() to parse task state info Date: Mon, 15 Jan 2024 02:23:06 -0500 Message-ID: <20240115072306.303993-5-zegao@tencent.com> X-Mailer: git-send-email 2.41.0 In-Reply-To: <20240115072306.303993-1-zegao@tencent.com> References: <20240115072306.303993-1-zegao@tencent.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Now that we have evsel__taskstate() which no longer relies on the hardcoded task state string and has good backward compatibility, we have a good reason to use it. Note TASK_STATE_TO_CHAR_STR and task bitmasks are useless now so we remove them for good. And now we pass the state info back and forth in a symbolic char which explains itself well instead. Signed-off-by: Ze Gao --- tools/perf/builtin-sched.c | 46 +++++++++----------------------------- 1 file changed, 10 insertions(+), 36 deletions(-) diff --git a/tools/perf/builtin-sched.c b/tools/perf/builtin-sched.c index ced6fffe8110..f1d62f6b6cab 100644 --- a/tools/perf/builtin-sched.c +++ b/tools/perf/builtin-sched.c @@ -92,13 +92,6 @@ struct sched_atom { struct task_desc *wakee; }; =20 -#define TASK_STATE_TO_CHAR_STR "RSDTtXZPI" - -/* task state bitmask, copied from include/linux/sched.h */ -#define TASK_RUNNING 0 -#define TASK_INTERRUPTIBLE 1 -#define TASK_UNINTERRUPTIBLE 2 - enum thread_state { THREAD_SLEEPING =3D 0, THREAD_WAIT_CPU, @@ -255,7 +248,7 @@ struct thread_runtime { u64 total_preempt_time; u64 total_delay_time; =20 - int last_state; + char last_state; =20 char shortname[3]; bool comm_changed; @@ -425,7 +418,7 @@ static void add_sched_event_wakeup(struct perf_sched *s= ched, struct task_desc *t } =20 static void add_sched_event_sleep(struct perf_sched *sched, struct task_de= sc *task, - u64 timestamp, u64 task_state __maybe_unused) + u64 timestamp, const char task_state __maybe_unused) { struct sched_atom *event =3D get_new_event(task, timestamp); =20 @@ -849,7 +842,7 @@ static int replay_switch_event(struct perf_sched *sched, *next_comm =3D evsel__strval(evsel, sample, "next_comm"); const u32 prev_pid =3D evsel__intval(evsel, sample, "prev_pid"), next_pid =3D evsel__intval(evsel, sample, "next_pid"); - const u64 prev_state =3D evsel__intval(evsel, sample, "prev_state"); + const char prev_state =3D evsel__taskstate(evsel, sample, "prev_state"); struct task_desc *prev, __maybe_unused *next; u64 timestamp0, timestamp =3D sample->time; int cpu =3D sample->cpu; @@ -1039,13 +1032,6 @@ static int thread_atoms_insert(struct perf_sched *sc= hed, struct thread *thread) return 0; } =20 -static char sched_out_state(u64 prev_state) -{ - const char *str =3D TASK_STATE_TO_CHAR_STR; - - return str[prev_state]; -} - static int add_sched_out_event(struct work_atoms *atoms, char run_state, @@ -1121,7 +1107,7 @@ static int latency_switch_event(struct perf_sched *sc= hed, { const u32 prev_pid =3D evsel__intval(evsel, sample, "prev_pid"), next_pid =3D evsel__intval(evsel, sample, "next_pid"); - const u64 prev_state =3D evsel__intval(evsel, sample, "prev_state"); + const char prev_state =3D evsel__taskstate(evsel, sample, "prev_state"); struct work_atoms *out_events, *in_events; struct thread *sched_out, *sched_in; u64 timestamp0, timestamp =3D sample->time; @@ -1157,7 +1143,7 @@ static int latency_switch_event(struct perf_sched *sc= hed, goto out_put; } } - if (add_sched_out_event(out_events, sched_out_state(prev_state), timestam= p)) + if (add_sched_out_event(out_events, prev_state, timestamp)) return -1; =20 in_events =3D thread_atoms_search(&sched->atom_root, sched_in, &sched->cm= p_pid); @@ -2022,24 +2008,12 @@ static void timehist_header(struct perf_sched *sche= d) printf("\n"); } =20 -static char task_state_char(struct thread *thread, int state) -{ - static const char state_to_char[] =3D TASK_STATE_TO_CHAR_STR; - unsigned bit =3D state ? ffs(state) : 0; - - /* 'I' for idle */ - if (thread__tid(thread) =3D=3D 0) - return 'I'; - - return bit < sizeof(state_to_char) - 1 ? state_to_char[bit] : '?'; -} - static void timehist_print_sample(struct perf_sched *sched, struct evsel *evsel, struct perf_sample *sample, struct addr_location *al, struct thread *thread, - u64 t, int state) + u64 t, const char state) { struct thread_runtime *tr =3D thread__priv(thread); const char *next_comm =3D evsel__strval(evsel, sample, "next_comm"); @@ -2080,7 +2054,7 @@ static void timehist_print_sample(struct perf_sched *= sched, print_sched_time(tr->dt_run, 6); =20 if (sched->show_state) - printf(" %5c ", task_state_char(thread, state)); + printf(" %5c ", thread->tid =3D=3D 0 ? 'I' : state); =20 if (sched->show_next) { snprintf(nstr, sizeof(nstr), "next: %s[%d]", next_comm, next_pid); @@ -2152,9 +2126,9 @@ static void timehist_update_runtime_stats(struct thre= ad_runtime *r, else if (r->last_time) { u64 dt_wait =3D tprev - r->last_time; =20 - if (r->last_state =3D=3D TASK_RUNNING) + if (r->last_state =3D=3D 'R') r->dt_preempt =3D dt_wait; - else if (r->last_state =3D=3D TASK_UNINTERRUPTIBLE) + else if (r->last_state =3D=3D 'D') r->dt_iowait =3D dt_wait; else r->dt_sleep =3D dt_wait; @@ -2579,7 +2553,7 @@ static int timehist_sched_change_event(struct perf_to= ol *tool, struct thread_runtime *tr =3D NULL; u64 tprev, t =3D sample->time; int rc =3D 0; - int state =3D evsel__intval(evsel, sample, "prev_state"); + const char state =3D evsel__taskstate(evsel, sample, "prev_state"); =20 addr_location__init(&al); if (machine__resolve(machine, &al, sample) < 0) { --=20 2.41.0