0.pre8.4
[sbcl.git] / src / code / gc.lisp
index 5ed0b35..2389750 100644 (file)
 
 #!-sb-fluid
 (declaim (inline dynamic-usage)) ; to reduce PROFILEd call overhead
-#!+(or cgc gencgc)
+#!+gencgc
 (def-c-var-frob dynamic-usage "bytes_allocated")
-#!-(or cgc gencgc)
+#!-gencgc
 (defun dynamic-usage ()
   (the (unsigned-byte 32)
        (- (sb!sys:sap-int (sb!c::dynamic-space-free-pointer))
           (current-dynamic-space-start))))
 
 (defun static-space-usage ()
-  (- (* sb!vm:*static-space-free-pointer* sb!vm:word-bytes)
+  (- (* sb!vm:*static-space-free-pointer* sb!vm:n-word-bytes)
      sb!vm:static-space-start))
 
 (defun read-only-space-usage ()
-  (- (* sb!vm::*read-only-space-free-pointer* sb!vm:word-bytes)
+  (- (* sb!vm::*read-only-space-free-pointer* sb!vm:n-word-bytes)
      sb!vm:read-only-space-start))
 
 (defun control-stack-usage ()
-  #!-x86 (- (sb!sys:sap-int (sb!c::control-stack-pointer-sap))
-            sb!vm:control-stack-start)
-  #!+x86 (- sb!vm:control-stack-end
-           (sb!sys:sap-int (sb!c::control-stack-pointer-sap))))
+  #!-stack-grows-downward-not-upward
+  (- (sb!sys:sap-int (sb!c::control-stack-pointer-sap))
+     sb!vm:control-stack-start)
+  #!+stack-grows-downward-not-upward
+  (- sb!vm:control-stack-end
+     (sb!sys:sap-int (sb!c::control-stack-pointer-sap))))
 
 (defun binding-stack-usage ()
   (- (sb!sys:sap-int (sb!c::binding-stack-pointer-sap))
   (format t "Control stack usage is:   ~10:D bytes.~%" (control-stack-usage))
   (format t "Binding stack usage is:   ~10:D bytes.~%" (binding-stack-usage))
   (format t "Garbage collection is currently ~:[enabled~;DISABLED~].~%"
-         *gc-inhibit*))
+         (> *gc-inhibit* 0)))
 
 (defun room-intermediate-info ()
   (room-minimal-info)
   (sb!vm:memory-usage :count-spaces '(:dynamic)
                      :print-spaces t
-                     :cutoff 0.05s0
+                     :cutoff 0.05f0
                      :print-summary nil))
 
 (defun room-maximal-info ()
@@ -140,7 +142,14 @@ and submit it as a patch."
 ;;;
 ;;; Unlike CMU CL, we don't export this variable. (There's no need to,
 ;;; since our BYTES-CONSED-BETWEEN-GCS function is SETFable.)
-(defvar *bytes-consed-between-gcs* (* 2 (expt 10 6)))
+(defvar *bytes-consed-between-gcs*
+  #!+gencgc (* 4 (expt 10 6))
+  ;; Stop-and-copy GC is really really slow when used too often. CSR
+  ;; reported that even on his old 64 Mb SPARC, 20 Mb is much faster
+  ;; than 4 Mb when rebuilding SBCL ca. 0.7.1. For modern machines
+  ;; with >> 128 Mb memory, the optimum could be significantly more
+  ;; than this, but at least 20 Mb should be better than 4 Mb.
+  #!-gencgc (* 20 (expt 10 6)))
 (declaim (type index *bytes-consed-between-gcs*))
 
 ;;;; GC hooks
@@ -170,27 +179,51 @@ and submit it as a patch."
 
 ;;; a limit to help catch programs which allocate too much memory,
 ;;; since a hard heap overflow is so hard to recover from
+;;;
+;;; FIXME: Like *GC-TRIGGER*, this variable (1) should probably be
+;;; denominated in a larger unit than bytes and (2) should probably be
+;;; renamed so that it's clear from the name what unit it's
+;;; denominated in.
 (declaim (type (or unsigned-byte null) *soft-heap-limit*))
-(defvar *soft-heap-limit* nil)
+(defvar *soft-heap-limit*
+  ;; As long as *GC-TRIGGER* is DECLAIMed as INDEX, we know that
+  ;; MOST-POSITIVE-FIXNUM is a hard limit on how much memory can be
+  ;; allocated. (Not necessarily *the* hard limit, which is fairly
+  ;; likely something like a Unix per-process limit that we don't know
+  ;; about, but a hard limit anyway.) And this gives us a reasonable
+  ;; conservative default for the soft limit...
+  (- most-positive-fixnum
+     *bytes-consed-between-gcs*))
+
+;;;; The following specials are used to control when garbage
+;;;; collection occurs.
 
 ;;; When the dynamic usage increases beyond this amount, the system
 ;;; notes that a garbage collection needs to occur by setting
 ;;; *NEED-TO-COLLECT-GARBAGE* to T. It starts out as NIL meaning
 ;;; nobody has figured out what it should be yet.
-(defvar *gc-trigger* nil)
-
+;;;
+;;; FIXME: *GC-TRIGGER* seems to be denominated in bytes, not words.
+;;; And limiting it to INDEX is fairly reasonable in order to avoid
+;;; bignum arithmetic on every allocation, and to minimize the need
+;;; for thought about weird gotchas of the GC-control mechanism itself
+;;; consing as it operates. But as of sbcl-0.7.5, 512Mbytes of memory
+;;; costs $54.95 at Fry's in Dallas but cheap consumer 64-bit machines
+;;; are still over the horizon, so gratuitously limiting our heap size
+;;; to FIXNUM bytes seems fairly stupid. It'd be reasonable to
+;;; (1) allow arbitrary UNSIGNED-BYTE values of *GC-TRIGGER*, or
+;;; (2) redenominate this variable in words instead of bytes, postponing
+;;;     the problem to heaps which exceed 50% of the machine's address
+;;;     space, or even
+;;; (3) redemoninate this variable in CONS-sized two-word units,
+;;;     allowing it to cover the entire memory space at the price of
+;;;     possible loss of clarity.
+;;; (And whatever is done, it'd also be good to rename the variable so
+;;; that it's clear what unit it's denominated in.)
 (declaim (type (or index null) *gc-trigger*))
+(defvar *gc-trigger* nil)
 
-;;; On the X86, we store the GC trigger in a ``static'' symbol instead
-;;; of letting magic C code handle it. It gets initialized by the
-;;; startup code.
-#!+x86
-(defvar sb!vm::*internal-gc-trigger*)
-
-;;;; The following specials are used to control when garbage collection
-;;;; occurs.
-
-;;; When non-NIL, inhibits garbage collection.
+;;; When >0, inhibits garbage collection.
 (defvar *gc-inhibit*) ; initialized in cold init
 
 ;;; This flag is used to prevent recursive entry into the garbage
@@ -203,14 +236,15 @@ and submit it as a patch."
 \f
 (defun default-gc-notify-before (notify-stream bytes-in-use)
   (declare (type stream notify-stream))
-  (format notify-stream
-         "~&; GC is beginning with ~:D bytes in use at internal runtime ~:D.~%"
-         bytes-in-use
-         (get-internal-run-time))
+  (format
+   notify-stream
+   "~&; GC is beginning with ~:D bytes in use at internal runtime ~:D.~%"
+   bytes-in-use
+   (get-internal-run-time))
   (finish-output notify-stream))
 (defparameter *gc-notify-before* #'default-gc-notify-before
   #!+sb-doc
-  "This function bound to this variable is invoked before GC'ing (unless
+  "The function bound to this variable is invoked before GC'ing (unless
   *GC-NOTIFY-STREAM* is NIL) with the value of *GC-NOTIFY-STREAM* and
   current amount of dynamic usage (in bytes). It should notify the
   user that the system is going to GC.")
@@ -230,21 +264,21 @@ and submit it as a patch."
   (finish-output notify-stream))
 (defparameter *gc-notify-after* #'default-gc-notify-after
   #!+sb-doc
-  "The function bound to this variable is invoked after GC'ing with
-the value of *GC-NOTIFY-STREAM*, the amount of dynamic usage (in
-bytes) now free, the number of bytes freed by the GC, and the new GC
-trigger threshold. The function should notify the user that the system
-has finished GC'ing.")
+  "The function bound to this variable is invoked after GC'ing with the
+value of *GC-NOTIFY-STREAM*, the amount of dynamic usage (in bytes) now
+free, the number of bytes freed by the GC, and the new GC trigger
+threshold; or if *GC-NOTIFY-STREAM* is NIL, it's not invoked. The
+function should notify the user that the system has finished GC'ing.")
 \f
 ;;;; internal GC
 
-(sb!alien:def-alien-routine collect-garbage sb!c-call:int
-  #!+gencgc (last-gen sb!c-call:int))
+(sb!alien:define-alien-routine collect-garbage sb!alien:int
+  (#!+gencgc last-gen #!-gencgc ignore sb!alien:int))
 
-(sb!alien:def-alien-routine set-auto-gc-trigger sb!c-call:void
-  (dynamic-usage sb!c-call:unsigned-long))
+(sb!alien:define-alien-routine set-auto-gc-trigger sb!alien:void
+  (dynamic-usage sb!alien:unsigned-long))
 
-(sb!alien:def-alien-routine clear-auto-gc-trigger sb!c-call:void)
+(sb!alien:define-alien-routine clear-auto-gc-trigger sb!alien:void)
 
 ;;; This variable contains the function that does the real GC. This is
 ;;; for low-level GC experimentation. Do not touch it if you do not
@@ -253,7 +287,7 @@ has finished GC'ing.")
 \f
 ;;;; SUB-GC
 
-;;; Used to carefully invoke hooks.
+;;; This is used to carefully invoke hooks.
 (eval-when (:compile-toplevel :execute)
   (sb!xc:defmacro carefully-funcall (function &rest args)
     `(handler-case (funcall ,function ,@args)
@@ -266,6 +300,13 @@ has finished GC'ing.")
 ;;; is not greater than *GC-TRIGGER*.
 ;;;
 ;;; For GENCGC all generations < GEN will be GC'ed.
+
+;;; XXX need (1) some kind of locking to ensure that only one thread
+;;; at a time is trying to GC, (2) to look at all these specials and
+;;; work out how much of this "do we really need to GC now?" stuff is
+;;; actually necessary: I think we actually end up GCing every time we
+;;; hit this code
+
 (defun sub-gc (&key force-p (gen 0))
   (/show0 "entering SUB-GC")
   (unless *already-maybe-gcing*
@@ -284,12 +325,12 @@ has finished GC'ing.")
                                  *soft-heap-limit*)))
       (when soft-heap-limit-exceeded?
        (cerror "Continue with GC."
-               "soft heap limit exceeded (temporary new limit=~D)"
+               "soft heap limit exceeded (temporary new limit=~W)"
                *soft-heap-limit*))
       (when (and *gc-trigger* (> pre-gc-dynamic-usage *gc-trigger*))
        (setf *need-to-collect-garbage* t))
       (when (or force-p
-               (and *need-to-collect-garbage* (not *gc-inhibit*)))
+               (and *need-to-collect-garbage* (zerop *gc-inhibit*)))
        ;; KLUDGE: Wow, we really mask interrupts all the time we're
        ;; collecting garbage? That seems like a long time.. -- WHN 19991129
        (without-interrupts
@@ -315,17 +356,7 @@ has finished GC'ing.")
                   ;; triggered GC could've done a fair amount of
                   ;; consing.)
                   (pre-internal-gc-dynamic-usage (dynamic-usage))
-                  (ignore-me
-                   #!-gencgc (funcall *internal-gc*)
-                   ;; FIXME: This EQ test is pretty gross. Among its other
-                   ;; nastinesses, it looks as though it could break if we
-                   ;; recompile COLLECT-GARBAGE. We should probably just
-                   ;; straighten out the interface so that all *INTERNAL-GC*
-                   ;; functions accept a GEN argument (and then the
-                   ;; non-generational ones just ignore it).
-                   #!+gencgc (if (eq *internal-gc* #'collect-garbage)
-                                 (funcall *internal-gc* gen)
-                                 (funcall *internal-gc*)))
+                  (ignore-me (funcall *internal-gc* gen))
                   (post-gc-dynamic-usage (dynamic-usage))
                   (n-bytes-freed (- pre-internal-gc-dynamic-usage
                                     post-gc-dynamic-usage))
@@ -428,7 +459,7 @@ has finished GC'ing.")
 (defun gc-on ()
   #!+sb-doc
   "Enable the garbage collector."
-  (setq *gc-inhibit* nil)
+  (setq *gc-inhibit* 0)
   (when *need-to-collect-garbage*
     (sub-gc))
   nil)
@@ -436,7 +467,7 @@ has finished GC'ing.")
 (defun gc-off ()
   #!+sb-doc
   "Disable the garbage collector."
-  (setq *gc-inhibit* t)
+  (setq *gc-inhibit* 1)
   nil)
 \f
 ;;;; initialization stuff