From owner-svn-src-all@FreeBSD.ORG Wed Jun 20 04:11:34 2012 Return-Path: Delivered-To: svn-src-all@freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [69.147.83.52]) by hub.freebsd.org (Postfix) with ESMTP id E51371065670; Wed, 20 Jun 2012 04:11:34 +0000 (UTC) (envelope-from scottl@FreeBSD.org) Received: from svn.freebsd.org (svn.freebsd.org [IPv6:2001:4f8:fff6::2c]) by mx1.freebsd.org (Postfix) with ESMTP id CDFF58FC0C; Wed, 20 Jun 2012 04:11:34 +0000 (UTC) Received: from svn.freebsd.org (localhost [127.0.0.1]) by svn.freebsd.org (8.14.4/8.14.4) with ESMTP id q5K4BYOI017746; Wed, 20 Jun 2012 04:11:34 GMT (envelope-from scottl@svn.freebsd.org) Received: (from scottl@localhost) by svn.freebsd.org (8.14.4/8.14.4/Submit) id q5K4BYTw017744; Wed, 20 Jun 2012 04:11:34 GMT (envelope-from scottl@svn.freebsd.org) Message-Id: <201206200411.q5K4BYTw017744@svn.freebsd.org> From: Scott Long Date: Wed, 20 Jun 2012 04:11:34 +0000 (UTC) To: src-committers@freebsd.org, svn-src-all@freebsd.org, svn-src-head@freebsd.org X-SVN-Group: head MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Cc: Subject: svn commit: r237285 - head/sbin/camcontrol X-BeenThere: svn-src-all@freebsd.org X-Mailman-Version: 2.1.5 Precedence: list List-Id: "SVN commit messages for the entire src tree \(except for " user" and " projects" \)" List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Wed, 20 Jun 2012 04:11:35 -0000 Author: scottl Date: Wed Jun 20 04:11:34 2012 New Revision: 237285 URL: http://svn.freebsd.org/changeset/base/237285 Log: Add progress.c and progress.h, missed in the previous commit to camcontrol. Submitted by: Garrett Cooper Obtained from: Netflix, Inc. Added: head/sbin/camcontrol/progress.c (contents, props changed) head/sbin/camcontrol/progress.h (contents, props changed) Added: head/sbin/camcontrol/progress.c ============================================================================== --- /dev/null 00:00:00 1970 (empty, because file is newly added) +++ head/sbin/camcontrol/progress.c Wed Jun 20 04:11:34 2012 (r237285) @@ -0,0 +1,186 @@ +/* $NetBSD: progressbar.c,v 1.21 2009/04/12 10:18:52 lukem Exp $ */ + +/*- + * Copyright (c) 1997-2009 The NetBSD Foundation, Inc. + * All rights reserved. + * + * This code is derived from software contributed to The NetBSD Foundation + * by Luke Mewburn. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS + * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED + * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS + * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + */ + +#include +#include +#include + +#include +#include +#include +#include +#include +#include +#include + +#include +__FBSDID("$FreeBSD$"); + +#include "progress.h" + +static const char * const suffixes[] = { + "", /* 2^0 (byte) */ + "KiB", /* 2^10 Kibibyte */ + "MiB", /* 2^20 Mebibyte */ + "GiB", /* 2^30 Gibibyte */ + "TiB", /* 2^40 Tebibyte */ + "PiB", /* 2^50 Pebibyte */ + "EiB", /* 2^60 Exbibyte */ +}; + +#define NSUFFIXES (sizeof(suffixes) / sizeof(suffixes[0])) +#define SECSPERHOUR (60 * 60) +#define DEFAULT_TTYWIDTH 80 + +/* initialise progress meter structure */ +int +progress_init(progress_t *prog, const char *prefix, uint64_t total) +{ + struct winsize winsize; + int oerrno = errno; + + (void) memset(prog, 0x0, sizeof(*prog)); + prog->size = total; + prog->prefix = strdup(prefix); + prog->start = time(NULL); + if (ioctl(STDOUT_FILENO, TIOCGWINSZ, &winsize) != -1 && + winsize.ws_col != 0) { + prog->ttywidth = winsize.ws_col; + } else { + prog->ttywidth = DEFAULT_TTYWIDTH; + } + errno = oerrno; + return 1; +} + +/* update the values in the progress meter */ +int +progress_update(progress_t *prog, uint64_t done) +{ + prog->done = done; + prog->percent = (prog->done * 100) / prog->size; + prog->now = time(NULL); + prog->elapsed = prog->now - prog->start; + if (done == 0 || prog->elapsed == 0 || prog->done / prog->elapsed == 0) { + prog->eta = 0; + } else { + prog->eta = prog->size / (prog->done / prog->elapsed) - prog->elapsed; + } + return 1; +} + +/* update the values in the progress meter */ +int +progress_reset_size(progress_t *prog, uint64_t size) +{ + prog->size = size; + return 1; +} + +/* make it look pretty at the end - display done bytes (usually total) */ +int +progress_complete(progress_t *prog, uint64_t done) +{ + progress_update(prog, done); + progress_draw(prog); + printf("\n"); + return 1; +} + +/* draw the progress meter */ +int +progress_draw(progress_t *prog) +{ +#define BAROVERHEAD 45 /* non `*' portion of progress bar */ + /* + * stars should contain at least + * sizeof(buf) - BAROVERHEAD entries + */ + static const char stars[] = +"*****************************************************************************" +"*****************************************************************************" +"*****************************************************************************"; + unsigned bytesabbrev; + unsigned bpsabbrev; + int64_t secs; + uint64_t bytespersec; + uint64_t abbrevsize; + int64_t secsleft; + size_t barlength; + size_t starc; + char hours[12]; + char buf[256]; + int len; + + barlength = MIN(sizeof(buf) - 1, (unsigned)prog->ttywidth) - BAROVERHEAD - strlen(prog->prefix); + starc = (barlength * prog->percent) / 100; + abbrevsize = prog->done; + for (bytesabbrev = 0; abbrevsize >= 100000 && bytesabbrev < NSUFFIXES; bytesabbrev++) { + abbrevsize >>= 10; + } + if (bytesabbrev == NSUFFIXES) { + bytesabbrev--; + } + bytespersec = 0; + if (prog->done > 0) { + bytespersec = prog->done; + if (prog->elapsed > 0) { + bytespersec /= prog->elapsed; + } + } + for (bpsabbrev = 1; bytespersec >= 1024000 && bpsabbrev < NSUFFIXES; bpsabbrev++) { + bytespersec >>= 10; + } + if (prog->done == 0 || prog->elapsed <= 0 || prog->done > prog->size) { + secsleft = 0; + } else { + secsleft = prog->eta; + } + if ((secs = secsleft / SECSPERHOUR) > 0) { + (void) snprintf(hours, sizeof(hours), "%2lld:", (long long)secs); + } else { + (void) snprintf(hours, sizeof(hours), " "); + } + secs = secsleft % SECSPERHOUR; + len = snprintf(buf, sizeof(buf), + "\r%s %3lld%% |%.*s%*s| %5lld %-3s %3lld.%02d %.2sB/s %s%02d:%02d ETA", + (prog->prefix) ? prog->prefix : "", + (long long)prog->percent, + (int)starc, stars, (int)(barlength - starc), "", + (long long)abbrevsize, + suffixes[bytesabbrev], + (long long)(bytespersec / 1024), + (int)((bytespersec % 1024) * 100 / 1024), + suffixes[bpsabbrev], + hours, + (int)secs / 60, (int)secs % 60); + return (int)write(STDOUT_FILENO, buf, len); +} Added: head/sbin/camcontrol/progress.h ============================================================================== --- /dev/null 00:00:00 1970 (empty, because file is newly added) +++ head/sbin/camcontrol/progress.h Wed Jun 20 04:11:34 2012 (r237285) @@ -0,0 +1,60 @@ +/* $NetBSD: progressbar.c,v 1.21 2009/04/12 10:18:52 lukem Exp $ */ + +/*- + * Copyright (c) 1997-2012 The NetBSD Foundation, Inc. + * All rights reserved. + * + * This code is derived from software contributed to The NetBSD Foundation + * by Luke Mewburn. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS + * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED + * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS + * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + * + * $FreeBSD$ + */ + +#ifndef PROGRESS_H_ +#define PROGRESS_H_ 20100228 + +#include + +#include + +/* structure used to display a progress meter */ +typedef struct progress_t { + char *prefix; /* any prefix explanation */ + uint64_t size; /* total of bytes/units to be counted */ + uint64_t done; /* number of units counted to date */ + uint64_t percent; /* cache the percentage complete */ + time_t start; /* time we started this */ + time_t now; /* time now */ + time_t eta; /* estimated # of secs until completion */ + int64_t elapsed; /* cached # of elapsed seconds */ + int32_t ttywidth; /* width of tty in columns */ +} progress_t; + +int progress_init(progress_t */*meter*/, const char */*prefix*/, uint64_t /*size*/); +int progress_update(progress_t */*meter*/, uint64_t /*done*/); +int progress_draw(progress_t */*meter*/); +int progress_reset_size(progress_t */*meter*/, uint64_t /*size*/); +int progress_complete(progress_t */*meter*/, uint64_t /*done*/); + +#endif