summaryrefslogtreecommitdiff
path: root/guix/scripts/weather.scm
blob: 6d925d416ccb98f301dc2cd6470f0906efaf6c0f (about) (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
;;; GNU Guix --- Functional package management for GNU
;;; Copyright © 2017, 2018, 2019, 2020, 2021 Ludovic Courtès <ludo@gnu.org>
;;; Copyright © 2017 Ricardo Wurmus <rekado@elephly.net>
;;; Copyright © 2018 Kyle Meyer <kyle@kyleam.com>
;;; Copyright © 2020 Simon Tournier <zimon.toutoune@gmail.com>
;;;
;;; This file is part of GNU Guix.
;;;
;;; GNU Guix is free software; you can redistribute it and/or modify it
;;; under the terms of the GNU General Public License as published by
;;; the Free Software Foundation; either version 3 of the License, or (at
;;; your option) any later version.
;;;
;;; GNU Guix is distributed in the hope that it will be useful, but
;;; WITHOUT ANY WARRANTY; without even the implied warranty of
;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
;;; GNU General Public License for more details.
;;;
;;; You should have received a copy of the GNU General Public License
;;; along with GNU Guix.  If not, see <http://www.gnu.org/licenses/>.

(define-module (guix scripts weather)
  #:use-module (guix ui)
  #:use-module (guix scripts)
  #:use-module (guix packages)
  #:use-module (guix profiles)
  #:use-module (guix derivations)
  #:use-module (guix progress)
  #:use-module (guix monads)
  #:use-module (guix store)
  #:use-module (guix grafts)
  #:use-module (guix gexp)
  #:use-module ((guix build syscalls) #:select (terminal-columns))
  #:use-module ((guix build utils) #:select (every*))
  #:use-module (guix substitutes)
  #:use-module (guix narinfo)
  #:use-module (guix http-client)
  #:use-module (guix ci)
  #:use-module (guix sets)
  #:use-module (guix graph)
  #:autoload   (guix scripts graph) (%bag-node-type)
  #:use-module (gnu packages)
  #:use-module (web uri)
  #:use-module (srfi srfi-1)
  #:use-module (srfi srfi-19)
  #:use-module (srfi srfi-26)
  #:use-module (srfi srfi-34)
  #:use-module (srfi srfi-37)
  #:use-module (ice-9 match)
  #:use-module (ice-9 format)
  #:use-module (ice-9 vlist)
  #:export (guix-weather))

(define (all-packages)
  "Return the list of public packages we are going to query."
  (fold-packages (lambda (package result)
                   (match (package-replacement package)
                     ((? package? replacement)
                      (cons* replacement package result))
                     (#f
                      (cons package result))))
                 '()

                 ;; Dismiss deprecated packages but keep hidden packages.
                 #:select? (negate package-superseded)))

(define (call-with-progress-reporter reporter proc)
  "This is a variant of 'call-with-progress-reporter' that works with monadic
scope."
  ;; TODO: Move to a more appropriate place.
  (with-monad %store-monad
    (start-progress-reporter! reporter)
    (mlet* %store-monad ((report -> (lambda ()
                                      (progress-reporter-report! reporter)))
                         (result (proc report)))
      (stop-progress-reporter! reporter)
      (return result))))

(define* (package-outputs packages
                          #:optional (system (%current-system)))
  "Return the list of outputs of all of PACKAGES for the given SYSTEM."
  (define (lower-object/no-grafts obj system)
    (mlet* %store-monad ((previous (set-grafting #f))
                         (drv      (lower-object obj system))
                         (_        (set-grafting previous)))
      (return drv)))

  (let ((packages (filter (lambda (package)
                            (or (not (package? package))
                                (supported-package? package system)))
                          packages)))
    (format (current-error-port)
            (G_ "computing ~h package derivations for ~a...~%")
            (length packages) system)

    (call-with-progress-reporter (progress-reporter/bar (length packages))
      (lambda (report)
        (foldm %store-monad
               (lambda (package result)
                 ;; PACKAGE could in fact be a non-package object, for example
                 ;; coming from a user-specified manifest.  Thus, use
                 ;; 'lower-object' rather than 'package->derivation' here.
                 (mlet %store-monad ((drv (lower-object/no-grafts package
                                                                  system)))
                   (report)
                   (match (derivation->output-paths drv)
                     (((names . items) ...)
                      (return (append items result))))))
               '()
               packages)))))

(define (call-with-time thunk kont)
  "Call THUNK and pass KONT the elapsed time followed by THUNK's return
values."
  (let* ((start  (current-time time-monotonic))
         (result (call-with-values thunk list))
         (end    (current-time time-monotonic)))
    (apply kont (time-difference end start) result)))

(define-syntax-rule (let/time ((time result ... exp)) body ...)
  (call-with-time (lambda () exp) (lambda (time result ...) body ...)))

(define (histogram field proc seed lst)
  "Return an alist giving a histogram of all the values of FIELD for elements
of LST. FIELD must be a one element procedure that returns a field's value.
For each FIELD value, call PROC with the previous field-specific result.
Example:

  (histogram car (lambda (x n) (+ 1 n)) 0 '((a . x)(b . y)(a . z)))
  => ((a . 2) (b . 1))

meaning that we have two a's and one b."
  (let loop ((lst lst)
             (result '()))
    (match lst
      (()
       result)
      ((head . tail)
       (let ((value (field head)))
         (loop tail
               (match (assoc-ref result value)
                 (#f
                  `((,value . ,(proc head seed)) ,@result))
                 (previous
                  `((,value . ,(proc head previous))
                    ,@(alist-delete value result))))))))))

(define (throughput lst timestamp)
  "Return the throughput, in items per second, given the elements of LST,
calling TIMESTAMP to get the \"timestamp\" of each item."
  (let ((oldest (reduce min +inf.0 (map build-timestamp lst)))
        (now    (time-second (current-time time-utc))))
    (/ (length lst) (- now oldest) 1.)))

(define (queued-subset queue items)
  "Return the subset of ITEMS, a list of store file names, that appears in
QUEUE, a list of builds.  Return #f if elements in QUEUE lack information
about the derivations queued, as is the case with Hydra."
  (define queued
    (append-map (lambda (build)
                  (match (false-if-exception
                          (read-derivation-from-file (build-derivation build)))
                    (#f
                     '())
                    (drv
                     (match (derivation->output-paths drv)
                       (((names . items) ...) items)))))
                queue))

  (if (any (negate build-derivation) queue)
      #f                                          ;no derivation information
      (lset-intersection string=? queued items)))

(define (store-item-system store item)
  "Return the system (a string such as \"aarch64-linux\")) ITEM targets,
or #f if it could not be determined."
  (match (valid-derivers store item)
    ((drv . _)
     (and=> (false-if-exception (read-derivation-from-file drv))
            derivation-system))
    (()
     #f)))

(define* (report-server-coverage server items
                                 #:key display-missing?)
  "Report the subset of ITEMS available as substitutes on SERVER.
When DISPLAY-MISSING? is true, display the list of missing substitutes.
Return the coverage ratio, an exact number between 0 and 1."
  (define MiB (* (expt 2 20) 1.))

  (format #t (G_ "looking for ~h store items on ~a...~%")
          (length items) server)

  (let/time ((time narinfos requests-made
                   (lookup-narinfos
                    server items
                    #:make-progress-reporter
                    (lambda* (total #:key url #:allow-other-keys)
                      (progress-reporter/bar total)))))
    (format #t "~a~%" server)
    (let ((obtained  (length narinfos))
          (requested (length items))
          (missing   (lset-difference string=?
                                      items (map narinfo-path narinfos)))
          (sizes     (append-map (lambda (narinfo)
                                   (filter integer?
                                           (narinfo-file-sizes narinfo)))
                                 narinfos))
          (time      (+ (time-second time)
                        (/ (time-nanosecond time) 1e9))))
      (format #t (G_ "  ~,1f% substitutes available (~h out of ~h)~%")
              (* 100. (/ obtained requested 1.))
              obtained requested)
      (let ((total (/ (reduce + 0 sizes) MiB)))
        (match (length sizes)
          ((? zero?)
           (format #t (G_  "  unknown substitute sizes~%")))
          (len
           (if (= len obtained)
               (format #t (G_ "  ~,1h MiB of nars (compressed)~%") total)
               (format #t (G_ "  at least ~,1h MiB of nars (compressed)~%")
                       total)))))
      (format #t (G_ "  ~,1h MiB on disk (uncompressed)~%")
              (/ (reduce + 0 (map narinfo-size narinfos)) MiB))
      (when (> requests-made 0)
        (format #t (G_ "  ~,3h seconds per request (~,1h seconds in total)~%")
                (/ time requests-made 1.) time)
        (format #t (G_ "  ~,1h requests per second~%")
                (/ requests-made time 1.)))

      (guard (c ((http-get-error? c)
                 (if (= 404 (http-get-error-code c))
                     (format (current-error-port)
                             (G_ "  (continuous integration information \
unavailable)~%"))
                     (format (current-error-port)
                             (G_ "  '~a' returned ~a (~s)~%")
                             (uri->string (http-get-error-uri c))
                             (http-get-error-code c)
                             (http-get-error-reason c)))))
        (let* ((max    %query-limit)
               (queue  (queued-builds server max))
               (len    (length queue))
               (histo  (histogram build-system
                                  (lambda (build count)
                                    (+ 1 count))
                                  0 queue)))
          (newline)
          (unless (null? missing)
            (match (queued-subset queue missing)
              (#f #f)
              ((= length queued)
               (let ((missing (length missing)))
                 (format #t (G_ "  ~,1f% (~h out of ~h) of the missing items \
are queued~%")
                         (* 100. (/ queued missing))
                         queued missing)))))

          (if (>= len max)
              (format #t (G_ "  at least ~h queued builds~%") len)
              (format #t (G_ "  ~h queued builds~%") len))
          (for-each (match-lambda
                      ((system . count)
                       (format #t (G_ "      ~a: ~a (~0,1f%)~%")
                               system count (* 100. (/ count len)))))
                    histo))

        (let* ((latest     (latest-builds server))
               (builds/sec (throughput latest build-timestamp)))
          (format #t (G_ "  build rate: ~1,2f builds per hour~%")
                  (* builds/sec 3600.))
          (for-each (match-lambda
                      ((system . builds)
                       (format #t (G_ "      ~a: ~,2f builds per hour~%")
                               system
                               (* (throughput builds build-timestamp)
                                  3600.))))
                    (histogram build-system cons '() latest))))

      (when (and display-missing? (not (null? missing)))
        (newline)
        (format #t (G_ "Substitutes are missing for the following items:~%"))

        ;; Display two columns: store items, and their system type.
        (format #t "~:{  ~a ~a~%~}"
                (zip (map (let ((width (max (- (current-terminal-columns)
                                               20)
                                            0)))
                            (lambda (item)
                              (if (> (string-length item) width)
                                  item
                                  (string-pad-right item width))))
                          missing)
                     (with-store store
                       (map (lambda (item)
                              (or (store-item-system store item)
                                  (G_ "unknown system")))
                            missing)))))

      ;; Return the coverage ratio.
      (let ((total (length items)))
        (/ (- total (length missing)) total)))))


;;;
;;; Command-line options.
;;;

(define (show-help)
  (display (G_ "Usage: guix weather [OPTIONS] [PACKAGES ...]
Report the availability of substitutes.\n"))
  (display (G_ "
      --substitute-urls=URLS
                         check for available substitutes at URLS"))
  (display (G_ "
  -m, --manifest=MANIFEST
                         look up substitutes for packages specified in MANIFEST"))
  (display (G_ "
  -c, --coverage[=COUNT]
                         show substitute coverage for packages with at least
                         COUNT dependents"))
  (display (G_ "
      --display-missing  display the list of missing substitutes"))
  (display (G_ "
  -s, --system=SYSTEM    consider substitutes for SYSTEM--e.g., \"i686-linux\""))
  (newline)
  (display (G_ "
  -h, --help             display this help and exit"))
  (display (G_ "
  -V, --version          display version information and exit"))
  (newline)
  (show-bug-report-information))

(define %options
  (list  (option '(#\h "help") #f #f
                 (lambda args
                   (show-help)
                   (exit 0)))
         (option '(#\V "version") #f #f
                 (lambda args
                   (show-version-and-exit "guix weather")))

         (option '("substitute-urls") #t #f
                 (lambda (opt name arg result . rest)
                   (let ((urls (string-tokenize arg)))
                     (for-each (lambda (url)
                                 (unless (string->uri url)
                                   (leave (G_ "~a: invalid URL~%") url)))
                               urls)
                     (apply values
                            (alist-cons 'substitute-urls urls
                                        (alist-delete 'substitute-urls result))
                            rest))))
         (option '(#\m "manifest") #t #f
                 (lambda (opt name arg result)
                   (alist-cons 'manifest arg result)))
         (option '(#\c "coverage") #f #t
                 (lambda (opt name arg result)
                   (alist-cons 'coverage
                               (if arg (string->number* arg) 0)
                               result)))
         (option '("display-missing") #f #f
                 (lambda (opt name arg result)
                   (alist-cons 'display-missing? #t result)))
         (option '(#\s "system") #t #f
                 (lambda (opt name arg result)
                   (alist-cons 'system arg result)))))

(define %default-options
  `((substitute-urls . ,%default-substitute-urls)))

(define (load-manifest file)
  "Load the manifest from FILE and return the list of packages it refers to."
  (let* ((user-module (make-user-module '((guix profiles) (gnu))))
         (manifest    (load* file user-module)))
    (delete-duplicates (map manifest-entry-item
                            (manifest-transitive-entries manifest))
                       eq?)))


;;;
;;; Missing package substitutes.
;;;

(define* (package-partition-boundary pred packages
                                     #:key (system (%current-system)))
  "Return the subset of PACKAGES that are at the \"boundary\" between those
that match PRED and those that don't.  The returned packages themselves do not
match PRED but they have at least one direct dependency that does.

Note: The assumption is that, if P matches PRED, then all the dependencies of
P match PRED as well."
  ;; XXX: Graph theoreticians surely have something to teach us about this...
  (let loop ((packages packages)
             (result (setq))
             (visited vlist-null))
    (define (visited? package)
      (vhash-assq package visited))

    (match packages
      ((package . rest)
       (cond ((visited? package)
              (loop rest result visited))
             ((pred package)
              (loop rest result (vhash-consq package #t visited)))
             (else
              (let* ((bag  (package->bag package system))
                     (deps (filter-map (match-lambda
                                         ((label (? package? package) . _)
                                          (and (not (pred package))
                                               package))
                                         (_ #f))
                                       (bag-direct-inputs bag))))
                (loop (append deps rest)
                      (if (null? deps)
                          (set-insert package result)
                          result)
                      (vhash-consq package #t visited))))))
      (()
       (set->list result)))))

(define (package->output-mapping packages system)
  "Return a vhash that maps each item of PACKAGES to its corresponding output
store file names for SYSTEM."
  (foldm %store-monad
         (lambda (package mapping)
           (mlet %store-monad ((drv (package->derivation package system
                                                         #:graft? #f)))
             (return (vhash-consq package
                                  (match (derivation->output-paths drv)
                                    (((names . outputs) ...)
                                     outputs))
                                  mapping))))
         vlist-null
         packages))

(define (substitute-oracle server items)
  "Return a procedure that, when passed a store item (one of those listed in
ITEMS), returns true if SERVER has a substitute for it, false otherwise."
  (define available
    (fold (lambda (narinfo set)
            (set-insert (narinfo-path narinfo) set))
          (set)
          (lookup-narinfos server items)))

  (cut set-contains? available <>))

(define* (report-package-coverage-per-system server packages system
                                             #:key (threshold 0))
  "Report on the subset of PACKAGES that lacks SYSTEM substitutes on SERVER,
sorted by decreasing number of dependents.  Do not display those with less
than THRESHOLD dependents."
  (mlet* %store-monad ((packages -> (package-closure packages #:system system))
                       (mapping    (package->output-mapping packages system))
                       (back-edges (node-back-edges %bag-node-type packages)))
    (define items
      (vhash-fold (lambda (package items result)
                    (append items result))
                  '()
                  mapping))

    (define substitutable?
      (substitute-oracle server items))

    (define substitutable-package?
      (lambda (package)
        (match (vhash-assq package mapping)
          ((_ . items)
           (find substitutable? items))
          (#f
           #f))))

    (define missing
      (package-partition-boundary substitutable-package? packages
                                  #:system system))

    (define missing-count
      (length missing))

    (if (zero? threshold)
        (format #t (N_ "The following ~a package is missing from '~a' for \
'~a':~%"
                       "The following ~a packages are missing from '~a' for \
'~a':~%"
                       missing-count)
                missing-count server system)
        (format #t (N_ "~a package is missing from '~a' for '~a':~%"
                       "~a packages are missing from '~a' for '~a', among \
which:~%"
                       missing-count)
                missing-count server system))

    (for-each (match-lambda
                ((package count)
                 (match (vhash-assq package mapping)
                   ((_ . items)
                    (when (>= count threshold)
                      (format #t "  ~4d\t~a@~a\t~{~a ~}~%"
                              count
                              (package-name package) (package-version package)
                              items)))
                   (#f                      ;PACKAGE must be an internal thing
                    #f))))
              (sort (zip missing
                         (map (lambda (package)
                                (node-reachable-count (list package)
                                                      back-edges))
                              missing))
                    (match-lambda*
                      (((_ count1) (_ count2))
                       (< count2 count1)))))
    (return #t)))

(define* (report-package-coverage server packages systems
                                  #:key (threshold 0))
  "Report on the substitute coverage for PACKAGES, for each of SYSTEMS, on
SERVER.  Display information for packages with at least THRESHOLD dependents."
  (with-store store
    (run-with-store store
      (foldm %store-monad
             (lambda (system _)
               (report-package-coverage-per-system server packages system
                                                   #:threshold threshold))
             #f
             systems))))


;;;
;;; Entry point.
;;;

(define-command (guix-weather . args)
  (synopsis "report on the availability of pre-built package binaries")

  (define (package-list opts)
    ;; Return the package list specified by OPTS.
    (let ((files (filter-map (match-lambda
                               (('manifest . file) file)
                               (_ #f))
                             opts))
          (base  (filter-map (match-lambda
                               (('argument . spec)
                                (specification->package spec))
                               (_
                                #f))
                             opts)))
      (if (and (null? files) (null? base))
          (all-packages)
          (append base (append-map load-manifest files)))))

  (with-error-handling
    (parameterize ((current-terminal-columns (terminal-columns))

                   ;; Set grafting upfront in case the user's input depends on
                   ;; it (e.g., a manifest or code snippet that calls
                   ;; 'gexp->derivation').
                   (%graft?                  #f))
      (let* ((opts     (parse-command-line args %options
                                           (list %default-options)
                                           #:build-options? #f))
             (urls     (assoc-ref opts 'substitute-urls))
             (systems  (match (filter-map (match-lambda
                                            (('system . system) system)
                                            (_ #f))
                                          opts)
                         (() (list (%current-system)))
                         (systems systems)))
             (packages (package-list opts))
             (items    (with-store store
                         (concatenate
                          (run-with-store store
                            (mapm %store-monad
                                  (lambda (system)
                                    (package-outputs packages system))
                                  systems))))))
        (exit
         (every* (lambda (server)
                   (define coverage
                     (report-server-coverage server items
                                             #:display-missing?
                                             (assoc-ref opts 'display-missing?)))
                   (match (assoc-ref opts 'coverage)
                     (#f #f)
                     (threshold
                      ;; PACKAGES may include non-package objects coming from a
                      ;; manifest.  Filter them out.
                      (report-package-coverage server
                                               (filter package? packages)
                                               systems
                                               #:threshold threshold)))

                   (= 1 coverage))
                 urls))))))

;;; Local Variables:
;;; eval: (put 'let/time 'scheme-indent-function 1)
;;; End: