1ab61854John Levon/*
2ab61854John Levon * This file and its contents are supplied under the terms of the
3ab61854John Levon * Common Development and Distribution License ("CDDL"), version 1.0.
4ab61854John Levon * You may only use this file in accordance with the terms of version
5ab61854John Levon * 1.0 of the CDDL.
6ab61854John Levon *
7ab61854John Levon * A full copy of the text of the CDDL should have accompanied this
8ab61854John Levon * source.  A copy of the CDDL is also available via the Internet at
9ab61854John Levon * http://www.illumos.org/license/CDDL.
10ab61854John Levon */
11ab61854John Levon
12ab61854John Levon/*
13ab61854John Levon * Copyright 2018 Joyent, Inc.
14ab61854John Levon */
15ab61854John Levon
16ab61854John Levon/*
17ab61854John Levon * All we're doing is constantly modifying a thread name while DTrace is
18ab61854John Levon * watching us, making sure we don't break.
19ab61854John Levon */
20ab61854John Levon
21ab61854John Levon#include <sys/fcntl.h>
22ab61854John Levon#include <pthread.h>
23ab61854John Levon#include <stdlib.h>
24ab61854John Levon#include <stdio.h>
25ab61854John Levon
26ab61854John Levon#define	NR_THREADS (100)
27ab61854John Levon#define	RUNTIME (30) /* seconds */
28ab61854John Levon
29ab61854John Levonstatic void
30ab61854John Levonrandom_ascii(char *buf, size_t bufsize)
31ab61854John Levon{
32ab61854John Levon	char table[] = "abcdefghijklmnopqrstuvwxyz"
33ab61854John Levon	    "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789 ,.-#'?!";
34ab61854John Levon	size_t len = rand() % bufsize;
35ab61854John Levon
36ab61854John Levon	bzero(buf, bufsize);
37ab61854John Levon
38ab61854John Levon	for (size_t i = 0; i < len; i++) {
39ab61854John Levon		buf[i] = table[rand() % (sizeof (table) - 1)];
40ab61854John Levon	}
41ab61854John Levon}
42ab61854John Levon
43ab61854John Levonstatic void
44ab61854John Levonbusy()
45ab61854John Levon{
46ab61854John Levon	struct timeval tv1;
47ab61854John Levon	struct timeval tv2;
48ab61854John Levon
49ab61854John Levon	if (gettimeofday(&tv1, NULL) != 0)
50ab61854John Levon		abort();
51ab61854John Levon
52ab61854John Levon	for (;;) {
53ab61854John Levon		static volatile int i;
54ab61854John Levon		for (i = 0; i < 2000000; i++)
55ab61854John Levon			;
56ab61854John Levon
57ab61854John Levon		if (gettimeofday(&tv2, NULL) != 0)
58ab61854John Levon			abort();
59ab61854John Levon
60ab61854John Levon		/* janky, but we don't care */
61ab61854John Levon		if (tv2.tv_sec != tv1.tv_sec)
62ab61854John Levon			return;
63ab61854John Levon	}
64ab61854John Levon}
65ab61854John Levon
66ab61854John Levonstatic void *
67ab61854John Levonthread(void *arg)
68ab61854John Levon{
69ab61854John Levon	char name[PTHREAD_MAX_NAMELEN_NP];
70ab61854John Levon
71ab61854John Levon	for (size_t i = 0; ; i++) {
72ab61854John Levon		random_ascii(name, sizeof (name));
73ab61854John Levon
74ab61854John Levon		if ((i % 100) == 0) {
75ab61854John Levon			if (pthread_setname_np(pthread_self(), NULL) != 0)
76ab61854John Levon				abort();
77ab61854John Levon		} else {
78ab61854John Levon			(void) pthread_setname_np(pthread_self(), name);
79ab61854John Levon		}
80ab61854John Levon
81ab61854John Levon		busy();
82ab61854John Levon	}
83ab61854John Levon
84ab61854John Levon	return (NULL);
85ab61854John Levon}
86ab61854John Levon
87ab61854John Levonint
88ab61854John Levonmain(int argc, char **argv)
89ab61854John Levon{
90ab61854John Levon	pthread_t tids[NR_THREADS];
91ab61854John Levon
92ab61854John Levon	for (size_t i = 0; i < NR_THREADS; i++) {
93ab61854John Levon		if (pthread_create(&tids[i], NULL, thread, NULL) != 0)
94ab61854John Levon			exit(EXIT_FAILURE);
95ab61854John Levon	}
96ab61854John Levon
97ab61854John Levon	sleep(RUNTIME);
98ab61854John Levon	exit(EXIT_SUCCESS);
99ab61854John Levon}