[PATCH] Avoid decimal point localization in /proc/loadavg

classic Classic list List threaded Threaded
5 messages Options
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

[PATCH] Avoid decimal point localization in /proc/loadavg

Jon TURNEY
Explicitly format the contents of /proc/loadavg to avoid the decimal point
getting localized according to LC_NUMERIC. Using anything other than '.'
breaks top.

Signed-off-by: Jon Turney <[hidden email]>
---
 winsup/cygwin/fhandler_proc.cc | 11 ++++++++---
 1 file changed, 8 insertions(+), 3 deletions(-)

diff --git a/winsup/cygwin/fhandler_proc.cc b/winsup/cygwin/fhandler_proc.cc
index a7e816f..ad367e4 100644
--- a/winsup/cygwin/fhandler_proc.cc
+++ b/winsup/cygwin/fhandler_proc.cc
@@ -30,6 +30,7 @@ details. */
 #include <wctype.h>
 #include "cpuid.h"
 #include "mount.h"
+#include <math.h>
 
 #define _COMPILING_NEWLIB
 #include <dirent.h>
@@ -432,10 +433,14 @@ format_proc_loadavg (void *, char *&destbuf)
   double loadavg[3] = { 0.0, 0.0, 0.0 };
   getloadavg (loadavg, 3);
 
+#define HUNDRETHS(l) (int)((l - floor(l))*100)
+
   destbuf = (char *) crealloc_abort (destbuf, 48);
-  return sprintf (destbuf, "%.2f %.2f %.2f %u/%u\n",
-  loadavg[0], loadavg[1], loadavg[2], running,
-  (unsigned int)pids.npids);
+  return __small_sprintf (destbuf, "%u.%02u %u.%02u %u.%02u %u/%u\n",
+  (int)loadavg[0], HUNDRETHS(loadavg[0]),
+  (int)loadavg[1], HUNDRETHS(loadavg[1]),
+  (int)loadavg[2], HUNDRETHS(loadavg[2]),
+  running, (unsigned int)pids.npids);
 }
 
 static off_t
--
2.8.3

Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: [PATCH] Avoid decimal point localization in /proc/loadavg

cyg Simple
On 4/8/2017 8:55 AM, Jon Turney wrote:
> Explicitly format the contents of /proc/loadavg to avoid the decimal point
> getting localized according to LC_NUMERIC. Using anything other than '.'
> breaks top.
>

Would it be more prudent to update top to be locale aware?

--
cyg Simple
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: [PATCH] Avoid decimal point localization in /proc/loadavg

Corinna Vinschen-2
In reply to this post by Jon TURNEY
On Apr  8 13:55, Jon Turney wrote:

> Explicitly format the contents of /proc/loadavg to avoid the decimal point
> getting localized according to LC_NUMERIC. Using anything other than '.'
> breaks top.
>
> Signed-off-by: Jon Turney <[hidden email]>
> ---
>  winsup/cygwin/fhandler_proc.cc | 11 ++++++++---
>  1 file changed, 8 insertions(+), 3 deletions(-)
>
> diff --git a/winsup/cygwin/fhandler_proc.cc b/winsup/cygwin/fhandler_proc.cc
> index a7e816f..ad367e4 100644
> --- a/winsup/cygwin/fhandler_proc.cc
> +++ b/winsup/cygwin/fhandler_proc.cc
> @@ -30,6 +30,7 @@ details. */
>  #include <wctype.h>
>  #include "cpuid.h"
>  #include "mount.h"
> +#include <math.h>
>  
>  #define _COMPILING_NEWLIB
>  #include <dirent.h>
> @@ -432,10 +433,14 @@ format_proc_loadavg (void *, char *&destbuf)
>    double loadavg[3] = { 0.0, 0.0, 0.0 };
>    getloadavg (loadavg, 3);
>  
> +#define HUNDRETHS(l) (int)((l - floor(l))*100)
> +
>    destbuf = (char *) crealloc_abort (destbuf, 48);
> -  return sprintf (destbuf, "%.2f %.2f %.2f %u/%u\n",
> -  loadavg[0], loadavg[1], loadavg[2], running,
> -  (unsigned int)pids.npids);
> +  return __small_sprintf (destbuf, "%u.%02u %u.%02u %u.%02u %u/%u\n",
> +  (int)loadavg[0], HUNDRETHS(loadavg[0]),
> +  (int)loadavg[1], HUNDRETHS(loadavg[1]),
> +  (int)loadavg[2], HUNDRETHS(loadavg[2]),
> +  running, (unsigned int)pids.npids);
>  }
>  
>  static off_t
> --
> 2.8.3
Looks good.


Thanks,
Corinna

--
Corinna Vinschen                  Please, send mails regarding Cygwin to
Cygwin Maintainer                 cygwin AT cygwin DOT com
Red Hat

signature.asc (836 bytes) Download Attachment
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: [PATCH] Avoid decimal point localization in /proc/loadavg

Corinna Vinschen-2
In reply to this post by cyg Simple
On Apr  8 12:18, cyg Simple wrote:
> On 4/8/2017 8:55 AM, Jon Turney wrote:
> > Explicitly format the contents of /proc/loadavg to avoid the decimal point
> > getting localized according to LC_NUMERIC. Using anything other than '.'
> > breaks top.
> >
>
> Would it be more prudent to update top to be locale aware?

Not that it hurts to tweak top, but the linux kernel prints this with
decimal points independently of the locale of the current user, too.  


Corinna

--
Corinna Vinschen                  Please, send mails regarding Cygwin to
Cygwin Maintainer                 cygwin AT cygwin DOT com
Red Hat

signature.asc (836 bytes) Download Attachment
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: [PATCH] Avoid decimal point localization in /proc/loadavg

Brian Inglis
In reply to this post by Jon TURNEY
Also affects uptime - and anything else using /proc/loadavg

https://cygwin.com/ml/cygwin/2017-05/msg00190.html

Subject: $ uptime: bad data in /proc/loadavg
On 2017-05-12 20:44, Andrey Repin wrote:
> Greetings, All!
> Just a few days ago it worked, but now
> $ uptime
> bad data in /proc/loadavg
> $ cat /proc/loadavg
> 0,00 0,00 0,00 1/3
> It reporting approx the same from under elevated shell.

--
Take care. Thanks, Brian Inglis, Calgary, Alberta, Canada
Loading...