0.8.18.14:
[sbcl.git] / src / code / room.lisp
1 ;;;; heap-grovelling memory usage stuff
2
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.
11
12 (in-package "SB!VM")
13 \f
14 ;;;; type format database
15
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))))
26
27 (eval-when (:compile-toplevel :execute)
28
29 (defvar *meta-room-info* (make-array 256 :initial-element nil))
30
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)
41       ;;
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.]
47       ;;
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.
53       ;;
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)
69      (t
70       (setf (svref *meta-room-info* (symbol-value widetag))
71             (make-room-info :name name
72                             :kind :fixed
73                             :length size))))))
74
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)))
82
83 (setf (svref *meta-room-info* bignum-widetag)
84       (make-room-info :name 'bignum
85                       :kind :header))
86
87 (setf (svref *meta-room-info* closure-header-widetag)
88       (make-room-info :name 'closure
89                       :kind :closure))
90
91 (dolist (stuff '((simple-bit-vector-widetag . -3)
92                  (simple-vector-widetag . 2)
93                  (simple-array-unsigned-byte-2-widetag . -2)
94                  (simple-array-unsigned-byte-4-widetag . -1)
95                  (simple-array-unsigned-byte-7-widetag . 0)
96                  (simple-array-unsigned-byte-8-widetag . 0)
97                  (simple-array-unsigned-byte-15-widetag . 1)
98                  (simple-array-unsigned-byte-16-widetag . 1)
99                  (simple-array-unsigned-byte-31-widetag . 2)
100                  (simple-array-unsigned-byte-32-widetag . 2)
101                  (simple-array-unsigned-byte-60-widetag . 3)
102                  (simple-array-unsigned-byte-63-widetag . 3)
103                  (simple-array-unsigned-byte-64-widetag . 3)
104                  (simple-array-signed-byte-8-widetag . 0)
105                  (simple-array-signed-byte-16-widetag . 1)
106                  (simple-array-unsigned-byte-29-widetag . 2)
107                  (simple-array-signed-byte-30-widetag . 2)
108                  (simple-array-signed-byte-32-widetag . 2)
109                  (simple-array-signed-byte-61-widetag . 3)
110                  (simple-array-signed-byte-64-widetag . 3)
111                  (simple-array-single-float-widetag . 2)
112                  (simple-array-double-float-widetag . 3)
113                  (simple-array-complex-single-float-widetag . 3)
114                  (simple-array-complex-double-float-widetag . 4)))
115   (let* ((name (car stuff))
116          (size (cdr stuff))
117          (sname (string name)))
118     (when (boundp name)
119       (setf (svref *meta-room-info* (symbol-value name))
120             (make-room-info :name (intern (subseq sname
121                                                   0
122                                                   (mismatch sname "-WIDETAG"
123                                                             :from-end t)))
124                             :kind :vector
125                             :length size)))))
126
127 (setf (svref *meta-room-info* simple-base-string-widetag)
128       (make-room-info :name 'simple-base-string
129                       :kind :string
130                       :length 0))
131
132 #!+sb-unicode
133 (setf (svref *meta-room-info* simple-character-string-widetag)
134       (make-room-info :name 'simple-character-string
135                       :kind :string
136                       :length 2))
137
138 (setf (svref *meta-room-info* simple-array-nil-widetag)
139       (make-room-info :name 'simple-array-nil
140                       :kind :fixed
141                       :length 2))
142
143 (setf (svref *meta-room-info* code-header-widetag)
144       (make-room-info :name 'code
145                       :kind :code))
146
147 (setf (svref *meta-room-info* instance-header-widetag)
148       (make-room-info :name 'instance
149                       :kind :instance))
150
151 ) ; EVAL-WHEN
152
153 (defparameter *room-info* '#.*meta-room-info*)
154 (deftype spaces () '(member :static :dynamic :read-only))
155 \f
156 ;;;; MAP-ALLOCATED-OBJECTS
157
158 ;;; Since they're represented as counts of words, we should never
159 ;;; need bignums to represent these:
160 (declaim (type fixnum
161                *static-space-free-pointer*
162                *read-only-space-free-pointer*))
163
164 (defun space-bounds (space)
165   (declare (type spaces space))
166   (ecase space
167     (:static
168      (values (int-sap static-space-start)
169              (int-sap (* *static-space-free-pointer* n-word-bytes))))
170     (:read-only
171      (values (int-sap read-only-space-start)
172              (int-sap (* *read-only-space-free-pointer* n-word-bytes))))
173     (:dynamic
174      (values (int-sap #!+gencgc dynamic-space-start 
175                       #!-gencgc (current-dynamic-space-start))
176              (dynamic-space-free-pointer)))))
177
178 ;;; Return the total number of bytes used in SPACE.
179 (defun space-bytes (space)
180   (multiple-value-bind (start end) (space-bounds space)
181     (- (sap-int end) (sap-int start))))
182
183 ;;; Round SIZE (in bytes) up to the next dualword (eight byte) boundary.
184 #!-sb-fluid (declaim (inline round-to-dualword))
185 (defun round-to-dualword (size)
186   (declare (fixnum size))
187   (logand (the fixnum (+ size lowtag-mask)) (lognot lowtag-mask)))
188
189 ;;; Return the total size of a vector in bytes, including any pad.
190 #!-sb-fluid (declaim (inline vector-total-size))
191 (defun vector-total-size (obj info)
192   (let ((shift (room-info-length info))
193         (len (+ (length (the (simple-array * (*)) obj))
194                 (ecase (room-info-kind info)
195                   (:vector 0)
196                   (:string 1)))))
197     (declare (type (integer -3 3) shift))
198     (round-to-dualword
199      (+ (* vector-data-offset n-word-bytes)
200         (the fixnum
201              (if (minusp shift)
202                  (ash (the fixnum
203                            (+ len (the fixnum
204                                        (1- (the fixnum (ash 1 (- shift)))))))
205                       shift)
206                  (ash len shift)))))))
207
208 ;;; Iterate over all the objects allocated in SPACE, calling FUN with
209 ;;; the object, the object's type code, and the object's total size in
210 ;;; bytes, including any header and padding.
211 #!-sb-fluid (declaim (maybe-inline map-allocated-objects))
212 (defun map-allocated-objects (fun space)
213   (declare (type function fun) (type spaces space))
214   (without-gcing
215     (multiple-value-bind (start end) (space-bounds space)
216       (declare (type system-area-pointer start end))
217       (declare (optimize (speed 3) (safety 0)))
218       (let ((current start)
219             #+nil
220             (prev nil))
221         (loop
222           (let* ((header (sap-ref-word current 0))
223                  (header-widetag (logand header #xFF))
224                  (info (svref *room-info* header-widetag)))
225             (cond
226              ((or (not info)
227                   (eq (room-info-kind info) :lowtag))
228               (let ((size (* cons-size n-word-bytes)))
229                 (funcall fun
230                          (make-lisp-obj (logior (sap-int current)
231                                                 list-pointer-lowtag))
232                          list-pointer-lowtag
233                          size)
234                 (setq current (sap+ current size))))
235              ((eql header-widetag closure-header-widetag)
236               (let* ((obj (make-lisp-obj (logior (sap-int current)
237                                                  fun-pointer-lowtag)))
238                      (size (round-to-dualword
239                             (* (the fixnum (1+ (get-closure-length obj)))
240                                n-word-bytes))))
241                 (funcall fun obj header-widetag size)
242                 (setq current (sap+ current size))))
243              ((eq (room-info-kind info) :instance)
244               (let* ((obj (make-lisp-obj
245                            (logior (sap-int current) instance-pointer-lowtag)))
246                      (size (round-to-dualword
247                             (* (+ (%instance-length obj) 1) n-word-bytes))))
248                 (declare (fixnum size))
249                 (funcall fun obj header-widetag size)
250                 (aver (zerop (logand size lowtag-mask)))
251                 #+nil
252                 (when (> size 200000) (break "implausible size, prev ~S" prev))
253                 #+nil
254                 (setq prev current)
255                 (setq current (sap+ current size))))
256              (t
257               (let* ((obj (make-lisp-obj
258                            (logior (sap-int current) other-pointer-lowtag)))
259                      (size (ecase (room-info-kind info)
260                              (:fixed
261                               (aver (or (eql (room-info-length info)
262                                                (1+ (get-header-data obj)))
263                                         (floatp obj)
264                                         (simple-array-nil-p obj)))
265                               (round-to-dualword
266                                (* (room-info-length info) n-word-bytes)))
267                              ((:vector :string)
268                               (vector-total-size obj info))
269                              (:header
270                               (round-to-dualword
271                                (* (1+ (get-header-data obj)) n-word-bytes)))
272                              (:code
273                               (+ (the fixnum
274                                       (* (get-header-data obj) n-word-bytes))
275                                  (round-to-dualword
276                                   (* (the fixnum (%code-code-size obj))
277                                      n-word-bytes)))))))
278                 (declare (fixnum size))
279                 (funcall fun obj header-widetag size)
280                 (aver (zerop (logand size lowtag-mask)))
281                 #+nil
282                 (when (> size 200000)
283                   (break "Implausible size, prev ~S" prev))
284                 #+nil
285                 (setq prev current)
286                 (setq current (sap+ current size))))))
287           (unless (sap< current end)
288             (aver (sap= current end))
289             (return)))
290
291         #+nil
292         prev))))
293 \f
294 ;;;; MEMORY-USAGE
295
296 ;;; Return a list of 3-lists (bytes object type-name) for the objects
297 ;;; allocated in Space.
298 (defun type-breakdown (space)
299   (let ((sizes (make-array 256 :initial-element 0 :element-type 'fixnum))
300         (counts (make-array 256 :initial-element 0 :element-type 'fixnum)))
301     (map-allocated-objects
302      (lambda (obj type size)
303        (declare (fixnum size) (optimize (speed 3) (safety 0)) (ignore obj))
304        (incf (aref sizes type) size)
305        (incf (aref counts type)))
306      space)
307
308     (let ((totals (make-hash-table :test 'eq)))
309       (dotimes (i 256)
310         (let ((total-count (aref counts i)))
311           (unless (zerop total-count)
312             (let* ((total-size (aref sizes i))
313                    (name (room-info-name (aref *room-info* i)))
314                    (found (gethash name totals)))
315               (cond (found
316                      (incf (first found) total-size)
317                      (incf (second found) total-count))
318                     (t
319                      (setf (gethash name totals)
320                            (list total-size total-count name))))))))
321
322       (collect ((totals-list))
323         (maphash (lambda (k v)
324                    (declare (ignore k))
325                    (totals-list v))
326                  totals)
327         (sort (totals-list) #'> :key #'first)))))
328
329 ;;; Handle the summary printing for MEMORY-USAGE. Totals is a list of lists
330 ;;; (space-name . totals-for-space), where totals-for-space is the list
331 ;;; returned by TYPE-BREAKDOWN.
332 (defun print-summary (spaces totals)
333   (let ((summary (make-hash-table :test 'eq)))
334     (dolist (space-total totals)
335       (dolist (total (cdr space-total))
336         (push (cons (car space-total) total)
337               (gethash (third total) summary))))
338
339     (collect ((summary-totals))
340       (maphash (lambda (k v)
341                  (declare (ignore k))
342                  (let ((sum 0))
343                    (declare (fixnum sum))
344                    (dolist (space-total v)
345                      (incf sum (first (cdr space-total))))
346                    (summary-totals (cons sum v))))
347                summary)
348
349       (format t "~2&Summary of spaces: ~(~{~A ~}~)~%" spaces)
350       (let ((summary-total-bytes 0)
351             (summary-total-objects 0))
352         (declare (fixnum summary-total-bytes summary-total-objects))
353         (dolist (space-totals
354                  (mapcar #'cdr (sort (summary-totals) #'> :key #'car)))
355           (let ((total-objects 0)
356                 (total-bytes 0)
357                 name)
358             (declare (fixnum total-objects total-bytes))
359             (collect ((spaces))
360               (dolist (space-total space-totals)
361                 (let ((total (cdr space-total)))
362                   (setq name (third total))
363                   (incf total-bytes (first total))
364                   (incf total-objects (second total))
365                   (spaces (cons (car space-total) (first total)))))
366               (format t "~%~A:~%    ~:D bytes, ~:D object~:P"
367                       name total-bytes total-objects)
368               (dolist (space (spaces))
369                 (format t ", ~W% ~(~A~)"
370                         (round (* (cdr space) 100) total-bytes)
371                         (car space)))
372               (format t ".~%")
373               (incf summary-total-bytes total-bytes)
374               (incf summary-total-objects total-objects))))
375         (format t "~%Summary total:~%    ~:D bytes, ~:D objects.~%"
376                 summary-total-bytes summary-total-objects)))))
377
378 ;;; Report object usage for a single space.
379 (defun report-space-total (space-total cutoff)
380   (declare (list space-total) (type (or single-float null) cutoff))
381   (format t "~2&Breakdown for ~(~A~) space:~%" (car space-total))
382   (let* ((types (cdr space-total))
383          (total-bytes (reduce #'+ (mapcar #'first types)))
384          (total-objects (reduce #'+ (mapcar #'second types)))
385          (cutoff-point (if cutoff
386                            (truncate (* (float total-bytes) cutoff))
387                            0))
388          (reported-bytes 0)
389          (reported-objects 0))
390     (declare (fixnum total-objects total-bytes cutoff-point reported-objects
391                      reported-bytes))
392     (loop for (bytes objects name) in types do
393       (when (<= bytes cutoff-point)
394         (format t "  ~10:D bytes for ~9:D other object~2:*~P.~%"
395                 (- total-bytes reported-bytes)
396                 (- total-objects reported-objects))
397         (return))
398       (incf reported-bytes bytes)
399       (incf reported-objects objects)
400       (format t "  ~10:D bytes for ~9:D ~(~A~) object~2:*~P.~%"
401               bytes objects name))
402     (format t "  ~10:D bytes for ~9:D ~(~A~) object~2:*~P (space total.)~%"
403             total-bytes total-objects (car space-total))))
404
405 ;;; Print information about the heap memory in use. PRINT-SPACES is a
406 ;;; list of the spaces to print detailed information for.
407 ;;; COUNT-SPACES is a list of the spaces to scan. For either one, T
408 ;;; means all spaces (i.e. :STATIC, :DYNAMIC and :READ-ONLY.) If
409 ;;; PRINT-SUMMARY is true, then summary information will be printed.
410 ;;; The defaults print only summary information for dynamic space. If
411 ;;; true, CUTOFF is a fraction of the usage in a report below which
412 ;;; types will be combined as OTHER.
413 (defun memory-usage (&key print-spaces (count-spaces '(:dynamic))
414                           (print-summary t) cutoff)
415   (declare (type (or single-float null) cutoff))
416   (let* ((spaces (if (eq count-spaces t)
417                      '(:static :dynamic :read-only)
418                      count-spaces))
419          (totals (mapcar (lambda (space)
420                            (cons space (type-breakdown space)))
421                          spaces)))
422
423     (dolist (space-total totals)
424       (when (or (eq print-spaces t)
425                 (member (car space-total) print-spaces))
426         (report-space-total space-total cutoff)))
427
428     (when print-summary (print-summary spaces totals)))
429
430   (values))
431 \f
432 ;;; Print info about how much code and no-ops there are in SPACE.
433 (defun count-no-ops (space)
434   (declare (type spaces space))
435   (let ((code-words 0)
436         (no-ops 0)
437         (total-bytes 0))
438     (declare (fixnum code-words no-ops)
439              (type unsigned-byte total-bytes))
440     (map-allocated-objects
441      (lambda (obj type size)
442        (declare (fixnum size) (optimize (safety 0)))
443        (when (eql type code-header-widetag)
444          (incf total-bytes size)
445          (let ((words (truly-the fixnum (%code-code-size obj)))
446                (sap (truly-the system-area-pointer
447                                (%primitive code-instructions obj))))
448            (incf code-words words)
449            (dotimes (i words)
450              (when (zerop (sap-ref-word sap (* i n-word-bytes)))
451                (incf no-ops))))))
452      space)
453
454     (format t
455             "~:D code-object bytes, ~:D code words, with ~:D no-ops (~D%).~%"
456             total-bytes code-words no-ops
457             (round (* no-ops 100) code-words)))
458
459   (values))
460 \f
461 (defun descriptor-vs-non-descriptor-storage (&rest spaces)
462   (let ((descriptor-words 0)
463         (non-descriptor-headers 0)
464         (non-descriptor-bytes 0))
465     (declare (type unsigned-byte descriptor-words non-descriptor-headers
466                    non-descriptor-bytes))
467     (dolist (space (or spaces '(:read-only :static :dynamic)))
468       (declare (inline map-allocated-objects))
469       (map-allocated-objects
470        (lambda (obj type size)
471          (declare (fixnum size) (optimize (safety 0)))
472          (case type
473            (#.code-header-widetag
474             (let ((inst-words (truly-the fixnum (%code-code-size obj))))
475               (declare (type fixnum inst-words))
476               (incf non-descriptor-bytes (* inst-words n-word-bytes))
477               (incf descriptor-words
478                     (- (truncate size n-word-bytes) inst-words))))
479            ((#.bignum-widetag
480              #.single-float-widetag
481              #.double-float-widetag
482              #.simple-base-string-widetag
483              #!+sb-unicode #.simple-character-string-widetag
484              #.simple-array-nil-widetag
485              #.simple-bit-vector-widetag
486              #.simple-array-unsigned-byte-2-widetag
487              #.simple-array-unsigned-byte-4-widetag
488              #.simple-array-unsigned-byte-8-widetag
489              #.simple-array-unsigned-byte-16-widetag
490              #.simple-array-unsigned-byte-32-widetag
491              #.simple-array-signed-byte-8-widetag
492              #.simple-array-signed-byte-16-widetag
493              ; #.simple-array-signed-byte-30-widetag
494              #.simple-array-signed-byte-32-widetag
495              #.simple-array-single-float-widetag
496              #.simple-array-double-float-widetag
497              #.simple-array-complex-single-float-widetag
498              #.simple-array-complex-double-float-widetag)
499             (incf non-descriptor-headers)
500             (incf non-descriptor-bytes (- size n-word-bytes)))
501            ((#.list-pointer-lowtag
502              #.instance-pointer-lowtag
503              #.ratio-widetag
504              #.complex-widetag
505              #.simple-array-widetag
506              #.simple-vector-widetag
507              #.complex-base-string-widetag
508              #.complex-vector-nil-widetag
509              #.complex-bit-vector-widetag
510              #.complex-vector-widetag
511              #.complex-array-widetag
512              #.closure-header-widetag
513              #.funcallable-instance-header-widetag
514              #.value-cell-header-widetag
515              #.symbol-header-widetag
516              #.sap-widetag
517              #.weak-pointer-widetag
518              #.instance-header-widetag)
519             (incf descriptor-words (truncate size n-word-bytes)))
520            (t
521             (error "bogus widetag: ~W" type))))
522        space))
523     (format t "~:D words allocated for descriptor objects.~%"
524             descriptor-words)
525     (format t "~:D bytes data/~:D words header for non-descriptor objects.~%"
526             non-descriptor-bytes non-descriptor-headers)
527     (values)))
528 \f
529 ;;; Print a breakdown by instance type of all the instances allocated
530 ;;; in SPACE. If TOP-N is true, print only information for the 
531 ;;; TOP-N types with largest usage.
532 (defun instance-usage (space &key (top-n 15))
533   (declare (type spaces space) (type (or fixnum null) top-n))
534   (format t "~2&~@[Top ~W ~]~(~A~) instance types:~%" top-n space)
535   (let ((totals (make-hash-table :test 'eq))
536         (total-objects 0)
537         (total-bytes 0))
538     (declare (fixnum total-objects total-bytes))
539     (map-allocated-objects
540      (lambda (obj type size)
541        (declare (fixnum size) (optimize (speed 3) (safety 0)))
542        (when (eql type instance-header-widetag)
543          (incf total-objects)
544          (incf total-bytes size)
545          (let* ((classoid (layout-classoid (%instance-ref obj 0)))
546                 (found (gethash classoid totals)))
547            (cond (found
548                   (incf (the fixnum (car found)))
549                   (incf (the fixnum (cdr found)) size))
550                  (t
551                   (setf (gethash classoid totals) (cons 1 size)))))))
552      space)
553
554     (collect ((totals-list))
555       (maphash (lambda (classoid what)
556                  (totals-list (cons (prin1-to-string
557                                      (classoid-proper-name classoid))
558                                     what)))
559                totals)
560       (let ((sorted (sort (totals-list) #'> :key #'cddr))
561             (printed-bytes 0)
562             (printed-objects 0))
563         (declare (fixnum printed-bytes printed-objects))
564         (dolist (what (if top-n
565                           (subseq sorted 0 (min (length sorted) top-n))
566                           sorted))
567           (let ((bytes (cddr what))
568                 (objects (cadr what)))
569             (incf printed-bytes bytes)
570             (incf printed-objects objects)
571             (format t "  ~A: ~:D bytes, ~:D object~:P.~%" (car what)
572                     bytes objects)))
573
574         (let ((residual-objects (- total-objects printed-objects))
575               (residual-bytes (- total-bytes printed-bytes)))
576           (unless (zerop residual-objects)
577             (format t "  Other types: ~:D bytes, ~:D object~:P.~%"
578                     residual-bytes residual-objects))))
579
580       (format t "  ~:(~A~) instance total: ~:D bytes, ~:D object~:P.~%"
581               space total-bytes total-objects)))
582
583   (values))
584 \f
585 ;;;; PRINT-ALLOCATED-OBJECTS
586
587 (defun print-allocated-objects (space &key (percent 0) (pages 5)
588                                       type larger smaller count
589                                       (stream *standard-output*))
590   (declare (type (integer 0 99) percent) (type index pages)
591            (type stream stream) (type spaces space)
592            (type (or index null) type larger smaller count))
593   (multiple-value-bind (start-sap end-sap) (space-bounds space)
594     (let* ((space-start (sap-int start-sap))
595            (space-end (sap-int end-sap))
596            (space-size (- space-end space-start))
597            (pagesize (sb!sys:get-page-size))
598            (start (+ space-start (round (* space-size percent) 100)))
599            (printed-conses (make-hash-table :test 'eq))
600            (pages-so-far 0)
601            (count-so-far 0)
602            (last-page 0))
603       (declare (type (unsigned-byte 32) last-page start)
604                (fixnum pages-so-far count-so-far pagesize))
605       (labels ((note-conses (x)
606                  (unless (or (atom x) (gethash x printed-conses))
607                    (setf (gethash x printed-conses) t)
608                    (note-conses (car x))
609                    (note-conses (cdr x)))))
610         (map-allocated-objects
611          (lambda (obj obj-type size)
612            (declare (optimize (safety 0)))
613            (let ((addr (get-lisp-obj-address obj)))
614              (when (>= addr start)
615                (when (if count
616                          (> count-so-far count)
617                          (> pages-so-far pages))
618                  (return-from print-allocated-objects (values)))
619
620                (unless count
621                  (let ((this-page (* (the (values (unsigned-byte 32) t)
622                                        (truncate addr pagesize))
623                                      pagesize)))
624                    (declare (type (unsigned-byte 32) this-page))
625                    (when (/= this-page last-page)
626                      (when (< pages-so-far pages)
627                        ;; FIXME: What is this? (ERROR "Argh..")? or
628                        ;; a warning? or code that can be removed
629                        ;; once the system is stable? or what?
630                        (format stream "~2&**** Page ~W, address ~X:~%"
631                                pages-so-far addr))
632                      (setq last-page this-page)
633                      (incf pages-so-far))))
634
635                (when (and (or (not type) (eql obj-type type))
636                           (or (not smaller) (<= size smaller))
637                           (or (not larger) (>= size larger)))
638                  (incf count-so-far)
639                  (case type
640                    (#.code-header-widetag
641                     (let ((dinfo (%code-debug-info obj)))
642                       (format stream "~&Code object: ~S~%"
643                               (if dinfo
644                                   (sb!c::compiled-debug-info-name dinfo)
645                                   "No debug info."))))
646                    (#.symbol-header-widetag
647                     (format stream "~&~S~%" obj))
648                    (#.list-pointer-lowtag
649                     (unless (gethash obj printed-conses)
650                       (note-conses obj)
651                       (let ((*print-circle* t)
652                             (*print-level* 5)
653                             (*print-length* 10))
654                         (format stream "~&~S~%" obj))))
655                    (t
656                     (fresh-line stream)
657                     (let ((str (write-to-string obj :level 5 :length 10
658                                                 :pretty nil)))
659                       (unless (eql type instance-header-widetag)
660                         (format stream "~S: " (type-of obj)))
661                       (format stream "~A~%"
662                               (subseq str 0 (min (length str) 60))))))))))
663          space))))
664   (values))
665 \f
666 ;;;; LIST-ALLOCATED-OBJECTS, LIST-REFERENCING-OBJECTS
667
668 (defvar *ignore-after* nil)
669
670 (defun valid-obj (space x)
671   (or (not (eq space :dynamic))
672       ;; this test looks bogus if the allocator doesn't work linearly,
673       ;; which I suspect is the case for GENCGC.  -- CSR, 2004-06-29
674       (< (get-lisp-obj-address x) (get-lisp-obj-address *ignore-after*))))
675
676 (defun maybe-cons (space x stuff)
677   (if (valid-obj space x)
678       (cons x stuff)
679       stuff))
680
681 (defun list-allocated-objects (space &key type larger smaller count
682                                      test)
683   (declare (type spaces space)
684            (type (or index null) larger smaller type count)
685            (type (or function null) test)
686            (inline map-allocated-objects))
687   (unless *ignore-after*
688     (setq *ignore-after* (cons 1 2)))
689   (collect ((counted 0 1+))
690     (let ((res ()))
691       (map-allocated-objects
692        (lambda (obj obj-type size)
693          (declare (optimize (safety 0)))
694          (when (and (or (not type) (eql obj-type type))
695                     (or (not smaller) (<= size smaller))
696                     (or (not larger) (>= size larger))
697                     (or (not test) (funcall test obj)))
698            (setq res (maybe-cons space obj res))
699            (when (and count (>= (counted) count))
700              (return-from list-allocated-objects res))))
701        space)
702       res)))
703
704 (defun map-referencing-objects (fun space object)
705   (declare (type spaces space) (inline map-allocated-objects))
706   (unless *ignore-after*
707     (setq *ignore-after* (cons 1 2)))
708   (flet ((maybe-call (fun obj)
709            (when (valid-obj space obj)
710              (funcall fun obj))))
711     (map-allocated-objects
712      (lambda (obj obj-type size)
713        (declare (optimize (safety 0)) (ignore obj-type size))
714        (typecase obj
715          (cons
716           (when (or (eq (car obj) object)
717                     (eq (cdr obj) object))
718             (maybe-call fun obj)))
719          (instance
720           (dotimes (i (%instance-length obj))
721             (when (eq (%instance-ref obj i) object)
722               (maybe-call fun obj)
723               (return))))
724          (code-component
725           (let ((length (get-header-data obj)))
726             (do ((i code-constants-offset (1+ i)))
727                 ((= i length))
728               (when (eq (code-header-ref obj i) object)
729                 (maybe-call fun obj)
730                 (return)))))
731          (simple-vector
732           (dotimes (i (length obj))
733             (when (eq (svref obj i) object)
734               (maybe-call fun obj)
735               (return))))
736          (symbol
737           (when (or (eq (symbol-name obj) object)
738                     (eq (symbol-package obj) object)
739                     (eq (symbol-plist obj) object)
740                     (eq (symbol-value obj) object))
741             (maybe-call fun obj)))))
742      space)))
743
744 (defun list-referencing-objects (space object)
745   (collect ((res))
746     (map-referencing-objects
747      (lambda (obj) (res obj)) space object)
748     (res)))