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