[5d4902]: src / code / gc.lisp Maximize Restore History

Download this file

gc.lisp    476 lines (421 with data), 18.5 kB

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
;;;; garbage collection and allocation-related code
;;;; This software is part of the SBCL system. See the README file for
;;;; more information.
;;;;
;;;; This software is derived from the CMU CL system, which was
;;;; written at Carnegie Mellon University and released into the
;;;; public domain. The software is in the public domain and is
;;;; provided with absolutely no warranty. See the COPYING and CREDITS
;;;; files for more information.
(in-package "SB!KERNEL")
;;;; DYNAMIC-USAGE and friends
(declaim (special sb!vm:*read-only-space-free-pointer*
sb!vm:*static-space-free-pointer*))
(eval-when (:compile-toplevel :execute)
(sb!xc:defmacro def-c-var-frob (lisp-fun c-var-name)
`(progn
#!-sb-fluid (declaim (inline ,lisp-fun))
(defun ,lisp-fun ()
(sb!alien:extern-alien ,c-var-name (sb!alien:unsigned 32))))))
#!-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
#!+gencgc
(def-c-var-frob dynamic-usage "bytes_allocated")
#!-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:n-word-bytes)
sb!vm:static-space-start))
(defun read-only-space-usage ()
(- (* sb!vm::*read-only-space-free-pointer* sb!vm:n-word-bytes)
sb!vm:read-only-space-start))
;;;; ROOM
(defun room-minimal-info ()
(format t "Dynamic space usage is: ~10:D bytes.~%" (dynamic-usage))
(format t "Read-only space usage is: ~10:D bytes.~%" (read-only-space-usage))
(format t "Static space usage is: ~10:D bytes.~%" (static-space-usage))
(format t "Dunno Control stack usage~%")
(format t "Dunno Binding stack usage~%")
(format t "Garbage collection is currently ~:[enabled~;DISABLED~].~%"
*gc-inhibit*))
(defun room-intermediate-info ()
(room-minimal-info)
(sb!vm:memory-usage :count-spaces '(:dynamic)
:print-spaces t
:cutoff 0.05f0
:print-summary nil))
(defun room-maximal-info ()
(room-minimal-info)
(sb!vm:memory-usage :count-spaces '(:static :dynamic))
(sb!vm:instance-usage :dynamic :top-n 10)
(sb!vm:instance-usage :static :top-n 10))
(defun room (&optional (verbosity :default))
#!+sb-doc
"Print to *STANDARD-OUTPUT* information about the state of internal
storage and its management. The optional argument controls the
verbosity of output. If it is T, ROOM prints out a maximal amount of
information. If it is NIL, ROOM prints out a minimal amount of
information. If it is :DEFAULT or it is not supplied, ROOM prints out
an intermediate amount of information."
(fresh-line)
(ecase verbosity
((t)
(room-maximal-info))
((nil)
(room-minimal-info))
(:default
(room-intermediate-info)))
(values))
;;;; GET-BYTES-CONSED
;;; the total number of bytes freed so far (including any freeing
;;; which goes on in PURIFY)
;;;
;;; (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 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 ()
#!+sb-doc
"Return the number of bytes consed since the program began. Typically
this result will be a consed bignum, so if you have an application (e.g.
profiling) which can't tolerate the overhead of consing bignums, you'll
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)
*n-bytes-freed-or-purified*))
;;;; variables and constants
;;; the minimum amount of dynamic space which must be consed before a
;;; GC will be triggered
;;;
;;; 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*
#!+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
(defvar *before-gc-hooks* nil ; actually initialized in cold init
#!+sb-doc
"A list of functions that are called before garbage collection occurs.
The functions should take no arguments.")
(defvar *after-gc-hooks* nil ; actually initialized in cold init
#!+sb-doc
"A list of functions that are called after garbage collection occurs.
The functions should take no arguments.")
(defvar *gc-notify-stream* nil ; (actually initialized in cold init)
#!+sb-doc
"When non-NIL, this must be a STREAM; and the functions bound to
*GC-NOTIFY-BEFORE* and *GC-NOTIFY-AFTER* are called with the
STREAM value before and after a garbage collection occurs
respectively.")
(defvar *gc-run-time* 0
#!+sb-doc
"the total CPU time spent doing garbage collection (as reported by
GET-INTERNAL-RUN-TIME)")
(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
;;;
;;; 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*
;; 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.
;;;
;;; 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)
;;; When non-NIL, inhibits garbage collection.
(defvar *gc-inhibit*) ; initialized in cold init
;;; This flag is used to prevent recursive entry into the garbage
;;; collector.
(defvar *already-maybe-gcing*) ; initialized in cold init
;;; When T, indicates that the dynamic usage has exceeded the value
;;; *GC-TRIGGER*.
(defvar *need-to-collect-garbage* nil) ; initialized in cold init
(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))
(finish-output notify-stream))
(defparameter *gc-notify-before* #'default-gc-notify-before
#!+sb-doc
"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.")
(defun default-gc-notify-after (notify-stream
bytes-retained
bytes-freed
new-trigger)
(declare (type stream notify-stream))
(format notify-stream
"~&; 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
(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; 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:define-alien-routine collect-garbage sb!alien:int
#!+gencgc (last-gen sb!alien:int))
(sb!alien:define-alien-routine set-auto-gc-trigger sb!alien:void
(dynamic-usage sb!alien:unsigned-long))
(sb!alien:define-alien-routine clear-auto-gc-trigger sb!alien:void)
(def-c-var-frob gc-thread-pid "gc_thread_pid")
(defun other-thread-collect-garbage (gen)
(setf (sb!alien:extern-alien "maybe_gc_pending" (sb!alien:unsigned 32))
(1+ gen))
(sb!unix:unix-kill (gc-thread-pid) :SIGALRM))
;;; 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
;;; know what you are doing.
(defvar *internal-gc* #'other-thread-collect-garbage)
;;;; SUB-GC
;;; 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)
(error (cond)
(warn "(FUNCALL ~S~{ ~S~}) lost:~%~A" ',function ',args cond)
nil))))
;;; SUB-GC decides when and if to do a garbage collection. The FORCE-P
;;; flags controls whether a GC should occur even if the dynamic usage
;;; 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?" crap 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*
(let* ((*already-maybe-gcing* t)
(start-time (get-internal-run-time))
(pre-gc-dynamic-usage (dynamic-usage))
;; Currently we only check *SOFT-HEAP-LIMIT* at GC time,
;; not for every allocation. That makes it cheap to do,
;; even if it is a little ugly.
(soft-heap-limit-exceeded? (and *soft-heap-limit*
(> pre-gc-dynamic-usage
*soft-heap-limit*)))
(*soft-heap-limit* (if soft-heap-limit-exceeded?
(+ pre-gc-dynamic-usage
*bytes-consed-between-gcs*)
*soft-heap-limit*)))
(when soft-heap-limit-exceeded?
(cerror "Continue with GC."
"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*)))
;; KLUDGE: Wow, we really mask interrupts all the time we're
;; collecting garbage? That seems like a long time.. -- WHN 19991129
(without-interrupts
;; FIXME: We probably shouldn't do this evil thing to
;; *STANDARD-OUTPUT* in a binding which is wrapped around
;; calls to user-settable GC hook functions.
(let ((*standard-output* *terminal-io*))
(when *gc-notify-stream*
(if (streamp *gc-notify-stream*)
(carefully-funcall *gc-notify-before*
*gc-notify-stream*
pre-gc-dynamic-usage)
(warn
"*GC-NOTIFY-STREAM* is set, but not a STREAM -- ignored.")))
(dolist (hook *before-gc-hooks*)
(carefully-funcall hook))
(when *gc-trigger*
(clear-auto-gc-trigger))
(let* (;; We do DYNAMIC-USAGE once more here in order to
;; get a more accurate measurement of the space
;; actually freed, since the messing around, e.g.
;; GC-notify stuff, since the DYNAMIC-USAGE which
;; triggered GC could've done a fair amount of
;; consing.)
(pre-internal-gc-dynamic-usage (dynamic-usage))
(ignore-me (funcall *internal-gc* gen))
(post-gc-dynamic-usage (dynamic-usage))
(n-bytes-freed (- pre-internal-gc-dynamic-usage
post-gc-dynamic-usage))
;; In sbcl-0.6.12.39, the raw N-BYTES-FREED from
;; GENCGC could sometimes be substantially negative
;; (e.g. -5872). I haven't looked into what causes
;; that, but I suspect it has to do with
;; fluctuating inefficiency in the way that the
;; GENCGC packs things into page boundaries.
;; Bumping the raw result up to 0 is a little ugly,
;; but shouldn't be a problem, and it's even
;; possible to sort of justify it: the packing
;; inefficiency which has caused (DYNAMIC-USAGE) to
;; grow is effectively consing, or at least
;; overhead of consing, so it's sort of correct to
;; add it to the running total of consing. ("Man
;; isn't a rational animal, he's a rationalizing
;; animal.":-) -- WHN 2001-06-23
(eff-n-bytes-freed (max 0 n-bytes-freed)))
(declare (ignore ignore-me))
(/show0 "got (DYNAMIC-USAGE) and 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")
(let ((new-gc-trigger (+ post-gc-dynamic-usage
*bytes-consed-between-gcs*)))
(/show0 "setting *GC-TRIGGER*")
(setf *gc-trigger* new-gc-trigger))
(/show0 "calling SET-AUTO-GC-TRIGGER")
(set-auto-gc-trigger *gc-trigger*)
(dolist (hook *after-gc-hooks*)
(/show0 "doing a hook from *AFTER-GC--HOOKS*")
;; FIXME: This hook should be called with the same
;; kind of information as *GC-NOTIFY-AFTER*. In
;; particular, it would be nice for the hook function
;; to be able to adjust *GC-TRIGGER* intelligently to
;; e.g. 108% of total memory usage.
(carefully-funcall hook))
(when *gc-notify-stream*
(if (streamp *gc-notify-stream*)
(carefully-funcall *gc-notify-after*
*gc-notify-stream*
post-gc-dynamic-usage
eff-n-bytes-freed
*gc-trigger*)
(warn
"*GC-NOTIFY-STREAM* is set, but not a stream -- ignored.")))))
(scrub-control-stack))) ;XXX again? we did this from C ...
(incf *gc-run-time* (- (get-internal-run-time)
start-time))))
;; FIXME: should probably return (VALUES), here and in RETURN-FROM
nil)
;;; This routine is called by the allocation miscops to decide whether
;;; a GC should occur. The argument, OBJECT, is the newly allocated
;;; object which must be returned to the caller.
(defun maybe-gc (&optional object)
(sub-gc)
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 with
generational garbage collectors, but is ignored in this implementation."
(sub-gc :force-p t :gen (if full 6 gen)))
;;;; auxiliary functions
(defun bytes-consed-between-gcs ()
#!+sb-doc
"Return the amount of memory that will be allocated before the next garbage
collection is initiated. This can be set with SETF."
*bytes-consed-between-gcs*)
(defun (setf bytes-consed-between-gcs) (val)
;; FIXME: Shouldn't this (and the DECLAIM for the underlying variable)
;; be for a strictly positive number type, e.g.
;; (AND (INTEGER 1) FIXNUM)?
(declare (type index val))
(let ((old *bytes-consed-between-gcs*))
(setf *bytes-consed-between-gcs* val)
(when *gc-trigger*
(setf *gc-trigger* (+ *gc-trigger* (- val old)))
(cond ((<= (dynamic-usage) *gc-trigger*)
(clear-auto-gc-trigger)
(set-auto-gc-trigger *gc-trigger*))
(t
;; FIXME: If SCRUB-CONTROL-STACK is required here, why
;; isn't it built into SUB-GC? And *is* it required here?
(sb!sys:scrub-control-stack)
(sub-gc)))))
val)
(defun gc-on ()
#!+sb-doc
"Enable the garbage collector."
(setq *gc-inhibit* nil)
(when *need-to-collect-garbage*
(sub-gc))
nil)
(defun gc-off ()
#!+sb-doc
"Disable the garbage collector."
(setq *gc-inhibit* t)
nil)
;;;; initialization stuff
(defun gc-reinit ()
(when *gc-trigger*
(if (< *gc-trigger* (dynamic-usage))
(sub-gc)
(set-auto-gc-trigger *gc-trigger*))))