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 "@(#)trivial.c 1.0 08/17/06 Apple Inc." 59#endif 60 61 62 63#include <unistd.h> 64#include <stdlib.h> 65#include <stdio.h> 66#include <stdbool.h> 67#include <string.h> 68// add additional headers needed here. 69#include <fcntl.h> 70 71#include "../libmicro.h" 72 73#if DEBUG 74# define debug(fmt, args...) (void) fprintf(stderr, fmt "\n" , ##args) 75#else 76# define debug(fmt, args...) 77#endif 78 79#define MAXPATHLEN 1024 80/* 81 * Your state variables should live in the tsd_t struct below 82 */ 83typedef struct { 84 int ts_once; 85} tsd_t; 86 87/* 88 * You can have any lower-case option you want to define. 89 * options are specified in the lm_optstr as either a 90 * single lower-case letter, or a single lower case letter 91 * with a colon after it. In this example, you can optionally 92 * specify -c {str} -e or -t {number} 93 * -c takes a string (quote the string if blanks) 94 * -e is a boolean 95 * -t takes a numeric 96 * argument. 97 */ 98static char * optf; // allocated in benchmark_init, freed in benchmark_fini. 99 100 101int 102benchmark_init() 103{ 104 debug("benchmark_init\n"); 105 /* 106 * the lm_optstr must be defined here or no options for you 107 * 108 * ...and the framework will throw an error 109 * 110 */ 111 (void) sprintf(lm_optstr, "f:"); 112 /* 113 * tsd_t is the state info struct that we pass around 114 * 115 * lm_tsdsize will allocate the space we need for this 116 * structure throughout the rest of the framework 117 */ 118 lm_tsdsize = sizeof (tsd_t); 119 120 (void) sprintf(lm_usage, 121 " -f filename\n" 122 "notes: measures file creation using open(2)\n"); 123 124 optf = malloc(MAXPATHLEN); 125 sprintf(optf, "/tmp/create_file_%d", getpid()); 126 return (0); 127} 128 129/* 130 * This is where you parse your lower-case arguments. 131 * the format was defined in the lm_optstr assignment 132 * in benchmark_init 133 */ 134int 135benchmark_optswitch(int opt, char *optarg) 136{ 137 debug("benchmark_optswitch\n"); 138 139 switch (opt) { 140 case 'f': 141 strncpy(optf, optarg, 20); 142 (void)fprintf(stderr, "optf = %s\n", optf); 143 break; 144 default: 145 return (-1); 146 } 147 return (0); 148} 149 150int 151benchmark_initrun() 152{ 153 debug("benchmark_initrun\n"); 154 return (0); 155} 156 157int 158benchmark_initworker(void *tsd) 159{ 160 /* 161 * initialize your state variables here first 162 */ 163// tsd_t *ts = (tsd_t *)tsd; 164// debug("benchmark_initworker: ts_once = %i\n",ts->ts_once); 165 return (0); 166} 167 168/*ARGSUSED*/ 169int 170benchmark_initbatch(void *tsd) 171{ 172 /* 173 * initialize your state variables here second 174 */ 175 tsd_t *ts = (tsd_t *)tsd; 176 // useless code to show what you can do. 177 ts->ts_once++; 178 ts->ts_once--; 179 debug("benchmark_initbatch: ts_once = %i\n",ts->ts_once); 180 return (0); 181} 182 183int 184benchmark(void *tsd, result_t *res) 185{ 186 /* 187 * try not to initialize things here. This is the main 188 * loop of things to get timed. Start a server in 189 * benchmark_initbatch 190 */ 191// tsd_t *ts = (tsd_t *)tsd; 192 int i; 193 194 debug("in to benchmark - optB = %i : ts_once = %i\n", lm_optB, ts->ts_once); 195 for (i = 0; i < lm_optB; i++) { 196 if (!open(optf, O_CREAT)) 197 res->re_errors++; 198 } 199 res->re_count = i; 200 debug("out of benchmark - optB = %i : ts_once = %i\n", lm_optB, ts->ts_once); 201 202 return (0); 203} 204 205int 206benchmark_finibatch(void *tsd) 207{ 208// tsd_t *ts = (tsd_t *)tsd; 209// debug("benchmark_finibatch: ts_once = %i\n",ts->ts_once); 210 return (0); 211} 212 213int 214benchmark_finiworker(void *tsd) 215{ 216 tsd_t *ts = (tsd_t *)tsd; 217 // useless code to show what you can do. 218 ts->ts_once++; 219 ts->ts_once--; 220 debug("benchmark_finiworker: ts_once = %i\n",ts->ts_once); 221 return (0); 222} 223 224char * 225benchmark_result() 226{ 227 static char result = '\0'; 228 debug("benchmark_result\n"); 229 return (&result); 230} 231 232int 233benchmark_finirun() 234{ 235 debug("benchmark_finirun\n"); 236 return (0); 237} 238 239 240int 241benchmark_fini() 242{ 243 debug("benchmark_fini\n"); 244 free(optf); 245 return (0); 246} 247 248