Skip to content

Commit d07be81

Browse files
Changwoo Minhtejun
authored andcommitted
sched_ext: Add time helpers for BPF schedulers
The following functions are added for BPF schedulers: - time_delta(after, before) - time_after(a, b) - time_before(a, b) - time_after_eq(a, b) - time_before_eq(a, b) - time_in_range(a, b, c) - time_in_range_open(a, b, c) Signed-off-by: Changwoo Min <changwoo@igalia.com> Acked-by: Andrea Righi <arighi@nvidia.com> Signed-off-by: Tejun Heo <tj@kernel.org>
1 parent 2e1ce39 commit d07be81

File tree

1 file changed

+94
-0
lines changed

1 file changed

+94
-0
lines changed

tools/sched_ext/include/scx/common.bpf.h

Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -408,6 +408,100 @@ static __always_inline const struct cpumask *cast_mask(struct bpf_cpumask *mask)
408408
void bpf_rcu_read_lock(void) __ksym;
409409
void bpf_rcu_read_unlock(void) __ksym;
410410

411+
/*
412+
* Time helpers, most of which are from jiffies.h.
413+
*/
414+
415+
/**
416+
* time_delta - Calculate the delta between new and old time stamp
417+
* @after: first comparable as u64
418+
* @before: second comparable as u64
419+
*
420+
* Return: the time difference, which is >= 0
421+
*/
422+
static inline s64 time_delta(u64 after, u64 before)
423+
{
424+
return (s64)(after - before) > 0 ? : 0;
425+
}
426+
427+
/**
428+
* time_after - returns true if the time a is after time b.
429+
* @a: first comparable as u64
430+
* @b: second comparable as u64
431+
*
432+
* Do this with "<0" and ">=0" to only test the sign of the result. A
433+
* good compiler would generate better code (and a really good compiler
434+
* wouldn't care). Gcc is currently neither.
435+
*
436+
* Return: %true is time a is after time b, otherwise %false.
437+
*/
438+
static inline bool time_after(u64 a, u64 b)
439+
{
440+
return (s64)(b - a) < 0;
441+
}
442+
443+
/**
444+
* time_before - returns true if the time a is before time b.
445+
* @a: first comparable as u64
446+
* @b: second comparable as u64
447+
*
448+
* Return: %true is time a is before time b, otherwise %false.
449+
*/
450+
static inline bool time_before(u64 a, u64 b)
451+
{
452+
return time_after(b, a);
453+
}
454+
455+
/**
456+
* time_after_eq - returns true if the time a is after or the same as time b.
457+
* @a: first comparable as u64
458+
* @b: second comparable as u64
459+
*
460+
* Return: %true is time a is after or the same as time b, otherwise %false.
461+
*/
462+
static inline bool time_after_eq(u64 a, u64 b)
463+
{
464+
return (s64)(a - b) >= 0;
465+
}
466+
467+
/**
468+
* time_before_eq - returns true if the time a is before or the same as time b.
469+
* @a: first comparable as u64
470+
* @b: second comparable as u64
471+
*
472+
* Return: %true is time a is before or the same as time b, otherwise %false.
473+
*/
474+
static inline bool time_before_eq(u64 a, u64 b)
475+
{
476+
return time_after_eq(b, a);
477+
}
478+
479+
/**
480+
* time_in_range - Calculate whether a is in the range of [b, c].
481+
* @a: time to test
482+
* @b: beginning of the range
483+
* @c: end of the range
484+
*
485+
* Return: %true is time a is in the range [b, c], otherwise %false.
486+
*/
487+
static inline bool time_in_range(u64 a, u64 b, u64 c)
488+
{
489+
return time_after_eq(a, b) && time_before_eq(a, c);
490+
}
491+
492+
/**
493+
* time_in_range_open - Calculate whether a is in the range of [b, c).
494+
* @a: time to test
495+
* @b: beginning of the range
496+
* @c: end of the range
497+
*
498+
* Return: %true is time a is in the range [b, c), otherwise %false.
499+
*/
500+
static inline bool time_in_range_open(u64 a, u64 b, u64 c)
501+
{
502+
return time_after_eq(a, b) && time_before(a, c);
503+
}
504+
411505

412506
/*
413507
* Other helpers

0 commit comments

Comments
 (0)