Statistics
| Branch: | Tag: | Revision:

mininet / examples / limit.py @ 0b7c277e

History | View | Annotate | Download (1.63 KB)

1 4ac1148e Bob Lantz
#!/usr/bin/python
2
3
"""
4
limit.py: example of using link and CPU limits
5
"""
6
7
from mininet.net import Mininet
8 216a4b7c Bob Lantz
from mininet.link import TCIntf
9 4ac1148e Bob Lantz
from mininet.node import CPULimitedHost
10
from mininet.topolib import TreeTopo
11
from mininet.util import custom, quietRun
12
from mininet.log import setLogLevel
13
from time import sleep
14
15 216a4b7c Bob Lantz
def testLinkLimit( net, bw ):
16 14ff3ad3 Bob Lantz
    "Run bandwidth limit test"
17 216a4b7c Bob Lantz
    print '*** Testing network %.2f Mbps bandwidth limit' % bw
18
    net.iperf( )
19 4ac1148e Bob Lantz
20 216a4b7c Bob Lantz
def testCpuLimit( net, cpu ):
21 14ff3ad3 Bob Lantz
    "run CPU limit test"
22 216a4b7c Bob Lantz
    pct = cpu * 100
23
    print '*** Testing CPU %.0f%% bandwidth limit' % pct
24 4ac1148e Bob Lantz
    h1, h2 = net.hosts
25
    h1.cmd( 'while true; do a=1; done &' )
26
    h2.cmd( 'while true; do a=1; done &' )
27
    pid1 = h1.cmd( 'echo $!' ).strip()
28
    pid2 = h2.cmd( 'echo $!' ).strip()
29
    cmd = 'ps -p %s,%s -o pid,%%cpu,args' % ( pid1, pid2 )
30 14ff3ad3 Bob Lantz
    # It's a shame that this is what pylint prefers
31
    for _ in range( 5 ):
32
        sleep( 1 )
33 216a4b7c Bob Lantz
        print quietRun( cmd ).strip()
34 4ac1148e Bob Lantz
    h1.cmd( 'kill %1')
35
    h2.cmd( 'kill %1')
36
37 0b7c277e Bob Lantz
def limit( bw=1, cpu=.4 ):
38 216a4b7c Bob Lantz
    """Example/test of link and CPU bandwidth limits
39
       bw: interface bandwidth limit in Mbps
40
       cpu: cpu limit as fraction of overall CPU time"""
41
    intf = custom( TCIntf, bw=1 )
42 4ac1148e Bob Lantz
    myTopo = TreeTopo( depth=1, fanout=2 )
43 216a4b7c Bob Lantz
    for sched in 'rt', 'cfs':
44
        print '*** Testing with', sched, 'bandwidth limiting'
45
        host = custom( CPULimitedHost, sched=sched, cpu=cpu )
46
        net = Mininet( topo=myTopo, intf=intf, host=host )
47
        net.start()
48
        testLinkLimit( net, bw=bw )
49
        testCpuLimit( net, cpu=cpu )
50
        net.stop()
51 4ac1148e Bob Lantz
52
if __name__ == '__main__':
53
    setLogLevel( 'info' )
54
    limit()