blob: 05971fe8a259ca921eb325cd7cafb6c29b6f3fbd [file] [log] [blame]
Neels Hofmeyr25c97412021-11-13 23:19:33 +01001/*! \file foo.c
2 * Report the cumulative counter of time for which a flag is true as rate counter.
3 */
4/* Copyright (C) 2021 by sysmocom - s.f.m.c. GmbH <info@sysmocom.de>
5 *
6 * All Rights Reserved
7 *
8 * Author: Neels Hofmeyr <nhofmeyr@sysmocom.de>
9 *
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU Affero General Public License as published by
12 * the Free Software Foundation; either version 3 of the License, or
13 * (at your option) any later version.
14 *
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU Affero General Public License for more details.
19 *
20 * You should have received a copy of the GNU Affero General Public License
21 * along with this program. If not, see <http://www.gnu.org/licenses/>.
22 *
23 */
24
25/*! \addtogroup time_cc
26 *
27 * Report the cumulative counter of time for which a flag is true as rate counter.
28 *
29 * Useful for reporting cumulative time counters as defined in 3GPP TS 52.402, for example allAvailableSDCCHAllocated,
30 * allAvailableTCHAllocated, availablePDCHAllocatedTime.
31 *
32 * For a usage example, see the description of struct osmo_time_cc.
33 *
34 * @{
35 * \file time_cc.c
36 */
37#include "config.h"
38#ifdef HAVE_CLOCK_GETTIME
39
40#include <limits.h>
41#include <time.h>
42
43#include <osmocom/core/tdef.h>
44#include <osmocom/core/rate_ctr.h>
45#include <osmocom/core/time_cc.h>
46
47#define GRAN_USEC(TIME_CC) ((TIME_CC)->cfg.gran_usec ? : 1000000)
48#define ROUND_THRESHOLD_USEC(TIME_CC) ((TIME_CC)->cfg.round_threshold_usec ? \
49 OSMO_MIN((TIME_CC)->cfg.round_threshold_usec, GRAN_USEC(TIME_CC)) \
50 : (GRAN_USEC(TIME_CC) / 2))
51
52static uint64_t time_now_usec()
53{
54 struct timespec tp;
55 if (osmo_clock_gettime(CLOCK_MONOTONIC, &tp))
56 return 0;
57 return (uint64_t)tp.tv_sec * 1000000 + tp.tv_nsec / 1000;
58}
59
60static void osmo_time_cc_forget_sum(struct osmo_time_cc *tc, uint64_t now);
61
62static void osmo_time_cc_update_from_tdef(struct osmo_time_cc *tc, uint64_t now)
63{
64 bool do_forget_sum = false;
65 if (!tc->cfg.T_defs)
66 return;
67 if (tc->cfg.T_gran) {
68 uint64_t was = GRAN_USEC(tc);
69 tc->cfg.gran_usec = osmo_tdef_get(tc->cfg.T_defs, tc->cfg.T_gran, OSMO_TDEF_US, -1);
70 if (was != GRAN_USEC(tc))
71 do_forget_sum = true;
72 }
73 if (tc->cfg.T_round_threshold)
74 tc->cfg.round_threshold_usec = osmo_tdef_get(tc->cfg.T_defs, tc->cfg.T_round_threshold,
75 OSMO_TDEF_US, -1);
76 if (tc->cfg.T_forget_sum) {
77 uint64_t was = tc->cfg.forget_sum_usec;
78 tc->cfg.forget_sum_usec = osmo_tdef_get(tc->cfg.T_defs, tc->cfg.T_forget_sum, OSMO_TDEF_US, -1);
79 if (tc->cfg.forget_sum_usec && was != tc->cfg.forget_sum_usec)
80 do_forget_sum = true;
81 }
82
83 if (do_forget_sum && tc->sum)
84 osmo_time_cc_forget_sum(tc, now);
85}
86
87static void osmo_time_cc_schedule_timer(struct osmo_time_cc *tc, uint64_t now);
88
89/*! Clear out osmo_timer and internal counting state of struct osmo_time_cc. The .cfg remains unaffected. After calling,
90 * the osmo_time_cc instance can be used again to accumulate state as if it had just been initialized. */
91void osmo_time_cc_cleanup(struct osmo_time_cc *tc)
92{
93 osmo_timer_del(&tc->timer);
94 *tc = (struct osmo_time_cc){
95 .cfg = tc->cfg,
96 };
97}
98
99static void osmo_time_cc_start(struct osmo_time_cc *tc, uint64_t now)
100{
101 osmo_time_cc_cleanup(tc);
102 tc->start_time = now;
103 tc->last_counted_time = now;
104 osmo_time_cc_update_from_tdef(tc, now);
105 osmo_time_cc_schedule_timer(tc, now);
106}
107
108static void osmo_time_cc_count_time(struct osmo_time_cc *tc, uint64_t now)
109{
110 uint64_t time_delta = now - tc->last_counted_time;
111 tc->last_counted_time = now;
112 if (!tc->flag_state)
113 return;
114 /* Flag is currently true, cumulate the elapsed time */
115 tc->total_sum += time_delta;
116 tc->sum += time_delta;
117}
118
119static void osmo_time_cc_report(struct osmo_time_cc *tc, uint64_t now)
120{
121 uint64_t delta;
122 uint64_t n;
Neels Hofmeyr25c97412021-11-13 23:19:33 +0100123 /* We report a sum "rounded up", ahead of time. If the granularity period has not yet elapsed after the last
124 * reporting, do not report again yet. */
125 if (tc->reported_sum > tc->sum)
126 return;
127 delta = tc->sum - tc->reported_sum;
128 /* elapsed full periods */
129 n = delta / GRAN_USEC(tc);
130 /* If the delta has passed round_threshold (normally half of gran_usec), increment. */
131 delta -= n * GRAN_USEC(tc);
132 if (delta >= ROUND_THRESHOLD_USEC(tc))
133 n++;
134 if (!n)
135 return;
136
137 /* integer sanity, since rate_ctr_add() takes an int argument. */
138 if (n > INT_MAX)
139 n = INT_MAX;
Neels Hofmeyrb54229d2022-03-08 23:41:23 +0100140 if (tc->cfg.rate_ctr)
141 rate_ctr_add(tc->cfg.rate_ctr, n);
Neels Hofmeyr25c97412021-11-13 23:19:33 +0100142 /* Store the increments of gran_usec that were counted. */
143 tc->reported_sum += n * GRAN_USEC(tc);
144}
145
146static void osmo_time_cc_forget_sum(struct osmo_time_cc *tc, uint64_t now)
147{
148 tc->reported_sum = 0;
149 tc->sum = 0;
150
151 if (tc->last_counted_time < now)
152 tc->last_counted_time = now;
153}
154
155/*! Initialize struct osmo_time_cc. Call this once before use, and before setting up the .cfg items. */
156void osmo_time_cc_init(struct osmo_time_cc *tc)
157{
158 *tc = (struct osmo_time_cc){0};
159}
160
161/*! Report state to be recorded by osmo_time_cc instance. Setting an unchanged state repeatedly has no effect. */
162void osmo_time_cc_set_flag(struct osmo_time_cc *tc, bool flag)
163{
164 uint64_t now = time_now_usec();
165 if (!tc->start_time)
166 osmo_time_cc_start(tc, now);
167 /* No flag change == no effect */
168 if (flag == tc->flag_state)
169 return;
170 /* Sum up elapsed time, report increments for that. */
171 osmo_time_cc_count_time(tc, now);
172 osmo_time_cc_report(tc, now);
173 tc->flag_state = flag;
174 osmo_time_cc_schedule_timer(tc, now);
175}
176
177static void osmo_time_cc_timer_cb(void *data)
178{
179 struct osmo_time_cc *tc = data;
180 uint64_t now = time_now_usec();
181
182 osmo_time_cc_update_from_tdef(tc, now);
183
184 if (tc->flag_state) {
185 osmo_time_cc_count_time(tc, now);
186 osmo_time_cc_report(tc, now);
187 } else if (tc->cfg.forget_sum_usec && tc->sum
188 && (now >= tc->last_counted_time + tc->cfg.forget_sum_usec)) {
189 osmo_time_cc_forget_sum(tc, now);
190 }
191 osmo_time_cc_schedule_timer(tc, now);
192}
193
194/*! Figure out the next time we should do anything, if the flag state remains unchanged. */
195static void osmo_time_cc_schedule_timer(struct osmo_time_cc *tc, uint64_t now)
196{
197 uint64_t next_event = UINT64_MAX;
198
199 osmo_time_cc_update_from_tdef(tc, now);
200
201 /* If it is required, when will the next forget_sum happen? */
202 if (tc->cfg.forget_sum_usec && !tc->flag_state && tc->sum > 0) {
203 uint64_t next_forget_time = tc->last_counted_time + tc->cfg.forget_sum_usec;
204 next_event = OSMO_MIN(next_event, next_forget_time);
205 }
206 /* Next rate_ctr increment? */
Neels Hofmeyrb54229d2022-03-08 23:41:23 +0100207 if (tc->flag_state) {
Neels Hofmeyr25c97412021-11-13 23:19:33 +0100208 uint64_t next_inc = now + (tc->reported_sum - tc->sum) + ROUND_THRESHOLD_USEC(tc);
209 next_event = OSMO_MIN(next_event, next_inc);
210 }
211
212 /* No event coming up? */
213 if (next_event == UINT64_MAX)
214 return;
215
216 if (next_event <= now)
217 next_event = 0;
218 else
219 next_event -= now;
220
221 osmo_timer_setup(&tc->timer, osmo_time_cc_timer_cb, tc);
222 osmo_timer_del(&tc->timer);
223 osmo_timer_schedule(&tc->timer, next_event / 1000000, next_event % 1000000);
224}
225
226#endif /* HAVE_CLOCK_GETTIME */
227
228/*! @} */