Add two simple example BPF schedulers - simple and qmap. * simple: In terms of scheduling, it behaves identical to not having any operation implemented at all. The two operations it implements are only to improve visibility and exit handling. On certain homogeneous configurations, this actually can perform pretty well. * qmap: A fixed five level priority scheduler to demonstrate queueing PIDs on BPF maps for scheduling. While not very practical, this is useful as a simple example and will be used to demonstrate different features. v7: - Compat helpers stripped out in prepartion of upstreaming as the upstreamed patchset will be the baselinfe. Utility macros that can be used to implement compat features are kept. - Explicitly disable map autoattach on struct_ops to avoid trying to attach twice while maintaining compatbility with older libbpf. v6: - Common header files reorganized and cleaned up. Compat helpers are added to demonstrate how schedulers can maintain backward compatibility with older kernels while making use of newly added features. - simple_select_cpu() added to keep track of the number of local dispatches. This is needed because the default ops.select_cpu() implementation is updated to dispatch directly and won't call ops.enqueue(). - Updated to reflect the sched_ext API changes. Switching all tasks is the default behavior now and scx_qmap supports partial switching when `-p` is specified. - tools/sched_ext/Kconfig dropped. This will be included in the doc instead. v5: - Improve Makefile. Build artifects are now collected into a separate dir which change be changed. Install and help targets are added and clean actually cleans everything. - MEMBER_VPTR() improved to improve access to structs. ARRAY_ELEM_PTR() and RESIZEABLE_ARRAY() are added to support resizable arrays in .bss. - Add scx_common.h which provides common utilities to user code such as SCX_BUG[_ON]() and RESIZE_ARRAY(). - Use SCX_BUG[_ON]() to simplify error handling. v4: - Dropped _example prefix from scheduler names. v3: - Rename scx_example_dummy to scx_example_simple and restructure a bit to ease later additions. Comment updates. - Added declarations for BPF inline iterators. In the future, hopefully, these will be consolidated into a generic BPF header so that they don't need to be replicated here. v2: - Updated with the generic BPF cpumask helpers. Signed-off-by: Tejun Heo <tj@kernel.org> Reviewed-by: David Vernet <dvernet@meta.com> Acked-by: Josh Don <joshdon@google.com> Acked-by: Hao Luo <haoluo@google.com> Acked-by: Barret Rhoden <brho@google.com>
75 lines
2.2 KiB
C
75 lines
2.2 KiB
C
/* SPDX-License-Identifier: GPL-2.0 */
|
|
/*
|
|
* Copyright (c) 2023 Meta Platforms, Inc. and affiliates.
|
|
* Copyright (c) 2023 Tejun Heo <tj@kernel.org>
|
|
* Copyright (c) 2023 David Vernet <dvernet@meta.com>
|
|
*/
|
|
#ifndef __SCHED_EXT_COMMON_H
|
|
#define __SCHED_EXT_COMMON_H
|
|
|
|
#ifdef __KERNEL__
|
|
#error "Should not be included by BPF programs"
|
|
#endif
|
|
|
|
#include <stdarg.h>
|
|
#include <stdio.h>
|
|
#include <stdlib.h>
|
|
#include <stdint.h>
|
|
#include <errno.h>
|
|
|
|
typedef uint8_t u8;
|
|
typedef uint16_t u16;
|
|
typedef uint32_t u32;
|
|
typedef uint64_t u64;
|
|
typedef int8_t s8;
|
|
typedef int16_t s16;
|
|
typedef int32_t s32;
|
|
typedef int64_t s64;
|
|
|
|
#define SCX_BUG(__fmt, ...) \
|
|
do { \
|
|
fprintf(stderr, "[SCX_BUG] %s:%d", __FILE__, __LINE__); \
|
|
if (errno) \
|
|
fprintf(stderr, " (%s)\n", strerror(errno)); \
|
|
else \
|
|
fprintf(stderr, "\n"); \
|
|
fprintf(stderr, __fmt __VA_OPT__(,) __VA_ARGS__); \
|
|
fprintf(stderr, "\n"); \
|
|
\
|
|
exit(EXIT_FAILURE); \
|
|
} while (0)
|
|
|
|
#define SCX_BUG_ON(__cond, __fmt, ...) \
|
|
do { \
|
|
if (__cond) \
|
|
SCX_BUG((__fmt) __VA_OPT__(,) __VA_ARGS__); \
|
|
} while (0)
|
|
|
|
/**
|
|
* RESIZE_ARRAY - Convenience macro for resizing a BPF array
|
|
* @__skel: the skeleton containing the array
|
|
* @elfsec: the data section of the BPF program in which the array exists
|
|
* @arr: the name of the array
|
|
* @n: the desired array element count
|
|
*
|
|
* For BPF arrays declared with RESIZABLE_ARRAY(), this macro performs two
|
|
* operations. It resizes the map which corresponds to the custom data
|
|
* section that contains the target array. As a side effect, the BTF info for
|
|
* the array is adjusted so that the array length is sized to cover the new
|
|
* data section size. The second operation is reassigning the skeleton pointer
|
|
* for that custom data section so that it points to the newly memory mapped
|
|
* region.
|
|
*/
|
|
#define RESIZE_ARRAY(__skel, elfsec, arr, n) \
|
|
do { \
|
|
size_t __sz; \
|
|
bpf_map__set_value_size((__skel)->maps.elfsec##_##arr, \
|
|
sizeof((__skel)->elfsec##_##arr->arr[0]) * (n)); \
|
|
(__skel)->elfsec##_##arr = \
|
|
bpf_map__initial_value((__skel)->maps.elfsec##_##arr, &__sz); \
|
|
} while (0)
|
|
|
|
#include "user_exit_info.h"
|
|
#include "compat.h"
|
|
|
|
#endif /* __SCHED_EXT_COMMON_H */
|