sluice-0.02.02/0000775000175000017500000000000012602321304011574 5ustar kingkingsluice-0.02.02/Makefile0000664000175000017500000000255712602321276013255 0ustar kingking# # Copyright (C) 2014-2015 Canonical, Ltd. # # This program is free software; you can redistribute it and/or # modify it under the terms of the GNU General Public License # as published by the Free Software Foundation; either version 2 # of the License, or (at your option) any later version. # # This program is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # # You should have received a copy of the GNU General Public License # along with this program; if not, write to the Free Software # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. # VERSION=0.02.02 CFLAGS += -Wall -Wextra -DVERSION='"$(VERSION)"' -O2 BINDIR=/usr/bin MANDIR=/usr/share/man/man1 sluice: sluice.o $(CC) $(CFLAGS) $< -o $@ $(LDFLAGS) sluice.1.gz: sluice.1 gzip -c $< > $@ dist: rm -rf sluice-$(VERSION) mkdir sluice-$(VERSION) cp -rp Makefile sluice.c sluice.1 COPYING sluice-$(VERSION) tar -zcf sluice-$(VERSION).tar.gz sluice-$(VERSION) rm -rf sluice-$(VERSION) clean: rm -f sluice sluice.o sluice.1.gz rm -f sluice-$(VERSION).tar.gz install: sluice sluice.1.gz mkdir -p ${DESTDIR}${BINDIR} cp sluice ${DESTDIR}${BINDIR} mkdir -p ${DESTDIR}${MANDIR} cp sluice.1.gz ${DESTDIR}${MANDIR} sluice-0.02.02/sluice.c0000664000175000017500000011204612602321276013240 0ustar kingking/* * Copyright (C) 2014-2015 Canonical * * This program is free software; you can redistribute it and/or * modify it under the terms of the GNU General Public License * as published by the Free Software Foundation; either version 2 * of the License, or (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. * * Author Colin Ian King, colin.king@canonical.com */ #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #define KB (1024ULL) #define MB (KB * KB) #define GB (KB * MB) #define TB (KB * GB) #define PB (KB * TB) #define UNDERRUN_MAX (100) /* Max underruns before warning, see -w */ #define UNDERRUN_ADJUST_MAX (1) /* Underruns before adjusting rate */ #define OVERRUN_ADJUST_MAX (1) /* Overruns before adjusting rate */ #define DELAY_SHIFT_MIN (0) /* Min shift, see -s */ #define DELAY_SHIFT_MAX (16) /* Max shift, see -s */ #define IO_SIZE_MIN (1) /* Min io buffer size, see -i */ #define IO_SIZE_MAX (1ULL * GB) /* Max io buffer size, see -i */ #define DELAY_MIN (0.01) /* Min delay time, see -c */ #define DELAY_MAX (10.00) /* Max delay time, see -c */ #define DATA_RATE_MIN (0.1) /* Min data rate, see -r */ #define FREQ_MIN (0.01) /* Min frequency, see -f */ #define DRIFT_MAX (11) /* Number of drift stats, see -S */ #define DRIFT_PERCENT_START (0.0625) /* Drift stats first point */ #define DEFAULT_FREQ (0.250) /* Default verbose feedback freq, see -f */ #define DEBUG_RATE (0) /* Set to non-zero to get datarate debug */ #define DEBUG_SETUP (0) /* Set to non-zero to dump setup state */ #define OPT_VERBOSE (0x00000001) /* -v */ #define OPT_GOT_RATE (0x00000002) /* -r */ #define OPT_GOT_IOSIZE (0x00000004) /* -i */ #define OPT_GOT_CONST_DELAY (0x00000008) /* -c */ #define OPT_WARNING (0x00000010) /* -w */ #define OPT_UNDERRUN (0x00000020) /* -u */ #define OPT_DISCARD_STDOUT (0x00000040) /* -d */ #define OPT_OVERRUN (0x00000080) /* -o */ #define OPT_ZERO (0x00000100) /* -z */ #define OPT_URANDOM (0x00000200) /* -R */ #define OPT_APPEND (0x00000400) /* -a */ #define OPT_STATS (0x00000800) /* -S */ #define OPT_NO_RATE_CONTROL (0x00001000) /* -n */ #define OPT_TIMED_RUN (0x00002000) /* -T */ #define OPT_INPUT_FILE (0x00004000) /* -I */ #define OPT_VERSION (0x00008000) /* -V */ #define OPT_PROGRESS (0x00010000) /* -p */ #define OPT_MAX_TRANS_SIZE (0x00020000) /* -m */ #define OPT_SKIP_READ_ERRORS (0x00040000) /* -e */ #define OPT_GOT_SHIFT (0x00080000) /* -s */ #define EXIT_BAD_OPTION (1) #define EXIT_FILE_ERROR (2) #define EXIT_DELAY_ERROR (3) #define EXIT_TIME_ERROR (4) #define EXIT_SIGNAL_ERROR (5) #define EXIT_READ_ERROR (6) #define EXIT_WRITE_ERROR (7) #define EXIT_ALLOC_ERROR (8) #define BUF_SIZE(sz) ((((size_t)sz) < 1) ? 1 : ((size_t)sz)) /* R = read, W = write, D = delay */ #define DELAY_R_W_D (0x00000000) /* full delay */ #define DELAY_D_R_W (0x00000001) /* full delay */ #define DELAY_R_D_W (0x00000002) /* full delay */ #define DELAY_D_R_D_W (0x00000003) /* 2 * 1/2 delay */ #define DELAY_R_D_W_D (0x00000004) /* 2 * 1/2 delay */ #define DELAY_D_R_D_W_D (0x00000005) /* 3 * 1/3 delay */ #define DELAY_MODE_MIN 0 #define DELAY_MODE_MAX DELAY_D_R_D_W_D #define DELAY_D (0x01) /* delay */ #define DELAY_S (0x00) /* skip */ #define DELAY_SET_ACTION(a1, a2, a3) ((a1 << 0) | (a2 << 1) | (a3 << 2)) #define DELAY_GET_ACTION(n, action) ((1 << n) & action) typedef struct { double divisor; /* delay divisor */ uint8_t mode; /* User specified mode */ uint8_t action; /* action bit map */ } delay_info_t; /* * action bit# * 0 sleep on/off * read * 1 sleep on/off * write * 2 sleep on/off */ static delay_info_t delay_info[] = { { 1.0, DELAY_R_W_D, DELAY_SET_ACTION(DELAY_S, DELAY_S, DELAY_D) }, { 1.0, DELAY_D_R_W, DELAY_SET_ACTION(DELAY_D, DELAY_S, DELAY_S) }, { 1.0, DELAY_R_D_W, DELAY_SET_ACTION(DELAY_S, DELAY_D, DELAY_S) }, { 2.0, DELAY_D_R_D_W, DELAY_SET_ACTION(DELAY_D, DELAY_D, DELAY_S) }, { 2.0, DELAY_R_D_W_D, DELAY_SET_ACTION(DELAY_S, DELAY_D, DELAY_D) }, { 3.0, DELAY_D_R_D_W_D, DELAY_SET_ACTION(DELAY_D, DELAY_D, DELAY_D) }, }; /* scaling factor */ typedef struct { const char ch; /* Scaling suffix */ const uint64_t scale; /* Amount to scale by */ } scale_t; /* various statistics */ typedef struct { uint64_t reads; /* Total read calls */ uint64_t writes; /* Total write calls */ uint64_t total_bytes; /* Total bytes copied */ uint64_t underruns; /* Count of underruns */ uint64_t overruns; /* Count of overruns */ uint64_t delays; /* Count of delays */ uint64_t reallocs; /* Count of buffer reallocations */ uint64_t perfect; /* Count of no under/overruns */ uint64_t io_size_min; /* Minimum buffer size */ uint64_t io_size_max; /* Maximum buffer size */ uint64_t drift[DRIFT_MAX];/* Drift from desired rate */ uint64_t drift_total; /* Number of drift samples */ double time_begin; /* Time began */ double time_end; /* Time ended */ double target_rate; /* Target transfer rate */ double buf_size_total; /* For average buffer size */ double rate_min; /* Minimum rate */ double rate_max; /* Maximum rate */ bool rate_set; /* Min/max set or not? */ } stats_t; static unsigned int opt_flags; static const char *app_name = "sluice"; static const char *dev_urandom = "/dev/urandom"; static volatile bool sluice_finish = false; static const scale_t byte_scales[] = { { 'b', 1ULL }, { 'k', 1ULL << 10 }, /* Kilobytes */ { 'm', 1ULL << 20 }, /* Megabytes */ { 'g', 1ULL << 30 }, /* Gigabytes */ { 't', 1ULL << 40 }, /* Terabytes */ { 'p', 1ULL << 60 }, /* Petabytes */ { 0, 0 }, }; static const scale_t time_scales[] = { { 's', 1 }, { 'm', 60 }, { 'h', 3600 }, { 'd', 24 * 3600 }, { 'y', 365 * 24 * 3600 }, }; static const scale_t second_scales[] = { { 's', 1 }, { 'm', 60 }, { 'h', 3600 }, { 'd', 24 * 3600 }, { 'w', 7 * 24 * 3600 }, { 'y', 365 * 24 * 3600 }, { ' ', INT64_MAX }, }; /* * count_bits() * count bits set, from C Programming Language 2nd Ed */ static inline unsigned int count_bits(const unsigned int val) { register unsigned int c, n = val; for (c = 0; n; c++) n &= n - 1; return c; } /* * handle_sigint() * catch SIGINT, jump to tidy termination */ static void handle_sigint(int dummy) { (void)dummy; sluice_finish = true; } /* * handle_siginfo() * catch SIGINFO/SIGUSR1, toggle verbose mode */ static void handle_siginfo(int dummy) { (void)dummy; opt_flags ^= OPT_VERBOSE; } /* * handle_sigusr2() * catch SIGUSR2, toggle underflow/overflow */ static void handle_sigusr2(int dummy) { (void)dummy; opt_flags ^= (OPT_OVERRUN | OPT_UNDERRUN); } /* * stats_init() * Initialize statistics */ static inline void stats_init(stats_t *const stats) { stats->reads = 0; stats->writes = 0; stats->total_bytes = 0; stats->underruns = 0; stats->overruns = 0; stats->delays = 0; stats->reallocs = 0; stats->perfect = 0; stats->io_size_min = 0; stats->io_size_max = 0; memset(&stats->drift, 0, sizeof(stats->drift)); stats->drift_total = 0; stats->time_begin = 0.0; stats->time_end = 0.0; stats->target_rate = 0.0; stats->buf_size_total = 0.0; stats->rate_min = 0.0; stats->rate_max = 0.0; stats->rate_set = false; } /* * secs_to_str() * report seconds in different units. */ static const char *secs_to_str(const double secs) { static char buf[64]; int i; for (i = 0; i < 5; i++) { if (secs <= second_scales[i + 1].scale) break; } snprintf(buf, sizeof(buf), "%.2f %c", secs / second_scales[i].scale, second_scales[i].ch); return buf; } /* * size_to_str() * report size in different units */ static void size_to_str( const double val, const char *const fmt, char *const buf, const size_t buflen) { double v = val; int i; static char *sizes[] = { "B ", /* Bytes */ "KB", /* Kilobytes */ "MB", /* Megabytes */ "GB", /* Gigabytes */ "TB", /* Terabytes */ "PB", /* Petabytes */ "EB", /* Exabytes */ "ZB", /* Zettabytes */ "YB", /* Yottabytes */ }; for (i = 0; i < 8; i++, v /= 1024.0) { if (v <= 512.0) break; } snprintf(buf, buflen, fmt, v, sizes[i]); } /* * double_to_str() * convert double size in bytes to string */ static const char *double_to_str(const double val) { static char buf[64]; size_to_str(val, "%.2f %s", buf, sizeof(buf)); return buf; } /* * stats_info() * display run time statistics */ static void stats_info(const stats_t *stats) { const double secs = stats->time_end - stats->time_begin; double avg_wr_sz; struct tms t; if (secs <= 0.0) { fprintf(stderr, "Cannot compute statistics\n"); return; } avg_wr_sz = stats->writes ? stats->buf_size_total / stats->writes : 0.0; fprintf(stderr, "Data: %s\n", double_to_str((double)stats->total_bytes)); fprintf(stderr, "Reads: %" PRIu64 "\n", stats->reads); fprintf(stderr, "Writes: %" PRIu64 "\n", stats->writes); fprintf(stderr, "Avg. Write Size: %s\n", double_to_str(avg_wr_sz)); fprintf(stderr, "Duration: %s\n", secs_to_str(secs)); fprintf(stderr, "Delays: %" PRIu64 "\n", stats->delays); fprintf(stderr, "Buffer reallocs: %" PRIu64 "\n", stats->reallocs); fprintf(stderr, "\n"); if (!(opt_flags & OPT_NO_RATE_CONTROL)) { fprintf(stderr, "Target rate: %s/s\n", double_to_str(stats->target_rate)); } fprintf(stderr, "Average rate: %s/s\n", double_to_str((double)stats->total_bytes / secs)); fprintf(stderr, "Minimum rate: %s/s\n", double_to_str(stats->rate_min)); fprintf(stderr, "Maximum rate: %s/s\n", double_to_str(stats->rate_max)); fprintf(stderr, "Minimum buffer: %s\n", double_to_str((double)stats->io_size_min)); fprintf(stderr, "Maximum buffer: %s\n", double_to_str((double)stats->io_size_max)); if (times(&t) != (clock_t)-1) { /* CPU utilitation stats, if available */ long int ticks_per_sec; if ((ticks_per_sec = sysconf(_SC_CLK_TCK)) > 0) { fprintf(stderr, "User time: %s\n", secs_to_str((double)t.tms_utime / (double)ticks_per_sec)); fprintf(stderr, "System time: %s\n", secs_to_str((double)t.tms_stime / (double)ticks_per_sec)); fprintf(stderr, "Total delay time: %s\n", secs_to_str(secs - (double)(t.tms_utime + t.tms_stime) / (double)ticks_per_sec)); } } if (!(opt_flags & OPT_NO_RATE_CONTROL)) { /* The following only make sense if we have rate stats */ int i; uint64_t drift_sum = 0; double last_percent = 0.0, percent = DRIFT_PERCENT_START; double total = stats->underruns + stats->overruns + stats->perfect; fprintf(stderr, "Overruns: %6.2f%%\n", total ? 100.0 * (double)stats->underruns / total : 0.0); fprintf(stderr, "Underruns: %6.2f%%\n", total ? 100.0 * (double)stats->overruns / total : 0.0); fprintf(stderr, "\nDrift from target rate: (%%)\n"); for (i = 0; i < DRIFT_MAX; i++, percent *= 2.0) { fprintf(stderr, " %6.3f%% - %6.3f%%: %6.2f%%\n", last_percent, percent - 0.0001, stats->drift_total ? 100.0 * (double)stats->drift[i] / (double)stats->drift_total : 0.0); last_percent = percent; drift_sum += stats->drift[i]; } fprintf(stderr, " >%6.3f%% : %6.2f%%\n", (double)last_percent, stats->drift_total ? 100.0 - ((100.0 * (double)drift_sum) / (double)stats->drift_total) : 0.0); } } /* * timeval_to_double() * convert timeval to seconds as a double */ static double timeval_to_double(void) { struct timeval tv; redo: errno = 0; /* clear to be safe */ if (gettimeofday(&tv, NULL) < 0) { if (errno == EINTR) /* should not occur */ goto redo; fprintf(stderr, "gettimeofday error: errno=%d (%s).\n", errno, strerror(errno)); return -1.0; } return (double)tv.tv_sec + ((double)tv.tv_usec / 1000000.0); } /* * get_uint64() * get a uint64 value */ static uint64_t get_uint64(const char *const str, size_t *const len) { uint64_t val; *len = strlen(str); errno = 0; val = (uint64_t)strtoull(str, NULL, 10); if (errno) { fprintf(stderr, "Invalid value %s.\n", str); exit(EXIT_BAD_OPTION); } if (*len == 0) { fprintf(stderr, "Value %s is an invalid size.\n", str); exit(EXIT_BAD_OPTION); } return val; } /* * get_double() * get a double value */ static double get_double(const char *const str, char **endptr, size_t *const len) { double val; *len = strlen(str); errno = 0; val = strtod(str, endptr); if (errno) { fprintf(stderr, "Invalid value %s.\n", str); exit(EXIT_BAD_OPTION); } if (*len == 0) { fprintf(stderr, "Value %s is an invalid size.\n", str); exit(EXIT_BAD_OPTION); } return val; } /* * get_double_scale() * get a value and scale it by the given scale factor */ static double get_double_scale( const char *const str, const scale_t scales[], const char *const msg) { double val; size_t len = strlen(str); int i; char ch, *endptr; endptr = NULL; val = get_double(str, &endptr, &len); if (!endptr) return val; ch = *endptr; if (!ch) return val; if (*(endptr + 1)) { fprintf(stderr, "Expecting 1 character size specifier, got '%s'.\n", endptr); exit(EXIT_BAD_OPTION); } if (val < 0.0) { fprintf(stderr, "Value %s cannot be negative\n", str); exit(EXIT_BAD_OPTION); } if (isdigit(ch) || ch == '.') return val; ch = tolower(ch); for (i = 0; scales[i].ch; i++) { if (ch == scales[i].ch) return val * scales[i].scale; } fprintf(stderr, "Illegal %s specifier '%c'\n", msg, *endptr); exit(EXIT_BAD_OPTION); } /* * get_uint64_scale() * get a value and scale it by the given scale factor */ static inline uint64_t get_uint64_scale( const char *const str, const scale_t scales[], const char *const msg) { return (uint64_t)get_double_scale(str, scales, msg); } /* * get_uint64_byte() * size in bytes, K bytes, M bytes, G bytes, T bytes or P bytes */ static inline uint64_t get_uint64_byte(const char *const str) { return get_uint64_scale(str, byte_scales, "length"); } /* * get_double_byte() * size in bytes, K bytes, M bytes, G bytes, T bytes or P bytes */ static inline double get_double_byte(const char *const str) { return get_double_scale(str, byte_scales, "length"); } /* * get_uint64_time() * time in seconds, minutes, hours, days or years */ static inline uint64_t get_uint64_time(const char *const str) { return get_uint64_scale(str, time_scales, "time"); } /* * show_usage() * show options */ static void show_usage(void) { printf("%s, version %s\n\n", app_name, VERSION); printf("Usage: %s [options]\n", app_name); printf(" -a append to file (-t, -O options only).\n"); printf(" -c delay specify constant delay time (seconds).\n"); printf(" -d discard output (no output).\n"); printf(" -D delay mode.\n"); printf(" -e skip read errors.\n"); printf(" -f freq frequency of -v statistics.\n"); printf(" -h print this help.\n"); printf(" -i size set io read/write size in bytes.\n"); printf(" -m size set maximum amount to process.\n"); printf(" -n no rate controls, just copy data untouched.\n"); printf(" -o shrink read/write buffer to avoid overrun.\n"); printf(" -O file short cut for -dt file; output to a file.\n"); printf(" -p enable verbose mode with progress stats.\n"); printf(" -P pidfile save process ID into file pidfile.\n"); printf(" -r rate set rate (in bytes per second).\n"); printf(" -R ignore stdin, read from %s.\n", dev_urandom); printf(" -s shift controls delay or buffer size adjustment.\n"); printf(" -S display statistics at end of stream to stderr.\n"); printf(" -t file tee output to file.\n"); printf(" -T time stop after a specified amount of time.\n"); printf(" -u expand read/write buffer to avoid underrun.\n"); printf(" -v set verbose mode (to stderr).\n"); printf(" -V print version information.\n"); printf(" -w warn on data rate underrun.\n"); printf(" -z ignore stdin, generate zeros.\n"); } #define DELAY(delay, stats) \ if (delay > 0) { \ stats.delays++; \ if (usleep((useconds_t)delay) < 0) { \ if (errno == EINTR) { \ if (sluice_finish) \ goto finish; \ /* \ * usleep got interrupted, let \ * subsequent I/O cater with the \ * delay deltas rather than \ * trying to figure out how much \ * time was lost on early exit \ * from usleep \ */ \ } else { \ fprintf(stderr, "usleep error: " \ "errno=%d (%s).\n", \ errno, strerror(errno)); \ ret = EXIT_DELAY_ERROR; \ goto tidy; \ } \ } \ } #define DO_DELAY(delay, di, n, stats) \ if (DELAY_GET_ACTION(n, di->action)) \ DELAY(delay / di->divisor, stats); static delay_info_t *get_delay_info(uint64_t delay_mode) { int i; if (delay_mode > DELAY_MODE_MAX) { fprintf(stderr, "Delay mode -D %" PRIu64 " is too large, range 0..%u.\n", delay_mode, DELAY_MODE_MAX); return NULL; } for (i = 0; i <= DELAY_MODE_MAX; i++) { if (delay_info[i].mode == delay_mode) return &delay_info[i]; } fprintf(stderr, "Cannot find delay mode %" PRIu64 ".\n", delay_mode); return NULL; } int main(int argc, char **argv) { char run = ' '; /* Overrun/underrun flag */ char *buffer = NULL; /* Temp I/O buffer */ char *out_filename = NULL; /* -t or -O option filename */ char *in_filename = NULL; /* -I option filename */ char *pid_filename = NULL; /* -P option filename */ double delay; double io_size = 0.9; /* -i IO buffer size */ double data_rate = 0.0; /* -r data rate */ double secs_start, secs_last, freq = DEFAULT_FREQ; double const_delay = -1.0; /* -c delay time between I/O */ uint64_t last_delay = 0; /* Delays in 1/1000000 of a second */ uint64_t total_bytes = 0; /* cumulative number of bytes read */ uint64_t max_trans = 0; /* -m maximum data transferred */ uint64_t adjust_shift = 0; /* -s adjustment scaling shift */ uint64_t timed_run = 0; /* -T timed run duration */ uint64_t delay_mode = DELAY_R_W_D; /* read, write then delay */ off_t progress_size = 0; int underrun_adjust = UNDERRUN_ADJUST_MAX; int overrun_adjust = OVERRUN_ADJUST_MAX; int fdin = -1, fdout, fdtee = -1; int underruns = 0, overruns = 0, warnings = 0; int ret = EXIT_SUCCESS; bool eof = false; /* EOF on input */ stats_t stats; /* Data rate statistics */ struct sigaction new_action; delay_info_t *di = NULL; stats_init(&stats); for (;;) { const int c = getopt(argc, argv, "ar:h?i:vm:wudot:f:zRs:c:O:SnT:I:VpeD:P:"); size_t len; if (c == -1) break; switch (c) { case 'a': opt_flags |= OPT_APPEND; break; case 'c': opt_flags |= (OPT_GOT_CONST_DELAY | OPT_UNDERRUN | OPT_OVERRUN); const_delay = atof(optarg); underrun_adjust = 1; overrun_adjust = 1; break; case 'D': delay_mode = get_uint64(optarg, &len); break; case 'd': opt_flags |= OPT_DISCARD_STDOUT; break; case 'e': opt_flags |= OPT_SKIP_READ_ERRORS; break; case 'f': freq = atof(optarg); break; case 'h': show_usage(); exit(EXIT_SUCCESS); case 'i': opt_flags |= OPT_GOT_IOSIZE; io_size = (double)get_uint64_byte(optarg); break; case 'I': opt_flags |= OPT_INPUT_FILE; in_filename = optarg; break; case 'm': opt_flags |= OPT_MAX_TRANS_SIZE; max_trans = get_uint64_byte(optarg); break; case 'n': opt_flags |= OPT_NO_RATE_CONTROL; break; case 'o': opt_flags |= OPT_OVERRUN; break; case 'O': opt_flags |= OPT_DISCARD_STDOUT; out_filename = optarg; break; case 'p': opt_flags |= (OPT_PROGRESS | OPT_VERBOSE); break; case 'P': pid_filename = optarg; break; case 'r': data_rate = get_double_byte(optarg); if (data_rate > 1.0 * PB) { fprintf(stderr, "Data rate too high.\n"); exit(EXIT_BAD_OPTION); } opt_flags |= OPT_GOT_RATE; break; case 'R': opt_flags |= OPT_URANDOM; break; case 's': opt_flags |= OPT_GOT_SHIFT; adjust_shift = get_uint64(optarg, &len); break; case 'S': opt_flags |= OPT_STATS; break; case 't': out_filename = optarg; break; case 'T': opt_flags |= OPT_TIMED_RUN; timed_run = get_uint64_time(optarg); break; case 'u': opt_flags |= OPT_UNDERRUN; break; case 'v': opt_flags |= OPT_VERBOSE; break; case 'V': opt_flags |= OPT_VERSION; printf("%s: %s\n", app_name, VERSION); exit(EXIT_SUCCESS); break; case 'w': opt_flags |= OPT_WARNING; break; case 'z': opt_flags |= OPT_ZERO; break; case '?': printf("Try '%s -h' for more information.\n", app_name); exit(EXIT_BAD_OPTION); default: show_usage(); exit(EXIT_BAD_OPTION); } } if (pid_filename) { FILE *pid_file = fopen(pid_filename, "w"); if (pid_file) { fprintf(pid_file, "%d\n", getpid()); (void)fclose(pid_file); } else { fprintf(stderr, "Cannot create pid file '%s', errno=%d (%s).\n", pid_filename, errno, strerror(errno)); ret = EXIT_FILE_ERROR; goto tidy; } } if ((di = get_delay_info(delay_mode)) == NULL) { ret = EXIT_FILE_ERROR; goto tidy; } if ((opt_flags & OPT_NO_RATE_CONTROL) && (opt_flags & (OPT_GOT_CONST_DELAY | OPT_GOT_RATE | OPT_UNDERRUN | OPT_OVERRUN))) { fprintf(stderr, "Cannot use -n option with -c, -r, -u or -o options.\n"); ret = EXIT_BAD_OPTION; goto tidy; } if (!out_filename && (opt_flags & OPT_APPEND)) { fprintf(stderr, "Must use -t filename when using the -a option.\n"); ret = EXIT_BAD_OPTION; goto tidy; } if (!(opt_flags & (OPT_GOT_RATE | OPT_NO_RATE_CONTROL))) { fprintf(stderr, "Must specify data rate with -r option (or use -n for no rate control).\n"); ret = EXIT_BAD_OPTION; goto tidy; } if ((opt_flags & (OPT_GOT_IOSIZE | OPT_GOT_CONST_DELAY)) == (OPT_GOT_IOSIZE | OPT_GOT_CONST_DELAY)) { fprintf(stderr, "Cannot use both -i and -c options together.\n"); ret = EXIT_BAD_OPTION; goto tidy; } if ((opt_flags & OPT_GOT_RATE) && (data_rate < DATA_RATE_MIN)) { fprintf(stderr, "Rate value %.2f too low. Minimum allowed is %.2f bytes/sec.\n", data_rate, DATA_RATE_MIN); ret = EXIT_BAD_OPTION; goto tidy; } if (freq < FREQ_MIN) { fprintf(stderr, "Frequency %.3f too low. Minimum allowed is %.3f Hz.\n", freq, FREQ_MIN); ret = EXIT_BAD_OPTION; goto tidy; } #if DELAY_SHIFT_MIN > 0 if (adjust_shift < DELAY_SHIFT_MIN || adjust_shift > DELAY_SHIFT_MAX) { #else if (adjust_shift > DELAY_SHIFT_MAX) { #endif fprintf(stderr, "Delay shift must be %d .. %d.\n", DELAY_SHIFT_MIN, DELAY_SHIFT_MAX); ret = EXIT_BAD_OPTION; goto tidy; } if ((opt_flags & OPT_GOT_CONST_DELAY) && (const_delay < DELAY_MIN || const_delay > DELAY_MAX)) { fprintf(stderr, "Delay time must be %.2f .. %.2f seconds.\n", DELAY_MIN, DELAY_MAX); ret = EXIT_BAD_OPTION; goto tidy; } /* * No size specified, then default rate */ if (!(opt_flags & OPT_GOT_IOSIZE)) { if (opt_flags & OPT_GOT_CONST_DELAY) { io_size = data_rate * const_delay; if (io_size < IO_SIZE_MIN) { fprintf(stderr, "Delay too small, internal buffer too small.\n"); ret = EXIT_BAD_OPTION; goto tidy; } if (io_size > IO_SIZE_MAX) { fprintf(stderr, "Delay too large, internal buffer too big.\n"); ret = EXIT_BAD_OPTION; goto tidy; } } else { if (opt_flags & OPT_NO_RATE_CONTROL) { io_size = 4 * KB; } else { /* * User has not specified -i or -c, so define * the io_size based on 1/32 of the data rate, * e.g. ~32 writes per second */ io_size = data_rate / 32.0; /* Make sure we don't have small sized I/O */ if (io_size < IO_SIZE_MIN) io_size = IO_SIZE_MIN; if (io_size > IO_SIZE_MAX) { io_size = IO_SIZE_MAX; /* fprintf(stderr, "Rate too high for the buffer size, maximum allowed: %s/sec.\n", double_to_str((double)IO_SIZE_MAX * 32.0)); fprintf(stderr, "Use -i to explicitly set a larger buffer size.\n"); ret = EXIT_BAD_OPTION; goto tidy; */ } } } } if (opt_flags & OPT_MAX_TRANS_SIZE) if (io_size > max_trans) io_size = (double)max_trans; if ((io_size < IO_SIZE_MIN) || (io_size > IO_SIZE_MAX)) { fprintf(stderr, "I/O buffer size too large, maximum allowed: %s.\n", double_to_str((double)IO_SIZE_MAX)); ret = EXIT_BAD_OPTION; goto tidy; } if ((buffer = malloc(BUF_SIZE(io_size))) == NULL) { fprintf(stderr,"Cannot allocate buffer of %.0f bytes.\n", io_size); ret = EXIT_ALLOC_ERROR; goto tidy; } if (opt_flags & OPT_ZERO) memset(buffer, 0, (size_t)io_size); if (count_bits(opt_flags & (OPT_ZERO | OPT_URANDOM | OPT_INPUT_FILE)) > 1) { fprintf(stderr, "Cannot use -z, -R or -I options together.\n"); ret = EXIT_BAD_OPTION; goto tidy; } if (opt_flags & OPT_INPUT_FILE) { struct stat buf; fdin = open(in_filename, O_RDONLY); if (fdin < 0) { fprintf(stderr, "open on %s failed: errno = %d (%s).\n", in_filename, errno, strerror(errno)); ret = EXIT_FILE_ERROR; goto tidy; } if (fstat(fdin, &buf) < 0) { fprintf(stderr, "fstat on file %s failed: errno = %d (%s).\n", in_filename, errno, strerror(errno)); progress_size = 0; } else { progress_size = buf.st_size; } } if (opt_flags & OPT_MAX_TRANS_SIZE) progress_size = (off_t)max_trans; if (opt_flags & OPT_URANDOM) { fdin = open(dev_urandom, O_RDONLY); if (fdin < 0) { fprintf(stderr, "Cannot open %s: errno=%d (%s).\n", dev_urandom, errno, strerror(errno)); ret = EXIT_FILE_ERROR; goto tidy; } } if (out_filename) { int open_flags = (opt_flags & OPT_APPEND) ? O_APPEND : O_TRUNC; (void)umask(0077); fdtee = open(out_filename, O_CREAT | open_flags | O_WRONLY, S_IRUSR | S_IWUSR); if (fdtee < 0) { fprintf(stderr, "open on %s failed: errno = %d (%s).\n", out_filename, errno, strerror(errno)); ret = EXIT_FILE_ERROR; goto tidy; } } /* Default to stdin if not specified */ if (fdin == -1) fdin = fileno(stdin); fdout = fileno(stdout); if ((secs_start = timeval_to_double()) < 0.0) { ret = EXIT_TIME_ERROR; goto tidy; } if (opt_flags & OPT_NO_RATE_CONTROL) { delay = 0.0; } else if (opt_flags & OPT_GOT_CONST_DELAY) { delay = 1000000.0 * const_delay; } else { delay = io_size * 1000000.0 / (double)data_rate; } #if DEBUG_SETUP fprintf(stderr, "io_size: %.0f\n", io_size); fprintf(stderr, "data_rate: %f\n", data_rate); fprintf(stderr, "const_delay: %f\n", const_delay); fprintf(stderr, "delay: %f\n", delay); fprintf(stderr, "progress_size: %" PRIu64 "\n", (uint64_t)progress_size); fprintf(stderr, "max_trans: %" PRIu64 "\n", max_trans); fprintf(stderr, "shift: %" PRIu64 "\n", adjust_shift); #endif secs_last = secs_start; stats.time_begin = secs_start; stats.target_rate = data_rate; memset(&new_action, 0, sizeof(new_action)); new_action.sa_handler = handle_sigint; sigemptyset(&new_action.sa_mask); new_action.sa_flags = 0; if (sigaction(SIGINT, &new_action, NULL) < 0) { fprintf(stderr, "Sigaction failed: errno=%d (%s).\n", errno, strerror(errno)); ret = EXIT_SIGNAL_ERROR; goto tidy; } memset(&new_action, 0, sizeof(new_action)); new_action.sa_handler = handle_siginfo; sigemptyset(&new_action.sa_mask); new_action.sa_flags = 0; if (sigaction(SIGUSR1, &new_action, NULL) < 0) { fprintf(stderr, "Sigaction failed: errno=%d (%s).\n", errno, strerror(errno)); ret = EXIT_SIGNAL_ERROR; goto tidy; } #ifdef SIGINFO if (sigaction(SIGINFO, &new_action, NULL) < 0) { fprintf(stderr, "Sigaction failed: errno=%d (%s).\n", errno, strerror(errno)); ret = EXIT_SIGNAL_ERROR; goto tidy; } #endif memset(&new_action, 0, sizeof(new_action)); new_action.sa_handler = handle_sigusr2; sigemptyset(&new_action.sa_mask); new_action.sa_flags = 0; if (sigaction(SIGUSR2, &new_action, NULL) < 0) { fprintf(stderr, "Sigaction failed: errno=%d (%s).\n", errno, strerror(errno)); ret = EXIT_SIGNAL_ERROR; goto tidy; } /* * Main loop: * read data until buffer is full * write data * get new data rate * adjust delay or buffer size * check for timeout */ while (!(eof | sluice_finish)) { uint64_t inbufsize = 0; bool complete = false; double current_rate, secs_now; DO_DELAY(delay, di, 0, stats); if (opt_flags & OPT_ZERO) { inbufsize = (uint64_t)io_size; total_bytes += (uint64_t)io_size; stats.reads++; } else { char *ptr = buffer; while (!complete && (inbufsize < (uint64_t)io_size)) { uint64_t sz = (uint64_t)io_size - inbufsize; ssize_t n; /* * We hit the user specified max * limit to transfer */ if (max_trans && (total_bytes + sz) > max_trans) { sz = max_trans - total_bytes; complete = true; } n = read(fdin, ptr, (ssize_t)sz); if (n < 0) { if (errno == EINTR) { if (sluice_finish) goto finish; /* read needs re-doing */ continue; } /* Ignore errors? */ if (opt_flags & OPT_SKIP_READ_ERRORS) { /* Ensure block is empty */ memset(ptr, 0, sz); n = sz; } else { fprintf(stderr,"read error: errno=%d (%s).\n", errno, strerror(errno)); ret = EXIT_READ_ERROR; goto tidy; } } if (n == 0) { eof = true; break; } inbufsize += n; total_bytes += n; ptr += n; stats.reads++; } } if (eof) break; DO_DELAY(delay, di, 1, stats); stats.writes++; stats.total_bytes += inbufsize; stats.buf_size_total += inbufsize; if (!(opt_flags & OPT_DISCARD_STDOUT)) { if (write(fdout, buffer, (size_t)inbufsize) < 0) { fprintf(stderr,"Write error: errno=%d (%s).\n", errno, strerror(errno)); ret = EXIT_WRITE_ERROR; goto tidy; } } /* -t Tee mode output */ if (fdtee >= 0) { redo_write: if (write(fdtee, buffer, (size_t)inbufsize) < 0) { if (errno == EINTR) { if (sluice_finish) goto finish; /* write needs re-doing */ goto redo_write; } else { fprintf(stderr, "write error: errno=%d (%s).\n", errno, strerror(errno)); ret = EXIT_WRITE_ERROR; goto tidy; } } } if (eof) break; DO_DELAY(delay, di, 2, stats); if ((secs_now = timeval_to_double()) < 0.0) { ret = EXIT_TIME_ERROR; goto tidy; } current_rate = ((double)total_bytes) / (secs_now - secs_start); /* Update min/max rate stats */ if (stats.rate_set) { if (current_rate > stats.rate_max) stats.rate_max = current_rate; if (current_rate < stats.rate_min) stats.rate_min = current_rate; if (io_size > stats.io_size_max) stats.io_size_max = io_size; if (io_size < stats.io_size_min) stats.io_size_min = io_size; } else { stats.rate_min = current_rate; stats.rate_max = current_rate; stats.io_size_min = io_size; stats.io_size_max = io_size; stats.rate_set = true; } /* Update drift stats only if we have rate controls enabled */ if (!(opt_flags & OPT_NO_RATE_CONTROL)) { double drift_rate = 100.0 * fabs(current_rate - data_rate) / data_rate; int i; double percent = DRIFT_PERCENT_START; stats.drift_total++; for (i = 0; i < DRIFT_MAX; i++, percent *= 2.0) { if (drift_rate < percent) { stats.drift[i]++; break; } } } #if DEBUG_RATE fprintf(stderr, "rate-pre : %.2f delay: %.2f io_size: %.3f\n", current_rate, delay, io_size); #endif if (opt_flags & OPT_NO_RATE_CONTROL) { /* No rate to compare to */ run = '-'; } else { if (current_rate > data_rate) { /* Overrun */ run = '+' ; if (!(opt_flags & OPT_GOT_CONST_DELAY)) { if (adjust_shift) delay += ((last_delay >> adjust_shift) + 100); else { double secs_desired = secs_start + ((total_bytes + inbufsize) / data_rate); delay = 1000000.0 * (secs_desired - secs_now); if (delay < 0) delay = 0; } } warnings = 0; underruns = 0; overruns++; stats.overruns++; } else if (current_rate < data_rate) { /* Underrun */ run = '-' ; if (!(opt_flags & OPT_GOT_CONST_DELAY)) { if (adjust_shift) delay -= ((last_delay >> adjust_shift) + 100); else { double secs_desired = secs_start + ((total_bytes + inbufsize) / data_rate); delay = 1000000.0 * (secs_desired - secs_now); if (delay < 0) delay = 0; } } warnings++; underruns++; stats.underruns++; overruns = 0; } else { /* Perfect, rather unlikely.. */ warnings = 0; underruns = 0; overruns = 0; stats.perfect++; run = '0'; } /* Avoid the impossible */ if (delay < 0) delay = 0; if ((opt_flags & OPT_UNDERRUN) && (underruns >= underrun_adjust)) { /* Adjust rate due to underruns */ char *tmp; double tmp_io_size; if (adjust_shift) { /* Adjust by scaling io_size */ tmp_io_size = io_size + (io_size / (1 << adjust_shift)); /* * If size is too small, we get * stuck at 1 */ if (tmp_io_size < 1) tmp_io_size = 1; } else { /* * Adjust by comparing differences * in rates */ tmp_io_size = io_size + (data_rate - current_rate) * const_delay; } /* Need to grow buffer? */ if ((tmp_io_size > io_size) && (tmp_io_size < IO_SIZE_MAX)) { stats.reallocs++; tmp = realloc(buffer, BUF_SIZE(tmp_io_size)); if (tmp) { if (opt_flags & OPT_ZERO) memset(tmp, 0, tmp_io_size); buffer = tmp; io_size = tmp_io_size; } } underruns = 0; } if ((opt_flags & OPT_OVERRUN) && (overruns >= overrun_adjust)) { /* Adjust rate due to overruns */ char *tmp; double tmp_io_size; if (adjust_shift) { /* Adjust by scaling io_size */ tmp_io_size = io_size - (io_size / (1 << adjust_shift)); /* * If size is too small, we get * stuck at 1 */ if (tmp_io_size < 1) tmp_io_size = 1; } else { /* * Adjust by comparing differences * in rates */ tmp_io_size = io_size + (data_rate - current_rate) * const_delay; } /* Need to grow buffer? */ if ((tmp_io_size > io_size) && (tmp_io_size < IO_SIZE_MAX)) { stats.reallocs++; tmp = realloc(buffer, BUF_SIZE(tmp_io_size)); if (tmp) { if (opt_flags & OPT_ZERO) memset(tmp, 0, tmp_io_size); buffer = tmp; io_size = tmp_io_size; } } overruns = 0; } /* Too many continuous underruns? */ if ((opt_flags & OPT_WARNING) && (warnings > UNDERRUN_MAX)) { fprintf(stderr, "Warning: data underrun, " "use larger I/O size (-i option)\n"); opt_flags &= ~OPT_WARNING; } } last_delay = (uint64_t)delay; /* Output feedback in verbose mode */ if ((opt_flags & OPT_VERBOSE) && (secs_now > secs_last + freq)) { char current_rate_str[32]; char total_bytes_str[32]; size_to_str(current_rate, "%7.1f %s", current_rate_str, sizeof(current_rate_str)); size_to_str(total_bytes, "%7.1f %s", total_bytes_str, sizeof(total_bytes_str)); if (opt_flags & OPT_PROGRESS) { /* Progress % and ETA estimates */ double secs = secs_now - secs_start; if (progress_size) { double percent = 100.0 * (double)progress_size / (double)stats.total_bytes; double alpha = secs * (double)progress_size / (double)stats.total_bytes; double secs_left = alpha - secs; fprintf(stderr,"Rate: %s/S, " "Total: %s, Dur: %.1f S, %5.1f%% ETA: %s \r", current_rate_str, total_bytes_str, secs, percent, secs_to_str(secs_left)); } else { /* No size, avoid division by zero */ fprintf(stderr,"Rate: %s/S, " "Total: %s, Dur: %.1f S, ??.?%% ETA: ?.? S \r", current_rate_str, total_bytes_str, secs); } } else { /* Default progress info */ char io_size_str[32]; size_to_str(io_size, "%7.1f %s", io_size_str, sizeof(io_size_str)); fprintf(stderr,"Rate: %s/S, Adj: %c, " "Total: %s, Dur: %.1f S, Buf: %s \r", current_rate_str, run, total_bytes_str, secs_now - secs_start, io_size_str); } (void)fflush(stderr); secs_last = secs_now; } #if DEBUG_RATE fprintf(stderr, "rate-post: %.2f delay: %.2f io_size: %.3f\n", current_rate, delay, io_size); #endif /* Timed run, if we timed out then stop */ if ((opt_flags & OPT_TIMED_RUN) && ((secs_now - secs_start) > timed_run)) break; if (max_trans && total_bytes >= max_trans) break; } ret = EXIT_SUCCESS; finish: if (opt_flags & OPT_VERBOSE) fprintf(stderr, "%78s\r", ""); if (opt_flags & OPT_STATS) { if ((stats.time_end = timeval_to_double()) < 0.0) { ret = EXIT_TIME_ERROR; goto tidy; } stats_info(&stats); } tidy: if (pid_filename) { (void)unlink(pid_filename); } if ((fdin != -1) && (opt_flags & OPT_URANDOM)) { (void)close(fdin); } free(buffer); if (fdtee >= 0) (void)close(fdtee); exit(ret); } sluice-0.02.02/sluice.10000664000175000017500000002676612602321276013173 0ustar kingking.\" Hey, EMACS: -*- nroff -*- .\" First parameter, NAME, should be all caps .\" Second parameter, SECTION, should be 1-8, maybe w/ subsection .\" other parameters are allowed: see man(7), man(1) .TH SLUICE 1 "May 18, 2015" .\" Please adjust this date whenever revising the manpage. .\" .\" Some roff macros, for reference: .\" .nh disable hyphenation .\" .hy enable hyphenation .\" .ad l left justify .\" .ad b justify to both left and right margins .\" .nf disable filling .\" .fi enable filling .\" .br insert line break .\" .sp insert n+1 empty lines .\" for manpage-specific macros, see man(7) .SH NAME sluice \- a tool to control data flow at a specified rate .br .SH SYNOPSIS .B sluice .RI [options] .br .SH DESCRIPTION sluice reads input and outputs a specified data rate. It has various data rate controlling mechanisms that can be tuned for specific use cases where necessary. .SH OPTIONS sluice options are as follow: .TP .B \-a append output to a file (used in conjunction with the \-t 'tee' or \-O options). Instead of creating a new file or truncating an existing file, this option appends data to the file. .TP .B \-c delay enables a constant delay time (in seconds) between writes. This option adjusts the output buffer size to try and keep the data rate constant. The output buffer size in this mode is initially set to the data rate \(mu the delay. .br This option is mutually exclusive to the \-i option and implicitly enables the \-o overrun and \-u underrun buffer management options to dynamically re-size the read/write buffer to keep the data rate constant. By default this adjusts the buffer based on the total amount of data transferred and the time to write this (equivalent to the \-s 0 turning mode). However, if the \-s shift value is greater than 0, then the new size is adjusted by the previous size right shifted by the shift value. .TP .B \-d discard data, do not copy it to stdout. This makes sluice act as a data sink. .TP .B \-D mode select the delay mode. The are various approaches to when to perform the data rate delays. The default is to perform the read, then write and finally the delay for each iteration. However, the \-D option allows one to select the delay mode as follows: .TS center; cB cB cB c l l. Mode Delay strategy Delay Duration 0 Read, Write, Delay (default) 1 \(mu delay time 1 Delay, Read, Write 1 \(mu delay time 2 Read, Delay, Write 1 \(mu delay time 3 Delay, Read, Delay, Write 2 \(mu 1/2 delay time 4 Read, Delay, Write, Delay 2 \(mu 1/2 delay time 5 Delay, Read, Delay, Write, Delay 3 \(mu 1/3 delay time .TE .br Note that modes 3 and 4 perform two delays each comprising of 1/2 the delay time and mode 5 performs 3 delays each comprising of 1/3 the delay time. .br Modes 1, 3, 5 maybe considered as not entirely accurate in terms of the total run duration. In these modes an extraneous delay occurs before the final end-of-file empty read is performed. .TP .B \-e ignore read errors. The failed read is replaced by zeros. .TP .B \-f freq specify the frequency of \-v verbose statistics updates. The default is 1/4 of a second. Note that sluice will try to emit updates close to the requested frequency, however, if the read/write rate is less than the frequency then the updates occur only at the read/write rate. .TP .B \-h show help .TP .B \-i size specify the read/write size in bytes. The K, M, G, T and P suffixes allow one to specify size in Kilobytes, Megabytes, Gigabytes, Terabytes and Petabytes respectively. This option is mutually exclusive to the \-c option. .br In this mode, the delays between writes are used to control the data rate. By default the delay is based on the total amount of data transferred and the time taken to write this. This is equivalent to the \-s 0 tuning mode. However, if the \-s shift value is greater than 0, then the new delay is adjusted by the previous delay right shifted by the shift value. .br A special hybrid rate control mode can be invoked by also using the \-o overflow and \-u underflow options to also enable dynamic re-sizing of the read/write buffer. By default this adjusts the buffer based on the total amount of data transferred and the time to write this (equivalent to the \-s 0 turning mode). However, if the \-s shift value is greater than 0, then the new size is adjusted by the previous size right shifted by the shift value. .TP .B \-I file read input from file rather than from stdin. .TP .B \-m size specify amount of data to process, the default size is in bytes, but the K, M, G, T and P suffixes can specify size in Kilobytes, Megabytes, Gigabytes, Terabytes and Petabytes respectively. If this size is less than the write size, then the write size is truncated to be the \-m size. .TP .B \-n no rate control. This is just a straight data copy much like cat and all data rate controls cannot be used. Combined with the \-v and \-S options one can observe the data rates of the copy. .TP .B \-o detect overrun and re-size read/write buffer size to try and stop overrun. This will shrink the buffer each time consecutive overruns are detected. See the \-s option for details of the size re-adjustment mechanism. .TP .B \-O file send output to file, equivalent to \-dt file .TP .B \-p enable verbose stats showing % progress and ETA information. This is only valid using the \-I or \-m option and the if file size is non-zero. See the \-v option for more details. .TP .B \-P pidfile write the process ID of sluice in file pidfile. The file is removed when sluice exits. .TP .B \-r rate specify the data rate in bytes per second. The K, M, G and T suffixes can specify the rate in Kilobytes/sec, Megabytes/sec, Gigabytes/sec and Terabytes/sec respectively. This option must always be provided except when the \-n option is used. .TP .B \-R do not read from stdin, instead read random data from /dev/urandom. .TP .B \-s shift modify the rate adjustment shift. This is a data rate tuning scaling factor used by the \-r, \-c, \-o and -\u options. .br For the \-r option, the delay between each write is controlled by modifying the previous delay by adding or subtracting the previous delay right shifted by this shift value. The larger the shift value the longer it takes to adjust up/down to the specified rate. The smaller the shift value the quicker it takes to reach the optimal delay, however, this can result in a highly fluctuating rates at the the beginning because the delay varies by a large amount causing large overruns and underruns. A shift value of 3 works well for most fast rates. .br For the \-c, \-o and \-u options, the size of the buffer is modified by adding or subtracting the previous size shifted by the shift value. Again, a shift value of 3 works well for most fast rates. .br If the shift value is set to 0, then the shift rate adjustment tuning mechanism is explicitly turned off and data rates are adjusted based on the total amount of data transferred and the time to write this. .br Small \-s shift values of 1 and 2 can cause rapid oscillations before data rate damping fully kicks into action. The value of \-s 0 (the default) is recommended for accurate low-speed data transfers. .TP .B \-S print various performance and buffering statistics to stderr when end of file is reached. .TP .B \-t file tee output to the specified file. Output is written to both stdout and to the named file. By default, the file will be created if it does not exist or re-written if it already exists. Use the \-a option to append to an existing file. .TP .B \-T t stop slice test after t seconds. One can also specify the units of time in seconds, minutes, hours, days or years with the suffix s, m, h, d or y. .TP .B \-u detect underrun and re-size read/write buffer size to try and stop underrun. This will expand the buffer each time consecutive underruns are detected. The buffer will not be expanded any more than 4MB in size. See the \-s option for details of the size re-adjustment mechanism. .TP .B \-v write verbose statistics to stderr. By default, this will display the current data rate, the last data rate adjusment ('-' = underrun, '+' = overrun), total bytes transferred, duration and the current buffer size. .br With the \-p option, the progess statistics are displayed. This will display the current data rate, total bytes transferred, duration, percentage complete so far and the estimated time to completion. Note that the estimation is available using the \-I and \-m options and if the file size is non-zero. .TP .B \-V print version information to standard out and exit successfully. .TP .B \-w warn if a long burst of continuous data rate underrun occurs, the warning is issued just once. To overcome the underrun increase the \-i read/write buffer size or use the \-u option to auto-expand the read/write buffer. Too many underruns implies that too small a buffer or not enough CPU is available to keep up with the required data rate. .TP .B \-z do not read from stdin, instead generate a stream of zeros (equivalent to reading from /dev/zero). .TP .B SIGUSR1 SIGINFO Sending SIGUSR1 (or SIGINFO on BSD systems) will toggle the verbose data rate mode on/off. .TP .B SIGUSR2 Toggle underrun/overrun (-u, -o) options on/off. .SH NOTES If neither \-i or \-c options are used, then sluice defaults to using a write buffer size of 1/32 of the data rate and bounded between the limits of 1 byte and 64MB. Sluice will try to keep the data rate steady by adjusting the delay between writes. To tune this, see the \-s option. .SH EXAMPLES .LP Read /dev/zero and write in 4K sizes at the rate of 1MB/sec to the file 'example.dat' .RS 8 cat /dev/zero | sluice \-i 4K \-r 1M > example.dat .RE .LP Read 32MB from /dev/zero and write at the rate of 64K/sec to stdout with feedback on duration and ETA on stderr using 4K buffer writes and a tuning shift of 4. .RS 8 cat /dev/zero | sluice \-r 64K \-vp \-m 32M \-i 4K \-s 4 .RE .LP Generate a stream of zeros and write at a rate of 1MB/sec to a fifo named 'myfifo' with underrun and overrun buffer management .RS 8 sluice \-z \-u \-o \-r 1MB \-O myfifo .RE .LP Write random data at 5MB per second to the file 'myfile' doing a write every 0.1 seconds .RS 8 sluice \-R \-r 5M \-c 0.1 > myfile .RE .LP Write zeros to the file 'example-file' in 64K chunks and measure write rate as a crude throughput test .RS 8 sluice \-nzSv \-f 0.1 \-i 64K > example-file .RE .LP Read data from somehost.com on port 1234 at a rate of 2MB per second and discard the data, e.g. this is a constant rate data sink. .RS 8 nc somehost.com 1234 | sluice -d -r 2MB -i 8K .RE .SH EXIT STATUS Sluice sets the exit status as follows: .TS center; cB cB c l. Status Decription 0 Exited successfully. 1 Invalid or out of range option provided. 2 File open error. 3 Sleep error. 4 Failed to get time of day. 5 Signal handler setup error. 6 Read error (file or stdin). 7 Write error (file or stdout). 8 Buffer allocation failed. .TE .SH BUGS Stopping and starting sluice using SIGSTOP and SIGCONT will interfere with the internal buffering rate calculations causing sluice to try to catch up and this may affect the short term data rate immediately after the SIGCONT. .SH SEE ALSO .BR cat(1), .BR pv(1), .BR cstream(1) .SH AUTHOR sluice was written by Colin King with testing feedback and help from Kamal Mostafa. .PP This manual page was written by Colin King , for the Ubuntu project (but may be used by others). .SH COPYRIGHT Copyright \(co 2014-2015 Canonical Ltd. .br This is free software; see the source for copying conditions. There is NO warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. sluice-0.02.02/COPYING0000664000175000017500000004325412602321276012647 0ustar kingking GNU GENERAL PUBLIC LICENSE Version 2, June 1991 Copyright (C) 1989, 1991 Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA Everyone is permitted to copy and distribute verbatim copies of this license document, but changing it is not allowed. Preamble The licenses for most software are designed to take away your freedom to share and change it. By contrast, the GNU General Public License is intended to guarantee your freedom to share and change free software--to make sure the software is free for all its users. This General Public License applies to most of the Free Software Foundation's software and to any other program whose authors commit to using it. (Some other Free Software Foundation software is covered by the GNU Lesser General Public License instead.) You can apply it to your programs, too. When we speak of free software, we are referring to freedom, not price. Our General Public Licenses are designed to make sure that you have the freedom to distribute copies of free software (and charge for this service if you wish), that you receive source code or can get it if you want it, that you can change the software or use pieces of it in new free programs; and that you know you can do these things. To protect your rights, we need to make restrictions that forbid anyone to deny you these rights or to ask you to surrender the rights. These restrictions translate to certain responsibilities for you if you distribute copies of the software, or if you modify it. For example, if you distribute copies of such a program, whether gratis or for a fee, you must give the recipients all the rights that you have. You must make sure that they, too, receive or can get the source code. And you must show them these terms so they know their rights. We protect your rights with two steps: (1) copyright the software, and (2) offer you this license which gives you legal permission to copy, distribute and/or modify the software. Also, for each author's protection and ours, we want to make certain that everyone understands that there is no warranty for this free software. If the software is modified by someone else and passed on, we want its recipients to know that what they have is not the original, so that any problems introduced by others will not reflect on the original authors' reputations. Finally, any free program is threatened constantly by software patents. We wish to avoid the danger that redistributors of a free program will individually obtain patent licenses, in effect making the program proprietary. To prevent this, we have made it clear that any patent must be licensed for everyone's free use or not licensed at all. The precise terms and conditions for copying, distribution and modification follow. GNU GENERAL PUBLIC LICENSE TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION 0. This License applies to any program or other work which contains a notice placed by the copyright holder saying it may be distributed under the terms of this General Public License. The "Program", below, refers to any such program or work, and a "work based on the Program" means either the Program or any derivative work under copyright law: that is to say, a work containing the Program or a portion of it, either verbatim or with modifications and/or translated into another language. (Hereinafter, translation is included without limitation in the term "modification".) Each licensee is addressed as "you". Activities other than copying, distribution and modification are not covered by this License; they are outside its scope. The act of running the Program is not restricted, and the output from the Program is covered only if its contents constitute a work based on the Program (independent of having been made by running the Program). Whether that is true depends on what the Program does. 1. You may copy and distribute verbatim copies of the Program's source code as you receive it, in any medium, provided that you conspicuously and appropriately publish on each copy an appropriate copyright notice and disclaimer of warranty; keep intact all the notices that refer to this License and to the absence of any warranty; and give any other recipients of the Program a copy of this License along with the Program. You may charge a fee for the physical act of transferring a copy, and you may at your option offer warranty protection in exchange for a fee. 2. You may modify your copy or copies of the Program or any portion of it, thus forming a work based on the Program, and copy and distribute such modifications or work under the terms of Section 1 above, provided that you also meet all of these conditions: a) You must cause the modified files to carry prominent notices stating that you changed the files and the date of any change. b) You must cause any work that you distribute or publish, that in whole or in part contains or is derived from the Program or any part thereof, to be licensed as a whole at no charge to all third parties under the terms of this License. c) If the modified program normally reads commands interactively when run, you must cause it, when started running for such interactive use in the most ordinary way, to print or display an announcement including an appropriate copyright notice and a notice that there is no warranty (or else, saying that you provide a warranty) and that users may redistribute the program under these conditions, and telling the user how to view a copy of this License. (Exception: if the Program itself is interactive but does not normally print such an announcement, your work based on the Program is not required to print an announcement.) These requirements apply to the modified work as a whole. If identifiable sections of that work are not derived from the Program, and can be reasonably considered independent and separate works in themselves, then this License, and its terms, do not apply to those sections when you distribute them as separate works. But when you distribute the same sections as part of a whole which is a work based on the Program, the distribution of the whole must be on the terms of this License, whose permissions for other licensees extend to the entire whole, and thus to each and every part regardless of who wrote it. Thus, it is not the intent of this section to claim rights or contest your rights to work written entirely by you; rather, the intent is to exercise the right to control the distribution of derivative or collective works based on the Program. In addition, mere aggregation of another work not based on the Program with the Program (or with a work based on the Program) on a volume of a storage or distribution medium does not bring the other work under the scope of this License. 3. You may copy and distribute the Program (or a work based on it, under Section 2) in object code or executable form under the terms of Sections 1 and 2 above provided that you also do one of the following: a) Accompany it with the complete corresponding machine-readable source code, which must be distributed under the terms of Sections 1 and 2 above on a medium customarily used for software interchange; or, b) Accompany it with a written offer, valid for at least three years, to give any third party, for a charge no more than your cost of physically performing source distribution, a complete machine-readable copy of the corresponding source code, to be distributed under the terms of Sections 1 and 2 above on a medium customarily used for software interchange; or, c) Accompany it with the information you received as to the offer to distribute corresponding source code. (This alternative is allowed only for noncommercial distribution and only if you received the program in object code or executable form with such an offer, in accord with Subsection b above.) The source code for a work means the preferred form of the work for making modifications to it. For an executable work, complete source code means all the source code for all modules it contains, plus any associated interface definition files, plus the scripts used to control compilation and installation of the executable. However, as a special exception, the source code distributed need not include anything that is normally distributed (in either source or binary form) with the major components (compiler, kernel, and so on) of the operating system on which the executable runs, unless that component itself accompanies the executable. If distribution of executable or object code is made by offering access to copy from a designated place, then offering equivalent access to copy the source code from the same place counts as distribution of the source code, even though third parties are not compelled to copy the source along with the object code. 4. You may not copy, modify, sublicense, or distribute the Program except as expressly provided under this License. Any attempt otherwise to copy, modify, sublicense or distribute the Program is void, and will automatically terminate your rights under this License. However, parties who have received copies, or rights, from you under this License will not have their licenses terminated so long as such parties remain in full compliance. 5. You are not required to accept this License, since you have not signed it. However, nothing else grants you permission to modify or distribute the Program or its derivative works. These actions are prohibited by law if you do not accept this License. Therefore, by modifying or distributing the Program (or any work based on the Program), you indicate your acceptance of this License to do so, and all its terms and conditions for copying, distributing or modifying the Program or works based on it. 6. Each time you redistribute the Program (or any work based on the Program), the recipient automatically receives a license from the original licensor to copy, distribute or modify the Program subject to these terms and conditions. You may not impose any further restrictions on the recipients' exercise of the rights granted herein. You are not responsible for enforcing compliance by third parties to this License. 7. If, as a consequence of a court judgment or allegation of patent infringement or for any other reason (not limited to patent issues), conditions are imposed on you (whether by court order, agreement or otherwise) that contradict the conditions of this License, they do not excuse you from the conditions of this License. If you cannot distribute so as to satisfy simultaneously your obligations under this License and any other pertinent obligations, then as a consequence you may not distribute the Program at all. For example, if a patent license would not permit royalty-free redistribution of the Program by all those who receive copies directly or indirectly through you, then the only way you could satisfy both it and this License would be to refrain entirely from distribution of the Program. If any portion of this section is held invalid or unenforceable under any particular circumstance, the balance of the section is intended to apply and the section as a whole is intended to apply in other circumstances. It is not the purpose of this section to induce you to infringe any patents or other property right claims or to contest validity of any such claims; this section has the sole purpose of protecting the integrity of the free software distribution system, which is implemented by public license practices. Many people have made generous contributions to the wide range of software distributed through that system in reliance on consistent application of that system; it is up to the author/donor to decide if he or she is willing to distribute software through any other system and a licensee cannot impose that choice. This section is intended to make thoroughly clear what is believed to be a consequence of the rest of this License. 8. If the distribution and/or use of the Program is restricted in certain countries either by patents or by copyrighted interfaces, the original copyright holder who places the Program under this License may add an explicit geographical distribution limitation excluding those countries, so that distribution is permitted only in or among countries not thus excluded. In such case, this License incorporates the limitation as if written in the body of this License. 9. The Free Software Foundation may publish revised and/or new versions of the General Public License from time to time. Such new versions will be similar in spirit to the present version, but may differ in detail to address new problems or concerns. Each version is given a distinguishing version number. If the Program specifies a version number of this License which applies to it and "any later version", you have the option of following the terms and conditions either of that version or of any later version published by the Free Software Foundation. If the Program does not specify a version number of this License, you may choose any version ever published by the Free Software Foundation. 10. If you wish to incorporate parts of the Program into other free programs whose distribution conditions are different, write to the author to ask for permission. For software which is copyrighted by the Free Software Foundation, write to the Free Software Foundation; we sometimes make exceptions for this. Our decision will be guided by the two goals of preserving the free status of all derivatives of our free software and of promoting the sharing and reuse of software generally. NO WARRANTY 11. BECAUSE THE PROGRAM IS LICENSED FREE OF CHARGE, THERE IS NO WARRANTY FOR THE PROGRAM, TO THE EXTENT PERMITTED BY APPLICABLE LAW. EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR OTHER PARTIES PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE PROGRAM IS WITH YOU. SHOULD THE PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF ALL NECESSARY SERVICING, REPAIR OR CORRECTION. 12. IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY AND/OR REDISTRIBUTE THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED TO LOSS OF DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER PROGRAMS), EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. END OF TERMS AND CONDITIONS How to Apply These Terms to Your New Programs If you develop a new program, and you want it to be of the greatest possible use to the public, the best way to achieve this is to make it free software which everyone can redistribute and change under these terms. To do so, attach the following notices to the program. It is safest to attach them to the start of each source file to most effectively convey the exclusion of warranty; and each file should have at least the "copyright" line and a pointer to where the full notice is found. Copyright (C) This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. Also add information on how to contact you by electronic and paper mail. If the program is interactive, make it output a short notice like this when it starts in an interactive mode: Gnomovision version 69, Copyright (C) year name of author Gnomovision comes with ABSOLUTELY NO WARRANTY; for details type `show w'. This is free software, and you are welcome to redistribute it under certain conditions; type `show c' for details. The hypothetical commands `show w' and `show c' should show the appropriate parts of the General Public License. Of course, the commands you use may be called something other than `show w' and `show c'; they could even be mouse-clicks or menu items--whatever suits your program. You should also get your employer (if you work as a programmer) or your school, if any, to sign a "copyright disclaimer" for the program, if necessary. Here is a sample; alter the names: Yoyodyne, Inc., hereby disclaims all copyright interest in the program `Gnomovision' (which makes passes at compilers) written by James Hacker. , 1 April 1989 Ty Coon, President of Vice This General Public License does not permit incorporating your program into proprietary programs. If your program is a subroutine library, you may consider it more useful to permit linking proprietary applications with the library. If this is what you want to do, use the GNU Lesser General Public License instead of this License.