[Top][All Lists]
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: portable implementation of load-average
From: |
Stefan Monnier |
Subject: |
Re: portable implementation of load-average |
Date: |
Wed, 22 Sep 2004 09:37:54 -0400 |
User-agent: |
Gnus/5.1006 (Gnus v5.10.6) Emacs/21.3.50 (gnu/linux) |
> (let ((uptime (shell-command-to-string "/usr/bin/uptime")))
You probably want to use `call-process' here instead, so as to avoid running
`sh' (running `uptime' is already making your `load-average' much slower
than the C version, so I think it's important to try and limit the inflicted
pain).
Maybe caching, so as to only run `uptime' at most once every minute or so,
would be in order.
BTW, is /usr/bin/uptime setgid 0 on those systems, or does it use some other
method to get the info (a method that Emacs could maybe use)?
> (if (string-match "load average: \\([0-9.]+\\), \\([0-9.]+\\),
> \\([0-9.]+\\)" uptime)
> (if wantfloat
> (list
> (string-to-number (match-string 1 uptime))
> (string-to-number (match-string 2 uptime))
> (string-to-number (match-string 3 uptime))
> )
> (list
> (truncate (* 100 (string-to-number (match-string 1
> uptime))))
> (truncate (* 100 (string-to-number (match-string 2
> uptime))))
> (truncate (* 100 (string-to-number (match-string 3
> uptime))))
> )
> )
> (if wantfloat
> (list 0.0 0.0 0.0)
> (list 0 0 0)
> )
> )
How 'bout:
(mapcar (if wantfloat 'identity (lambda (x) (truncate (* 100 x))))
(if (string-match "load average: \\([0-9.]+\\), \\([0-9.]+\\),
\\([0-9.]+\\)" uptime)
(mapcar (lambda (n) (string-to-number (match-string n uptime)))
'(1 2 3))
'(0.0 0.0 0.0)))
-- Stefan