progress.scm 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428
  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-record-type* <progress-bar-style>
  148. progress-bar-style make-progress-bar-style progress-bar-style?
  149. (start progress-bar-style-start)
  150. (stop progress-bar-style-stop)
  151. (filled progress-bar-style-filled)
  152. (steps progress-bar-style-steps))
  153. (define ascii-bar-style
  154. (progress-bar-style
  155. (start #\[)
  156. (stop #\])
  157. (filled #\#)
  158. (steps '())))
  159. (define unicode-bar-style
  160. (progress-bar-style
  161. (start #\x2595)
  162. (stop #\x258f)
  163. (filled #\x2588)
  164. (steps '(#\x258F #\x258E #\x258D #\x258C #\x258B #\x258A #\x2589))))
  165. (define* (progress-bar % #:optional (bar-width 20))
  166. "Return % as a string representing an ASCII-art progress bar. The total
  167. width of the bar is BAR-WIDTH."
  168. (let* ((bar-style (if (equal? (port-encoding (current-output-port)) "UTF-8")
  169. unicode-bar-style
  170. ascii-bar-style))
  171. (bar-width (max 3 (- bar-width 2)))
  172. (intermediates (+ (length (progress-bar-style-steps bar-style)) 1))
  173. (step (inexact->exact (floor (/ (* % bar-width intermediates) 100))))
  174. (filled (quotient step intermediates))
  175. (intermediate
  176. (list-ref (cons #f (progress-bar-style-steps bar-style))
  177. (modulo step intermediates)))
  178. (empty (- bar-width filled (if intermediate 1 0))))
  179. (simple-format #f "~a~a~a~a~a"
  180. (string (progress-bar-style-start bar-style))
  181. (make-string filled (progress-bar-style-filled bar-style))
  182. (if intermediate (string intermediate) "")
  183. (make-string empty #\space)
  184. (string (progress-bar-style-stop bar-style)))))
  185. (define (erase-current-line port)
  186. "Write an ANSI erase-current-line sequence to PORT to erase the whole line and
  187. move the cursor to the beginning of the line."
  188. (display "\r\x1b[K" port))
  189. (define* (display-download-progress file size
  190. #:key
  191. (tty? #t)
  192. start-time (transferred 0)
  193. (log-port (current-error-port)))
  194. "Write the progress report to LOG-PORT. Use START-TIME (a SRFI-19 time
  195. object) and TRANSFERRED (a total number of bytes) to determine the
  196. throughput. When TTY? is false, assume LOG-PORT is not a tty and do not emit
  197. ANSI escape codes."
  198. (define elapsed
  199. (duration->seconds
  200. (time-difference (current-time (time-type start-time))
  201. start-time)))
  202. (cond ((and (not tty?)
  203. size (not (zero? size))
  204. transferred)
  205. ;; Display a dot for at most every 10%.
  206. (when (zero? (modulo (round (* 100. (/ transferred size))) 10))
  207. (display "." log-port)
  208. (force-output log-port)))
  209. ((and (number? size) (not (zero? size)))
  210. (let* ((% (* 100.0 (/ transferred size)))
  211. (throughput (/ transferred elapsed))
  212. (left (format #f " ~a ~a" file
  213. (byte-count->string size)))
  214. (right (format #f "~a/s ~a ~a~6,1f%"
  215. (byte-count->string throughput)
  216. (seconds->string elapsed)
  217. (progress-bar %) %)))
  218. (erase-current-line log-port)
  219. (display (string-pad-middle left right
  220. (current-terminal-columns))
  221. log-port)
  222. (force-output log-port)))
  223. (else
  224. ;; If we don't know the total size, the last transfer will have a 0B
  225. ;; size. Don't display it.
  226. (unless (zero? transferred)
  227. (let* ((throughput (/ transferred elapsed))
  228. (left (format #f " ~a" file))
  229. (right (format #f "~a/s ~a | ~a transferred"
  230. (byte-count->string throughput)
  231. (seconds->string elapsed)
  232. (byte-count->string transferred))))
  233. (erase-current-line log-port)
  234. (display (string-pad-middle left right
  235. (current-terminal-columns))
  236. log-port)
  237. (force-output log-port))))))
  238. (define %progress-interval
  239. ;; Default interval between subsequent outputs for rate-limited displays.
  240. (make-time time-duration 200000000 0))
  241. (define* (progress-reporter/file file size
  242. #:optional (log-port (current-output-port))
  243. #:key (abbreviation basename))
  244. "Return a <progress-reporter> object to show the progress of FILE's download,
  245. which is SIZE bytes long. The progress report is written to LOG-PORT, with
  246. ABBREVIATION used to shorten FILE for display."
  247. (let ((start-time (current-time time-monotonic))
  248. (transferred 0))
  249. (define (render)
  250. (display-download-progress (abbreviation file) size
  251. #:start-time start-time
  252. #:transferred transferred
  253. #:log-port log-port))
  254. (progress-reporter
  255. (start render)
  256. ;; Report the progress every 300ms or longer.
  257. (report
  258. (let ((rate-limited-render (rate-limited render %progress-interval)))
  259. (lambda (value)
  260. (set! transferred value)
  261. (rate-limited-render))))
  262. ;; Don't miss the last report.
  263. (stop render))))
  264. (define* (progress-reporter/bar total
  265. #:optional
  266. (prefix "")
  267. (port (current-error-port)))
  268. "Return a reporter that shows a progress bar every time one of the TOTAL
  269. tasks is performed. Write PREFIX at the beginning of the line."
  270. (define done 0)
  271. (define (draw-bar)
  272. (let* ((ratio (* 100. (/ done total))))
  273. (erase-current-line port)
  274. (if (string-null? prefix)
  275. (display (progress-bar ratio (current-terminal-columns)) port)
  276. (let ((width (- (current-terminal-columns)
  277. (string-length prefix) 3)))
  278. (display prefix port)
  279. (display " " port)
  280. (display (progress-bar ratio width) port)))
  281. (force-output port)))
  282. (define draw-bar/rate-limited
  283. (rate-limited draw-bar %progress-interval))
  284. (define (report-progress)
  285. (set! done (+ 1 done))
  286. (unless (> done total)
  287. (draw-bar/rate-limited)))
  288. (progress-reporter
  289. (start (lambda ()
  290. (set! done 0)))
  291. (report report-progress)
  292. (stop (lambda ()
  293. (erase-current-line port)
  294. (unless (string-null? prefix)
  295. (display prefix port)
  296. (newline port))
  297. (force-output port)))))
  298. (define* (progress-reporter/trace file url size
  299. #:optional (log-port (current-output-port)))
  300. "Like 'progress-reporter/file', but instead of returning human-readable
  301. progress reports, write \"build trace\" lines to be processed elsewhere."
  302. (define total 0) ;bytes transferred
  303. (define (report-progress transferred)
  304. (define message
  305. (format #f "@ download-progress ~a ~a ~a ~a~%"
  306. file url (or size "-") transferred))
  307. (display message log-port) ;should be atomic
  308. (flush-output-port log-port))
  309. (progress-reporter
  310. (start (lambda ()
  311. (set! total 0)
  312. (display (format #f "@ download-started ~a ~a ~a~%"
  313. file url (or size "-"))
  314. log-port)))
  315. (report (let ((report (rate-limited report-progress %progress-interval)))
  316. (lambda (transferred)
  317. (set! total transferred)
  318. (report transferred))))
  319. (stop (lambda ()
  320. (let ((size (or size total)))
  321. (report-progress size)
  322. (display (format #f "@ download-succeeded ~a ~a ~a~%"
  323. file url size)
  324. log-port))))))
  325. ;; TODO: replace '(@ (guix build utils) dump-port))'.
  326. (define* (dump-port* in out
  327. #:key (buffer-size 16384)
  328. (reporter progress-reporter/silent))
  329. "Read as much data as possible from IN and write it to OUT, using chunks of
  330. BUFFER-SIZE bytes. After each successful transfer of BUFFER-SIZE bytes or
  331. less, report the total number of bytes transferred to the REPORTER, which
  332. should be a <progress-reporter> object."
  333. (define buffer
  334. (make-bytevector buffer-size))
  335. (call-with-progress-reporter reporter
  336. (lambda (report)
  337. (let loop ((total 0)
  338. (bytes (get-bytevector-n! in buffer 0 buffer-size)))
  339. (or (eof-object? bytes)
  340. (let ((total (+ total bytes)))
  341. (put-bytevector out buffer 0 bytes)
  342. (report total)
  343. (loop total (get-bytevector-n! in buffer 0 buffer-size))))))))
  344. (define* (progress-report-port reporter port
  345. #:key
  346. (close? #t)
  347. download-size)
  348. "Return a port that continuously reports the bytes read from PORT using
  349. REPORTER, which should be a <progress-reporter> object. When CLOSE? is true,
  350. PORT is closed when the returned port is closed.
  351. When DOWNLOAD-SIZE is passed, do not read more than DOWNLOAD-SIZE bytes from
  352. PORT. This is important to avoid blocking when the remote side won't close
  353. the underlying connection."
  354. (match reporter
  355. (($ <progress-reporter> start report stop)
  356. (let* ((total 0)
  357. (read! (lambda (bv start count)
  358. (let* ((count (if download-size
  359. (min count (- download-size total))
  360. count))
  361. (n (match (get-bytevector-n! port bv start count)
  362. ((? eof-object?) 0)
  363. (x x))))
  364. (set! total (+ total n))
  365. (report total)
  366. n))))
  367. (start)
  368. (make-custom-binary-input-port "progress-port-proc"
  369. read! #f #f
  370. (lambda ()
  371. ;; XXX: Kludge! When used through
  372. ;; 'decompressed-port', this port ends
  373. ;; up being closed twice: once in a
  374. ;; child process early on, and at the
  375. ;; end in the parent process. Ignore
  376. ;; the early close so we don't output
  377. ;; a spurious "download-succeeded"
  378. ;; trace.
  379. (unless (zero? total)
  380. (stop))
  381. (when close?
  382. (close-port port))))))))