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 70#include "../libmicro.h" 71 72#if DEBUG 73# define debug(fmt, args...) (void) fprintf(stderr, fmt "\n" , ##args) 74#else 75# define debug(fmt, args...) 76#endif 77 78/* 79 * Your state variables should live in the tsd_t struct below 80 */ 81typedef struct { 82 int ts_once; 83} tsd_t; 84 85/* 86 * You can have any lower-case option you want to define. 87 * options are specified in the lm_optstr as either a 88 * single lower-case letter, or a single lower case letter 89 * with a colon after it. In this example, you can optionally 90 * specify -c {str} -e or -t {number} 91 * -c takes a string (quote the string if blanks) 92 * -e is a boolean 93 * -t takes a numeric 94 * argument. 95 */ 96static char * optc; // allocated in benchmark_init, freed in benchmark_fini. 97static bool opte = false; 98static int optt = 1; 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, "c:et:"); 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 " [-c string]\n" 122 " [-e] optional parameter\n" 123 " [-t int (default 1)]\n" 124 "notes: measures nothing\n"); 125 126 optc = malloc(20); 127 return (0); 128} 129 130/* 131 * This is where you parse your lower-case arguments. 132 * the format was defined in the lm_optstr assignment 133 * in benchmark_init 134 */ 135int 136benchmark_optswitch(int opt, char *optarg) 137{ 138 debug("benchmark_optswitch\n"); 139 140 switch (opt) { 141 case 'c': 142 strncpy(optc, optarg, 20); 143 debug("optc = %s\n", optc); 144 break; 145 case 'e': 146 opte = true; 147 debug("opte = %s\n", opte? "true": "false"); 148 break; 149 case 't': 150 optt = sizetoint(optarg); 151 debug("optt = %d\n", optt); 152 break; 153 default: 154 return (-1); 155 } 156 return (0); 157} 158 159int 160benchmark_initrun() 161{ 162 debug("benchmark_initrun\n"); 163 return (0); 164} 165 166int 167benchmark_initworker(void *tsd) 168{ 169 /* 170 * initialize your state variables here first 171 */ 172 tsd_t *ts = (tsd_t *)tsd; 173 ts->ts_once = optt; 174 debug("benchmark_initworker: ts_once = %i\n",ts->ts_once); 175 return (0); 176} 177 178/*ARGSUSED*/ 179int 180benchmark_initbatch(void *tsd) 181{ 182 /* 183 * initialize your state variables here second 184 */ 185 tsd_t *ts = (tsd_t *)tsd; 186 // useless code to show what you can do. 187 ts->ts_once++; 188 ts->ts_once--; 189 debug("benchmark_initbatch: ts_once = %i\n",ts->ts_once); 190 return (0); 191} 192 193int 194benchmark(void *tsd, result_t *res) 195{ 196 /* 197 * try not to initialize things here. This is the main 198 * loop of things to get timed. Start a server in 199 * benchmark_initbatch 200 */ 201 tsd_t *ts = (tsd_t *)tsd; 202 int i; 203 204 debug("in to benchmark - optB = %i : ts_once = %i\n", lm_optB, ts->ts_once); 205 for (i = 0; i < lm_optB; i++) { 206 /* 207 * just to show that ts really contains state 208 */ 209 ts->ts_once++; 210 } 211 res->re_count = i; 212 debug("out of benchmark - optB = %i : ts_once = %i\n", lm_optB, ts->ts_once); 213 214 return (0); 215} 216 217int 218benchmark_finibatch(void *tsd) 219{ 220 tsd_t *ts = (tsd_t *)tsd; 221 /* 222 * more proof of state passing 223 */ 224 ts->ts_once = optt; 225 debug("benchmark_finibatch: ts_once = %i\n",ts->ts_once); 226 return (0); 227} 228 229int 230benchmark_finiworker(void *tsd) 231{ 232 tsd_t *ts = (tsd_t *)tsd; 233 // useless code to show what you can do. 234 ts->ts_once++; 235 ts->ts_once--; 236 debug("benchmark_finiworker: ts_once = %i\n",ts->ts_once); 237 return (0); 238} 239 240char * 241benchmark_result() 242{ 243 static char result = '\0'; 244 debug("benchmark_result\n"); 245 return (&result); 246} 247 248int 249benchmark_finirun() 250{ 251 debug("benchmark_finirun\n"); 252 return (0); 253} 254 255 256int 257benchmark_fini() 258{ 259 debug("benchmark_fini\n"); 260 free(optc); 261 return (0); 262} 263 264