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 2007 Sun Microsystems, Inc.  All rights reserved.
28 * Use is subject to license terms.
29 */
30
31/*
32 * malloc benchmark (crude)
33 */
34
35
36#include <unistd.h>
37#include <stdlib.h>
38#include <stdio.h>
39#include <fcntl.h>
40#include <string.h>
41
42#include "libmicro.h"
43
44static int		optg = 100;
45static int		opts[32] = {32};
46static int		optscnt = 0;
47
48typedef struct {
49	void 			**ts_glob;
50} tsd_t;
51
52int
53benchmark_init()
54{
55	lm_tsdsize = sizeof (tsd_t);
56
57	(void) sprintf(lm_optstr, "s:g:");
58
59	(void) sprintf(lm_usage,
60	    "       [-g number of mallocs before free (default %d)]\n"
61	    "       [-s size to malloc (default %d)."
62	    "  Up to 32 sizes accepted\n"
63	    "notes: measures malloc()/free()",
64	    optg, opts[0]);
65
66	(void) sprintf(lm_header, "%6s %6s", "glob", "sizes");
67
68	return (0);
69}
70
71int
72benchmark_optswitch(int opt, char *optarg)
73{
74	switch (opt) {
75	case 'g':
76		optg = sizetoint(optarg);
77		break;
78	case 's':
79		opts[optscnt] = sizetoint(optarg);
80		optscnt = ++optscnt & (31);
81		break;
82	default:
83		return (-1);
84	}
85
86	return (0);
87}
88
89int
90benchmark_initworker(void *tsd)
91{
92	tsd_t			*ts = (tsd_t *)tsd;
93
94	if (optscnt == 0)
95		optscnt = 1;
96
97	ts->ts_glob = malloc(sizeof (void *)* optg);
98	if (ts->ts_glob == NULL) {
99		return (1);
100	}
101	return (0);
102}
103
104int
105benchmark(void *tsd, result_t *res)
106{
107	tsd_t			*ts = (tsd_t *)tsd;
108	int			i, j, k;
109
110	for (i = 0; i < lm_optB; i++) {
111		for (k = j = 0; j < optg; j++) {
112			if ((ts->ts_glob[j] = malloc(opts[k++])) == NULL)
113				res->re_errors++;
114			if (k >= optscnt)
115				k = 0;
116		}
117		for (j = 0; j < optg; j++) {
118			free(ts->ts_glob[j]);
119		}
120	}
121
122	res->re_count = i * j;
123
124	return (0);
125}
126
127char *
128benchmark_result()
129{
130	static char  result[256];
131	int i;
132
133	(void) sprintf(result, "%6d ", optg);
134
135	for (i = 0; i < optscnt; i++)
136		(void) sprintf(result + strlen(result), "%d ", opts[i]);
137	return (result);
138}
139