Changeset 13438
- Timestamp:
- Feb 8, 2010, 11:05:04 AM (15 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/working-0711/ccl/library/core-files.lisp
r13388 r13438 1 1 ;;; 2 ;;; Copyright (C) 2009 ,Clozure Associates and contributors2 ;;; Copyright (C) 2009-2010 Clozure Associates and contributors 3 3 ;;; This file is part of Clozure CL. 4 4 ;;; … … 22 22 23 23 (export '(open-core close-core 24 core-heap-utilization map-core-areas map-core- pointers24 core-heap-utilization map-core-areas map-core-region map-core-pointers 25 25 core-q core-l core-w core-b 26 26 core-consp core-symbolp core-functionp core-listp core-nullp core-uvector-p 27 27 core-uvtype core-uvtypep core-uvref core-uvsize 28 core-car core-cdr core-object-type core-istruct-type 28 core-car core-cdr core-object-typecode-type 29 core-istruct-type core-struct-type core-instance-type 30 core-object-type-key core-type-string 29 31 copy-from-core core-list 30 32 core-keyword-package core-find-package core-find-symbol … … 37 39 core-instance-class 38 40 core-instance-p 39 core-instance-class-name40 41 core-string-equal 41 42 core-all-processes core-process-name … … 71 72 72 73 (defmethod print-object :around ((core core-info) (stream t)) 73 (let ((*print-array* nil)) 74 (let ((*print-array* nil) 75 (*print-simple-bit-vector* nil)) 74 76 (call-next-method))) 75 77 … … 93 95 ;; TODO: after load sections, check if highest heap address is a fixnum, and 94 96 ;; arrange to use fixnum-only versions of the reading functions. 95 (defun open-core (pathname &key (method :mmap)) 97 (defun open-core (pathname &key (method :mmap) (core-info nil)) 98 (when core-info (check-type core-info core-info)) 96 99 (when *current-core* 97 100 (close-core)) 98 101 (let* ((sections (read-sections pathname)) 99 (core (make-core-info :pathname pathname :sections sections))) 102 (core (or core-info (make-core-info)))) 103 (setf (core-info-pathname core) pathname) 104 (setf (core-info-sections core) sections) 105 (setf (core-info-symbol-ptrs core) nil) 106 (setf (core-info-classes-hash-table-ptr core) nil) 107 (setf (core-info-lfun-names-table-ptr core) nil) 108 (setf (core-info-process-class core) nil) 100 109 (ecase method 101 110 (:mmap (let ((mapped-vector (map-file-to-ivector pathname '(unsigned-byte 8)))) … … 103 112 (loop for data across sections do (incf (cdr data) offset)) 104 113 (setf (core-info-mapped-ivector core) mapped-vector) 105 (setf (core-info-raw-ivector core) vector)))) 106 (:stream (setf (core-info-stream core) 107 (open pathname :element-type '(unsigned-byte 8))))) 114 (setf (core-info-raw-ivector core) vector) 115 (setf (core-info-stream core) nil)))) 116 (:stream (setf (core-info-stream core) (open pathname :element-type '(unsigned-byte 8)) 117 (core-info-mapped-ivector core) nil 118 (core-info-raw-ivector core) nil))) 108 119 (setq *current-core* core)) 120 ;;(unless (every (lambda (sect) (fixnump (car sect))) (core-info-sections (current-core))) 121 ;; (error "Non-fixnum addresses not supported")) 109 122 pathname) 110 123 … … 196 209 (declare (type basic-input-stream s) (optimize (speed 3) (safety 0))) 197 210 (when offset (stream-position s offset)) 198 (%i+ (core-stream-readb s nil) ( ash (core-stream-readb s nil) 8)))211 (%i+ (core-stream-readb s nil) (%ilsl 8 (core-stream-readb s nil)))) 199 212 200 213 (defun core-stream-readl (s offset) 201 214 (declare (type basic-input-stream s) (optimize (speed 3) (safety 0))) 202 215 (when offset (stream-position s offset)) 203 (%i+ (core-stream-readw s nil) ( ash (core-stream-readw s nil) 16)))216 (%i+ (core-stream-readw s nil) (%ilsl 16 (core-stream-readw s nil)))) 204 217 205 218 (defun core-stream-readq (s offset) 206 219 (declare (type basic-input-stream s) (optimize (speed 3) (safety 0))) 207 220 (when offset (stream-position s offset)) 208 (+ (core-stream-readl s nil) (ash ( core-stream-readl s nil) 32)))221 (+ (core-stream-readl s nil) (ash (the fixnum (core-stream-readl s nil)) 32))) 209 222 210 223 (defun core-ivector-readb (vec offset) … … 215 228 (defun core-ivector-readw (vec offset) 216 229 (declare (optimize (speed 3) (safety 0))) 217 (%i+ (core-ivector-readb vec offset) ( ash (core-ivector-readb vec (%i+ offset 1)) 8)))230 (%i+ (core-ivector-readb vec offset) (%ilsl 8 (core-ivector-readb vec (+ offset 1))))) 218 231 219 232 (defun core-ivector-readl (vec offset) 220 233 (declare (optimize (speed 3) (safety 0))) 221 (%i+ (core-ivector-readw vec offset) ( ash (core-ivector-readw vec (%i+ offset 2)) 16)))234 (%i+ (core-ivector-readw vec offset) (%ilsl 16 (core-ivector-readw vec (+ offset 2))))) 222 235 223 236 (defun core-ivector-readq (vec offset) 224 237 (declare (optimize (speed 3) (safety 0))) 225 (+ (core-ivector-readl vec offset) (ash (core-ivector-readl vec ( %i+ offset 4)) 32)))238 (+ (core-ivector-readl vec offset) (ash (core-ivector-readl vec (+ offset 4)) 32))) 226 239 227 240 … … 302 315 303 316 (defun uvheader-size (header) 304 ( ash header (- target::num-subtag-bits)))317 (the fixnum (ash header (- target::num-subtag-bits)))) 305 318 306 319 (defun uvheader-byte-size (header) … … 328 341 (unless (eq symbol 'bogus) 329 342 (cond ((setq pos (position symbol *immheader-0-types*)) 330 ( logior (ash pos target::ntagbits) target::fulltag-immheader-0))343 (%ilogior (%ilsl target::ntagbits pos) target::fulltag-immheader-0)) 331 344 ((setq pos (position symbol *immheader-1-types*)) 332 ( logior (ash pos target::ntagbits) target::fulltag-immheader-1))345 (%ilogior (%ilsl target::ntagbits pos) target::fulltag-immheader-1)) 333 346 ((setq pos (position symbol *immheader-2-types*)) 334 ( logior (ash pos target::ntagbits) target::fulltag-immheader-2))347 (%ilogior (%ilsl target::ntagbits pos) target::fulltag-immheader-2)) 335 348 ((setq pos (position symbol *nodeheader-0-types*)) 336 ( logior (ash pos target::ntagbits) target::fulltag-nodeheader-0))349 (%ilogior (%ilsl target::ntagbits pos) target::fulltag-nodeheader-0)) 337 350 ((setq pos (position symbol *nodeheader-1-types*)) 338 ( logior (ash pos target::ntagbits) target::fulltag-nodeheader-1)))))351 (%ilogior (%ilsl target::ntagbits pos) target::fulltag-nodeheader-1))))) 339 352 340 353 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; … … 364 377 365 378 (defun map-core-area (area-ptr fun) 366 (let* ((ptr (core-q area-ptr target::area.low)) 367 (end (core-q area-ptr target::area.active))) 368 (loop 369 (when (>= ptr end) (return)) 370 (let ((header (core-q ptr))) 371 (cond ((uvheader-p header) 372 (let ((subtag (uvheader-typecode header))) 373 (funcall fun 374 (+ ptr (cond ((eq subtag target::subtag-symbol) target::fulltag-symbol) 375 ((eq subtag target::subtag-function) target::fulltag-function) 376 (t target::fulltag-misc))))) 377 (let* ((bytes (uvheader-byte-size header)) 378 (total (logandc2 (%i+ bytes (+ target::node-size (1- target::dnode-size))) 379 (1- target::dnode-size)))) 380 (declare (fixnum bytes total)) 381 (incf ptr total))) 382 (t 383 (funcall fun (+ ptr target::fulltag-cons)) 384 (incf ptr target::cons.size))))))) 379 (map-core-region (core-q area-ptr target::area.low) 380 (core-q area-ptr target::area.active) 381 fun)) 382 383 (defun map-core-region (ptr end fun) 384 (loop 385 while (< ptr end) as header = (core-q ptr) 386 do (cond ((uvheader-p header) 387 (let ((subtag (uvheader-typecode header))) 388 (funcall fun 389 (+ ptr (cond ((eq subtag target::subtag-symbol) target::fulltag-symbol) 390 ((eq subtag target::subtag-function) target::fulltag-function) 391 (t target::fulltag-misc))))) 392 (let* ((bytes (uvheader-byte-size header)) 393 (total (logandc2 (%i+ bytes (+ target::node-size (1- target::dnode-size))) 394 (1- target::dnode-size)))) 395 (declare (fixnum bytes total)) 396 (incf ptr total))) 397 (t 398 (funcall fun (+ ptr target::fulltag-cons)) 399 (incf ptr target::cons.size))))) 385 400 386 401 … … 437 452 (addr (+ (logandc2 vec-ptr target::fulltagmask) target::node-size)) 438 453 (typecode (uvheader-typecode header)) 439 (tag ( logand typecode target::fulltagmask))454 (tag (%ilogand typecode target::fulltagmask)) 440 455 (len (uvheader-size header))) 441 456 (assert (< -1 index len)) 442 (cond ((or (eq ltag target::fulltag-nodeheader-0)443 (eq ltag target::fulltag-nodeheader-1))444 (core-q addr ( ash index target::word-shift)))445 ((eq ltag target::ivector-class-64-bit)457 (cond ((or (eq tag target::fulltag-nodeheader-0) 458 (eq tag target::fulltag-nodeheader-1)) 459 (core-q addr (%ilsl target::word-shift index))) 460 ((eq tag target::ivector-class-64-bit) 446 461 (cond ((eq typecode target::subtag-double-float-vector) 447 462 (error "~s not implemented yet" 'target::subtag-double-float-vector)) 448 463 (t 449 (core-q addr ( ash index target::word-shift)))))464 (core-q addr (%ilsl target::word-shift index))))) 450 465 ((eq tag target::ivector-class-32-bit) 451 466 (cond ((eq typecode target::subtag-simple-base-string) 452 ( code-char (core-l addr (ash index 2))))467 (%code-char (core-l addr (%ilsl 2 index)))) 453 468 ((eq typecode target::subtag-single-float-vector) 454 469 (error "~s not implemented yet" 'target::subtag-single-float-vector)) 455 (t (core-l addr ( ash index 2)))))470 (t (core-l addr (%ilsl 2 index))))) 456 471 ((eq typecode target::subtag-bit-vector) 457 (let ((byte (core-b addr ( ash (+ index 7) -3))))472 (let ((byte (core-b addr (%iasr 3 (%i+ index 7))))) 458 473 (error "not implemented, for ~b" byte))) 459 474 ((>= typecode target::min-8-bit-ivector-subtag) 460 475 (core-b addr index)) 461 (t (core-w addr ( ash index 1))))))476 (t (core-w addr (%ilsl 1 index)))))) 462 477 463 478 (defun core-uvsize (vec-ptr) … … 472 487 (core-q obj target::cons.cdr)) 473 488 474 (defun core-object-type (obj)489 (defun core-object-typecode-type (obj) 475 490 (let ((fulltag (logand obj target::fulltagmask))) 476 491 (cond ((eq fulltag target::fulltag-cons) 'cons) … … 490 505 'bogus)))) 491 506 507 (defun core-object-type-key (obj) 508 ;; Returns either a symbol (for built-in types) or a pointer to type symbol or class. 509 ;; Whatever it returns must be suitable for use in an eql hash table; use core-type-string 510 ;; to get a printable rep. 511 (let ((type (core-object-typecode-type obj))) 512 (case type 513 (internal-structure (core-istruct-type obj)) 514 (structure (core-struct-type obj)) 515 (instance (core-instance-type obj)) 516 (t type)))) 517 518 (defun core-type-string (object-type) 519 (with-output-to-string (s) 520 (if (fixnump object-type) 521 (core-print object-type s) 522 (prin1 object-type s)))) 523 492 524 (defun core-istruct-type (obj) 493 525 (and (core-uvtypep obj :istruct) 494 526 (core-car (core-uvref obj 0)))) 495 527 528 (defun core-struct-type (obj) 529 (and (core-uvtypep obj :struct) 530 (core-uvref (core-car (core-uvref obj 0)) 1))) 531 532 (defun core-instance-type (obj) 533 (and (core-uvtypep obj :instance) 534 (core-uvref (core-uvref (core-instance-class obj) instance.slots) %class.name))) 535 496 536 497 537 (defun core-object-type-and-size (obj) … … 507 547 (values (uvheader-type header) logsize total)))))) 508 548 509 (defun core-heap-utilization (&key area unit sort) 510 (let* ((hash (make-hash-table :shared nil)) 511 (total-physsize 0) 512 (div (ecase unit 513 ((nil) 1) 514 (:kb 1024.0d0) 515 (:mb (* 1024.0d0 1024.0d0)) 516 (:gb (* 1024.0d0 1024.0d0 1024.0d0)))) 517 (sort-key (ecase sort 518 (:count #'cadr) 519 (:logical-size #'caddr) 520 ((:physical-size nil) #'cdddr))) 549 (defun core-heap-utilization (&key (stream *debug-io*) area unit (sort :size) classes (threshold 0.00005)) 550 (let* ((obj-hash (make-hash-table :shared nil)) 551 (slotv-hash (make-hash-table :shared nil)) 521 552 (all nil)) 522 (map-core-areas (lambda (obj )553 (map-core-areas (lambda (obj &aux (hash obj-hash)) 523 554 (multiple-value-bind (type logsize physsize) (core-object-type-and-size obj) 555 (when classes 556 (when (core-uvtypep obj :slot-vector) 557 (setq hash slotv-hash 558 obj (core-uvref obj slot-vector.instance))) 559 (setq type (core-object-type-key obj))) 524 560 (let ((a (or (gethash type hash) 525 (setf (gethash type hash) (list *0 0 0)))))561 (setf (gethash type hash) (list 0 0 0))))) 526 562 (incf (car a)) 527 563 (incf (cadr a) logsize) 528 (incf (c ddr a) physsize))))564 (incf (caddr a) physsize)))) 529 565 :area area) 530 566 (maphash (lambda (type data) 531 (incf total-physsize (cddr data)) 532 (push (cons type data) all)) 533 hash) 534 (setq all (sort all #'> :key sort-key)) 535 (format t "~&Object type~42tCount Logical size Physical size % of Heap~%~50t~a~66t~:*~a" 536 (ecase unit 537 ((nil) " (in bytes)") 538 (:kb "(in kilobytes)") 539 (:mb "(in megabytes)") 540 (:gb "(in gigabytes)"))) 541 (loop for (type count logsize . physsize) in all 542 do (if unit 543 (format t "~&~a~36t~11d~16,2f~16,2f~11,2f%" 544 type 545 count 546 (/ logsize div) 547 (/ physsize div) 548 (* 100.0 (/ physsize total-physsize))) 549 (format t "~&~a~36t~11d~16d~16d~11,2f%" 550 type 551 count 552 logsize 553 physsize 554 (* 100.0 (/ physsize total-physsize))))) 555 (if unit 556 (format t "~&Total~63t~16,2f" (/ total-physsize div)) 557 (format t "~&Total~63t~16d" total-physsize))) 558 (values)) 567 (push (cons (core-type-string type) data) all)) 568 obj-hash) 569 (maphash (lambda (type data) 570 (push (cons (concatenate 'string (core-type-string type) " slot-vector") data) all)) 571 slotv-hash) 572 (report-heap-utilization all :stream stream :unit unit :sort sort :threshold threshold))) 559 573 560 574 … … 563 577 (defmethod print-object ((obj unresolved-address) stream) 564 578 (let* ((address (unresolved-address-address obj))) 565 (format stream "#<Core ~S~@[[~d]~] #x~x >" 566 (core-object-type address) 579 (format stream "#<Core ~A~@[[~d]~] #x~x >" 580 (or (ignore-errors (core-type-string (core-object-type-key address))) 581 (core-object-typecode-type address)) 567 582 (and (core-uvector-p address) (core-uvsize address)) 568 583 address))) … … 615 630 (len (uvheader-size header)) 616 631 (vec (%alloc-misc len typecode))) 632 (declare (type fixnum typecode tag len)) 617 633 (cond ((or (eq tag target::fulltag-nodeheader-0) 618 634 (eq tag target::fulltag-nodeheader-1)) 619 (when (eq ltypecode target::subtag-function)635 (when (eq typecode target::subtag-function) 620 636 ;; Don't bother copying the code for now 621 637 (let ((skip (core-l addr))) 638 (declare (fixnum skip)) 622 639 (assert (<= 0 skip len)) 623 640 (incf addr (ash skip target::word-shift)) 624 641 (decf len skip))) 625 642 (dotimes (i len) 643 (declare (fixnum i)) 626 644 (setf (%svref vec i) 627 (copy-from-core (core-q addr ( ash i target::word-shift)) :depth depth)))645 (copy-from-core (core-q addr (%ilsl target::word-shift i)) :depth depth))) 628 646 (let ((ptrtag (logand vec-ptr target::fulltagmask))) 629 (cond ((eq lptrtag target::fulltag-symbol)647 (cond ((eq ptrtag target::fulltag-symbol) 630 648 (%symvector->symptr vec)) 631 ((eq lptrtag target::fulltag-function)649 ((eq ptrtag target::fulltag-function) 632 650 (%function-vector-to-function vec)) 633 651 (t vec)))) … … 638 656 (t 639 657 (dotimes (i len vec) 640 (setf (uvref vec i) (core-q addr ( ash i target::word-shift)))))))658 (setf (uvref vec i) (core-q addr (%ilsl target::word-shift i))))))) 641 659 ((eq tag target::ivector-class-32-bit) 642 660 (cond ((eq typecode target::subtag-simple-base-string) 643 661 (dotimes (i len vec) 644 (setf (uvref vec i) ( code-char (core-l addr (ash i 2))))))662 (setf (uvref vec i) (%code-char (core-l addr (%ilsl 2 i)))))) 645 663 ((eq typecode target::subtag-single-float-vector) 646 664 (warn "~s not implemented yet" 'target::subtag-single-float-vector) … … 648 666 (t 649 667 (dotimes (i len vec) 650 (setf (uvref vec i) (core-l addr ( ash i 2)))))))668 (setf (uvref vec i) (core-l addr (%ilsl 2 i))))))) 651 669 ((eq typecode target::subtag-bit-vector) 652 670 (warn "bit vector not implemented yet") … … 657 675 (t 658 676 (dotimes (i len vec) 659 (setf (uvref vec i) (core-w addr ( ash i 1))))))))677 (setf (uvref vec i) (core-w addr (%ilsl 1 i)))))))) 660 678 661 679 (defun map-core-pointers (fn &key area) … … 672 690 (len (uvheader-size header)) 673 691 (addr (+ (logandc2 obj target::fulltagmask) target::node-size))) 674 (when (eql typecode target::subtag-function) 692 (declare (fixnum typecode len)) 693 (when (eq typecode target::subtag-function) 675 694 (let ((skip (core-l addr))) 695 (declare (fixnum skip)) 676 696 (assert (<= 0 skip len)) 677 (incf addr ( ash skip target::word-shift))697 (incf addr (%ilsl target::word-shift skip)) 678 698 (decf len skip))) 679 699 (dotimes (i len) 680 (funcall fn (core-q addr ( ash i target::word-shift)) obj i))))))))700 (funcall fn (core-q addr (%ilsl target::word-shift i)) obj i)))))))) 681 701 :area area)) 682 702 … … 708 728 (matchp (core-instance-class obj))))) 709 729 710 711 (defun core-instance-class-name (obj)712 (let* ((class (core-instance-class obj))713 (class-slots (core-uvref class instance.slots))714 (name (core-uvref class-slots %class.name)))715 (core-symbol-name name)))716 730 717 731 (defun core-symptr (obj) … … 858 872 (logbitp $lfbits-trampoline-bit (core-lfun-bits fun))) 859 873 (let* ((addr (+ (logandc2 fun target::fulltagmask) target::node-size))) 860 (setq fun (core-q addr ( ash (core-l addr) target::word-shift)))874 (setq fun (core-q addr (%ilsl target::word-shift (core-l addr)))) 861 875 (when (core-uvtypep fun :simple-vector) 862 876 (setq fun (core-uvref fun 0))) … … 928 942 929 943 (defun core-print (obj &optional (stream t) depth) 930 ;; TODO: could dispatch on core-object-type ...944 ;; TODO: could dispatch on core-object-typecode-type... 931 945 (cond ((core-nullp obj) (format stream "NIL")) 932 946 ((core-symbolp obj) … … 950 964 (format stream ")")) 951 965 (t (format stream "#<core ~s x~x>" 952 (core-object-type obj) obj))))966 (core-object-typecode-type obj) obj)))) 953 967 954 968 (defun core-print-symbol (sym stream) … … 1008 1022 (defun core-print-process (proc stream) 1009 1023 (format stream "#<~a ~s LWP(~d) #x~x>" 1010 (core- instance-class-name proc)1024 (core-symbol-name (core-instance-type proc)) 1011 1025 (core-process-name proc) 1012 1026 (core-q (core-process-tcr proc) target::tcr.native-thread-id)
Note:
See TracChangeset
for help on using the changeset viewer.
