3841639b4d37d8daa198711a59ac65584845f690
[util-vserver.git] / util-vserver / kernel / sched.h
1 #if     defined(__KERNEL__) && defined(_VX_INFO_DEF_)
2
3 #include <linux/spinlock.h>
4 #include <linux/jiffies.h>
5 #include <asm/param.h>
6 #include <asm/cpumask.h>
7
8 /* context sub struct */
9
10 struct _vx_sched {
11         spinlock_t tokens_lock; /* lock for this structure */
12
13         int fill_rate;          /* Fill rate: add X tokens... */
14         int interval;           /* Divisor:   per Y jiffies   */
15         int tokens;             /* number of CPU tokens in this context */
16         int tokens_min;         /* Limit:     minimum for unhold */
17         int tokens_max;         /* Limit:     no more than N tokens */
18         uint32_t jiffies;       /* add an integral multiple of Y to this */
19
20         uint64_t ticks;         /* token tick events */
21         cpumask_t cpus_allowed; /* cpu mask for context */
22 };
23
24 static inline void vx_info_init_sched(struct _vx_sched *sched)
25 {
26         /* scheduling; hard code starting values as constants */
27         sched->fill_rate        = 1;
28         sched->interval = 4;
29         sched->tokens   = HZ >> 2;
30         sched->tokens_min       = HZ >> 4;
31         sched->tokens_max       = HZ >> 1;
32         sched->jiffies          = jiffies;
33         sched->tokens_lock      = SPIN_LOCK_UNLOCKED;
34 }
35
36 static inline int vx_info_proc_sched(struct _vx_sched *sched, char *buffer)
37 {
38         return sprintf(buffer,
39                 "Ticks:\t%16lld\n"
40                 "Token:\t\t%8d\n"
41                 "FillRate:\t%8d\n"
42                 "Interval:\t%8d\n"              
43                 "TokensMin:\t%8d\n"
44                 "TokensMax:\t%8d\n"
45                 ,sched->ticks
46                 ,sched->tokens
47                 ,sched->fill_rate
48                 ,sched->interval
49                 ,sched->tokens_min
50                 ,sched->tokens_max
51                 );
52 }
53
54
55 #else   /* _VX_INFO_DEF_ */
56 #ifndef _VX_SCHED_H
57 #define _VX_SCHED_H
58
59 #include "switch.h"
60
61 /*  sched vserver commands */
62
63 #define VCMD_set_sched_v1       VC_CMD(SYSTEST, 1, 1)
64
65 struct  vcmd_set_sched_v1 {
66         int32_t fill_rate;
67         int32_t period;
68         int32_t fill_level;
69         int32_t bucket_size;
70 };
71
72 #define VCMD_set_sched          VC_CMD(SCHED, 1, 2)
73
74 struct  vcmd_set_sched_v2 {
75         int32_t fill_rate;
76         int32_t interval;
77         int32_t tokens;
78         int32_t tokens_min;
79         int32_t tokens_max;
80         uint64_t cpu_mask;
81 };
82
83 #define SCHED_KEEP              (-2)
84
85 #ifdef  __KERNEL__
86
87 extern int vc_set_sched_v1(uint32_t, void *);
88 extern int vc_set_sched(uint32_t, void *);
89
90
91 #define VAVAVOOM_RATIO          50
92
93 #include "context.h"
94
95
96 /* scheduling stuff */
97
98 int effective_vavavoom(struct task_struct *, int);
99
100 int vx_tokens_recalc(struct vx_info *);
101
102 /* update the token allocation for a process */
103 static inline int vx_tokens_avail(struct task_struct *tsk)
104 {
105         struct vx_info *vxi = tsk->vx_info;
106         int tokens;
107
108         spin_lock(&vxi->sched.tokens_lock);
109         tokens = vx_tokens_recalc(vxi);
110         spin_unlock(&vxi->sched.tokens_lock);
111         return tokens;
112 }
113
114 /* new stuff ;) */
115
116 static inline int vx_need_resched(struct task_struct *p, struct vx_info *vxi)
117 {
118         p->time_slice--;
119         if (vxi) {
120                 int tokens = 0;
121
122                 if (vxi->sched.tokens > 0) {
123                         spin_lock(&vxi->sched.tokens_lock);
124                         tokens = --vxi->sched.tokens;
125                         spin_unlock(&vxi->sched.tokens_lock);
126                 }
127                 return ((p->time_slice == 0) || (tokens == 0));
128         } else
129                 return (p->time_slice == 0);
130 }
131
132
133 #endif  /* __KERNEL__ */
134
135 #endif  /* _VX_SCHED_H */
136 #endif