[Pdbg] [PATCH v3 2/3] libpdbg: Add function to register progress tick callback
Alistair Popple
alistair at popple.id.au
Thu May 17 15:44:27 AEST 2018
For long running library operations such as getmem it is useful to give the
application an ability to report progress back to the user. This adds a way to
register a progress_tick callback which takes the current and maximum progress
counts.
Signed-off-by: Alistair Popple <alistair at popple.id.au>
Suggested-by: Amitay Isaacs <amitay at ozlabs.org>
---
Makefile.am | 3 +--
libpdbg/libpdbg.c | 13 +++++++++++++
libpdbg/libpdbg.h | 5 +++++
src/progress.c | 10 ++++------
src/progress.h | 4 ++--
5 files changed, 25 insertions(+), 10 deletions(-)
diff --git a/Makefile.am b/Makefile.am
index ae8929b..dd6569d 100644
--- a/Makefile.am
+++ b/Makefile.am
@@ -12,7 +12,7 @@ AM_CFLAGS = -I$(top_srcdir)/ccan/array_size -Wall -Werror -O2
pdbg_SOURCES = \
src/main.c src/cfam.c src/scom.c src/reg.c src/mem.c src/thread.c \
- src/ring.c src/htm.c src/options_ at ARCH@.c
+ src/ring.c src/htm.c src/progress.c src/options_ at ARCH@.c
pdbg_LDADD = fake.dtb.o p8-fsi.dtb.o p8-i2c.dtb.o p9w-fsi.dtb.o p8-host.dtb.o \
p9z-fsi.dtb.o p9r-fsi.dtb.o p9-kernel.dtb.o libpdbg.la libfdt.la \
@@ -52,7 +52,6 @@ libpdbg_la_SOURCES = \
libpdbg/adu.c \
libpdbg/device.c \
libpdbg/target.c \
- libpdbg/progress.c \
libpdbg/htm.c
M4_V = $(M4_V_$(V))
diff --git a/libpdbg/libpdbg.c b/libpdbg/libpdbg.c
index 0d91cc3..5b4ffdb 100644
--- a/libpdbg/libpdbg.c
+++ b/libpdbg/libpdbg.c
@@ -4,6 +4,8 @@
#include "device.h"
#include "libpdbg.h"
+static pdbg_progress_tick_t progress_tick;
+
struct pdbg_target *__pdbg_next_target(const char *class, struct pdbg_target *parent, struct pdbg_target *last)
{
struct pdbg_target *next, *tmp;
@@ -167,3 +169,14 @@ int pdbg_get_u64_property(struct pdbg_target *target, const char *name, uint64_t
return -1;
}
+
+void pdbg_progress_tick(uint64_t cur, uint64_t end)
+{
+ if (progress_tick)
+ progress_tick(cur, end);
+}
+
+void pdbg_set_progress_tick(pdbg_progress_tick_t fn)
+{
+ progress_tick = fn;
+}
diff --git a/libpdbg/libpdbg.h b/libpdbg/libpdbg.h
index 50baacc..7d05bcc 100644
--- a/libpdbg/libpdbg.h
+++ b/libpdbg/libpdbg.h
@@ -133,4 +133,9 @@ int adu_putmem(struct pdbg_target *target, uint64_t addr, uint8_t *input, uint64
int opb_read(struct pdbg_target *target, uint32_t addr, uint32_t *data);
int opb_write(struct pdbg_target *target, uint32_t addr, uint32_t data);
+typedef void (*pdbg_progress_tick_t)(uint64_t cur, uint64_t end);
+
+void pdbg_set_progress_tick(pdbg_progress_tick_t fn);
+void pdbg_progress_tick(uint64_t cur, uint64_t end);
+
#endif
diff --git a/src/progress.c b/src/progress.c
index cf95015..fe443b9 100644
--- a/src/progress.c
+++ b/src/progress.c
@@ -22,7 +22,6 @@
#include "progress.h"
-static uint64_t progress_max;
static uint64_t progress_pcent;
static uint64_t progress_n_upd;
static time_t progress_prevsec;
@@ -30,11 +29,10 @@ static struct timespec progress_start;
#define PROGRESS_CHARS 50
-void progress_init(uint64_t count)
+void progress_init(void)
{
unsigned int i;
- progress_max = count;
progress_pcent = 0;
progress_n_upd = ULONG_MAX;
progress_prevsec = ULONG_MAX;
@@ -46,16 +44,16 @@ void progress_init(uint64_t count)
fflush(stderr);
clock_gettime(CLOCK_MONOTONIC, &progress_start);}
-void progress_tick(uint64_t cur)
+void progress_tick(uint64_t cur, uint64_t end)
{
unsigned int i, pos;
struct timespec now;
uint64_t pcent;
double sec;
- pcent = (cur * 100) / progress_max;
+ pcent = (cur * 100) / end;
if (progress_pcent == pcent && cur < progress_n_upd &&
- cur < progress_max)
+ cur < end)
return;
progress_pcent = pcent;
pos = (pcent * PROGRESS_CHARS) / 101;
diff --git a/src/progress.h b/src/progress.h
index 990f6d5..48c93b2 100644
--- a/src/progress.h
+++ b/src/progress.h
@@ -17,8 +17,8 @@
#include <inttypes.h>
-void progress_init(uint64_t count);
-void progress_tick(uint64_t cur);
+void progress_init(void);
+void progress_tick(uint64_t cur, uint64_t end);
void progress_end(void);
#endif /* __PROGRESS_H */
--
2.11.0
More information about the Pdbg
mailing list