curses: Add ability to reset statistics

Press 'r' in curses mode to reset a statistic counter to
start counting from 0 again. This does NOT reset the
counter from the source itself but merely emulates a
counter reset. If you restart bmon, the counter will be
back to its total value.

Signed-off-by: Thomas Graf <tgraf@suug.ch>
This commit is contained in:
Thomas Graf 2016-01-29 12:11:44 +01:00
parent 1f79ea4396
commit 4efaa8ce9e
5 changed files with 40 additions and 6 deletions

View File

@ -42,6 +42,9 @@ struct rate
/* Value of r_current at last read */
uint64_t r_prev;
/* Reset value to substract to emulate statistics reset */
uint64_t r_reset;
/* Rate per second calculated every `rate_interval' */
float r_rate;
@ -49,6 +52,8 @@ struct rate
timestamp_t r_last_calc;
};
extern uint64_t rate_get_total(struct rate *);
enum {
ATTR_TYPE_UNSPEC,
ATTR_TYPE_COUNTER,
@ -134,5 +139,6 @@ extern struct attr * attr_select_prev(void);
extern struct attr * attr_current(void);
extern void attr_start_collecting_history(struct attr *);
extern void attr_reset_counter(struct attr *a);
#endif

View File

@ -524,6 +524,11 @@ static float __calc_usage(double rate, uint64_t max)
return 100.0f / ((double) max / (rate * cfg_rate_interval));
}
uint64_t rate_get_total(struct rate *r)
{
return r->r_total - r->r_reset;
}
void attr_calc_usage(struct attr *a, float *rx, float *tx,
uint64_t rxmax, uint64_t txmax)
{
@ -626,6 +631,14 @@ void attr_notify_update(struct attr *a, timestamp_t *ts)
}
}
void attr_reset_counter(struct attr *a)
{
if (a->a_def->ad_type == ATTR_TYPE_COUNTER) {
a->a_rx_rate.r_reset = a->a_rx_rate.r_total;
a->a_tx_rate.r_reset = a->a_tx_rate.r_total;
}
}
static void __exit attr_exit(void)
{
struct attr_def *ad, *n;

View File

@ -101,10 +101,10 @@ static void print_attr_detail(struct element *e, struct attr *a, void *arg)
char *rx_u, *tx_u;
int rxprec, txprec;
double rx = unit_value2str(a->a_rx_rate.r_total,
double rx = unit_value2str(rate_get_total(&a->a_rx_rate),
a->a_def->ad_unit,
&rx_u, &rxprec);
double tx = unit_value2str(a->a_tx_rate.r_total,
double tx = unit_value2str(rate_get_total(&a->a_tx_rate),
a->a_def->ad_unit,
&tx_u, &txprec);

View File

@ -241,10 +241,10 @@ static void draw_attr_detail(struct element *e, struct attr *a, void *arg)
int rxprec, txprec, ncol;
struct detail_arg *da = arg;
double rx = unit_value2str(a->a_rx_rate.r_total,
double rx = unit_value2str(rate_get_total(&a->a_rx_rate),
a->a_def->ad_unit,
&rx_u, &rxprec);
double tx = unit_value2str(a->a_tx_rate.r_total,
double tx = unit_value2str(rate_get_total(&a->a_tx_rate),
a->a_def->ad_unit,
&tx_u, &txprec);
@ -392,6 +392,7 @@ static void draw_help(void)
mvaddnstr(y+15, x+3, "H Start recording history data", -1);
mvaddnstr(y+16, x+3, "TAB Switch time unit of graph", -1);
mvaddnstr(y+17, x+3, "<, > Change number of graphs", -1);
mvaddnstr(y+18, x+3, "r Reset counter of element", -1);
attroff(A_STANDOUT);
@ -1079,6 +1080,16 @@ out:
refresh();
}
static void __reset_attr_counter(struct element *e, struct attr *a, void *arg)
{
attr_reset_counter(a);
}
static void reset_counters(void)
{
element_foreach_attr(current_element, __reset_attr_counter, NULL);
}
static int handle_input(int ch)
{
switch (ch)
@ -1198,6 +1209,10 @@ static int handle_input(int ch)
case '\t':
history_select_next();
return 1;
case 'r':
reset_counters();
return 1;
}
return 0;

View File

@ -123,10 +123,10 @@ static char *get_token(struct element_group *g, struct element *e,
goto out;
if (!strncasecmp(type, "rx:", 3)) {
snprintf(buf, len, "%" PRIu64, a->a_rx_rate.r_total);
snprintf(buf, len, "%" PRIu64, rate_get_total(&a->a_rx_rate));
return buf;
} else if (!strncasecmp(type, "tx:", 3)) {
snprintf(buf, len, "%" PRIu64, a->a_tx_rate.r_total);
snprintf(buf, len, "%" PRIu64, rate_get_total(&a->a_tx_rate));
return buf;
} else if (!strncasecmp(type, "rxrate:", 7)) {
snprintf(buf, len, "%.2f", a->a_rx_rate.r_rate);