ovni/src/emu/emu_stat.c

98 lines
2.2 KiB
C
Raw Normal View History

2023-02-16 15:54:25 +01:00
/* Copyright (c) 2021-2023 Barcelona Supercomputing Center (BSC)
* SPDX-License-Identifier: GPL-3.0-or-later */
#define _POSIX_C_SOURCE 200112L
#include "emu_stat.h"
#include <time.h>
#include <string.h>
static double
get_time(void)
{
struct timespec ts;
clock_gettime(CLOCK_MONOTONIC, &ts);
return (double) ts.tv_sec + (double) ts.tv_nsec * 1.0e-9;
}
void
2023-02-17 17:36:01 +01:00
emu_stat_init(struct emu_stat *stat)
2023-02-16 15:54:25 +01:00
{
memset(stat, 0, sizeof(struct emu_stat));
2023-02-16 17:03:41 +01:00
double t = get_time();
stat->last_time = t;
2023-02-17 17:36:01 +01:00
stat->reported_time = t;
2023-02-16 17:03:41 +01:00
stat->first_time = t;
2023-02-17 17:36:01 +01:00
stat->maxcalls = 100;
stat->period = 0.2;
2023-02-16 15:54:25 +01:00
}
void
2023-02-17 17:36:01 +01:00
emu_stat_report(struct emu_stat *stat, struct player *player, int last)
2023-02-16 15:54:25 +01:00
{
2023-02-16 17:03:41 +01:00
double progress = player_progress(player);
int64_t nprocessed = player_nprocessed(player);
2023-02-16 15:54:25 +01:00
2023-02-16 17:03:41 +01:00
double time_now = get_time();
double time_elapsed = time_now - stat->first_time;
double time_total = time_elapsed / progress;
double time_left = time_total - time_elapsed;
2023-02-16 15:54:25 +01:00
2023-02-17 17:36:01 +01:00
int tmin = (int) (time_left / 60.0);
int tsec = (int) ((time_left / 60.0 - tmin) * 60.0);
// int64_t delta_nprocessed = nprocessed - stat->last_nprocessed;
// double time_delta = time_now - stat->reported_time;
// double speed = 0.0;
// if (time_delta > 0.0)
// speed = (double) delta_nprocessed / time_delta;
2023-02-16 17:03:41 +01:00
/* Compute average speed since the beginning */
2023-02-17 17:36:01 +01:00
double avgspeed = 0.0;
2023-02-16 17:03:41 +01:00
if (time_elapsed > 0.0)
2023-02-17 17:36:01 +01:00
avgspeed = (double) nprocessed / time_elapsed;
2023-02-16 15:54:25 +01:00
2023-02-17 17:36:01 +01:00
verr(NULL, "ovniemu: %5.1f%% done at %.0f kev/s (%d min %d s left) \r",
2023-02-16 17:03:41 +01:00
progress * 100.0,
2023-02-17 17:36:01 +01:00
avgspeed * 1e-3,
tmin, tsec);
if (last)
fprintf(stderr, "\n");
2023-02-16 15:54:25 +01:00
2023-02-17 17:36:01 +01:00
stat->reported_time = time_now;
stat->last_nprocessed = nprocessed;
2023-02-16 15:54:25 +01:00
}
void
emu_stat_update(struct emu_stat *stat, struct player *player)
{
2023-02-17 17:36:01 +01:00
stat->ncalls++;
/* Don't do anything until we arrive at a minimum number of calls */
if (stat->ncalls < stat->maxcalls)
return;
stat->ncalls = 0;
2023-02-16 15:54:25 +01:00
double t = get_time();
2023-02-17 17:36:01 +01:00
//double dt = t - stat->last_time;
//if (dt > 300.0e-6) {
// struct emu_ev *ev = player_ev(player);
// struct stream *stream = player_stream(player);
// err("event mcv=%s took %.3f us in %s",
// ev->mcv, stream->relpath,
// dt * 1e6);
//}
stat->last_time = t;
2023-02-16 15:54:25 +01:00
/* Not yet */
2023-02-17 17:36:01 +01:00
if (stat->last_time - stat->reported_time < stat->period)
2023-02-16 15:54:25 +01:00
return;
2023-02-17 17:36:01 +01:00
emu_stat_report(stat, player, 0);
2023-02-16 15:54:25 +01:00
}