📄 dtutil.c
字号:
if ( (count = read (*fd, buffer, size)) != size) { Fprintf ("Pattern file '%s' read error!\n", file); if ((ssize_t)count == FAILURE) { report_error ("read", TRUE); exit (exit_status); } else { LogMsg (efp, logLevelCrit, 0, "Attempted to read %d bytes, read only %d bytes.", size, count); exit (FATAL_ERROR); } } patbuf_size = size; setup_pattern (buffer, size); /* * Currently don't need pattern file open for anything else. */ (void) close (*fd); *fd = -1;}/************************************************************************ * * * setup_pattern() - Setup pattern variables. * * * * Inputs: buffer = Pointer to pattern buffer. * * size = Size of pattern buffer. * * * * Outputs: Returns on success, exits on open failure. * * * * Return Value: * * Void. * * * ************************************************************************/voidsetup_pattern (u_char *buffer, size_t size){ patbuf_size = size; pattern_buffer = buffer; pattern_bufptr = buffer; pattern_bufend = buffer + size; pattern = (u_int32) 0; switch (size) { case sizeof(u_char): pattern = (u_int32) buffer[0]; break; case sizeof(u_short): pattern = ( ((u_int32)buffer[1] << 8) | (u_int32)buffer[0] ); break; case 0x03: pattern = ( ((u_int32)buffer[2] << 16) | ((u_int32)buffer[1] << 8) | (u_int32) buffer[0] ); break; default: pattern = ( ((u_int32)buffer[3] << 24) | ((u_int32)buffer[2] << 16) | ((u_int32)buffer[1] << 8) | (u_int32)buffer[0]); break; }}/* * Copy pattern bytes to pattern buffer with proper byte ordering. */voidcopy_pattern (u_int32 pattern, u_char *buffer){ buffer[0] = (u_char) pattern; buffer[1] = (u_char) (pattern >> 8); buffer[2] = (u_char) (pattern >> 16); buffer[3] = (u_char) (pattern >> 24);}/* * Function to display ASCII time. */voidprint_time (FILE *fp, clock_t time){ u_int hr, min, sec, frac; frac = time % hz; frac = (frac * 100) / hz; time /= hz; sec = time % 60; time /= 60; min = time % 60; if (hr = time / 60) { fprintf (fp, "%dh", hr); } fprintf (fp, "%02dm", min); fprintf (fp, "%02d.", sec); fprintf (fp, "%02ds\n", frac);}voidformat_time (clock_t time){ clock_t hr, min, sec, frac; frac = (time % hz); frac = (frac * 100) / hz; time /= hz; sec = time % 60; time /= 60; min = time % 60; if (hr = time / 60) { Lprintf ("%dh", hr); } Lprintf ("%02dm", min); Lprintf ("%02d.", sec); Lprintf ("%02ds\n", frac);}#if defined(DEC)/* * Format table() sysinfo time. */voidformat_ltime (long time, int tps){ long hr, min, sec, frac; /*Lprintf ("%.3f - ", (float)((float)time/(float)tps));*/ frac = (time % tps); frac = (frac * 1000) / tps; time /= tps; sec = time % 60; time /= 60; min = time % 60; if (hr = time / 60) { Lprintf ("%dh", hr); } Lprintf ("%02dm", min); Lprintf ("%02d.", sec); Lprintf ("%03ds\n", frac);}#endif /* defined(DEC) *//************************************************************************ * * * seek_file() Seeks to the specified file offset. * * * * Inputs: fd = The file descriptor. * * records = The number of records. * * size = The size of each record. * * whence = The method of setting position: * * * * SEEK_SET (L_SET) = Set to offset bytes. * * SEEK_CUR (L_INCR) = Increment by offset bytes. * * SEEK_END (L_XTND) = Extend by offset bytes. * * * * offset = (record count * size of each record) * * * * Return Value: * * Returns file position on Success, (off_t)-1 on Failure. * * * ************************************************************************/off_tseek_file (int fd, u_long records, off_t size, int whence){ off_t pos; /* * Seek to specifed file position. */ if ((pos = lseek (fd, (off_t)(records * size), whence)) == (off_t)-1) { Fprintf("lseek failed (fd %d, offset " FUF ", whence %d)\n", fd, (off_t)(records * size), whence); report_error ("lseek", TRUE); } return (pos);}/* * Utility functions to handle file positioning. */off_tseek_position (struct dinfo *dip, off_t offset, int whence){ off_t pos;#if defined(DEBUG) if (Debug_flag) { Printf ("attempting lseek (fd=%d, offset=%lu, whence=%d)\n", dip->di_fd, offset, whence); }#endif /* defined(DEBUG) */ /* * Seek to specifed file position. */ if ((pos = lseek (dip->di_fd, offset, whence)) == (off_t) -1) { Fprintf("lseek failed (fd %d, offset " FUF ", whence %d)\n", dip->di_fd, offset, whence); report_error ("lseek", TRUE); terminate (exit_status); }#if defined(DEBUG) if (Debug_flag) { Printf ("pos=%lu = lseek (fd=%d, offset=%lu, whence=%d)\n", pos, dip->di_fd, offset, whence); }#endif /* defined(DEBUG) */ return (pos);}#if !defined(INLINE_FUNCS)off_tget_position (struct dinfo *dip){#if defined(_BSD) return (seek_position (dip, (off_t) 0, L_INCR));#else /* !defined(_BSD) */ return (seek_position (dip, (off_t) 0, SEEK_CUR));#endif /* defined(_BSD) */}#endif /* !defined(INLINE_FUNCS) */u_int32get_lba (struct dinfo *dip){ off_t pos; if ( (pos = get_position(dip)) ) { return ( (u_int32)(pos / lbdata_size) ); } else { return ( (u_int32) 0 ); }}off_tincr_position (struct dinfo *dip, off_t offset){#if defined(_BSD) off_t pos = seek_position (dip, offset, L_INCR);#else /* !defined(_BSD) */ off_t pos = seek_position (dip, offset, SEEK_CUR);#endif /* defined(_BSD) */ if (Debug_flag || rDebugFlag) { u_int32 lba = (pos / (off_t)dip->di_dsize); Printf ("Seeked to block %lu (%#lx) at byte position " FUF "\n", lba, lba, pos); } return (pos);}off_tset_position (struct dinfo *dip, off_t offset){#if defined(_BSD) off_t pos = seek_position (dip, offset, L_SET);#else /* !defined(_BSD) */ off_t pos = seek_position (dip, offset, SEEK_SET);#endif /* defined(_BSD) */ if (Debug_flag || rDebugFlag) { u_int32 lba = (pos / (off_t)dip->di_dsize); Printf ("Seeked to block %lu (%#lx) at byte position " FUF ".\n", lba, lba, pos); } return (pos);}#if !defined(INLINE_FUNCS)off_tmake_position(struct dinfo *dip, u_int32 lba){ return ( (off_t)(lba * lbdata_size));}#endif /* !defined(INLINE_FUNCS) */voidshow_position (struct dinfo *dip, off_t pos){ if (debug_flag || rDebugFlag) { u_int32 lba = make_lba(dip, pos); Printf ("Current file offset is " FUF " (" FXF "), relative lba is %u (%#x)\n", pos, pos, lba, lba); }}u_longget_random(void){ /* * These first 2 functions generate a number in range (0 - 2^31). */#if defined(RAND48) return ( (u_long)lrand48() );#elif defined(RANDOM) return ( (u_long)random() );#else /* Use ANSI rand() below... */ /* * ANSI specifies rand() returns value in range (0 - 32767). * NOTE: BSD rand() returns value in range (0 - 2^31) also. */ return ( (u_long)rand() );#endif /* defined(RAND28) */}/* * Function to calculate variable length request size. */size_tget_variable (struct dinfo *dip){ size_t length; u_long randum; randum = get_random(); length = (size_t)((randum % max_size) + min_size); if (dip->di_dsize) { length = roundup(length, dip->di_dsize); } if (length > max_size) length = max_size; return (length);}/* * Function to set random number generator seed. */voidset_rseed(u_int seed){#if defined(RAND48) /* System V */ srand48 ((long) seed);#elif defined(RANDOM) /* BSD? */ srandom (seed);#else /* ANSI */ srand (seed);#endif}/* * Function to set position for random I/O. */off_tdo_random (struct dinfo *dip, bool doseek, size_t xfer_size){ off_t pos, dsize, ralign; large_t randum; u_long records; dsize = (off_t)(dip->di_dsize); ralign = (off_t)((random_align) ? random_align : dsize); if (dip->di_mode == READ_MODE) { records = dip->di_records_read; } else { records = dip->di_records_written; } /* * Ensure the random alignment size is modulo the device size. */ if ( (ralign % dsize) != 0) ralign = roundup(ralign,dsize); randum = (large_t)get_random(); /* * Set position so that the I/O is in the range from file_position to * data_limit and is block size aligned. * * Since randum only ranges to 2^31, treat it as a block number to * allow random access to more blocks in a large device or file. * * Note: This scaling/rounding/range checking kills performance! */ if (records & 0x01) { randum *= dsize; /* Scale upwards every other record. */ } pos = (off_t)(randum % rdata_limit); /* * Ensure the upper data limit isn't exceeded. */ while ((pos + xfer_size) >= rdata_limit) { pos = ( (pos + xfer_size) % rdata_limit); } /* * Round up and align as necessary. */ pos = roundup(pos, ralign); /* * Ensure the position is within random range. */ if (pos < file_position) { if ((pos + file_position + xfer_size) >= rdata_limit) { pos = (file_position + ((pos + xfer_size) / dsize)); } else { pos += file_position; } /* Round up and adjust back down as necessary. (yea, ugly :-) */ while ( ((pos = roundup(pos, ralign)) + xfer_size) >= rdata_limit) { pos -= (ralign + dsize); } if (pos < file_position) pos = file_position; /* too low! */ } if (doseek) { return (set_position (dip, pos)); } else { /* * Note: For AIO, we just calculate the random position. */ if (Debug_flag || rDebugFlag) { u_int32 lba = (pos / dsize); Printf ("Random position set to " FUF " block %lu (%#lx).\n", pos, lba, lba); } return (pos); }}/************************************************************************ * * * skip_records() Skip past specified number of records. * * * * Inputs: dip = The device info pointer. * * records = The number of records. * * buffer = The buffer to read into. * * size = The size of each record. * * * * Return Value: * * Returns SUCCESS/FAILURE/WARNING = Ok/Read Failed/Partial Read. * * * ************************************************************************/intskip_records ( struct dinfo *dip, u_long records, u_char *buffer, off_t size){ u_long i; size_t count; int status = SUCCESS; /* * Skip over the specified record(s). */ for (i = 0; i < records; i++) { count = read (dip->di_fd, buffer, size); if ( (status = check_read (dip, count, size)) == FAILURE) { break; } } return (status);}/************************************************************************ * * * myalloc() Allocate aligned buffer at specified offset. * * * * Description: This function first gets a page aligned buffer using * * malloc(), then it returns a pointer offset bytes into * * the page. This was done to test page alignment code * * in device drivers. * * * * Inputs: size = The size of the buffer to allocate. * * offset = The offset into the page. * * * * Outputs: Returns the allocated buffer or 0 for failure. * * * ************************************************************************/void *myalloc (size_t size, int offset){ u_char *bp;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -