forked from usnistgov/ndn-dpdk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmintmr.c
83 lines (69 loc) · 2.23 KB
/
mintmr.c
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
#include "mintmr.h"
#include "../core/logger.h"
N_LOG_INIT(MinTmr);
MinSched*
MinSched_New(int nSlotBits, TscDuration interval, MinTmrCb cb, uintptr_t ctx) {
uint32_t nSlots = RTE_BIT32(nSlotBits);
NDNDPDK_ASSERT(nSlots != 0);
MinSched* sched = rte_zmalloc("MinSched", sizeof(MinSched) + nSlots * sizeof(MinTmr), 0);
sched->interval = interval;
sched->cb = cb;
sched->ctx = ctx;
sched->nSlots = nSlots;
sched->slotMask = nSlots - 1;
sched->lastSlot = nSlots - 1;
sched->nextTime = rte_get_tsc_cycles();
N_LOGI("New sched=%p slots=%" PRIu16 " interval=%" PRIu64 " cb=%p", sched, sched->nSlots,
sched->interval, cb);
MinSched_Clear(sched);
return sched;
}
void
MinSched_Clear(MinSched* sched) {
for (uint32_t i = 0; i < sched->nSlots; ++i) {
CDS_INIT_LIST_HEAD(&sched->slot[i]);
}
}
void
MinSched_Close(MinSched* sched) {
rte_free(sched);
}
void
MinSched_Trigger_(MinSched* sched, TscTime now) {
while (now >= sched->nextTime) {
sched->lastSlot = (sched->lastSlot + 1) & sched->slotMask;
N_LOGV("Trigger sched=%p slot=%" PRIu16 " time=%" PRIu64 " now=%" PRIu64, sched,
sched->lastSlot, sched->nextTime, now);
sched->nextTime += sched->interval;
struct cds_list_head* pos;
struct cds_list_head* p;
cds_list_for_each_safe (pos, p, &sched->slot[sched->lastSlot]) {
MinTmr* tmr = cds_list_entry(pos, MinTmr, h);
cds_list_del_init(pos);
sched->cb(tmr, sched->ctx);
}
}
}
void
MinTmr_Cancel_(MinTmr* tmr) {
N_LOGD("Cancel tmr=%p", tmr);
cds_list_del_init(&tmr->h);
}
bool
MinTmr_After(MinTmr* tmr, TscDuration after, MinSched* sched) {
if (likely(tmr->h.next != NULL)) {
cds_list_del(&tmr->h);
}
uint64_t nSlotsAway = RTE_MAX(after, 0) / sched->interval + 1;
if (unlikely(nSlotsAway >= sched->nSlots)) {
N_LOGW("After(too-far) sched=%p tmr=%p after=%" PRId64 " nSlotsAway=%" PRIu64, sched, tmr,
after, nSlotsAway);
MinTmr_Init(tmr);
return false;
}
uint32_t slotNum = (sched->lastSlot + nSlotsAway) & sched->slotMask;
N_LOGD("After sched=%p tmr=%p after=%" PRId64 " slot=%" PRIu16 " last=%" PRIu16, sched, tmr,
after, slotNum, sched->lastSlot);
cds_list_add_tail(&tmr->h, &sched->slot[slotNum]);
return true;
}