46accbc37cdcadd846d63ef395522889b113ae95
[nepi.git] / src / nepi / resources / linux / udptest.py
1 #
2 #    NEPI, a framework to manage network experiments
3 #    Copyright (C) 2013 INRIA
4 #
5 #    This program is free software: you can redistribute it and/or modify
6 #    it under the terms of the GNU General Public License as published by
7 #    the Free Software Foundation, either version 3 of the License, or
8 #    (at your option) any later version.
9 #
10 #    This program is distributed in the hope that it will be useful,
11 #    but WITHOUT ANY WARRANTY; without even the implied warranty of
12 #    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 #    GNU General Public License for more details.
14 #
15 #    You should have received a copy of the GNU General Public License
16 #    along with this program.  If not, see <http://www.gnu.org/licenses/>.
17 #
18 # Author: Alina Quereilhac <alina.quereilhac@inria.fr>
19
20 from nepi.execution.attribute import Attribute, Flags, Types
21 from nepi.execution.resource import clsinit_copy, ResourceState, \
22         reschedule_delay 
23 from nepi.resources.linux.application import LinuxApplication
24 from nepi.util.timefuncs import tnow
25
26 import os
27
28 @clsinit_copy
29 class LinuxUdpTest(LinuxApplication):
30     """ Uses the hpcbench udptest tool to gather UDP measurements.
31     Measurements require two ends, a server and a client RM.
32
33     http://hpcbench.sourceforge.net/
34     """
35     _rtype = "LinuxUdpTest"
36
37     @classmethod
38     def _register_attributes(cls):
39         s = Attribute("s",
40             "Runs in server mode. ",
41             type = Types.Bool,
42             default = False,
43             flags = Flags.Design)
44
45         p = Attribute("p",
46             "Port to listen to in server mode, or to connect to in client mode. "
47             "Defaults to 5678. ",
48             type = Types.Integer,
49             flags = Flags.Design)
50
51         a = Attribute("a",
52             "Client option. Perform UDP Round Trip Time (latency) ",
53             type = Types.Bool,
54             flags = Flags.Design)
55
56         A = Attribute("A",
57             "Client option. "
58             "Message size for UDP RTT test. "
59             "UDP RTT (latency) test with specified message size.",
60             type = Types.Integer,
61             flags = Flags.Design)
62
63         b = Attribute("b",
64             "Client option. "
65             "Client UDP buffer size in bytes. Using system default "
66             "value if not defined.",
67             type = Types.Integer,
68             flags = Flags.Design)
69
70         B = Attribute("B",
71             "Client option. "
72             "Server UDP buffer size in bytes. The same as cleint's by default.",
73             type = Types.Integer,
74             flags = Flags.Design)
75
76         c = Attribute("c",
77             "Client option. "
78             "CPU log option. Tracing system info during the test. "
79             "Only available when output is defined. ",
80             type = Types.Bool,
81             flags = Flags.Design)
82
83         d = Attribute("d",
84             "Client option. "
85             "Data size of each read/write in bytes. The same as packet size "
86             "by default.",
87             type = Types.Integer,
88             flags = Flags.Design)
89
90         e = Attribute("e",
91             "Client option. "
92             "Exponential test (data size of each sending increasing from 1 "
93             "byte to packet size). ",
94             type = Types.Bool,
95             flags = Flags.Design)
96
97         g = Attribute("g",
98             "Client option. "
99             "UDP traffic generator (Keep sending data to a host). "
100             "Work without server's support.",
101             type = Types.Bool,
102             flags = Flags.Design)
103
104         target = Attribute("target",
105             "Client option. "
106             "Hostname or IP address of UDP server. Must be specified.",
107             flags = Flags.Design)
108
109         i = Attribute("i",
110             "Client option. "
111             "Bidirectional UDP throuhgput test. Default is unidirection "
112             "stream test. ",
113             type = Types.Bool,
114             flags = Flags.Design)
115
116         l = Attribute("l",
117             "Client option. "
118             "UDP datagram (packet) size in bytes ( < udp-buffer-szie ). "
119             "1460 by default.",
120             type = Types.Integer,
121             flags = Flags.Design)
122
123         m = Attribute("m",
124             "Client option. "
125             "Total message size in bytes. 1048576 by default.",
126             type = Types.Integer,
127             flags = Flags.Design)
128
129         o = Attribute("o",
130             "Client option. "
131             "Output file name. ",
132             flags = Flags.Design)
133
134         P = Attribute("P",
135             "Client option. "
136             "Write the plot file for gnuplot. Only enable when the output "
137             "is specified. ",
138             type = Types.Bool,
139             flags = Flags.Design)
140
141         q = Attribute("q",
142             "Client option. "
143             "Define the TOS field of IP packets. "
144             "Six values can be used for this setting:\n"
145             " 1:(IPTOS)-Minimize delay\n"
146             " 2:(IPTOS)-Maximize throughput\n"
147             " 3:(DiffServ)-Class1 with low drop probability\n"
148             " 4:(DiffServ)-class1 with high drop probability\n"
149             " 5:(DiffServ)-Class4 with low drop probabiltiy\n"
150             " 6:(DiffServ)-Class4 with high drop probabiltiy\n"
151             "Write the plot file for gnuplot. Only enable when the output "
152             "is specified. ",
153             type = Types.Enumerate,
154             allowed = ["1", "2", "3", "4", "5", "6"],
155             flags = Flags.Design)
156
157         r = Attribute("r",
158             "Client option. "
159             "Repetition of tests. 10 by default. ",
160             type = Types.Integer,
161             flags = Flags.Design)
162
163         t = Attribute("t",
164             "Client option. "
165             "Test time constraint in seconds. 5 by default. ",
166             type = Types.Integer,
167             flags = Flags.Design)
168
169         T = Attribute("T",
170             "Client option. "
171             "Throughput constraint for UDP generator or throughput "
172             "test. Unlimited by default. ",
173             type = Types.Integer,
174             flags = Flags.Design)
175
176         continuous = Attribute("continuous",
177             "Run nping in a while loop",
178             type = Types.Bool,
179             default = False,
180             flags = Flags.Design)
181
182         print_timestamp = Attribute("printTimestamp",
183             "Print timestamp before running nping",
184             type = Types.Bool,
185             default = False,
186             flags = Flags.Design)
187
188         cls._register_attribute(s)
189         cls._register_attribute(p)
190         cls._register_attribute(a)
191         cls._register_attribute(A)
192         cls._register_attribute(b)
193         cls._register_attribute(B)
194         cls._register_attribute(c)
195         cls._register_attribute(d)
196         cls._register_attribute(e)
197         cls._register_attribute(g)
198         cls._register_attribute(target)
199         cls._register_attribute(g)
200         cls._register_attribute(i)
201         cls._register_attribute(l)
202         cls._register_attribute(m)
203         cls._register_attribute(o)
204         cls._register_attribute(P)
205         cls._register_attribute(q)
206         cls._register_attribute(r)
207         cls._register_attribute(t)
208         cls._register_attribute(T)
209         cls._register_attribute(continuous)
210         cls._register_attribute(print_timestamp)
211
212     def __init__(self, ec, guid):
213         super(LinuxUdpTest, self).__init__(ec, guid)
214         self._home = "udptest-%s" % self.guid
215
216     def do_deploy(self):
217         if not self.get("command"):
218             self.set("command", self._start_command)
219
220         if not self.get("sources"):
221             self.set("sources", self._sources)
222
223         if not self.get("install"):
224             self.set("install", self._install)
225
226         if not self.get("build"):
227             self.set("build", self._build)
228
229         if not self.get("env"):
230             self.set("env", self._environment)
231
232         if not self.get("depends"):
233             self.set("depends", self._depends)
234
235         super(LinuxUdpTest, self).do_deploy()
236
237     def upload_start_command(self):
238         super(LinuxUdpTest, self).upload_start_command()
239
240         if self.get("s") == True:
241             # We want to make sure the server is running
242             # before the client starts.
243             # Run the command as a bash script in background,
244             # in the host ( but wait until the command has
245             # finished to continue )
246             self._run_in_background()
247     
248     def do_start(self):
249         if self.get("s") == True:
250             # Server is already running
251             if self.state == ResourceState.READY:
252                 command = self.get("command")
253                 self.info("Starting command '%s'" % command)
254
255                 self.set_started()
256             else:
257                 msg = " Failed to execute command '%s'" % command
258                 self.error(msg, out, err)
259                 raise RuntimeError, err
260         else:
261             super(LinuxUdpTest, self).do_start()
262  
263     @property
264     def _start_command(self):
265         args = []
266         if self.get("continuous") == True:
267             args.append("while true; do ")
268
269         if self.get("printTimestamp") == True:
270             args.append("""echo "`date +'%Y%m%d%H%M%S'`";""")
271
272         if self.get("s") == True:
273             args.append("udpserver")
274         else:
275             args.append("udptest")
276
277         if self.get("p"):
278             args.append("-p %d" % self.get("p"))
279         if self.get("a") == True:
280             args.append("-a")
281         if self.get("A"):
282             args.append("-A %d" % self.get("A"))
283         if self.get("b"):
284             args.append("-b %d" % self.get("b"))
285         if self.get("B"):
286             args.append("-B %d" % self.get("B"))
287         if self.get("c") == True:
288             args.append("-c")
289         if self.get("d"):
290             args.append("-d %d" % self.get("d"))
291         if self.get("e") == True:
292             args.append("-e")
293         if self.get("g") == True:
294             args.append("-g")
295         if self.get("target"):
296             args.append("-h %s" % self.get("target"))
297         if self.get("i") == True:
298             args.append("-i")
299         if self.get("l"):
300             args.append("-l %d" % self.get("l"))
301         if self.get("m"):
302             args.append("-m %d" % self.get("m"))
303         if self.get("o"):
304             args.append("-o %d" % self.get("o"))
305         if self.get("P"):
306             args.append("-P %d" % self.get("P"))
307         if self.get("q"):
308             args.append("-q %s" % self.get("q"))
309         if self.get("r"):
310             args.append("-r %d" % self.get("r"))
311         if self.get("t"):
312             args.append("-t %d" % self.get("t"))
313         if self.get("T"):
314             args.append("-T %d" % self.get("T"))
315
316         if self.get("continuous") == True:
317             args.append("; done ")
318
319         command = " ".join(args)
320
321         return command
322
323     @property
324     def _sources(self):
325         return "http://hpcbench.sourceforge.net/udp.tar.gz"
326
327     @property
328     def _depends(self):
329         return "gcc make"
330
331     @property
332     def _build(self):
333         sources = self.get("sources").split(" ")[0]
334         sources = os.path.basename(sources)
335
336         return (
337             # Evaluate if ccnx binaries are already installed
338             " ( "
339                 " test -f ${BIN}/udptest && "
340                 " echo 'binaries found, nothing to do' "
341             " ) || ( "
342             # If not, untar and build
343                 " ( "
344                     " mkdir -p ${SRC}/udptest && "
345                     " tar xf ${SRC}/%(sources)s --strip-components=1 -C ${SRC}/udptest "
346                  " ) && "
347                     "cd ${SRC}/udptest && "
348                     # Just execute and silence warnings...
349                     " ( make ) "
350              " )") % ({ 'sources': sources,
351                  })
352
353     @property
354     def _install(self):
355         return (
356             # Evaluate if ccnx binaries are already installed
357             " ( "
358                 " test -f ${BIN}/udptest && "
359                 " echo 'binaries found, nothing to do' "
360             " ) || ( "
361             # If not, install
362                 "  mv ${SRC}/udptest ${BIN} "
363             " )")
364
365     @property
366     def _environment(self):
367         return "PATH=$PATH:${BIN}/udptest"
368
369     def valid_connection(self, guid):
370         # TODO: Validate!
371         return True
372