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

perf tools: Move branch option parsing to own file

.. to allow sharing between builtin-record and builtin-top later. No
code changes, just moved code.

Signed-off-by: Andi Kleen <ak@linux.intel.com>
Cc: Jiri Olsa <jolsa@redhat.com>
Cc: Namhyung Kim <namhyung@kernel.org>
Cc: Stephane Eranian <eranian@google.com>
Link: http://lkml.kernel.org/r/1432749114-904-9-git-send-email-andi@firstfloor.org
[ Rename too generic branch.[ch] name to parse-branch-options.[ch] ]
Signed-off-by: Arnaldo Carvalho de Melo <acme@redhat.com>

authored by

Andi Kleen and committed by
Arnaldo Carvalho de Melo
f00898f4 83be34a7

+100 -88
+1 -88
tools/perf/builtin-record.c
··· 28 28 #include "util/thread_map.h" 29 29 #include "util/data.h" 30 30 #include "util/auxtrace.h" 31 + #include "util/parse-branch-options.h" 31 32 32 33 #include <unistd.h> 33 34 #include <sched.h> ··· 750 749 out_delete_session: 751 750 perf_session__delete(session); 752 751 return status; 753 - } 754 - 755 - #define BRANCH_OPT(n, m) \ 756 - { .name = n, .mode = (m) } 757 - 758 - #define BRANCH_END { .name = NULL } 759 - 760 - struct branch_mode { 761 - const char *name; 762 - int mode; 763 - }; 764 - 765 - static const struct branch_mode branch_modes[] = { 766 - BRANCH_OPT("u", PERF_SAMPLE_BRANCH_USER), 767 - BRANCH_OPT("k", PERF_SAMPLE_BRANCH_KERNEL), 768 - BRANCH_OPT("hv", PERF_SAMPLE_BRANCH_HV), 769 - BRANCH_OPT("any", PERF_SAMPLE_BRANCH_ANY), 770 - BRANCH_OPT("any_call", PERF_SAMPLE_BRANCH_ANY_CALL), 771 - BRANCH_OPT("any_ret", PERF_SAMPLE_BRANCH_ANY_RETURN), 772 - BRANCH_OPT("ind_call", PERF_SAMPLE_BRANCH_IND_CALL), 773 - BRANCH_OPT("abort_tx", PERF_SAMPLE_BRANCH_ABORT_TX), 774 - BRANCH_OPT("in_tx", PERF_SAMPLE_BRANCH_IN_TX), 775 - BRANCH_OPT("no_tx", PERF_SAMPLE_BRANCH_NO_TX), 776 - BRANCH_OPT("cond", PERF_SAMPLE_BRANCH_COND), 777 - BRANCH_END 778 - }; 779 - 780 - static int 781 - parse_branch_stack(const struct option *opt, const char *str, int unset) 782 - { 783 - #define ONLY_PLM \ 784 - (PERF_SAMPLE_BRANCH_USER |\ 785 - PERF_SAMPLE_BRANCH_KERNEL |\ 786 - PERF_SAMPLE_BRANCH_HV) 787 - 788 - uint64_t *mode = (uint64_t *)opt->value; 789 - const struct branch_mode *br; 790 - char *s, *os = NULL, *p; 791 - int ret = -1; 792 - 793 - if (unset) 794 - return 0; 795 - 796 - /* 797 - * cannot set it twice, -b + --branch-filter for instance 798 - */ 799 - if (*mode) 800 - return -1; 801 - 802 - /* str may be NULL in case no arg is passed to -b */ 803 - if (str) { 804 - /* because str is read-only */ 805 - s = os = strdup(str); 806 - if (!s) 807 - return -1; 808 - 809 - for (;;) { 810 - p = strchr(s, ','); 811 - if (p) 812 - *p = '\0'; 813 - 814 - for (br = branch_modes; br->name; br++) { 815 - if (!strcasecmp(s, br->name)) 816 - break; 817 - } 818 - if (!br->name) { 819 - ui__warning("unknown branch filter %s," 820 - " check man page\n", s); 821 - goto error; 822 - } 823 - 824 - *mode |= br->mode; 825 - 826 - if (!p) 827 - break; 828 - 829 - s = p + 1; 830 - } 831 - } 832 - ret = 0; 833 - 834 - /* default to any branch */ 835 - if ((*mode & ~ONLY_PLM) == 0) { 836 - *mode = PERF_SAMPLE_BRANCH_ANY; 837 - } 838 - error: 839 - free(os); 840 - return ret; 841 752 } 842 753 843 754 static void callchain_debug(void)
+1
tools/perf/util/Build
··· 75 75 libperf-y += cloexec.o 76 76 libperf-y += thread-stack.o 77 77 libperf-$(CONFIG_AUXTRACE) += auxtrace.o 78 + libperf-y += parse-branch-options.o 78 79 79 80 libperf-$(CONFIG_LIBELF) += symbol-elf.o 80 81 libperf-$(CONFIG_LIBELF) += probe-event.o
+93
tools/perf/util/parse-branch-options.c
··· 1 + #include "perf.h" 2 + #include "util/util.h" 3 + #include "util/debug.h" 4 + #include "util/parse-options.h" 5 + #include "util/parse-branch-options.h" 6 + 7 + #define BRANCH_OPT(n, m) \ 8 + { .name = n, .mode = (m) } 9 + 10 + #define BRANCH_END { .name = NULL } 11 + 12 + struct branch_mode { 13 + const char *name; 14 + int mode; 15 + }; 16 + 17 + static const struct branch_mode branch_modes[] = { 18 + BRANCH_OPT("u", PERF_SAMPLE_BRANCH_USER), 19 + BRANCH_OPT("k", PERF_SAMPLE_BRANCH_KERNEL), 20 + BRANCH_OPT("hv", PERF_SAMPLE_BRANCH_HV), 21 + BRANCH_OPT("any", PERF_SAMPLE_BRANCH_ANY), 22 + BRANCH_OPT("any_call", PERF_SAMPLE_BRANCH_ANY_CALL), 23 + BRANCH_OPT("any_ret", PERF_SAMPLE_BRANCH_ANY_RETURN), 24 + BRANCH_OPT("ind_call", PERF_SAMPLE_BRANCH_IND_CALL), 25 + BRANCH_OPT("abort_tx", PERF_SAMPLE_BRANCH_ABORT_TX), 26 + BRANCH_OPT("in_tx", PERF_SAMPLE_BRANCH_IN_TX), 27 + BRANCH_OPT("no_tx", PERF_SAMPLE_BRANCH_NO_TX), 28 + BRANCH_OPT("cond", PERF_SAMPLE_BRANCH_COND), 29 + BRANCH_END 30 + }; 31 + 32 + int 33 + parse_branch_stack(const struct option *opt, const char *str, int unset) 34 + { 35 + #define ONLY_PLM \ 36 + (PERF_SAMPLE_BRANCH_USER |\ 37 + PERF_SAMPLE_BRANCH_KERNEL |\ 38 + PERF_SAMPLE_BRANCH_HV) 39 + 40 + uint64_t *mode = (uint64_t *)opt->value; 41 + const struct branch_mode *br; 42 + char *s, *os = NULL, *p; 43 + int ret = -1; 44 + 45 + if (unset) 46 + return 0; 47 + 48 + /* 49 + * cannot set it twice, -b + --branch-filter for instance 50 + */ 51 + if (*mode) 52 + return -1; 53 + 54 + /* str may be NULL in case no arg is passed to -b */ 55 + if (str) { 56 + /* because str is read-only */ 57 + s = os = strdup(str); 58 + if (!s) 59 + return -1; 60 + 61 + for (;;) { 62 + p = strchr(s, ','); 63 + if (p) 64 + *p = '\0'; 65 + 66 + for (br = branch_modes; br->name; br++) { 67 + if (!strcasecmp(s, br->name)) 68 + break; 69 + } 70 + if (!br->name) { 71 + ui__warning("unknown branch filter %s," 72 + " check man page\n", s); 73 + goto error; 74 + } 75 + 76 + *mode |= br->mode; 77 + 78 + if (!p) 79 + break; 80 + 81 + s = p + 1; 82 + } 83 + } 84 + ret = 0; 85 + 86 + /* default to any branch */ 87 + if ((*mode & ~ONLY_PLM) == 0) { 88 + *mode = PERF_SAMPLE_BRANCH_ANY; 89 + } 90 + error: 91 + free(os); 92 + return ret; 93 + }
+5
tools/perf/util/parse-branch-options.h
··· 1 + #ifndef _PERF_PARSE_BRANCH_OPTIONS_H 2 + #define _PERF_PARSE_BRANCH_OPTIONS_H 1 3 + struct option; 4 + int parse_branch_stack(const struct option *opt, const char *str, int unset); 5 + #endif /* _PERF_PARSE_BRANCH_OPTIONS_H */