blob: f8857c2da509eb50662dcb2e9286a4708badd8c2 [file] [log] [blame]
mbligh44394242009-06-15 21:31:32 +00001import os, time, re, logging
mbligh67a72f92009-01-13 19:31:08 +00002from autotest_lib.client.bin import test, utils
mbligh44394242009-06-15 21:31:32 +00003from autotest_lib.client.bin.net import net_utils
mbligh67a72f92009-01-13 19:31:08 +00004from autotest_lib.client.common_lib import error
mbligh9f857922008-06-05 16:19:07 +00005
mbligh44394242009-06-15 21:31:32 +00006MPSTAT_IX = 0
7NETPERF_IX = 1
mbligh56a91f02006-09-14 17:55:19 +00008
9class netperf2(test.test):
mbligh44394242009-06-15 21:31:32 +000010 version = 3
mbligh56a91f02006-09-14 17:55:19 +000011
mbligha5630a52008-09-03 22:09:50 +000012 # ftp://ftp.netperf.org/netperf/netperf-2.4.4.tar.gz
13 def setup(self, tarball = 'netperf-2.4.4.tar.gz'):
mbligh44394242009-06-15 21:31:32 +000014 self.job.require_gcc()
mbligh8b352852008-06-07 01:07:08 +000015 tarball = utils.unmap_url(self.bindir, tarball, self.tmpdir)
mbligh53da18e2009-01-05 21:13:26 +000016 utils.extract_tarball_to_dir(tarball, self.srcdir)
jadmanski0afbb632008-06-06 21:10:57 +000017 os.chdir(self.srcdir)
mbligh56a91f02006-09-14 17:55:19 +000018
mbligh44394242009-06-15 21:31:32 +000019 utils.system('patch -p0 < ../wait_before_data.patch')
mblighc21cb462008-10-22 03:34:40 +000020 # Fixing up a compile issue under newer systems that have
21 # CPU_SET_S defined on /usr/include/sched.h, backported from
22 # upstream svn trunk
mbligh1eb6e9e2009-03-11 17:14:45 +000023 utils.system('patch -p0 < ../fix_netperf_build.patch')
jadmanski0afbb632008-06-06 21:10:57 +000024 utils.system('./configure')
25 utils.system('make')
mbligh44394242009-06-15 21:31:32 +000026 utils.system('sync')
mbligh54e56842006-09-23 06:02:54 +000027
28
jadmanski0afbb632008-06-06 21:10:57 +000029 def initialize(self):
mbligh44394242009-06-15 21:31:32 +000030 self.server_prog = '%s&' % os.path.join(self.srcdir, 'src/netserver')
31 self.client_prog = '%s' % os.path.join(self.srcdir, 'src/netperf')
32 self.valid_tests = ['TCP_STREAM', 'TCP_MAERTS', 'TCP_RR', 'TCP_CRR',
33 'TCP_SENDFILE', 'UDP_STREAM', 'UDP_RR']
mbligha5630a52008-09-03 22:09:50 +000034 self.results = []
mbligh865ee822008-10-03 16:03:34 +000035 self.actual_times = []
mbligh44394242009-06-15 21:31:32 +000036 self.netif = ''
37 self.network = net_utils.network()
38 self.network_utils = net_utils.network_utils()
mbligh56a91f02006-09-14 17:55:19 +000039
mbligh6f5bbce2007-08-10 19:18:37 +000040
mbligh44394242009-06-15 21:31:32 +000041 def run_once(self, server_ip, client_ip, role, test = 'TCP_STREAM',
42 test_time = 15, stream_list = [1], test_specific_args = '',
43 cpu_affinity = '', dev = '', bidi = False, wait_time = 5):
44 """
45 server_ip: IP address of host running netserver
46 client_ip: IP address of host running netperf client(s)
47 role: 'client' or 'server'
48 test: one of TCP_STREAM, TCP_MEARTS, TCP_RR, TCP_CRR, TCP_SENDFILE,
49 UDP_STREAM or UDP_RR
50 test_time: time to run the test for in seconds
51 stream_list: list of number of netperf streams to launch
52 test_specific_args: Optional test specific args. For example to set
53 the request,response size for RR tests to 200,100, set it
54 to: '-- -r 200,100'. Or, to set the send buffer size of STREAM
55 tests to 200, set it to: '-- -m 200'
56 cpu_affinity: netperf/netserver processes will get taskset to the
57 cpu_affinity. cpu_affinity is specified as a bitmask in hex
58 without the leading 0x. For example, to run on CPUs 0 & 5,
59 cpu_affinity needs to be '21'
60 dev: device on which to run traffic on. For example, to run on
61 inteface eth1, set it to 'eth1'.
62 bidi: bi-directional traffic. This is supported for TCP_STREAM
63 test only. The RR & CRR tests are bi-directional by nature.
64 wait_time: Time to wait after establishing data/control connections
65 but before sending data traffic.
66 """
mbligha5630a52008-09-03 22:09:50 +000067 if test not in self.valid_tests:
68 raise error.TestError('invalid test specified')
69 self.role = role
mbligh52fa6922008-09-05 20:37:08 +000070 self.test = test
mbligh865ee822008-10-03 16:03:34 +000071 self.test_time = test_time
mbligh44394242009-06-15 21:31:32 +000072 self.wait_time = wait_time
mbligh52fa6922008-09-05 20:37:08 +000073 self.stream_list = stream_list
mbligh44394242009-06-15 21:31:32 +000074 self.bidi = bidi
mbligha5630a52008-09-03 22:09:50 +000075
jadmanski0afbb632008-06-06 21:10:57 +000076 server_tag = server_ip + '#netperf-server'
77 client_tag = client_ip + '#netperf-client'
78 all = [server_tag, client_tag]
mbligha5630a52008-09-03 22:09:50 +000079
mbligh44394242009-06-15 21:31:32 +000080 # If a specific device has been requested, configure it.
81 if dev:
82 timeout = 60
83 if role == 'server':
84 self.configure_interface(dev, server_ip)
85 self.ping(client_ip, timeout)
86 else:
87 self.configure_interface(dev, client_ip)
88 self.ping(server_ip, timeout)
89
mbligha5630a52008-09-03 22:09:50 +000090 for num_streams in stream_list:
91 if role == 'server':
mbligh44394242009-06-15 21:31:32 +000092 self.server_start(cpu_affinity)
mbligha5630a52008-09-03 22:09:50 +000093 try:
mbligh15b8a262008-09-16 16:50:49 +000094 # Wait up to ten minutes for the client to reach this
mbligh52fa6922008-09-05 20:37:08 +000095 # point.
mbligh15b8a262008-09-16 16:50:49 +000096 self.job.barrier(server_tag, 'start_%d' % num_streams,
mbligh44394242009-06-15 21:31:32 +000097 600).rendezvous(*all)
mbligh15b8a262008-09-16 16:50:49 +000098 # Wait up to test_time + 5 minutes for the test to
mbligh52fa6922008-09-05 20:37:08 +000099 # complete
mbligh15b8a262008-09-16 16:50:49 +0000100 self.job.barrier(server_tag, 'stop_%d' % num_streams,
mbligh44394242009-06-15 21:31:32 +0000101 test_time+300).rendezvous(*all)
mbligha5630a52008-09-03 22:09:50 +0000102 finally:
103 self.server_stop()
104
105 elif role == 'client':
mbligh15b8a262008-09-16 16:50:49 +0000106 # Wait up to ten minutes for the server to start
107 self.job.barrier(client_tag, 'start_%d' % num_streams,
mbligh44394242009-06-15 21:31:32 +0000108 600).rendezvous(*all)
109 self.client(server_ip, test, test_time, num_streams,
110 test_specific_args, cpu_affinity)
mbligh15b8a262008-09-16 16:50:49 +0000111 # Wait up to 5 minutes for the server to also reach this point
112 self.job.barrier(client_tag, 'stop_%d' % num_streams,
mbligh44394242009-06-15 21:31:32 +0000113 300).rendezvous(*all)
mbligha5630a52008-09-03 22:09:50 +0000114 else:
115 raise error.TestError('invalid role specified')
mbligh54e56842006-09-23 06:02:54 +0000116
mbligh44394242009-06-15 21:31:32 +0000117 self.restore_interface()
mbligh56a91f02006-09-14 17:55:19 +0000118
mbligh44394242009-06-15 21:31:32 +0000119
120 def configure_interface(self, dev, ip_addr):
121 self.netif = net_utils.netif(dev)
122 self.netif.up()
123 if self.netif.get_ipaddr() != ip_addr:
124 self.netif.set_ipaddr(ip_addr)
125
126
127 def restore_interface(self):
128 if self.netif:
129 self.netif.restore()
130
131
132 def server_start(self, cpu_affinity):
mbligh78be24a2008-06-13 21:40:08 +0000133 utils.system('killall netserver', ignore_status=True)
mbligh44394242009-06-15 21:31:32 +0000134 cmd = self.server_prog
135 if cpu_affinity:
136 cmd = 'taskset %s %s' % (cpu_affinity, cmd)
137
138 self.results.append(utils.system_output(cmd, retain_output=True))
mbligh56a91f02006-09-14 17:55:19 +0000139
mbligh54e56842006-09-23 06:02:54 +0000140
jadmanski0afbb632008-06-06 21:10:57 +0000141 def server_stop(self):
mbligha5630a52008-09-03 22:09:50 +0000142 utils.system('killall netserver', ignore_status=True)
mbligh56a91f02006-09-14 17:55:19 +0000143
mbligh54e56842006-09-23 06:02:54 +0000144
mbligh44394242009-06-15 21:31:32 +0000145 def client(self, server_ip, test, test_time, num_streams,
146 test_specific_args, cpu_affinity):
147 args = '-H %s -t %s -l %d' % (server_ip, test, test_time)
148 if self.wait_time:
149 args += ' -s %d ' % self.wait_time
150
151 # Append the test specific arguments.
152 if test_specific_args:
153 args += ' ' + test_specific_args
154
155 cmd = '%s %s' % (self.client_prog, args)
156
157 if cpu_affinity:
158 cmd = 'taskset %s %s' % (cpu_affinity, cmd)
mbligh56a91f02006-09-14 17:55:19 +0000159
mbligha5630a52008-09-03 22:09:50 +0000160 try:
mbligh44394242009-06-15 21:31:32 +0000161 cmds = []
162
163 # Get 5 mpstat samples. Since tests with large number of streams
164 # take a long time to start up all the streams, we'll toss out the
165 # first and last sample when recording results
166 interval = max(1, test_time / 5)
167 cmds.append('sleep %d && mpstat -P ALL %s 5' % (self.wait_time,
168 interval))
169
170 # Add the netperf commands
171 for i in xrange(num_streams):
172 cmds.append(cmd)
173 if self.bidi and test == 'TCP_STREAM':
174 cmds.append(cmd.replace('TCP_STREAM', 'TCP_MAERTS'))
175
mbligh865ee822008-10-03 16:03:34 +0000176 t0 = time.time()
mbligh44394242009-06-15 21:31:32 +0000177 # Launch all commands in parallel
178 out = utils.run_parallel(cmds, timeout=test_time + 500,
179 ignore_status=True)
mbligh865ee822008-10-03 16:03:34 +0000180 t1 = time.time()
181
mbligh44394242009-06-15 21:31:32 +0000182 self.results.append(out)
183 self.actual_times.append(t1 - t0 - self.wait_time)
184 # Log test output
185 logging.info(out)
mbligh865ee822008-10-03 16:03:34 +0000186
mbligha5630a52008-09-03 22:09:50 +0000187 except error.CmdError, e:
188 """ Catch errors due to timeout, but raise others
189 The actual error string is:
190 "Command did not complete within %d seconds"
191 called in function join_bg_job in the file common_lib/utils.py
mblighe8fa3af2006-09-28 23:14:56 +0000192
mbligha5630a52008-09-03 22:09:50 +0000193 Looking for 'within' is probably not the best way to do this but
194 works for now"""
195
mbligh52fa6922008-09-05 20:37:08 +0000196 if ('within' in e.additional_text
197 or 'non-zero' in e.additional_text):
lmr4c607f22009-06-02 11:50:38 +0000198 logging.debug(e.additional_text)
mbligh44394242009-06-15 21:31:32 +0000199 self.results.append(None)
mbligh865ee822008-10-03 16:03:34 +0000200 self.actual_times.append(1)
mbligha5630a52008-09-03 22:09:50 +0000201 else:
202 raise
203
204
205 def postprocess(self):
mbligh52fa6922008-09-05 20:37:08 +0000206 if self.role == 'client':
mbligh44394242009-06-15 21:31:32 +0000207 # if profilers are enabled, the test gets runs twice
208 if (len(self.stream_list) != len(self.results) and
209 2*len(self.stream_list) != len(self.results)):
mbligh52fa6922008-09-05 20:37:08 +0000210 raise error.TestError('Mismatched number of results')
211
212 function = None
213 keys = None
214
215 # Each of the functions return tuples in which the keys define
216 # what that item in the tuple represents
mbligh44394242009-06-15 21:31:32 +0000217 if self.test in ['TCP_STREAM', 'TCP_MAERTS', 'TCP_SENDFILE']:
mbligh52fa6922008-09-05 20:37:08 +0000218 function = self.process_tcp_stream
219 keys = ('Throughput',)
220 elif self.test == 'UDP_STREAM':
221 function = self.process_udp_stream
222 keys = ('Throughput', 'Errors')
223 elif self.test in ['TCP_RR', 'TCP_CRR', 'UDP_RR']:
224 function = self.process_request_response
225 keys = ('Transfer_Rate',)
226 else:
227 raise error.TestError('Unhandled test')
228
mbligh52fa6922008-09-05 20:37:08 +0000229 for i, streams in enumerate(self.stream_list):
230 attr = {'stream_count':streams}
231 keyval = {}
232 temp_vals = []
mbligh52fa6922008-09-05 20:37:08 +0000233
234 # Short circuit to handle errors due to client timeouts
mbligh44394242009-06-15 21:31:32 +0000235 if not self.results[i]:
mbligh52fa6922008-09-05 20:37:08 +0000236 self.write_iteration_keyval(attr, keyval)
237 continue
238
mbligh44394242009-06-15 21:31:32 +0000239 # Collect output of netperf sessions
240 failed_streams_count = 0
241 for result in self.results[i][NETPERF_IX:]:
242 if result.exit_status:
243 failed_streams_count += 1
244 else:
245 temp_vals.append(function(result.stdout))
246
247 keyval['Failed_streams_count'] = failed_streams_count
248
249 # Process mpstat output
250 mpstat_out = self.results[i][MPSTAT_IX].stdout
251 cpu_stats = self.network_utils.process_mpstat(mpstat_out, 5)
252 keyval['CPU_C'] = 100 - cpu_stats['idle']
253 keyval['CPU_C_SYS'] = cpu_stats['sys']
254 keyval['CPU_C_HI'] = cpu_stats['irq']
255 keyval['CPU_C_SI'] = cpu_stats['soft']
256 keyval['INTRS_C'] = cpu_stats['intr/s']
257
258 actual_time = self.actual_times[i]
259 keyval['actual_time'] = actual_time
260 logging.info('actual_time: %f', actual_time)
mbligh52fa6922008-09-05 20:37:08 +0000261
mbligh6d858fa2008-09-19 21:19:03 +0000262 # Compute the sum of elements returned from function which
mbligh52fa6922008-09-05 20:37:08 +0000263 # represent the string contained in keys
264 for j, key in enumerate(keys):
265 vals = [x[j] for x in temp_vals]
mbligh865ee822008-10-03 16:03:34 +0000266 # scale result by the actual time taken
mbligh44394242009-06-15 21:31:32 +0000267 keyval[key] = sum(vals)
mbligh865ee822008-10-03 16:03:34 +0000268
269 # record 'Efficiency' as perf/CPU
mbligh44394242009-06-15 21:31:32 +0000270 if keyval['CPU_C'] != 0:
271 keyval['Efficieny_C'] = keyval[keys[0]]/keyval['CPU_C']
272 else:
273 keyval['Efficieny_C'] = keyval[keys[0]]
mbligh52fa6922008-09-05 20:37:08 +0000274
275 self.write_iteration_keyval(attr, keyval)
276
277
278 def process_tcp_stream(self, output):
mbligh44394242009-06-15 21:31:32 +0000279 """Parses the following (works for both TCP_STREAM, TCP_MAERTS and
280 TCP_SENDFILE) and returns a singleton containing throughput.
mbligh52fa6922008-09-05 20:37:08 +0000281
mbligh44394242009-06-15 21:31:32 +0000282 TCP STREAM TEST from 0.0.0.0 (0.0.0.0) port 0 AF_INET to foo.bar.com \
283 (10.10.10.3) port 0 AF_INET
mbligh52fa6922008-09-05 20:37:08 +0000284 Recv Send Send
285 Socket Socket Message Elapsed
286 Size Size Size Time Throughput
287 bytes bytes bytes secs. 10^6bits/sec
288
289 87380 16384 16384 2.00 941.28
290 """
291
292 return float(output.splitlines()[6].split()[4]),
293
294
295 def process_udp_stream(self, output):
296 """Parses the following and returns a touple containing throughput
297 and the number of errors.
298
mbligh44394242009-06-15 21:31:32 +0000299 UDP UNIDIRECTIONAL SEND TEST from 0.0.0.0 (0.0.0.0) port 0 AF_INET \
300 to foo.bar.com (10.10.10.3) port 0 AF_INET
mbligh52fa6922008-09-05 20:37:08 +0000301 Socket Message Elapsed Messages
302 Size Size Time Okay Errors Throughput
303 bytes bytes secs # # 10^6bits/sec
304
305 129024 65507 2.00 3673 0 961.87
306 131072 2.00 3673 961.87
307 """
308
309 line_tokens = output.splitlines()[5].split()
310 return float(line_tokens[5]), int(line_tokens[4])
311
312
313 def process_request_response(self, output):
314 """Parses the following which works for both rr (TCP and UDP) and crr
315 tests and returns a singleton containing transfer rate.
316
mbligh44394242009-06-15 21:31:32 +0000317 TCP REQUEST/RESPONSE TEST from 0.0.0.0 (0.0.0.0) port 0 AF_INET \
318 to foo.bar.com (10.10.10.3) port 0 AF_INET
mbligh52fa6922008-09-05 20:37:08 +0000319 Local /Remote
320 Socket Size Request Resp. Elapsed Trans.
321 Send Recv Size Size Time Rate
322 bytes Bytes bytes bytes secs. per sec
323
324 16384 87380 1 1 2.00 14118.53
325 16384 87380
326 """
327
328 return float(output.splitlines()[6].split()[5]),
mbligh44394242009-06-15 21:31:32 +0000329
330
331 def ping(self, ip, timeout):
332 curr_time = time.time()
333 end_time = curr_time + timeout
334 while curr_time < end_time:
335 if not os.system('ping -c 1 ' + ip):
336 # Ping succeeded
337 return
338 # Ping failed. Lets sleep a bit and try again.
339 time.sleep(5)
340 curr_time = time.time()
341
342 return