1/*
2 * Copyright (c) 2006 Apple Inc.  All Rights Reserved.
3 *
4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
5 *
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. The rights granted to you under the License
10 * may not be used to create, or enable the creation or redistribution of,
11 * unlawful or unlicensed copies of an Apple operating system, or to
12 * circumvent, violate, or enable the circumvention or violation of, any
13 * terms of an Apple operating system software license agreement.
14 *
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
17 *
18 * The Original Code and all software distributed under the License are
19 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23 * Please see the License for the specific language governing rights and
24 * limitations under the License.
25 *
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
27 */
28
29
30/*
31 *	Order of Execution
32 *
33 *	benchmark_init
34 *
35 *	benchmark_optswitch
36 *
37 *		benchmark_initrun
38 *
39 *			benchmark_initworker
40 *				benchmark_initbatch
41 *					benchmark
42 *				benchmark_finibatch
43 *				benchmark_initbatch
44 *					benchmark
45 *				benchmark_finibatch, etc.
46 *			benchmark_finiworker
47 *
48 *		benchmark_result
49 *
50 *		benchmark_finirun
51 *
52 *	benchmark_fini
53 */
54
55
56
57#ifdef	__sun
58#pragma ident	"@(#)vm_allocate.c	1.0	09/17/06 Apple Inc."
59#endif
60
61
62
63#include <unistd.h>
64#include <stdlib.h>
65#include <stdio.h>
66#include <string.h>
67#include <mach/mach.h>
68
69#include "../libmicro.h"
70
71/*
72 *	Your state variables should live in the tsd_t struct below
73 */
74typedef struct {
75        int     ts_once;
76} tsd_t;
77
78unsigned char * arena;
79unsigned int    arenaSize = 1;
80
81static int 	optt = 0;
82
83/*ARGSUSED*/
84int
85benchmark_initbatch(void *tsd)
86{
87	/*
88	 * initialize your state variables here second
89	 */
90	//tsd_t			*ts = (tsd_t *)tsd;
91	//(void) fprintf(stderr, "benchmark_initbatch: ts_once = %i\n",ts->ts_once);
92	return (0);
93}
94
95int
96benchmark_finirun()
97{
98	(void) fprintf(stderr, "benchmark_finirun\n");
99	return (0);
100}
101
102int
103benchmark_init()
104{
105	(void) fprintf(stderr, "benchmark_init\n");
106	/*
107	 *	the lm_optstr must be defined here or no options for you
108	 *
109	 * 	...and the framework will throw an error
110	 *
111	 */
112	(void) sprintf(lm_optstr, "t:");
113	/*
114	 *	working hypothesis:
115	 *
116	 * 	tsd_t is the struct that we can pass around our
117	 *	state info in
118	 *
119	 *	lm_tsdsize will allocate the space we need for this
120	 *	structure throughout the rest of the framework
121	 */
122	lm_tsdsize = sizeof (tsd_t);
123	lm_defB = 1;
124
125
126	(void) sprintf(lm_usage,
127	    "       [-t int (default 1)]\n"
128	    "notes: measures nothing\n");
129	return (0);
130}
131
132int
133benchmark_fini()
134{
135	(void) fprintf(stderr, "benchmark_fini\n");
136	return (0);
137}
138
139int
140benchmark_finibatch(void *tsd)
141{
142	tsd_t			*ts = (tsd_t *)tsd;
143	/*
144	 *	more proof of state passing
145	 */
146	ts->ts_once = optt;
147	//(void) fprintf(stderr, "benchmark_finibatch: ts_once = %i\n",ts->ts_once);
148	return (0);
149}
150
151char *
152benchmark_result()
153{
154	static char		result = '\0';
155	(void) fprintf(stderr, "benchmark_result\n");
156	return (&result);
157}
158
159int
160benchmark_finiworker(void *tsd)
161{
162	//tsd_t			*ts = (tsd_t *)tsd;
163	//(void) fprintf(stderr, "benchmark_finiworker: ts_once = %i\n",ts->ts_once);
164	//vm_deallocate( mach_task_self(), (vm_address_t) arena, arenaSize * vm_page_size);
165
166	return (0);
167}
168
169int
170benchmark_optswitch(int opt, char *optarg)
171{
172	(void) fprintf(stderr, "benchmark_optswitch\n");
173
174	switch (opt) {
175	case 't':
176		optt = sizetoint(optarg);
177		break;
178	default:
179		return (-1);
180	}
181	return (0);
182}
183
184int
185benchmark_initworker(void *tsd)
186{
187	/*
188	 *	initialize your state variables here first
189	 */
190	//tsd_t			*ts = (tsd_t *)tsd;
191	//ts->ts_once = optt;
192	//(void) fprintf(stderr, "benchmark_initworker: ts_once = %i\n",ts->ts_once);
193	if ( optt > 0 ) {
194		arenaSize = optt;
195	}
196	// warmup
197	vm_allocate( mach_task_self(), (vm_address_t *) &arena, arenaSize * vm_page_size, 1);
198
199	vm_deallocate( mach_task_self(), (vm_address_t) arena, arenaSize * vm_page_size);
200	//arena = ( unsigned char * )malloc( arenaSize);
201	return (0);
202}
203
204int
205benchmark_initrun()
206{
207	//(void) fprintf(stderr, "benchmark_initrun\n");
208	return (0);
209}
210
211int
212benchmark(void *tsd, result_t *res)
213{
214	/*
215	 *	initialize your state variables here last
216	 *
217	 * 	and realize that you are paying for your initialization here
218	 *	and it is really a bad idea
219	 */
220	//tsd_t			*ts = (tsd_t *)tsd;
221	int			i;
222
223	//(void) fprintf(stderr, "in to benchmark - optB = %i\n", lm_optB);
224	for (i = 0; i < lm_optB; i++) {
225		/*
226		 *	just to show that ts really contains state
227		 */
228		 //(void) fprintf(stderr, "i is %i\n",i);
229		if (vm_allocate( mach_task_self(), (vm_address_t *) &arena, arenaSize * vm_page_size, 1))
230			abort();
231		if (vm_deallocate( mach_task_self(), (vm_address_t) arena, arenaSize * vm_page_size))
232			abort();
233
234	}
235	res->re_count = i;
236	//(void) fprintf(stderr, "out of benchmark - optB = %i : ts_once = %i\n", lm_optB, ts->ts_once);
237
238	return (0);
239}
240