From nobody@FreeBSD.org  Mon Aug 17 09:45:39 2009
Return-Path: <nobody@FreeBSD.org>
Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2001:4f8:fff6::34])
	by hub.freebsd.org (Postfix) with ESMTP id 1780B106568E
	for <freebsd-gnats-submit@FreeBSD.org>; Mon, 17 Aug 2009 09:45:39 +0000 (UTC)
	(envelope-from nobody@FreeBSD.org)
Received: from www.freebsd.org (www.freebsd.org [IPv6:2001:4f8:fff6::21])
	by mx1.freebsd.org (Postfix) with ESMTP id E0ABA8FC16
	for <freebsd-gnats-submit@FreeBSD.org>; Mon, 17 Aug 2009 09:45:38 +0000 (UTC)
Received: from www.freebsd.org (localhost [127.0.0.1])
	by www.freebsd.org (8.14.3/8.14.3) with ESMTP id n7H9jcqM083456
	for <freebsd-gnats-submit@FreeBSD.org>; Mon, 17 Aug 2009 09:45:38 GMT
	(envelope-from nobody@www.freebsd.org)
Received: (from nobody@localhost)
	by www.freebsd.org (8.14.3/8.14.3/Submit) id n7H9jc7F083455;
	Mon, 17 Aug 2009 09:45:38 GMT
	(envelope-from nobody)
Message-Id: <200908170945.n7H9jc7F083455@www.freebsd.org>
Date: Mon, 17 Aug 2009 09:45:38 GMT
From: Peter Vereshagin <peter@vereshagin.org>
To: freebsd-gnats-submit@FreeBSD.org
Subject: cc --fast-math leads to nan% CPU consumption in ps and top
X-Send-Pr-Version: www-3.1
X-GNATS-Notify:

>Number:         137869
>Category:       bin
>Synopsis:       cc --fast-math leads to nan% CPU consumption in ps and top
>Confidential:   no
>Severity:       non-critical
>Priority:       low
>Responsible:    gavin
>State:          closed
>Quarter:        
>Keywords:       
>Date-Required:  
>Class:          sw-bug
>Submitter-Id:   current-users
>Arrival-Date:   Mon Aug 17 09:50:04 UTC 2009
>Closed-Date:    Wed Aug 19 13:54:19 UTC 2009
>Last-Modified:  Wed Aug 19 13:54:19 UTC 2009
>Originator:     Peter Vereshagin
>Release:        RELENG_7_2 as of early August, 2009
>Organization:
Private
>Environment:
FreeBSD somehost 7.2-RELEASE-p3 FreeBSD 7.2-RELEASE-p3 #2: Mon Aug  3 12:36:59 MSD 2009     root@somehost:/var/src/sys/i386/compile/MKERN  i386
>Description:
I built and installed world and kernel with this optimisations in my make.conf:
===
CPUTYPE=pentium4 
CFLAGS=-O2 -pipe -funit-at-a-time -fpeel-loops -ftracer -funswitch-loops -mmmx -msse -msse2 -march=pentium4 -mtune=pentium4 -ffast-math 
CFLAGS+=-g
COPTFLAGS=-O2 -pipe  -funit-at-a-time -fpeel-loops -ftracer -funswitch-loops -mmmx -msse -msse2 -march=pentium4 -mtune=pentium4 -ffast-math 
CXXFLAGS+=-fconserve-space
===
Every little thing was great till I noticed the top and the ps -u show the nan% in the CPU% column.
The workaround was to disable -ffast-math in make.conf and
===
cd /usr/src/usr.bin/top
make clean all install
cd /usr/src/bin/ps
make clean all install
===
Indeed I disabled all the optimizations first and rebuilt-reinstalled world first. Then on portupgrade I noticed few of my ports failed to compile with -ffast-math, at least the postgresql-client.
Thus I rebuilt and reinstalled  my world and kernel with every of those optimizations turned on and recompiled top and ps only without fast-math.
So this works fine now but I guess it should be better to do something with ps and top in particular, because the whole rest of the world is quite patient to the fast-math.
>How-To-Repeat:
compile top and ps with -ffast-math option for the gcc.
>Fix:
There should be warnings on the buildworld process about -ffast-math if it is not compatible with it, so world either should work with this gcc feature or shouldn't at all. It obfuscates a lot about where the trouble is, it's possible to be located in the kernel, the debugging of the kernel is not appropriate for production use, where gcc optimizations are high demand.

>Release-Note:
>Audit-Trail:
State-Changed-From-To: open->feedback 
State-Changed-By: gavin 
State-Changed-When: Mon Aug 17 11:28:34 UTC 2009 
State-Changed-Why:  
To submitter: Looking at the man page for gcc suggests that -ffast-math enables 
-fno-math-errno, -funsafe-math-optimizations, -fno-trapping-math, 
-ffinite-math-only, -fno-rounding-math, -fno-signaling-nans and fcx-limited-range. 

Perhaps you could spend some time and figure out which of these options are 
breaking top and ps? 

Although, the man page also says about -ffast-math: 
.  This option [...] can result in incorrect output for programs which depend on 
.  an exact implementation of IEEE or ISO rules/specifications for math functions. 
so it may well just because top and ps depend on the math functions being 
accurate. 

Lastly, compiling with unusual optimisation settings isn't supported, so it 
may be that this bug isn't fixed unless there is a real underlying problem. 


Responsible-Changed-From-To: freebsd-bugs->gavin 
Responsible-Changed-By: gavin 
Responsible-Changed-When: Mon Aug 17 11:28:34 UTC 2009 
Responsible-Changed-Why:  
Track 

http://www.freebsd.org/cgi/query-pr.cgi?pr=137869 

From: David Schultz <das@FreeBSD.ORG>
To: bug-followup@FreeBSD.ORG, gavin@FreeBSD.ORG
Cc: peter@vereshagin.org
Subject: Re: bin/137869: cc --fast-math leads to nan% CPU consumption in ps and top
Date: Mon, 17 Aug 2009 18:04:38 -0400

 The gcc documentation for -ffast-math warns that it enables fast
 but sometimes incorrect optimizations, which break the standard
 behavior that many applications depend upon.  I'm unsure of the
 best way to make this admonition more clear; there are many other
 flags and settings that you can use to shoot yourself in the foot;
 it's not just -ffast-math.

From: Bruce Evans <brde@optusnet.com.au>
To: Peter Vereshagin <peter@vereshagin.org>
Cc: gavin@FreeBSD.org, bug-followup@FreeBSD.org
Subject: Re: bin/137869: cc --fast-math leads to nan% CPU consumption in ps
 and top
Date: Tue, 18 Aug 2009 18:41:03 +1000 (EST)

 On Mon, 17 Aug 2009, Peter Vereshagin wrote:
 
 > We all come down to Monterey, gavin!
 >
 > Those are the minimal gcc-related settings I can repeat this now:
 > ===
 > CPUTYPE=pentium4
 > CFLAGS=-funsafe-math-optimizations -fno-math-errno
 > ===
 
 These are unsupported.
 
 > I know these features are unstable but every other thing in the buildworld works with them and almost everything in the ports, too.
 
 They are in fact unusable and useless, respectively.
 
 Almost everything in libm depends on IEEE features which are broken
 by -ffast-math.  gcc doesn't really understand how to make "math" fast,
 so -ffast-math actually slows down parts of libm (mainly by replacing
 fast accurate library functions by slow inline i387 hardware functions).
 However, I guess on average it speeds up libm, and the speedup is
 precisely in cases that it breaks libm.  The breakage is mainly in
 unusual cases which might not actually occur.  It is very hard to test
 all the cases or otherwise guarantee that the error cases don't occur.
 
 -fno-math-errno is useless since it is the default in FreeBSD.
 
 > So I'd wish a warning to be while buildworld process that this particular feature is that unstable and hidden danger is to be met on that way. Because I can prove there was someone else who was in the same exact trouble before I got it. (and was lazy to file a PR ;-)
 
 Please don't format mail for 270-column terminals.
 
 libm could abort if compiled with -ffast-math.  Since it is hard to very
 CFLAGS during makeworld, this would prevent most uses of -ffast-math.
 
 >
 > 2009/08/17 11:37:34 +0000 gavin@FreeBSD.org => To peter@vereshagin.org :
 >> Perhaps you could spend some time and figure out which of these options are
 >> breaking top and ps?
 >>
 >> Although, the man page also says about -ffast-math:
 >> .  This option [...] can result in incorrect output for programs which depend on
 
 s/can result in/usually results in/
 
 >> .  an exact implementation of IEEE or ISO rules/specifications for math functions.
 
 s/math functions/floating point operations/
 
 All FP operations are affected, and functions in libm or in other libraries
 are only specially affected insofar as they actually depend on IEEE.
 
 >> so it may well just because top and ps depend on the math functions being
 >> accurate.
 
 It's actually suprising that top and ps are noticeably affected.  Both only
 use FP for a few simple scaling operations.
 
 Bruce

From: Peter Vereshagin <peter@vereshagin.org>
To: David Schultz <das@FreeBSD.ORG>
Cc: bug-followup@FreeBSD.ORG, gavin@FreeBSD.ORG, peter@vereshagin.org
Subject: Re: bin/137869: cc --fast-math leads to nan% CPU consumption in ps
	and top
Date: Wed, 19 Aug 2009 12:11:36 +0500

 You are right, let's forget this all, thanks a lot.
 
 2009/08/17 18:04:38 -0400 David Schultz <das@FreeBSD.ORG> => To bug-followup@FreeBSD.ORG :
 DS> best way to make this admonition more clear; there are many other
 DS> it's not just -ffast-math.
 
 73! Peter
 -- 
State-Changed-From-To: feedback->closed 
State-Changed-By: gavin 
State-Changed-When: Wed Aug 19 13:52:09 UTC 2009 
State-Changed-Why:  
Submitter is happy for this to be closed.  -fast-math is not supported and is 
known to cause problems like this with applications that expect the math 
functions to work correctly.  There will also be other subtle side-effects, 
for example it is documented that ntpdate(8) and ntpd(8) also won't work. 

http://www.freebsd.org/cgi/query-pr.cgi?pr=137869 
>Unformatted:
