1/* Copyright (c) 2017 - 2022 LiteSpeed Technologies Inc.  See LICENSE. */
2#include <time.h>
3#ifdef WIN32
4#include <vc_compat.h>
5#define localtime_r(a,b) localtime_s(b,a)
6#endif
7
8#include "lsquic_eng_hist.h"
9
10#if ENG_HIST_ENABLED
11
12#define LSQUIC_LOGGER_MODULE LSQLM_ENG_HIST
13#include "lsquic_types.h"
14#include "lsquic_logger.h"
15
16
17static void
18log_hist_slice (const struct hist_slice *slice, time_t t)
19{
20    size_t strftime(char *s, size_t max, const char *format,
21                                  const struct tm *tm);
22    if (slice->sl_packets_in == 0 &&
23        slice->sl_packets_out == 0 &&
24        slice->sl_del_mini_conns == 0 &&
25        slice->sl_del_full_conns == 0)
26        return;
27
28    struct tm tm;
29    char timestr[sizeof("12:00:00")];
30
31    localtime_r(&t, &tm);
32    strftime(timestr, sizeof(timestr), "%T", &tm);
33
34    LSQ_DEBUG("%s: pi: %u; po: %u; +mc: %u; -mc: %u; +fc: %u; -fc: %u",
35        timestr,
36        slice->sl_packets_in,
37        slice->sl_packets_out,
38        slice->sl_new_mini_conns,
39        slice->sl_del_mini_conns,
40        slice->sl_new_full_conns,
41        slice->sl_del_full_conns);
42}
43
44
45void
46lsquic_eng_hist_log (const struct eng_hist *hist)
47{
48    unsigned i, idx;
49    time_t t0 = time(NULL) - ENG_HIST_NELEMS + 1;
50    for (i = 0; i < ENG_HIST_NELEMS; ++i)
51    {
52        idx = (hist->eh_prev_idx + i + 1) & (ENG_HIST_NELEMS - 1);
53        if (i >= ENG_HIST_NELEMS - ENG_HIST_N_TO_PRINT)
54            log_hist_slice(&hist->eh_slices[idx], t0 + i);
55    }
56}
57
58#endif
59