summaryrefslogtreecommitdiff
path: root/perf_ev_open.c
blob: 9b06586b78f47cc70be3670b17a06fd173e8d32e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
/* Copyright (c) 2015, Linaro Limited
 * All rights reserved.
 *
 * SPDX-License-Identifier:	BSD-3-Clause
 *
 * This program is used to read perf cycle counter using 
 * perf_event_open/read syscall
 * 
 */

#define _GNU_SOURCE
#include <stdio.h>
#include <stdint.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/syscall.h>
#include <linux/perf_event.h>
#include <getopt.h>

static int fddev = -1;

static int  counter_init(unsigned int counter)
{
	static struct perf_event_attr attr;
	attr.type = PERF_TYPE_HARDWARE;
	if (counter < 0 || counter > PERF_COUNT_HW_MAX)
		return -1;

	attr.config = counter;
	fddev = syscall(__NR_perf_event_open, &attr, 0, -1, -1, 0);
	if (fddev == -1) {
		fprintf(stderr,
			"Error opening perf_event_open for counter %llx\n"
			, attr.config);
		return -1;
	}

	return 0;
}

static inline long long cpucycles(void)
{
	long long result = 0;
	if (read(fddev, &result, sizeof(result)) < sizeof(result))
		return 0;

	return result;
}

/* loop body to keep things interested. Make sure it gets inlined. */
static inline int
loop(int* __restrict__ a, int* __restrict__ b, int n)
{
	unsigned sum = 0;
	int i = 0;
	for (i = 0; i < n; ++i)
		if (a[i] > b[i])
			sum += a[i] + 5;

	return sum;
}

void print_usage(char *argv)
{
	printf("Usage: %s -c perf_hw_counter -n length\n"
	"%s - read perf hardware counters from userspace\n\n"
	"-c\t perf_hw_counter is enum value of hw counter for platform\n"
	"Refer to 'enum perf_hw_id'[file <linux/perf_event.h>] for hw\n"
	"counter enum value in range of 0 to PERF_COUNT_HW_MAX\n"
	"-n\t Length size array will implement busyloop\n"
	"e.g.\n"
	"\t%s -c 0 -n 64\n", argv, argv, argv);
}

int main(int ac, char *argv[])
{
	long long time_start = 0;
	long long time_end   = 0;
	int option = 0;
	int len = -1, cnt = -1;
	int *a  = NULL;
	int *b  = NULL;
	int i, result, sum = 0;

	while ((option = getopt(ac, argv, "c:n:")) != -1) {
		switch (option) {
		case 'c':
			cnt = atoi(optarg);
			break;
		case 'n':
			len = atoi(optarg);
			break;
		default:
			print_usage(argv[0]);
			exit(EXIT_FAILURE);
		}
	}

	if (len == -1 || cnt == -1) {
		print_usage(argv[0]);
		exit(EXIT_FAILURE);
	}

	if (counter_init(cnt) < 0) {
		printf("Error: Counter Invalid\n");
		print_usage(argv[0]);
		exit(EXIT_FAILURE);
	}

	a = malloc(len*sizeof(*a));
	b = malloc(len*sizeof(*b));

	for (i = 0; i < len; ++i) {
		a[i] = i+128;
		b[i] = i+64;
	}

	time_start = time_end = 0;
	/* --------------------Critical section-------------- */
	time_start = cpucycles();
	for (i = 0; i < 1000; i++) {
		sum = loop(a, b, len);
		cpucycles();
	}

	time_end = cpucycles();
	/* ---------------------------------- */
	result = (time_end-time_start)/1000;
	printf("\nsum=%d Avg count [ Loop + Read ] = %ld\n", sum,
	       (time_end-time_start)/1000);
	time_start = time_end = 0;
	/* --------------------Critical section-------------- */
	time_start = cpucycles();
	for (i = 0; i < 1000; i++)
		sum = loop(a, b, len);

	time_end = cpucycles();
	/* ---------------------------------- */
	printf("sum=%d Avg count [ Loop        ] = %ld\n", sum,
	       (time_end-time_start)/1000);
	printf("\n--------------------------------------------------------\n");
	printf("\tDelay[cpucycles]=%d", result-((time_end-time_start)/1000));
	printf("\n--------------------------------------------------------\n");
	free(a);
	free(b);
	close(fddev);
	return 0;
}