1 ;;;; OS interface functions for SBCL under BSD Unix.
3 ;;;; This code was written as part of the CMU Common Lisp project at
4 ;;;; Carnegie Mellon University, and has been placed in the public
9 ;;;; Check that target machine features are set up consistently with
12 (eval-when (:compile-toplevel
:load-toplevel
:execute
)
13 (error "The :BSD feature is missing, we shouldn't be doing this code."))
15 (define-alien-routine ("sysctl" %sysctl
) int
17 (namelen unsigned-int
)
19 (oldlenp (* sb
!unix
:size-t
))
21 (newlen sb
!unix
:size-t
))
24 (define-alien-routine ("sysctlbyname" %sysctlbyname
) int
27 (oldlenp (* sb
!unix
:size-t
))
29 (newlen sb
!unix
:size-t
))
31 (defun sysctl (type &rest name
)
32 "Retrieves an integer or string value with the given name."
33 (let ((name-len (length name
)))
34 (when (> name-len ctl-maxname
)
35 (error "sysctl name ~S is too long" name
))
36 (with-alien ((name-array (array int
#.ctl-maxname
))
37 (result-len sb
!unix
:size-t
))
38 (dotimes (off name-len
)
39 (setf (deref name-array off
) (elt name off
)))
42 (with-alien ((result int
))
43 (setf result-len
(alien-size int
:bytes
))
44 (unless (minusp (%sysctl
(cast name-array
(* int
)) name-len
45 (addr result
) (addr result-len
) nil
0))
48 (unless (minusp (%sysctl
(cast name-array
(* int
)) name-len
49 nil
(addr result-len
) nil
0))
50 (with-alien ((result (* char
) (make-alien char result-len
)))
51 (if (minusp (%sysctl
(cast name-array
(* int
)) name-len
52 result
(addr result-len
) nil
0))
54 (sb!unix
::newcharstar-string result
)))))))))
57 (defun sysctlbyname (type name
)
58 "Retrieves an integer or string value with the given name."
59 (with-alien ((result-len sb
!unix
:size-t
))
62 (with-alien ((result int
))
63 (setf result-len
(alien-size int
:bytes
))
64 (unless (minusp (%sysctlbyname name
(addr result
)
65 (addr result-len
) nil
0))
68 (unless (minusp (%sysctlbyname name nil
(addr result-len
) nil
0))
69 (with-alien ((result (* char
) (make-alien char result-len
)))
70 (if (minusp (%sysctlbyname name result
(addr result-len
) nil
0))
72 (sb!unix
::newcharstar-string result
))))))))
74 (defun software-type ()
75 "Return a string describing the supporting software."
76 #!-gnu-kfreebsd
(sysctl :str ctl-kern kern-ostype
)
77 #!+gnu-kfreebsd
"GNU/kFreeBSD")
79 (defun software-version ()
80 "Return a string describing version of the supporting software, or NIL
82 (or sb
!sys
::*software-version
*
83 (setf sb
!sys
::*software-version
*
84 (sysctl :str ctl-kern kern-osrelease
))))
86 ;;; Return system time, user time and number of page faults.
87 (defun get-system-info ()
88 (multiple-value-bind (err? utime stime maxrss ixrss idrss
90 (sb!unix
:unix-getrusage sb
!unix
:rusage_self
)
91 (declare (ignore maxrss ixrss idrss isrss minflt
))
93 (simple-perror "Unix system call getrusage() failed" :errno utime
))
95 (values utime stime majflt
)))
97 ;;; Return the system page size.
98 (defun get-page-size ()
99 (sysctl :int ctl-hw hw-pagesize
))
101 ;;; support for CL:MACHINE-VERSION defined OAOO elsewhere
102 (defun get-machine-version ()
103 (or #!+darwin
(sysctlbyname :str
"machdep.cpu.brand_string")
104 (sysctl :str ctl-hw hw-model
)))