~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

TOMOYO Linux Cross Reference
Linux/tools/perf/arch/x86/tests/sample-parsing.c

Version: ~ [ linux-6.11.5 ] ~ [ linux-6.10.14 ] ~ [ linux-6.9.12 ] ~ [ linux-6.8.12 ] ~ [ linux-6.7.12 ] ~ [ linux-6.6.58 ] ~ [ linux-6.5.13 ] ~ [ linux-6.4.16 ] ~ [ linux-6.3.13 ] ~ [ linux-6.2.16 ] ~ [ linux-6.1.114 ] ~ [ linux-6.0.19 ] ~ [ linux-5.19.17 ] ~ [ linux-5.18.19 ] ~ [ linux-5.17.15 ] ~ [ linux-5.16.20 ] ~ [ linux-5.15.169 ] ~ [ linux-5.14.21 ] ~ [ linux-5.13.19 ] ~ [ linux-5.12.19 ] ~ [ linux-5.11.22 ] ~ [ linux-5.10.228 ] ~ [ linux-5.9.16 ] ~ [ linux-5.8.18 ] ~ [ linux-5.7.19 ] ~ [ linux-5.6.19 ] ~ [ linux-5.5.19 ] ~ [ linux-5.4.284 ] ~ [ linux-5.3.18 ] ~ [ linux-5.2.21 ] ~ [ linux-5.1.21 ] ~ [ linux-5.0.21 ] ~ [ linux-4.20.17 ] ~ [ linux-4.19.322 ] ~ [ linux-4.18.20 ] ~ [ linux-4.17.19 ] ~ [ linux-4.16.18 ] ~ [ linux-4.15.18 ] ~ [ linux-4.14.336 ] ~ [ linux-4.13.16 ] ~ [ linux-4.12.14 ] ~ [ linux-4.11.12 ] ~ [ linux-4.10.17 ] ~ [ linux-4.9.337 ] ~ [ linux-4.4.302 ] ~ [ linux-3.10.108 ] ~ [ linux-2.6.32.71 ] ~ [ linux-2.6.0 ] ~ [ linux-2.4.37.11 ] ~ [ unix-v6-master ] ~ [ ccs-tools-1.8.9 ] ~ [ policy-sample ] ~
Architecture: ~ [ i386 ] ~ [ alpha ] ~ [ m68k ] ~ [ mips ] ~ [ ppc ] ~ [ sparc ] ~ [ sparc64 ] ~

  1 // SPDX-License-Identifier: GPL-2.0-only
  2 #include <stdbool.h>
  3 #include <inttypes.h>
  4 #include <stdlib.h>
  5 #include <string.h>
  6 #include <linux/bitops.h>
  7 #include <linux/kernel.h>
  8 #include <linux/types.h>
  9 
 10 #include "event.h"
 11 #include "evsel.h"
 12 #include "debug.h"
 13 #include "util/sample.h"
 14 #include "util/synthetic-events.h"
 15 
 16 #include "tests/tests.h"
 17 #include "arch-tests.h"
 18 
 19 #define COMP(m) do {                                    \
 20         if (s1->m != s2->m) {                           \
 21                 pr_debug("Samples differ at '"#m"'\n"); \
 22                 return false;                           \
 23         }                                               \
 24 } while (0)
 25 
 26 static bool samples_same(const struct perf_sample *s1,
 27                          const struct perf_sample *s2,
 28                          u64 type)
 29 {
 30         if (type & PERF_SAMPLE_WEIGHT_STRUCT) {
 31                 COMP(ins_lat);
 32                 COMP(retire_lat);
 33         }
 34 
 35         return true;
 36 }
 37 
 38 static int do_test(u64 sample_type)
 39 {
 40         struct evsel evsel = {
 41                 .needs_swap = false,
 42                 .core = {
 43                         . attr = {
 44                                 .sample_type = sample_type,
 45                                 .read_format = 0,
 46                         },
 47                 },
 48         };
 49         union perf_event *event;
 50         struct perf_sample sample = {
 51                 .weight         = 101,
 52                 .ins_lat        = 102,
 53                 .retire_lat     = 103,
 54         };
 55         struct perf_sample sample_out;
 56         size_t i, sz, bufsz;
 57         int err, ret = -1;
 58 
 59         sz = perf_event__sample_event_size(&sample, sample_type, 0);
 60         bufsz = sz + 4096; /* Add a bit for overrun checking */
 61         event = malloc(bufsz);
 62         if (!event) {
 63                 pr_debug("malloc failed\n");
 64                 return -1;
 65         }
 66 
 67         memset(event, 0xff, bufsz);
 68         event->header.type = PERF_RECORD_SAMPLE;
 69         event->header.misc = 0;
 70         event->header.size = sz;
 71 
 72         err = perf_event__synthesize_sample(event, sample_type, 0, &sample);
 73         if (err) {
 74                 pr_debug("%s failed for sample_type %#"PRIx64", error %d\n",
 75                          "perf_event__synthesize_sample", sample_type, err);
 76                 goto out_free;
 77         }
 78 
 79         /* The data does not contain 0xff so we use that to check the size */
 80         for (i = bufsz; i > 0; i--) {
 81                 if (*(i - 1 + (u8 *)event) != 0xff)
 82                         break;
 83         }
 84         if (i != sz) {
 85                 pr_debug("Event size mismatch: actual %zu vs expected %zu\n",
 86                          i, sz);
 87                 goto out_free;
 88         }
 89 
 90         evsel.sample_size = __evsel__sample_size(sample_type);
 91 
 92         err = evsel__parse_sample(&evsel, event, &sample_out);
 93         if (err) {
 94                 pr_debug("%s failed for sample_type %#"PRIx64", error %d\n",
 95                          "evsel__parse_sample", sample_type, err);
 96                 goto out_free;
 97         }
 98 
 99         if (!samples_same(&sample, &sample_out, sample_type)) {
100                 pr_debug("parsing failed for sample_type %#"PRIx64"\n",
101                          sample_type);
102                 goto out_free;
103         }
104 
105         ret = 0;
106 out_free:
107         free(event);
108 
109         return ret;
110 }
111 
112 /**
113  * test__x86_sample_parsing - test X86 specific sample parsing
114  *
115  * This function implements a test that synthesizes a sample event, parses it
116  * and then checks that the parsed sample matches the original sample. If the
117  * test passes %0 is returned, otherwise %-1 is returned.
118  *
119  * For now, the PERF_SAMPLE_WEIGHT_STRUCT is the only X86 specific sample type.
120  * The test only checks the PERF_SAMPLE_WEIGHT_STRUCT type.
121  */
122 int test__x86_sample_parsing(struct test_suite *test __maybe_unused, int subtest __maybe_unused)
123 {
124         return do_test(PERF_SAMPLE_WEIGHT_STRUCT);
125 }
126 

~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

kernel.org | git.kernel.org | LWN.net | Project Home | SVN repository | Mail admin

Linux® is a registered trademark of Linus Torvalds in the United States and other countries.
TOMOYO® is a registered trademark of NTT DATA CORPORATION.

sflogo.php