File: self_smpl_multi.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 (494 lines) | stat: -rw-r--r-- 11,542 bytes parent folder | download | duplicates (4)
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
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
/*
 *
 * self_smpl_multi.c - multi-thread self-sampling program
 *
 * Copyright (c) 2009  Google, Inc
 * Modified by Stephane Eranian <eranian@gmail.com>
 *
 * Based on:
 * Copyright (c) 2008 Mark W. Krentel
 * Contributed by Mark W. Krentel <krentel@cs.rice.edu>
 * Modified 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.
 *
 *  Test perfmon overflow without PAPI.
 *
 *  Create a new thread, launch perfmon overflow counters in both
 *  threads, print the number of interrupts per thread and per second,
 *  and look for anomalous interrupts.  Look for mismatched thread
 *  ids, bad message type, or failed pfm_restart().
 *
 *  self_smpl_multi is a test program to stress signal delivery in the context
 *  of a multi-threaded self-sampling program which is common with PAPI and HPC.
 * 
 *  There is an issue with existing (as of 2.6.30) kernel which do not provide
 *  a reliable way of having the signal delivered to the thread in which the
 *  counter overflow occurred. This is problematic for many self-monitoring
 *  program.
 *
 *  This program demonstrates the issue by tracking the number of times
 *  the signal goes to the wrong thread. The bad behavior is exacerbated
 *  if the monitored threads, themselves, already use signals. Here we
 *  use SIGLARM.
 *
 *  Note that kernel developers have been made aware of this problem and
 *  a fix has been proposed. It introduces a new F_SETOWN_EX command to
 *  fcntl().
 */
#include <sys/time.h>
#include <sys/types.h>
#include <err.h>
#include <errno.h>
#include <fcntl.h>
#include <pthread.h>
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <getopt.h>
#include <sys/ioctl.h>
#include <sys/mman.h>

#include "perf_util.h"

#define PROGRAM_TIME  8
#define THRESHOLD  20000000

static int program_time = PROGRAM_TIME;
static int threshold = THRESHOLD;
static int signum = SIGIO;
static pthread_barrier_t barrier;


static int buffer_pages = 1;

#define MAX_THR  128

/*
 *  the following definitions come
 *  from the F_SETOWN_EX patch from Peter Zijlstra
 * Check out: http://lkml.org/lkml/2009/8/4/128 
 */
#ifndef F_SETOWN_EX
#define F_SETOWN_EX	15
#define F_GETOWN_EX	16

#define F_OWNER_TID	0
#define F_OWNER_PID	1
#define F_OWNER_PGRP	2

struct f_owner_ex {
	int	type;
	pid_t	pid;
};
#endif

struct over_args {
	int fd;
	pid_t tid;
	int id;
	perf_event_desc_t *fds;
};

struct over_args fd2ov[MAX_THR];

long count[MAX_THR];
long total[MAX_THR];
long iter[MAX_THR];
long mismatch[MAX_THR];
long bad_msg[MAX_THR];
long bad_restart[MAX_THR];
int fown;

static __thread int myid; /* TLS */
static __thread perf_event_desc_t *fds; /* TLS */
static __thread int num_fds; /* TLS */

pid_t
gettid(void)
{
	return (pid_t)syscall(__NR_gettid);
}

void
user_callback(int m)
{
	count[m]++;
	total[m]++;
}

void
do_cycles(void)
{
	struct timeval start, last, now;
	unsigned long x, sum;

	gettimeofday(&start, NULL);
	last = start;
	count[myid] = 0;
	total[myid] = 0;
	iter[myid] = 0;

	do {

		sum = 1;
		for (x = 1; x < 250000; x++) {
			/* signal pending to private queue because of
			 * pthread_kill(), i.e., tkill()
			 */
			if ((x % 5000) == 0)
				pthread_kill(pthread_self(), SIGUSR1);
			sum += x;
		}
		iter[myid]++;

		gettimeofday(&now, NULL);
		if (now.tv_sec > last.tv_sec) {
			printf("%ld: myid = %3d, fd = %3d, count = %4ld, iter = %4ld, rate = %ld/Kiter\n",
				(long)(now.tv_sec - start.tv_sec),
				myid,
				fd2ov[myid].fd,
				count[myid], iter[myid],
				(1000 * count[myid])/iter[myid]);

			count[myid] = 0;
			iter[myid] = 0;
			last = now;
		}
	} while (now.tv_sec < start.tv_sec + program_time);
}

#define DPRINT(str)   \
printf("(%s) si->fd = %d, ov->self = 0x%lx, self = 0x%lx\n",   \
       str, fd, (unsigned long)ov->self, (unsigned long)self)
void
sigusr1_handler(int sig, siginfo_t *info, void *context)
{
}

/*
 * a signal handler cannot safely invoke printf()
 */
void
sigio_handler(int sig, siginfo_t *info, void *context)
{
	perf_event_desc_t *fdx;
	struct perf_event_header ehdr;
	struct over_args *ov;
	int fd, i, ret;
	pid_t tid;

	/*
	 * positive si_code indicate kernel generated signal
	 * which is normal for SIGIO
	 */
	if (info->si_code < 0)
		errx(1, "signal not generated by kernel");

	/*
	 * SIGPOLL = SIGIO
	 * expect POLL_HUP instead of POLL_IN because we are
	 * in one-shot mode (IOC_REFRESH)
	 */
	if (info->si_code != POLL_HUP)
		errx(1, "signal not generated by SIGIO: %d", info->si_code);

	fd = info->si_fd;
 	tid = gettid();

	for(i=0; i < MAX_THR; i++)
		if (fd2ov[i].fd == fd)
			break;

	if (i == MAX_THR)
		errx(1, "bad info.si_fd: %d", fd);

 	ov = &fd2ov[i];

	/*
 	 * current thread id may not always match the id
 	 * associated with the file descriptor
 	 *
 	 * We need to use the other's thread fds info
 	 * otherwise, it is going to get stuck with no
 	 * more samples generated
 	 */
	if (tid != ov->tid) {
		mismatch[myid]++;
		fdx = ov->fds;
	} else {
		fdx = fds;
	}

	/*
 	 * read sample header
 	 */
	ret = perf_read_buffer(fdx+0, &ehdr, sizeof(ehdr));
	if (ret) {
		errx(1, "cannot read event header");
	}

	/*
	 * message we do not handle
	 */
	if (ehdr.type != PERF_RECORD_SAMPLE) {
		bad_msg[myid]++;
		goto skip;
	}
	user_callback(myid);
skip:
	/* mark sample as consumed */
	perf_skip_buffer(fdx+0, ehdr.size);

	/*
	 * re-arm period, next notification after wakeup_events
	 */
	ret = ioctl(fd, PERF_EVENT_IOC_REFRESH, 1);
	if (ret)
		err(1, "cannot refresh");
}

void
overflow_start(char *name)
{
	struct f_owner_ex fown_ex;
	struct over_args *ov;
	size_t pgsz;
	int ret, fd, flags;

	fds = NULL;
	num_fds = 0;
	ret = perf_setup_list_events("cycles:u", &fds, &num_fds);
	if (ret || !num_fds)
		errx(1, "cannot monitor event");

	pgsz = sysconf(_SC_PAGESIZE);
	ov = &fd2ov[myid];

	/* do not enable now */
	fds[0].hw.disabled = 1;

	/* notify after 1 sample */
	fds[0].hw.wakeup_events = 1;
	fds[0].hw.sample_type = PERF_SAMPLE_IP;
	fds[0].hw.sample_period = threshold;
	fds[0].hw.read_format = 0;

	fds[0].fd = fd = perf_event_open(&fds[0].hw, gettid(), -1, -1, 0);
	if (fd == -1)
		err(1, "cannot attach event %s", fds[0].name);

	ov->fd = fd;
	ov->tid = gettid();
	ov->id = myid;
	ov->fds = fds;

	flags = fcntl(fd, F_GETFL, 0);
	if (fcntl(fd, F_SETFL, flags | O_ASYNC) < 0)
		err(1, "fcntl SETFL failed");

	fown_ex.type = F_OWNER_TID;
	fown_ex.pid  = gettid();
	ret = fcntl(fd,
		    (fown ? F_SETOWN_EX : F_SETOWN), 
		    (fown ? (unsigned long)&fown_ex: (unsigned long)gettid()));
	if (ret)
		err(1, "fcntl SETOWN failed");

	if (fcntl(fd, F_SETSIG, signum) < 0)
		err(1, "fcntl SETSIG failed");

	fds[0].buf = mmap(NULL, (buffer_pages + 1)* pgsz, PROT_READ|PROT_WRITE, MAP_SHARED, fd, 0);
	if (fds[0].buf == MAP_FAILED)
		err(1, "cannot mmap buffer");
	
	fds[0].pgmsk = (buffer_pages * pgsz) - 1;

	printf("launch %s: fd: %d, tid: %d\n", name, fd, ov->tid);

	/*
 	 * activate event for wakeup_events (samples)
 	 */
	ret = ioctl(fd, PERF_EVENT_IOC_REFRESH , 1);
	if (ret == -1)
		err(1, "cannot refresh");

}

void
overflow_stop(void)
{
	int ret;
	ret = ioctl(fd2ov[myid].fd, PERF_EVENT_IOC_DISABLE, 0);
	if (ret)
		err(1, "cannot stop");
}

void *
my_thread(void *v)
{
	int retval = 0;
	
	myid = (unsigned long)v;

	pthread_barrier_wait(&barrier);

	overflow_start("side");
	do_cycles();
	overflow_stop();

	perf_free_fds(fds, num_fds);

	pthread_exit((void *)&retval);
}

static void
usage(void)
{
	printf("self_smpl_multi [-t secs] [-p period] [-s signal] [-f] [-n threads]\n"
		"-t secs: duration of the run in seconds\n"
		"-p period: sampling period in CPU cycles\n"
		"-s signal: signal to use (default: SIGIO)\n"
		"-n thread: number of threads to create (default: 1)\n"
		"-f : use F_SETOWN_EX for correct delivery of signal to thread (default: off)\n");
}

/*
 *  Program args: program_time, threshold, signum.
 */
int
main(int argc, char **argv)
{
	struct sigaction sa;
	pthread_t allthr[MAX_THR];
	sigset_t set, old, new;
	int i, ret, max_thr = 1;

	while ((i=getopt(argc, argv, "t:p:s:fhn:")) != EOF) {
		switch(i) {
		case 'h':
			usage();
			return 0;
		case 't':
			program_time = atoi(optarg);
			break;
		case 'p':
			threshold = atoi(optarg);
			break;
		case 's':
			signum = atoi(optarg);
			break;
		case 'f':
			fown = 1;
			break;
		case 'n':
			max_thr = atoi(optarg);
			if (max_thr >= MAX_THR)
				errx(1, "no more than %d threads", MAX_THR);
			break;
		default:
			errx(1, "invalid option");
		}
	}
	printf("program_time = %d, threshold = %d, signum = %d fcntl(%s), threads = %d\n",
		program_time, threshold, signum,
		fown ? "F_SETOWN_EX" : "F_SETOWN",
		max_thr);

	for (i = 0; i < MAX_THR; i++) {
		mismatch[i] = 0;
		bad_msg[i] = 0;
		bad_restart[i] = 0;
	}

	memset(&sa, 0, sizeof(sa));
	sigemptyset(&set);

	sa.sa_sigaction = sigusr1_handler;
	sa.sa_mask = set;
	sa.sa_flags = SA_SIGINFO;

	if (sigaction(SIGUSR1, &sa, NULL) != 0)
		errx(1, "sigaction failed");

	memset(&sa, 0, sizeof(sa));
	sigemptyset(&set);

	sa.sa_sigaction = sigio_handler;
	sa.sa_mask = set;
	sa.sa_flags = SA_SIGINFO;

	if (sigaction(signum, &sa, NULL) != 0)
		errx(1, "sigaction failed");

	if (pfm_initialize() != PFM_SUCCESS)
		errx(1, "pfm_initialize failed");

	/*
 	 * +1 because main thread is also using the barrier
 	 */
	pthread_barrier_init(&barrier, 0, max_thr+1);

	for(i=0; i < max_thr; i++) {
		ret = pthread_create(allthr+i, NULL, my_thread, (void *)(unsigned long)i);
		if (ret)
			err(1, "pthread_create failed");
	}
	myid = i;
	sigemptyset(&set);
	sigemptyset(&new);
	sigaddset(&set, SIGIO);
	sigaddset(&new, SIGIO);

	if (pthread_sigmask(SIG_BLOCK, &set, NULL))
		err(1, "cannot mask SIGIO in main thread");

	ret = sigprocmask(SIG_SETMASK, NULL, &old);
	if (ret)
		err(1, "sigprocmask failed");

	if (sigismember(&old, SIGIO)) {
		warnx("program started with SIGIO masked, unmasking it now\n");
		ret = sigprocmask(SIG_UNBLOCK, &new, NULL);
		if (ret)
			err(1, "sigprocmask failed");
	}

	pthread_barrier_wait(&barrier);
	printf("\n\n");

	for (i = 0; i < max_thr; i++) {
		pthread_join(allthr[i], NULL);
	}
	printf("\n\n");
	for (i = 0; i < max_thr; i++) {
		printf("myid = %3d, fd = %3d, total = %4ld, mismatch = %ld, "
			"bad_msg = %ld, bad_restart = %ld\n",
			fd2ov[i].id, fd2ov[i].fd, total[i], mismatch[i],
			bad_msg[i], bad_restart[i]);
	}
	/* free libpfm resources cleanly */
	pfm_terminate();

	return (0);
}