xref: /freebsd/tools/test/stress2/misc/pmc5.sh (revision a90b9d0159070121c221b966469c3e36d912bf82)
1#!/bin/sh
2
3#
4# Copyright (c) 2017 Dell EMC Isilon
5# All rights reserved.
6#
7# Redistribution and use in source and binary forms, with or without
8# modification, are permitted provided that the following conditions
9# are met:
10# 1. Redistributions of source code must retain the above copyright
11#    notice, this list of conditions and the following disclaimer.
12# 2. Redistributions in binary form must reproduce the above copyright
13#    notice, this list of conditions and the following disclaimer in the
14#    documentation and/or other materials provided with the distribution.
15#
16# THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19# ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26# SUCH DAMAGE.
27#
28
29# pmc fuzz test
30
31. ../default.cfg
32
33kldstat -v | grep -q hwpmc  || { kldload hwpmc; loaded=1; }
34dir=/tmp
35odir=`pwd`
36cd $dir
37sed '1,/^EOF/d' < $odir/$0 > $dir/pmc5.c
38mycc -o pmc5 -Wall -Wextra -O0 -g pmc5.c -lpmc -lpthread || exit 1
39rm -f pmc5.c
40
41for i in `jot 100`; do
42	./pmc5
43done > /dev/null 2>&1
44
45rm -rf pmc5 pmc5.core
46[ $loaded ] && kldunload hwpmc
47exit 0
48
49EOF
50#include <sys/param.h>
51#include <sys/event.h>
52#include <sys/mman.h>
53
54#include <machine/atomic.h>
55
56#include <err.h>
57#include <pmc.h>
58#include <pmclog.h>
59#include <pthread.h>
60#include <stdio.h>
61#include <stdlib.h>
62#include <unistd.h>
63
64static volatile u_int *share;
65static int fd1[2];
66static int kq;
67#define THREADS 20
68#define SYNC 0
69
70void *
71test(void *arg __unused)
72{
73
74	void *rfd;
75	char *cmdline[] = { "/usr/bin/true", NULL };
76
77	atomic_add_int(&share[SYNC], 1);
78	while (share[SYNC] != THREADS)
79		;
80	if ((rfd = pmclog_open(kq)) == NULL)
81		err(1, "pmclog_open(%d)", kq);
82//	if (pmc_configure_logfile(fd1[1]) < 0)
83	if (pmc_configure_logfile(kq) < 0)
84		err(1, "ERROR: Cannot configure log file");
85	sleep(1);
86	usleep(arc4random() % 20000);
87        if (execve(cmdline[0], cmdline, NULL) == -1)
88		err(1, "execve");
89
90	return (0);
91}
92
93int
94main(void)
95{
96	struct kevent ev[3];
97	pthread_t tid[THREADS];
98	size_t len;
99	int i, n, rc;
100
101	len = PAGE_SIZE;
102	if ((share = mmap(NULL, len, PROT_READ | PROT_WRITE,
103	    MAP_ANON | MAP_SHARED, -1, 0)) == MAP_FAILED)
104		err(1, "mmap");
105	if (pmc_init() == -1)
106		err(1, "pmc_init");
107
108	if (pipe(fd1) == -1)
109		err(1, "pipe()");
110
111	if ((kq = kqueue()) < 0)
112		err(1, "kqueue(). %s:%d", __FILE__, __LINE__);
113
114	n = 0;
115	EV_SET(&ev[n], fd1[1], EVFILT_WRITE,
116		    EV_ADD | EV_ENABLE | EV_CLEAR, 0, 0, 0);
117	n++;
118
119	if (kevent(kq, ev, n, NULL, 0, NULL) < 0)
120		err(1, "kevent(). %s:%d", __FILE__, __LINE__);
121	n = 0;
122	EV_SET(&ev[n], fd1[1], EVFILT_WRITE,
123		    EV_DELETE, 0, 0, 0);
124	n++;
125	if (kevent(kq, ev, n, NULL, 0, NULL) < 0)
126		warn("kevent(). %s:%d", __FILE__, __LINE__);
127
128	for (i = 0; i < THREADS; i++) {
129		if ((rc = pthread_create(&tid[i], NULL, test, NULL)) != 0)
130			errc(1, rc, "test()");
131	}
132	for (i = 0; i < THREADS; i++) {
133		if ((rc = pthread_join(tid[i], NULL)) != 0)
134			errc(1, rc, "pthread_join(%d)", i);
135	}
136
137	return (0);
138}
139