profiling.scm 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. ;;; GNU Guix --- Functional package management for GNU
  2. ;;; Copyright © 2017, 2019 Ludovic Courtès <ludo@gnu.org>
  3. ;;;
  4. ;;; This file is part of GNU Guix.
  5. ;;;
  6. ;;; GNU Guix is free software; you can redistribute it and/or modify it
  7. ;;; under the terms of the GNU General Public License as published by
  8. ;;; the Free Software Foundation; either version 3 of the License, or (at
  9. ;;; your option) any later version.
  10. ;;;
  11. ;;; GNU Guix is distributed in the hope that it will be useful, but
  12. ;;; WITHOUT ANY WARRANTY; without even the implied warranty of
  13. ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. ;;; GNU General Public License for more details.
  15. ;;;
  16. ;;; You should have received a copy of the GNU General Public License
  17. ;;; along with GNU Guix. If not, see <http://www.gnu.org/licenses/>.
  18. (define-module (guix profiling)
  19. #:use-module (ice-9 match)
  20. #:autoload (ice-9 format) (format)
  21. #:export (profiled?
  22. register-profiling-hook!))
  23. ;;; Commentary:
  24. ;;;
  25. ;;; Basic support for Guix-specific profiling.
  26. ;;;
  27. ;;; Code:
  28. (define profiled?
  29. (let ((profiled
  30. (or (and=> (getenv "GUIX_PROFILING") string-tokenize)
  31. '())))
  32. (lambda (component)
  33. "Return true if COMPONENT profiling is active."
  34. (member component profiled))))
  35. (define %profiling-hooks
  36. ;; List of profiling hooks.
  37. (map (match-lambda
  38. ("after-gc" after-gc-hook)
  39. ((or "exit" #f) exit-hook))
  40. (or (and=> (getenv "GUIX_PROFILING_EVENTS") string-tokenize)
  41. '("exit"))))
  42. (define (register-profiling-hook! component thunk)
  43. "Register THUNK as a profiling hook for COMPONENT, a string such as
  44. \"rpc\"."
  45. (when (profiled? component)
  46. (for-each (lambda (hook)
  47. (add-hook! hook thunk))
  48. %profiling-hooks)))
  49. (define (show-gc-stats)
  50. "Display garbage collection statistics."
  51. (define MiB (* 1024 1024.))
  52. (define stats (gc-stats))
  53. (format (current-error-port) "Garbage collection statistics:
  54. heap size: ~,2f MiB
  55. allocated: ~,2f MiB
  56. GC times: ~a
  57. time spent in GC: ~,2f seconds (~d% of user time)~%"
  58. (/ (assq-ref stats 'heap-size) MiB)
  59. (/ (assq-ref stats 'heap-total-allocated) MiB)
  60. (assq-ref stats 'gc-times)
  61. (/ (assq-ref stats 'gc-time-taken)
  62. internal-time-units-per-second 1.)
  63. (inexact->exact
  64. (round (* (/ (assq-ref stats 'gc-time-taken)
  65. (tms:utime (times)) 1.)
  66. 100)))))
  67. (register-profiling-hook! "gc" show-gc-stats)