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