-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpool.patch
More file actions
93 lines (85 loc) · 2.9 KB
/
Copy pathpool.patch
File metadata and controls
93 lines (85 loc) · 2.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
diff --git a/src/cspcl.c b/src/cspcl.c
index 1603696..9dd426e 100644
--- a/src/cspcl.c
+++ b/src/cspcl.c
@@ -12,6 +12,7 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
+#include <time.h>
/* CSP library headers */
#include <csp/arch/csp_malloc.h>
@@ -47,10 +48,19 @@ static int cspcl_pool_unlock(cspcl_conn_pool_t *pool) {
#endif
}
+static uint64_t cspcl_now_ms(void) {
+ struct timespec ts;
+ clock_gettime(CLOCK_MONOTONIC, &ts);
+ return (uint64_t)ts.tv_sec * 1000ULL + (uint64_t)ts.tv_nsec / 1000000ULL;
+}
+
static csp_conn_t *cspcl_pool_get_or_create_locked(cspcl_conn_pool_t *pool,
uint8_t dest_addr,
uint8_t dest_port) {
+ uint64_t now = cspcl_now_ms();
size_t free_slot = CSPCL_CONN_POOL_SIZE;
+ size_t lru_idx = 0;
+ uint64_t lru_time = UINT64_MAX;
for (size_t i = 0; i < CSPCL_CONN_POOL_SIZE; i++) {
if (!pool->entries[i].used) {
@@ -63,8 +73,14 @@ static csp_conn_t *cspcl_pool_get_or_create_locked(cspcl_conn_pool_t *pool,
if (pool->entries[i].dest_addr == dest_addr &&
pool->entries[i].dest_port == dest_port &&
pool->entries[i].conn != NULL) {
+ pool->entries[i].last_used_ms = now;
return pool->entries[i].conn;
}
+
+ if (pool->entries[i].last_used_ms < lru_time) {
+ lru_time = pool->entries[i].last_used_ms;
+ lru_idx = i;
+ }
}
csp_conn_t *conn = csp_connect(CSP_PRIO_NORM, dest_addr, dest_port,
@@ -74,7 +90,8 @@ static csp_conn_t *cspcl_pool_get_or_create_locked(cspcl_conn_pool_t *pool,
}
if (free_slot == CSPCL_CONN_POOL_SIZE) {
- free_slot = 0;
+ /* No free slot found, evict LRU */
+ free_slot = lru_idx;
if (pool->entries[free_slot].conn != NULL) {
csp_close(pool->entries[free_slot].conn);
}
@@ -84,6 +101,7 @@ static csp_conn_t *cspcl_pool_get_or_create_locked(cspcl_conn_pool_t *pool,
pool->entries[free_slot].dest_addr = dest_addr;
pool->entries[free_slot].dest_port = dest_port;
pool->entries[free_slot].conn = conn;
+ pool->entries[free_slot].last_used_ms = now;
return conn;
}
@@ -102,6 +120,7 @@ static void cspcl_pool_invalidate_locked(cspcl_conn_pool_t *pool,
}
pool->entries[i].conn = NULL;
pool->entries[i].used = false;
+ pool->entries[i].last_used_ms = 0;
return;
}
}
@@ -255,6 +274,7 @@ void cspcl_conn_pool_cleanup(cspcl_conn_pool_t *pool) {
}
pool->entries[i].used = false;
pool->entries[i].conn = NULL;
+ pool->entries[i].last_used_ms = 0;
}
(void)cspcl_pool_unlock(pool);
diff --git a/src/cspcl.h b/src/cspcl.h
index 66ced25..0290768 100644
--- a/src/cspcl.h
+++ b/src/cspcl.h
@@ -104,6 +104,7 @@ typedef struct {
uint8_t dest_addr;
uint8_t dest_port;
csp_conn_t *conn;
+ uint64_t last_used_ms; /* Monotonic ms timestamp for LRU eviction */
} cspcl_conn_pool_entry_t;
typedef struct {