2 * This file is part of the Palacios Virtual Machine Monitor developed
3 * by the V3VEE Project with funding from the United States National
4 * Science Foundation and the Department of Energy.
6 * The V3VEE Project is a joint project between Northwestern University
7 * and the University of New Mexico. You can find out more at
10 * Copyright (c) 2013, Oscar Mondragon <omondrag@cs.unm.edu>
11 * Copyright (c) 2013, Patrick G. Bridges <bridges@cs.unm.edu>
12 * Copyright (c) 2013, The V3VEE Project <http://www.v3vee.org>
13 * All rights reserved.
15 * Author: Oscar Mondragon <omondrag@cs.unm.edu>
16 * Patrick G. Bridges <bridges@cs.unm.edu>
18 * This is free software. You are permitted to use,
19 * redistribute, and modify it as specified in the file "V3VEE_LICENSE".
22 #include <palacios/vmm.h>
23 #include <palacios/vm_guest.h>
24 #include <palacios/vmm_scheduler.h>
25 #include <palacios/vmm_hashtable.h>
27 #ifndef V3_CONFIG_DEBUG_SCHEDULER
29 #define PrintDebug(fmt, args...)
32 static char default_strategy[] = "host";
33 static struct hashtable * master_scheduler_table = NULL;
34 static int create_host_scheduler();
36 static struct vm_scheduler_impl *scheduler = NULL;
38 static uint_t scheduler_hash_fn(addr_t key) {
39 char * name = (char *)key;
40 return v3_hash_buffer((uint8_t *)name, strlen(name));
43 static int scheduler_eq_fn(addr_t key1, addr_t key2) {
44 char * name1 = (char *)key1;
45 char * name2 = (char *)key2;
47 return (strcmp(name1, name2) == 0);
50 int V3_init_scheduling() {
52 PrintDebug(VM_NONE, VCORE_NONE,"Initializing scheduler");
54 master_scheduler_table = v3_create_htable(0, scheduler_hash_fn, scheduler_eq_fn);
55 return create_host_scheduler();
59 int v3_register_scheduler(struct vm_scheduler_impl *s) {
61 PrintDebug(VM_NONE, VCORE_NONE,"Registering Scheduler (%s)\n", s->name);
63 if (v3_htable_search(master_scheduler_table, (addr_t)(s->name))) {
64 PrintError(VM_NONE, VCORE_NONE, "Multiple instances of scheduler (%s)\n", s->name);
68 if (v3_htable_insert(master_scheduler_table,
71 PrintError(VM_NONE, VCORE_NONE, "Could not register scheduler (%s)\n", s->name);
78 struct vm_scheduler_impl *v3_scheduler_lookup(char *name)
80 return (struct vm_scheduler_impl *)v3_htable_search(master_scheduler_table, (addr_t)(name));
83 int V3_enable_scheduler() {
87 sched_name = v3_lookup_option("scheduler");
90 scheduler = v3_scheduler_lookup(sched_name);
94 scheduler = v3_scheduler_lookup(default_strategy);
97 PrintDebug(VM_NONE, VCORE_NONE,"Scheduler %s found",scheduler->name);
100 PrintError(VM_NONE, VCORE_NONE,"Specified Palacios scheduler \"%s\" not found.\n", default_strategy);
103 if (scheduler->init) {
104 return scheduler->init();
110 int v3_scheduler_register_vm(struct v3_vm_info *vm) {
111 if (scheduler->vm_init) {
112 return scheduler->vm_init(vm);
117 int v3_scheduler_register_core(struct guest_info *core) {
118 if (scheduler->core_init) {
119 return scheduler->core_init(core);
124 int v3_scheduler_admit_vm(struct v3_vm_info *vm) {
125 if (scheduler->admit) {
126 return scheduler->admit(vm);
131 int v3_scheduler_notify_remap(struct v3_vm_info *vm) {
132 if (scheduler->remap) {
133 return scheduler->remap(vm);
138 int v3_scheduler_notify_dvfs(struct v3_vm_info *vm) {
139 if (scheduler->dvfs) {
140 return scheduler->dvfs(vm);
145 void v3_schedule(struct guest_info *core) {
146 if (scheduler->schedule) {
147 scheduler->schedule(core);
151 void v3_yield(struct guest_info *core, int usec) {
152 if (scheduler->yield) {
153 scheduler->yield(core, usec);
158 int host_sched_vm_init(struct v3_vm_info *vm)
161 PrintDebug(vm, VCORE_NONE,"Sched. host_sched_init\n");
163 char * schedule_hz_str = v3_cfg_val(vm->cfg_data->cfg, "schedule_hz");
164 uint32_t sched_hz = 100;
167 if (schedule_hz_str) {
168 sched_hz = atoi(schedule_hz_str);
171 PrintDebug(vm, VCORE_NONE,"CPU_KHZ = %d, schedule_freq=%p\n", V3_CPU_KHZ(),
172 (void *)(addr_t)sched_hz);
174 uint64_t yield_cycle_period = (V3_CPU_KHZ() * 1000) / sched_hz;
175 vm->sched_priv_data = (void *)yield_cycle_period;
180 int host_sched_core_init(struct guest_info *core)
182 PrintDebug(core->vm_info, core,"Sched. host_sched_core_init\n");
184 uint64_t t = v3_get_host_time(&core->time_state);
185 core->sched_priv_data = (void *)t;
190 void host_sched_schedule(struct guest_info *core)
193 cur_cycle = v3_get_host_time(&core->time_state);
195 if (cur_cycle > ( (uint64_t)core->sched_priv_data + (uint64_t)core->vm_info->sched_priv_data)) {
199 uint64_t yield_start_cycle = (uint64_t) core->sched_priv_data;
200 yield_start_cycle += (uint64_t)core->vm_info->sched_priv_data;
201 core->sched_priv_data = (void *)yield_start_cycle;
207 * unconditional cpu yield
208 * if the yielding thread is a guest context, the guest quantum is reset on resumption
209 * Non guest context threads should call this function with a NULL argument
211 * usec <0 => the non-timed yield is used
212 * usec >=0 => the timed yield is used, which also usually implies interruptible
214 void host_sched_yield(struct guest_info * core, int usec) {
215 uint64_t yield_start_cycle;
221 yield_start_cycle = (uint64_t) core->sched_priv_data
222 + (uint64_t)core->vm_info->sched_priv_data;
223 core->sched_priv_data = (void *)yield_start_cycle;
227 int host_sched_admit(struct v3_vm_info *vm){
231 static struct vm_scheduler_impl host_sched_impl = {
235 .vm_init = host_sched_vm_init,
237 .core_init = host_sched_core_init,
239 .schedule = host_sched_schedule,
240 .yield = host_sched_yield,
241 .admit = host_sched_admit,
246 static int create_host_scheduler()
248 v3_register_scheduler(&host_sched_impl);