Commit a683c288d46336410ddfa2f5c70046dd1d4a5d1e
Committed by
Tom Rini
1 parent
095728803e
Exists in
master
and in
54 other branches
spi: Add progress percentage and write speed to `sf update`
Output a progress update only at most 10 times per second, to avoid saturating (and waiting on) the console. Make the summary line to fit on a single line. Make sure that cursor sits at the end of each update line instead of the beginning. Sample output: SF: Detected W25Q32 with page size 4 KiB, total 4 MiB Update SPI 1331200 bytes written, 2863104 bytes skipped in 21.912s, speed 199728 B/s time: 21.919 seconds, 21919 ticks Skipping verify Signed-off-by: Simon Glass <sjg@chromium.org> Signed-off-by: James Miller <jamesmiller@chromium.org> Signed-off-by: Taylor Hutt <thutt@chromium.org> [trini: Drop 'const' from bytes_per_second()] Signed-off-by: Tom Rini <trini@ti.com>
Showing 1 changed file with 39 additions and 2 deletions Side-by-side Diff
common/cmd_sf.c
... | ... | @@ -67,6 +67,23 @@ |
67 | 67 | return 1; |
68 | 68 | } |
69 | 69 | |
70 | +/** | |
71 | + * This function takes a byte length and a delta unit of time to compute the | |
72 | + * approximate bytes per second | |
73 | + * | |
74 | + * @param len amount of bytes currently processed | |
75 | + * @param start_ms start time of processing in ms | |
76 | + * @return bytes per second if OK, 0 on error | |
77 | + */ | |
78 | +static ulong bytes_per_second(unsigned int len, ulong start_ms) | |
79 | +{ | |
80 | + /* less accurate but avoids overflow */ | |
81 | + if (len >= ((unsigned int) -1) / 1024) | |
82 | + return len / (max(get_timer(start_ms) / 1024, 1)); | |
83 | + else | |
84 | + return 1024 * len / max(get_timer(start_ms), 1); | |
85 | +} | |
86 | + | |
70 | 87 | static int do_spi_flash_probe(int argc, char * const argv[]) |
71 | 88 | { |
72 | 89 | unsigned int bus = CONFIG_SF_DEFAULT_BUS; |
73 | 90 | |
74 | 91 | |
75 | 92 | |
... | ... | @@ -167,11 +184,26 @@ |
167 | 184 | const char *end = buf + len; |
168 | 185 | size_t todo; /* number of bytes to do in this pass */ |
169 | 186 | size_t skipped = 0; /* statistics */ |
187 | + const ulong start_time = get_timer(0); | |
188 | + size_t scale = 1; | |
189 | + const char *start_buf = buf; | |
190 | + ulong delta; | |
170 | 191 | |
192 | + if (end - buf >= 200) | |
193 | + scale = (end - buf) / 100; | |
171 | 194 | cmp_buf = malloc(flash->sector_size); |
172 | 195 | if (cmp_buf) { |
196 | + ulong last_update = get_timer(0); | |
197 | + | |
173 | 198 | for (; buf < end && !err_oper; buf += todo, offset += todo) { |
174 | 199 | todo = min(end - buf, flash->sector_size); |
200 | + if (get_timer(last_update) > 100) { | |
201 | + printf(" \rUpdating, %zu%% %lu B/s", | |
202 | + 100 - (end - buf) / scale, | |
203 | + bytes_per_second(buf - start_buf, | |
204 | + start_time)); | |
205 | + last_update = get_timer(0); | |
206 | + } | |
175 | 207 | err_oper = spi_flash_update_block(flash, offset, todo, |
176 | 208 | buf, cmp_buf, &skipped); |
177 | 209 | } |
178 | 210 | |
... | ... | @@ -179,12 +211,17 @@ |
179 | 211 | err_oper = "malloc"; |
180 | 212 | } |
181 | 213 | free(cmp_buf); |
214 | + putc('\r'); | |
182 | 215 | if (err_oper) { |
183 | 216 | printf("SPI flash failed in %s step\n", err_oper); |
184 | 217 | return 1; |
185 | 218 | } |
186 | - printf("%zu bytes written, %zu bytes skipped\n", len - skipped, | |
187 | - skipped); | |
219 | + | |
220 | + delta = get_timer(start_time); | |
221 | + printf("%zu bytes written, %zu bytes skipped", len - skipped, | |
222 | + skipped); | |
223 | + printf(" in %ld.%lds, speed %ld B/s\n", | |
224 | + delta / 1000, delta % 1000, bytes_per_second(len, start_time)); | |
188 | 225 | |
189 | 226 | return 0; |
190 | 227 | } |