Skip to content

Commit

Permalink
sched: Manipulate sched classes via sched hooks
Browse files Browse the repository at this point in the history
This skill also apply to shell to manipulate varity of cmds. The major
difference is identification. Because sched hooks are out of order,
I create a new member in `struct sched` to identify the sched class.
  • Loading branch information
iankuan authored and jserv committed Oct 19, 2017
1 parent e48d497 commit 266bde5
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 20 deletions.
10 changes: 9 additions & 1 deletion include/kernel/sched.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,21 +11,29 @@ struct thread_info;

#define SCHED_CLASS_RR 0
#define SCHED_CLASS_BITMAP 1
typedef int sched_class_t;

#define SCHED_OPT_NONE 0
#define SCHED_OPT_RESTORE_ONLY 1
#define SCHED_OPT_RESET 2
#define SCHED_OPT_TICK 3

/* scheduler implementation hooks */
#define HOOK_SCHED_CLASS(name, sched_struct) \
static struct sched *sched_class_##name \
__attribute__((section(".sched.class"), aligned(sizeof(long)), \
used)) = sched_struct;

struct sched {
sched_class_t class_type;
int (*init)(void);
int (*enqueue)(struct thread_info *thread);
int (*dequeue)(struct thread_info *thread);
int (*elect)(int switch_type);
};

int sched_init();
int sched_select(int sched_type);
int sched_select(sched_class_t sched_type);
int sched_enqueue(struct thread_info *thread);
int sched_dequeue(struct thread_info *thread);
int sched_elect(int flags);
Expand Down
47 changes: 30 additions & 17 deletions kernel/sched.c
Original file line number Diff line number Diff line change
@@ -1,35 +1,48 @@
#include <kernel/sched.h>
#include <kernel/thread.h>

extern const struct sched sched_rr;
extern const struct sched sched_bitmap;
extern unsigned long __sched_classes_start__;
extern unsigned long __sched_classes_end__;

static const struct sched *sched;
/* static vars to repesent sched classes list */
static struct sched **sched_classes =
(struct sched **) &__sched_classes_start__;
static struct sched **sched_classes_end =
(struct sched **) &__sched_classes_end__;

static struct sched *sched;

int sched_init()
{
int ret;
int ret = 0;

ret = sched_rr.init();
ret = sched_bitmap.init();
/* Initialize each scheduler class by traversing hooks */
for (struct sched **c = sched_classes;
c < sched_classes_end; c++) {
struct sched *class = *c;
ret |= class->init();
}

return ret;
}

int sched_select(int sched_type)
int sched_select(sched_class_t sched_type)
{
switch (sched_type) {
case SCHED_CLASS_RR:
sched = &sched_rr;
break;
case SCHED_CLASS_BITMAP:
sched = &sched_bitmap;
break;
default:
return -1;
int ret = -1;
struct sched *class = NULL;

/* Examine specified sched class in hooks or not */
for (struct sched **c = sched_classes;
c < sched_classes_end; c++)
if (sched_type == (*c)->class_type)
class = *c;

if (class) {
sched = class;
ret = 0;
}

return 0;
return ret;
}

int sched_enqueue(struct thread_info *thread)
Expand Down
5 changes: 4 additions & 1 deletion kernel/sched/bitmap.c
Original file line number Diff line number Diff line change
Expand Up @@ -112,10 +112,13 @@ static int sched_bitmap_elect(int flags)
}

// clang-format off
const struct sched sched_bitmap = {
static struct sched sched_bitmap = {
.class_type = SCHED_CLASS_BITMAP,
.init = sched_bitmap_init,
.enqueue = sched_bitmap_enqueue,
.dequeue = sched_bitmap_dequeue,
.elect = sched_bitmap_elect,
};
// clang-format on

HOOK_SCHED_CLASS(bitmap, &sched_bitmap)
5 changes: 4 additions & 1 deletion kernel/sched/rr.c
Original file line number Diff line number Diff line change
Expand Up @@ -87,10 +87,13 @@ int sched_rr_elect(int switch_type)
}

// clang-format off
const struct sched sched_rr = {
static struct sched sched_rr = {
.class_type = SCHED_CLASS_RR,
.init = sched_rr_init,
.enqueue = sched_rr_enqueue,
.dequeue = sched_rr_dequeue,
.elect = sched_rr_elect,
};
// clang-format on

HOOK_SCHED_CLASS(RR, &sched_rr)
3 changes: 3 additions & 0 deletions piko.lds.S
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@ SECTIONS
PROVIDE(__shell_cmd_start__ = .);
KEEP(*(.shell_cmd*))
PROVIDE(__shell_cmd_end__ = .);
PROVIDE(__sched_classes_start__ = .);
KEEP(*(.sched.class*))
PROVIDE(__sched_classes_end__ = .);
PROVIDE(__rodata_start__ = .);
*(.rodata*)
PROVIDE(__rodata_end__ = .);
Expand Down

0 comments on commit 266bde5

Please sign in to comment.