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
|
/* 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>
static int fddev = -1;
__attribute__((constructor)) static void
init(void)
{
static struct perf_event_attr attr;
attr.type = PERF_TYPE_HARDWARE;
attr.config = PERF_COUNT_HW_CPU_CYCLES;
fddev = syscall(__NR_perf_event_open, &attr, 0, -1, -1, 0);
}
__attribute__((destructor)) static void
fini(void)
{
close(fddev);
}
static inline long long
cpucycles(void)
{
long long result = 0;
if (read(fddev, &result, sizeof(result)) < sizeof(result))
return 0;
return result;
}
/* Simple 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;
}
int
main(int ac, char **av)
{
long long time_start = 0;
long long time_end = 0;
int *a = NULL;
int *b = NULL;
int len = 0;
int i, sum = 0;
if (ac != 2)
return -1;
len = atoi(av[1]);
printf("%s: len = %d\n", av[0], len);
a = malloc(len*sizeof(*a));
b = malloc(len*sizeof(*b));
for (i = 0; i < len; ++i) {
a[i] = i+128;
b[i] = i+64;
}
printf("%s: beginning loop\n", av[0]);
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();
/* ---------------------------------- */
printf("sum = %d; Avg time delta[Loop + Read] = %llu\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 time delta[Loop]\t = %llu\n", sum,
(time_end - time_start)/1000);
free(a);
free(b);
return 0;
}
|