all messages for Emacs-related lists mirrored at yhetil.org
 help / color / mirror / code / Atom feed
* portable implementation of load-average
@ 2004-09-22 12:34 Joe Buehler
  2004-09-22 13:37 ` Stefan Monnier
  0 siblings, 1 reply; 3+ messages in thread
From: Joe Buehler @ 2004-09-22 12:34 UTC (permalink / raw


My AIX 4.3 boxes did not support load average in the modeline
(emacs 21.2 if it matters) so I fixed it.

Looking at this, it appears to be because /dev/kmem needs to be read
for the information, and setting emacs setgid to group 0 would be a big
security hole.

Attached is a portable implementation in LISP that uses the
"uptime" program.  Please consider adding this to emacs.  You should
have a copyright assignment on file for me for emacs.

The "uptime" program provides compatible output
formats (and so the below works) on AIX 4.3, AIX 5.2, Solaris 8,
HPUX 11, HPUX 11i, RedHat 9 -- all the machines to which I have access.

I'm not a LISP programmer -- feel free to do whatever with
my implementation if you don't like it.
-- 
Joe Buehler

;; supply a portable definition of load-average if the emacs one does not work
(condition-case nil
	(load-average)
   (error
    (defun load-average (&optional wantfloat)
	 "get load average"
	 (let ((uptime (shell-command-to-string "/usr/bin/uptime")))
	   (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)
		   )
		 )
	   )
	 )
    ))

;;;;;; the end

^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: portable implementation of load-average
  2004-09-22 12:34 portable implementation of load-average Joe Buehler
@ 2004-09-22 13:37 ` Stefan Monnier
  2004-09-22 13:59   ` Joe Buehler
  0 siblings, 1 reply; 3+ messages in thread
From: Stefan Monnier @ 2004-09-22 13:37 UTC (permalink / raw
  Cc: emacs-devel

> 	 (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

^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: portable implementation of load-average
  2004-09-22 13:37 ` Stefan Monnier
@ 2004-09-22 13:59   ` Joe Buehler
  0 siblings, 0 replies; 3+ messages in thread
From: Joe Buehler @ 2004-09-22 13:59 UTC (permalink / raw


Stefan Monnier wrote:

> 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).

Like I said, I'm not a LISP programmer -- it's all yours, feel free to
change what you want.

The only way I currently use it is as part of (display-time), so I don't
need any caching myself -- it runs once a minute as is.

> 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)?

It is not setgid for linux and solaris, it is setgid for the others.  Whether emacs
works as is for linux and solaris I don't know, but I presume it does not
work for the other OS's -- definitely not for AIX 4.3.
-- 
Joe Buehler

^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2004-09-22 13:59 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2004-09-22 12:34 portable implementation of load-average Joe Buehler
2004-09-22 13:37 ` Stefan Monnier
2004-09-22 13:59   ` Joe Buehler

Code repositories for project(s) associated with this external index

	https://git.savannah.gnu.org/cgit/emacs.git
	https://git.savannah.gnu.org/cgit/emacs/org-mode.git

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.