-
Notifications
You must be signed in to change notification settings - Fork 203
/
Copy pathpf_scheduler.c
580 lines (519 loc) · 15.8 KB
/
pf_scheduler.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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
/*********************************************************************
* _ _ _
* _ __ | |_ _ | | __ _ | |__ ___
* | '__|| __|(_)| | / _` || '_ \ / __|
* | | | |_ _ | || (_| || |_) |\__ \
* |_| \__|(_)|_| \__,_||_.__/ |___/
*
* www.rt-labs.com
* Copyright 2018 rt-labs AB, Sweden.
*
* This software is dual-licensed under GPLv3 and a commercial
* license. See the file LICENSE.md distributed with this software for
* full license information.
********************************************************************/
#ifdef UNIT_TEST
#define os_get_current_time_us mock_os_get_current_time_us
#endif
#include "pf_includes.h"
#include <inttypes.h>
#include <string.h>
static bool pf_scheduler_is_linked (pnet_t * net, uint32_t first, uint32_t ix)
{
bool ret = false;
uint32_t cnt = 0;
if (ix < PF_MAX_TIMEOUTS)
{
while ((first < PF_MAX_TIMEOUTS) && (cnt < 20))
{
if (first == ix)
{
ret = true;
}
first = net->scheduler_timeouts[first].next;
cnt++;
}
}
return ret;
}
static void pf_scheduler_unlink (
pnet_t * net,
volatile uint32_t * p_q,
uint32_t ix)
{
/* Unlink from busy list */
uint32_t prev_ix;
uint32_t next_ix;
if (ix >= PF_MAX_TIMEOUTS)
{
LOG_ERROR (
PNET_LOG,
"Sched(%d): ix (%u) is invalid\n",
__LINE__,
(unsigned)ix);
}
else if (pf_scheduler_is_linked (net, *p_q, ix) == false)
{
LOG_ERROR (
PNET_LOG,
"Sched(%d): %s is not in Q\n",
__LINE__,
net->scheduler_timeouts[ix].p_name);
}
else
{
prev_ix = net->scheduler_timeouts[ix].prev;
next_ix = net->scheduler_timeouts[ix].next;
if (*p_q == ix)
{
*p_q = next_ix;
}
if (next_ix < PF_MAX_TIMEOUTS)
{
net->scheduler_timeouts[next_ix].prev = prev_ix;
}
if (prev_ix < PF_MAX_TIMEOUTS)
{
net->scheduler_timeouts[prev_ix].next = next_ix;
}
}
}
static void pf_scheduler_link_after (
pnet_t * net,
volatile uint32_t * p_q,
uint32_t ix,
uint32_t pos)
{
uint32_t next_ix;
if (ix >= PF_MAX_TIMEOUTS)
{
LOG_ERROR (
PNET_LOG,
"Sched(%d): ix (%u) is invalid\n",
__LINE__,
(unsigned)ix);
}
else if (pf_scheduler_is_linked (net, *p_q, ix) == true)
{
LOG_ERROR (
PNET_LOG,
"Sched(%d): %s is already in Q\n",
__LINE__,
net->scheduler_timeouts[ix].p_name);
}
else if (pos >= PF_MAX_TIMEOUTS)
{
/* Put first in possible non-empty Q */
net->scheduler_timeouts[ix].prev = PF_MAX_TIMEOUTS;
net->scheduler_timeouts[ix].next = *p_q;
if (*p_q < PF_MAX_TIMEOUTS)
{
net->scheduler_timeouts[*p_q].prev = ix;
}
*p_q = ix;
}
else if (*p_q >= PF_MAX_TIMEOUTS)
{
/* Q is empty - insert first in Q */
net->scheduler_timeouts[ix].prev = PF_MAX_TIMEOUTS;
net->scheduler_timeouts[ix].next = PF_MAX_TIMEOUTS;
*p_q = ix;
}
else
{
next_ix = net->scheduler_timeouts[pos].next;
if (next_ix < PF_MAX_TIMEOUTS)
{
net->scheduler_timeouts[next_ix].prev = ix;
}
net->scheduler_timeouts[pos].next = ix;
net->scheduler_timeouts[ix].prev = pos;
net->scheduler_timeouts[ix].next = next_ix;
}
}
static void pf_scheduler_link_before (
pnet_t * net,
volatile uint32_t * p_q,
uint32_t ix,
uint32_t pos)
{
uint32_t prev_ix;
if (ix >= PF_MAX_TIMEOUTS)
{
LOG_ERROR (
PNET_LOG,
"Sched(%d): ix (%u) is invalid\n",
__LINE__,
(unsigned)ix);
}
else if (pf_scheduler_is_linked (net, *p_q, ix) == true)
{
LOG_ERROR (
PNET_LOG,
"Sched(%d): %s is already in Q\n",
__LINE__,
net->scheduler_timeouts[ix].p_name);
}
else if (pos >= PF_MAX_TIMEOUTS)
{
/* Put first in possible non-empty Q */
net->scheduler_timeouts[ix].prev = PF_MAX_TIMEOUTS;
net->scheduler_timeouts[ix].next = *p_q;
if (*p_q < PF_MAX_TIMEOUTS)
{
net->scheduler_timeouts[*p_q].prev = ix;
}
*p_q = ix;
}
else if (*p_q >= PF_MAX_TIMEOUTS)
{
/* Q is empty - insert first in Q */
net->scheduler_timeouts[ix].prev = PF_MAX_TIMEOUTS;
net->scheduler_timeouts[ix].next = PF_MAX_TIMEOUTS;
*p_q = ix;
}
else
{
prev_ix = net->scheduler_timeouts[pos].prev;
if (prev_ix < PF_MAX_TIMEOUTS)
{
net->scheduler_timeouts[prev_ix].next = ix;
}
net->scheduler_timeouts[pos].prev = ix;
net->scheduler_timeouts[ix].next = pos;
net->scheduler_timeouts[ix].prev = prev_ix;
if (*p_q == pos)
{
/* ix is now first in the Q */
*p_q = ix;
}
}
}
void pf_scheduler_init (pnet_t * net, uint32_t tick_interval)
{
uint32_t ix;
net->scheduler_timeout_first = PF_MAX_TIMEOUTS; /* Nothing in queue */
net->scheduler_timeout_free = PF_MAX_TIMEOUTS; /* Nothing in queue. */
if (net->scheduler_timeout_mutex == NULL)
{
net->scheduler_timeout_mutex = os_mutex_create();
}
memset ((void *)net->scheduler_timeouts, 0, sizeof (net->scheduler_timeouts));
net->scheduler_tick_interval = tick_interval;
CC_ASSERT (net->scheduler_tick_interval > 0);
/* Link all entries into a list and put them into the free queue. */
for (ix = PF_MAX_TIMEOUTS; ix > 0; ix--)
{
net->scheduler_timeouts[ix - 1].p_name = "<free>";
net->scheduler_timeouts[ix - 1].in_use = false;
pf_scheduler_link_before (
net,
&net->scheduler_timeout_free,
ix - 1,
net->scheduler_timeout_free);
}
}
void pf_scheduler_exit (pnet_t * net){
if (net->scheduler_timeout_mutex != NULL)
{
os_mutex_destroy (net->scheduler_timeout_mutex);
memset (&net->scheduler_timeout_mutex, 0, sizeof (net->scheduler_timeout_mutex));
}
}
int pf_scheduler_add (
pnet_t * net,
uint32_t delay,
const char * p_name,
pf_scheduler_timeout_ftn_t cb,
void * arg,
uint32_t * p_timeout)
{
uint32_t ix_this;
uint32_t ix_prev;
uint32_t ix_free;
uint32_t now = os_get_current_time_us();
delay =
pf_scheduler_sanitize_delay (delay, net->scheduler_tick_interval, true);
os_mutex_lock (net->scheduler_timeout_mutex);
/* Unlink from the free list */
ix_free = net->scheduler_timeout_free;
pf_scheduler_unlink (net, &net->scheduler_timeout_free, ix_free);
os_mutex_unlock (net->scheduler_timeout_mutex);
if (ix_free >= PF_MAX_TIMEOUTS)
{
LOG_ERROR (
PNET_LOG,
"SCHEDULER(%d): Out of timeout resources!!\n",
__LINE__);
return -1;
}
net->scheduler_timeouts[ix_free].in_use = true;
net->scheduler_timeouts[ix_free].p_name = p_name;
net->scheduler_timeouts[ix_free].cb = cb;
net->scheduler_timeouts[ix_free].arg = arg;
net->scheduler_timeouts[ix_free].when = now + delay;
os_mutex_lock (net->scheduler_timeout_mutex);
if (net->scheduler_timeout_first >= PF_MAX_TIMEOUTS)
{
/* Put into empty q */
pf_scheduler_link_before (
net,
&net->scheduler_timeout_first,
ix_free,
PF_MAX_TIMEOUTS);
}
else if (
((int32_t) (
net->scheduler_timeouts[ix_free].when -
net->scheduler_timeouts[net->scheduler_timeout_first].when)) <= 0)
{
/* Put first in non-empty q */
pf_scheduler_link_before (
net,
&net->scheduler_timeout_first,
ix_free,
net->scheduler_timeout_first);
}
else
{
/* Find pos in non-empty q */
ix_prev = net->scheduler_timeout_first;
ix_this = net->scheduler_timeouts[net->scheduler_timeout_first].next;
while ((ix_this < PF_MAX_TIMEOUTS) &&
(((int32_t) (
net->scheduler_timeouts[ix_free].when -
net->scheduler_timeouts[ix_this].when)) > 0))
{
ix_prev = ix_this;
ix_this = net->scheduler_timeouts[ix_this].next;
}
/* Put after ix_prev */
pf_scheduler_link_after (
net,
&net->scheduler_timeout_first,
ix_free,
ix_prev);
}
os_mutex_unlock (net->scheduler_timeout_mutex);
*p_timeout = ix_free + 1; /* Make sure 0 is invalid. */
return 0;
}
void pf_scheduler_remove (pnet_t * net, const char * p_name, uint32_t timeout)
{
uint16_t ix;
if (timeout == 0)
{
LOG_DEBUG (
PNET_LOG,
"SCHEDULER(%d): timeout == 0 for event %s. No removal.\n",
__LINE__,
p_name);
}
else
{
ix = timeout - 1; /* Refer to _add() on how p_timeout is created */
os_mutex_lock (net->scheduler_timeout_mutex);
if (net->scheduler_timeouts[ix].p_name != p_name)
{
LOG_ERROR (
PNET_LOG,
"SCHEDULER(%d): Expected %s but got %s. No removal.\n",
__LINE__,
net->scheduler_timeouts[ix].p_name,
p_name);
}
else
{
pf_scheduler_unlink (net, &net->scheduler_timeout_first, ix);
/* Insert into free list. */
net->scheduler_timeouts[ix].in_use = false;
pf_scheduler_link_before (
net,
&net->scheduler_timeout_free,
ix,
net->scheduler_timeout_free);
}
os_mutex_unlock (net->scheduler_timeout_mutex);
}
}
void pf_scheduler_tick (pnet_t * net)
{
uint32_t ix;
pf_scheduler_timeout_ftn_t ftn;
void * arg;
uint32_t pf_current_time = os_get_current_time_us();
os_mutex_lock (net->scheduler_timeout_mutex);
/* Send event to all expired delay entries. */
while ((net->scheduler_timeout_first < PF_MAX_TIMEOUTS) &&
((int32_t) (
pf_current_time -
net->scheduler_timeouts[net->scheduler_timeout_first].when) >= 0))
{
/* Unlink from busy list */
ix = net->scheduler_timeout_first;
pf_scheduler_unlink (net, &net->scheduler_timeout_first, ix);
ftn = net->scheduler_timeouts[ix].cb;
arg = net->scheduler_timeouts[ix].arg;
/* Insert into free list. */
net->scheduler_timeouts[ix].in_use = false;
pf_scheduler_link_before (
net,
&net->scheduler_timeout_free,
ix,
net->scheduler_timeout_free);
/* Send event without holding the mutex. */
os_mutex_unlock (net->scheduler_timeout_mutex);
ftn (net, arg, pf_current_time);
os_mutex_lock (net->scheduler_timeout_mutex);
}
os_mutex_unlock (net->scheduler_timeout_mutex);
}
void pf_scheduler_show (pnet_t * net)
{
uint32_t ix;
uint32_t cnt;
printf ("Scheduler (time now=%u):\n", (unsigned)os_get_current_time_us());
if (net->scheduler_timeout_mutex != NULL)
{
os_mutex_lock (net->scheduler_timeout_mutex);
}
printf (
"%-4s %-14s %-6s %-6s %-6s %s\n",
"idx",
"owner",
"in_use",
"next",
"prev",
"when");
for (ix = 0; ix < PF_MAX_TIMEOUTS; ix++)
{
printf (
"[%02u] %-14s %-6s %-6u %-6u %u\n",
(unsigned)ix,
net->scheduler_timeouts[ix].p_name,
net->scheduler_timeouts[ix].in_use ? "true" : "false",
(unsigned)net->scheduler_timeouts[ix].next,
(unsigned)net->scheduler_timeouts[ix].prev,
(unsigned)net->scheduler_timeouts[ix].when);
}
if (net->scheduler_timeout_mutex != NULL)
{
printf ("Free list:\n");
ix = net->scheduler_timeout_free;
cnt = 0;
while ((ix < PF_MAX_TIMEOUTS) && (cnt++ < 20))
{
printf ("%u ", (unsigned)ix);
ix = net->scheduler_timeouts[ix].next;
}
printf ("\nBusy list:\n");
ix = net->scheduler_timeout_first;
cnt = 0;
while ((ix < PF_MAX_TIMEOUTS) && (cnt++ < 20))
{
printf (
"%u (%u) ",
(unsigned)ix,
(unsigned)net->scheduler_timeouts[ix].when);
ix = net->scheduler_timeouts[ix].next;
}
os_mutex_unlock (net->scheduler_timeout_mutex);
}
printf ("\n");
printf (
"Uptime (in quanta of 10 ms): %" PRIu32 " \n",
pnal_get_system_uptime_10ms());
}
/**
* @internal
* Sanitize the delay to use with the scheduler, by taking the stack cycle time
* into account.
*
* If the requested delay is in the range 1.5 to 2.5 stack cycle times, this
* function will return a calculated delay giving a periodicity of 2 stack cycle
* times. If the requested time is less than 1.5 stack cycle times, the
* resulting periodicity is 1 stack cycle time.
*
* Number of stack cycle times to wait
*
* ^
* |
* 4 + +--------
* | |
* | |
* 3 + +-----------+
* | |
* | |
* 2 + +-----------+
* | |
* | |
* 1 +-----------------+
* |
* | Wanted delay
* 0 +-----------+-----------+-----------+-----------+---> (in stack cycle
* times)
*
* 0 1 2 3 4
*
* Scheduling a delay close to a multiple of the stack cycle time is risky, and
* should be avoided. These measurements were made using a Ubuntu Laptop:
*
* With a stack cycle time of 1 ms, a scheduled delay of 0-700 microseconds
* will cause a nice periodicity of 1 ms. A scheduled delay of 1000
* microseconds will sometimes fire at next cycle, sometimes not. This gives
* an event spacing of 1 or 2 ms.
*
* Similarly a scheduled delay of 1100 to 1700 microseconds causes
* a nice periodicity of 2 ms, and a scheduled delay of 2100 to 2700
* microseconds causes a nice periodicity of 3 ms.
*
* Thus calculate the number of stack cycles to wait, and calculate a delay
* corresponding to half a cycle less
* (by setting schedule_half_tick_in_advance = true). Some operating systems
* do not require this.
*
* This function calculates the delay time required to make the
* scheduler fire at a specific stack tick. However the time jitter in the
* firing is largely dependent on the underlaying operating system's ablitity to
* trigger the stack execution with a high time precision.
*
* @param wanted_delay In: Delay in microseconds.
* @param stack_cycle_time In: Stack cycle time in
* microseconds. Must be larger than 0.
* @param schedule_half_tick_in_advance In: Schedule event slightly
* earlier, to not be missed.
* @return Number of microseconds of delay to use with the scheduler.
*/
uint32_t pf_scheduler_sanitize_delay (
uint32_t wanted_delay,
uint32_t stack_cycle_time,
bool schedule_half_tick_in_advance)
{
uint32_t number_of_stack_ticks = 1; /* We must wait at least one tick */
uint32_t resulting_delay = 0;
CC_ASSERT (stack_cycle_time > 0);
/* Protect against "negative" or unreasonable values.
This might happen when subtracting two timestamps, if a deadline has been
missed. */
if (wanted_delay > PF_SCHEDULER_MAX_DELAY_US)
{
wanted_delay = 0;
}
/* Calculate integer number of ticks to wait. Use integer division for
* truncation */
if (wanted_delay > (stack_cycle_time + stack_cycle_time / 2))
{
number_of_stack_ticks =
(wanted_delay + (stack_cycle_time / 2)) / stack_cycle_time;
}
CC_ASSERT (number_of_stack_ticks >= 1);
CC_ASSERT (number_of_stack_ticks < 0x80000000); /* No rollover to 'negative'
numbers */
/* Calculate delay value for the sheduler */
resulting_delay = number_of_stack_ticks * stack_cycle_time;
if (schedule_half_tick_in_advance == true)
{
resulting_delay -= (stack_cycle_time / 2);
}
return resulting_delay;
}