Greg Kroah-Hartman | b244131 | 2017-11-01 15:07:57 +0100 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0 |
Jiri Olsa | a82bfd0 | 2017-09-08 14:05:08 +0200 | [diff] [blame] | 2 | #include <linux/kernel.h> |
Namhyung Kim | 688f2f5 | 2012-11-13 22:30:32 +0900 | [diff] [blame] | 3 | #include "../cache.h" |
| 4 | #include "progress.h" |
| 5 | |
Arnaldo Carvalho de Melo | 4d3001f | 2013-10-23 15:40:38 -0300 | [diff] [blame] | 6 | static void null_progress__update(struct ui_progress *p __maybe_unused) |
Namhyung Kim | 688f2f5 | 2012-11-13 22:30:32 +0900 | [diff] [blame] | 7 | { |
| 8 | } |
| 9 | |
Arnaldo Carvalho de Melo | 4779a2e | 2013-10-23 14:08:48 -0300 | [diff] [blame] | 10 | static struct ui_progress_ops null_progress__ops = |
Namhyung Kim | 688f2f5 | 2012-11-13 22:30:32 +0900 | [diff] [blame] | 11 | { |
Arnaldo Carvalho de Melo | 4779a2e | 2013-10-23 14:08:48 -0300 | [diff] [blame] | 12 | .update = null_progress__update, |
Namhyung Kim | 688f2f5 | 2012-11-13 22:30:32 +0900 | [diff] [blame] | 13 | }; |
| 14 | |
Arnaldo Carvalho de Melo | 4779a2e | 2013-10-23 14:08:48 -0300 | [diff] [blame] | 15 | struct ui_progress_ops *ui_progress__ops = &null_progress__ops; |
Namhyung Kim | 688f2f5 | 2012-11-13 22:30:32 +0900 | [diff] [blame] | 16 | |
Arnaldo Carvalho de Melo | 4d3001f | 2013-10-23 15:40:38 -0300 | [diff] [blame] | 17 | void ui_progress__update(struct ui_progress *p, u64 adv) |
Namhyung Kim | 688f2f5 | 2012-11-13 22:30:32 +0900 | [diff] [blame] | 18 | { |
Jiri Olsa | a82bfd0 | 2017-09-08 14:05:08 +0200 | [diff] [blame] | 19 | u64 last = p->curr; |
| 20 | |
Arnaldo Carvalho de Melo | 4d3001f | 2013-10-23 15:40:38 -0300 | [diff] [blame] | 21 | p->curr += adv; |
| 22 | |
| 23 | if (p->curr >= p->next) { |
Jiri Olsa | a82bfd0 | 2017-09-08 14:05:08 +0200 | [diff] [blame] | 24 | u64 nr = DIV_ROUND_UP(p->curr - last, p->step); |
| 25 | |
| 26 | p->next += nr * p->step; |
Arnaldo Carvalho de Melo | 4d3001f | 2013-10-23 15:40:38 -0300 | [diff] [blame] | 27 | ui_progress__ops->update(p); |
| 28 | } |
| 29 | } |
| 30 | |
| 31 | void ui_progress__init(struct ui_progress *p, u64 total, const char *title) |
| 32 | { |
| 33 | p->curr = 0; |
Jiri Olsa | 4d286c8 | 2017-09-08 14:05:07 +0200 | [diff] [blame] | 34 | p->next = p->step = total / 16 ?: 1; |
Arnaldo Carvalho de Melo | 4d3001f | 2013-10-23 15:40:38 -0300 | [diff] [blame] | 35 | p->total = total; |
| 36 | p->title = title; |
| 37 | |
Namhyung Kim | 688f2f5 | 2012-11-13 22:30:32 +0900 | [diff] [blame] | 38 | } |
Namhyung Kim | a5580f3 | 2012-11-13 22:30:34 +0900 | [diff] [blame] | 39 | |
| 40 | void ui_progress__finish(void) |
| 41 | { |
Arnaldo Carvalho de Melo | 4779a2e | 2013-10-23 14:08:48 -0300 | [diff] [blame] | 42 | if (ui_progress__ops->finish) |
| 43 | ui_progress__ops->finish(); |
Namhyung Kim | a5580f3 | 2012-11-13 22:30:34 +0900 | [diff] [blame] | 44 | } |