1/*
2 * CDDL HEADER START
3 *
4 * The contents of this file are subject to the terms
5 * of the Common Development and Distribution License
6 * (the "License").  You may not use this file except
7 * in compliance with the License.
8 *
9 * You can obtain a copy of the license at
10 * src/OPENSOLARIS.LICENSE
11 * or http://www.opensolaris.org/os/licensing.
12 * See the License for the specific language governing
13 * permissions and limitations under the License.
14 *
15 * When distributing Covered Code, include this CDDL
16 * HEADER in each file and include the License file at
17 * usr/src/OPENSOLARIS.LICENSE.  If applicable,
18 * add the following below this CDDL HEADER, with the
19 * fields enclosed by brackets "[]" replaced with your
20 * own identifying information: Portions Copyright [yyyy]
21 * [name of copyright owner]
22 *
23 * CDDL HEADER END
24 */
25
26/*
27 * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
28 * Use is subject to license terms.
29 */
30
31/*
32 * benchmark exit
33 */
34
35#include <sys/types.h>
36#include <sys/wait.h>
37#include <unistd.h>
38#include <stdlib.h>
39#include <stdio.h>
40
41#include "libmicro.h"
42
43typedef struct {
44	int			ts_once;
45	int			*ts_pids;
46} tsd_t;
47
48static int			opte = 0;
49static barrier_t		*b;
50
51int
52benchmark_init()
53{
54	lm_tsdsize = sizeof (tsd_t);
55	(void) sprintf(lm_optstr, "e");
56
57	(void) sprintf(lm_usage,
58	    "       [-e] (uses _exit() rather than exit())"
59	    "notes: measures exit()\n");
60
61	return (0);
62}
63
64/*ARGSUSED*/
65int
66benchmark_optswitch(int opt, char *optarg)
67{
68	switch (opt) {
69	case 'e':
70		opte = 1;
71		break;
72	default:
73		return (-1);
74	}
75	return (0);
76}
77
78int
79benchmark_initrun()
80{
81	b = barrier_create(lm_optP * lm_optT * (lm_optB + 1), 0);
82
83	return (0);
84}
85
86int
87benchmark_finirun()
88{
89	(void) barrier_destroy(b);
90
91	return (0);
92}
93
94int
95benchmark_initbatch(void *tsd)
96{
97	tsd_t			*ts = (tsd_t *)tsd;
98	int			i;
99	int			errors = 0;
100
101	if (ts->ts_once++ == 0) {
102		ts->ts_pids = (int *)malloc(lm_optB * sizeof (pid_t));
103		if (ts->ts_pids == NULL) {
104			errors ++;
105		}
106	}
107
108	/*
109	 * create processes to exit
110	 */
111
112	for (i = 0; i < lm_optB; i++) {
113		ts->ts_pids[i] = fork();
114		switch (ts->ts_pids[i]) {
115		case 0:
116			(void) barrier_queue(b, NULL);
117			if (opte)
118				_exit(0);
119			exit(0);
120			break;
121		case -1:
122			errors ++;
123			break;
124		default:
125			continue;
126		}
127	}
128
129	return (errors);
130}
131
132/*ARGSUSED*/
133int
134benchmark(void *tsd, result_t *res)
135{
136	int			i;
137
138	/*
139	 * start them all exiting
140	 */
141
142	(void) barrier_queue(b, NULL);
143
144	/*
145	 * wait for them all to exit
146	 */
147
148	for (i = 0; i < lm_optB; i++) {
149		switch (waitpid((pid_t)-1, NULL, 0)) {
150		case 0:
151			continue;
152		case -1:
153			res->re_errors++;
154		}
155	}
156
157	res->re_count = i;
158
159	return (0);
160}
161