Statistics
| Branch: | Tag: | Revision:

mininet / examples / limit.py @ 0b7c277e

History | View | Annotate | Download (1.63 KB)

1
#!/usr/bin/python
2

    
3
"""
4
limit.py: example of using link and CPU limits
5
"""
6

    
7
from mininet.net import Mininet
8
from mininet.link import TCIntf
9
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
def testLinkLimit( net, bw ):
16
    "Run bandwidth limit test"
17
    print '*** Testing network %.2f Mbps bandwidth limit' % bw
18
    net.iperf( )
19

    
20
def testCpuLimit( net, cpu ):
21
    "run CPU limit test"
22
    pct = cpu * 100
23
    print '*** Testing CPU %.0f%% bandwidth limit' % pct
24
    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
    # It's a shame that this is what pylint prefers
31
    for _ in range( 5 ):
32
        sleep( 1 )
33
        print quietRun( cmd ).strip()
34
    h1.cmd( 'kill %1')
35
    h2.cmd( 'kill %1')
36

    
37
def limit( bw=1, cpu=.4 ):
38
    """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
    myTopo = TreeTopo( depth=1, fanout=2 )
43
    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

    
52
if __name__ == '__main__':
53
    setLogLevel( 'info' )
54
    limit()