root/tools/perf/lib/tests/test-evlist.c

/* [<][>][^][v][top][bottom][index][help] */

DEFINITIONS

This source file includes following definitions.
  1. libperf_print
  2. test_stat_cpu
  3. test_stat_thread
  4. test_stat_thread_enable
  5. main

   1 // SPDX-License-Identifier: GPL-2.0
   2 #include <stdio.h>
   3 #include <stdarg.h>
   4 #include <linux/perf_event.h>
   5 #include <perf/cpumap.h>
   6 #include <perf/threadmap.h>
   7 #include <perf/evlist.h>
   8 #include <perf/evsel.h>
   9 #include <internal/tests.h>
  10 
  11 static int libperf_print(enum libperf_print_level level,
  12                          const char *fmt, va_list ap)
  13 {
  14         return vfprintf(stderr, fmt, ap);
  15 }
  16 
  17 static int test_stat_cpu(void)
  18 {
  19         struct perf_cpu_map *cpus;
  20         struct perf_evlist *evlist;
  21         struct perf_evsel *evsel;
  22         struct perf_event_attr attr1 = {
  23                 .type   = PERF_TYPE_SOFTWARE,
  24                 .config = PERF_COUNT_SW_CPU_CLOCK,
  25         };
  26         struct perf_event_attr attr2 = {
  27                 .type   = PERF_TYPE_SOFTWARE,
  28                 .config = PERF_COUNT_SW_TASK_CLOCK,
  29         };
  30         int err, cpu, tmp;
  31 
  32         cpus = perf_cpu_map__new(NULL);
  33         __T("failed to create cpus", cpus);
  34 
  35         evlist = perf_evlist__new();
  36         __T("failed to create evlist", evlist);
  37 
  38         evsel = perf_evsel__new(&attr1);
  39         __T("failed to create evsel1", evsel);
  40 
  41         perf_evlist__add(evlist, evsel);
  42 
  43         evsel = perf_evsel__new(&attr2);
  44         __T("failed to create evsel2", evsel);
  45 
  46         perf_evlist__add(evlist, evsel);
  47 
  48         perf_evlist__set_maps(evlist, cpus, NULL);
  49 
  50         err = perf_evlist__open(evlist);
  51         __T("failed to open evsel", err == 0);
  52 
  53         perf_evlist__for_each_evsel(evlist, evsel) {
  54                 cpus = perf_evsel__cpus(evsel);
  55 
  56                 perf_cpu_map__for_each_cpu(cpu, tmp, cpus) {
  57                         struct perf_counts_values counts = { .val = 0 };
  58 
  59                         perf_evsel__read(evsel, cpu, 0, &counts);
  60                         __T("failed to read value for evsel", counts.val != 0);
  61                 }
  62         }
  63 
  64         perf_evlist__close(evlist);
  65         perf_evlist__delete(evlist);
  66 
  67         perf_cpu_map__put(cpus);
  68         return 0;
  69 }
  70 
  71 static int test_stat_thread(void)
  72 {
  73         struct perf_counts_values counts = { .val = 0 };
  74         struct perf_thread_map *threads;
  75         struct perf_evlist *evlist;
  76         struct perf_evsel *evsel;
  77         struct perf_event_attr attr1 = {
  78                 .type   = PERF_TYPE_SOFTWARE,
  79                 .config = PERF_COUNT_SW_CPU_CLOCK,
  80         };
  81         struct perf_event_attr attr2 = {
  82                 .type   = PERF_TYPE_SOFTWARE,
  83                 .config = PERF_COUNT_SW_TASK_CLOCK,
  84         };
  85         int err;
  86 
  87         threads = perf_thread_map__new_dummy();
  88         __T("failed to create threads", threads);
  89 
  90         perf_thread_map__set_pid(threads, 0, 0);
  91 
  92         evlist = perf_evlist__new();
  93         __T("failed to create evlist", evlist);
  94 
  95         evsel = perf_evsel__new(&attr1);
  96         __T("failed to create evsel1", evsel);
  97 
  98         perf_evlist__add(evlist, evsel);
  99 
 100         evsel = perf_evsel__new(&attr2);
 101         __T("failed to create evsel2", evsel);
 102 
 103         perf_evlist__add(evlist, evsel);
 104 
 105         perf_evlist__set_maps(evlist, NULL, threads);
 106 
 107         err = perf_evlist__open(evlist);
 108         __T("failed to open evsel", err == 0);
 109 
 110         perf_evlist__for_each_evsel(evlist, evsel) {
 111                 perf_evsel__read(evsel, 0, 0, &counts);
 112                 __T("failed to read value for evsel", counts.val != 0);
 113         }
 114 
 115         perf_evlist__close(evlist);
 116         perf_evlist__delete(evlist);
 117 
 118         perf_thread_map__put(threads);
 119         return 0;
 120 }
 121 
 122 static int test_stat_thread_enable(void)
 123 {
 124         struct perf_counts_values counts = { .val = 0 };
 125         struct perf_thread_map *threads;
 126         struct perf_evlist *evlist;
 127         struct perf_evsel *evsel;
 128         struct perf_event_attr attr1 = {
 129                 .type     = PERF_TYPE_SOFTWARE,
 130                 .config   = PERF_COUNT_SW_CPU_CLOCK,
 131                 .disabled = 1,
 132         };
 133         struct perf_event_attr attr2 = {
 134                 .type     = PERF_TYPE_SOFTWARE,
 135                 .config   = PERF_COUNT_SW_TASK_CLOCK,
 136                 .disabled = 1,
 137         };
 138         int err;
 139 
 140         threads = perf_thread_map__new_dummy();
 141         __T("failed to create threads", threads);
 142 
 143         perf_thread_map__set_pid(threads, 0, 0);
 144 
 145         evlist = perf_evlist__new();
 146         __T("failed to create evlist", evlist);
 147 
 148         evsel = perf_evsel__new(&attr1);
 149         __T("failed to create evsel1", evsel);
 150 
 151         perf_evlist__add(evlist, evsel);
 152 
 153         evsel = perf_evsel__new(&attr2);
 154         __T("failed to create evsel2", evsel);
 155 
 156         perf_evlist__add(evlist, evsel);
 157 
 158         perf_evlist__set_maps(evlist, NULL, threads);
 159 
 160         err = perf_evlist__open(evlist);
 161         __T("failed to open evsel", err == 0);
 162 
 163         perf_evlist__for_each_evsel(evlist, evsel) {
 164                 perf_evsel__read(evsel, 0, 0, &counts);
 165                 __T("failed to read value for evsel", counts.val == 0);
 166         }
 167 
 168         perf_evlist__enable(evlist);
 169 
 170         perf_evlist__for_each_evsel(evlist, evsel) {
 171                 perf_evsel__read(evsel, 0, 0, &counts);
 172                 __T("failed to read value for evsel", counts.val != 0);
 173         }
 174 
 175         perf_evlist__disable(evlist);
 176 
 177         perf_evlist__close(evlist);
 178         perf_evlist__delete(evlist);
 179 
 180         perf_thread_map__put(threads);
 181         return 0;
 182 }
 183 
 184 int main(int argc, char **argv)
 185 {
 186         __T_START;
 187 
 188         libperf_init(libperf_print);
 189 
 190         test_stat_cpu();
 191         test_stat_thread();
 192         test_stat_thread_enable();
 193 
 194         __T_OK;
 195         return 0;
 196 }

/* [<][>][^][v][top][bottom][index][help] */