vmm: Collect and report scheduler stats
[akaros.git] / user / vmm / include / vmm / sched.h
1 /* Copyright (c) 2016 Google Inc.
2  * Barret Rhoden <brho@cs.berkeley.edu>
3  * See LICENSE for details.
4  *
5  * 2LS for virtual machines */
6
7 #pragma once
8
9 #include <parlib/uthread.h>
10 #include <sys/queue.h>
11
12 __BEGIN_DECLS
13
14 /* Three types of threads.  Guests are actual guest VMs.  Controllers are
15  * threads that are paired to guests and handles their exits.  Guests and
16  * controllers are 1:1 (via *buddy).  Task threads are for the VMM itself, such
17  * as a console thread. */
18
19 #define VMM_THREAD_GUEST                1
20 #define VMM_THREAD_CTLR                 2
21 #define VMM_THREAD_TASK                 3
22
23 #define VMM_THR_STACKSIZE               (4 * PGSIZE)
24
25 struct guest_thread;
26 struct ctlr_thread;
27 struct task_thread;
28
29 struct guest_thread {
30         struct uthread                          uthread;
31         struct ctlr_thread                      *buddy;
32         unsigned int                            gpc_id;
33         bool                                            halt_exit;
34         uth_mutex_t                                     *halt_mtx;
35         uth_cond_var_t                          *halt_cv;
36         unsigned long                           nr_vmexits;
37         // TODO: work out a real ops strategy.
38         bool (*vmcall)(struct guest_thread *gth, struct vm_trapframe *);
39 };
40
41 struct ctlr_thread {
42         struct uthread                          uthread;
43         struct guest_thread                     *buddy;
44         size_t                                          stacksize;
45         void                                            *stacktop;
46 };
47
48 struct task_thread {
49         struct uthread                          uthread;
50         void                                            *(*func)(void *);
51         void                                            *arg;
52         size_t                                          stacksize;
53         void                                            *stacktop;
54 };
55
56 struct virtual_machine;                 /* in vmm/vmm.h */
57 struct vmm_thread {
58         union {
59                 struct guest_thread             guest;
60                 struct ctlr_thread              ctlr;
61                 struct task_thread              task;
62         };
63         int                                                     type;
64         TAILQ_ENTRY(vmm_thread)         tq_next;
65         struct virtual_machine          *vm;
66         /* Sched stats */
67         int                                                     prev_vcoreid;
68         unsigned long                           nr_runs;
69         unsigned long                           nr_resched;
70 };
71
72 TAILQ_HEAD(vmm_thread_tq, vmm_thread);
73
74 extern int vmm_sched_period_usec;
75
76 /* Initialize a VMM for a virtual machine, which the caller fills out, except
77  * for gths.  This will set **gths in the struct virtual machine.  Do not free()
78  * the array.
79  *
80  * Set the parlib control variables (e.g. parlib_wants_to_be_mcp) before calling
81  * this initializer.
82  *
83  * Returns 0 on success, -1 o/w. */
84 int vmm_init(struct virtual_machine *vm, int flags);
85 /* Starts a guest thread/core. */
86 void start_guest_thread(struct guest_thread *gth);
87 /* Start and run a task thread. */
88 struct task_thread *vmm_run_task(struct virtual_machine *vm,
89                                  void *(*func)(void *), void *arg);
90
91 int vthread_attr_init(struct virtual_machine *vm, int vmmflags);
92 int vthread_attr_kernel_init(struct virtual_machine *vm, int vmmflags);
93 int vthread_create(struct virtual_machine *vm, int guest, void *rip, void *arg);
94
95 __END_DECLS