keep docstrings from PCL bootstrap around
[sbcl.git] / src / code / room.lisp
blob5f6d93c0ccf24daf01296d7a16b7ffe770512483
1 ;;;; heap-grovelling memory usage stuff
3 ;;;; This software is part of the SBCL system. See the README file for
4 ;;;; more information.
5 ;;;;
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.
12 (in-package "SB!VM")
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)
21 (kind (missing-arg)
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)))
37 (cond
38 ((not lowtag))
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.
60 (eql name 'thread))
61 ((not widetag)
62 (let ((info (make-room-info :name name
63 :kind :lowtag))
64 (lowtag (symbol-value lowtag)))
65 (declare (fixnum lowtag))
66 (dotimes (i 32)
67 (setf (svref *meta-room-info* (logior lowtag (ash i 3))) info))))
68 (variable)
70 (setf (svref *meta-room-info* (symbol-value widetag))
71 (make-room-info :name name
72 :kind :fixed
73 :length size))))))
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
81 :kind :header)))
83 (setf (svref *meta-room-info* bignum-widetag)
84 (make-room-info :name 'bignum
85 :kind :header))
87 (setf (svref *meta-room-info* closure-header-widetag)
88 (make-room-info :name 'closure
89 :kind :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))
118 (size (cdr stuff))
119 (sname (string name)))
120 (when (boundp name)
121 (setf (svref *meta-room-info* (symbol-value name))
122 (make-room-info :name (intern (subseq sname
124 (mismatch sname "-WIDETAG"
125 :from-end t)))
126 :kind :vector
127 :length size)))))
129 (setf (svref *meta-room-info* simple-base-string-widetag)
130 (make-room-info :name 'simple-base-string
131 :kind :string
132 :length 0))
134 #!+sb-unicode
135 (setf (svref *meta-room-info* simple-character-string-widetag)
136 (make-room-info :name 'simple-character-string
137 :kind :string
138 :length 2))
140 (setf (svref *meta-room-info* simple-array-nil-widetag)
141 (make-room-info :name 'simple-array-nil
142 :kind :fixed
143 :length 2))
145 (setf (svref *meta-room-info* code-header-widetag)
146 (make-room-info :name 'code
147 :kind :code))
149 (setf (svref *meta-room-info* instance-header-widetag)
150 (make-room-info :name 'instance
151 :kind :instance))
153 ) ; EVAL-WHEN
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))
168 (ecase space
169 (:static
170 (values (int-sap static-space-start)
171 (int-sap (* *static-space-free-pointer* n-word-bytes))))
172 (:read-only
173 (values (int-sap read-only-space-start)
174 (int-sap (* *read-only-space-free-pointer* n-word-bytes))))
175 (:dynamic
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 (declare (fixnum size))
190 (logand (the fixnum (+ size lowtag-mask)) (lognot lowtag-mask)))
192 ;;; Return the total size of a vector in bytes, including any pad.
193 #!-sb-fluid (declaim (inline vector-total-size))
194 (defun vector-total-size (obj info)
195 (let ((shift (room-info-length info))
196 (len (+ (length (the (simple-array * (*)) obj))
197 (ecase (room-info-kind info)
198 (:vector 0)
199 (:string 1)))))
200 (round-to-dualword
201 (+ (* vector-data-offset n-word-bytes)
202 (if (minusp shift)
203 (ash (+ len (1- (ash 1 (- shift))))
204 shift)
205 (ash len shift))))))
207 ;;; Access to the GENCGC page table for better precision in
208 ;;; MAP-ALLOCATED-OBJECTS
209 #!+gencgc
210 (progn
211 (define-alien-type (struct page)
212 (struct page
213 (start long)
214 ;; On platforms with small enough GC pages, this field
215 ;; will be a short. On platforms with larger ones, it'll
216 ;; be an int.
217 (bytes-used (unsigned
218 #.(if (typep sb!vm:gencgc-page-size
219 '(unsigned-byte 16))
221 32)))
222 (flags (unsigned 8))
223 (gen (signed 8))))
224 (declaim (inline find-page-index))
225 (define-alien-routine "find_page_index" long (index long))
226 (define-alien-variable "page_table" (* (struct page))))
228 ;;; Iterate over all the objects allocated in SPACE, calling FUN with
229 ;;; the object, the object's type code, and the object's total size in
230 ;;; bytes, including any header and padding.
231 #!-sb-fluid (declaim (maybe-inline map-allocated-objects))
232 (defun map-allocated-objects (fun space)
233 (declare (type function fun) (type spaces space))
234 (without-gcing
235 (multiple-value-bind (start end) (space-bounds space)
236 (declare (type system-area-pointer start end))
237 (declare (optimize (speed 3)))
238 (let ((current start)
239 #!+gencgc (skip-tests-until-addr 0))
240 (labels ((maybe-finish-mapping ()
241 (unless (sap< current end)
242 (aver (sap= current end))
243 (return-from map-allocated-objects)))
244 ;; GENCGC doesn't allocate linearly, which means that the
245 ;; dynamic space can contain large blocks zeros that get
246 ;; accounted as conses in ROOM (and slow down other
247 ;; applications of MAP-ALLOCATED-OBJECTS). To fix this
248 ;; check the GC page structure for the current address.
249 ;; If the page is free or the address is beyond the page-
250 ;; internal allocation offset (bytes-used) skip to the
251 ;; next page immediately.
252 (maybe-skip-page ()
253 #!+gencgc
254 (when (eq space :dynamic)
255 (loop with page-mask = #.(1- sb!vm:gencgc-page-size)
256 for addr of-type sb!vm:word = (sap-int current)
257 while (>= addr skip-tests-until-addr)
259 ;; For some reason binding PAGE with LET
260 ;; conses like mad (but gives no compiler notes...)
261 ;; Work around the problem with SYMBOL-MACROLET
262 ;; instead of trying to figure out the real
263 ;; issue. -- JES, 2005-05-17
264 (symbol-macrolet
265 ((page (deref page-table
266 (find-page-index addr))))
267 ;; Don't we have any nicer way to access C struct
268 ;; bitfields?
269 (let ((alloc-flag (ldb (byte 3 2)
270 (slot page 'flags)))
271 (bytes-used (slot page 'bytes-used)))
272 ;; If the page is not free and the current
273 ;; pointer is still below the allocation offset
274 ;; of the page
275 (when (and (not (zerop alloc-flag))
276 (<= (logand page-mask addr)
277 bytes-used))
278 ;; Don't bother testing again until we
279 ;; get past that allocation offset
280 (setf skip-tests-until-addr
281 (+ (logandc2 addr page-mask)
282 (the fixnum bytes-used)))
283 ;; And then continue with the scheduled
284 ;; mapping
285 (return-from maybe-skip-page))
286 ;; Move CURRENT to start of next page
287 (setf current (int-sap (+ (logandc2 addr page-mask)
288 sb!vm:gencgc-page-size)))
289 (maybe-finish-mapping)))))))
290 (declare (inline maybe-finish-mapping maybe-skip-page))
291 (loop
292 (maybe-finish-mapping)
293 (maybe-skip-page)
294 (let* ((header (sap-ref-word current 0))
295 (header-widetag (logand header #xFF))
296 (info (svref *room-info* header-widetag)))
297 (cond
298 ((or (not info)
299 (eq (room-info-kind info) :lowtag))
300 (let ((size (* cons-size n-word-bytes)))
301 (funcall fun
302 (%make-lisp-obj (logior (sap-int current)
303 list-pointer-lowtag))
304 list-pointer-lowtag
305 size)
306 (setq current (sap+ current size))))
307 ((eql header-widetag closure-header-widetag)
308 (let* ((obj (%make-lisp-obj (logior (sap-int current)
309 fun-pointer-lowtag)))
310 (size (round-to-dualword
311 (* (the fixnum (1+ (get-closure-length obj)))
312 n-word-bytes))))
313 (funcall fun obj header-widetag size)
314 (setq current (sap+ current size))))
315 ((eq (room-info-kind info) :instance)
316 (let* ((obj (%make-lisp-obj
317 (logior (sap-int current) instance-pointer-lowtag)))
318 (size (round-to-dualword
319 (* (+ (%instance-length obj) 1) n-word-bytes))))
320 (declare (fixnum size))
321 (funcall fun obj header-widetag size)
322 (aver (zerop (logand size lowtag-mask)))
323 (setq current (sap+ current size))))
325 (let* ((obj (%make-lisp-obj
326 (logior (sap-int current) other-pointer-lowtag)))
327 (size (ecase (room-info-kind info)
328 (:fixed
329 (aver (or (eql (room-info-length info)
330 (1+ (get-header-data obj)))
331 (floatp obj)
332 (simple-array-nil-p obj)))
333 (round-to-dualword
334 (* (room-info-length info) n-word-bytes)))
335 ((:vector :string)
336 (vector-total-size obj info))
337 (:header
338 (round-to-dualword
339 (* (1+ (get-header-data obj)) n-word-bytes)))
340 (:code
341 (+ (the fixnum
342 (* (get-header-data obj) n-word-bytes))
343 (round-to-dualword
344 (* (the fixnum (%code-code-size obj))
345 n-word-bytes)))))))
346 (declare (fixnum size))
347 (funcall fun obj header-widetag size)
348 (aver (zerop (logand size lowtag-mask)))
349 (setq current (sap+ current size))))))))))))
352 ;;;; MEMORY-USAGE
354 ;;; Return a list of 3-lists (bytes object type-name) for the objects
355 ;;; allocated in Space.
356 (defun type-breakdown (space)
357 (let ((sizes (make-array 256 :initial-element 0 :element-type 'fixnum))
358 (counts (make-array 256 :initial-element 0 :element-type 'fixnum)))
359 (map-allocated-objects
360 (lambda (obj type size)
361 (declare (fixnum size) (optimize (speed 3)) (ignore obj))
362 (incf (aref sizes type) size)
363 (incf (aref counts type)))
364 space)
366 (let ((totals (make-hash-table :test 'eq)))
367 (dotimes (i 256)
368 (let ((total-count (aref counts i)))
369 (unless (zerop total-count)
370 (let* ((total-size (aref sizes i))
371 (name (room-info-name (aref *room-info* i)))
372 (found (gethash name totals)))
373 (cond (found
374 (incf (first found) total-size)
375 (incf (second found) total-count))
377 (setf (gethash name totals)
378 (list total-size total-count name))))))))
380 (collect ((totals-list))
381 (maphash (lambda (k v)
382 (declare (ignore k))
383 (totals-list v))
384 totals)
385 (sort (totals-list) #'> :key #'first)))))
387 ;;; Handle the summary printing for MEMORY-USAGE. Totals is a list of lists
388 ;;; (space-name . totals-for-space), where totals-for-space is the list
389 ;;; returned by TYPE-BREAKDOWN.
390 (defun print-summary (spaces totals)
391 (let ((summary (make-hash-table :test 'eq)))
392 (dolist (space-total totals)
393 (dolist (total (cdr space-total))
394 (push (cons (car space-total) total)
395 (gethash (third total) summary))))
397 (collect ((summary-totals))
398 (maphash (lambda (k v)
399 (declare (ignore k))
400 (let ((sum 0))
401 (declare (fixnum sum))
402 (dolist (space-total v)
403 (incf sum (first (cdr space-total))))
404 (summary-totals (cons sum v))))
405 summary)
407 (format t "~2&Summary of spaces: ~(~{~A ~}~)~%" spaces)
408 (let ((summary-total-bytes 0)
409 (summary-total-objects 0))
410 (declare (fixnum summary-total-bytes summary-total-objects))
411 (dolist (space-totals
412 (mapcar #'cdr (sort (summary-totals) #'> :key #'car)))
413 (let ((total-objects 0)
414 (total-bytes 0)
415 name)
416 (declare (fixnum total-objects total-bytes))
417 (collect ((spaces))
418 (dolist (space-total space-totals)
419 (let ((total (cdr space-total)))
420 (setq name (third total))
421 (incf total-bytes (first total))
422 (incf total-objects (second total))
423 (spaces (cons (car space-total) (first total)))))
424 (format t "~%~A:~% ~:D bytes, ~:D object~:P"
425 name total-bytes total-objects)
426 (dolist (space (spaces))
427 (format t ", ~W% ~(~A~)"
428 (round (* (cdr space) 100) total-bytes)
429 (car space)))
430 (format t ".~%")
431 (incf summary-total-bytes total-bytes)
432 (incf summary-total-objects total-objects))))
433 (format t "~%Summary total:~% ~:D bytes, ~:D objects.~%"
434 summary-total-bytes summary-total-objects)))))
436 ;;; Report object usage for a single space.
437 (defun report-space-total (space-total cutoff)
438 (declare (list space-total) (type (or single-float null) cutoff))
439 (format t "~2&Breakdown for ~(~A~) space:~%" (car space-total))
440 (let* ((types (cdr space-total))
441 (total-bytes (reduce #'+ (mapcar #'first types)))
442 (total-objects (reduce #'+ (mapcar #'second types)))
443 (cutoff-point (if cutoff
444 (truncate (* (float total-bytes) cutoff))
446 (reported-bytes 0)
447 (reported-objects 0))
448 (declare (fixnum total-objects total-bytes cutoff-point reported-objects
449 reported-bytes))
450 (loop for (bytes objects name) in types do
451 (when (<= bytes cutoff-point)
452 (format t " ~10:D bytes for ~9:D other object~2:*~P.~%"
453 (- total-bytes reported-bytes)
454 (- total-objects reported-objects))
455 (return))
456 (incf reported-bytes bytes)
457 (incf reported-objects objects)
458 (format t " ~10:D bytes for ~9:D ~(~A~) object~2:*~P.~%"
459 bytes objects name))
460 (format t " ~10:D bytes for ~9:D ~(~A~) object~2:*~P (space total.)~%"
461 total-bytes total-objects (car space-total))))
463 ;;; Print information about the heap memory in use. PRINT-SPACES is a
464 ;;; list of the spaces to print detailed information for.
465 ;;; COUNT-SPACES is a list of the spaces to scan. For either one, T
466 ;;; means all spaces (i.e. :STATIC, :DYNAMIC and :READ-ONLY.) If
467 ;;; PRINT-SUMMARY is true, then summary information will be printed.
468 ;;; The defaults print only summary information for dynamic space. If
469 ;;; true, CUTOFF is a fraction of the usage in a report below which
470 ;;; types will be combined as OTHER.
471 (defun memory-usage (&key print-spaces (count-spaces '(:dynamic))
472 (print-summary t) cutoff)
473 (declare (type (or single-float null) cutoff))
474 (let* ((spaces (if (eq count-spaces t)
475 '(:static :dynamic :read-only)
476 count-spaces))
477 (totals (mapcar (lambda (space)
478 (cons space (type-breakdown space)))
479 spaces)))
481 (dolist (space-total totals)
482 (when (or (eq print-spaces t)
483 (member (car space-total) print-spaces))
484 (report-space-total space-total cutoff)))
486 (when print-summary (print-summary spaces totals)))
488 (values))
490 ;;; Print info about how much code and no-ops there are in SPACE.
491 (defun count-no-ops (space)
492 (declare (type spaces space))
493 (let ((code-words 0)
494 (no-ops 0)
495 (total-bytes 0))
496 (declare (fixnum code-words no-ops)
497 (type unsigned-byte total-bytes))
498 (map-allocated-objects
499 (lambda (obj type size)
500 (declare (fixnum size))
501 (when (eql type code-header-widetag)
502 (incf total-bytes size)
503 (let ((words (truly-the fixnum (%code-code-size obj)))
504 (sap (truly-the system-area-pointer
505 (%primitive code-instructions obj))))
506 (incf code-words words)
507 (dotimes (i words)
508 (when (zerop (sap-ref-word sap (* i n-word-bytes)))
509 (incf no-ops))))))
510 space)
512 (format t
513 "~:D code-object bytes, ~:D code words, with ~:D no-ops (~D%).~%"
514 total-bytes code-words no-ops
515 (round (* no-ops 100) code-words)))
517 (values))
519 (defun descriptor-vs-non-descriptor-storage (&rest spaces)
520 (let ((descriptor-words 0)
521 (non-descriptor-headers 0)
522 (non-descriptor-bytes 0))
523 (declare (type unsigned-byte descriptor-words non-descriptor-headers
524 non-descriptor-bytes))
525 (dolist (space (or spaces '(:read-only :static :dynamic)))
526 (declare (inline map-allocated-objects))
527 (map-allocated-objects
528 (lambda (obj type size)
529 (declare (fixnum size))
530 (case type
531 (#.code-header-widetag
532 (let ((inst-words (truly-the fixnum (%code-code-size obj))))
533 (declare (type fixnum inst-words))
534 (incf non-descriptor-bytes (* inst-words n-word-bytes))
535 (incf descriptor-words
536 (- (truncate size n-word-bytes) inst-words))))
537 ((#.bignum-widetag
538 #.single-float-widetag
539 #.double-float-widetag
540 #.simple-base-string-widetag
541 #!+sb-unicode #.simple-character-string-widetag
542 #.simple-array-nil-widetag
543 #.simple-bit-vector-widetag
544 #.simple-array-unsigned-byte-2-widetag
545 #.simple-array-unsigned-byte-4-widetag
546 #.simple-array-unsigned-byte-8-widetag
547 #.simple-array-unsigned-byte-16-widetag
548 #.simple-array-unsigned-byte-32-widetag
549 #.simple-array-signed-byte-8-widetag
550 #.simple-array-signed-byte-16-widetag
551 ; #.simple-array-signed-byte-30-widetag
552 #.simple-array-signed-byte-32-widetag
553 #.simple-array-single-float-widetag
554 #.simple-array-double-float-widetag
555 #.simple-array-complex-single-float-widetag
556 #.simple-array-complex-double-float-widetag)
557 (incf non-descriptor-headers)
558 (incf non-descriptor-bytes (- size n-word-bytes)))
559 ((#.list-pointer-lowtag
560 #.instance-pointer-lowtag
561 #.ratio-widetag
562 #.complex-widetag
563 #.simple-array-widetag
564 #.simple-vector-widetag
565 #.complex-base-string-widetag
566 #.complex-vector-nil-widetag
567 #.complex-bit-vector-widetag
568 #.complex-vector-widetag
569 #.complex-array-widetag
570 #.closure-header-widetag
571 #.funcallable-instance-header-widetag
572 #.value-cell-header-widetag
573 #.symbol-header-widetag
574 #.sap-widetag
575 #.weak-pointer-widetag
576 #.instance-header-widetag)
577 (incf descriptor-words (truncate size n-word-bytes)))
579 (error "bogus widetag: ~W" type))))
580 space))
581 (format t "~:D words allocated for descriptor objects.~%"
582 descriptor-words)
583 (format t "~:D bytes data/~:D words header for non-descriptor objects.~%"
584 non-descriptor-bytes non-descriptor-headers)
585 (values)))
587 ;;; Print a breakdown by instance type of all the instances allocated
588 ;;; in SPACE. If TOP-N is true, print only information for the
589 ;;; TOP-N types with largest usage.
590 (defun instance-usage (space &key (top-n 15))
591 (declare (type spaces space) (type (or fixnum null) top-n))
592 (format t "~2&~@[Top ~W ~]~(~A~) instance types:~%" top-n space)
593 (let ((totals (make-hash-table :test 'eq))
594 (total-objects 0)
595 (total-bytes 0))
596 (declare (fixnum total-objects total-bytes))
597 (map-allocated-objects
598 (lambda (obj type size)
599 (declare (fixnum size) (optimize (speed 3)))
600 (when (eql type instance-header-widetag)
601 (incf total-objects)
602 (incf total-bytes size)
603 (let* ((classoid (layout-classoid (%instance-ref obj 0)))
604 (found (gethash classoid totals)))
605 (cond (found
606 (incf (the fixnum (car found)))
607 (incf (the fixnum (cdr found)) size))
609 (setf (gethash classoid totals) (cons 1 size)))))))
610 space)
612 (collect ((totals-list))
613 (maphash (lambda (classoid what)
614 (totals-list (cons (prin1-to-string
615 (classoid-proper-name classoid))
616 what)))
617 totals)
618 (let ((sorted (sort (totals-list) #'> :key #'cddr))
619 (printed-bytes 0)
620 (printed-objects 0))
621 (declare (fixnum printed-bytes printed-objects))
622 (dolist (what (if top-n
623 (subseq sorted 0 (min (length sorted) top-n))
624 sorted))
625 (let ((bytes (cddr what))
626 (objects (cadr what)))
627 (incf printed-bytes bytes)
628 (incf printed-objects objects)
629 (format t " ~A: ~:D bytes, ~:D object~:P.~%" (car what)
630 bytes objects)))
632 (let ((residual-objects (- total-objects printed-objects))
633 (residual-bytes (- total-bytes printed-bytes)))
634 (unless (zerop residual-objects)
635 (format t " Other types: ~:D bytes, ~:D object~:P.~%"
636 residual-bytes residual-objects))))
638 (format t " ~:(~A~) instance total: ~:D bytes, ~:D object~:P.~%"
639 space total-bytes total-objects)))
641 (values))
643 ;;;; PRINT-ALLOCATED-OBJECTS
645 (defun print-allocated-objects (space &key (percent 0) (pages 5)
646 type larger smaller count
647 (stream *standard-output*))
648 (declare (type (integer 0 99) percent) (type index pages)
649 (type stream stream) (type spaces space)
650 (type (or index null) type larger smaller count))
651 (multiple-value-bind (start-sap end-sap) (space-bounds space)
652 (let* ((space-start (sap-int start-sap))
653 (space-end (sap-int end-sap))
654 (space-size (- space-end space-start))
655 (pagesize (sb!sys:get-page-size))
656 (start (+ space-start (round (* space-size percent) 100)))
657 (printed-conses (make-hash-table :test 'eq))
658 (pages-so-far 0)
659 (count-so-far 0)
660 (last-page 0))
661 (declare (type (unsigned-byte 32) last-page start)
662 (fixnum pages-so-far count-so-far pagesize))
663 (labels ((note-conses (x)
664 (unless (or (atom x) (gethash x printed-conses))
665 (setf (gethash x printed-conses) t)
666 (note-conses (car x))
667 (note-conses (cdr x)))))
668 (map-allocated-objects
669 (lambda (obj obj-type size)
670 (let ((addr (get-lisp-obj-address obj)))
671 (when (>= addr start)
672 (when (if count
673 (> count-so-far count)
674 (> pages-so-far pages))
675 (return-from print-allocated-objects (values)))
677 (unless count
678 (let ((this-page (* (the (values (unsigned-byte 32) t)
679 (truncate addr pagesize))
680 pagesize)))
681 (declare (type (unsigned-byte 32) this-page))
682 (when (/= this-page last-page)
683 (when (< pages-so-far pages)
684 ;; FIXME: What is this? (ERROR "Argh..")? or
685 ;; a warning? or code that can be removed
686 ;; once the system is stable? or what?
687 (format stream "~2&**** Page ~W, address ~X:~%"
688 pages-so-far addr))
689 (setq last-page this-page)
690 (incf pages-so-far))))
692 (when (and (or (not type) (eql obj-type type))
693 (or (not smaller) (<= size smaller))
694 (or (not larger) (>= size larger)))
695 (incf count-so-far)
696 (case type
697 (#.code-header-widetag
698 (let ((dinfo (%code-debug-info obj)))
699 (format stream "~&Code object: ~S~%"
700 (if dinfo
701 (sb!c::compiled-debug-info-name dinfo)
702 "No debug info."))))
703 (#.symbol-header-widetag
704 (format stream "~&~S~%" obj))
705 (#.list-pointer-lowtag
706 (unless (gethash obj printed-conses)
707 (note-conses obj)
708 (let ((*print-circle* t)
709 (*print-level* 5)
710 (*print-length* 10))
711 (format stream "~&~S~%" obj))))
713 (fresh-line stream)
714 (let ((str (write-to-string obj :level 5 :length 10
715 :pretty nil)))
716 (unless (eql type instance-header-widetag)
717 (format stream "~S: " (type-of obj)))
718 (format stream "~A~%"
719 (subseq str 0 (min (length str) 60))))))))))
720 space))))
721 (values))
723 ;;;; LIST-ALLOCATED-OBJECTS, LIST-REFERENCING-OBJECTS
725 (defvar *ignore-after* nil)
727 (defun valid-obj (space x)
728 (or (not (eq space :dynamic))
729 ;; this test looks bogus if the allocator doesn't work linearly,
730 ;; which I suspect is the case for GENCGC. -- CSR, 2004-06-29
731 (< (get-lisp-obj-address x) (get-lisp-obj-address *ignore-after*))))
733 (defun maybe-cons (space x stuff)
734 (if (valid-obj space x)
735 (cons x stuff)
736 stuff))
738 (defun list-allocated-objects (space &key type larger smaller count
739 test)
740 (declare (type spaces space)
741 (type (or index null) larger smaller type count)
742 (type (or function null) test)
743 (inline map-allocated-objects))
744 (unless *ignore-after*
745 (setq *ignore-after* (cons 1 2)))
746 (collect ((counted 0 1+))
747 (let ((res ()))
748 (map-allocated-objects
749 (lambda (obj obj-type size)
750 (when (and (or (not type) (eql obj-type type))
751 (or (not smaller) (<= size smaller))
752 (or (not larger) (>= size larger))
753 (or (not test) (funcall test obj)))
754 (setq res (maybe-cons space obj res))
755 (when (and count (>= (counted) count))
756 (return-from list-allocated-objects res))))
757 space)
758 res)))
760 (defun map-referencing-objects (fun space object)
761 (declare (type spaces space) (inline map-allocated-objects))
762 (unless *ignore-after*
763 (setq *ignore-after* (cons 1 2)))
764 (flet ((maybe-call (fun obj)
765 (when (valid-obj space obj)
766 (funcall fun obj))))
767 (map-allocated-objects
768 (lambda (obj obj-type size)
769 (declare (ignore obj-type size))
770 (typecase obj
771 (cons
772 (when (or (eq (car obj) object)
773 (eq (cdr obj) object))
774 (maybe-call fun obj)))
775 (instance
776 (dotimes (i (%instance-length obj))
777 (when (eq (%instance-ref obj i) object)
778 (maybe-call fun obj)
779 (return))))
780 (code-component
781 (let ((length (get-header-data obj)))
782 (do ((i code-constants-offset (1+ i)))
783 ((= i length))
784 (when (eq (code-header-ref obj i) object)
785 (maybe-call fun obj)
786 (return)))))
787 (simple-vector
788 (dotimes (i (length obj))
789 (when (eq (svref obj i) object)
790 (maybe-call fun obj)
791 (return))))
792 (symbol
793 (when (or (eq (symbol-name obj) object)
794 (eq (symbol-package obj) object)
795 (eq (symbol-plist obj) object)
796 (and (boundp obj)
797 (eq (symbol-value obj) object)))
798 (maybe-call fun obj)))))
799 space)))
801 (defun list-referencing-objects (space object)
802 (collect ((res))
803 (map-referencing-objects
804 (lambda (obj) (res obj)) space object)
805 (res)))