]> git.saurik.com Git - apple/xnu.git/blame - tools/tests/libMicro/malloc.c
xnu-3248.60.10.tar.gz
[apple/xnu.git] / tools / tests / libMicro / malloc.c
CommitLineData
b0d623f7
A
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 int tmp;
75 switch (opt) {
76 case 'g':
77 optg = sizetoint(optarg);
78 break;
79 case 's':
80 opts[optscnt] = sizetoint(optarg);
81 tmp = ((++optscnt) & (0x1F));
82 optscnt = tmp;
83 break;
84 default:
85 return (-1);
86 }
87
88 return (0);
89}
90
91int
92benchmark_initworker(void *tsd)
93{
94 tsd_t *ts = (tsd_t *)tsd;
95
96 if (optscnt == 0)
97 optscnt = 1;
98
99 ts->ts_glob = malloc(sizeof (void *)* optg);
100 if (ts->ts_glob == NULL) {
101 return (1);
102 }
103 return (0);
104}
105
106int
107benchmark(void *tsd, result_t *res)
108{
109 tsd_t *ts = (tsd_t *)tsd;
110 int i, j, k;
111
112 for (i = 0; i < lm_optB; i++) {
113 for (k = j = 0; j < optg; j++) {
114 if ((ts->ts_glob[j] = malloc(opts[k++])) == NULL)
115 res->re_errors++;
116 if (k >= optscnt)
117 k = 0;
118 }
119 for (j = 0; j < optg; j++) {
120 free(ts->ts_glob[j]);
121 }
122 }
123
124 res->re_count = i * j;
125
126 return (0);
127}
128
129char *
130benchmark_result()
131{
132 static char result[256];
133 int i;
134
135 (void) sprintf(result, "%6d ", optg);
136
137 for (i = 0; i < optscnt; i++)
138 (void) sprintf(result + strlen(result), "%d ", opts[i]);
139 return (result);
140}