progress.scm 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376
  1. ;;; GNU Guix --- Functional package management for GNU
  2. ;;; Copyright © 2017 Sou Bunnbu <iyzsong@gmail.com>
  3. ;;; Copyright © 2015 Steve Sprang <scs@stevesprang.com>
  4. ;;; Copyright © 2017, 2018, 2019 Ludovic Courtès <ludo@gnu.org>
  5. ;;; Copyright © 2018 Clément Lassieur <clement@lassieur.org>
  6. ;;;
  7. ;;; This file is part of GNU Guix.
  8. ;;;
  9. ;;; GNU Guix is free software; you can redistribute it and/or modify it
  10. ;;; under the terms of the GNU General Public License as published by
  11. ;;; the Free Software Foundation; either version 3 of the License, or (at
  12. ;;; your option) any later version.
  13. ;;;
  14. ;;; GNU Guix is distributed in the hope that it will be useful, but
  15. ;;; WITHOUT ANY WARRANTY; without even the implied warranty of
  16. ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. ;;; GNU General Public License for more details.
  18. ;;;
  19. ;;; You should have received a copy of the GNU General Public License
  20. ;;; along with GNU Guix. If not, see <http://www.gnu.org/licenses/>.
  21. (define-module (guix progress)
  22. #:use-module (guix records)
  23. #:use-module (srfi srfi-19)
  24. #:use-module (rnrs io ports)
  25. #:use-module (rnrs bytevectors)
  26. #:use-module (ice-9 format)
  27. #:use-module (ice-9 match)
  28. #:export (<progress-reporter>
  29. progress-reporter
  30. make-progress-reporter
  31. progress-reporter?
  32. call-with-progress-reporter
  33. start-progress-reporter!
  34. stop-progress-reporter!
  35. progress-reporter-report!
  36. progress-reporter/silent
  37. progress-reporter/file
  38. progress-reporter/bar
  39. progress-reporter/trace
  40. progress-report-port
  41. display-download-progress
  42. erase-current-line
  43. progress-bar
  44. byte-count->string
  45. current-terminal-columns
  46. dump-port*))
  47. ;;; Commentary:
  48. ;;;
  49. ;;; Helper to write progress report code for downloads, etc.
  50. ;;;
  51. ;;; Code:
  52. (define-record-type* <progress-reporter>
  53. progress-reporter make-progress-reporter progress-reporter?
  54. (start progress-reporter-start) ; thunk
  55. (report progress-reporter-report) ; procedure
  56. (stop progress-reporter-stop)) ; thunk
  57. (define (call-with-progress-reporter reporter proc)
  58. "Start REPORTER for progress reporting, and call @code{(@var{proc} report)}
  59. with the resulting report procedure. When @var{proc} returns, the REPORTER is
  60. stopped."
  61. (match reporter
  62. (($ <progress-reporter> start report stop)
  63. (dynamic-wind start (lambda () (proc report)) stop))))
  64. (define (start-progress-reporter! reporter)
  65. "Low-level procedure to start REPORTER."
  66. (match reporter
  67. (($ <progress-reporter> start report stop)
  68. (start))))
  69. (define (progress-reporter-report! reporter . args)
  70. "Low-level procedure to lead REPORTER to emit a report."
  71. (match reporter
  72. (($ <progress-reporter> start report stop)
  73. (apply report args))))
  74. (define (stop-progress-reporter! reporter)
  75. "Low-level procedure to stop REPORTER."
  76. (match reporter
  77. (($ <progress-reporter> start report stop)
  78. (stop))))
  79. (define progress-reporter/silent
  80. (make-progress-reporter noop noop noop))
  81. ;;;
  82. ;;; File download progress report.
  83. ;;;
  84. (cond-expand
  85. (guile-2.2
  86. ;; Guile 2.2.2 has a bug whereby 'time-monotonic' objects have seconds and
  87. ;; nanoseconds swapped (fixed in Guile commit 886ac3e). Work around it.
  88. (define time-monotonic time-tai))
  89. (else #t))
  90. (define (nearest-exact-integer x)
  91. "Given a real number X, return the nearest exact integer, with ties going to
  92. the nearest exact even integer."
  93. (inexact->exact (round x)))
  94. (define (duration->seconds duration)
  95. "Return the number of seconds represented by DURATION, a 'time-duration'
  96. object, as an inexact number."
  97. (+ (time-second duration)
  98. (/ (time-nanosecond duration) 1e9)))
  99. (define (seconds->string duration)
  100. "Given DURATION in seconds, return a string representing it in 'mm:ss' or
  101. 'hh:mm:ss' format, as needed."
  102. (if (not (number? duration))
  103. "00:00"
  104. (let* ((total-seconds (nearest-exact-integer duration))
  105. (extra-seconds (modulo total-seconds 3600))
  106. (num-hours (quotient total-seconds 3600))
  107. (hours (and (positive? num-hours) num-hours))
  108. (mins (quotient extra-seconds 60))
  109. (secs (modulo extra-seconds 60)))
  110. (format #f "~@[~2,'0d:~]~2,'0d:~2,'0d" hours mins secs))))
  111. (define (byte-count->string size)
  112. "Given SIZE in bytes, return a string representing it in a human-readable
  113. way."
  114. (let ((KiB 1024.)
  115. (MiB (expt 1024. 2))
  116. (GiB (expt 1024. 3))
  117. (TiB (expt 1024. 4)))
  118. (cond
  119. ((< size KiB) (format #f "~dB" (nearest-exact-integer size)))
  120. ((< size MiB) (format #f "~dKiB" (nearest-exact-integer (/ size KiB))))
  121. ((< size GiB) (format #f "~,1fMiB" (/ size MiB)))
  122. ((< size TiB) (format #f "~,2fGiB" (/ size GiB)))
  123. (else (format #f "~,3fTiB" (/ size TiB))))))
  124. (define (string-pad-middle left right len)
  125. "Combine LEFT and RIGHT with enough padding in the middle so that the
  126. resulting string has length at least LEN (it may overflow). If the string
  127. does not overflow, the last char in RIGHT will be flush with the LEN
  128. column."
  129. (let* ((total-used (+ (string-length left)
  130. (string-length right)))
  131. (num-spaces (max 1 (- len total-used)))
  132. (padding (make-string num-spaces #\space)))
  133. (string-append left padding right)))
  134. (define (rate-limited proc interval)
  135. "Return a procedure that will forward the invocation to PROC when the time
  136. elapsed since the previous forwarded invocation is greater or equal to
  137. INTERVAL (a time-duration object), otherwise does nothing and returns #f."
  138. (let ((previous-at #f))
  139. (lambda args
  140. (let* ((now (current-time time-monotonic))
  141. (forward-invocation (lambda ()
  142. (set! previous-at now)
  143. (apply proc args))))
  144. (if previous-at
  145. (let ((elapsed (time-difference now previous-at)))
  146. (if (time>=? elapsed interval)
  147. (forward-invocation)
  148. #f))
  149. (forward-invocation))))))
  150. (define current-terminal-columns
  151. ;; Number of columns of the terminal.
  152. (make-parameter 80))
  153. (define* (progress-bar % #:optional (bar-width 20))
  154. "Return % as a string representing an ASCII-art progress bar. The total
  155. width of the bar is BAR-WIDTH."
  156. (let* ((bar-width (max 3 (- bar-width 2)))
  157. (fraction (/ % 100))
  158. (filled (inexact->exact (floor (* fraction bar-width))))
  159. (empty (- bar-width filled)))
  160. (format #f "[~a~a]"
  161. (make-string filled #\#)
  162. (make-string empty #\space))))
  163. (define (erase-current-line port)
  164. "Write an ANSI erase-current-line sequence to PORT to erase the whole line and
  165. move the cursor to the beginning of the line."
  166. (display "\r\x1b[K" port))
  167. (define* (display-download-progress file size
  168. #:key
  169. start-time (transferred 0)
  170. (log-port (current-error-port)))
  171. "Write the progress report to LOG-PORT. Use START-TIME (a SRFI-19 time
  172. object) and TRANSFERRED (a total number of bytes) to determine the
  173. throughput."
  174. (define elapsed
  175. (duration->seconds
  176. (time-difference (current-time (time-type start-time))
  177. start-time)))
  178. (if (and (number? size) (not (zero? size)))
  179. (let* ((% (* 100.0 (/ transferred size)))
  180. (throughput (/ transferred elapsed))
  181. (left (format #f " ~a ~a" file
  182. (byte-count->string size)))
  183. (right (format #f "~a/s ~a ~a~6,1f%"
  184. (byte-count->string throughput)
  185. (seconds->string elapsed)
  186. (progress-bar %) %)))
  187. (erase-current-line log-port)
  188. (display (string-pad-middle left right
  189. (current-terminal-columns))
  190. log-port)
  191. (force-output log-port))
  192. ;; If we don't know the total size, the last transfer will have a 0B
  193. ;; size. Don't display it.
  194. (unless (zero? transferred)
  195. (let* ((throughput (/ transferred elapsed))
  196. (left (format #f " ~a" file))
  197. (right (format #f "~a/s ~a | ~a transferred"
  198. (byte-count->string throughput)
  199. (seconds->string elapsed)
  200. (byte-count->string transferred))))
  201. (erase-current-line log-port)
  202. (display (string-pad-middle left right
  203. (current-terminal-columns))
  204. log-port)
  205. (force-output log-port)))))
  206. (define %progress-interval
  207. ;; Default interval between subsequent outputs for rate-limited displays.
  208. (make-time time-duration 200000000 0))
  209. (define* (progress-reporter/file file size
  210. #:optional (log-port (current-output-port))
  211. #:key (abbreviation basename))
  212. "Return a <progress-reporter> object to show the progress of FILE's download,
  213. which is SIZE bytes long. The progress report is written to LOG-PORT, with
  214. ABBREVIATION used to shorten FILE for display."
  215. (let ((start-time (current-time time-monotonic))
  216. (transferred 0))
  217. (define (render)
  218. (display-download-progress (abbreviation file) size
  219. #:start-time start-time
  220. #:transferred transferred
  221. #:log-port log-port))
  222. (progress-reporter
  223. (start render)
  224. ;; Report the progress every 300ms or longer.
  225. (report
  226. (let ((rate-limited-render (rate-limited render %progress-interval)))
  227. (lambda (value)
  228. (set! transferred value)
  229. (rate-limited-render))))
  230. ;; Don't miss the last report.
  231. (stop render))))
  232. (define* (progress-reporter/bar total
  233. #:optional
  234. (prefix "")
  235. (port (current-error-port)))
  236. "Return a reporter that shows a progress bar every time one of the TOTAL
  237. tasks is performed. Write PREFIX at the beginning of the line."
  238. (define done 0)
  239. (define (report-progress)
  240. (set! done (+ 1 done))
  241. (unless (> done total)
  242. (let* ((ratio (* 100. (/ done total))))
  243. (erase-current-line port)
  244. (if (string-null? prefix)
  245. (display (progress-bar ratio (current-terminal-columns)) port)
  246. (let ((width (- (current-terminal-columns)
  247. (string-length prefix) 3)))
  248. (display prefix port)
  249. (display " " port)
  250. (display (progress-bar ratio width) port)))
  251. (force-output port))))
  252. (progress-reporter
  253. (start (lambda ()
  254. (set! done 0)))
  255. (report report-progress)
  256. (stop (lambda ()
  257. (erase-current-line port)
  258. (unless (string-null? prefix)
  259. (display prefix port)
  260. (newline port))
  261. (force-output port)))))
  262. (define* (progress-reporter/trace file url size
  263. #:optional (log-port (current-output-port)))
  264. "Like 'progress-reporter/file', but instead of returning human-readable
  265. progress reports, write \"build trace\" lines to be processed elsewhere."
  266. (define total 0) ;bytes transferred
  267. (define (report-progress transferred)
  268. (define message
  269. (format #f "@ download-progress ~a ~a ~a ~a~%"
  270. file url (or size "-") transferred))
  271. (display message log-port) ;should be atomic
  272. (flush-output-port log-port))
  273. (progress-reporter
  274. (start (lambda ()
  275. (set! total 0)
  276. (display (format #f "@ download-started ~a ~a ~a~%"
  277. file url (or size "-"))
  278. log-port)))
  279. (report (let ((report (rate-limited report-progress %progress-interval)))
  280. (lambda (transferred)
  281. (set! total transferred)
  282. (report transferred))))
  283. (stop (lambda ()
  284. (let ((size (or size total)))
  285. (report-progress size)
  286. (display (format #f "@ download-succeeded ~a ~a ~a~%"
  287. file url size)
  288. log-port))))))
  289. ;; TODO: replace '(@ (guix build utils) dump-port))'.
  290. (define* (dump-port* in out
  291. #:key (buffer-size 16384)
  292. (reporter progress-reporter/silent))
  293. "Read as much data as possible from IN and write it to OUT, using chunks of
  294. BUFFER-SIZE bytes. After each successful transfer of BUFFER-SIZE bytes or
  295. less, report the total number of bytes transferred to the REPORTER, which
  296. should be a <progress-reporter> object."
  297. (define buffer
  298. (make-bytevector buffer-size))
  299. (call-with-progress-reporter reporter
  300. (lambda (report)
  301. (let loop ((total 0)
  302. (bytes (get-bytevector-n! in buffer 0 buffer-size)))
  303. (or (eof-object? bytes)
  304. (let ((total (+ total bytes)))
  305. (put-bytevector out buffer 0 bytes)
  306. (report total)
  307. (loop total (get-bytevector-n! in buffer 0 buffer-size))))))))
  308. (define (progress-report-port reporter port)
  309. "Return a port that continuously reports the bytes read from PORT using
  310. REPORTER, which should be a <progress-reporter> object."
  311. (match reporter
  312. (($ <progress-reporter> start report stop)
  313. (let* ((total 0)
  314. (read! (lambda (bv start count)
  315. (let ((n (match (get-bytevector-n! port bv start count)
  316. ((? eof-object?) 0)
  317. (x x))))
  318. (set! total (+ total n))
  319. (report total)
  320. n))))
  321. (start)
  322. (make-custom-binary-input-port "progress-port-proc"
  323. read! #f #f
  324. (lambda ()
  325. ;; XXX: Kludge! When used through
  326. ;; 'decompressed-port', this port ends
  327. ;; up being closed twice: once in a
  328. ;; child process early on, and at the
  329. ;; end in the parent process. Ignore
  330. ;; the early close so we don't output
  331. ;; a spurious "download-succeeded"
  332. ;; trace.
  333. (unless (zero? total)
  334. (stop))
  335. (close-port port)))))))