X-Git-Url: http://repo.macrolet.net/gitweb/?a=blobdiff_plain;f=src%2Fcode%2Fgc.lisp;h=8bf7ee397b9e267f74eebe73d76adc4af608511b;hb=86210c4e406c1b2ff10cc3bac0e71435867db48b;hp=8949bd50aa95293e37d610ca13c5ed7b80c0a400;hpb=4a466c4908db0f6f5c468ae0eabb500ffac07aba;p=sbcl.git diff --git a/src/code/gc.lisp b/src/code/gc.lisp index 8949bd5..8bf7ee3 100644 --- a/src/code/gc.lisp +++ b/src/code/gc.lisp @@ -23,33 +23,39 @@ (defun ,lisp-fun () (sb!alien:extern-alien ,c-var-name (sb!alien:unsigned 32)))))) -#!+(or cgc gencgc) (progn -#!-sb-fluid (declaim (inline dynamic-usage)) -(def-c-var-frob dynamic-usage "bytes_allocated")) - +#!-gencgc +(progn + ;; This is called once per PROFILEd function call, so it's worth a + ;; little possible space cost to reduce its time cost. + #!-sb-fluid + (declaim (inline current-dynamic-space-start)) + (def-c-var-frob current-dynamic-space-start "current_dynamic_space")) + +#!-sb-fluid +(declaim (inline dynamic-usage)) ; to reduce PROFILEd call overhead +#!+(or cgc gencgc) +(def-c-var-frob dynamic-usage "bytes_allocated") #!-(or cgc gencgc) (defun dynamic-usage () (the (unsigned-byte 32) (- (sb!sys:sap-int (sb!c::dynamic-space-free-pointer)) (current-dynamic-space-start)))) -#!-gencgc (progn -#!-sb-fluid (declaim (inline current-dynamic-space-start)) -(def-c-var-frob current-dynamic-space-start "current_dynamic_space")) - (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)) @@ -105,8 +111,17 @@ ;;; (We save this so that we can calculate the total number of bytes ;;; ever allocated by adding this to the number of bytes currently ;;; allocated and never freed.) -(declaim (type pcounter *n-bytes-freed-or-purified-pcounter*)) -(defvar *n-bytes-freed-or-purified-pcounter* (make-pcounter)) +(declaim (type unsigned-byte *n-bytes-freed-or-purified*)) +(defvar *n-bytes-freed-or-purified* 0) +(push (lambda () + (setf *n-bytes-freed-or-purified* 0)) + ;; KLUDGE: It's probably not quite safely right either to do + ;; this in *BEFORE-SAVE-INITIALIZATIONS* (since consing, or even + ;; worse, something which depended on (GET-BYTES-CONSED), might + ;; happen after that) or in *AFTER-SAVE-INITIALIZATIONS*. But + ;; it's probably not a big problem, and there seems to be no + ;; other obvious time to do it. -- WHN 2001-07-30 + *after-save-initializations*) (declaim (ftype (function () unsigned-byte) get-bytes-consed)) (defun get-bytes-consed () @@ -118,7 +133,7 @@ probably want either to hack in at a lower level (as the code in the SB-PROFILE package does), or to design a more microefficient interface and submit it as a patch." (+ (dynamic-usage) - (pcounter->integer *n-bytes-freed-or-purified-pcounter*))) + *n-bytes-freed-or-purified*)) ;;;; variables and constants @@ -127,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 @@ -156,13 +178,13 @@ and submit it as a patch." (declaim (type index *gc-run-time*)) ;;; a limit to help catch programs which allocate too much memory, -;;; since a hard heap overflow is so hard to recover from. +;;; since a hard heap overflow is so hard to recover from (declaim (type (or unsigned-byte null) *soft-heap-limit*)) (defvar *soft-heap-limit* nil) -;;; Internal trigger. 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 +;;; 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) @@ -190,9 +212,11 @@ and submit it as a patch." (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.~%" - bytes-in-use) + (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 @@ -207,31 +231,30 @@ and submit it as a patch." new-trigger) (declare (type stream notify-stream)) (format notify-stream - "~&; GC has finished with ~:D bytes in use (~:D bytes freed).~%" + "~&; GC has finished with ~:D bytes in use (~:D bytes freed)~@ + ; at internal runtime ~:D. The new GC trigger is ~:D bytes.~%" bytes-retained - bytes-freed) - (format notify-stream - "~&; The new GC trigger is ~:D bytes.~%" + bytes-freed + (get-internal-run-time) new-trigger) (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.") ;;;; 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 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 @@ -240,7 +263,7 @@ has finished GC'ing.") ;;;; 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) @@ -253,7 +276,7 @@ has finished GC'ing.") ;;; is not greater than *GC-TRIGGER*. ;;; ;;; For GENCGC all generations < GEN will be GC'ed. -(defun sub-gc (&key force-p (gen 0)) +(defun sub-gc (&key force-p (gen 0)) (/show0 "entering SUB-GC") (unless *already-maybe-gcing* (let* ((*already-maybe-gcing* t) @@ -271,7 +294,7 @@ 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)) @@ -334,8 +357,8 @@ has finished GC'ing.") (eff-n-bytes-freed (max 0 n-bytes-freed))) (declare (ignore ignore-me)) (/show0 "got (DYNAMIC-USAGE) and EFF-N-BYTES-FREED") - (incf-pcounter *n-bytes-freed-or-purified-pcounter* - eff-n-bytes-freed) + (incf *n-bytes-freed-or-purified* + eff-n-bytes-freed) (/show0 "clearing *NEED-TO-COLLECT-GARBAGE*") (setf *need-to-collect-garbage* nil) (/show0 "calculating NEW-GC-TRIGGER") @@ -376,14 +399,13 @@ has finished GC'ing.") object) ;;; This is the user-advertised garbage collection function. - (defun gc (&key (gen 0) (full nil) &allow-other-keys) #!+(and sb-doc gencgc) "Initiate a garbage collection. GEN controls the number of generations to garbage collect." #!+(and sb-doc (not gencgc)) - "Initiate a garbage collection. GEN may be provided for compatibility, but - is ignored." + "Initiate a garbage collection. GEN may be provided for compatibility with + generational garbage collectors, but is ignored in this implementation." (sub-gc :force-p t :gen (if full 6 gen)))