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

TOMOYO Linux Cross Reference
Linux/tools/lib/perf/Documentation/examples/counting.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 #include <linux/perf_event.h>
  2 #include <perf/evlist.h>
  3 #include <perf/evsel.h>
  4 #include <perf/cpumap.h>
  5 #include <perf/threadmap.h>
  6 #include <perf/mmap.h>
  7 #include <perf/core.h>
  8 #include <perf/event.h>
  9 #include <stdio.h>
 10 #include <unistd.h>
 11 
 12 static int libperf_print(enum libperf_print_level level,
 13                          const char *fmt, va_list ap)
 14 {
 15         return vfprintf(stderr, fmt, ap);
 16 }
 17 
 18 int main(int argc, char **argv)
 19 {
 20         int count = 100000, err = 0;
 21         struct perf_evlist *evlist;
 22         struct perf_evsel *evsel;
 23         struct perf_thread_map *threads;
 24         struct perf_counts_values counts;
 25 
 26         struct perf_event_attr attr1 = {
 27                 .type        = PERF_TYPE_SOFTWARE,
 28                 .config      = PERF_COUNT_SW_CPU_CLOCK,
 29                 .read_format = PERF_FORMAT_TOTAL_TIME_ENABLED|PERF_FORMAT_TOTAL_TIME_RUNNING,
 30                 .disabled    = 1,
 31         };
 32         struct perf_event_attr attr2 = {
 33                 .type        = PERF_TYPE_SOFTWARE,
 34                 .config      = PERF_COUNT_SW_TASK_CLOCK,
 35                 .read_format = PERF_FORMAT_TOTAL_TIME_ENABLED|PERF_FORMAT_TOTAL_TIME_RUNNING,
 36                 .disabled    = 1,
 37         };
 38 
 39         libperf_init(libperf_print);
 40         threads = perf_thread_map__new_dummy();
 41         if (!threads) {
 42                 fprintf(stderr, "failed to create threads\n");
 43                 return -1;
 44         }
 45         perf_thread_map__set_pid(threads, 0, 0);
 46         evlist = perf_evlist__new();
 47         if (!evlist) {
 48                 fprintf(stderr, "failed to create evlist\n");
 49                 goto out_threads;
 50         }
 51         evsel = perf_evsel__new(&attr1);
 52         if (!evsel) {
 53                 fprintf(stderr, "failed to create evsel1\n");
 54                 goto out_evlist;
 55         }
 56         perf_evlist__add(evlist, evsel);
 57         evsel = perf_evsel__new(&attr2);
 58         if (!evsel) {
 59                 fprintf(stderr, "failed to create evsel2\n");
 60                 goto out_evlist;
 61         }
 62         perf_evlist__add(evlist, evsel);
 63         perf_evlist__set_maps(evlist, NULL, threads);
 64         err = perf_evlist__open(evlist);
 65         if (err) {
 66                 fprintf(stderr, "failed to open evsel\n");
 67                 goto out_evlist;
 68         }
 69         perf_evlist__enable(evlist);
 70         while (count--);
 71         perf_evlist__disable(evlist);
 72         perf_evlist__for_each_evsel(evlist, evsel) {
 73                 perf_evsel__read(evsel, 0, 0, &counts);
 74                 fprintf(stdout, "count %llu, enabled %llu, run %llu\n",
 75                                 counts.val, counts.ena, counts.run);
 76         }
 77         perf_evlist__close(evlist);
 78 out_evlist:
 79         perf_evlist__delete(evlist);
 80 out_threads:
 81         perf_thread_map__put(threads);
 82         return err;
 83 }
 84 

~ [ 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