File: self_basic.c

package info (click to toggle)
libpfm4 4.11.1%2Bgit32-gd0b85fb-1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 8,724 kB
  • sloc: ansic: 172,126; makefile: 689; python: 183; sh: 17
file content (146 lines) | stat: -rw-r--r-- 4,022 bytes parent folder | download | duplicates (7)
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
/*
 * self-basic.c - example of a simple self monitoring task no-helper
 *
 * Copyright (c) 2010 Google, Inc
 * Contributed by Stephane Eranian <eranian@gmail.com>
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
 * of the Software, and to permit persons to whom the Software is furnished to do so,
 * subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in all
 * copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
 * INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
 * PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
 * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
 * CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE
 * OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 *
 * This file is part of libpfm, a performance monitoring support library for
 * applications on Linux.
 */

#include <sys/types.h>
#include <inttypes.h>
#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>
#include <errno.h>
#include <unistd.h>
#include <string.h>
#include <locale.h>
#include <sys/ioctl.h>
#include <err.h>

#include <perfmon/pfmlib_perf_event.h>

#define N 30

static unsigned long 
fib(unsigned long n)
{
	if (n == 0)
		return 0;
	if (n == 1)
		return 2;
	return fib(n-1)+fib(n-2);
}

int
main(int argc, char **argv)
{
	struct perf_event_attr attr;
	int fd, ret;
	uint64_t count = 0, values[3];

	setlocale(LC_ALL, "");

	/*
	 * Initialize libpfm library (required before we can use it)
	 */
	ret = pfm_initialize();
	if (ret != PFM_SUCCESS)
		errx(1, "cannot initialize library: %s", pfm_strerror(ret));

	memset(&attr, 0, sizeof(attr));

	/*
 	 * 1st argument: event string
 	 * 2nd argument: default privilege level (used if not specified in the event string)
 	 * 3rd argument: the perf_event_attr to initialize
 	 */
	ret = pfm_get_perf_event_encoding("cycles:u", PFM_PLM0|PFM_PLM3, &attr, NULL, NULL);
	if (ret != PFM_SUCCESS)
		errx(1, "cannot find encoding: %s", pfm_strerror(ret));

	/*
	 * request timing information because event may be multiplexed
	 * and thus it may not count all the time. The scaling information
	 * will be used to scale the raw count as if the event had run all
	 * along
	 */
	attr.read_format = PERF_FORMAT_TOTAL_TIME_ENABLED|PERF_FORMAT_TOTAL_TIME_RUNNING;

	/* do not start immediately after perf_event_open() */
	attr.disabled = 1;

	/*
 	 * create the event and attach to self
 	 * Note that it attaches only to the main thread, there is no inheritance
 	 * to threads that may be created subsequently.
 	 *
 	 * if mulithreaded, then getpid() must be replaced by gettid()
 	 */
	fd = perf_event_open(&attr, getpid(), -1, -1, 0);
	if (fd < 0) 
		err(1, "cannot create event");

	/*
 	 * start counting now
 	 */
	ret = ioctl(fd, PERF_EVENT_IOC_ENABLE, 0);
	if (ret)
		err(1, "ioctl(enable) failed");

	printf("Fibonacci(%d)=%lu\n", N, fib(N));

	/*
 	 * stop counting
 	 */
	ret = ioctl(fd, PERF_EVENT_IOC_DISABLE, 0);
	if (ret)
		err(1, "ioctl(disable) failed");

	/*
 	 * read the count + scaling values
 	 *
 	 * It is not necessary to stop an event to read its value
 	 */
	ret = read(fd, values, sizeof(values));
	if (ret != sizeof(values))
		err(1, "cannot read results: %s", strerror(errno));

	/*
 	 * scale count
	 *
	 * values[0] = raw count
	 * values[1] = TIME_ENABLED
	 * values[2] = TIME_RUNNING
 	 */
	if (values[2])
		count = (uint64_t)((double)values[0] * values[1]/values[2]);

	printf("count=%'"PRIu64"\n", count);

	close(fd);

	/* free libpfm resources cleanly */
	pfm_terminate();

	return 0;
}