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

perf util: Add host_is_bigendian to util.h

Avoid libtraceevent dependency for tep_is_bigendian or trace-event.h
dependency for bigendian. Add a new host_is_bigendian to util.h, using
the compiler defined __BYTE_ORDER__ when available.

Committer notes:

Added:

#else /* !__BYTE_ORDER__ */

On that nested #ifdef block, as per Namhyung's suggestion.

Signed-off-by: Ian Rogers <irogers@google.com>
Acked-by: Namhyung Kim <namhyung@kernel.org>
Cc: Adrian Hunter <adrian.hunter@intel.com>
Cc: Alexander Shishkin <alexander.shishkin@linux.intel.com>
Cc: Ingo Molnar <mingo@redhat.com>
Cc: Jiri Olsa <jolsa@kernel.org>
Cc: Kan Liang <kan.liang@linux.intel.com>
Cc: Leo Yan <leo.yan@linaro.org>
Cc: Mark Rutland <mark.rutland@arm.com>
Cc: Masami Hiramatsu <mhiramat@kernel.org>
Cc: Peter Zijlstra <peterz@infradead.org>
Cc: Stephane Eranian <eranian@google.com>
Cc: Steven Rostedt (VMware) <rostedt@goodmis.org>
Link: https://lore.kernel.org/r/20221130062935.2219247-3-irogers@google.com
Signed-off-by: Arnaldo Carvalho de Melo <acme@redhat.com>

authored by

Ian Rogers and committed by
Arnaldo Carvalho de Melo
5b7a29fb fce9a619

+29 -22
+2 -2
tools/perf/tests/code-reading.c
··· 16 16 #include "dso.h" 17 17 #include "env.h" 18 18 #include "parse-events.h" 19 - #include "trace-event.h" 20 19 #include "evlist.h" 21 20 #include "evsel.h" 22 21 #include "thread_map.h" ··· 27 28 #include "util/mmap.h" 28 29 #include "util/string2.h" 29 30 #include "util/synthetic-events.h" 31 + #include "util/util.h" 30 32 #include "thread.h" 31 33 32 34 #include "tests.h" ··· 79 79 * see disassemble_bytes() at binutils/objdump.c for details 80 80 * how objdump chooses display endian) 81 81 */ 82 - if (bytes_read > 1 && !bigendian()) { 82 + if (bytes_read > 1 && !host_is_bigendian()) { 83 83 unsigned char *chunk_end = chunk_start + bytes_read - 1; 84 84 unsigned char tmp; 85 85
+2 -2
tools/perf/tests/sample-parsing.c
··· 13 13 #include "evsel.h" 14 14 #include "debug.h" 15 15 #include "util/synthetic-events.h" 16 - #include "util/trace-event.h" 16 + #include "util/util.h" 17 17 18 18 #include "tests.h" 19 19 ··· 117 117 COMP(branch_stack->hw_idx); 118 118 for (i = 0; i < s1->branch_stack->nr; i++) { 119 119 if (needs_swap) 120 - return ((tep_is_bigendian()) ? 120 + return ((host_is_bigendian()) ? 121 121 (FLAG(s2).value == BS_EXPECTED_BE) : 122 122 (FLAG(s2).value == BS_EXPECTED_LE)); 123 123 else
+1 -4
tools/perf/util/evsel.c
··· 2320 2320 * as it has variable bit-field sizes. Instead the 2321 2321 * macro takes the bit-field position/size, 2322 2322 * swaps it based on the host endianness. 2323 - * 2324 - * tep_is_bigendian() is used here instead of 2325 - * bigendian() to avoid python test fails. 2326 2323 */ 2327 - if (tep_is_bigendian()) { 2324 + if (host_is_bigendian()) { 2328 2325 new_val = bitfield_swap(value, 0, 1); 2329 2326 new_val |= bitfield_swap(value, 1, 1); 2330 2327 new_val |= bitfield_swap(value, 2, 1);
+3 -11
tools/perf/util/trace-event-info.c
··· 26 26 #include <api/fs/tracing_path.h> 27 27 #include "evsel.h" 28 28 #include "debug.h" 29 + #include "util.h" 29 30 30 31 #define VERSION "0.6" 31 32 #define MAX_EVENT_LENGTH 512 ··· 38 37 char *name; 39 38 struct tracepoint_path *next; 40 39 }; 41 - 42 - int bigendian(void) 43 - { 44 - unsigned char str[] = { 0x1, 0x2, 0x3, 0x4, 0x0, 0x0, 0x0, 0x0}; 45 - unsigned int *ptr; 46 - 47 - ptr = (unsigned int *)(void *)str; 48 - return *ptr == 0x01020304; 49 - } 50 40 51 41 /* unfortunately, you can not stat debugfs or proc files for size */ 52 42 static int record_file(const char *file, ssize_t hdr_sz) ··· 71 79 72 80 /* ugh, handle big-endian hdr_size == 4 */ 73 81 sizep = (char*)&size; 74 - if (bigendian()) 82 + if (host_is_bigendian()) 75 83 sizep += sizeof(u64) - hdr_sz; 76 84 77 85 if (hdr_sz && pwrite(output_fd, sizep, hdr_sz, hdr_pos) < 0) { ··· 556 564 return -1; 557 565 558 566 /* save endian */ 559 - if (bigendian()) 567 + if (host_is_bigendian()) 560 568 buf[0] = 1; 561 569 else 562 570 buf[0] = 0;
+2 -1
tools/perf/util/trace-event-read.c
··· 17 17 18 18 #include "trace-event.h" 19 19 #include "debug.h" 20 + #include "util.h" 20 21 21 22 static int input_fd; 22 23 ··· 415 414 return -1; 416 415 } 417 416 file_bigendian = buf[0]; 418 - host_bigendian = bigendian(); 417 + host_bigendian = host_is_bigendian() ? 1 : 0; 419 418 420 419 if (trace_event__init(tevent)) { 421 420 pr_debug("trace_event__init failed");
-2
tools/perf/util/trace-event.h
··· 27 27 28 28 struct tep_event *trace_event__tp_format_id(int id); 29 29 30 - int bigendian(void); 31 - 32 30 void event_format__fprintf(struct tep_event *event, 33 31 int cpu, void *data, int size, FILE *fp); 34 32
+19
tools/perf/util/util.h
··· 94 94 0; \ 95 95 }) 96 96 97 + static inline bool host_is_bigendian(void) 98 + { 99 + #ifdef __BYTE_ORDER__ 100 + #if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__ 101 + return false; 102 + #elif __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__ 103 + return true; 104 + #else 105 + #error "Unrecognized __BYTE_ORDER__" 106 + #endif 107 + #else /* !__BYTE_ORDER__ */ 108 + unsigned char str[] = { 0x1, 0x2, 0x3, 0x4, 0x0, 0x0, 0x0, 0x0}; 109 + unsigned int *ptr; 110 + 111 + ptr = (unsigned int *)(void *)str; 112 + return *ptr == 0x01020304; 113 + #endif 114 + } 115 + 97 116 #endif /* __PERF_UTIL_H */