1 ;;; GNU Guix --- Functional package management for GNU
2 ;;; Copyright © 2015, 2016 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-34)
33 #:use-module (srfi srfi-37)
34 #:use-module (ice-9 match)
35 #:use-module (ice-9 format)
44 ;; Size profile of a store item.
45 (define-record-type <profile>
46 (profile file self-size closure-size)
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
52 (define substitutable-path-info*
53 (store-lift substitutable-path-info))
55 (define (query-path-info* item)
56 "Monadic version of 'query-path-info' that returns #f when ITEM is not in
59 (guard (c ((nix-protocol-error? c)
60 ;; ITEM is not in the store; return #f.
62 (values (query-path-info store item) store))))
64 (define (file-size item)
65 "Return the size in bytes of ITEM, resorting to information from substitutes
66 if ITEM is not in the store."
67 (mlet %store-monad ((info (query-path-info* item)))
69 (return (path-info-nar-size info))
70 (mlet %store-monad ((info (substitutable-path-info* (list item))))
73 ;; The nar size is an approximation, but a good one.
74 (return (substitutable-nar-size info)))
76 (leave (_ "no available substitute information for '~a'~%")
79 (define* (display-profile profile #:optional (port (current-output-port)))
80 "Display PROFILE, a list of PROFILE objects, to PORT."
81 (define MiB (expt 2 20))
83 (format port "~64a ~8a ~a\n"
84 (_ "store item") (_ "total") (_ "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.)))))
93 ((($ <profile> _ _ total1) ($ <profile> _ _ total2))
94 (> total1 total2)))))))
96 (define display-profile*
97 (lift display-profile %store-monad))
99 (define (substitutable-requisites store item)
100 "Return the list of requisites of ITEM based on information available in
102 (let loop ((items (list item))
106 (delete-duplicates result))
108 (let ((info (substitutable-path-info store
109 (delete-duplicates items))))
110 (loop (remove (lambda (item) ;XXX: complexity
111 (member item result))
112 (append-map substitutable-references info))
113 (append (append-map substitutable-references info)
116 (define (requisites* item)
117 "Return as a monadic value the requisites of ITEMS, based either on the
118 information available in the local store or using information about
121 (guard (c ((nix-protocol-error? c)
122 (values (substitutable-requisites store item)
124 (values (requisites store item) store))))
126 (define (mappend-map mproc lst)
127 "Apply MPROC to each item of LST and concatenate the resulting list."
128 (with-monad %store-monad
129 (>>= (mapm %store-monad mproc lst)
131 (return (concatenate lstlst))))))
133 (define (store-profile items)
134 "Return as a monadic value a list of <profile> objects representing the
135 profile of ITEMS and their requisites."
136 (mlet* %store-monad ((refs (>>= (mappend-map requisites* items)
138 (return (delete-duplicates
139 (append items refs))))))
140 (sizes (mapm %store-monad
142 (>>= (file-size item)
144 (return (cons item size)))))
146 (define (dependency-size item)
147 (mlet %store-monad ((deps (requisites* item)))
150 (return (+ (assoc-ref sizes item) total)))
152 (delete-duplicates (cons item deps)))))
157 (mlet %store-monad ((dependencies (dependency-size item)))
158 (return (profile item size dependencies)))))
161 (define* (ensure-store-item spec-or-item)
162 "Return a store file name. If SPEC-OR-ITEM is a store file name, return it
163 as is. Otherwise, assume SPEC-OR-ITEM is a package output specification such
164 as \"guile:debug\" or \"gcc-4.8\" and return its store file name."
165 (with-monad %store-monad
166 (if (store-path? spec-or-item)
167 (return spec-or-item)
168 (let-values (((package output)
169 (specification->package+output spec-or-item)))
170 (mlet %store-monad ((drv (package->derivation package)))
171 ;; Note: we don't try building DRV like 'guix archive' does
172 ;; because we don't have to since we can instead rely on
173 ;; substitute meta-data.
174 (return (derivation->output-path drv output)))))))
181 ;; Autoload Guile-Charting.
182 ;; XXX: Use this hack instead of #:autoload to avoid compilation errors.
183 ;; See <http://bugs.gnu.org/12202>.
184 (module-autoload! (current-module)
185 '(charting) '(make-page-map))
187 (define (profile->page-map profiles file)
188 "Write a 'page map' chart of PROFILES, a list of <profile> objects, to FILE,
189 the name of a PNG file."
191 (string-drop name (+ (string-length (%store-prefix)) 28)))
194 (fold2 (lambda (profile result offset)
196 (($ <profile> name self)
197 (let ((self (inexact->exact
198 (round (/ self (expt 2. 10))))))
199 (values `((,(strip name) ,offset . ,self)
206 ((($ <profile> _ _ total1) ($ <profile> _ _ total2))
207 (> total1 total2))))))
209 ;; TRANSLATORS: This is the title of a graph, meaning that the graph
210 ;; represents a profile of the store (the "store" being the place where
211 ;; packages are stored.)
212 (make-page-map (_ "store profile") data
213 #:write-to-png file))
221 (display (_ "Usage: guix size [OPTION]... PACKAGE
222 Report the size of PACKAGE and its dependencies.\n"))
224 --substitute-urls=URLS
225 fetch substitute from URLS if they are authorized"))
227 -s, --system=SYSTEM consider packages for SYSTEM--e.g., \"i686-linux\""))
229 -m, --map-file=FILE write to FILE a graphical map of disk usage"))
232 -h, --help display this help and exit"))
234 -V, --version display version information and exit"))
236 (show-bug-report-information))
239 ;; Specifications of the command-line options.
240 (list (option '(#\s "system") #t #f
241 (lambda (opt name arg result)
242 (alist-cons 'system arg
243 (alist-delete 'system result eq?))))
244 (option '("substitute-urls") #t #f
245 (lambda (opt name arg result . rest)
247 (alist-cons 'substitute-urls
248 (string-tokenize arg)
249 (alist-delete 'substitute-urls result))
251 (option '(#\m "map-file") #t #f
252 (lambda (opt name arg result)
253 (alist-cons 'map-file arg result)))
254 (option '(#\h "help") #f #f
258 (option '(#\V "version") #f #f
260 (show-version-and-exit "guix size")))))
262 (define %default-options
263 `((system . ,(%current-system))))
270 (define (guix-size . args)
272 (let* ((opts (parse-command-line args %options (list %default-options)))
273 (files (filter-map (match-lambda
274 (('argument . file) file)
277 (map-file (assoc-ref opts 'map-file))
278 (system (assoc-ref opts 'system))
279 (urls (assoc-ref opts 'substitute-urls)))
282 (leave (_ "missing store item argument\n")))
285 ;; Turn off grafts because (1) hydra.gnu.org does not serve grafted
286 ;; packages, and (2) they do not make any difference on the
288 (parameterize ((%graft? #f))
290 (set-build-options store
291 #:use-substitutes? #t
292 #:substitute-urls urls)
294 (run-with-store store
295 (mlet* %store-monad ((item (ensure-store-item file))
296 (profile (store-profile (list item))))
299 (profile->page-map profile map-file)
301 (display-profile* profile)))
304 (leave (_ "too many arguments\n")))))))