cbb9663f955f66c1d8b7c7b229b85b1264624944
[tests.git] / qaapi / qa / tests / node / node_cpu_sched.py
1 #!/usr/bin/python
2
3 """
4 This file contains a stand-alone CPU scheduler test to be run on a node.
5 It still needs to be integrated into the testing framework.
6 """
7
8 import sys
9 import commands
10 import re
11 from threading import Thread
12
13 class CpuTest:
14     def __init__(self, ctx, cpu, resv, share, min, max, desc):
15         self.ctx = ctx
16         self.cpu = cpu
17         self.resv = resv
18         self.share = share
19         self.min = min
20         self.max = max
21         self.desc = desc
22
23
24 class Spinner(Thread):
25     def __init__(self, test):
26         Thread.__init__(self)
27
28         cmd = '/usr/bin/time ./busyloop.py 15'
29         if test.share:
30             flags = '--idle-time'
31         else:
32             flags = ''
33
34         self.cmd = 'taskset -c ' + str(test.cpu) \
35             + ' vcontext --create --xid ' + str(test.ctx) + ' --disconnect ' \
36             + '-- vsched --fill-rate ' + str(test.resv) + ' --interval 100 ' \
37             + '--fill-rate2 ' + str(test.share) + ' --interval2 1000 ' \
38             + '--tokens 100 --tokens-min 50 --tokens-max 100 --force '\
39             + flags + ' -- vattribute --flag sched_hard -- ' + cmd
40
41         self.test = test
42
43     def run(self):
44         self.output = commands.getoutput( self.cmd )
45
46     def passed(self):
47         match = re.search('elapsed (\d+)%CPU', self.output)
48         if match:
49             self.pct = int(match.group(1))
50         else:
51             print "Error parsing output: cannot get CPU%"
52             self.pct = 0
53
54         return ( self.pct >= self.test.min and self.pct <= self.test.max )
55
56
57 def run_test(testlist):
58     failures = 0
59
60     for test in testlist:
61         test.thread = Spinner(test)
62         test.thread.start()
63
64     for test in testlist:
65         test.thread.join()
66
67     for test in testlist:
68         if test.thread.passed():
69             print "[PASSED] (" + str(test.thread.pct) + "%)\t", test.desc
70         else:
71             print "[FAILED] (" + str(test.thread.pct) + "%)\t", test.desc
72             failures += 1
73
74     return failures
75
76 ### Test 1: test share scheduling of a single task (should get 100%)
77 def test_1():
78     test = CpuTest(ctx=600, cpu=0, resv=0, share=1, min=99, max=100, 
79                    desc = "Test 1: single ctx, one share")
80
81     return run_test([ test ])
82
83 ### Test 2: test hard rate-limiting to 25% (should get <= 25%)
84 def test_2():
85     test = CpuTest(ctx=600, cpu=0, resv=25, share=0, min=24, max=25, 
86                    desc = "Test 2: single ctx, 25% resv")
87
88     return run_test([ test ])
89
90 ### Test 3: test hard & share scheduling of a single task (should get 100%)
91 def test_3():
92     test = CpuTest(ctx=600, cpu=0, resv=25, share=1, min=99, max=100, 
93                    desc = "Test 3: single ctx, 25% resv, one share")
94
95     return run_test([ test ])
96
97 ### Test 4: test relative share scheduling of several tasks
98 def test_4():
99     test1 = CpuTest(ctx=600, cpu=0, resv=0, share=1, min=15, max=17, 
100                    desc = "Test 4: ctx 1, one share")
101     test2 = CpuTest(ctx=601, cpu=0, resv=0, share=2, min=32, max=34, 
102                    desc = "        ctx 2, two shares")
103     test3 = CpuTest(ctx=602, cpu=0, resv=0, share=3, min=49, max=51, 
104                    desc = "        ctx 3, three shares")
105
106     return run_test([ test1, test2, test3 ])
107
108 ### Test 5: test hard rate limit and shares
109 def test_5():
110     test1 = CpuTest(ctx=600, cpu=0, resv=0, share=1, min=14, max=16, 
111                    desc = "Test 5: ctx 1, one share")
112     test2 = CpuTest(ctx=601, cpu=0, resv=0, share=2, min=29, max=33, 
113                    desc = "        ctx 2, two shares")
114     test3 = CpuTest(ctx=602, cpu=0, resv=0, share=3, min=44, max=47, 
115                    desc = "        ctx 3, three shares")
116     test4 = CpuTest(ctx=603, cpu=0, resv=10, share=0, min=9, max=10, 
117                    desc = "        ctx 4, 10% resv")
118
119     return run_test([ test1, test2, test3, test4 ])
120
121 ### Test 6: test guarantee and shares
122 def test_6():
123     test1 = CpuTest(ctx=600, cpu=0, resv=0, share=1, min=9, max=11, 
124                    desc = "Test 6: ctx 1, one share")
125     test2 = CpuTest(ctx=601, cpu=0, resv=0, share=2, min=19, max=21, 
126                    desc = "        ctx 2, two shares")
127     test3 = CpuTest(ctx=602, cpu=0, resv=0, share=3, min=29, max=31, 
128                    desc = "        ctx 3, three shares")
129     test4 = CpuTest(ctx=603, cpu=0, resv=30, share=1, min=38, max=41, 
130                    desc = "        ctx 4, 30% resv, one share")
131
132     return run_test([ test1, test2, test3, test4 ])
133
134 ### Now, run all tests on two processors
135 ### Test 7: SMP active (both tasks should get 100% on an SMP)
136 def test_7():
137     test1 = CpuTest(ctx=600, cpu=0, resv=0, share=1, min=99, max=100, 
138                    desc = "Test 7: ctx 1, processor 1, one share")
139     test2 = CpuTest(ctx=601, cpu=1, resv=0, share=1, min=99, max=100, 
140                    desc = "        ctx 2, processor 2, one share")
141
142     return run_test([ test1, test2 ])
143
144 ### Test 8: SMP, test hard rate-limiting to 25% (should get <= 25%)
145 def test_8():
146     test1 = CpuTest(ctx=600, cpu=0, resv=25, share=0, min=24, max=25, 
147                    desc = "Test 8: ctx 1, proc 1, 25% resv")
148     test2 = CpuTest(ctx=601, cpu=1, resv=25, share=0, min=24, max=25, 
149                    desc = "        ctx 2, proc 2, 25% resv")
150
151     return run_test([ test1, test2 ])
152
153 ### Test 9: SMP, test hard & share scheduling of a single task 
154 def test_9():
155     test1 = CpuTest(ctx=600, cpu=0, resv=25, share=1, min=99, max=100, 
156                    desc = "Test 9: ctx 1, proc 1, 25% resv, one share")
157     test2 = CpuTest(ctx=601, cpu=1, resv=25, share=1, min=99, max=100, 
158                    desc = "        ctx 2, proc 2, 25% resv, one share")
159
160     return run_test([ test1, test2 ])
161
162 ### Test 10: SMP, test relative share scheduling of several tasks
163 def test_10():
164     test1 = CpuTest(ctx=600, cpu=0, resv=0, share=1, min=15, max=17, 
165                    desc = "Test 10: ctx 1, proc 1, one share")
166     test2 = CpuTest(ctx=601, cpu=0, resv=0, share=2, min=32, max=34, 
167                    desc = "         ctx 2, proc 1, two shares")
168     test3 = CpuTest(ctx=602, cpu=0, resv=0, share=3, min=49, max=51, 
169                    desc = "         ctx 3, proc 1, three shares")
170     test4 = CpuTest(ctx=603, cpu=1, resv=0, share=1, min=15, max=17, 
171                    desc = "         ctx 4, proc 2, one share")
172     test5 = CpuTest(ctx=604, cpu=1, resv=0, share=2, min=32, max=34, 
173                    desc = "         ctx 5, proc 2, two shares")
174     test6 = CpuTest(ctx=605, cpu=1, resv=0, share=3, min=49, max=51, 
175                    desc = "         ctx 6, proc 2, three shares")
176
177     return run_test([ test1, test2, test3, test4, test5, test6 ])
178
179 ### Test 11: SMP, test hard rate limit and shares
180 def test_11():
181     test1 = CpuTest(ctx=600, cpu=0, resv=0, share=1, min=14, max=16, 
182                    desc = "Test 11: ctx 1, proc 1, one share")
183     test2 = CpuTest(ctx=601, cpu=0, resv=0, share=2, min=29, max=33, 
184                    desc = "         ctx 2, proc 1, two shares")
185     test3 = CpuTest(ctx=602, cpu=0, resv=0, share=3, min=44, max=47, 
186                    desc = "         ctx 3, proc 1, three shares")
187     test4 = CpuTest(ctx=603, cpu=0, resv=10, share=0, min=9, max=10, 
188                    desc = "         ctx 4, proc 1, 10% resv")
189     test5 = CpuTest(ctx=604, cpu=1, resv=0, share=1, min=14, max=16, 
190                    desc = "         ctx 5, proc 2, one share")
191     test6 = CpuTest(ctx=605, cpu=1, resv=0, share=2, min=29, max=33, 
192                    desc = "         ctx 6, proc 2, two shares")
193     test7 = CpuTest(ctx=606, cpu=1, resv=0, share=3, min=44, max=47, 
194                    desc = "         ctx 7, proc 2, three shares")
195     test8 = CpuTest(ctx=607, cpu=1, resv=10, share=0, min=9, max=10, 
196                    desc = "         ctx 8, proc 2, 10% resv")
197
198     return run_test([ test1, test2, test3, test4, test5, test6, test7, test8 ])
199
200 ### Test 12: SMP, test guarantee and shares
201 def test_12():
202     test1 = CpuTest(ctx=600, cpu=0, resv=0, share=1, min=9, max=11, 
203                    desc = "Test 12: ctx 1, proc 1, one share")
204     test2 = CpuTest(ctx=601, cpu=0, resv=0, share=2, min=19, max=21, 
205                    desc = "         ctx 2, proc 1, two shares")
206     test3 = CpuTest(ctx=602, cpu=0, resv=0, share=3, min=29, max=31, 
207                    desc = "         ctx 3, proc 1, three shares")
208     test4 = CpuTest(ctx=603, cpu=0, resv=30, share=1, min=38, max=41, 
209                    desc = "         ctx 4, proc 1, 30% resv, one share")
210     test5 = CpuTest(ctx=604, cpu=1, resv=0, share=1, min=9, max=11, 
211                    desc = "         ctx 5, proc 2, one share")
212     test6 = CpuTest(ctx=605, cpu=1, resv=0, share=2, min=19, max=21, 
213                    desc = "         ctx 6, proc 2, two shares")
214     test7 = CpuTest(ctx=606, cpu=1, resv=0, share=3, min=29, max=31, 
215                    desc = "         ctx 7, proc 2, three shares")
216     test8 = CpuTest(ctx=607, cpu=1, resv=30, share=1, min=38, max=41, 
217                    desc = "         ctx 8, proc 2, 30% resv, one share")
218
219     return run_test([ test1, test2, test3, test4, test5, test6, test7, test8 ])
220
221
222 def main():
223     failures = 0
224
225     failures += test_1()
226     failures += test_2()
227     failures += test_3()
228     failures += test_4()
229     failures += test_5()
230     failures += test_6()
231     failures += test_7()
232     failures += test_8()
233     failures += test_9()
234     failures += test_10()
235     failures += test_11()
236     failures += test_12()
237
238     return failures
239
240 if __name__ == "__main__":
241     sys.exit(main())