progress.scm 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369
  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, 2020 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. (define (nearest-exact-integer x)
  85. "Given a real number X, return the nearest exact integer, with ties going to
  86. the nearest exact even integer."
  87. (inexact->exact (round x)))
  88. (define (duration->seconds duration)
  89. "Return the number of seconds represented by DURATION, a 'time-duration'
  90. object, as an inexact number."
  91. (+ (time-second duration)
  92. (/ (time-nanosecond duration) 1e9)))
  93. (define (seconds->string duration)
  94. "Given DURATION in seconds, return a string representing it in 'mm:ss' or
  95. 'hh:mm:ss' format, as needed."
  96. (if (not (number? duration))
  97. "00:00"
  98. (let* ((total-seconds (nearest-exact-integer duration))
  99. (extra-seconds (modulo total-seconds 3600))
  100. (num-hours (quotient total-seconds 3600))
  101. (hours (and (positive? num-hours) num-hours))
  102. (mins (quotient extra-seconds 60))
  103. (secs (modulo extra-seconds 60)))
  104. (format #f "~@[~2,'0d:~]~2,'0d:~2,'0d" hours mins secs))))
  105. (define (byte-count->string size)
  106. "Given SIZE in bytes, return a string representing it in a human-readable
  107. way."
  108. (let ((KiB 1024.)
  109. (MiB (expt 1024. 2))
  110. (GiB (expt 1024. 3))
  111. (TiB (expt 1024. 4)))
  112. (cond
  113. ((< size KiB) (format #f "~dB" (nearest-exact-integer size)))
  114. ((< size MiB) (format #f "~dKiB" (nearest-exact-integer (/ size KiB))))
  115. ((< size GiB) (format #f "~,1fMiB" (/ size MiB)))
  116. ((< size TiB) (format #f "~,2fGiB" (/ size GiB)))
  117. (else (format #f "~,3fTiB" (/ size TiB))))))
  118. (define (string-pad-middle left right len)
  119. "Combine LEFT and RIGHT with enough padding in the middle so that the
  120. resulting string has length at least LEN (it may overflow). If the string
  121. does not overflow, the last char in RIGHT will be flush with the LEN
  122. column."
  123. (let* ((total-used (+ (string-length left)
  124. (string-length right)))
  125. (num-spaces (max 1 (- len total-used)))
  126. (padding (make-string num-spaces #\space)))
  127. (string-append left padding right)))
  128. (define (rate-limited proc interval)
  129. "Return a procedure that will forward the invocation to PROC when the time
  130. elapsed since the previous forwarded invocation is greater or equal to
  131. INTERVAL (a time-duration object), otherwise does nothing and returns #f."
  132. (let ((previous-at #f))
  133. (lambda args
  134. (let* ((now (current-time time-monotonic))
  135. (forward-invocation (lambda ()
  136. (set! previous-at now)
  137. (apply proc args))))
  138. (if previous-at
  139. (let ((elapsed (time-difference now previous-at)))
  140. (if (time>=? elapsed interval)
  141. (forward-invocation)
  142. #f))
  143. (forward-invocation))))))
  144. (define current-terminal-columns
  145. ;; Number of columns of the terminal.
  146. (make-parameter 80))
  147. (define* (progress-bar % #:optional (bar-width 20))
  148. "Return % as a string representing an ASCII-art progress bar. The total
  149. width of the bar is BAR-WIDTH."
  150. (let* ((bar-width (max 3 (- bar-width 2)))
  151. (fraction (/ % 100))
  152. (filled (inexact->exact (floor (* fraction bar-width))))
  153. (empty (- bar-width filled)))
  154. (format #f "[~a~a]"
  155. (make-string filled #\#)
  156. (make-string empty #\space))))
  157. (define (erase-current-line port)
  158. "Write an ANSI erase-current-line sequence to PORT to erase the whole line and
  159. move the cursor to the beginning of the line."
  160. (display "\r\x1b[K" port))
  161. (define* (display-download-progress file size
  162. #:key
  163. start-time (transferred 0)
  164. (log-port (current-error-port)))
  165. "Write the progress report to LOG-PORT. Use START-TIME (a SRFI-19 time
  166. object) and TRANSFERRED (a total number of bytes) to determine the
  167. throughput."
  168. (define elapsed
  169. (duration->seconds
  170. (time-difference (current-time (time-type start-time))
  171. start-time)))
  172. (if (and (number? size) (not (zero? size)))
  173. (let* ((% (* 100.0 (/ transferred size)))
  174. (throughput (/ transferred elapsed))
  175. (left (format #f " ~a ~a" file
  176. (byte-count->string size)))
  177. (right (format #f "~a/s ~a ~a~6,1f%"
  178. (byte-count->string throughput)
  179. (seconds->string elapsed)
  180. (progress-bar %) %)))
  181. (erase-current-line log-port)
  182. (display (string-pad-middle left right
  183. (current-terminal-columns))
  184. log-port)
  185. (force-output log-port))
  186. ;; If we don't know the total size, the last transfer will have a 0B
  187. ;; size. Don't display it.
  188. (unless (zero? transferred)
  189. (let* ((throughput (/ transferred elapsed))
  190. (left (format #f " ~a" file))
  191. (right (format #f "~a/s ~a | ~a transferred"
  192. (byte-count->string throughput)
  193. (seconds->string elapsed)
  194. (byte-count->string transferred))))
  195. (erase-current-line log-port)
  196. (display (string-pad-middle left right
  197. (current-terminal-columns))
  198. log-port)
  199. (force-output log-port)))))
  200. (define %progress-interval
  201. ;; Default interval between subsequent outputs for rate-limited displays.
  202. (make-time time-duration 200000000 0))
  203. (define* (progress-reporter/file file size
  204. #:optional (log-port (current-output-port))
  205. #:key (abbreviation basename))
  206. "Return a <progress-reporter> object to show the progress of FILE's download,
  207. which is SIZE bytes long. The progress report is written to LOG-PORT, with
  208. ABBREVIATION used to shorten FILE for display."
  209. (let ((start-time (current-time time-monotonic))
  210. (transferred 0))
  211. (define (render)
  212. (display-download-progress (abbreviation file) size
  213. #:start-time start-time
  214. #:transferred transferred
  215. #:log-port log-port))
  216. (progress-reporter
  217. (start render)
  218. ;; Report the progress every 300ms or longer.
  219. (report
  220. (let ((rate-limited-render (rate-limited render %progress-interval)))
  221. (lambda (value)
  222. (set! transferred value)
  223. (rate-limited-render))))
  224. ;; Don't miss the last report.
  225. (stop render))))
  226. (define* (progress-reporter/bar total
  227. #:optional
  228. (prefix "")
  229. (port (current-error-port)))
  230. "Return a reporter that shows a progress bar every time one of the TOTAL
  231. tasks is performed. Write PREFIX at the beginning of the line."
  232. (define done 0)
  233. (define (report-progress)
  234. (set! done (+ 1 done))
  235. (unless (> done total)
  236. (let* ((ratio (* 100. (/ done total))))
  237. (erase-current-line port)
  238. (if (string-null? prefix)
  239. (display (progress-bar ratio (current-terminal-columns)) port)
  240. (let ((width (- (current-terminal-columns)
  241. (string-length prefix) 3)))
  242. (display prefix port)
  243. (display " " port)
  244. (display (progress-bar ratio width) port)))
  245. (force-output port))))
  246. (progress-reporter
  247. (start (lambda ()
  248. (set! done 0)))
  249. (report report-progress)
  250. (stop (lambda ()
  251. (erase-current-line port)
  252. (unless (string-null? prefix)
  253. (display prefix port)
  254. (newline port))
  255. (force-output port)))))
  256. (define* (progress-reporter/trace file url size
  257. #:optional (log-port (current-output-port)))
  258. "Like 'progress-reporter/file', but instead of returning human-readable
  259. progress reports, write \"build trace\" lines to be processed elsewhere."
  260. (define total 0) ;bytes transferred
  261. (define (report-progress transferred)
  262. (define message
  263. (format #f "@ download-progress ~a ~a ~a ~a~%"
  264. file url (or size "-") transferred))
  265. (display message log-port) ;should be atomic
  266. (flush-output-port log-port))
  267. (progress-reporter
  268. (start (lambda ()
  269. (set! total 0)
  270. (display (format #f "@ download-started ~a ~a ~a~%"
  271. file url (or size "-"))
  272. log-port)))
  273. (report (let ((report (rate-limited report-progress %progress-interval)))
  274. (lambda (transferred)
  275. (set! total transferred)
  276. (report transferred))))
  277. (stop (lambda ()
  278. (let ((size (or size total)))
  279. (report-progress size)
  280. (display (format #f "@ download-succeeded ~a ~a ~a~%"
  281. file url size)
  282. log-port))))))
  283. ;; TODO: replace '(@ (guix build utils) dump-port))'.
  284. (define* (dump-port* in out
  285. #:key (buffer-size 16384)
  286. (reporter progress-reporter/silent))
  287. "Read as much data as possible from IN and write it to OUT, using chunks of
  288. BUFFER-SIZE bytes. After each successful transfer of BUFFER-SIZE bytes or
  289. less, report the total number of bytes transferred to the REPORTER, which
  290. should be a <progress-reporter> object."
  291. (define buffer
  292. (make-bytevector buffer-size))
  293. (call-with-progress-reporter reporter
  294. (lambda (report)
  295. (let loop ((total 0)
  296. (bytes (get-bytevector-n! in buffer 0 buffer-size)))
  297. (or (eof-object? bytes)
  298. (let ((total (+ total bytes)))
  299. (put-bytevector out buffer 0 bytes)
  300. (report total)
  301. (loop total (get-bytevector-n! in buffer 0 buffer-size))))))))
  302. (define (progress-report-port reporter port)
  303. "Return a port that continuously reports the bytes read from PORT using
  304. REPORTER, which should be a <progress-reporter> object."
  305. (match reporter
  306. (($ <progress-reporter> start report stop)
  307. (let* ((total 0)
  308. (read! (lambda (bv start count)
  309. (let ((n (match (get-bytevector-n! port bv start count)
  310. ((? eof-object?) 0)
  311. (x x))))
  312. (set! total (+ total n))
  313. (report total)
  314. n))))
  315. (start)
  316. (make-custom-binary-input-port "progress-port-proc"
  317. read! #f #f
  318. (lambda ()
  319. ;; XXX: Kludge! When used through
  320. ;; 'decompressed-port', this port ends
  321. ;; up being closed twice: once in a
  322. ;; child process early on, and at the
  323. ;; end in the parent process. Ignore
  324. ;; the early close so we don't output
  325. ;; a spurious "download-succeeded"
  326. ;; trace.
  327. (unless (zero? total)
  328. (stop))
  329. (close-port port)))))))