File: syst.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 (223 lines) | stat: -rw-r--r-- 5,542 bytes parent folder | download | duplicates (14)
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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
/*
 * syst.c - example of a simple system wide monitoring program
 *
 * Copyright (c) 2002-2006 Hewlett-Packard Development Company, L.P.
 * Contributed by Stephane Eranian <eranian@hpl.hp.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.
 */
#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 <sys/ioctl.h>
#include <err.h>

#include "perf_util.h"

typedef struct {
	const char *events;
	int delay;
	int excl;
	int cpu;
	int group;
} options_t;

static options_t options;
static perf_event_desc_t **all_fds;
static int *num_fds;

void
setup_cpu(int cpu)
{
	perf_event_desc_t *fds;
	int i, ret;

	ret = perf_setup_list_events(options.events, &all_fds[cpu], &num_fds[cpu]);
	if (ret || (num_fds == 0))
		errx(1, "cannot setup events\n");
	fds = all_fds[cpu]; /* temp */

	fds[0].fd = -1;
	for(i=0; i < num_fds[cpu]; i++) {
		fds[i].hw.disabled = options.group ? !i : 1;

		if (options.excl && ((options.group && !i) || (!options.group)))
			fds[i].hw.exclusive = 1;
			
		fds[i].hw.disabled = options.group ? !i : 1;

		/* request timing information necessary for scaling counts */
		fds[i].hw.read_format = PERF_FORMAT_SCALE;
		fds[i].fd = perf_event_open(&fds[i].hw, -1, cpu, (options.group ? fds[0].fd : -1), 0);
		if (fds[i].fd == -1)
			err(1, "cannot attach event to CPU%d %s", cpu, fds[i].name);
	}
}

void
measure(void)
{
	perf_event_desc_t *fds;
	long lret;
	int c, cmin, cmax, ncpus;
	int i, ret, l;

	printf("<press CTRL-C to quit before %ds time limit>\n", options.delay);

	cmin = 0;

	lret = sysconf(_SC_NPROCESSORS_ONLN);
	if (lret < 0)
		err(1, "cannot get number of online processors");

	cmax = (int)lret;

	ncpus = cmax;
	if (options.cpu != -1) {
		cmin = options.cpu;
		cmax = cmin + 1;
	}
	all_fds = calloc(ncpus, sizeof(perf_event_desc_t *));
	num_fds = calloc(ncpus, sizeof(int));

	if (!all_fds || !num_fds)
		err(1, "cannot allocate memory for internal structures");
	for(c=cmin ; c < cmax; c++)
		setup_cpu(c);

	/*
	 * FIX this for hotplug CPU
	 */
	for(c=cmin ; c < cmax; c++) {
		fds = all_fds[c];
		if (options.group) 
			ret = ioctl(fds[0].fd, PERF_EVENT_IOC_ENABLE, 0);
		else for(i=0; i < num_fds[c]; i++) {
			ret = ioctl(fds[i].fd, PERF_EVENT_IOC_ENABLE, 0);
			if (ret)
				err(1, "cannot enable event %s\n", fds[i].name);
		}
	}

	for(l=0; l < options.delay; l++) {

		sleep(1);

		puts("------------------------");
		for(c = cmin; c < cmax; c++) {
			fds = all_fds[c];
			for(i=0; i < num_fds[c]; i++) {
				uint64_t val, delta;
				double ratio;

				ret = read(fds[i].fd, fds[i].values, sizeof(fds[i].values));
				if (ret != sizeof(fds[i].values)) {
					if (ret == -1)
						err(1, "cannot read event %d:%d", i, ret);
					else
						warnx("could not read event%d", i);
				}

				/*
				 * scaling because we may be sharing the PMU and
				 * thus may be multiplexed
				 */
				val = perf_scale(fds[i].values);
				ratio = perf_scale_ratio(fds[i].values);
				delta = perf_scale_delta(fds[i].values, fds[i].prev_values);

				printf("CPU%d val=%-20"PRIu64" %-20"PRIu64" raw=%"PRIu64" ena=%"PRIu64" run=%"PRIu64" ratio=%.2f %s\n",
					c,
					val,
					delta,
					fds[i].values[0],
					fds[i].values[1], fds[i].values[2], ratio,
					fds[i].name);
				fds[i].prev_values[0] = fds[i].values[0];
				fds[i].prev_values[1] = fds[i].values[1];
				fds[i].prev_values[2] = fds[i].values[2];
			}
		}
	}
	for(c = cmin; c < cmax; c++) {
		fds = all_fds[c];
		for(i=0; i < num_fds[c]; i++)
			close(fds[i].fd);
		perf_free_fds(fds, num_fds[c]);
	}
}

static void
usage(void)
{
	printf("usage: syst [-c cpu] [-x] [-h] [-d delay] [-g] [-e event1,event2,...]\n");
}

int
main(int argc, char **argv)
{
	int c, ret;

	options.cpu = -1;

	while ((c=getopt(argc, argv,"hc:e:d:gx")) != -1) {
		switch(c) {
			case 'x':
				options.excl = 1;
				break;
			case 'e':
				options.events = optarg;
				break;
			case 'c':
				options.cpu = atoi(optarg);
				break;
			case 'g':
				options.group = 1;
				break;
			case 'd':
				options.delay = atoi(optarg);
				break;
			case 'h':
				usage();
				exit(0);
			default:
				errx(1, "unknown error");
		}
	}
	if (!options.delay)
		options.delay = 20;

	if (!options.events)
		options.events = "cycles,instructions";

	ret = pfm_initialize();
	if (ret != PFM_SUCCESS)
		errx(1, "libpfm initialization failed: %s\n", pfm_strerror(ret));
	
	measure();

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

	return 0;
}