Skip to content

Commit

Permalink
common/stats: print percentage of elapsed time during sampling time
Browse files Browse the repository at this point in the history
  • Loading branch information
kasper93 committed Feb 25, 2025
1 parent ebb22bc commit c6c0f3f
Showing 1 changed file with 8 additions and 6 deletions.
14 changes: 8 additions & 6 deletions common/stats.c
Original file line number Diff line number Diff line change
Expand Up @@ -132,15 +132,17 @@ void stats_global_query(struct mpv_global *global, struct mpv_node *out)

node_init(out, MPV_FORMAT_NODE_ARRAY, NULL);

#define FMT_T(e) mp_tprintf(80, "%.3f ms", (e))
#define FMT_T(e, t) ((t) > 0 ? mp_tprintf(80, "%.3f ms (%.2f%%)", (e), ((e) / (t)) * 100) \
: mp_tprintf(80, "%.3f ms", (e)))

int64_t now = mp_time_ns();
double t_ms = 0;
if (stats->last_time) {
double t_ms = MP_TIME_NS_TO_MS(now - stats->last_time);
t_ms = MP_TIME_NS_TO_MS(now - stats->last_time);
struct mpv_node *ne = node_array_add(out, MPV_FORMAT_NODE_MAP);
node_map_add_string(ne, "name", "poll-time");
node_map_add_double(ne, "value", t_ms);
node_map_add_string(ne, "text", FMT_T(t_ms));
node_map_add_string(ne, "text", FMT_T(t_ms, 0));

// Very dirty way to reset everything if the stats.lua page was probably
// closed. Not enough energy left for clean solution. Fuck it.
Expand Down Expand Up @@ -184,9 +186,9 @@ void stats_global_query(struct mpv_global *global, struct mpv_node *out)
}
double t_cpu = MP_TIME_NS_TO_MS(e->val_th);
if (e->cpu_start_ns >= 0)
add_stat(out, e, "cpu", t_cpu, FMT_T(t_cpu));
add_stat(out, e, "cpu", t_cpu, FMT_T(t_cpu, t_ms));
double t_rt = MP_TIME_NS_TO_MS(e->val_rt);
add_stat(out, e, "time", t_rt, FMT_T(t_rt));
add_stat(out, e, "time", t_rt, FMT_T(t_rt, t_ms));
e->val_rt = e->val_th = 0;
break;
}
Expand All @@ -196,7 +198,7 @@ void stats_global_query(struct mpv_global *global, struct mpv_node *out)
e->cpu_start_ns = t;
double t_msec = MP_TIME_NS_TO_MS(t - e->cpu_start_ns);
if (e->cpu_start_ns >= 0)
add_stat(out, e, NULL, t_msec, FMT_T(t_msec));
add_stat(out, e, NULL, t_msec, FMT_T(t_msec, t_ms));
e->cpu_start_ns = t;
break;
}
Expand Down

0 comments on commit c6c0f3f

Please sign in to comment.