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