Skip to content

Commit 348d71e

Browse files
Balázs BarkóBalázs Barkó
authored andcommitted
stats: create timer to aggregated stats
timer has a lazy init (only initilaze if needed - something in the hashtable) possible feature: create configurable FREQUENCY_OF_UPDATE Signed-off-by: Balázs Barkó <Balazs.Barko@oneidentity.com>
1 parent e1eb321 commit 348d71e

File tree

1 file changed

+60
-0
lines changed

1 file changed

+60
-0
lines changed

lib/stats/aggregator/stats-aggregator-registry.c

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,18 +25,74 @@
2525
#include "stats/stats-registry.h"
2626
#include "stats/stats-query.h"
2727
#include "cfg.h"
28+
#include "iv.h"
29+
#include "timeutils/cache.h"
30+
#include "mainloop.h"
2831

32+
#define FREQUENCY_OF_UPDATE 60
2933

3034
typedef struct
3135
{
3236
GHashTable *aggregators;
37+
struct iv_timer update_timer;
3338
} StatsAggregatorContainer;
3439

3540
static StatsAggregatorContainer stats_container;
3641

3742
static GStaticMutex stats_aggregator_mutex = G_STATIC_MUTEX_INIT;
3843
static gboolean stats_aggregator_locked;
3944

45+
static void
46+
_update_func (gpointer _key, gpointer _value, gpointer _user_data)
47+
{
48+
StatsAggregator *self = (StatsAggregator *) _value;
49+
50+
if (!stats_aggregator_is_orphaned(self))
51+
stats_aggregator_aggregate(self);
52+
}
53+
54+
static void
55+
_start_timer(void)
56+
{
57+
main_loop_assert_main_thread();
58+
iv_validate_now();
59+
stats_container.update_timer.expires = iv_now;
60+
stats_container.update_timer.expires.tv_sec += FREQUENCY_OF_UPDATE;
61+
62+
iv_timer_register(&stats_container.update_timer);
63+
}
64+
65+
static void
66+
_update(void *cookie)
67+
{
68+
g_hash_table_foreach(stats_container.aggregators, _update_func, NULL);
69+
70+
if(g_hash_table_size(stats_container.aggregators) > 0
71+
&& !iv_timer_registered(&stats_container.update_timer))
72+
_start_timer();
73+
}
74+
75+
static void
76+
_init_timer(void)
77+
{
78+
IV_TIMER_INIT(&stats_container.update_timer);
79+
stats_container.update_timer.cookie = NULL;
80+
stats_container.update_timer.handler = _update;
81+
}
82+
83+
static void
84+
_stop_timer(void)
85+
{
86+
main_loop_assert_main_thread();
87+
if (iv_timer_registered(&stats_container.update_timer))
88+
iv_timer_unregister(&stats_container.update_timer);
89+
}
90+
91+
static void
92+
_deinit_timer(void)
93+
{
94+
_stop_timer();
95+
}
4096

4197
void
4298
stats_aggregator_lock(void)
@@ -121,12 +177,16 @@ stats_aggregator_registry_deinit(void)
121177
g_hash_table_destroy(stats_container.aggregators);
122178
stats_container.aggregators = NULL;
123179
g_static_mutex_free(&stats_aggregator_mutex);
180+
_deinit_timer();
124181
}
125182

126183
static void
127184
_insert_to_table(StatsAggregator *value)
128185
{
129186
g_hash_table_insert(stats_container.aggregators, &value->key, value);
187+
188+
if (!iv_timer_registered(&stats_container.update_timer))
189+
_start_timer();
130190
}
131191

132192
static gboolean

0 commit comments

Comments
 (0)