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 fork
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
43static barrier_t		*b;
44
45typedef struct {
46	int			ts_once;
47	int			*ts_pids;
48} tsd_t;
49
50int
51benchmark_init()
52{
53	lm_tsdsize = sizeof (tsd_t);
54	(void) sprintf(lm_usage, "notes: measures fork()\n");
55
56	return (0);
57}
58
59int
60benchmark_initrun()
61{
62	b = barrier_create(lm_optP * lm_optT * (lm_optB + 1), 0);
63
64	return (0);
65}
66
67int
68benchmark_finirun()
69{
70	(void) barrier_destroy(b);
71
72	return (0);
73}
74
75int
76benchmark_initbatch(void *tsd)
77{
78	tsd_t			*ts = (tsd_t *)tsd;
79	int			errors = 0;
80
81	if (ts->ts_once++ == 0) {
82		ts->ts_pids = (int *)malloc(lm_optB * sizeof (pid_t));
83		if (ts->ts_pids == NULL) {
84			errors++;
85		}
86	}
87
88	return (errors);
89}
90
91int
92benchmark(void *tsd, result_t *res)
93{
94	tsd_t			*ts = (tsd_t *)tsd;
95	int			i;
96
97	for (i = 0; i < lm_optB; i++) {
98		ts->ts_pids[i] = fork();
99		switch (ts->ts_pids[i]) {
100		case 0:
101			(void) barrier_queue(b, NULL);
102			exit(0);
103			break;
104		case -1:
105			res->re_errors++;
106			break;
107		default:
108			continue;
109		}
110	}
111	res->re_count = lm_optB;
112
113	(void) barrier_queue(b, NULL);
114
115	return (0);
116}
117
118int
119benchmark_finibatch(void *tsd)
120{
121	tsd_t			*ts = (tsd_t *)tsd;
122	int			i;
123
124	for (i = 0; i < lm_optB; i++) {
125		if (ts->ts_pids[i] > 0) {
126			(void) waitpid(ts->ts_pids[i], NULL, 0);
127		}
128	}
129
130	return (0);
131}
132