1 /* 2 * CDDL HEADER START 3 * 4 * The contents of this file are subject to the terms of the 5 * Common Development and Distribution License (the "License"). 6 * You may not use this file except in compliance with the License. 7 * 8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 9 * or http://www.opensolaris.org/os/licensing. 10 * See the License for the specific language governing permissions 11 * and limitations under the License. 12 * 13 * When distributing Covered Code, include this CDDL HEADER in each 14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 15 * If applicable, add the following below this CDDL HEADER, with the 16 * fields enclosed by brackets "[]" replaced with your own identifying 17 * information: Portions Copyright [yyyy] [name of copyright owner] 18 * 19 * CDDL HEADER END 20 */ 21 /* 22 * Copyright 2008 Sun Microsystems, Inc. All rights reserved. 23 * Use is subject to license terms. 24 */ 25 26 #pragma ident "%Z%%M% %I% %E% SMI" 27 28 #include <stdio.h> 29 #include <fcntl.h> 30 #include <limits.h> 31 #include <time.h> 32 #include <libgen.h> 33 #include <unistd.h> 34 #include <strings.h> 35 #include "filebench.h" 36 #include "ipc.h" 37 #include "eventgen.h" 38 #include "utils.h" 39 40 /* 41 * Routines to access high resolution system time, initialize and 42 * shutdown filebench, log filebench run progress and errors, and 43 * access system information strings. 44 */ 45 46 47 #if !defined(sun) && defined(USE_RDTSC) 48 /* 49 * Lets us use the rdtsc instruction to get highres time. 50 * Thanks to libmicro 51 */ 52 uint64_t cpu_hz = 0; 53 54 /* 55 * Uses the rdtsc instruction to get high resolution (cpu 56 * clock ticks) time. Only used for non Sun compiles. 57 */ 58 __inline__ long long 59 rdtsc(void) 60 { 61 unsigned long long x; 62 __asm__ volatile(".byte 0x0f, 0x31" : "=A" (x)); 63 return (x); 64 } 65 66 /* 67 * Get high resolution time in nanoseconds. This is the version 68 * used when not compiled for Sun systems. It uses rdtsc call to 69 * get clock ticks and converts to nanoseconds 70 */ 71 uint64_t 72 gethrtime(void) 73 { 74 uint64_t hrt; 75 76 /* convert to nanosecs and return */ 77 hrt = 1000000000UL * rdtsc() / cpu_hz; 78 return (hrt); 79 } 80 81 /* 82 * Gets CPU clock frequency in MHz from cpuinfo file. 83 * Converts to cpu_hz and stores in cpu_hz global uint64_t. 84 * Only used for non Sun compiles. 85 */ 86 static uint64_t 87 parse_cpu_hz(void) 88 { 89 /* 90 * Parse the following from /proc/cpuinfo. 91 * cpu MHz : 2191.563 92 */ 93 FILE *cpuinfo; 94 double hertz = -1; 95 uint64_t hz; 96 97 if ((cpuinfo = fopen("/proc/cpuinfo", "r")) == NULL) { 98 filebench_log(LOG_ERROR, "open /proc/cpuinfo failed: %s", 99 strerror(errno)); 100 filebench_shutdown(1); 101 } 102 while (!feof(cpuinfo)) { 103 char buffer[80]; 104 105 fgets(buffer, 80, cpuinfo); 106 if (strlen(buffer) == 0) continue; 107 if (strncasecmp(buffer, "cpu MHz", 7) == 0) { 108 char *token = strtok(buffer, ":"); 109 110 if (token != NULL) { 111 token = strtok((char *)NULL, ":"); 112 hertz = strtod(token, NULL); 113 } 114 break; 115 } 116 } 117 hz = hertz * 1000000; 118 119 return (hz); 120 } 121 122 #elif !defined(sun) 123 124 /* 125 * Get high resolution time in nanoseconds. This is the version 126 * used if compiled for Sun systems. It calls gettimeofday 127 * to get current time and converts it to nanoseconds. 128 */ 129 uint64_t 130 gethrtime(void) 131 { 132 struct timeval tv; 133 uint64_t hrt; 134 135 gettimeofday(&tv, NULL); 136 137 hrt = (uint64_t)tv.tv_sec * 1000000000UL + 138 (uint64_t)tv.tv_usec * 1000UL; 139 return (hrt); 140 } 141 #endif 142 143 /* 144 * Main filebench initialization. Opens the random number 145 * "device" file or shuts down the run if one is not found. 146 * Sets the cpu clock frequency variable or shuts down the 147 * run if one is not found. 148 */ 149 void 150 filebench_init(void) 151 { 152 fb_random_init(); 153 154 #if defined(USE_RDTSC) && (LINUX_PORT) 155 cpu_hz = parse_cpu_hz(); 156 if (cpu_hz <= 0) { 157 filebench_log(LOG_ERROR, "Error getting CPU Mhz: %s", 158 strerror(errno)); 159 filebench_shutdown(1); 160 } 161 #endif /* USE_RDTSC */ 162 163 } 164 165 extern int lex_lineno; 166 167 /* 168 * Writes a message consisting of information formated by 169 * "fmt" to the log file, dump file or stdout. The supplied 170 * "level" argument determines which file to write to and 171 * what other actions to take. The level LOG_LOG writes to 172 * the "log" file, and will open the file on the first 173 * invocation. The level LOG_DUMP writes to the "dump" file, 174 * and will open it on the first invocation. Other levels 175 * print to the stdout device, with the amount of information 176 * dependent on the error level and the current error level 177 * setting in filebench_shm->debug_level. 178 */ 179 void filebench_log 180 __V((int level, const char *fmt, ...)) 181 { 182 va_list args; 183 hrtime_t now; 184 char line[131072]; 185 char buf[131072]; 186 187 if (level == LOG_FATAL) 188 goto fatal; 189 190 /* open logfile if not already open and writing to it */ 191 if ((level == LOG_LOG) && 192 (filebench_shm->log_fd < 0)) { 193 char path[MAXPATHLEN]; 194 char *s; 195 196 (void) strcpy(path, filebench_shm->fscriptname); 197 if ((s = strstr(path, ".f"))) 198 *s = 0; 199 else 200 (void) strcpy(path, "filebench"); 201 202 (void) strcat(path, ".csv"); 203 204 filebench_shm->log_fd = 205 open(path, O_RDWR | O_CREAT | O_TRUNC, 0666); 206 } 207 208 /* 209 * if logfile still not open, switch to LOG_ERROR level so 210 * it gets reported to stdout 211 */ 212 if ((level == LOG_LOG) && 213 (filebench_shm->log_fd < 0)) { 214 (void) snprintf(line, sizeof (line), "Open logfile failed: %s", 215 strerror(errno)); 216 level = LOG_ERROR; 217 } 218 219 /* open dumpfile if not already open and writing to it */ 220 if ((level == LOG_DUMP) && 221 (*filebench_shm->dump_filename == 0)) 222 return; 223 224 if ((level == LOG_DUMP) && 225 (filebench_shm->dump_fd < 0)) { 226 227 filebench_shm->dump_fd = 228 open(filebench_shm->dump_filename, 229 O_RDWR | O_CREAT | O_TRUNC, 0666); 230 } 231 232 if ((level == LOG_DUMP) && 233 (filebench_shm->dump_fd < 0)) { 234 (void) snprintf(line, sizeof (line), "Open logfile failed: %s", 235 strerror(errno)); 236 level = LOG_ERROR; 237 } 238 239 /* Quit if this is a LOG_ERROR messages and they are disabled */ 240 if ((filebench_shm->shm_1st_err) && (level == LOG_ERROR)) 241 return; 242 243 if (level == LOG_ERROR1) { 244 if (filebench_shm->shm_1st_err) 245 return; 246 247 /* A LOG_ERROR1 temporarily disables LOG_ERROR messages */ 248 filebench_shm->shm_1st_err = 1; 249 level = LOG_ERROR; 250 } 251 252 /* Only log greater than debug setting */ 253 if ((level != LOG_DUMP) && (level != LOG_LOG) && 254 (level > filebench_shm->debug_level)) 255 return; 256 257 now = gethrtime(); 258 259 fatal: 260 261 #ifdef __STDC__ 262 va_start(args, fmt); 263 #else 264 char *fmt; 265 va_start(args); 266 fmt = va_arg(args, char *); 267 #endif 268 269 (void) vsprintf(line, fmt, args); 270 271 va_end(args); 272 273 if (level == LOG_FATAL) { 274 (void) fprintf(stdout, "%s\n", line); 275 return; 276 } 277 278 /* Serialize messages to log */ 279 (void) ipc_mutex_lock(&filebench_shm->msg_lock); 280 281 if (level == LOG_LOG) { 282 if (filebench_shm->log_fd > 0) { 283 (void) snprintf(buf, sizeof (buf), "%s\n", line); 284 (void) write(filebench_shm->log_fd, buf, strlen(buf)); 285 (void) fsync(filebench_shm->log_fd); 286 } 287 288 } else if (level == LOG_DUMP) { 289 if (filebench_shm->dump_fd != -1) { 290 (void) snprintf(buf, sizeof (buf), "%s\n", line); 291 (void) write(filebench_shm->dump_fd, buf, strlen(buf)); 292 (void) fsync(filebench_shm->dump_fd); 293 } 294 295 } else if (filebench_shm->debug_level > LOG_INFO) { 296 (void) fprintf(stdout, "%5d: %4.3f: %s", 297 (int)my_pid, (now - filebench_shm->epoch) / FSECS, 298 line); 299 } else { 300 (void) fprintf(stdout, "%4.3f: %s", 301 (now - filebench_shm->epoch) / FSECS, 302 line); 303 } 304 305 if (level == LOG_ERROR) { 306 if (my_procflow == NULL) 307 (void) fprintf(stdout, " on line %d", lex_lineno); 308 } 309 310 if ((level != LOG_LOG) && (level != LOG_DUMP)) { 311 (void) fprintf(stdout, "\n"); 312 (void) fflush(stdout); 313 } 314 315 (void) ipc_mutex_unlock(&filebench_shm->msg_lock); 316 } 317 318 /* 319 * Stops the run and exits filebench. If filebench is 320 * currently running a workload, calls procflow_shutdown() 321 * to stop the run. Also closes and deletes shared memory. 322 */ 323 void 324 filebench_shutdown(int error) { 325 filebench_log(LOG_DEBUG_IMPL, "Shutdown"); 326 (void) unlink("/tmp/filebench_shm"); 327 if (filebench_shm->shm_running) 328 procflow_shutdown(); 329 filebench_shm->f_abort = 1; 330 ipc_ismdelete(); 331 exit(error); 332 } 333 334 /* 335 * Put the hostname in ${hostname}. The system supplied 336 * host name string is copied into an allocated string and 337 * the pointer to the string is placed in the supplied 338 * variable "var". If var->var_val.string already points to 339 * a string, the string is freed. The routine always 340 * returns zero (0). 341 */ 342 var_t * 343 host_var(var_t *var) 344 { 345 char hoststr[128]; 346 char *strptr; 347 348 (void) gethostname(hoststr, 128); 349 if (VAR_HAS_STRING(var) && var->var_val.string) 350 free(var->var_val.string); 351 352 if ((strptr = fb_stralloc(hoststr)) == NULL) { 353 filebench_log(LOG_ERROR, 354 "unable to allocate string for host name"); 355 return (NULL); 356 } 357 358 VAR_SET_STR(var, strptr); 359 return (0); 360 } 361 362 /* 363 * Put the date string in ${date}. The system supplied date is 364 * copied into an allocated string and the pointer to the string 365 * is placed in the supplied var_t's var_val.string. If 366 * var->var_val.string already points to a string, the string 367 * is freed. The routine returns a pointer to the supplied var_t, 368 * unless it is unable to allocate string for the date, in which 369 * case it returns NULL. 370 */ 371 var_t * 372 date_var(var_t *var) 373 { 374 char datestr[128]; 375 char *strptr; 376 #ifdef HAVE_CFTIME 377 time_t t = time(NULL); 378 #else 379 struct tm t; 380 #endif 381 382 #ifdef HAVE_CFTIME 383 cftime(datestr, "%y%m%d%H" "%M", &t); 384 #else 385 (void) strftime(datestr, sizeof (datestr), "%y%m%d%H %M", &t); 386 #endif 387 388 if (VAR_HAS_STRING(var) && var->var_val.string) 389 free(var->var_val.string); 390 391 if ((strptr = fb_stralloc(datestr)) == NULL) { 392 filebench_log(LOG_ERROR, 393 "unable to allocate string for date"); 394 return (NULL); 395 } 396 397 VAR_SET_STR(var, strptr); 398 399 return (var); 400 } 401 402 extern char *fscriptname; 403 404 /* 405 * Put the script name in ${script}. The path name of the script 406 * used with this filebench run trimmed of the trailing ".f" and 407 * all leading subdirectories. The remaining script name is 408 * copied into the var_val.string field of the supplied variable 409 * "var". The routine returns a pointer to the supplied var_t, 410 * unless it is unable to allocate string space, in which case it 411 * returns NULL. 412 */ 413 var_t * 414 script_var(var_t *var) 415 { 416 char *scriptstr; 417 char *f = fb_stralloc(fscriptname); 418 char *strptr; 419 420 /* Trim the .f suffix */ 421 for (scriptstr = f + strlen(f) - 1; scriptstr != f; scriptstr--) { 422 if (*scriptstr == '.') { 423 *scriptstr = 0; 424 break; 425 } 426 } 427 428 if ((strptr = fb_stralloc(basename(f))) == NULL) { 429 filebench_log(LOG_ERROR, 430 "unable to allocate string for script name"); 431 free(f); 432 return (NULL); 433 } 434 435 VAR_SET_STR(var, strptr); 436 free(f); 437 438 return (var); 439 } 440