1 ;;;; heap-grovelling memory usage stuff
3 ;;;; This software is part of the SBCL system. See the README file for
6 ;;;; This software is derived from the CMU CL system, which was
7 ;;;; written at Carnegie Mellon University and released into the
8 ;;;; public domain. The software is in the public domain and is
9 ;;;; provided with absolutely no warranty. See the COPYING and CREDITS
10 ;;;; files for more information.
14 ;;;; type format database
16 (eval-when (:compile-toplevel
:load-toplevel
:execute
)
17 (def!struct
(room-info (:make-load-form-fun just-dump-it-normally
))
18 ;; the name of this type
19 (name nil
:type symbol
)
20 ;; kind of type (how we determine length)
22 :type
(member :lowtag
:fixed
:header
:vector
23 :string
:code
:closure
:instance
))
24 ;; length if fixed-length, shift amount for element size if :VECTOR
25 (length nil
:type
(or fixnum null
))))
27 (eval-when (:compile-toplevel
:execute
)
29 (defvar *meta-room-info
* (make-array 256 :initial-element nil
))
31 (dolist (obj *primitive-objects
*)
32 (let ((widetag (primitive-object-widetag obj
))
33 (lowtag (primitive-object-lowtag obj
))
34 (name (primitive-object-name obj
))
35 (variable (primitive-object-variable-length-p obj
))
36 (size (primitive-object-size obj
)))
39 (;; KLUDGE described in dan_b message "Another one for the
40 ;; collection [bug 108]" (sbcl-devel 2004-01-22)
42 ;; In a freshly started SBCL 0.8.7.20ish, (TIME (ROOM T)) causes
43 ;; debugger invoked on a SB-INT:BUG in thread 5911:
44 ;; failed AVER: "(SAP= CURRENT END)"
45 ;; [WHN: Similar things happened on one but not the other of my
46 ;; machines when I just run ROOM a lot in a loop.]
48 ;; This appears to be due to my [DB] abuse of the primitive
49 ;; object macros to define a thread object that shares a lowtag
50 ;; with fixnums and has no widetag: it looks like the code that
51 ;; generates *META-ROOM-INFO* infers from this that even fixnums
52 ;; are thread-sized - probably undesirable.
54 ;; This [the fix; the EQL NAME 'THREAD clause here] is more in the
55 ;; nature of a workaround than a really good fix. I'm not sure
56 ;; what a really good fix is: I /think/ it's probably to remove
57 ;; the :LOWTAG option in DEFINE-PRIMITIVE-OBJECT THREAD, then teach
58 ;; genesis to generate the necessary OBJECT_SLOT_OFFSET macros
59 ;; for assembly source in the runtime/genesis/*.h files.
62 (let ((info (make-room-info :name name
64 (lowtag (symbol-value lowtag
)))
65 (declare (fixnum lowtag
))
67 (setf (svref *meta-room-info
* (logior lowtag
(ash i
3))) info
))))
70 (setf (svref *meta-room-info
* (symbol-value widetag
))
71 (make-room-info :name name
75 (dolist (code (list #!+sb-unicode complex-character-string-widetag
76 complex-base-string-widetag simple-array-widetag
77 complex-bit-vector-widetag complex-vector-widetag
78 complex-array-widetag complex-vector-nil-widetag
))
79 (setf (svref *meta-room-info
* code
)
80 (make-room-info :name
'array-header
83 (setf (svref *meta-room-info
* bignum-widetag
)
84 (make-room-info :name
'bignum
87 (setf (svref *meta-room-info
* closure-header-widetag
)
88 (make-room-info :name
'closure
91 ;; FIXME: This looks rather brittle. Can we get more of these numbers
92 ;; from somewhere sensible?
93 (dolist (stuff '((simple-bit-vector-widetag . -
3)
94 (simple-vector-widetag .
#.sb
!vm
:word-shift
)
95 (simple-array-unsigned-byte-2-widetag . -
2)
96 (simple-array-unsigned-byte-4-widetag . -
1)
97 (simple-array-unsigned-byte-7-widetag .
0)
98 (simple-array-unsigned-byte-8-widetag .
0)
99 (simple-array-unsigned-byte-15-widetag .
1)
100 (simple-array-unsigned-byte-16-widetag .
1)
101 (simple-array-unsigned-byte-31-widetag .
2)
102 (simple-array-unsigned-byte-32-widetag .
2)
103 (simple-array-unsigned-byte-60-widetag .
3)
104 (simple-array-unsigned-byte-63-widetag .
3)
105 (simple-array-unsigned-byte-64-widetag .
3)
106 (simple-array-signed-byte-8-widetag .
0)
107 (simple-array-signed-byte-16-widetag .
1)
108 (simple-array-unsigned-byte-29-widetag .
2)
109 (simple-array-signed-byte-30-widetag .
2)
110 (simple-array-signed-byte-32-widetag .
2)
111 (simple-array-signed-byte-61-widetag .
3)
112 (simple-array-signed-byte-64-widetag .
3)
113 (simple-array-single-float-widetag .
2)
114 (simple-array-double-float-widetag .
3)
115 (simple-array-complex-single-float-widetag .
3)
116 (simple-array-complex-double-float-widetag .
4)))
117 (let* ((name (car stuff
))
119 (sname (string name
)))
121 (setf (svref *meta-room-info
* (symbol-value name
))
122 (make-room-info :name
(intern (subseq sname
124 (mismatch sname
"-WIDETAG"
129 (setf (svref *meta-room-info
* simple-base-string-widetag
)
130 (make-room-info :name
'simple-base-string
135 (setf (svref *meta-room-info
* simple-character-string-widetag
)
136 (make-room-info :name
'simple-character-string
140 (setf (svref *meta-room-info
* simple-array-nil-widetag
)
141 (make-room-info :name
'simple-array-nil
145 (setf (svref *meta-room-info
* code-header-widetag
)
146 (make-room-info :name
'code
149 (setf (svref *meta-room-info
* instance-header-widetag
)
150 (make-room-info :name
'instance
155 (defparameter *room-info
* '#.
*meta-room-info
*)
156 (deftype spaces
() '(member :static
:dynamic
:read-only
))
158 ;;;; MAP-ALLOCATED-OBJECTS
160 ;;; Since they're represented as counts of words, we should never
161 ;;; need bignums to represent these:
162 (declaim (type fixnum
163 *static-space-free-pointer
*
164 *read-only-space-free-pointer
*))
166 (defun space-bounds (space)
167 (declare (type spaces space
))
170 (values (int-sap static-space-start
)
171 (int-sap (* *static-space-free-pointer
* n-word-bytes
))))
173 (values (int-sap read-only-space-start
)
174 (int-sap (* *read-only-space-free-pointer
* n-word-bytes
))))
176 (values (int-sap (current-dynamic-space-start))
177 (dynamic-space-free-pointer)))))
179 ;;; Return the total number of bytes used in SPACE.
180 (defun space-bytes (space)
181 (multiple-value-bind (start end
) (space-bounds space
)
182 (- (sap-int end
) (sap-int start
))))
184 ;;; Round SIZE (in bytes) up to the next dualword boundary. A dualword
185 ;;; is eight bytes on platforms with 32-bit word size and 16 bytes on
186 ;;; platforms with 64-bit word size.
187 #!-sb-fluid
(declaim (inline round-to-dualword
))
188 (defun round-to-dualword (size)
189 (logand (the word
(+ size lowtag-mask
)) (lognot lowtag-mask
)))
191 ;;; Return the total size of a vector in bytes, including any pad.
192 #!-sb-fluid
(declaim (inline vector-total-size
))
193 (defun vector-total-size (obj info
)
194 (let ((shift (room-info-length info
))
195 (len (+ (length (the (simple-array * (*)) obj
))
196 (ecase (room-info-kind info
)
200 (+ (* vector-data-offset n-word-bytes
)
202 (ash (+ len
(1- (ash 1 (- shift
))))
206 ;;; Access to the GENCGC page table for better precision in
207 ;;; MAP-ALLOCATED-OBJECTS
210 (define-alien-type (struct page
)
213 ;; On platforms with small enough GC pages, this field
214 ;; will be a short. On platforms with larger ones, it'll
216 (bytes-used (unsigned
217 #.
(if (typep sb
!vm
:gencgc-page-size
223 (declaim (inline find-page-index
))
224 (define-alien-routine "find_page_index" long
(index long
))
225 (define-alien-variable "page_table" (* (struct page
))))
227 ;;; Iterate over all the objects allocated in SPACE, calling FUN with
228 ;;; the object, the object's type code, and the object's total size in
229 ;;; bytes, including any header and padding. CAREFUL makes
230 ;;; MAP-ALLOCATED-OBJECTS slightly more accurate, but a lot slower: it
231 ;;; is intended for slightly more demanding uses of heap groveling
233 #!-sb-fluid
(declaim (maybe-inline map-allocated-objects
))
234 (defun map-allocated-objects (fun space
&optional careful
)
235 (declare (type function fun
) (type spaces space
))
236 (flet ((make-obj (tagged-address)
238 (make-lisp-obj tagged-address nil
)
239 (values (%make-lisp-obj tagged-address
) t
))))
241 (multiple-value-bind (start end
) (space-bounds space
)
242 (declare (type system-area-pointer start end
))
243 (declare (optimize (speed 3)))
244 (let ((current start
)
246 (skip-tests-until-addr 0))
247 (labels ((maybe-finish-mapping ()
248 (unless (sap< current end
)
249 (aver (sap= current end
))
250 (return-from map-allocated-objects
)))
251 ;; GENCGC doesn't allocate linearly, which means that the
252 ;; dynamic space can contain large blocks zeros that get
253 ;; accounted as conses in ROOM (and slow down other
254 ;; applications of MAP-ALLOCATED-OBJECTS). To fix this
255 ;; check the GC page structure for the current address.
256 ;; If the page is free or the address is beyond the page-
257 ;; internal allocation offset (bytes-used) skip to the
258 ;; next page immediately.
261 (when (eq space
:dynamic
)
262 (loop with page-mask
= #.
(1- sb
!vm
:gencgc-page-size
)
263 for addr of-type sb
!vm
:word
= (sap-int current
)
264 while
(>= addr skip-tests-until-addr
)
266 ;; For some reason binding PAGE with LET
267 ;; conses like mad (but gives no compiler notes...)
268 ;; Work around the problem with SYMBOL-MACROLET
269 ;; instead of trying to figure out the real
270 ;; issue. -- JES, 2005-05-17
272 ((page (deref page-table
273 (find-page-index addr
))))
274 ;; Don't we have any nicer way to access C struct
276 (let ((alloc-flag (ldb (byte 3 2)
278 (bytes-used (slot page
'bytes-used
)))
279 ;; If the page is not free and the current
280 ;; pointer is still below the allocation offset
282 (when (and (not (zerop alloc-flag
))
283 (<= (logand page-mask addr
)
285 ;; Don't bother testing again until we
286 ;; get past that allocation offset
287 (setf skip-tests-until-addr
288 (+ (logandc2 addr page-mask
) bytes-used
))
289 ;; And then continue with the
291 (return-from maybe-skip-page
))
292 ;; Move CURRENT to start of next page.
293 (setf current
(int-sap (+ (logandc2 addr page-mask
)
294 sb
!vm
:gencgc-page-size
)))
295 (maybe-finish-mapping))))))
296 (maybe-map (obj obj-tag n-obj-bytes
&optional
(ok t
))
297 (let ((next (typecase n-obj-bytes
298 (fixnum (sap+ current n-obj-bytes
))
299 (integer (sap+ current n-obj-bytes
)))))
300 ;; If this object would take us past END, it must
301 ;; be either bogus, or it has been allocated after
302 ;; the call to M-A-O.
303 (cond ((and ok next
(sap<= next end
))
304 (funcall fun obj obj-tag n-obj-bytes
)
307 (setf current
(sap+ current n-word-bytes
)))))))
308 (declare (inline maybe-finish-mapping maybe-skip-page maybe-map
))
310 (maybe-finish-mapping)
312 (let* ((header (sap-ref-word current
0))
313 (header-widetag (logand header
#xFF
))
314 (info (svref *room-info
* header-widetag
)))
317 (eq (room-info-kind info
) :lowtag
))
318 (multiple-value-bind (obj ok
)
319 (make-obj (logior (sap-int current
) list-pointer-lowtag
))
322 (* cons-size n-word-bytes
)
324 ((eql header-widetag closure-header-widetag
)
325 (let* ((obj (%make-lisp-obj
(logior (sap-int current
)
326 fun-pointer-lowtag
)))
327 (size (round-to-dualword
328 (* (the fixnum
(1+ (get-closure-length obj
)))
330 (maybe-map obj header-widetag size
)))
331 ((eq (room-info-kind info
) :instance
)
332 (let* ((obj (%make-lisp-obj
333 (logior (sap-int current
) instance-pointer-lowtag
)))
334 (size (round-to-dualword
335 (* (+ (%instance-length obj
) 1) n-word-bytes
))))
336 (aver (zerop (logand size lowtag-mask
)))
337 (maybe-map obj header-widetag size
)))
339 (multiple-value-bind (obj ok
)
340 (make-obj (logior (sap-int current
) other-pointer-lowtag
))
342 (ecase (room-info-kind info
)
344 (aver (or (eql (room-info-length info
)
345 (1+ (get-header-data obj
)))
347 (simple-array-nil-p obj
)))
349 (* (room-info-length info
) n-word-bytes
)))
351 (vector-total-size obj info
))
354 (* (1+ (get-header-data obj
)) n-word-bytes
)))
357 (* (get-header-data obj
) n-word-bytes
))
359 (* (the fixnum
(%code-code-size obj
))
363 (when size
(aver (zerop (logand size lowtag-mask
))))
364 (maybe-map obj header-widetag size
))))
368 (null (frob))))))))))))))))
373 ;;; Return a list of 3-lists (bytes object type-name) for the objects
374 ;;; allocated in Space.
375 (defun type-breakdown (space)
376 (let ((sizes (make-array 256 :initial-element
0 :element-type
'(unsigned-byte #.sb
!vm
:n-word-bits
)))
377 (counts (make-array 256 :initial-element
0 :element-type
'(unsigned-byte #.sb
!vm
:n-word-bits
))))
378 (map-allocated-objects
379 (lambda (obj type size
)
380 (declare (word size
) (optimize (speed 3)) (ignore obj
))
381 (incf (aref sizes type
) size
)
382 (incf (aref counts type
)))
385 (let ((totals (make-hash-table :test
'eq
)))
387 (let ((total-count (aref counts i
)))
388 (unless (zerop total-count
)
389 (let* ((total-size (aref sizes i
))
390 (name (room-info-name (aref *room-info
* i
)))
391 (found (gethash name totals
)))
393 (incf (first found
) total-size
)
394 (incf (second found
) total-count
))
396 (setf (gethash name totals
)
397 (list total-size total-count name
))))))))
399 (collect ((totals-list))
400 (maphash (lambda (k v
)
404 (sort (totals-list) #'> :key
#'first
)))))
406 ;;; Handle the summary printing for MEMORY-USAGE. Totals is a list of lists
407 ;;; (space-name . totals-for-space), where totals-for-space is the list
408 ;;; returned by TYPE-BREAKDOWN.
409 (defun print-summary (spaces totals
)
410 (let ((summary (make-hash-table :test
'eq
)))
411 (dolist (space-total totals
)
412 (dolist (total (cdr space-total
))
413 (push (cons (car space-total
) total
)
414 (gethash (third total
) summary
))))
416 (collect ((summary-totals))
417 (maphash (lambda (k v
)
420 (declare (unsigned-byte sum
))
421 (dolist (space-total v
)
422 (incf sum
(first (cdr space-total
))))
423 (summary-totals (cons sum v
))))
426 (format t
"~2&Summary of spaces: ~(~{~A ~}~)~%" spaces
)
427 (let ((summary-total-bytes 0)
428 (summary-total-objects 0))
429 (declare (unsigned-byte summary-total-bytes summary-total-objects
))
430 (dolist (space-totals
431 (mapcar #'cdr
(sort (summary-totals) #'> :key
#'car
)))
432 (let ((total-objects 0)
435 (declare (unsigned-byte total-objects total-bytes
))
437 (dolist (space-total space-totals
)
438 (let ((total (cdr space-total
)))
439 (setq name
(third total
))
440 (incf total-bytes
(first total
))
441 (incf total-objects
(second total
))
442 (spaces (cons (car space-total
) (first total
)))))
443 (format t
"~%~A:~% ~:D bytes, ~:D object~:P"
444 name total-bytes total-objects
)
445 (dolist (space (spaces))
446 (format t
", ~W% ~(~A~)"
447 (round (* (cdr space
) 100) total-bytes
)
450 (incf summary-total-bytes total-bytes
)
451 (incf summary-total-objects total-objects
))))
452 (format t
"~%Summary total:~% ~:D bytes, ~:D objects.~%"
453 summary-total-bytes summary-total-objects
)))))
455 ;;; Report object usage for a single space.
456 (defun report-space-total (space-total cutoff
)
457 (declare (list space-total
) (type (or single-float null
) cutoff
))
458 (format t
"~2&Breakdown for ~(~A~) space:~%" (car space-total
))
459 (let* ((types (cdr space-total
))
460 (total-bytes (reduce #'+ (mapcar #'first types
)))
461 (total-objects (reduce #'+ (mapcar #'second types
)))
462 (cutoff-point (if cutoff
463 (truncate (* (float total-bytes
) cutoff
))
466 (reported-objects 0))
467 (declare (unsigned-byte total-objects total-bytes cutoff-point reported-objects
469 (loop for
(bytes objects name
) in types do
470 (when (<= bytes cutoff-point
)
471 (format t
" ~10:D bytes for ~9:D other object~2:*~P.~%"
472 (- total-bytes reported-bytes
)
473 (- total-objects reported-objects
))
475 (incf reported-bytes bytes
)
476 (incf reported-objects objects
)
477 (format t
" ~10:D bytes for ~9:D ~(~A~) object~2:*~P.~%"
479 (format t
" ~10:D bytes for ~9:D ~(~A~) object~2:*~P (space total.)~%"
480 total-bytes total-objects
(car space-total
))))
482 ;;; Print information about the heap memory in use. PRINT-SPACES is a
483 ;;; list of the spaces to print detailed information for.
484 ;;; COUNT-SPACES is a list of the spaces to scan. For either one, T
485 ;;; means all spaces (i.e. :STATIC, :DYNAMIC and :READ-ONLY.) If
486 ;;; PRINT-SUMMARY is true, then summary information will be printed.
487 ;;; The defaults print only summary information for dynamic space. If
488 ;;; true, CUTOFF is a fraction of the usage in a report below which
489 ;;; types will be combined as OTHER.
490 (defun memory-usage (&key print-spaces
(count-spaces '(:dynamic
))
491 (print-summary t
) cutoff
)
492 (declare (type (or single-float null
) cutoff
))
493 (let* ((spaces (if (eq count-spaces t
)
494 '(:static
:dynamic
:read-only
)
496 (totals (mapcar (lambda (space)
497 (cons space
(type-breakdown space
)))
500 (dolist (space-total totals
)
501 (when (or (eq print-spaces t
)
502 (member (car space-total
) print-spaces
))
503 (report-space-total space-total cutoff
)))
505 (when print-summary
(print-summary spaces totals
)))
509 ;;; Print info about how much code and no-ops there are in SPACE.
510 (defun count-no-ops (space)
511 (declare (type spaces space
))
515 (declare (fixnum code-words no-ops
)
516 (type unsigned-byte total-bytes
))
517 (map-allocated-objects
518 (lambda (obj type size
)
519 (when (eql type code-header-widetag
)
520 (let ((words (truly-the fixnum
(%code-code-size obj
)))
521 (sap (truly-the system-area-pointer
522 (%primitive code-instructions obj
)))
524 (declare (fixnum size
))
525 (incf total-bytes size
)
526 (incf code-words words
)
528 (when (zerop (sap-ref-word sap
(* i n-word-bytes
)))
533 "~:D code-object bytes, ~:D code words, with ~:D no-ops (~D%).~%"
534 total-bytes code-words no-ops
535 (round (* no-ops
100) code-words
)))
539 (defun descriptor-vs-non-descriptor-storage (&rest spaces
)
540 (let ((descriptor-words 0)
541 (non-descriptor-headers 0)
542 (non-descriptor-bytes 0))
543 (declare (type unsigned-byte descriptor-words non-descriptor-headers
544 non-descriptor-bytes
))
545 (dolist (space (or spaces
'(:read-only
:static
:dynamic
)))
546 (declare (inline map-allocated-objects
))
547 (map-allocated-objects
548 (lambda (obj type size
)
550 (#.code-header-widetag
551 (let ((inst-words (truly-the fixnum
(%code-code-size obj
)))
553 (declare (type fixnum size inst-words
))
554 (incf non-descriptor-bytes
(* inst-words n-word-bytes
))
555 (incf descriptor-words
556 (- (truncate size n-word-bytes
) inst-words
))))
558 #.single-float-widetag
559 #.double-float-widetag
560 #.simple-base-string-widetag
561 #!+sb-unicode
#.simple-character-string-widetag
562 #.simple-array-nil-widetag
563 #.simple-bit-vector-widetag
564 #.simple-array-unsigned-byte-2-widetag
565 #.simple-array-unsigned-byte-4-widetag
566 #.simple-array-unsigned-byte-8-widetag
567 #.simple-array-unsigned-byte-16-widetag
568 #.simple-array-unsigned-byte-32-widetag
569 #.simple-array-signed-byte-8-widetag
570 #.simple-array-signed-byte-16-widetag
571 ;; #.simple-array-signed-byte-30-widetag
572 #.simple-array-signed-byte-32-widetag
573 #.simple-array-single-float-widetag
574 #.simple-array-double-float-widetag
575 #.simple-array-complex-single-float-widetag
576 #.simple-array-complex-double-float-widetag
)
577 (incf non-descriptor-headers
)
578 (incf non-descriptor-bytes
(- size n-word-bytes
)))
579 ((#.list-pointer-lowtag
580 #.instance-pointer-lowtag
583 #.simple-array-widetag
584 #.simple-vector-widetag
585 #.complex-base-string-widetag
586 #.complex-vector-nil-widetag
587 #.complex-bit-vector-widetag
588 #.complex-vector-widetag
589 #.complex-array-widetag
590 #.closure-header-widetag
591 #.funcallable-instance-header-widetag
592 #.value-cell-header-widetag
593 #.symbol-header-widetag
595 #.weak-pointer-widetag
596 #.instance-header-widetag
)
597 (incf descriptor-words
(truncate (the fixnum size
) n-word-bytes
)))
599 (error "bogus widetag: ~W" type
))))
601 (format t
"~:D words allocated for descriptor objects.~%"
603 (format t
"~:D bytes data/~:D words header for non-descriptor objects.~%"
604 non-descriptor-bytes non-descriptor-headers
)
607 ;;; Print a breakdown by instance type of all the instances allocated
608 ;;; in SPACE. If TOP-N is true, print only information for the
609 ;;; TOP-N types with largest usage.
610 (defun instance-usage (space &key
(top-n 15))
611 (declare (type spaces space
) (type (or fixnum null
) top-n
))
612 (format t
"~2&~@[Top ~W ~]~(~A~) instance types:~%" top-n space
)
613 (let ((totals (make-hash-table :test
'eq
))
616 (declare (unsigned-byte total-objects total-bytes
))
617 (map-allocated-objects
618 (lambda (obj type size
)
619 (declare (optimize (speed 3)))
620 (when (eql type instance-header-widetag
)
622 (let* ((classoid (layout-classoid (%instance-ref obj
0)))
623 (found (gethash classoid totals
))
625 (declare (fixnum size
))
626 (incf total-bytes size
)
628 (incf (the fixnum
(car found
)))
629 (incf (the fixnum
(cdr found
)) size
))
631 (setf (gethash classoid totals
) (cons 1 size
)))))))
634 (collect ((totals-list))
635 (maphash (lambda (classoid what
)
636 (totals-list (cons (prin1-to-string
637 (classoid-proper-name classoid
))
640 (let ((sorted (sort (totals-list) #'> :key
#'cddr
))
643 (declare (unsigned-byte printed-bytes printed-objects
))
644 (dolist (what (if top-n
645 (subseq sorted
0 (min (length sorted
) top-n
))
647 (let ((bytes (cddr what
))
648 (objects (cadr what
)))
649 (incf printed-bytes bytes
)
650 (incf printed-objects objects
)
651 (format t
" ~A: ~:D bytes, ~:D object~:P.~%" (car what
)
654 (let ((residual-objects (- total-objects printed-objects
))
655 (residual-bytes (- total-bytes printed-bytes
)))
656 (unless (zerop residual-objects
)
657 (format t
" Other types: ~:D bytes, ~:D object~:P.~%"
658 residual-bytes residual-objects
))))
660 (format t
" ~:(~A~) instance total: ~:D bytes, ~:D object~:P.~%"
661 space total-bytes total-objects
)))
665 ;;;; PRINT-ALLOCATED-OBJECTS
667 (defun print-allocated-objects (space &key
(percent 0) (pages 5)
668 type larger smaller count
669 (stream *standard-output
*))
670 (declare (type (integer 0 99) percent
) (type index pages
)
671 (type stream stream
) (type spaces space
)
672 (type (or index null
) type larger smaller count
))
673 (multiple-value-bind (start-sap end-sap
) (space-bounds space
)
674 (let* ((space-start (sap-int start-sap
))
675 (space-end (sap-int end-sap
))
676 (space-size (- space-end space-start
))
677 (pagesize (sb!sys
:get-page-size
))
678 (start (+ space-start
(round (* space-size percent
) 100)))
679 (printed-conses (make-hash-table :test
'eq
))
683 (declare (type (unsigned-byte 32) last-page start
)
684 (fixnum pages-so-far count-so-far pagesize
))
685 (labels ((note-conses (x)
686 (unless (or (atom x
) (gethash x printed-conses
))
687 (setf (gethash x printed-conses
) t
)
688 (note-conses (car x
))
689 (note-conses (cdr x
)))))
690 (map-allocated-objects
691 (lambda (obj obj-type size
)
692 (let ((addr (get-lisp-obj-address obj
)))
693 (when (>= addr start
)
695 (> count-so-far count
)
696 (> pages-so-far pages
))
697 (return-from print-allocated-objects
(values)))
700 (let ((this-page (* (the (values (unsigned-byte 32) t
)
701 (truncate addr pagesize
))
703 (declare (type (unsigned-byte 32) this-page
))
704 (when (/= this-page last-page
)
705 (when (< pages-so-far pages
)
706 ;; FIXME: What is this? (ERROR "Argh..")? or
707 ;; a warning? or code that can be removed
708 ;; once the system is stable? or what?
709 (format stream
"~2&**** Page ~W, address ~X:~%"
711 (setq last-page this-page
)
712 (incf pages-so-far
))))
714 (when (and (or (not type
) (eql obj-type type
))
715 (or (not smaller
) (<= size smaller
))
716 (or (not larger
) (>= size larger
)))
719 (#.code-header-widetag
720 (let ((dinfo (%code-debug-info obj
)))
721 (format stream
"~&Code object: ~S~%"
723 (sb!c
::compiled-debug-info-name dinfo
)
725 (#.symbol-header-widetag
726 (format stream
"~&~S~%" obj
))
727 (#.list-pointer-lowtag
728 (unless (gethash obj printed-conses
)
730 (let ((*print-circle
* t
)
733 (format stream
"~&~S~%" obj
))))
736 (let ((str (write-to-string obj
:level
5 :length
10
738 (unless (eql type instance-header-widetag
)
739 (format stream
"~S: " (type-of obj
)))
740 (format stream
"~A~%"
741 (subseq str
0 (min (length str
) 60))))))))))
745 ;;;; LIST-ALLOCATED-OBJECTS, LIST-REFERENCING-OBJECTS
747 (defvar *ignore-after
* nil
)
749 (defun valid-obj (space x
)
750 (or (not (eq space
:dynamic
))
751 ;; this test looks bogus if the allocator doesn't work linearly,
752 ;; which I suspect is the case for GENCGC. -- CSR, 2004-06-29
753 (< (get-lisp-obj-address x
) (get-lisp-obj-address *ignore-after
*))))
755 (defun maybe-cons (space x stuff
)
756 (if (valid-obj space x
)
760 (defun list-allocated-objects (space &key type larger smaller count
762 (declare (type spaces space
)
763 (type (or index null
) larger smaller type count
)
764 (type (or function null
) test
)
765 (inline map-allocated-objects
))
766 (unless *ignore-after
*
767 (setq *ignore-after
* (cons 1 2)))
768 (collect ((counted 0 1+))
770 (map-allocated-objects
771 (lambda (obj obj-type size
)
772 (when (and (or (not type
) (eql obj-type type
))
773 (or (not smaller
) (<= size smaller
))
774 (or (not larger
) (>= size larger
))
775 (or (not test
) (funcall test obj
)))
776 (setq res
(maybe-cons space obj res
))
777 (when (and count
(>= (counted) count
))
778 (return-from list-allocated-objects res
))))
782 (defun map-referencing-objects (fun space object
)
783 (declare (type spaces space
) (inline map-allocated-objects
))
784 (unless *ignore-after
*
785 (setq *ignore-after
* (cons 1 2)))
786 (flet ((maybe-call (fun obj
)
787 (when (valid-obj space obj
)
789 (map-allocated-objects
790 (lambda (obj obj-type size
)
791 (declare (ignore obj-type size
))
794 (when (or (eq (car obj
) object
)
795 (eq (cdr obj
) object
))
796 (maybe-call fun obj
)))
798 (dotimes (i (%instance-length obj
))
799 (when (eq (%instance-ref obj i
) object
)
803 (let ((length (get-header-data obj
)))
804 (do ((i code-constants-offset
(1+ i
)))
806 (when (eq (code-header-ref obj i
) object
)
810 (dotimes (i (length obj
))
811 (when (eq (svref obj i
) object
)
815 (when (or (eq (symbol-name obj
) object
)
816 (eq (symbol-package obj
) object
)
817 (eq (symbol-plist obj
) object
)
819 (eq (symbol-value obj
) object
)))
820 (maybe-call fun obj
)))))
823 (defun list-referencing-objects (space object
)
825 (map-referencing-objects
826 (lambda (obj) (res obj
)) space object
)