From patchwork Thu Aug 2 04:35:05 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Amitay Isaacs X-Patchwork-Id: 952577 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Received: from lists.ozlabs.org (lists.ozlabs.org [IPv6:2401:3900:2:1::3]) (using TLSv1.2 with cipher ADH-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id 41gy5n1B2Wz9rxx for ; Thu, 2 Aug 2018 14:35:25 +1000 (AEST) Authentication-Results: ozlabs.org; dmarc=none (p=none dis=none) header.from=ozlabs.org Authentication-Results: ozlabs.org; dkim=fail reason="signature verification failed" (2048-bit key; secure) header.d=ozlabs.org header.i=@ozlabs.org header.b="G7bYSqT/"; dkim-atps=neutral Received: from lists.ozlabs.org (lists.ozlabs.org [IPv6:2401:3900:2:1::3]) by lists.ozlabs.org (Postfix) with ESMTP id 41gy5l1NDqzF1Qn for ; Thu, 2 Aug 2018 14:35:23 +1000 (AEST) Authentication-Results: lists.ozlabs.org; dmarc=none (p=none dis=none) header.from=ozlabs.org Authentication-Results: lists.ozlabs.org; dkim=fail reason="signature verification failed" (2048-bit key; secure) header.d=ozlabs.org header.i=@ozlabs.org header.b="G7bYSqT/"; dkim-atps=neutral X-Original-To: pdbg@lists.ozlabs.org Delivered-To: pdbg@lists.ozlabs.org Received: from ozlabs.org (bilbo.ozlabs.org [203.11.71.1]) (using TLSv1.2 with cipher ADH-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by lists.ozlabs.org (Postfix) with ESMTPS id 41gy5W2ykHzF1R6 for ; Thu, 2 Aug 2018 14:35:11 +1000 (AEST) Authentication-Results: lists.ozlabs.org; dmarc=none (p=none dis=none) header.from=ozlabs.org Authentication-Results: lists.ozlabs.org; dkim=pass (2048-bit key; secure) header.d=ozlabs.org header.i=@ozlabs.org header.b="G7bYSqT/"; dkim-atps=neutral Received: from authenticated.ozlabs.org (localhost [127.0.0.1]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPSA id 41gy5W01cLz9s3x; Thu, 2 Aug 2018 14:35:10 +1000 (AEST) Authentication-Results: ozlabs.org; dmarc=none (p=none dis=none) header.from=ozlabs.org DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=ozlabs.org; s=201707; t=1533184511; bh=YC5NmQ+ZrLocy8sWPgD2AqShf+0S9M2agc3gwZ/izec=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=G7bYSqT/wzGNsHqjxJKd96TAabsO93lqIDvCsC/ESoj6pBW7b5Mz02Q72rOg4Ts9P OEKsYPxBzZaiUuJrX2AaOxo+XszFb/ogK+7JNtHpUzC5G5jHTzWGXBF10+VYGPCnxw 26n49iF10nsQu+WAyfwPDFngPgZ7Iun0lHE6dHJ+7BSdJGzetnlrgsYDPgvGlduDZN /+6DnR4KtWd+9LEWx/DUGb3xxCH4PMze4uoIeg0bBTo0y8UaV1R7mHf6z3JMoBC8Lb 1TUjIpi7g4UkE9rBMOayxnrgbJ2y30Nvx277atWqIKOiHMEYXkiW+HeljHl+G7QysD wpt5q4TqwFelw== From: Amitay Isaacs To: pdbg@lists.ozlabs.org Date: Thu, 2 Aug 2018 14:35:05 +1000 Message-Id: <20180802043507.28734-3-amitay@ozlabs.org> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20180802043507.28734-1-amitay@ozlabs.org> References: <20180802043507.28734-1-amitay@ozlabs.org> Subject: [Pdbg] [PATCH 2/4] progress: Refactor progress_bar to print progress bars X-BeenThere: pdbg@lists.ozlabs.org X-Mailman-Version: 2.1.27 Precedence: list List-Id: "mailing list for https://github.com/open-power/pdbg development" List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Amitay Isaacs MIME-Version: 1.0 Errors-To: pdbg-bounces+incoming=patchwork.ozlabs.org@lists.ozlabs.org Sender: "Pdbg" Signed-off-by: Amitay Isaacs --- src/progress.c | 37 ++++++++++++++++++++----------------- 1 file changed, 20 insertions(+), 17 deletions(-) diff --git a/src/progress.c b/src/progress.c index 9772374..8aef486 100644 --- a/src/progress.c +++ b/src/progress.c @@ -29,43 +29,46 @@ static struct timespec progress_start; #define PROGRESS_CHARS 50 -void progress_init(void) +static void progress_bar(unsigned int percent) { - unsigned int i; + unsigned int i, progress; - progress_pcent = 0; - progress_n_upd = ULONG_MAX; - progress_prevsec = ULONG_MAX; + progress = (percent * PROGRESS_CHARS) / 101; fprintf(stderr, "\r["); - for (i = 0; i < PROGRESS_CHARS; i++) + for (i = 0; i <= progress; i++) + fprintf(stderr, "="); + for (; i < PROGRESS_CHARS; i++) fprintf(stderr, " "); - fprintf(stderr, "] 0%%"); + fprintf(stderr, "] %u%%", percent); fflush(stderr); +} + +void progress_init(void) +{ + progress_pcent = 0; + progress_n_upd = ULONG_MAX; + progress_prevsec = ULONG_MAX; + + progress_bar(0); + clock_gettime(CLOCK_MONOTONIC, &progress_start); } void progress_tick(uint64_t cur, uint64_t end) { - unsigned int i, pos; struct timespec now; - uint64_t pcent; + unsigned int pcent; double sec; - pcent = (cur * 100) / end; + pcent = (unsigned int)((cur * 100) / end); if (progress_pcent == pcent && cur < progress_n_upd && cur < end) return; progress_pcent = pcent; - pos = (pcent * PROGRESS_CHARS) / 101; clock_gettime(CLOCK_MONOTONIC, &now); - fprintf(stderr, "\r["); - for (i = 0; i <= pos; i++) - fprintf(stderr, "="); - for (; i < PROGRESS_CHARS; i++) - fprintf(stderr, " "); - fprintf(stderr, "] %" PRIu64 "%%", pcent); + progress_bar(pcent); sec = difftime(now.tv_sec, progress_start.tv_sec); if (sec >= 5 && pcent > 0) {