progress.scm 16 KB

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