Skip site navigation (1)Skip section navigation (2)
Date:      Fri, 1 Dec 2006 09:09:28 GMT
From:      Rostislav Krasny<rosti.bsd@gmail.com>
To:        freebsd-gnats-submit@FreeBSD.org
Subject:   bin/106116: [PATCH] wrong definition of kilobit per second in systat(1)
Message-ID:  <200612010909.kB199Su5019349@www.freebsd.org>
Resent-Message-ID: <200612010910.kB19AIti020481@freefall.freebsd.org>

next in thread | raw e-mail | index | archive | help

>Number:         106116
>Category:       bin
>Synopsis:       [PATCH] wrong definition of kilobit per second in systat(1)
>Confidential:   no
>Severity:       non-critical
>Priority:       low
>Responsible:    freebsd-bugs
>State:          open
>Quarter:        
>Keywords:       
>Date-Required:
>Class:          sw-bug
>Submitter-Id:   current-users
>Arrival-Date:   Fri Dec 01 09:10:18 GMT 2006
>Closed-Date:
>Last-Modified:
>Originator:     Rostislav Krasny
>Release:        
>Organization:
>Environment:
>Description:
Revision 1.11 of src/usr.bin/systat/convtbl.c has following definitions:

#define BIT	(8)
#define BYTE	(1)

#define BITS	(1)
#define BYTES	(1)
#define KILO	(1024LL)
#define MEGA	(KILO * 1024)
#define GIGA	(MEGA * 1024)
#define TERA	(GIGA * 1024)

static struct convtbl convtbl[] = {
	/* mul, scale, str, name */
	[SC_BYTE] =	{ BYTE, BYTES, "B",  "byte"  },
	[SC_KILOBYTE] =	{ BYTE, KILO,  "KB", "kbyte" },
	[SC_MEGABYTE] =	{ BYTE, MEGA,  "MB", "mbyte" },
	[SC_GIGABYTE] =	{ BYTE, GIGA,  "GB", "gbyte" },
	[SC_TERABYTE] =	{ BYTE, TERA,  "TB", "tbyte" },

	[SC_BIT] =	{ BIT, BITS, "b",  "bit"  },
	[SC_KILOBIT] =	{ BIT, KILO, "Kb", "kbit" },
	[SC_MEGABIT] =	{ BIT, MEGA, "Mb", "mbit" },
	[SC_GIGABIT] =	{ BIT, GIGA, "Gb", "gbit" },
	[SC_TERABIT] =	{ BIT, TERA, "Tb", "tbit" },

	[SC_AUTO] =	{ 0, 0, "", "auto" }
};

As you can see it defines kilobit as 1024 bits, megabit as 1024 kilobits and so on. But this is wrong. Unlike prefixes of bytes, prefixes of bits are powers of 10 and not powers of 2. For example 1.5 Mbps means 1500000 bps and not 1572864 bps.

For more information see this http://physics.nist.gov/cuu/Units/binary.html

You can also find following definitions on src/sys/net/if.h

#define	IF_Kbps(x)	((x) * 1000)		/* kilobits/sec. */
#define	IF_Mbps(x)	(IF_Kbps((x) * 1000))	/* megabits/sec. */
#define	IF_Gbps(x)	(IF_Mbps((x) * 1000))	/* gigabits/sec. */

They were added by Sam Leffler in revision 1.81 as an adoption from NetBSD. These definitions exist in FreeBSD, NetBSD, OpenBSD and DragonflyBSD. So a current version of the systat(1) util is inconsistent to any BSD kernel.

In a Linux camp a kilobit is also defined as a power of 10:
http://www.lpi.org/en/lpi/english/certification/the_lpic_program/glossary_of_terms#K
>How-To-Repeat:
Just check the code.
>Fix:
--- convtbl.c.orig	Tue Nov 28 11:58:37 2006
+++ convtbl.c	Tue Nov 28 12:09:25 2006
@@ -37,11 +37,16 @@
 #define BYTE	(1)
 
 #define BITS	(1)
+#define KILOBIT	(1000LL)
+#define MEGABIT	(KILOBIT * KILOBIT)
+#define GIGABIT	(MEGABIT * KILOBIT)
+#define TERABIT	(GIGABIT * KILOBIT)
+
 #define BYTES	(1)
 #define KILO	(1024LL)
-#define MEGA	(KILO * 1024)
-#define GIGA	(MEGA * 1024)
-#define TERA	(GIGA * 1024)
+#define MEGA	(KILO * KILO)
+#define GIGA	(MEGA * KILO)
+#define TERA	(GIGA * KILO)
 
 struct convtbl {
 	uintmax_t	 mul;
@@ -58,11 +63,11 @@
 	[SC_GIGABYTE] =	{ BYTE, GIGA,  "GB", "gbyte" },
 	[SC_TERABYTE] =	{ BYTE, TERA,  "TB", "tbyte" },
 
-	[SC_BIT] =	{ BIT, BITS, "b",  "bit"  },
-	[SC_KILOBIT] =	{ BIT, KILO, "Kb", "kbit" },
-	[SC_MEGABIT] =	{ BIT, MEGA, "Mb", "mbit" },
-	[SC_GIGABIT] =	{ BIT, GIGA, "Gb", "gbit" },
-	[SC_TERABIT] =	{ BIT, TERA, "Tb", "tbit" },
+	[SC_BIT] =	{ BIT, BITS,	"b",  "bit"  },
+	[SC_KILOBIT] =	{ BIT, KILOBIT, "Kb", "kbit" },
+	[SC_MEGABIT] =	{ BIT, MEGABIT, "Mb", "mbit" },
+	[SC_GIGABIT] =	{ BIT, GIGABIT, "Gb", "gbit" },
+	[SC_TERABIT] =	{ BIT, TERABIT, "Tb", "tbit" },
 
 	[SC_AUTO] =	{ 0, 0, "", "auto" }
 };

>Release-Note:
>Audit-Trail:
>Unformatted:



Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?200612010909.kB199Su5019349>