From patchwork Sun Mar 10 03:26:38 2013 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Vimalkumar X-Patchwork-Id: 226409 X-Patchwork-Delegate: shemminger@vyatta.com Return-Path: X-Original-To: patchwork-incoming@ozlabs.org Delivered-To: patchwork-incoming@ozlabs.org Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by ozlabs.org (Postfix) with ESMTP id 1B0DB2C02BE for ; Sun, 10 Mar 2013 14:26:56 +1100 (EST) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1752007Ab3CJD0w (ORCPT ); Sat, 9 Mar 2013 22:26:52 -0500 Received: from mail-da0-f51.google.com ([209.85.210.51]:46350 "EHLO mail-da0-f51.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1751843Ab3CJD0v (ORCPT ); Sat, 9 Mar 2013 22:26:51 -0500 Received: by mail-da0-f51.google.com with SMTP id z17so450819dal.24 for ; Sat, 09 Mar 2013 19:26:51 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=x-received:from:to:cc:subject:date:message-id:x-mailer; bh=hy4N0vXCVHU5sd6P16YrMQX8QeJFFUVg+Q/GdjZPVlk=; b=ZzhEGLn05H/6Vk2VcJ6PXreYTJoBw+il92XrYC1VVec+ynsZoywLdsfJPAqZ6PmZTF DrMcrA2cIdquc3oFLvFlvvvXxaNwfRoi+jGtC6axHUd8vdC+0EyU/WCi06XSC1g2s4jE k+Sud7el5TdL7N63U5f4tvp6Fx69sMYVES0HhSyNbZuw5ofh/9yDNv8BjFT8pjcV+9dC ZKfGFZNmisP+ADAPpnFgNoCPpxtObl40C+SBzqwnxJJq6gF4pkmkdlWgAtcLixYmigSg 9NcxTxEusrDTdMb2PX0dTGj47izyM8K+HAR4n6kUQ6gYvx1pj4AAJlw6gzsQh6hMkdXl Q6lg== X-Received: by 10.68.231.42 with SMTP id td10mr16432950pbc.174.1362886011315; Sat, 09 Mar 2013 19:26:51 -0800 (PST) Received: from localhost.localdomain ([128.12.216.95]) by mx.google.com with ESMTPS id xr3sm13173367pbc.46.2013.03.09.19.26.49 (version=TLSv1 cipher=RC4-SHA bits=128/128); Sat, 09 Mar 2013 19:26:49 -0800 (PST) From: Vimalkumar To: netdev@vger.kernel.org, shemminger@vyatta.com, eric.dumazet@gmail.com Cc: Vimalkumar Subject: [PATCH] print_rate: use knowledge in rate_suffix table for human readable rates. Date: Sat, 9 Mar 2013 19:26:38 -0800 Message-Id: <1362885998-14292-1-git-send-email-j.vimal@gmail.com> X-Mailer: git-send-email 1.7.5.4 Sender: netdev-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: netdev@vger.kernel.org Signed-off-by: Vimalkumar --- tc/tc_util.c | 26 ++++++++++++-------------- 1 files changed, 12 insertions(+), 14 deletions(-) diff --git a/tc/tc_util.c b/tc/tc_util.c index 0939536..6e68d87 100644 --- a/tc/tc_util.c +++ b/tc/tc_util.c @@ -139,7 +139,8 @@ static const struct rate_suffix { { "GBps", 8000000000. }, { "TiBps", 8.*1024.*1024.*1024.*1024. }, { "TBps", 8000000000000. }, - { NULL } + { NULL }, + { NULL }, }; @@ -171,21 +172,18 @@ void print_rate(char *buf, int len, __u64 rate) { double tmp = (double)rate*8; extern int use_iec; + int start = 0; + const struct rate_suffix *s; if (use_iec) { - if (tmp >= 1000.0*1024.0*1024.0) - snprintf(buf, len, "%.0fMibit", tmp/(1024.0*1024.0)); - else if (tmp >= 1000.0*1024) - snprintf(buf, len, "%.0fKibit", tmp/1024); - else - snprintf(buf, len, "%.0fbit", tmp); - } else { - if (tmp >= 1000.0*1000000.0) - snprintf(buf, len, "%.0fMbit", tmp/1000000.0); - else if (tmp >= 1000.0 * 1000.0) - snprintf(buf, len, "%.0fKbit", tmp/1000.0); - else - snprintf(buf, len, "%.0fbit", tmp); + start = 1; + } + + snprintf(buf, len, "%.0fbit", tmp); + for (s = &suffixes[start]; s->name; s += 2) { + if (tmp >= s->scale) { + snprintf(buf, len, "%.3f%s", tmp / s->scale, s->name); + } } }