size.scm 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337
  1. ;;; GNU Guix --- Functional package management for GNU
  2. ;;; Copyright © 2015, 2016, 2017, 2018, 2019, 2020 Ludovic Courtès <ludo@gnu.org>
  3. ;;; Copyright © 2019 Simon Tournier <zimon.toutoune@gmail.com>
  4. ;;;
  5. ;;; This file is part of GNU Guix.
  6. ;;;
  7. ;;; GNU Guix is free software; you can redistribute it and/or modify it
  8. ;;; under the terms of the GNU General Public License as published by
  9. ;;; the Free Software Foundation; either version 3 of the License, or (at
  10. ;;; your option) any later version.
  11. ;;;
  12. ;;; GNU Guix is distributed in the hope that it will be useful, but
  13. ;;; WITHOUT ANY WARRANTY; without even the implied warranty of
  14. ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. ;;; GNU General Public License for more details.
  16. ;;;
  17. ;;; You should have received a copy of the GNU General Public License
  18. ;;; along with GNU Guix. If not, see <http://www.gnu.org/licenses/>.
  19. (define-module (guix scripts size)
  20. #:use-module (guix ui)
  21. #:use-module (guix scripts)
  22. #:use-module (guix scripts build)
  23. #:use-module (guix store)
  24. #:use-module (guix monads)
  25. #:use-module (guix combinators)
  26. #:use-module (guix packages)
  27. #:use-module (guix derivations)
  28. #:use-module (gnu packages)
  29. #:use-module (srfi srfi-1)
  30. #:use-module (srfi srfi-9)
  31. #:use-module (srfi srfi-11)
  32. #:use-module (srfi srfi-26)
  33. #:use-module (srfi srfi-34)
  34. #:use-module (srfi srfi-37)
  35. #:use-module (ice-9 match)
  36. #:use-module (ice-9 format)
  37. #:use-module (ice-9 vlist)
  38. #:export (profile?
  39. profile-file
  40. profile-self-size
  41. profile-closure-size
  42. store-profile
  43. guix-size))
  44. ;; Size profile of a store item.
  45. (define-record-type <profile>
  46. (profile file self-size closure-size)
  47. profile?
  48. (file profile-file) ;store item
  49. (self-size profile-self-size) ;size in bytes
  50. (closure-size profile-closure-size)) ;size of dependencies in bytes
  51. (define substitutable-path-info*
  52. (store-lift substitutable-path-info))
  53. (define (file-size item)
  54. "Return the size in bytes of ITEM, resorting to information from substitutes
  55. if ITEM is not in the store."
  56. (mlet %store-monad ((info (query-path-info* item)))
  57. (if info
  58. (return (path-info-nar-size info))
  59. (mlet %store-monad ((info (substitutable-path-info* (list item))))
  60. (match info
  61. ((info)
  62. ;; The nar size is an approximation, but a good one.
  63. (return (substitutable-nar-size info)))
  64. (()
  65. (leave (G_ "no available substitute information for '~a'~%")
  66. item)))))))
  67. (define profile-closure<?
  68. (match-lambda*
  69. ((($ <profile> name1 self1 total1)
  70. ($ <profile> name2 self2 total2))
  71. (< total1 total2))))
  72. (define profile-self<?
  73. (match-lambda*
  74. ((($ <profile> name1 self1 total1)
  75. ($ <profile> name2 self2 total2))
  76. (< self1 self2))))
  77. (define* (display-profile profile #:optional (port (current-output-port))
  78. #:key (profile<? profile-closure<?))
  79. "Display PROFILE, a list of PROFILE objects, to PORT. Sort entries
  80. according to PROFILE<?."
  81. (define MiB (expt 2 20))
  82. (format port "~64a ~8a ~a\n"
  83. (G_ "store item") (G_ "total") (G_ "self"))
  84. (let ((whole (reduce + 0 (map profile-self-size profile))))
  85. (for-each (match-lambda
  86. (($ <profile> name self total)
  87. (format port "~64a ~6,1f ~6,1f ~5,1f%\n"
  88. name (/ total MiB) (/ self MiB)
  89. (* 100. (/ self whole 1.)))))
  90. (sort profile (negate profile<?)))
  91. (format port (G_ "total: ~,1f MiB~%") (/ whole MiB 1.))))
  92. (define display-profile*
  93. (lift display-profile %store-monad))
  94. (define (substitutable-requisites store items)
  95. "Return the list of requisites of ITEMS based on information available in
  96. substitutes."
  97. (let loop ((items items)
  98. (result '()))
  99. (match items
  100. (()
  101. (delete-duplicates result))
  102. (items
  103. (let ((info (substitutable-path-info store
  104. (delete-duplicates items))))
  105. (loop (remove (lambda (item) ;XXX: complexity
  106. (member item result))
  107. (append-map substitutable-references info))
  108. (append (append-map substitutable-references info)
  109. result)))))))
  110. (define (requisites* items)
  111. "Return as a monadic value the requisites of ITEMS, based either on the
  112. information available in the local store or using information about
  113. substitutes."
  114. (lambda (store)
  115. (let-values (((local missing)
  116. (partition (cut valid-path? store <>) items)))
  117. (values (delete-duplicates
  118. (append (requisites store local)
  119. (substitutable-requisites store missing)))
  120. store))))
  121. (define (store-profile items)
  122. "Return as a monadic value a list of <profile> objects representing the
  123. profile of ITEMS and their requisites."
  124. (mlet* %store-monad ((refs (>>= (requisites* items)
  125. (lambda (refs)
  126. (return (delete-duplicates
  127. (append items refs))))))
  128. (sizes (mapm %store-monad
  129. (lambda (item)
  130. (>>= (file-size item)
  131. (lambda (size)
  132. (return (cons item size)))))
  133. refs)))
  134. (define size-table
  135. (fold (lambda (pair result)
  136. (match pair
  137. ((item . size)
  138. (vhash-cons item size result))))
  139. vlist-null sizes))
  140. (define (dependency-size item)
  141. (mlet %store-monad ((deps (requisites* (list item))))
  142. (foldm %store-monad
  143. (lambda (item total)
  144. (return (+ (match (vhash-assoc item size-table)
  145. ((_ . size) size))
  146. total)))
  147. 0
  148. (delete-duplicates (cons item deps)))))
  149. (mapm %store-monad
  150. (match-lambda
  151. ((item . size)
  152. (mlet %store-monad ((dependencies (dependency-size item)))
  153. (return (profile item size dependencies)))))
  154. sizes)))
  155. (define* (ensure-store-item spec-or-item)
  156. "Return a store file name. If SPEC-OR-ITEM is a store file name, return it
  157. as is. Otherwise, assume SPEC-OR-ITEM is a package output specification such
  158. as \"guile:debug\" or \"gcc-4.8\" and return its store file name."
  159. (with-monad %store-monad
  160. (if (store-path? spec-or-item)
  161. (return spec-or-item)
  162. (let-values (((package output)
  163. (specification->package+output spec-or-item)))
  164. (mlet %store-monad ((drv (package->derivation package)))
  165. ;; Note: we don't try building DRV like 'guix archive' does
  166. ;; because we don't have to since we can instead rely on
  167. ;; substitute meta-data.
  168. (return (derivation->output-path drv output)))))))
  169. ;;;
  170. ;;; Charts.
  171. ;;;
  172. ;; Autoload Guile-Charting.
  173. ;; XXX: Use this hack instead of #:autoload to avoid compilation errors.
  174. ;; See <http://bugs.gnu.org/12202>.
  175. (module-autoload! (current-module)
  176. '(charting) '(make-page-map))
  177. (define (profile->page-map profiles file)
  178. "Write a 'page map' chart of PROFILES, a list of <profile> objects, to FILE,
  179. the name of a PNG file."
  180. (define (strip name)
  181. (string-drop name (+ (string-length (%store-prefix)) 28)))
  182. (define data
  183. (fold2 (lambda (profile result offset)
  184. (match profile
  185. (($ <profile> name self)
  186. (let ((self (inexact->exact
  187. (round (/ self (expt 2. 10))))))
  188. (values `((,(strip name) ,offset . ,self)
  189. ,@result)
  190. (+ offset self))))))
  191. '()
  192. 0
  193. (sort profiles
  194. (match-lambda*
  195. ((($ <profile> name1 self1 total1)
  196. ($ <profile> name2 self2 total2))
  197. (> total1 total2))))))
  198. ;; TRANSLATORS: This is the title of a graph, meaning that the graph
  199. ;; represents a profile of the store (the "store" being the place where
  200. ;; packages are stored.)
  201. (make-page-map (G_ "store profile") data
  202. #:write-to-png file))
  203. ;;;
  204. ;;; Options.
  205. ;;;
  206. (define (show-help)
  207. (display (G_ "Usage: guix size [OPTION]... PACKAGE|STORE-ITEM
  208. Report the size of the PACKAGE or STORE-ITEM, with its dependencies.\n"))
  209. (display (G_ "
  210. --substitute-urls=URLS
  211. fetch substitute from URLS if they are authorized"))
  212. ;; TRANSLATORS: "closure" and "self" must not be translated.
  213. (display (G_ "
  214. --sort=KEY sort according to KEY--\"closure\" or \"self\""))
  215. (display (G_ "
  216. -m, --map-file=FILE write to FILE a graphical map of disk usage"))
  217. (newline)
  218. (display (G_ "
  219. -L, --load-path=DIR prepend DIR to the package module search path"))
  220. (newline)
  221. (display (G_ "
  222. -h, --help display this help and exit"))
  223. (display (G_ "
  224. -V, --version display version information and exit"))
  225. (newline)
  226. (show-native-build-options-help)
  227. (newline)
  228. (show-bug-report-information))
  229. (define %options
  230. ;; Specifications of the command-line options.
  231. (cons* (option '("substitute-urls") #t #f
  232. (lambda (opt name arg result . rest)
  233. (apply values
  234. (alist-cons 'substitute-urls
  235. (string-tokenize arg)
  236. (alist-delete 'substitute-urls result))
  237. rest)))
  238. (option '("sort") #t #f
  239. (lambda (opt name arg result . rest)
  240. (match arg
  241. ("closure"
  242. (alist-cons 'profile<? profile-closure<? result))
  243. ("self"
  244. (alist-cons 'profile<? profile-self<? result))
  245. (_
  246. (leave (G_ "~a: invalid sorting key~%") arg)))))
  247. (option '(#\m "map-file") #t #f
  248. (lambda (opt name arg result)
  249. (alist-cons 'map-file arg result)))
  250. (find (lambda (option)
  251. (member "load-path" (option-names option)))
  252. %standard-build-options)
  253. (option '(#\h "help") #f #f
  254. (lambda args
  255. (show-help)
  256. (exit 0)))
  257. (option '(#\V "version") #f #f
  258. (lambda args
  259. (show-version-and-exit "guix size")))
  260. %standard-native-build-options))
  261. (define %default-options
  262. `((system . ,(%current-system))
  263. (profile<? . ,profile-self<?)))
  264. ;;;
  265. ;;; Entry point.
  266. ;;;
  267. (define-command (guix-size . args)
  268. (category packaging)
  269. (synopsis "profile the on-disk size of packages")
  270. (with-error-handling
  271. (let* ((opts (parse-command-line args %options (list %default-options)
  272. #:build-options? #f))
  273. (files (filter-map (match-lambda
  274. (('argument . file) file)
  275. (_ #f))
  276. opts))
  277. (profile<? (assoc-ref opts 'profile<?))
  278. (map-file (assoc-ref opts 'map-file))
  279. (system (assoc-ref opts 'system))
  280. (urls (assoc-ref opts 'substitute-urls)))
  281. (match files
  282. (()
  283. (leave (G_ "missing store item argument\n")))
  284. ((files ..1)
  285. (leave-on-EPIPE
  286. ;; Turn off grafts because (1) substitute servers do not serve grafted
  287. ;; packages, and (2) they do not make any difference on the
  288. ;; resulting size.
  289. (parameterize ((%graft? #f))
  290. (with-store store
  291. (set-build-options store
  292. #:use-substitutes? #t
  293. #:substitute-urls urls)
  294. (run-with-store store
  295. (mlet* %store-monad ((items (mapm %store-monad
  296. ensure-store-item files))
  297. (profile (store-profile items)))
  298. (if map-file
  299. (begin
  300. (profile->page-map profile map-file)
  301. (return #t))
  302. (display-profile* profile (current-output-port)
  303. #:profile<? profile<?)))
  304. #:system system)))))))))