aboutsummaryrefslogtreecommitdiff
path: root/guix-build-coordinator/utils.scm
blob: 0a3383cf68c873044362ddc4bc8784c9ab255d96 (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
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
(define-module (guix-build-coordinator utils)
  #:use-module (srfi srfi-1)
  #:use-module (srfi srfi-11)
  #:use-module (srfi srfi-19)
  #:use-module (srfi srfi-26)
  #:use-module (srfi srfi-60)
  #:use-module (ice-9 q)
  #:use-module (ice-9 ftw)
  #:use-module (ice-9 popen)
  #:use-module (ice-9 iconv)
  #:use-module (ice-9 match)
  #:use-module (ice-9 format)
  #:use-module (ice-9 threads)
  #:use-module (ice-9 textual-ports)
  #:use-module (ice-9 rdelim)
  #:use-module (ice-9 binary-ports)
  #:use-module (ice-9 suspendable-ports)
  #:use-module ((ice-9 ports internal) #:select (port-poll))
  #:use-module (ice-9 exceptions)
  #:use-module (rnrs bytevectors)
  #:use-module (web uri)
  #:use-module (web http)
  #:use-module (web client)
  #:use-module (web request)
  #:use-module (web response)
  #:use-module (gcrypt pk-crypto)
  #:use-module (gcrypt hash)
  #:use-module (gcrypt random)
  #:use-module (json)
  #:use-module (prometheus)
  #:use-module (guix pki)
  #:use-module (guix utils)
  #:use-module (guix config)
  #:use-module (guix store)
  #:use-module (guix status)
  #:use-module (guix base64)
  #:use-module (guix progress)
  #:use-module (guix derivations)
  #:use-module ((guix http-client)
                #:select (http-fetch))
  #:use-module (guix serialization)
  #:use-module ((guix build syscalls)
                #:select (set-thread-name))
  #:use-module (guix scripts substitute)
  #:export (random-v4-uuid

            &port-timeout
            &port-read-timeout
            &port-write-timeout

            port-timeout-error?
            port-read-timeout-error?
            port-write-timeout-error?

            with-port-timeouts

            request-query-parameters

            call-with-streaming-http-request

            find-missing-substitutes-for-output
            has-substiutes-no-cache?

            read-derivation-from-file*

            non-blocking-port

            substitute-derivation
            read-derivation-through-substitutes

            narinfo-string

            retry-on-error

            s3-list-objects
            s3-cp

            log-delay
            call-with-delay-logging

            with-time-logging

            create-work-queue
            create-thread-pool

            throttle

            get-load-average
            get-uptime

            running-on-the-hurd?

            get-guix-memory-metrics-updater

            open-socket-for-uri*

            check-locale!))

(eval-when (eval load compile)
  (begin
    (when (defined? 'narinfo-references (resolve-module '(guix narinfo)))
      ;; This module contains narinfo-references in newer version of Guix
      (use-modules (guix narinfo)))

    (when (defined? 'lookup-narinfos (resolve-module '(guix substitutes)))
      ;; This module may contain lookup-narinfos in newer version of Guix
      (use-modules (guix substitutes)))))

(define (random-v4-uuid)
  ;; https://tools.ietf.org/html/rfc4122#page-14
  ;;
  ;; The pattern in characters is: 8, 4, 4, 4, 12
  ;; The pattern in bytes is:      4, 2, 2, 2, 6
  ;;
  ;; time-low "-" time-mid "-" time-high-and-version "-"
  ;;   clock-seq-and-reserved clock-seq-low "-" node
  ;;
  ;; - Set the two most significant bits (bits 6 and 7) of the
  ;;   clock_seq_hi_and_reserved to zero and one, respectively.
  ;; - Set the four most significant bits (bits 12 through 15) of the
  ;;   time_hi_and_version field to the 4-bit version number from
  ;;   Section 4.1.3.

  (let* ((bytes 16)
         (bv (gen-random-bv bytes)))

    (let ((version 4)
          (6th-byte (array-ref bv 6))   ; Most significant byte in
                                        ; time_hi_and_version
          (8th-byte (array-ref bv 8)))  ; Most significant byte in
                                        ; clock_seq_hi_and_reserved

      (array-set!
       bv
       (logior (logand #b00001111 6th-byte)
               (rotate-bit-field version 4 0 8))
       6)

      (array-set!
       bv
       ;; Set bits 6 and 7 to 0 and 1 respectively
       (logior #b10000000
               (logand #b00111111
                       8th-byte))
       8))

    (let* ((int
            (bytevector-uint-ref bv 0 (endianness big) bytes))
           (hex-string
            (format #f "~32,'0x" int)))
      (string-join
       (fold (lambda (part-length result)
               (let ((start (string-length
                             (string-join result ""))))
                 (append
                  result
                  (list (substring hex-string
                                   start
                                   (+ start part-length))))))
             '()
             (list 8 4 4 4 12))
       "-"))))


(define (request-query-parameters request)
  (define (parse-query-string query)
    "Parse and decode the URI query string QUERY and return an alist."
    (let lp ((lst (map uri-decode (string-split query (char-set #\& #\=)))))
      (match lst
        ((key value . rest)
         (cons (cons key value) (lp rest)))
        (("") '())
        (() '()))))

  (let ((query (uri-query (request-uri request))))
    (if (and query
             (not (string-null? query)))
        (map (match-lambda
               ((name . value)
                (cons (string->symbol name)
                      value)))
             (parse-query-string query))
        '())))

(define &port-timeout
  (make-exception-type '&port-timeout
                       &external-error
                       '(port)))

(define make-port-timeout-error
  (record-constructor &port-timeout))

(define port-timeout-error?
  (record-predicate &port-timeout))

(define &port-read-timeout
  (make-exception-type '&port-read-timeout
                       &port-timeout
                       '()))

(define make-port-read-timeout-error
  (record-constructor &port-read-timeout))

(define port-read-timeout-error?
  (record-predicate &port-read-timeout))

(define &port-write-timeout
  (make-exception-type '&port-write-timeout
                       &port-timeout
                       '()))

(define make-port-write-timeout-error
  (record-constructor &port-write-timeout))

(define port-write-timeout-error?
  (record-predicate &port-write-timeout))

(define* (with-port-timeouts thunk #:key (timeout (* 120 1000)))

  ;; When the GC runs, it restarts the poll syscall, but the timeout remains
  ;; unchanged! When the timeout is longer than the time between the syscall
  ;; restarting, I think this renders the timeout useless. Therefore, this
  ;; code uses a short timeout, and repeatedly calls poll while watching the
  ;; clock to see if it has timed out overall.
  (define poll-timeout-ms 200)

  (define (wait port mode)
    (let ((timeout-internal
           (+ (get-internal-real-time)
              (* internal-time-units-per-second
                 (/ timeout 1000)))))
      (let loop ((poll-value
                  (port-poll port mode poll-timeout-ms)))
        (if (= poll-value 0)
            (if (> (get-internal-real-time)
                   timeout-internal)
                (raise-exception
                 (if (string=? mode "r")
                     (make-port-read-timeout-error port)
                     (make-port-write-timeout-error port)))
                (loop (port-poll port mode poll-timeout-ms)))
            poll-value))))

  (parameterize
      ((current-read-waiter
        (lambda (port)
          (wait port "r")))
       (current-write-waiter
        (lambda (port)
          (wait port "w"))))
    (thunk)))

(define* (call-with-streaming-http-request uri
                                           content-length
                                           callback
                                           #:key (headers '())
                                           (method 'PUT)
                                           streaming?)
  (with-port-timeouts
   (lambda ()
     (let* ((port (open-socket-for-uri* uri))
            (request
             (build-request
              uri
              #:method method
              #:version '(1 . 1)
              #:headers `((connection close)
                          (content-length . ,content-length)
                          (Content-Type . "application/octet-stream")
                          ;; read-request-body/patch looks for this header
                          (Stream-Body . "true")
                          ,@headers)
              #:port port)))

       (set-port-encoding! port "ISO-8859-1")
       (setvbuf port 'block (expt 2 13))
       (with-exception-handler
           (lambda (exp)
             (simple-format #t "error: ~A ~A: ~A\n"
                            method (uri-path uri) exp)
             (close-port port)
             (raise-exception exp))
         (lambda ()
           (let ((request (write-request request port)))
             (callback port)
             (force-output port)

             (let ((response (read-response port)))
               (if streaming?
                   (values response
                           (response-body-port response))
                   (let ((body (read-response-body response)))
                     (close-port port)
                     (values response
                             body)))))))))))

(define (find-missing-substitutes-for-output store substitute-urls output)
  (if (valid-path? store output)
      '()
      (let ((narinfo
             (any (lambda (substitute-url)
                    (let ((result (lookup-narinfos substitute-url (list output))))
                      (if (null? result)
                          #f
                          (first result))))
                  substitute-urls)))
        (if narinfo
            (append-map
             (lambda (reference)
               (let ((referenced-output
                      (string-append (%store-prefix) "/" reference)))
                 (if (string=? referenced-output output)
                     '()
                     (find-missing-substitutes-for-output store
                                                          substitute-urls
                                                          referenced-output))))
             (narinfo-references narinfo))
            (list output)))))

;; Work around Guix holding on to broken connections to substitute servers
;; (because of mishandling gnutls errors).
(let ((mod (resolve-module '(guix scripts substitute))))
  (when (module-variable mod '%max-cached-connections)
    (module-set! mod '%max-cached-connections 0)))

(define (has-substiutes-no-cache? substitute-urls file)
  (define %narinfo-cache-directory
    (if (zero? (getuid))
        (or (and=> (getenv "XDG_CACHE_HOME")
                   (cut string-append <> "/guix/substitute"))
            (string-append %state-directory "/substitute/cache"))
        (string-append (cache-directory #:ensure? #f) "/substitute")))

  ;; Because there's no control over the caching of 404 lookups, and I'd
  ;; rather not reach inside and monkey patch the Guix code, just delete any
  ;; cache files
  (let ((hash-part (store-path-hash-part file))
        (directories
         (scandir %narinfo-cache-directory
                  (lambda (s) (= (string-length s) 52)))))

    (for-each (lambda (directory)
                (let ((cache-file
                       (string-append
                        %narinfo-cache-directory "/"
                        directory "/" hash-part)))
                  ;; Use monitor to avoid multiple threads trying to delete
                  ;; the same file at the same time
                  (monitor
                   (when (file-exists? cache-file)
                     (with-exception-handler
                         (lambda (exn)
                           (simple-format
                            (current-error-port)
                            "error: when deleting substitute cache file: ~A\n"
                            exn))
                       (lambda ()
                         (delete-file cache-file))
                       #:unwind? #t)))))
              (or directories '())))

  (let ((substitute-urls
         (append-map (lambda (substitute-url)
                       (let ((log-port (open-output-string)))
                         (with-throw-handler #t
                           (lambda ()
                             (if (null?
                                  ;; I doubt the caching is thread safe, so
                                  ;; only make one request at a time.
                                  (monitor
                                   (parameterize ((current-error-port log-port))
                                     (lookup-narinfos substitute-url
                                                      (list file)))))
                                 '()
                                 (list substitute-url)))
                           (lambda (key . args)
                             (simple-format
                              (current-error-port)
                              "exception in has-substiutes-no-cache? (~A) ~A: ~A\n"
                              substitute-url key args)
                             (display (string-append
                                       (get-output-string log-port)
                                       "\n")
                                      (current-error-port))
                             (close-output-port log-port)))))
                     substitute-urls)))
    substitute-urls))

(define (non-blocking-port port)
  "Make PORT non-blocking and return it."
  (let ((flags (fcntl port F_GETFL)))
    (when (zero? (logand O_NONBLOCK flags))
      (fcntl port F_SETFL (logior O_NONBLOCK flags)))
    port))

(define* (substitute-derivation store
                                derivation-name
                                #:key substitute-urls)
  (let ((log-port (open-output-string)))
    (apply set-build-options
           store
           `(,@(if substitute-urls
                   `(#:substitute-urls ,substitute-urls)
                   '())
             #:max-silent-time 60
             #:timeout ,(* 10 60)))

    (with-exception-handler
        (lambda (exn)
          (let* ((log-string
                  (get-output-string log-port))
                 (lines
                  (string-split log-string #\newline))
                 (last-n-lines
                  (if (< 10 (length lines))
                      (take-right lines 10)
                      lines)))
            (close-output-port log-port)
            (simple-format
             (current-error-port)
             "exception when substituting derivation: ~A:\n  ~A\n"
             exn (string-join last-n-lines "\n"))
            (raise-exception exn)))
      (lambda ()
        (parameterize ((current-build-output-port log-port))
          (ensure-path store derivation-name)))
      #:unwind? #t)))

(define read-derivation-from-file*
  (let ((%derivation-cache
         (@@ (guix derivations) %derivation-cache)))
    (lambda (file)
      (or (and file (hash-ref %derivation-cache file))
          (let ((drv
                 ;; read-derivation can call read-derivation-from-file, so to
                 ;; avoid having many open files when reading a derivation with
                 ;; inputs, read it in to a string first.
                 (call-with-input-string
                     ;; Avoid calling scm_i_relativize_path in
                     ;; fport_canonicalize_filename since this leads to lots
                     ;; of readlink calls
                     (with-fluids ((%file-port-name-canonicalization 'none))
                       (call-with-input-file file
                         get-string-all))
                   (lambda (port)
                     (set-port-filename! port file)
                     (read-derivation port read-derivation-from-file*)))))
            (hash-set! %derivation-cache file drv)
            drv)))))

(define (read-derivation-through-substitutes derivation-name
                                             substitute-urls)
  (define %fetch-timeout
    5)

  (define open-connection-for-uri/cached
    (let ((cache '()))
      (lambda* (uri #:key fresh? (timeout %fetch-timeout) verify-certificate?)
        (define host (uri-host uri))
        (define scheme (uri-scheme uri))
        (define key (list host scheme (uri-port uri)))

        (and (not (memq scheme '(file #f)))
             (match (assoc-ref cache key)
               (#f
                (let ((socket
                       (open-socket-for-uri*
                        uri
                        #:verify-certificate? verify-certificate?)))
                  (set! cache (alist-cons key socket cache))
                  socket))
               (socket
                (if (or fresh? (port-closed? socket))
                    (begin
                      (false-if-exception (close-port socket))
                      (set! cache (alist-delete key cache))
                      (open-connection-for-uri/cached uri #:timeout timeout
                                                      #:verify-certificate?
                                                      verify-certificate?))
                    (begin
                      ;; Drain input left from the previous use.
                      (drain-input socket)
                      socket))))))))

  (define (derivation-name->bytevector derivation-name)
    (let ((narinfo
           (any (lambda (substitute-url)
                  (let ((result (lookup-narinfos substitute-url
                                                 (list derivation-name)
                                                 #:open-connection
                                                 open-connection-for-uri/cached)))
                    (if (null? result)
                        #f
                        (first result))))
                substitute-urls)))
      (if narinfo
          (let*-values (((uri compression file-size)
                         (narinfo-best-uri narinfo))
                        ((raw download-size)
                         (http-fetch
                          (first (narinfo-uris narinfo))
                          #:open-connection open-connection-for-uri/cached
                          #:keep-alive? #t))
                        ((progress)
                         (let* ((dl-size  (or download-size
                                              (and (equal? compression "none")
                                                   (narinfo-size narinfo)))))
                           ;; Keep RAW open upon completion so we can later
                           ;; reuse the underlying connection.  Pass the
                           ;; download size so that this procedure won't block
                           ;; reading from RAW.
                           (progress-report-port progress-reporter/silent
                                                 raw
                                                 #:close? #f
                                                 #:download-size dl-size)))
                        ((input pids)
                         ;; NOTE: This 'progress' port of current process will be
                         ;; closed here, while the child process doing the
                         ;; reporting will close it upon exit.
                         (decompressed-port (string->symbol compression)
                                            progress)))

            ;; The decompressor can be an external program, so wait for it to
            ;; exit
            (every (compose zero? cdr waitpid) pids)

            (fold-archive (lambda (file type contents result)
                            (match contents
                              ((port . bytes)
                               (get-bytevector-n port bytes))))
                          #f
                          input
                          #f))
          (error "could not fetch narinfo"))))

  (define (read-derivation-from-file/custom derivation-name)
    ;; Try the local store
    (or (and (file-exists? derivation-name)
             (with-store/non-blocking
              store
              (valid-path? store derivation-name))
             (read-derivation-from-file* derivation-name))

        ;; Otherwise try the network
        (call-with-input-bytevector
         (derivation-name->bytevector derivation-name)
         (lambda (port)
           (set-port-filename! port derivation-name)
           (read-derivation port
                            read-derivation-from-file/custom)))))

  (read-derivation-from-file/custom derivation-name))

(define* (narinfo-string store-path hash size references
                         compressed-files
                         #:key (nar-path "nar")
                         system derivation
                         public-key private-key)
  (define (signed-string s)
    (let* ((hash (bytevector->hash-data (sha256 (string->utf8 s))
                                        #:key-type (key-type public-key))))
      (signature-sexp hash private-key public-key)))

  (define base64-encode-string
    (compose base64-encode string->utf8))

  (define* (store-item->recutils compression file-size)
    (let ((url (encode-and-join-uri-path
                `(,@(split-and-decode-uri-path nar-path)
                  ,@(if (eq? compression 'none)
                        '()
                        (list (symbol->string compression)))
                  ,(basename store-path)))))
      (format #f "URL: ~a~%Compression: ~a~%~@[FileSize: ~a~%~]"
              url
              (or compression "none")
              file-size)))

  (let* ((base-info  (format #f
                             "\
StorePath: ~a
NarHash: sha256:~a
NarSize: ~d
References: ~a~%"
                             store-path
                             hash
                             size
                             (string-join references " ")))
         ;; Do not render a "Deriver" or "System" line if we are rendering
         ;; info for a derivation.
         (info       (if derivation
                         (format #f "~aSystem: ~a~%Deriver: ~a~%"
                                 base-info
                                 system
                                 (basename derivation))
                         base-info))
         (signature  (base64-encode-string
                      (canonical-sexp->string (signed-string info)))))
    (format #f "~aSignature: 1;~a;~a~%~{~a~}"
            info
            (gethostname)
            signature
            (map (match-lambda
                   ((compression compressed-size)
                    (store-item->recutils compression
                                          compressed-size)))
                 compressed-files))))

(define* (retry-on-error f #:key times delay ignore no-retry error-hook
                         (sleep-impl sleep))
  (let loop ((attempt 1))
    (match (with-exception-handler
               (lambda (exn)
                 (if (cond
                      ((list? ignore)
                       (any (lambda (test)
                              (test exn))
                            ignore))
                      ((procedure? ignore)
                       (ignore exn))
                      (else #f))
                     `(#t . (,exn))
                     (begin
                       (when (cond
                              ((list? no-retry)
                               (any (lambda (test)
                                      (test exn))
                                    no-retry))
                              ((procedure? no-retry)
                               (no-retry exn))
                              (else #f))
                         (raise-exception exn))

                       (cons #f exn))))
             (lambda ()
               (call-with-values f
                 (lambda vals
                   (cons #t vals))))
             #:unwind? #t)
      ((#t . return-values)
       (when (> attempt 1)
         (simple-format
          (current-error-port)
          "retry success: ~A\n  on attempt ~A of ~A\n"
          f
          attempt
          times))
       (apply values return-values))
      ((#f . exn)
       (if (>= attempt
               (- times 1))
           (begin
             (simple-format
              (current-error-port)
              "error: ~A:\n  ~A,\n  attempt ~A of ~A, last retry in ~A\n"
              f
              exn
              attempt
              times
              delay)
             (when error-hook
               (error-hook attempt exn))
             (sleep-impl delay)
             (simple-format
              (current-error-port)
              "running last retry of ~A after ~A failed attempts\n"
              f
              attempt)
             (f))
           (begin
             (simple-format
              (current-error-port)
              "error: ~A:\n  ~A,\n  attempt ~A of ~A, retrying in ~A\n"
              f
              exn
              attempt
              times
              delay)
             (when error-hook
               (error-hook attempt exn))
             (sleep-impl delay)
             (loop (+ 1 attempt))))))))

(define* (s3-list-objects s3-bucket prefix
                          #:key
                          (command "aws")
                          (command-line-arguments '()))
  (let ((command
         `(,command
           "s3api"
           ,@command-line-arguments
           "list-objects"
           "--bucket" ,s3-bucket
           "--prefix" ,prefix)))
    (simple-format
     #t
     "running: ~A\n"
     (string-join command))
    (let ((pipe (apply open-pipe*
                       OPEN_READ
                       command)))

      (let ((output    (get-string-all pipe))
            (exit-code (status:exit-val
                        (close-pipe pipe))))
        (if (zero? exit-code)
            (if (string-null? output)
                '()
                (json-string->scm output))
            (raise-exception
             (make-exception-with-message
              output)))))))

(define* (s3-cp s3-bucket source destination
                #:key
                (command "aws")
                (command-line-arguments '()))
  (let ((command
         `(,command
           "s3"
           ,@command-line-arguments
           "cp"
           ,source
           ,(simple-format #f "s3://~A/~A"
                           s3-bucket destination))))
    (simple-format
     #t
     "running: ~A\n"
     (string-join command))
    (let ((exit-code
           (status:exit-val
            (apply system* command))))
      (unless (zero? exit-code)
        (raise-exception
         (make-exception-with-message
          (simple-format
           #f
           "error: command failed (~A): ~A\n"
           exit-code
           command))))
      #t)))

(define delay-logging-fluid
  (make-thread-local-fluid))
(define delay-logging-depth-fluid
  (make-thread-local-fluid 0))

(define (log-delay proc duration)
  (and=> (fluid-ref delay-logging-fluid)
         (lambda (recorder)
           (recorder proc duration))))

(define* (call-with-delay-logging proc #:key (threshold 1) (args '()))
  (let ((start (get-internal-real-time))
        (trace '())
        (root-logger? (eq? #f (fluid-ref delay-logging-fluid))))

    (define (format-seconds seconds)
      (format #f "~5f" seconds))

    (call-with-values
        (lambda ()
          (with-fluid* delay-logging-depth-fluid
            (+ 1 (fluid-ref delay-logging-depth-fluid))
            (lambda ()
              (if root-logger?
                  (with-fluid* delay-logging-fluid
                    (lambda (proc duration)
                      (set! trace
                            (cons (list proc
                                        duration
                                        (fluid-ref delay-logging-depth-fluid))
                                  trace))
                      #t)
                    (lambda ()
                      (apply proc args)))
                  (apply proc args)))))
      (lambda vals
        (let ((elapsed-seconds
               (/ (- (get-internal-real-time)
                     start)
                  internal-time-units-per-second)))
          (if (and (> elapsed-seconds threshold)
                   root-logger?)
              (let ((lines
                     (cons
                      (simple-format #f "warning: delay of ~A seconds: ~A"
                                     (format-seconds elapsed-seconds)
                                     proc)
                      (filter-map
                       (match-lambda
                         ((proc duration depth)
                          (if (>= duration 0.001)
                              (string-append
                               (make-string (* 2 depth) #\space)
                               (simple-format #f "~A: ~A"
                                              (format-seconds duration)
                                              proc))
                              #f)))
                       trace))))
                (display (string-append
                          (string-join lines "\n")
                          "\n")))
              (unless root-logger?
                ((fluid-ref delay-logging-fluid) proc elapsed-seconds))))
        (apply values vals)))))

(define (call-with-time-logging name thunk)
  (let ((start   (current-time time-utc)))
    (call-with-values
        thunk
      (lambda vals
        (let* ((end     (current-time time-utc))
               (elapsed (time-difference end start)))
          (display
           (format #f
                   "~a took ~f seconds~%"
                   name
                   (+ (time-second elapsed)
                      (/ (time-nanosecond elapsed) 1e9))))
          (apply values vals))))))

(define-syntax-rule (with-time-logging name exp ...)
  "Log under NAME the time taken to evaluate EXP."
  (call-with-time-logging name (lambda () exp ...)))

(define* (create-work-queue thread-count-parameter proc
                            #:key thread-start-delay
                            (thread-stop-delay
                             (make-time time-duration 0 0))
                            (name "unnamed")
                            priority<?)
  (let ((queue (make-q))
        (queue-mutex (make-mutex))
        (job-available (make-condition-variable))
        (running-job-args (make-hash-table)))

    (define get-thread-count
      (cond
       ((number? thread-count-parameter)
        (const thread-count-parameter))
       ((eq? thread-count-parameter #f)
        ;; Run one thread per job
        (lambda ()
          (+ (q-length queue)
             (hash-count (lambda (index val)
                           (list? val))
                         running-job-args))))
       (else
        thread-count-parameter)))

    (define process-job
      (if priority<?
          (lambda* (args #:key priority)
            (with-mutex queue-mutex
              (enq! queue (cons priority args))
              (set-car!
               queue
               (stable-sort! (car queue)
                             (lambda (a b)
                               (priority<?
                                (car a)
                                (car b)))))
              (sync-q! queue)
              (start-new-threads-if-necessary (get-thread-count))
              (signal-condition-variable job-available)))
          (lambda args
            (with-mutex queue-mutex
              (enq! queue args)
              (start-new-threads-if-necessary (get-thread-count))
              (signal-condition-variable job-available)))))

    (define (count-threads)
      (with-mutex queue-mutex
        (hash-count (const #t) running-job-args)))

    (define (count-jobs)
      (with-mutex queue-mutex
        (+ (q-length queue)
           (hash-count (lambda (index val)
                         (list? val))
                       running-job-args))))

    (define (list-jobs)
      (with-mutex queue-mutex
        (append (if priority<?
                    (map cdr (car queue))
                    (list-copy (car queue)))
                (hash-fold (lambda (key val result)
                             (if val
                                 (cons val result)
                                 result))
                           '()
                           running-job-args))))

    (define (thread-process-job job-args)
      (with-exception-handler
          (lambda (exn)
            (simple-format (current-error-port)
                           "~A work queue, job raised exception ~A: ~A\n"
                           name job-args exn))
        (lambda ()
          (with-throw-handler #t
            (lambda ()
              (apply proc job-args))
            (lambda (key . args)
              (simple-format
               (current-error-port)
               "~A work queue, exception when handling job: ~A ~A\n"
               name key args)
              (let* ((stack (make-stack #t 3))
                     (backtrace
                      (call-with-output-string
                        (lambda (port)
                          (display-backtrace stack port)
                          (newline port)))))
                (display
                 backtrace
                 (current-error-port))))))
        #:unwind? #t))

    (define (start-thread thread-index)
      (define (too-many-threads?)
        (let ((running-jobs-count
               (hash-count (lambda (index val)
                             (list? val))
                           running-job-args))
              (desired-thread-count (get-thread-count)))

          (>= running-jobs-count
              desired-thread-count)))

      (define (thread-idle-for-too-long? last-job-finished-at)
        (time>=?
         (time-difference (current-time time-monotonic)
                          last-job-finished-at)
         thread-stop-delay))

      (define (stop-thread)
        (hash-remove! running-job-args
                      thread-index)
        (unlock-mutex queue-mutex))

      (call-with-new-thread
       (lambda ()
         (catch 'system-error
           (lambda ()
             (set-thread-name
              (string-append name " q t "
                             (number->string thread-index))))
           (const #t))

         (let loop ((last-job-finished-at (current-time time-monotonic)))
           (lock-mutex queue-mutex)

           (if (too-many-threads?)
               (stop-thread)
               (let ((job-args
                      (if (q-empty? queue)
                          ;; #f from wait-condition-variable indicates a timeout
                          (if (wait-condition-variable
                               job-available
                               queue-mutex)
                              ;; Another thread could have taken
                              ;; the job in the mean time
                              (if (q-empty? queue)
                                  #f
                                  (if priority<?
                                      (cdr (deq! queue))
                                      (deq! queue)))
                              #f)
                          (if priority<?
                              (cdr (deq! queue))
                              (deq! queue)))))

                 (if job-args
                     (begin
                       (hash-set! running-job-args
                                  thread-index
                                  job-args)

                       (unlock-mutex queue-mutex)
                       (thread-process-job job-args)

                       (with-mutex queue-mutex
                         (hash-set! running-job-args
                                    thread-index
                                    #f))

                       (loop (current-time time-monotonic)))
                     (if (thread-idle-for-too-long? last-job-finished-at)
                         (stop-thread)
                         (begin
                           (unlock-mutex queue-mutex)

                           (loop last-job-finished-at))))))))))


    (define start-new-threads-if-necessary
      (let ((previous-thread-started-at (make-time time-monotonic 0 0)))
        (lambda (desired-count)
          (let* ((thread-count
                  (hash-count (const #t) running-job-args))
                 (threads-to-start
                  (- desired-count thread-count)))
            (when (> threads-to-start 0)
              (for-each
               (lambda (thread-index)
                 (when (eq? (hash-ref running-job-args
                                      thread-index
                                      'slot-free)
                            'slot-free)
                   (let* ((now     (current-time time-monotonic))
                          (elapsed (time-difference now
                                                    previous-thread-started-at)))
                     (when (or (eq? #f thread-start-delay)
                               (time>=? elapsed thread-start-delay))
                       (set! previous-thread-started-at now)
                       (hash-set! running-job-args
                                  thread-index
                                  #f)
                       (start-thread thread-index)))))
               (iota desired-count)))))))

    (if (procedure? thread-count-parameter)
        (call-with-new-thread
         (lambda ()
           (catch 'system-error
             (lambda ()
               (set-thread-name
                (string-append name " q t")))
             (const #t))

           (while #t
             (sleep 15)
             (with-mutex queue-mutex
               (let ((idle-threads (hash-count (lambda (index val)
                                                 (eq? #f val))
                                               running-job-args)))
                 (when (= 0 idle-threads)
                   (start-new-threads-if-necessary (get-thread-count))))))))
        (start-new-threads-if-necessary (get-thread-count)))

    (values process-job count-jobs count-threads list-jobs)))

(define* (create-thread-pool thread-count-parameter get-job proc
                             #:key thread-start-delay
                             (thread-stop-delay
                              (make-time time-duration 0 0))
                             (name "unnamed"))
  (let ((pool-mutex (make-mutex))
        (job-available (make-condition-variable))
        (running-job-args (make-hash-table)))

    (define get-thread-count
      (cond
       ((number? thread-count-parameter)
        (const thread-count-parameter))
       (else
        (lambda ()
          (with-exception-handler
              (lambda (exn)
                (count-threads))
            thread-count-parameter
            #:unwind? #t)))))

    (define (get-job/safe running-jobs)
      (with-exception-handler
          (lambda (exn)
            #f)
        (lambda ()
          (get-job running-jobs))
        #:unwind? #t))

    (define (count-threads)
      (hash-count (const #t) running-job-args))

    (define (list-jobs)
      (append (hash-fold (lambda (key val result)
                           (or (and val
                                    (cons val result))
                               result))
                         '()
                         running-job-args)))

    (define (thread-process-job job-args)
      (with-exception-handler
          (lambda (exn)
            (with-exception-handler
                (lambda _
                  #f)
              (lambda ()
                ;; Logging may raise an exception, so try and just keep going.
                (display
                 (simple-format
                  #f
                  "~A thread pool, job raised exception ~A: ~A\n"
                  name job-args exn)
                 (current-error-port)))
              #:unwind? #t))
        (lambda ()
          (with-throw-handler #t
            (lambda ()
              (apply proc job-args))
            (lambda (key . args)
              (simple-format
               (current-error-port)
               "~A thread pool, exception when handling job: ~A ~A\n"
               name key args)
              (let* ((stack (make-stack #t 3))
                     (backtrace
                      (call-with-output-string
                        (lambda (port)
                          (display-backtrace stack port)
                          (newline port)))))
                (display
                 backtrace
                 (current-error-port))))))
        #:unwind? #t))

    (define (start-thread thread-index)
      (define (too-many-threads?)
        (let ((running-jobs-count
               (hash-count (lambda (index val)
                             (list? val))
                           running-job-args))
              (desired-thread-count (get-thread-count)))

          (>= running-jobs-count
              desired-thread-count)))

      (define (thread-idle-for-too-long? last-job-finished-at)
        (time>=?
         (time-difference (current-time time-monotonic)
                          last-job-finished-at)
         thread-stop-delay))

      (define (stop-thread)
        (hash-remove! running-job-args
                      thread-index)
        (unlock-mutex pool-mutex))

      (call-with-new-thread
       (lambda ()
         (catch 'system-error
           (lambda ()
             (set-thread-name
              (string-append name " p t "
                             (number->string thread-index))))
           (const #t))

         (let loop ((last-job-finished-at (current-time time-monotonic)))
           (lock-mutex pool-mutex)

           (hash-set! running-job-args
                      thread-index
                      #f)

           (let ((job-args
                  (or (get-job/safe (list-jobs))
                      ;; #f from wait-condition-variable indicates a timeout
                      (if (wait-condition-variable
                           job-available
                           pool-mutex)
                          (get-job/safe (list-jobs))
                          #f))))
             (if job-args
                 (begin
                   (hash-set! running-job-args
                              thread-index
                              job-args)

                   (unlock-mutex pool-mutex)
                   (thread-process-job job-args)

                   (loop (current-time time-monotonic)))
                 (if (or (thread-idle-for-too-long? last-job-finished-at)
                         (too-many-threads?))
                     (stop-thread)
                     (begin
                       (unlock-mutex pool-mutex)

                       (loop last-job-finished-at)))))))))

    (define start-new-threads-if-necessary
      (let ((previous-thread-started-at (make-time time-monotonic 0 0)))
        (lambda (desired-count)
            (let* ((thread-count
                    (hash-count (const #t) running-job-args))
                   (threads-to-start
                    (- desired-count thread-count)))
              (when (> threads-to-start 0)
                (for-each
                 (lambda (thread-index)
                   (when (eq? (hash-ref running-job-args
                                        thread-index
                                        'slot-free)
                              'slot-free)
                     (let* ((now     (current-time time-monotonic))
                            (elapsed (time-difference now
                                                      previous-thread-started-at)))
                       (when (or (eq? #f thread-start-delay)
                                 (time>=? elapsed thread-start-delay))
                         (set! previous-thread-started-at now)
                         (hash-set! running-job-args
                                    thread-index
                                    #f)
                         (start-thread thread-index)))))
                 (iota desired-count)))))))

    (if (procedure? thread-count-parameter)
        (call-with-new-thread
         (lambda ()
           (catch 'system-error
             (lambda ()
               (set-thread-name
                (string-append name " p t ")))
             (const #t))

           (lock-mutex pool-mutex)
           (while #t
             (with-exception-handler
                 (lambda _
                   ;; Things are going really wrong, we probably can't even
                   ;; log without risking another exception, so just sleep and
                   ;; try again.
                   (sleep 10))
               (lambda ()
                 (let ((idle-threads (hash-count (lambda (index val)
                                                   (eq? #f val))
                                                 running-job-args)))
                   (when (= 0 idle-threads)
                     (start-new-threads-if-necessary (get-thread-count))))

                 ;; Wake a worker thread to check if a job is available
                 (signal-condition-variable job-available)

                 (wait-condition-variable
                  job-available
                  pool-mutex))
               #:unwind? #t))))

        (start-new-threads-if-necessary (get-thread-count)))

    (values pool-mutex job-available count-threads list-jobs)))

(define (throttle min-duration thunk)
  (let ((next-min-runtime 0))
    (lambda ()
      (if (> (get-internal-real-time)
             next-min-runtime)
          (begin
            (set! next-min-runtime
                  (+ (get-internal-real-time)
                     (* min-duration internal-time-units-per-second)))
            (thunk))
          #f))))

(define* (get-load-average #:key (period 5))
  (if (file-exists? "/proc/loadavg")
      (let ((line (call-with-input-file "/proc/loadavg" get-line)))
        (match (string-split line #\space)
          ((1min 5min 15min _ _)
           (string->number
            (cond
             ((= period 1) 1min)
             ((= period 5) 5min)
             ((= period 15) 15min))))))
      #f))

(define (get-uptime)
  (if (file-exists? "/proc/uptime")
      (let ((line (call-with-input-file "/proc/uptime" get-line)))
        (match (string-split line #\space)
          ((uptime _)
           (string->number uptime))))
      #f))

(define (running-on-the-hurd?)
  (let ((cached-system #f))
    (unless cached-system
      (set! cached-system (utsname:sysname (uname))))
    (string=? cached-system "GNU")))

(define (get-guix-memory-metrics-updater registry)
  (define %memoization-tables
    (@@ (guix memoization) %memoization-tables))

  (define memoization-table-entry-count-metric
    (make-gauge-metric
     registry "guix_memoization_table_entry_count"
     #:labels '(procedure)))

  (define %derivation-cache
    (@@ (guix derivations) %derivation-cache))

  (define derivation-cache-entry-count-metric
    (make-gauge-metric
     registry "guix_derivation_cache_entry_count"))

  (lambda ()
    (metric-set
     derivation-cache-entry-count-metric
     ;; hash-count doesn't work for weak tables?
     (hash-fold (lambda (k v result)
                  (+ 1 result))
                0
                %derivation-cache))

    (hash-for-each
     (lambda (proc table)
       (metric-set
        memoization-table-entry-count-metric
        (hash-count (const #t) table)
        #:label-values `((procedure . ,(simple-format
                                        #f "~A"
                                        proc)))))
     %memoization-tables)))

;; Returns the port as well as the raw socket
(define* (open-socket-for-uri* uri
                               #:key (verify-certificate? #t)
                               (non-blocking? #t))
  (define tls-wrap
    (@@ (web client) tls-wrap))

  (define https?
    (eq? 'https (uri-scheme uri)))

  (define plain-uri
    (if https?
        (build-uri
         'http
         #:userinfo (uri-userinfo uri)
         #:host (uri-host uri)
         #:port (or (uri-port uri) 443)
         #:path (uri-path uri)
         #:query (uri-query uri)
         #:fragment (uri-fragment uri))
        uri))

  (let* ((s (open-socket-for-uri plain-uri))
         (port
          (if https?
              (tls-wrap s (uri-host uri)
                        #:verify-certificate? verify-certificate?)
              s)))
    (values port
            (if non-blocking?
                ;; Guile/guile-gnutls don't handle the handshake happening on
                ;; a non blocking socket, so change the behavior here.
                (non-blocking-port s)
                s))))

(define (check-locale!)
  (with-exception-handler
      (lambda (exn)
        (display
         (simple-format
          #f
          "exception when calling setlocale: ~A
falling back to en_US.utf8\n"
          exn)
         (current-error-port))

        (with-exception-handler
            (lambda (exn)
              (display
               (simple-format
                #f
                "exception when calling setlocale with en_US.utf8: ~A\n"
                exn)
               (current-error-port))

              (exit 1))
          (lambda _
            (setlocale LC_ALL "en_US.utf8"))
          #:unwind? #t))
    (lambda _
      (setlocale LC_ALL ""))
    #:unwind? #t))