size.scm 13 KB

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