trafilo 0.1.0
Streaming event-handler framework in C
Loading...
Searching...
No Matches
window.h
Go to the documentation of this file.
1#ifndef WINDOW_H
2#define WINDOW_H
3
4#include <time.h>
5#include <stdlib.h>
6#include <stdint.h>
7
11typedef struct ts_node {
12 struct timespec ts;
13 struct ts_node *next;
14} ts_node_t;
15
19typedef struct sliding_window_t {
20 ts_node_t *head; /* oldest retained timestamp, eviction happens here */
21 ts_node_t *tail; /* newest retained timestamp, insertion happens here*/
22 size_t count; /* deque size */
23
24 long window_size_ms; /* retention horizon */
25 long slide_ms; /* emit cadence */
26
27 struct timespec last_emit; /* Last sink() */
29
30/***********
31 * Functions
32 **********/
33
41 long window_size_ms,
42 long slide_ms);
43
49
56int sliding_window_add(sliding_window_t *w, struct timespec event_ts);
57
65 struct timespec now);
66
73 struct timespec now);
74
81
87struct timespec sliding_window_oldest(const sliding_window_t *w);
88
94struct timespec sliding_window_newest(const sliding_window_t *w);
95#endif
A sliding sampling window represented as a linked list of timestamps.
Definition window.h:19
struct timespec last_emit
Definition window.h:27
ts_node_t * head
Definition window.h:20
long window_size_ms
Definition window.h:24
size_t count
Definition window.h:22
ts_node_t * tail
Definition window.h:21
long slide_ms
Definition window.h:25
A timestamp node.
Definition window.h:11
struct ts_node * next
Definition window.h:13
struct timespec ts
Definition window.h:12
int sliding_window_should_emit(const sliding_window_t *w, struct timespec now)
Checks if it's time to emit (slide_ms elapsed since last emit).
Definition window.c:78
void sliding_window_mark_emitted(sliding_window_t *w, struct timespec now)
Called on emit, sets the last emit time.
Definition window.c:90
size_t sliding_window_count(const sliding_window_t *w)
Get the count of events currently in the window.
Definition window.c:95
void sliding_window_destroy(sliding_window_t *w)
Graceful cleanup of a window. Frees all ts_node_t in the window, not the window itself.
Definition window.c:29
int sliding_window_add(sliding_window_t *w, struct timespec event_ts)
Append the event timestamp as ts_node_t to tail. Evicts head while head->ts < (event_ts - window_size...
Definition window.c:42
struct timespec sliding_window_newest(const sliding_window_t *w)
Get the timestamp of the newest event in the window.
Definition window.c:102
struct timespec sliding_window_oldest(const sliding_window_t *w)
Get the timestamp of the oldest event in the window.
Definition window.c:97
void sliding_window_init(sliding_window_t *w, long window_size_ms, long slide_ms)
Initialize a sliding window.
Definition window.c:18