cachepc-linux

Fork of AMDESE/linux with modifications for CachePC side-channel attack
git clone https://git.sinitax.com/sinitax/cachepc-linux
Log | Files | Refs | README | LICENSE | sfeed.txt

counting.c (2197B)


      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
     12static int libperf_print(enum libperf_print_level level,
     13                         const char *fmt, va_list ap)
     14{
     15	return vfprintf(stderr, fmt, ap);
     16}
     17
     18int 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);
     78out_evlist:
     79	perf_evlist__delete(evlist);
     80out_threads:
     81	perf_thread_map__put(threads);
     82	return err;
     83}