xref: /freebsd/tools/test/stress2/misc/pmc4.sh (revision 8a272653)
18a272653SPeter Holm#!/bin/sh
28a272653SPeter Holm
38a272653SPeter Holm#
48a272653SPeter Holm# Copyright (c) 2017 Dell EMC Isilon
58a272653SPeter Holm# All rights reserved.
68a272653SPeter Holm#
78a272653SPeter Holm# Redistribution and use in source and binary forms, with or without
88a272653SPeter Holm# modification, are permitted provided that the following conditions
98a272653SPeter Holm# are met:
108a272653SPeter Holm# 1. Redistributions of source code must retain the above copyright
118a272653SPeter Holm#    notice, this list of conditions and the following disclaimer.
128a272653SPeter Holm# 2. Redistributions in binary form must reproduce the above copyright
138a272653SPeter Holm#    notice, this list of conditions and the following disclaimer in the
148a272653SPeter Holm#    documentation and/or other materials provided with the distribution.
158a272653SPeter Holm#
168a272653SPeter Holm# THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
178a272653SPeter Holm# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
188a272653SPeter Holm# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
198a272653SPeter Holm# ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
208a272653SPeter Holm# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
218a272653SPeter Holm# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
228a272653SPeter Holm# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
238a272653SPeter Holm# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
248a272653SPeter Holm# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
258a272653SPeter Holm# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
268a272653SPeter Holm# SUCH DAMAGE.
278a272653SPeter Holm#
288a272653SPeter Holm
298a272653SPeter Holm# pmc fuzz test
308a272653SPeter Holm
318a272653SPeter Holm. ../default.cfg
328a272653SPeter Holm
338a272653SPeter Holmkldstat -v | grep -q hwpmc  || { kldload hwpmc; loaded=1; }
348a272653SPeter Holmdir=/tmp
358a272653SPeter Holmodir=`pwd`
368a272653SPeter Holmcd $dir
378a272653SPeter Holmsed '1,/^EOF/d' < $odir/$0 > $dir/pmc4.c
388a272653SPeter Holmmycc -o pmc4 -Wall -Wextra -O0 -g pmc4.c -lpmc -lpthread || exit 0
398a272653SPeter Holmrm -f pmc4.c
408a272653SPeter Holm
418a272653SPeter Holmfor i in `jot 100`; do
428a272653SPeter Holm	./pmc4
438a272653SPeter Holmdone > /dev/null 2>&1
448a272653SPeter Holm
458a272653SPeter Holmrm -rf pmc4 pmc4.core
468a272653SPeter Holm[ $loaded ] && kldunload hwpmc
478a272653SPeter Holmexit 0
488a272653SPeter Holm
498a272653SPeter HolmEOF
508a272653SPeter Holm#include <sys/param.h>
518a272653SPeter Holm#include <sys/event.h>
528a272653SPeter Holm
538a272653SPeter Holm#include <err.h>
548a272653SPeter Holm#include <pmc.h>
558a272653SPeter Holm#include <pmclog.h>
568a272653SPeter Holm#include <pthread.h>
578a272653SPeter Holm#include <stdio.h>
588a272653SPeter Holm#include <stdlib.h>
598a272653SPeter Holm#include <unistd.h>
608a272653SPeter Holm
618a272653SPeter Holmstatic int fd1[2];
628a272653SPeter Holmstatic int kq;
638a272653SPeter Holm#define THREADS 20
648a272653SPeter Holm
658a272653SPeter Holmstatic void *
668a272653SPeter Holmtest(void *arg __unused)
678a272653SPeter Holm{
688a272653SPeter Holm
698a272653SPeter Holm	void *rfd;
708a272653SPeter Holm	char *cmdline[] = { "/usr/bin/true", NULL };
718a272653SPeter Holm
728a272653SPeter Holm	if ((rfd = pmclog_open(kq)) == NULL)
738a272653SPeter Holm		err(1, "pmclog_open(%d)", kq);
748a272653SPeter Holm	if (pmc_configure_logfile(kq) < 0)
758a272653SPeter Holm		err(1, "ERROR: Cannot configure log file");
768a272653SPeter Holm	sleep(1);
778a272653SPeter Holm	usleep(arc4random() % 20000);
788a272653SPeter Holm        if (execve(cmdline[0], cmdline, NULL) == -1)
798a272653SPeter Holm		err(1, "execve");
808a272653SPeter Holm
818a272653SPeter Holm	return (0);
828a272653SPeter Holm}
838a272653SPeter Holm
848a272653SPeter Holmint
858a272653SPeter Holmmain(void)
868a272653SPeter Holm{
878a272653SPeter Holm	struct kevent ev[3];
888a272653SPeter Holm	pthread_t tid[THREADS];
898a272653SPeter Holm	int i, n, rc;
908a272653SPeter Holm
918a272653SPeter Holm	if (pmc_init() == -1)
928a272653SPeter Holm		err(1, "pmc_init");
938a272653SPeter Holm
948a272653SPeter Holm	if (pipe(fd1) == -1)
958a272653SPeter Holm		err(1, "pipe()");
968a272653SPeter Holm
978a272653SPeter Holm	if ((kq = kqueue()) < 0)
988a272653SPeter Holm		err(1, "kqueue(). %s:%d", __FILE__, __LINE__);
998a272653SPeter Holm
1008a272653SPeter Holm	n = 0;
1018a272653SPeter Holm	EV_SET(&ev[n], fd1[1], EVFILT_WRITE,
1028a272653SPeter Holm		    EV_ADD | EV_ENABLE | EV_CLEAR, 0, 0, 0);
1038a272653SPeter Holm	n++;
1048a272653SPeter Holm
1058a272653SPeter Holm	if (kevent(kq, ev, n, NULL, 0, NULL) < 0)
1068a272653SPeter Holm		err(1, "kevent(). %s:%d", __FILE__, __LINE__);
1078a272653SPeter Holm	n = 0;
1088a272653SPeter Holm	EV_SET(&ev[n], fd1[1], EVFILT_WRITE,
1098a272653SPeter Holm		    EV_DELETE, 0, 0, 0);
1108a272653SPeter Holm	n++;
1118a272653SPeter Holm	if (kevent(kq, ev, n, NULL, 0, NULL) < 0)
1128a272653SPeter Holm		warn("kevent(). %s:%d", __FILE__, __LINE__);
1138a272653SPeter Holm
1148a272653SPeter Holm	for (i = 0; i < THREADS; i++) {
1158a272653SPeter Holm		if ((rc = pthread_create(&tid[i], NULL, test, NULL)) != 0)
1168a272653SPeter Holm			errc(1, rc, "test()");
1178a272653SPeter Holm	}
1188a272653SPeter Holm	for (i = 0; i < THREADS; i++) {
1198a272653SPeter Holm		if ((rc = pthread_join(tid[i], NULL)) != 0)
1208a272653SPeter Holm			errc(1, rc, "pthread_join(%d)", i);
1218a272653SPeter Holm	}
1228a272653SPeter Holm
1238a272653SPeter Holm	return (0);
1248a272653SPeter Holm}
125