2 * linux/kernel/vserver/sched.c
4 * Virtual Server: Scheduler Support
6 * Copyright (C) 2004 Herbert Pƶtzl
8 * V0.01 adapted Sam Vilains version to 2.6.3
9 * V0.02 removed legacy interface
13 #include <linux/config.h>
14 #include <linux/sched.h>
15 #include <linux/vs_base.h>
16 #include <linux/vs_context.h>
17 #include <linux/vserver/context.h>
18 #include <linux/vserver/sched.h>
20 #include <asm/errno.h>
21 #include <asm/uaccess.h>
25 * recalculate the context's scheduling tokens
27 * ret > 0 : number of tokens available
28 * ret = 0 : context is paused
29 * ret < 0 : number of jiffies until new tokens arrive
32 int vx_tokens_recalc(struct vx_info *vxi)
34 long delta, tokens = 0;
36 if (__vx_flags(vxi->vx_flags, VXF_SCHED_PAUSE, 0))
40 delta = jiffies - vxi->sched.jiffies;
42 if (delta >= vxi->sched.interval) {
43 /* lockdown scheduler info */
44 spin_lock(&vxi->sched.tokens_lock);
46 /* calc integral token part */
47 delta = jiffies - vxi->sched.jiffies;
48 tokens = delta / vxi->sched.interval;
49 delta = tokens * vxi->sched.interval;
50 tokens *= vxi->sched.fill_rate;
52 atomic_add(tokens, &vxi->sched.tokens);
53 vxi->sched.jiffies += delta;
54 tokens = atomic_read(&vxi->sched.tokens);
56 if (tokens > vxi->sched.tokens_max) {
57 tokens = vxi->sched.tokens_max;
58 atomic_set(&vxi->sched.tokens, tokens);
60 spin_unlock(&vxi->sched.tokens_lock);
63 if ((tokens = vx_tokens_avail(vxi)) < vxi->sched.tokens_min) {
64 /* enough tokens will be available in */
65 if (vxi->sched.tokens_min == 0)
66 return delta - vxi->sched.interval;
67 return delta - vxi->sched.interval *
68 vxi->sched.tokens_min / vxi->sched.fill_rate;
71 /* we have some tokens left */
76 * effective_prio - return the priority that is based on the static
77 * priority but is modified by bonuses/penalties.
79 * We scale the actual sleep average [0 .... MAX_SLEEP_AVG]
80 * into a -4 ... 0 ... +4 bonus/penalty range.
82 * Additionally, we scale another amount based on the number of
83 * CPU tokens currently held by the context, if the process is
84 * part of a context (and the appropriate SCHED flag is set).
85 * This ranges from -5 ... 0 ... +15, quadratically.
87 * So, the total bonus is -9 .. 0 .. +19
88 * We use ~50% of the full 0...39 priority range so that:
90 * 1) nice +19 interactive tasks do not preempt nice 0 CPU hogs.
91 * 2) nice -20 CPU hogs do not get preempted by nice 0 tasks.
92 * unless that context is far exceeding its CPU allocation.
94 * Both properties are important to certain workloads.
96 int effective_vavavoom(task_t *p, int max_prio)
98 struct vx_info *vxi = p->vx_info;
101 /* lots of tokens = lots of vavavoom
102 * no tokens = no vavavoom */
103 if ((vavavoom = atomic_read(&vxi->sched.tokens)) >= 0) {
104 max = vxi->sched.tokens_max;
105 vavavoom = max - vavavoom;
107 vavavoom = max_prio * VAVAVOOM_RATIO / 100
108 * (vavavoom*vavavoom - (max >> 2)) / max;
109 /* alternative, geometric mapping
110 vavavoom = -( MAX_USER_PRIO*VAVAVOOM_RATIO/100 * vavavoom
111 / vxi->sched.tokens_max -
112 MAX_USER_PRIO*VAVAVOOM_RATIO/100/2); */
115 /* vavavoom = ( MAX_USER_PRIO*VAVAVOOM_RATIO/100*tokens_left(p) -
116 MAX_USER_PRIO*VAVAVOOM_RATIO/100/2); */
122 int vc_set_sched(uint32_t xid, void __user *data)
124 struct vcmd_set_sched_v2 vc_data;
127 if (copy_from_user (&vc_data, data, sizeof(vc_data)))
130 vxi = locate_vx_info(xid);
134 spin_lock(&vxi->sched.tokens_lock);
136 if (vc_data.interval != SCHED_KEEP)
137 vxi->sched.interval = vc_data.interval;
138 if (vc_data.fill_rate != SCHED_KEEP)
139 vxi->sched.fill_rate = vc_data.fill_rate;
140 if (vc_data.tokens_min != SCHED_KEEP)
141 vxi->sched.tokens_min = vc_data.tokens_min;
142 if (vc_data.tokens_max != SCHED_KEEP)
143 vxi->sched.tokens_max = vc_data.tokens_max;
144 if (vc_data.tokens != SCHED_KEEP)
145 atomic_set(&vxi->sched.tokens, vc_data.tokens);
147 /* Sanity check the resultant values */
148 if (vxi->sched.fill_rate <= 0)
149 vxi->sched.fill_rate = 1;
150 if (vxi->sched.interval <= 0)
151 vxi->sched.interval = HZ;
152 if (vxi->sched.tokens_max == 0)
153 vxi->sched.tokens_max = 1;
154 if (atomic_read(&vxi->sched.tokens) > vxi->sched.tokens_max)
155 atomic_set(&vxi->sched.tokens, vxi->sched.tokens_max);
156 if (vxi->sched.tokens_min > vxi->sched.tokens_max)
157 vxi->sched.tokens_min = vxi->sched.tokens_max;
159 spin_unlock(&vxi->sched.tokens_lock);