#include <mach/clock_types.h>
#include <sys/time.h>
#include "timestat.h"
#include "libtop.h"
#include "generic.h"
#include "preferences.h"
static bool time_insert_cell(struct statistic *s, const void *sample) {
const libtop_psamp_t *psamp = sample;
unsigned int usec, sec, min, hour, day;
char buf[GENERIC_INT_SIZE];
uint64_t timens = 0;
if(STATMODE_ACCUM == top_prefs_get_mode()) {
timens = psamp->total_timens - psamp->b_total_timens;
} else {
timens = psamp->total_timens;
}
sec = timens / NSEC_PER_SEC;
usec = (timens % NSEC_PER_SEC) / NSEC_PER_USEC ;
min = sec / 60;
hour = min / 60;
day = hour / 24;
if(min < 100) {
snprintf(buf, sizeof(buf), "%02u:%02u.%02u", min, sec % 60,
usec / 10000);
} else if(hour < 100) {
snprintf(buf, sizeof(buf), "%02u:%02u:%02u", hour, min % 60, sec % 60);
} else {
snprintf(buf, sizeof(buf), "%u hrs", hour);
}
return generic_insert_cell(s, buf);
}
static struct statistic_callbacks callbacks = {
.draw = generic_draw,
.resize_cells = generic_resize_cells,
.move_cells = generic_move_cells,
.get_request_size = generic_get_request_size,
.get_minimum_size = generic_get_minimum_size,
.insert_cell = time_insert_cell,
.reset_insertion = generic_reset_insertion
};
struct statistic *top_time_create(WINDOW *parent, const char *name) {
return create_statistic(STATISTIC_TIME, parent, NULL, &callbacks, name);
}