Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

perf util: Add evsel__taskstate() to parse the task state info instead

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 <zegao@tencent.com>
Cc: Steven Rostedt <rostedt@goodmis.org>
Link: https://lore.kernel.org/r/20240122070859.1394479-5-zegao@tencent.com
Signed-off-by: Namhyung Kim <namhyung@kernel.org>

authored by

Ze Gao and committed by
Namhyung Kim
df8bc77e 2f29a74f

+36 -1
+35 -1
tools/perf/util/evsel.c
··· 2939 2939 return sym; 2940 2940 } 2941 2941 2942 - static __maybe_unused const char *get_states(struct tep_format_field *prev_state_field) 2942 + static const char *get_states(struct tep_format_field *prev_state_field) 2943 2943 { 2944 2944 struct tep_print_flag_sym *sym; 2945 2945 struct tep_print_arg *arg; ··· 2962 2962 return convert_sym(sym); 2963 2963 } 2964 2964 return NULL; 2965 + } 2966 + 2967 + char evsel__taskstate(struct evsel *evsel, struct perf_sample *sample, const char *name) 2968 + { 2969 + static struct tep_format_field *prev_state_field; 2970 + static const char *states; 2971 + struct tep_format_field *field; 2972 + unsigned long long val; 2973 + unsigned int bit; 2974 + char state = '?'; /* '?' denotes unknown task state */ 2975 + 2976 + field = evsel__field(evsel, name); 2977 + 2978 + if (!field) 2979 + return state; 2980 + 2981 + if (!states || field != prev_state_field) { 2982 + states = get_states(field); 2983 + if (!states) 2984 + return state; 2985 + prev_state_field = field; 2986 + } 2987 + 2988 + /* 2989 + * Note since the kernel exposes TASK_REPORT_MAX to userspace 2990 + * to denote the 'preempted' state, we might as welll report 2991 + * 'R' for this case, which make senses to users as well. 2992 + * 2993 + * We can change this if we have a good reason in the future. 2994 + */ 2995 + val = evsel__intval(evsel, sample, name); 2996 + bit = val ? ffs(val) : 0; 2997 + state = (!bit || bit > strlen(states)) ? 'R' : states[bit-1]; 2998 + return state; 2965 2999 } 2966 3000 #endif 2967 3001
+1
tools/perf/util/evsel.h
··· 339 339 void *evsel__rawptr(struct evsel *evsel, struct perf_sample *sample, const char *name); 340 340 u64 evsel__intval(struct evsel *evsel, struct perf_sample *sample, const char *name); 341 341 u64 evsel__intval_common(struct evsel *evsel, struct perf_sample *sample, const char *name); 342 + char evsel__taskstate(struct evsel *evsel, struct perf_sample *sample, const char *name); 342 343 343 344 static inline char *evsel__strval(struct evsel *evsel, struct perf_sample *sample, const char *name) 344 345 {