xref: /freebsd/tools/test/stress2/misc/pmc4.sh (revision 3078531de10dcae44b253a35125c949ff4235284)
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/pmc4.c
38mycc -o pmc4 -Wall -Wextra -O0 -g pmc4.c -lpmc -lpthread || exit 0
39rm -f pmc4.c
40
41for i in `jot 100`; do
42	./pmc4
43done > /dev/null 2>&1
44
45rm -rf pmc4 pmc4.core
46[ $loaded ] && kldunload hwpmc
47exit 0
48
49EOF
50#include <sys/param.h>
51#include <sys/event.h>
52
53#include <err.h>
54#include <pmc.h>
55#include <pmclog.h>
56#include <pthread.h>
57#include <stdio.h>
58#include <stdlib.h>
59#include <unistd.h>
60
61static int fd1[2];
62static int kq;
63#define THREADS 20
64
65static void *
66test(void *arg __unused)
67{
68
69	void *rfd;
70	char *cmdline[] = { "/usr/bin/true", NULL };
71
72	if ((rfd = pmclog_open(kq)) == NULL)
73		err(1, "pmclog_open(%d)", kq);
74	if (pmc_configure_logfile(kq) < 0)
75		err(1, "ERROR: Cannot configure log file");
76	sleep(1);
77	usleep(arc4random() % 20000);
78        if (execve(cmdline[0], cmdline, NULL) == -1)
79		err(1, "execve");
80
81	return (0);
82}
83
84int
85main(void)
86{
87	struct kevent ev[3];
88	pthread_t tid[THREADS];
89	int i, n, rc;
90
91	if (pmc_init() == -1)
92		err(1, "pmc_init");
93
94	if (pipe(fd1) == -1)
95		err(1, "pipe()");
96
97	if ((kq = kqueue()) < 0)
98		err(1, "kqueue(). %s:%d", __FILE__, __LINE__);
99
100	n = 0;
101	EV_SET(&ev[n], fd1[1], EVFILT_WRITE,
102		    EV_ADD | EV_ENABLE | EV_CLEAR, 0, 0, 0);
103	n++;
104
105	if (kevent(kq, ev, n, NULL, 0, NULL) < 0)
106		err(1, "kevent(). %s:%d", __FILE__, __LINE__);
107	n = 0;
108	EV_SET(&ev[n], fd1[1], EVFILT_WRITE,
109		    EV_DELETE, 0, 0, 0);
110	n++;
111	if (kevent(kq, ev, n, NULL, 0, NULL) < 0)
112		warn("kevent(). %s:%d", __FILE__, __LINE__);
113
114	for (i = 0; i < THREADS; i++) {
115		if ((rc = pthread_create(&tid[i], NULL, test, NULL)) != 0)
116			errc(1, rc, "test()");
117	}
118	for (i = 0; i < THREADS; i++) {
119		if ((rc = pthread_join(tid[i], NULL)) != 0)
120			errc(1, rc, "pthread_join(%d)", i);
121	}
122
123	return (0);
124}
125