xref: /openbmc/linux/tools/testing/selftests/mqueue/mq_perf_tests.c (revision 4f2c0a4acffbec01079c28f839422e64ddeff004)
17820b071SDoug Ledford /*
27820b071SDoug Ledford  * This application is Copyright 2012 Red Hat, Inc.
37820b071SDoug Ledford  *	Doug Ledford <dledford@redhat.com>
47820b071SDoug Ledford  *
57820b071SDoug Ledford  * mq_perf_tests is free software: you can redistribute it and/or modify
67820b071SDoug Ledford  * it under the terms of the GNU General Public License as published by
77820b071SDoug Ledford  * the Free Software Foundation, version 3.
87820b071SDoug Ledford  *
97820b071SDoug Ledford  * mq_perf_tests is distributed in the hope that it will be useful,
107820b071SDoug Ledford  * but WITHOUT ANY WARRANTY; without even the implied warranty of
117820b071SDoug Ledford  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
127820b071SDoug Ledford  * GNU General Public License for more details.
137820b071SDoug Ledford  *
147820b071SDoug Ledford  * For the full text of the license, see <http://www.gnu.org/licenses/>.
157820b071SDoug Ledford  *
167820b071SDoug Ledford  * mq_perf_tests.c
177820b071SDoug Ledford  *   Tests various types of message queue workloads, concentrating on those
187820b071SDoug Ledford  *   situations that invole large message sizes, large message queue depths,
197820b071SDoug Ledford  *   or both, and reports back useful metrics about kernel message queue
207820b071SDoug Ledford  *   performance.
217820b071SDoug Ledford  *
227820b071SDoug Ledford  */
237820b071SDoug Ledford #define _GNU_SOURCE
247820b071SDoug Ledford #include <stdio.h>
257820b071SDoug Ledford #include <stdlib.h>
267820b071SDoug Ledford #include <unistd.h>
277820b071SDoug Ledford #include <fcntl.h>
287820b071SDoug Ledford #include <string.h>
297820b071SDoug Ledford #include <limits.h>
307820b071SDoug Ledford #include <errno.h>
317820b071SDoug Ledford #include <signal.h>
327820b071SDoug Ledford #include <pthread.h>
337820b071SDoug Ledford #include <sched.h>
347820b071SDoug Ledford #include <sys/types.h>
357820b071SDoug Ledford #include <sys/time.h>
367820b071SDoug Ledford #include <sys/resource.h>
377820b071SDoug Ledford #include <sys/stat.h>
38*abd26d34SGeliang Tang #include <sys/param.h>
397820b071SDoug Ledford #include <mqueue.h>
407820b071SDoug Ledford #include <popt.h>
41c1ee4831SBen Hutchings #include <error.h>
427820b071SDoug Ledford 
43e6ee6ae4SShuah Khan (Samsung OSG) #include "../kselftest.h"
44e6ee6ae4SShuah Khan (Samsung OSG) 
457820b071SDoug Ledford static char *usage =
467820b071SDoug Ledford "Usage:\n"
477820b071SDoug Ledford "  %s [-c #[,#..] -f] path\n"
487820b071SDoug Ledford "\n"
497820b071SDoug Ledford "	-c #	Skip most tests and go straight to a high queue depth test\n"
507820b071SDoug Ledford "		and then run that test continuously (useful for running at\n"
517820b071SDoug Ledford "		the same time as some other workload to see how much the\n"
527820b071SDoug Ledford "		cache thrashing caused by adding messages to a very deep\n"
537820b071SDoug Ledford "		queue impacts the performance of other programs).  The number\n"
547820b071SDoug Ledford "		indicates which CPU core we should bind the process to during\n"
557820b071SDoug Ledford "		the run.  If you have more than one physical CPU, then you\n"
567820b071SDoug Ledford "		will need one copy per physical CPU package, and you should\n"
577820b071SDoug Ledford "		specify the CPU cores to pin ourself to via a comma separated\n"
587820b071SDoug Ledford "		list of CPU values.\n"
597820b071SDoug Ledford "	-f	Only usable with continuous mode.  Pin ourself to the CPUs\n"
607820b071SDoug Ledford "		as requested, then instead of looping doing a high mq\n"
617820b071SDoug Ledford "		workload, just busy loop.  This will allow us to lock up a\n"
627820b071SDoug Ledford "		single CPU just like we normally would, but without actually\n"
637820b071SDoug Ledford "		thrashing the CPU cache.  This is to make it easier to get\n"
647820b071SDoug Ledford "		comparable numbers from some other workload running on the\n"
657820b071SDoug Ledford "		other CPUs.  One set of numbers with # CPUs locked up running\n"
667820b071SDoug Ledford "		an mq workload, and another set of numbers with those same\n"
677820b071SDoug Ledford "		CPUs locked away from the test workload, but not doing\n"
687820b071SDoug Ledford "		anything to trash the cache like the mq workload might.\n"
697820b071SDoug Ledford "	path	Path name of the message queue to create\n"
707820b071SDoug Ledford "\n"
717820b071SDoug Ledford "	Note: this program must be run as root in order to enable all tests\n"
727820b071SDoug Ledford "\n";
737820b071SDoug Ledford 
747820b071SDoug Ledford char *MAX_MSGS = "/proc/sys/fs/mqueue/msg_max";
757820b071SDoug Ledford char *MAX_MSGSIZE = "/proc/sys/fs/mqueue/msgsize_max";
767820b071SDoug Ledford 
777820b071SDoug Ledford #define MAX_CPUS 64
787820b071SDoug Ledford char *cpu_option_string;
797820b071SDoug Ledford int cpus_to_pin[MAX_CPUS];
807820b071SDoug Ledford int num_cpus_to_pin;
817820b071SDoug Ledford pthread_t cpu_threads[MAX_CPUS];
827820b071SDoug Ledford pthread_t main_thread;
837820b071SDoug Ledford cpu_set_t *cpu_set;
847820b071SDoug Ledford int cpu_set_size;
857820b071SDoug Ledford int cpus_online;
867820b071SDoug Ledford 
877820b071SDoug Ledford #define MSG_SIZE 16
887820b071SDoug Ledford #define TEST1_LOOPS 10000000
897820b071SDoug Ledford #define TEST2_LOOPS 100000
907820b071SDoug Ledford int continuous_mode;
917820b071SDoug Ledford int continuous_mode_fake;
927820b071SDoug Ledford 
937820b071SDoug Ledford struct rlimit saved_limits, cur_limits;
947820b071SDoug Ledford int saved_max_msgs, saved_max_msgsize;
957820b071SDoug Ledford int cur_max_msgs, cur_max_msgsize;
967820b071SDoug Ledford FILE *max_msgs, *max_msgsize;
977820b071SDoug Ledford int cur_nice;
987820b071SDoug Ledford char *queue_path = "/mq_perf_tests";
997820b071SDoug Ledford mqd_t queue = -1;
1007820b071SDoug Ledford struct mq_attr result;
1017820b071SDoug Ledford int mq_prio_max;
1027820b071SDoug Ledford 
1037820b071SDoug Ledford const struct poptOption options[] = {
1047820b071SDoug Ledford 	{
1057820b071SDoug Ledford 		.longName = "continuous",
1067820b071SDoug Ledford 		.shortName = 'c',
1077820b071SDoug Ledford 		.argInfo = POPT_ARG_STRING,
1087820b071SDoug Ledford 		.arg = &cpu_option_string,
1097820b071SDoug Ledford 		.val = 'c',
1107820b071SDoug Ledford 		.descrip = "Run continuous tests at a high queue depth in "
1117820b071SDoug Ledford 			"order to test the effects of cache thrashing on "
1127820b071SDoug Ledford 			"other tasks on the system.  This test is intended "
1137820b071SDoug Ledford 			"to be run on one core of each physical CPU while "
1147820b071SDoug Ledford 			"some other CPU intensive task is run on all the other "
1157820b071SDoug Ledford 			"cores of that same physical CPU and the other task "
1167820b071SDoug Ledford 			"is timed.  It is assumed that the process of adding "
1177820b071SDoug Ledford 			"messages to the message queue in a tight loop will "
1187820b071SDoug Ledford 			"impact that other task to some degree.  Once the "
1197820b071SDoug Ledford 			"tests are performed in this way, you should then "
1207820b071SDoug Ledford 			"re-run the tests using fake mode in order to check "
1217820b071SDoug Ledford 			"the difference in time required to perform the CPU "
1227820b071SDoug Ledford 			"intensive task",
1237820b071SDoug Ledford 		.argDescrip = "cpu[,cpu]",
1247820b071SDoug Ledford 	},
1257820b071SDoug Ledford 	{
1267820b071SDoug Ledford 		.longName = "fake",
1277820b071SDoug Ledford 		.shortName = 'f',
1287820b071SDoug Ledford 		.argInfo = POPT_ARG_NONE,
1297820b071SDoug Ledford 		.arg = &continuous_mode_fake,
1307820b071SDoug Ledford 		.val = 0,
1317820b071SDoug Ledford 		.descrip = "Tie up the CPUs that we would normally tie up in"
1327820b071SDoug Ledford 			"continuous mode, but don't actually do any mq stuff, "
1337820b071SDoug Ledford 			"just keep the CPU busy so it can't be used to process "
1347820b071SDoug Ledford 			"system level tasks as this would free up resources on "
1357820b071SDoug Ledford 			"the other CPU cores and skew the comparison between "
1367820b071SDoug Ledford 			"the no-mqueue work and mqueue work tests",
1377820b071SDoug Ledford 		.argDescrip = NULL,
1387820b071SDoug Ledford 	},
1397820b071SDoug Ledford 	{
1407820b071SDoug Ledford 		.longName = "path",
1417820b071SDoug Ledford 		.shortName = 'p',
1427820b071SDoug Ledford 		.argInfo = POPT_ARG_STRING | POPT_ARGFLAG_SHOW_DEFAULT,
1437820b071SDoug Ledford 		.arg = &queue_path,
1447820b071SDoug Ledford 		.val = 'p',
1457820b071SDoug Ledford 		.descrip = "The name of the path to use in the mqueue "
1467820b071SDoug Ledford 			"filesystem for our tests",
1477820b071SDoug Ledford 		.argDescrip = "pathname",
1487820b071SDoug Ledford 	},
1497820b071SDoug Ledford 	POPT_AUTOHELP
1507820b071SDoug Ledford 	POPT_TABLEEND
1517820b071SDoug Ledford };
1527820b071SDoug Ledford 
1537820b071SDoug Ledford static inline void __set(FILE *stream, int value, char *err_msg);
1547820b071SDoug Ledford void shutdown(int exit_val, char *err_cause, int line_no);
1557820b071SDoug Ledford void sig_action_SIGUSR1(int signum, siginfo_t *info, void *context);
1567820b071SDoug Ledford void sig_action(int signum, siginfo_t *info, void *context);
1577820b071SDoug Ledford static inline int get(FILE *stream);
1587820b071SDoug Ledford static inline void set(FILE *stream, int value);
1597820b071SDoug Ledford static inline int try_set(FILE *stream, int value);
1607820b071SDoug Ledford static inline void getr(int type, struct rlimit *rlim);
1617820b071SDoug Ledford static inline void setr(int type, struct rlimit *rlim);
1627820b071SDoug Ledford static inline void open_queue(struct mq_attr *attr);
1637820b071SDoug Ledford void increase_limits(void);
1647820b071SDoug Ledford 
__set(FILE * stream,int value,char * err_msg)1657820b071SDoug Ledford static inline void __set(FILE *stream, int value, char *err_msg)
1667820b071SDoug Ledford {
1677820b071SDoug Ledford 	rewind(stream);
1687820b071SDoug Ledford 	if (fprintf(stream, "%d", value) < 0)
1697820b071SDoug Ledford 		perror(err_msg);
1707820b071SDoug Ledford }
1717820b071SDoug Ledford 
1727820b071SDoug Ledford 
shutdown(int exit_val,char * err_cause,int line_no)1737820b071SDoug Ledford void shutdown(int exit_val, char *err_cause, int line_no)
1747820b071SDoug Ledford {
1757820b071SDoug Ledford 	static int in_shutdown = 0;
1767820b071SDoug Ledford 	int errno_at_shutdown = errno;
1777820b071SDoug Ledford 	int i;
1787820b071SDoug Ledford 
1797820b071SDoug Ledford 	/* In case we get called by multiple threads or from an sighandler */
1807820b071SDoug Ledford 	if (in_shutdown++)
1817820b071SDoug Ledford 		return;
1827820b071SDoug Ledford 
183ce64763cSAthira Rajeev 	/* Free the cpu_set allocated using CPU_ALLOC in main function */
184ce64763cSAthira Rajeev 	CPU_FREE(cpu_set);
185ce64763cSAthira Rajeev 
1867820b071SDoug Ledford 	for (i = 0; i < num_cpus_to_pin; i++)
1877820b071SDoug Ledford 		if (cpu_threads[i]) {
1887820b071SDoug Ledford 			pthread_kill(cpu_threads[i], SIGUSR1);
1897820b071SDoug Ledford 			pthread_join(cpu_threads[i], NULL);
1907820b071SDoug Ledford 		}
1917820b071SDoug Ledford 
1927820b071SDoug Ledford 	if (queue != -1)
1937820b071SDoug Ledford 		if (mq_close(queue))
1947820b071SDoug Ledford 			perror("mq_close() during shutdown");
1957820b071SDoug Ledford 	if (queue_path)
1967820b071SDoug Ledford 		/*
1977820b071SDoug Ledford 		 * Be silent if this fails, if we cleaned up already it's
1987820b071SDoug Ledford 		 * expected to fail
1997820b071SDoug Ledford 		 */
2007820b071SDoug Ledford 		mq_unlink(queue_path);
2017820b071SDoug Ledford 	if (saved_max_msgs)
2027820b071SDoug Ledford 		__set(max_msgs, saved_max_msgs,
2037820b071SDoug Ledford 		      "failed to restore saved_max_msgs");
2047820b071SDoug Ledford 	if (saved_max_msgsize)
2057820b071SDoug Ledford 		__set(max_msgsize, saved_max_msgsize,
2067820b071SDoug Ledford 		      "failed to restore saved_max_msgsize");
2077820b071SDoug Ledford 	if (exit_val)
2087820b071SDoug Ledford 		error(exit_val, errno_at_shutdown, "%s at %d",
2097820b071SDoug Ledford 		      err_cause, line_no);
2107820b071SDoug Ledford 	exit(0);
2117820b071SDoug Ledford }
2127820b071SDoug Ledford 
sig_action_SIGUSR1(int signum,siginfo_t * info,void * context)2137820b071SDoug Ledford void sig_action_SIGUSR1(int signum, siginfo_t *info, void *context)
2147820b071SDoug Ledford {
2157820b071SDoug Ledford 	if (pthread_self() != main_thread)
2167820b071SDoug Ledford 		pthread_exit(0);
2177820b071SDoug Ledford 	else {
2187820b071SDoug Ledford 		fprintf(stderr, "Caught signal %d in SIGUSR1 handler, "
2197820b071SDoug Ledford 				"exiting\n", signum);
2207820b071SDoug Ledford 		shutdown(0, "", 0);
2217820b071SDoug Ledford 		fprintf(stderr, "\n\nReturned from shutdown?!?!\n\n");
2227820b071SDoug Ledford 		exit(0);
2237820b071SDoug Ledford 	}
2247820b071SDoug Ledford }
2257820b071SDoug Ledford 
sig_action(int signum,siginfo_t * info,void * context)2267820b071SDoug Ledford void sig_action(int signum, siginfo_t *info, void *context)
2277820b071SDoug Ledford {
2287820b071SDoug Ledford 	if (pthread_self() != main_thread)
2297820b071SDoug Ledford 		pthread_kill(main_thread, signum);
2307820b071SDoug Ledford 	else {
2317820b071SDoug Ledford 		fprintf(stderr, "Caught signal %d, exiting\n", signum);
2327820b071SDoug Ledford 		shutdown(0, "", 0);
2337820b071SDoug Ledford 		fprintf(stderr, "\n\nReturned from shutdown?!?!\n\n");
2347820b071SDoug Ledford 		exit(0);
2357820b071SDoug Ledford 	}
2367820b071SDoug Ledford }
2377820b071SDoug Ledford 
get(FILE * stream)2387820b071SDoug Ledford static inline int get(FILE *stream)
2397820b071SDoug Ledford {
2407820b071SDoug Ledford 	int value;
2417820b071SDoug Ledford 	rewind(stream);
2427820b071SDoug Ledford 	if (fscanf(stream, "%d", &value) != 1)
2437820b071SDoug Ledford 		shutdown(4, "Error reading /proc entry", __LINE__);
2447820b071SDoug Ledford 	return value;
2457820b071SDoug Ledford }
2467820b071SDoug Ledford 
set(FILE * stream,int value)2477820b071SDoug Ledford static inline void set(FILE *stream, int value)
2487820b071SDoug Ledford {
2497820b071SDoug Ledford 	int new_value;
2507820b071SDoug Ledford 
2517820b071SDoug Ledford 	rewind(stream);
2527820b071SDoug Ledford 	if (fprintf(stream, "%d", value) < 0)
2537820b071SDoug Ledford 		return shutdown(5, "Failed writing to /proc file", __LINE__);
2547820b071SDoug Ledford 	new_value = get(stream);
2557820b071SDoug Ledford 	if (new_value != value)
2567820b071SDoug Ledford 		return shutdown(5, "We didn't get what we wrote to /proc back",
2577820b071SDoug Ledford 				__LINE__);
2587820b071SDoug Ledford }
2597820b071SDoug Ledford 
try_set(FILE * stream,int value)2607820b071SDoug Ledford static inline int try_set(FILE *stream, int value)
2617820b071SDoug Ledford {
2627820b071SDoug Ledford 	int new_value;
2637820b071SDoug Ledford 
2647820b071SDoug Ledford 	rewind(stream);
2657820b071SDoug Ledford 	fprintf(stream, "%d", value);
2667820b071SDoug Ledford 	new_value = get(stream);
2677820b071SDoug Ledford 	return new_value == value;
2687820b071SDoug Ledford }
2697820b071SDoug Ledford 
getr(int type,struct rlimit * rlim)2707820b071SDoug Ledford static inline void getr(int type, struct rlimit *rlim)
2717820b071SDoug Ledford {
2727820b071SDoug Ledford 	if (getrlimit(type, rlim))
2737820b071SDoug Ledford 		shutdown(6, "getrlimit()", __LINE__);
2747820b071SDoug Ledford }
2757820b071SDoug Ledford 
setr(int type,struct rlimit * rlim)2767820b071SDoug Ledford static inline void setr(int type, struct rlimit *rlim)
2777820b071SDoug Ledford {
2787820b071SDoug Ledford 	if (setrlimit(type, rlim))
2797820b071SDoug Ledford 		shutdown(7, "setrlimit()", __LINE__);
2807820b071SDoug Ledford }
2817820b071SDoug Ledford 
2827820b071SDoug Ledford /**
2837820b071SDoug Ledford  * open_queue - open the global queue for testing
2847820b071SDoug Ledford  * @attr - An attr struct specifying the desired queue traits
2857820b071SDoug Ledford  * @result - An attr struct that lists the actual traits the queue has
2867820b071SDoug Ledford  *
2877820b071SDoug Ledford  * This open is not allowed to fail, failure will result in an orderly
2887820b071SDoug Ledford  * shutdown of the program.  The global queue_path is used to set what
2897820b071SDoug Ledford  * queue to open, the queue descriptor is saved in the global queue
2907820b071SDoug Ledford  * variable.
2917820b071SDoug Ledford  */
open_queue(struct mq_attr * attr)2927820b071SDoug Ledford static inline void open_queue(struct mq_attr *attr)
2937820b071SDoug Ledford {
2947820b071SDoug Ledford 	int flags = O_RDWR | O_EXCL | O_CREAT | O_NONBLOCK;
2957820b071SDoug Ledford 	int perms = DEFFILEMODE;
2967820b071SDoug Ledford 
2977820b071SDoug Ledford 	queue = mq_open(queue_path, flags, perms, attr);
2987820b071SDoug Ledford 	if (queue == -1)
2997820b071SDoug Ledford 		shutdown(1, "mq_open()", __LINE__);
3007820b071SDoug Ledford 	if (mq_getattr(queue, &result))
3017820b071SDoug Ledford 		shutdown(1, "mq_getattr()", __LINE__);
3027820b071SDoug Ledford 	printf("\n\tQueue %s created:\n", queue_path);
3037820b071SDoug Ledford 	printf("\t\tmq_flags:\t\t\t%s\n", result.mq_flags & O_NONBLOCK ?
3047820b071SDoug Ledford 	       "O_NONBLOCK" : "(null)");
305f15fed3dSShuah Khan 	printf("\t\tmq_maxmsg:\t\t\t%lu\n", result.mq_maxmsg);
306f15fed3dSShuah Khan 	printf("\t\tmq_msgsize:\t\t\t%lu\n", result.mq_msgsize);
307f15fed3dSShuah Khan 	printf("\t\tmq_curmsgs:\t\t\t%lu\n", result.mq_curmsgs);
3087820b071SDoug Ledford }
3097820b071SDoug Ledford 
fake_cont_thread(void * arg)3107820b071SDoug Ledford void *fake_cont_thread(void *arg)
3117820b071SDoug Ledford {
3127820b071SDoug Ledford 	int i;
3137820b071SDoug Ledford 
3147820b071SDoug Ledford 	for (i = 0; i < num_cpus_to_pin; i++)
3157820b071SDoug Ledford 		if (cpu_threads[i] == pthread_self())
3167820b071SDoug Ledford 			break;
3177820b071SDoug Ledford 	printf("\tStarted fake continuous mode thread %d on CPU %d\n", i,
3187820b071SDoug Ledford 	       cpus_to_pin[i]);
3197820b071SDoug Ledford 	while (1)
3207820b071SDoug Ledford 		;
3217820b071SDoug Ledford }
3227820b071SDoug Ledford 
cont_thread(void * arg)3237820b071SDoug Ledford void *cont_thread(void *arg)
3247820b071SDoug Ledford {
3257820b071SDoug Ledford 	char buff[MSG_SIZE];
3267820b071SDoug Ledford 	int i, priority;
3277820b071SDoug Ledford 
3287820b071SDoug Ledford 	for (i = 0; i < num_cpus_to_pin; i++)
3297820b071SDoug Ledford 		if (cpu_threads[i] == pthread_self())
3307820b071SDoug Ledford 			break;
3317820b071SDoug Ledford 	printf("\tStarted continuous mode thread %d on CPU %d\n", i,
3327820b071SDoug Ledford 	       cpus_to_pin[i]);
3337820b071SDoug Ledford 	while (1) {
3347820b071SDoug Ledford 		while (mq_send(queue, buff, sizeof(buff), 0) == 0)
3357820b071SDoug Ledford 			;
3367820b071SDoug Ledford 		mq_receive(queue, buff, sizeof(buff), &priority);
3377820b071SDoug Ledford 	}
3387820b071SDoug Ledford }
3397820b071SDoug Ledford 
3407820b071SDoug Ledford #define drain_queue() \
3417820b071SDoug Ledford 	while (mq_receive(queue, buff, MSG_SIZE, &prio_in) == MSG_SIZE)
3427820b071SDoug Ledford 
3437820b071SDoug Ledford #define do_untimed_send() \
3447820b071SDoug Ledford 	do { \
3457820b071SDoug Ledford 		if (mq_send(queue, buff, MSG_SIZE, prio_out)) \
3467820b071SDoug Ledford 			shutdown(3, "Test send failure", __LINE__); \
3477820b071SDoug Ledford 	} while (0)
3487820b071SDoug Ledford 
3497820b071SDoug Ledford #define do_send_recv() \
3507820b071SDoug Ledford 	do { \
3517820b071SDoug Ledford 		clock_gettime(clock, &start); \
3527820b071SDoug Ledford 		if (mq_send(queue, buff, MSG_SIZE, prio_out)) \
3537820b071SDoug Ledford 			shutdown(3, "Test send failure", __LINE__); \
3547820b071SDoug Ledford 		clock_gettime(clock, &middle); \
3557820b071SDoug Ledford 		if (mq_receive(queue, buff, MSG_SIZE, &prio_in) != MSG_SIZE) \
3567820b071SDoug Ledford 			shutdown(3, "Test receive failure", __LINE__); \
3577820b071SDoug Ledford 		clock_gettime(clock, &end); \
3587820b071SDoug Ledford 		nsec = ((middle.tv_sec - start.tv_sec) * 1000000000) + \
3597820b071SDoug Ledford 			(middle.tv_nsec - start.tv_nsec); \
3607820b071SDoug Ledford 		send_total.tv_nsec += nsec; \
3617820b071SDoug Ledford 		if (send_total.tv_nsec >= 1000000000) { \
3627820b071SDoug Ledford 			send_total.tv_sec++; \
3637820b071SDoug Ledford 			send_total.tv_nsec -= 1000000000; \
3647820b071SDoug Ledford 		} \
3657820b071SDoug Ledford 		nsec = ((end.tv_sec - middle.tv_sec) * 1000000000) + \
3667820b071SDoug Ledford 			(end.tv_nsec - middle.tv_nsec); \
3677820b071SDoug Ledford 		recv_total.tv_nsec += nsec; \
3687820b071SDoug Ledford 		if (recv_total.tv_nsec >= 1000000000) { \
3697820b071SDoug Ledford 			recv_total.tv_sec++; \
3707820b071SDoug Ledford 			recv_total.tv_nsec -= 1000000000; \
3717820b071SDoug Ledford 		} \
3727820b071SDoug Ledford 	} while (0)
3737820b071SDoug Ledford 
3747820b071SDoug Ledford struct test {
3757820b071SDoug Ledford 	char *desc;
3767820b071SDoug Ledford 	void (*func)(int *);
3777820b071SDoug Ledford };
3787820b071SDoug Ledford 
const_prio(int * prio)3797820b071SDoug Ledford void const_prio(int *prio)
3807820b071SDoug Ledford {
3817820b071SDoug Ledford 	return;
3827820b071SDoug Ledford }
3837820b071SDoug Ledford 
inc_prio(int * prio)3847820b071SDoug Ledford void inc_prio(int *prio)
3857820b071SDoug Ledford {
3867820b071SDoug Ledford 	if (++*prio == mq_prio_max)
3877820b071SDoug Ledford 		*prio = 0;
3887820b071SDoug Ledford }
3897820b071SDoug Ledford 
dec_prio(int * prio)3907820b071SDoug Ledford void dec_prio(int *prio)
3917820b071SDoug Ledford {
3927820b071SDoug Ledford 	if (--*prio < 0)
3937820b071SDoug Ledford 		*prio = mq_prio_max - 1;
3947820b071SDoug Ledford }
3957820b071SDoug Ledford 
random_prio(int * prio)3967820b071SDoug Ledford void random_prio(int *prio)
3977820b071SDoug Ledford {
3987820b071SDoug Ledford 	*prio = random() % mq_prio_max;
3997820b071SDoug Ledford }
4007820b071SDoug Ledford 
4017820b071SDoug Ledford struct test test2[] = {
4027820b071SDoug Ledford 	{"\n\tTest #2a: Time send/recv message, queue full, constant prio\n",
4037820b071SDoug Ledford 		const_prio},
4047820b071SDoug Ledford 	{"\n\tTest #2b: Time send/recv message, queue full, increasing prio\n",
4057820b071SDoug Ledford 		inc_prio},
4067820b071SDoug Ledford 	{"\n\tTest #2c: Time send/recv message, queue full, decreasing prio\n",
4077820b071SDoug Ledford 		dec_prio},
4087820b071SDoug Ledford 	{"\n\tTest #2d: Time send/recv message, queue full, random prio\n",
4097820b071SDoug Ledford 		random_prio},
4107820b071SDoug Ledford 	{NULL, NULL}
4117820b071SDoug Ledford };
4127820b071SDoug Ledford 
4137820b071SDoug Ledford /**
4147820b071SDoug Ledford  * Tests to perform (all done with MSG_SIZE messages):
4157820b071SDoug Ledford  *
4167820b071SDoug Ledford  * 1) Time to add/remove message with 0 messages on queue
4177820b071SDoug Ledford  * 1a) with constant prio
4187820b071SDoug Ledford  * 2) Time to add/remove message when queue close to capacity:
4197820b071SDoug Ledford  * 2a) with constant prio
4207820b071SDoug Ledford  * 2b) with increasing prio
4217820b071SDoug Ledford  * 2c) with decreasing prio
4227820b071SDoug Ledford  * 2d) with random prio
4237820b071SDoug Ledford  * 3) Test limits of priorities honored (double check _SC_MQ_PRIO_MAX)
4247820b071SDoug Ledford  */
perf_test_thread(void * arg)4257820b071SDoug Ledford void *perf_test_thread(void *arg)
4267820b071SDoug Ledford {
4277820b071SDoug Ledford 	char buff[MSG_SIZE];
4287820b071SDoug Ledford 	int prio_out, prio_in;
4297820b071SDoug Ledford 	int i;
4307820b071SDoug Ledford 	clockid_t clock;
4317820b071SDoug Ledford 	pthread_t *t;
4327820b071SDoug Ledford 	struct timespec res, start, middle, end, send_total, recv_total;
4337820b071SDoug Ledford 	unsigned long long nsec;
4347820b071SDoug Ledford 	struct test *cur_test;
4357820b071SDoug Ledford 
4367820b071SDoug Ledford 	t = &cpu_threads[0];
4377820b071SDoug Ledford 	printf("\n\tStarted mqueue performance test thread on CPU %d\n",
4387820b071SDoug Ledford 	       cpus_to_pin[0]);
4397820b071SDoug Ledford 	mq_prio_max = sysconf(_SC_MQ_PRIO_MAX);
4407820b071SDoug Ledford 	if (mq_prio_max == -1)
4417820b071SDoug Ledford 		shutdown(2, "sysconf(_SC_MQ_PRIO_MAX)", __LINE__);
4427820b071SDoug Ledford 	if (pthread_getcpuclockid(cpu_threads[0], &clock) != 0)
4437820b071SDoug Ledford 		shutdown(2, "pthread_getcpuclockid", __LINE__);
4447820b071SDoug Ledford 
4457820b071SDoug Ledford 	if (clock_getres(clock, &res))
4467820b071SDoug Ledford 		shutdown(2, "clock_getres()", __LINE__);
4477820b071SDoug Ledford 
4487820b071SDoug Ledford 	printf("\t\tMax priorities:\t\t\t%d\n", mq_prio_max);
449f15fed3dSShuah Khan 	printf("\t\tClock resolution:\t\t%lu nsec%s\n", res.tv_nsec,
4507820b071SDoug Ledford 	       res.tv_nsec > 1 ? "s" : "");
4517820b071SDoug Ledford 
4527820b071SDoug Ledford 
4537820b071SDoug Ledford 
4547820b071SDoug Ledford 	printf("\n\tTest #1: Time send/recv message, queue empty\n");
4557820b071SDoug Ledford 	printf("\t\t(%d iterations)\n", TEST1_LOOPS);
4567820b071SDoug Ledford 	prio_out = 0;
4577820b071SDoug Ledford 	send_total.tv_sec = 0;
4587820b071SDoug Ledford 	send_total.tv_nsec = 0;
4597820b071SDoug Ledford 	recv_total.tv_sec = 0;
4607820b071SDoug Ledford 	recv_total.tv_nsec = 0;
4617820b071SDoug Ledford 	for (i = 0; i < TEST1_LOOPS; i++)
4627820b071SDoug Ledford 		do_send_recv();
463f15fed3dSShuah Khan 	printf("\t\tSend msg:\t\t\t%ld.%lus total time\n",
4647820b071SDoug Ledford 	       send_total.tv_sec, send_total.tv_nsec);
4657820b071SDoug Ledford 	nsec = ((unsigned long long)send_total.tv_sec * 1000000000 +
4667820b071SDoug Ledford 		 send_total.tv_nsec) / TEST1_LOOPS;
467f15fed3dSShuah Khan 	printf("\t\t\t\t\t\t%lld nsec/msg\n", nsec);
468f15fed3dSShuah Khan 	printf("\t\tRecv msg:\t\t\t%ld.%lus total time\n",
4697820b071SDoug Ledford 	       recv_total.tv_sec, recv_total.tv_nsec);
4707820b071SDoug Ledford 	nsec = ((unsigned long long)recv_total.tv_sec * 1000000000 +
4717820b071SDoug Ledford 		recv_total.tv_nsec) / TEST1_LOOPS;
472f15fed3dSShuah Khan 	printf("\t\t\t\t\t\t%lld nsec/msg\n", nsec);
4737820b071SDoug Ledford 
4747820b071SDoug Ledford 
4757820b071SDoug Ledford 	for (cur_test = test2; cur_test->desc != NULL; cur_test++) {
476f15fed3dSShuah Khan 		printf("%s:\n", cur_test->desc);
4777820b071SDoug Ledford 		printf("\t\t(%d iterations)\n", TEST2_LOOPS);
4787820b071SDoug Ledford 		prio_out = 0;
4797820b071SDoug Ledford 		send_total.tv_sec = 0;
4807820b071SDoug Ledford 		send_total.tv_nsec = 0;
4817820b071SDoug Ledford 		recv_total.tv_sec = 0;
4827820b071SDoug Ledford 		recv_total.tv_nsec = 0;
4837820b071SDoug Ledford 		printf("\t\tFilling queue...");
4847820b071SDoug Ledford 		fflush(stdout);
4857820b071SDoug Ledford 		clock_gettime(clock, &start);
4867820b071SDoug Ledford 		for (i = 0; i < result.mq_maxmsg - 1; i++) {
4877820b071SDoug Ledford 			do_untimed_send();
4887820b071SDoug Ledford 			cur_test->func(&prio_out);
4897820b071SDoug Ledford 		}
4907820b071SDoug Ledford 		clock_gettime(clock, &end);
4917820b071SDoug Ledford 		nsec = ((unsigned long long)(end.tv_sec - start.tv_sec) *
4927820b071SDoug Ledford 			1000000000) + (end.tv_nsec - start.tv_nsec);
4937820b071SDoug Ledford 		printf("done.\t\t%lld.%llds\n", nsec / 1000000000,
4947820b071SDoug Ledford 		       nsec % 1000000000);
4957820b071SDoug Ledford 		printf("\t\tTesting...");
4967820b071SDoug Ledford 		fflush(stdout);
4977820b071SDoug Ledford 		for (i = 0; i < TEST2_LOOPS; i++) {
4987820b071SDoug Ledford 			do_send_recv();
4997820b071SDoug Ledford 			cur_test->func(&prio_out);
5007820b071SDoug Ledford 		}
5017820b071SDoug Ledford 		printf("done.\n");
502f15fed3dSShuah Khan 		printf("\t\tSend msg:\t\t\t%ld.%lus total time\n",
5037820b071SDoug Ledford 		       send_total.tv_sec, send_total.tv_nsec);
5047820b071SDoug Ledford 		nsec = ((unsigned long long)send_total.tv_sec * 1000000000 +
5057820b071SDoug Ledford 			 send_total.tv_nsec) / TEST2_LOOPS;
506f15fed3dSShuah Khan 		printf("\t\t\t\t\t\t%lld nsec/msg\n", nsec);
507f15fed3dSShuah Khan 		printf("\t\tRecv msg:\t\t\t%ld.%lus total time\n",
5087820b071SDoug Ledford 		       recv_total.tv_sec, recv_total.tv_nsec);
5097820b071SDoug Ledford 		nsec = ((unsigned long long)recv_total.tv_sec * 1000000000 +
5107820b071SDoug Ledford 			recv_total.tv_nsec) / TEST2_LOOPS;
511f15fed3dSShuah Khan 		printf("\t\t\t\t\t\t%lld nsec/msg\n", nsec);
5127820b071SDoug Ledford 		printf("\t\tDraining queue...");
5137820b071SDoug Ledford 		fflush(stdout);
5147820b071SDoug Ledford 		clock_gettime(clock, &start);
5157820b071SDoug Ledford 		drain_queue();
5167820b071SDoug Ledford 		clock_gettime(clock, &end);
5177820b071SDoug Ledford 		nsec = ((unsigned long long)(end.tv_sec - start.tv_sec) *
5187820b071SDoug Ledford 			1000000000) + (end.tv_nsec - start.tv_nsec);
5197820b071SDoug Ledford 		printf("done.\t\t%lld.%llds\n", nsec / 1000000000,
5207820b071SDoug Ledford 		       nsec % 1000000000);
5217820b071SDoug Ledford 	}
5227820b071SDoug Ledford 	return 0;
5237820b071SDoug Ledford }
5247820b071SDoug Ledford 
increase_limits(void)5257820b071SDoug Ledford void increase_limits(void)
5267820b071SDoug Ledford {
5277820b071SDoug Ledford 	cur_limits.rlim_cur = RLIM_INFINITY;
5287820b071SDoug Ledford 	cur_limits.rlim_max = RLIM_INFINITY;
5297820b071SDoug Ledford 	setr(RLIMIT_MSGQUEUE, &cur_limits);
5307820b071SDoug Ledford 	while (try_set(max_msgs, cur_max_msgs += 10))
5317820b071SDoug Ledford 		;
5327820b071SDoug Ledford 	cur_max_msgs = get(max_msgs);
5337820b071SDoug Ledford 	while (try_set(max_msgsize, cur_max_msgsize += 1024))
5347820b071SDoug Ledford 		;
5357820b071SDoug Ledford 	cur_max_msgsize = get(max_msgsize);
5367820b071SDoug Ledford 	if (setpriority(PRIO_PROCESS, 0, -20) != 0)
5377820b071SDoug Ledford 		shutdown(2, "setpriority()", __LINE__);
5387820b071SDoug Ledford 	cur_nice = -20;
5397820b071SDoug Ledford }
5407820b071SDoug Ledford 
main(int argc,char * argv[])5417820b071SDoug Ledford int main(int argc, char *argv[])
5427820b071SDoug Ledford {
5437820b071SDoug Ledford 	struct mq_attr attr;
5447820b071SDoug Ledford 	char *option, *next_option;
54513e634deSdann frazier 	int i, cpu, rc;
5467820b071SDoug Ledford 	struct sigaction sa;
5477820b071SDoug Ledford 	poptContext popt_context;
5487820b071SDoug Ledford 	void *retval;
5497820b071SDoug Ledford 
5507820b071SDoug Ledford 	main_thread = pthread_self();
5517820b071SDoug Ledford 	num_cpus_to_pin = 0;
5527820b071SDoug Ledford 
5537820b071SDoug Ledford 	if (sysconf(_SC_NPROCESSORS_ONLN) == -1) {
5547820b071SDoug Ledford 		perror("sysconf(_SC_NPROCESSORS_ONLN)");
5557820b071SDoug Ledford 		exit(1);
5567820b071SDoug Ledford 	}
557ce64763cSAthira Rajeev 
558ce64763cSAthira Rajeev 	if (getuid() != 0)
559ce64763cSAthira Rajeev 		ksft_exit_skip("Not running as root, but almost all tests "
560ce64763cSAthira Rajeev 			"require root in order to modify\nsystem settings.  "
561ce64763cSAthira Rajeev 			"Exiting.\n");
562ce64763cSAthira Rajeev 
563*abd26d34SGeliang Tang 	cpus_online = MIN(MAX_CPUS, sysconf(_SC_NPROCESSORS_ONLN));
5647820b071SDoug Ledford 	cpu_set = CPU_ALLOC(cpus_online);
5657820b071SDoug Ledford 	if (cpu_set == NULL) {
5667820b071SDoug Ledford 		perror("CPU_ALLOC()");
5677820b071SDoug Ledford 		exit(1);
5687820b071SDoug Ledford 	}
5697820b071SDoug Ledford 	cpu_set_size = CPU_ALLOC_SIZE(cpus_online);
5707820b071SDoug Ledford 	CPU_ZERO_S(cpu_set_size, cpu_set);
5717820b071SDoug Ledford 
5727820b071SDoug Ledford 	popt_context = poptGetContext(NULL, argc, (const char **)argv,
5737820b071SDoug Ledford 				      options, 0);
5747820b071SDoug Ledford 
5757820b071SDoug Ledford 	while ((rc = poptGetNextOpt(popt_context)) > 0) {
5767820b071SDoug Ledford 		switch (rc) {
5777820b071SDoug Ledford 		case 'c':
5787820b071SDoug Ledford 			continuous_mode = 1;
5797820b071SDoug Ledford 			option = cpu_option_string;
5807820b071SDoug Ledford 			do {
5817820b071SDoug Ledford 				next_option = strchr(option, ',');
5827820b071SDoug Ledford 				if (next_option)
5837820b071SDoug Ledford 					*next_option = '\0';
5847820b071SDoug Ledford 				cpu = atoi(option);
5857820b071SDoug Ledford 				if (cpu >= cpus_online)
5867820b071SDoug Ledford 					fprintf(stderr, "CPU %d exceeds "
5877820b071SDoug Ledford 						"cpus online, ignoring.\n",
5887820b071SDoug Ledford 						cpu);
5897820b071SDoug Ledford 				else
5907820b071SDoug Ledford 					cpus_to_pin[num_cpus_to_pin++] = cpu;
5917820b071SDoug Ledford 				if (next_option)
5927820b071SDoug Ledford 					option = ++next_option;
5937820b071SDoug Ledford 			} while (next_option && num_cpus_to_pin < MAX_CPUS);
5947820b071SDoug Ledford 			/* Double check that they didn't give us the same CPU
5957820b071SDoug Ledford 			 * more than once */
5967820b071SDoug Ledford 			for (cpu = 0; cpu < num_cpus_to_pin; cpu++) {
5977820b071SDoug Ledford 				if (CPU_ISSET_S(cpus_to_pin[cpu], cpu_set_size,
5987820b071SDoug Ledford 						cpu_set)) {
5997820b071SDoug Ledford 					fprintf(stderr, "Any given CPU may "
6007820b071SDoug Ledford 						"only be given once.\n");
601ce64763cSAthira Rajeev 					goto err_code;
6027820b071SDoug Ledford 				} else
6037820b071SDoug Ledford 					CPU_SET_S(cpus_to_pin[cpu],
6047820b071SDoug Ledford 						  cpu_set_size, cpu_set);
6057820b071SDoug Ledford 			}
6067820b071SDoug Ledford 			break;
6077820b071SDoug Ledford 		case 'p':
6087820b071SDoug Ledford 			/*
6097820b071SDoug Ledford 			 * Although we can create a msg queue with a
6107820b071SDoug Ledford 			 * non-absolute path name, unlink will fail.  So,
6117820b071SDoug Ledford 			 * if the name doesn't start with a /, add one
6127820b071SDoug Ledford 			 * when we save it.
6137820b071SDoug Ledford 			 */
6147820b071SDoug Ledford 			option = queue_path;
6157820b071SDoug Ledford 			if (*option != '/') {
6167820b071SDoug Ledford 				queue_path = malloc(strlen(option) + 2);
6177820b071SDoug Ledford 				if (!queue_path) {
6187820b071SDoug Ledford 					perror("malloc()");
619ce64763cSAthira Rajeev 					goto err_code;
6207820b071SDoug Ledford 				}
6217820b071SDoug Ledford 				queue_path[0] = '/';
6227820b071SDoug Ledford 				queue_path[1] = 0;
6237820b071SDoug Ledford 				strcat(queue_path, option);
6247820b071SDoug Ledford 				free(option);
6257820b071SDoug Ledford 			}
6267820b071SDoug Ledford 			break;
6277820b071SDoug Ledford 		}
6287820b071SDoug Ledford 	}
6297820b071SDoug Ledford 
6307820b071SDoug Ledford 	if (continuous_mode && num_cpus_to_pin == 0) {
6317820b071SDoug Ledford 		fprintf(stderr, "Must pass at least one CPU to continuous "
6327820b071SDoug Ledford 			"mode.\n");
6337820b071SDoug Ledford 		poptPrintUsage(popt_context, stderr, 0);
634ce64763cSAthira Rajeev 		goto err_code;
6357820b071SDoug Ledford 	} else if (!continuous_mode) {
6367820b071SDoug Ledford 		num_cpus_to_pin = 1;
6377820b071SDoug Ledford 		cpus_to_pin[0] = cpus_online - 1;
6387820b071SDoug Ledford 	}
6397820b071SDoug Ledford 
6407820b071SDoug Ledford 	max_msgs = fopen(MAX_MSGS, "r+");
6417820b071SDoug Ledford 	max_msgsize = fopen(MAX_MSGSIZE, "r+");
6427820b071SDoug Ledford 	if (!max_msgs)
6437820b071SDoug Ledford 		shutdown(2, "Failed to open msg_max", __LINE__);
6447820b071SDoug Ledford 	if (!max_msgsize)
6457820b071SDoug Ledford 		shutdown(2, "Failed to open msgsize_max", __LINE__);
6467820b071SDoug Ledford 
6477820b071SDoug Ledford 	/* Load up the current system values for everything we can */
6487820b071SDoug Ledford 	getr(RLIMIT_MSGQUEUE, &saved_limits);
6497820b071SDoug Ledford 	cur_limits = saved_limits;
6507820b071SDoug Ledford 	saved_max_msgs = cur_max_msgs = get(max_msgs);
6517820b071SDoug Ledford 	saved_max_msgsize = cur_max_msgsize = get(max_msgsize);
6527820b071SDoug Ledford 	errno = 0;
6537820b071SDoug Ledford 	cur_nice = getpriority(PRIO_PROCESS, 0);
6547820b071SDoug Ledford 	if (errno)
6557820b071SDoug Ledford 		shutdown(2, "getpriority()", __LINE__);
6567820b071SDoug Ledford 
6577820b071SDoug Ledford 	/* Tell the user our initial state */
6587820b071SDoug Ledford 	printf("\nInitial system state:\n");
6597820b071SDoug Ledford 	printf("\tUsing queue path:\t\t\t%s\n", queue_path);
660f15fed3dSShuah Khan 	printf("\tRLIMIT_MSGQUEUE(soft):\t\t\t%ld\n",
661f15fed3dSShuah Khan 		(long) saved_limits.rlim_cur);
662f15fed3dSShuah Khan 	printf("\tRLIMIT_MSGQUEUE(hard):\t\t\t%ld\n",
663f15fed3dSShuah Khan 		(long) saved_limits.rlim_max);
6647820b071SDoug Ledford 	printf("\tMaximum Message Size:\t\t\t%d\n", saved_max_msgsize);
6657820b071SDoug Ledford 	printf("\tMaximum Queue Size:\t\t\t%d\n", saved_max_msgs);
6667820b071SDoug Ledford 	printf("\tNice value:\t\t\t\t%d\n", cur_nice);
6677820b071SDoug Ledford 	printf("\n");
6687820b071SDoug Ledford 
6697820b071SDoug Ledford 	increase_limits();
6707820b071SDoug Ledford 
6717820b071SDoug Ledford 	printf("Adjusted system state for testing:\n");
6727820b071SDoug Ledford 	if (cur_limits.rlim_cur == RLIM_INFINITY) {
6737820b071SDoug Ledford 		printf("\tRLIMIT_MSGQUEUE(soft):\t\t\t(unlimited)\n");
6747820b071SDoug Ledford 		printf("\tRLIMIT_MSGQUEUE(hard):\t\t\t(unlimited)\n");
6757820b071SDoug Ledford 	} else {
676f15fed3dSShuah Khan 		printf("\tRLIMIT_MSGQUEUE(soft):\t\t\t%ld\n",
677f15fed3dSShuah Khan 		       (long) cur_limits.rlim_cur);
678f15fed3dSShuah Khan 		printf("\tRLIMIT_MSGQUEUE(hard):\t\t\t%ld\n",
679f15fed3dSShuah Khan 		       (long) cur_limits.rlim_max);
6807820b071SDoug Ledford 	}
6817820b071SDoug Ledford 	printf("\tMaximum Message Size:\t\t\t%d\n", cur_max_msgsize);
6827820b071SDoug Ledford 	printf("\tMaximum Queue Size:\t\t\t%d\n", cur_max_msgs);
6837820b071SDoug Ledford 	printf("\tNice value:\t\t\t\t%d\n", cur_nice);
6847820b071SDoug Ledford 	printf("\tContinuous mode:\t\t\t(%s)\n", continuous_mode ?
6857820b071SDoug Ledford 	       (continuous_mode_fake ? "fake mode" : "enabled") :
6867820b071SDoug Ledford 	       "disabled");
6877820b071SDoug Ledford 	printf("\tCPUs to pin:\t\t\t\t%d", cpus_to_pin[0]);
6887820b071SDoug Ledford 	for (cpu = 1; cpu < num_cpus_to_pin; cpu++)
6897820b071SDoug Ledford 			printf(",%d", cpus_to_pin[cpu]);
6907820b071SDoug Ledford 	printf("\n");
6917820b071SDoug Ledford 
6927820b071SDoug Ledford 	sa.sa_sigaction = sig_action_SIGUSR1;
6937820b071SDoug Ledford 	sigemptyset(&sa.sa_mask);
6947820b071SDoug Ledford 	sigaddset(&sa.sa_mask, SIGHUP);
6957820b071SDoug Ledford 	sigaddset(&sa.sa_mask, SIGINT);
6967820b071SDoug Ledford 	sigaddset(&sa.sa_mask, SIGQUIT);
6977820b071SDoug Ledford 	sigaddset(&sa.sa_mask, SIGTERM);
6987820b071SDoug Ledford 	sa.sa_flags = SA_SIGINFO;
6997820b071SDoug Ledford 	if (sigaction(SIGUSR1, &sa, NULL) == -1)
7007820b071SDoug Ledford 		shutdown(1, "sigaction(SIGUSR1)", __LINE__);
7017820b071SDoug Ledford 	sa.sa_sigaction = sig_action;
7027820b071SDoug Ledford 	if (sigaction(SIGHUP, &sa, NULL) == -1)
7037820b071SDoug Ledford 		shutdown(1, "sigaction(SIGHUP)", __LINE__);
7047820b071SDoug Ledford 	if (sigaction(SIGINT, &sa, NULL) == -1)
7057820b071SDoug Ledford 		shutdown(1, "sigaction(SIGINT)", __LINE__);
7067820b071SDoug Ledford 	if (sigaction(SIGQUIT, &sa, NULL) == -1)
7077820b071SDoug Ledford 		shutdown(1, "sigaction(SIGQUIT)", __LINE__);
7087820b071SDoug Ledford 	if (sigaction(SIGTERM, &sa, NULL) == -1)
7097820b071SDoug Ledford 		shutdown(1, "sigaction(SIGTERM)", __LINE__);
7107820b071SDoug Ledford 
7117820b071SDoug Ledford 	if (!continuous_mode_fake) {
7127820b071SDoug Ledford 		attr.mq_flags = O_NONBLOCK;
7137820b071SDoug Ledford 		attr.mq_maxmsg = cur_max_msgs;
7147820b071SDoug Ledford 		attr.mq_msgsize = MSG_SIZE;
7157820b071SDoug Ledford 		open_queue(&attr);
7167820b071SDoug Ledford 	}
7177820b071SDoug Ledford 	for (i = 0; i < num_cpus_to_pin; i++) {
7187820b071SDoug Ledford 		pthread_attr_t thread_attr;
7197820b071SDoug Ledford 		void *thread_func;
7207820b071SDoug Ledford 
7217820b071SDoug Ledford 		if (continuous_mode_fake)
7227820b071SDoug Ledford 			thread_func = &fake_cont_thread;
7237820b071SDoug Ledford 		else if (continuous_mode)
7247820b071SDoug Ledford 			thread_func = &cont_thread;
7257820b071SDoug Ledford 		else
7267820b071SDoug Ledford 			thread_func = &perf_test_thread;
7277820b071SDoug Ledford 
7287820b071SDoug Ledford 		CPU_ZERO_S(cpu_set_size, cpu_set);
7297820b071SDoug Ledford 		CPU_SET_S(cpus_to_pin[i], cpu_set_size, cpu_set);
7307820b071SDoug Ledford 		pthread_attr_init(&thread_attr);
7317820b071SDoug Ledford 		pthread_attr_setaffinity_np(&thread_attr, cpu_set_size,
7327820b071SDoug Ledford 					    cpu_set);
7337820b071SDoug Ledford 		if (pthread_create(&cpu_threads[i], &thread_attr, thread_func,
7347820b071SDoug Ledford 				   NULL))
7357820b071SDoug Ledford 			shutdown(1, "pthread_create()", __LINE__);
7367820b071SDoug Ledford 		pthread_attr_destroy(&thread_attr);
7377820b071SDoug Ledford 	}
7387820b071SDoug Ledford 
7397820b071SDoug Ledford 	if (!continuous_mode) {
7407820b071SDoug Ledford 		pthread_join(cpu_threads[0], &retval);
7417820b071SDoug Ledford 		shutdown((long)retval, "perf_test_thread()", __LINE__);
7427820b071SDoug Ledford 	} else {
7437820b071SDoug Ledford 		while (1)
7447820b071SDoug Ledford 			sleep(1);
7457820b071SDoug Ledford 	}
7467820b071SDoug Ledford 	shutdown(0, "", 0);
747ce64763cSAthira Rajeev 
748ce64763cSAthira Rajeev err_code:
749ce64763cSAthira Rajeev 	CPU_FREE(cpu_set);
750ce64763cSAthira Rajeev 	exit(1);
751ce64763cSAthira Rajeev 
7527820b071SDoug Ledford }
753