Statistics
| Branch: | Tag: | Revision:

mininet / examples / limit.py @ 216a4b7c

History | View | Annotate | Download (1.53 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
    print '*** Testing network %.2f Mbps bandwidth limit' % bw
17
    net.iperf( )
18

    
19
def testCpuLimit( net, cpu ):
20
    pct = cpu * 100
21
    print '*** Testing CPU %.0f%% bandwidth limit' % pct
22
    h1, h2 = net.hosts
23
    h1.cmd( 'while true; do a=1; done &' )
24
    h2.cmd( 'while true; do a=1; done &' )
25
    pid1 = h1.cmd( 'echo $!' ).strip()
26
    pid2 = h2.cmd( 'echo $!' ).strip()
27
    cmd = 'ps -p %s,%s -o pid,%%cpu,args' % ( pid1, pid2 )
28
    for i in range( 0, 5):
29
        sleep( 1 ) 
30
        print quietRun( cmd ).strip()
31
    h1.cmd( 'kill %1')
32
    h2.cmd( 'kill %1')
33

    
34
def limit( bw=1, cpu=.3 ):
35
    """Example/test of link and CPU bandwidth limits
36
       bw: interface bandwidth limit in Mbps
37
       cpu: cpu limit as fraction of overall CPU time"""
38
    intf = custom( TCIntf, bw=1 )
39
    myTopo = TreeTopo( depth=1, fanout=2 )
40
    for sched in 'rt', 'cfs':
41
        print '*** Testing with', sched, 'bandwidth limiting'
42
        host = custom( CPULimitedHost, sched=sched, cpu=cpu )
43
        net = Mininet( topo=myTopo, intf=intf, host=host )
44
        net.start()
45
        testLinkLimit( net, bw=bw )
46
        testCpuLimit( net, cpu=cpu )
47
        net.stop()
48

    
49
if __name__ == '__main__':
50
    setLogLevel( 'info' )
51
    limit()