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

perf util: Add helpers to parse task state string from libtraceevent

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@rorschach.local.home/

Signed-off-by: Steven Rostedt (Google) <rostedt@goodmis.org>
Signed-off-by: Ze Gao <zegao@tencent.com>
Link: https://lore.kernel.org/r/20240122070859.1394479-4-zegao@tencent.com
Signed-off-by: Namhyung Kim <namhyung@kernel.org>

authored by

Ze Gao and committed by
Namhyung Kim
2f29a74f ccc606a7

+112
+112
tools/perf/util/evsel.c
··· 2851 2851 return field ? format_field__intval(field, sample, evsel->needs_swap) : 0; 2852 2852 } 2853 2853 2854 + /* 2855 + * prev_state is of size long, which is 32 bits on 32 bit architectures. 2856 + * As it needs to have the same bits for both 32 bit and 64 bit architectures 2857 + * we can just assume that the flags we care about will all be within 2858 + * the 32 bits. 2859 + */ 2860 + #define MAX_STATE_BITS 32 2861 + 2862 + static const char *convert_sym(struct tep_print_flag_sym *sym) 2863 + { 2864 + static char save_states[MAX_STATE_BITS + 1]; 2865 + 2866 + memset(save_states, 0, sizeof(save_states)); 2867 + 2868 + /* This is the flags for the prev_state_field, now make them into a string */ 2869 + for (; sym; sym = sym->next) { 2870 + long bitmask = strtoul(sym->value, NULL, 0); 2871 + int i; 2872 + 2873 + for (i = 0; !(bitmask & 1); i++) 2874 + bitmask >>= 1; 2875 + 2876 + if (i >= MAX_STATE_BITS) 2877 + continue; 2878 + 2879 + save_states[i] = sym->str[0]; 2880 + } 2881 + 2882 + return save_states; 2883 + } 2884 + 2885 + static struct tep_print_arg_field * 2886 + find_arg_field(struct tep_format_field *prev_state_field, struct tep_print_arg *arg) 2887 + { 2888 + struct tep_print_arg_field *field; 2889 + 2890 + if (!arg) 2891 + return NULL; 2892 + 2893 + if (arg->type == TEP_PRINT_FIELD) 2894 + return &arg->field; 2895 + 2896 + if (arg->type == TEP_PRINT_OP) { 2897 + field = find_arg_field(prev_state_field, arg->op.left); 2898 + if (field && field->field == prev_state_field) 2899 + return field; 2900 + field = find_arg_field(prev_state_field, arg->op.right); 2901 + if (field && field->field == prev_state_field) 2902 + return field; 2903 + } 2904 + return NULL; 2905 + } 2906 + 2907 + static struct tep_print_flag_sym * 2908 + test_flags(struct tep_format_field *prev_state_field, struct tep_print_arg *arg) 2909 + { 2910 + struct tep_print_arg_field *field; 2911 + 2912 + field = find_arg_field(prev_state_field, arg->flags.field); 2913 + if (!field) 2914 + return NULL; 2915 + 2916 + return arg->flags.flags; 2917 + } 2918 + 2919 + static struct tep_print_flag_sym * 2920 + search_op(struct tep_format_field *prev_state_field, struct tep_print_arg *arg) 2921 + { 2922 + struct tep_print_flag_sym *sym = NULL; 2923 + 2924 + if (!arg) 2925 + return NULL; 2926 + 2927 + if (arg->type == TEP_PRINT_OP) { 2928 + sym = search_op(prev_state_field, arg->op.left); 2929 + if (sym) 2930 + return sym; 2931 + 2932 + sym = search_op(prev_state_field, arg->op.right); 2933 + if (sym) 2934 + return sym; 2935 + } else if (arg->type == TEP_PRINT_FLAGS) { 2936 + sym = test_flags(prev_state_field, arg); 2937 + } 2938 + 2939 + return sym; 2940 + } 2941 + 2942 + static __maybe_unused const char *get_states(struct tep_format_field *prev_state_field) 2943 + { 2944 + struct tep_print_flag_sym *sym; 2945 + struct tep_print_arg *arg; 2946 + struct tep_event *event; 2947 + 2948 + event = prev_state_field->event; 2949 + 2950 + /* 2951 + * Look at the event format fields, and search for where 2952 + * the prev_state is parsed via the format flags. 2953 + */ 2954 + for (arg = event->print_fmt.args; arg; arg = arg->next) { 2955 + /* 2956 + * Currently, the __print_flags() for the prev_state 2957 + * is embedded in operations, so they too must be 2958 + * searched. 2959 + */ 2960 + sym = search_op(prev_state_field, arg); 2961 + if (sym) 2962 + return convert_sym(sym); 2963 + } 2964 + return NULL; 2965 + } 2854 2966 #endif 2855 2967 2856 2968 bool evsel__fallback(struct evsel *evsel, struct target *target, int err,