1 ;;; GNU Guix --- Functional package management for GNU
2 ;;; Copyright © 2015, 2016, 2017 Ludovic Courtès <ludo@gnu.org>
4 ;;; This file is part of GNU Guix.
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.
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.
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/>.
19 (define-module (guix scripts size)
20 #:use-module (guix ui)
21 #:use-module (guix scripts)
22 #:use-module (guix store)
23 #:use-module (guix monads)
24 #:use-module (guix combinators)
25 #:use-module (guix grafts)
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)
45 ;; Size profile of a store item.
46 (define-record-type <profile>
47 (profile file self-size closure-size)
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
53 (define substitutable-path-info*
54 (store-lift substitutable-path-info))
56 (define (query-path-info* item)
57 "Monadic version of 'query-path-info' that returns #f when ITEM is not in
60 (guard (c ((nix-protocol-error? c)
61 ;; ITEM is not in the store; return #f.
63 (values (query-path-info store item) store))))
65 (define (file-size item)
66 "Return the size in bytes of ITEM, resorting to information from substitutes
67 if ITEM is not in the store."
68 (mlet %store-monad ((info (query-path-info* item)))
70 (return (path-info-nar-size info))
71 (mlet %store-monad ((info (substitutable-path-info* (list item))))
74 ;; The nar size is an approximation, but a good one.
75 (return (substitutable-nar-size info)))
77 (leave (G_ "no available substitute information for '~a'~%")
80 (define profile-closure<?
82 ((($ <profile> name1 self1 total1)
83 ($ <profile> name2 self2 total2))
86 (define profile-self<?
88 ((($ <profile> name1 self1 total1)
89 ($ <profile> name2 self2 total2))
92 (define* (display-profile profile #:optional (port (current-output-port))
93 #:key (profile<? profile-closure<?))
94 "Display PROFILE, a list of PROFILE objects, to PORT. Sort entries
95 according to PROFILE<?."
96 (define MiB (expt 2 20))
98 (format port "~64a ~8a ~a\n"
99 (G_ "store item") (G_ "total") (G_ "self"))
100 (let ((whole (reduce + 0 (map profile-self-size profile))))
101 (for-each (match-lambda
102 (($ <profile> name self total)
103 (format port "~64a ~6,1f ~6,1f ~5,1f%\n"
104 name (/ total MiB) (/ self MiB)
105 (* 100. (/ self whole 1.)))))
106 (sort profile (negate profile<?)))
107 (format port (G_ "total: ~,1f MiB~%") (/ whole MiB 1.))))
109 (define display-profile*
110 (lift display-profile %store-monad))
112 (define (substitutable-requisites store items)
113 "Return the list of requisites of ITEMS based on information available in
115 (let loop ((items items)
119 (delete-duplicates result))
121 (let ((info (substitutable-path-info store
122 (delete-duplicates items))))
123 (loop (remove (lambda (item) ;XXX: complexity
124 (member item result))
125 (append-map substitutable-references info))
126 (append (append-map substitutable-references info)
129 (define (requisites* items)
130 "Return as a monadic value the requisites of ITEMS, based either on the
131 information available in the local store or using information about
134 (let-values (((local missing)
135 (partition (cut valid-path? store <>) items)))
136 (values (delete-duplicates
137 (append (requisites store local)
138 (substitutable-requisites store missing)))
141 (define (store-profile items)
142 "Return as a monadic value a list of <profile> objects representing the
143 profile of ITEMS and their requisites."
144 (mlet* %store-monad ((refs (>>= (requisites* items)
146 (return (delete-duplicates
147 (append items refs))))))
148 (sizes (mapm %store-monad
150 (>>= (file-size item)
152 (return (cons item size)))))
154 (define (dependency-size item)
155 (mlet %store-monad ((deps (requisites* (list item))))
158 (return (+ (assoc-ref sizes item) total)))
160 (delete-duplicates (cons item deps)))))
165 (mlet %store-monad ((dependencies (dependency-size item)))
166 (return (profile item size dependencies)))))
169 (define* (ensure-store-item spec-or-item)
170 "Return a store file name. If SPEC-OR-ITEM is a store file name, return it
171 as is. Otherwise, assume SPEC-OR-ITEM is a package output specification such
172 as \"guile:debug\" or \"gcc-4.8\" and return its store file name."
173 (with-monad %store-monad
174 (if (store-path? spec-or-item)
175 (return spec-or-item)
176 (let-values (((package output)
177 (specification->package+output spec-or-item)))
178 (mlet %store-monad ((drv (package->derivation package)))
179 ;; Note: we don't try building DRV like 'guix archive' does
180 ;; because we don't have to since we can instead rely on
181 ;; substitute meta-data.
182 (return (derivation->output-path drv output)))))))
189 ;; Autoload Guile-Charting.
190 ;; XXX: Use this hack instead of #:autoload to avoid compilation errors.
191 ;; See <http://bugs.gnu.org/12202>.
192 (module-autoload! (current-module)
193 '(charting) '(make-page-map))
195 (define (profile->page-map profiles file)
196 "Write a 'page map' chart of PROFILES, a list of <profile> objects, to FILE,
197 the name of a PNG file."
199 (string-drop name (+ (string-length (%store-prefix)) 28)))
202 (fold2 (lambda (profile result offset)
204 (($ <profile> name self)
205 (let ((self (inexact->exact
206 (round (/ self (expt 2. 10))))))
207 (values `((,(strip name) ,offset . ,self)
214 ((($ <profile> name1 self1 total1)
215 ($ <profile> name2 self2 total2))
216 (> total1 total2))))))
218 ;; TRANSLATORS: This is the title of a graph, meaning that the graph
219 ;; represents a profile of the store (the "store" being the place where
220 ;; packages are stored.)
221 (make-page-map (G_ "store profile") data
222 #:write-to-png file))
230 (display (G_ "Usage: guix size [OPTION]... PACKAGE
231 Report the size of PACKAGE and its dependencies.\n"))
233 --substitute-urls=URLS
234 fetch substitute from URLS if they are authorized"))
236 -s, --system=SYSTEM consider packages for SYSTEM--e.g., \"i686-linux\""))
237 ;; TRANSLATORS: "closure" and "self" must not be translated.
239 --sort=KEY sort according to KEY--\"closure\" or \"self\""))
241 -m, --map-file=FILE write to FILE a graphical map of disk usage"))
244 -h, --help display this help and exit"))
246 -V, --version display version information and exit"))
248 (show-bug-report-information))
251 ;; Specifications of the command-line options.
252 (list (option '(#\s "system") #t #f
253 (lambda (opt name arg result)
254 (alist-cons 'system arg
255 (alist-delete 'system result eq?))))
256 (option '("substitute-urls") #t #f
257 (lambda (opt name arg result . rest)
259 (alist-cons 'substitute-urls
260 (string-tokenize arg)
261 (alist-delete 'substitute-urls result))
263 (option '("sort") #t #f
264 (lambda (opt name arg result . rest)
267 (alist-cons 'profile<? profile-closure<? result))
269 (alist-cons 'profile<? profile-self<? result))
271 (leave (G_ "~a: invalid sorting key~%") arg)))))
272 (option '(#\m "map-file") #t #f
273 (lambda (opt name arg result)
274 (alist-cons 'map-file arg result)))
275 (option '(#\h "help") #f #f
279 (option '(#\V "version") #f #f
281 (show-version-and-exit "guix size")))))
283 (define %default-options
284 `((system . ,(%current-system))
285 (profile<? . ,profile-self<?)))
292 (define (guix-size . args)
294 (let* ((opts (parse-command-line args %options (list %default-options)
295 #:build-options? #f))
296 (files (filter-map (match-lambda
297 (('argument . file) file)
300 (profile<? (assoc-ref opts 'profile<?))
301 (map-file (assoc-ref opts 'map-file))
302 (system (assoc-ref opts 'system))
303 (urls (assoc-ref opts 'substitute-urls)))
306 (leave (G_ "missing store item argument\n")))
309 ;; Turn off grafts because (1) hydra.gnu.org does not serve grafted
310 ;; packages, and (2) they do not make any difference on the
312 (parameterize ((%graft? #f))
314 (set-build-options store
315 #:use-substitutes? #t
316 #:substitute-urls urls)
318 (run-with-store store
319 (mlet* %store-monad ((items (mapm %store-monad
320 ensure-store-item files))
321 (profile (store-profile items)))
324 (profile->page-map profile map-file)
326 (display-profile* profile (current-output-port)
327 #:profile<? profile<?)))
328 #:system system)))))))))