Netstat is SLOW, 4.2BSD, Easy FIX

dpk at BRL-VGR.ARPA dpk at BRL-VGR.ARPA
Wed Jul 4 12:32:53 AEST 1984


From:      Doug Kingston <dpk at BRL-VGR.ARPA>

Index:	ucb/netstat/inet.c 4.2BSD FIX

Description:
	Depressed at how slow netstat was on the VAX after hashing
the host tables, I profiled netstat and found that it was spending
90 percent of its time in inetname() and most of that in getnetent().
A look at the code revealed that it was ALWAYS trying to look up the
net name for a net even when that net was INADDR_ANY (all zeros).
After looking this up (and failing, since that is not a valid net),
it checked for that case and copied "*" into a buffer and returned.

Repeat-By:
	Run "time netstat -a".  Sit and wait.  Look at the large CPU time.

Fix:
	The fix is to move the check for INADDR_ANY to the start of the
inetname() function to catch this trivial case and prevent any expensive
and fruitless lookups. A diff listing follows, although the line numbers
will be offset due to a header we add to the file.  The fix is simple.

312a316,319
> 	if (in.s_addr == INADDR_ANY) {	/* A quick check for the easy case */
> 		strcpy(line, "*");
> 		return (line);
> 	}
329,331c336
< 	if (in.s_addr == INADDR_ANY)
< 		strcpy(line, "*");
< 	else if (cp)
---
> 	if (cp)

			Yours for a better 4.2BSD,
					-Doug-

					Douglas P. Kingston III
					Advanced Computer Systems Team
					Ballistics Research Lab
					Attn: DRSMC-BLV-V (A)
					APG, Md. 21005

					(301) 278-6651



More information about the Comp.unix.wizards mailing list