Skip to content
Snippets Groups Projects
Commit ce781a18 authored by cody burkard's avatar cody burkard
Browse files

use cgroups to calculate percentage of cpu used

parent b85943dc
No related branches found
No related tags found
No related merge requests found
...@@ -94,6 +94,7 @@ ...@@ -94,6 +94,7 @@
import copy import copy
from time import sleep from time import sleep
from itertools import chain, groupby from itertools import chain, groupby
from math import ceil
from mininet.cli import CLI from mininet.cli import CLI
from mininet.log import info, error, debug, output, warn from mininet.log import info, error, debug, output, warn
...@@ -726,33 +727,41 @@ def runCpuLimitTest( self, cpu, duration=5 ): ...@@ -726,33 +727,41 @@ def runCpuLimitTest( self, cpu, duration=5 ):
duration: test duration in seconds duration: test duration in seconds
returns a single list of measured CPU fractions as floats. returns a single list of measured CPU fractions as floats.
""" """
cores = int( quietRun( 'nproc' ) )
pct = cpu * 100 pct = cpu * 100
info('*** Testing CPU %.0f%% bandwidth limit\n' % pct) info( '*** Testing CPU %.0f%% bandwidth limit\n' % pct )
hosts = self.hosts hosts = self.hosts
cores = int( quietRun( 'nproc' ) )
# number of processes to run a while loop on per host
num_procs = int( ceil( cores * cpu ) )
pids = {}
for h in hosts: for h in hosts:
h.cmd( 'while true; do a=1; done &' ) pids[ h ] = []
pids = [h.cmd( 'echo $!' ).strip() for h in hosts] for _core in range( num_procs ):
pids_str = ",".join(["%s" % pid for pid in pids]) h.cmd( 'while true; do a=1; done &' )
cmd = 'ps -p %s -o pid,%%cpu,args' % pids_str pids[ h ].append( h.cmd( 'echo $!' ).strip() )
# It's a shame that this is what pylint prefers outputs = {}
outputs = [] time = {}
for _ in range( duration ): # get the initial cpu time for each host
for host in hosts:
outputs[ host ] = []
with open( '/sys/fs/cgroup/cpuacct/%s/cpuacct.usage' % host, 'r' ) as f:
time[ host ] = float( f.read() )
for _ in range( 5 ):
sleep( 1 ) sleep( 1 )
outputs.append( quietRun( cmd ).strip() ) for host in hosts:
for h in hosts: with open( '/sys/fs/cgroup/cpuacct/%s/cpuacct.usage' % host, 'r' ) as f:
h.cmd( 'kill $!' ) readTime = float( f.read() )
outputs[ host ].append( ( ( readTime - time[ host ] )
/ 1000000000 ) / cores * 100 )
time[ host ] = readTime
for h, pids in pids.items():
for pid in pids:
h.cmd( 'kill -9 %s' % pid )
cpu_fractions = [] cpu_fractions = []
for test_output in outputs: for _host, outputs in outputs.items():
# Split by line. Ignore first line, which looks like this: for pct in outputs:
# PID %CPU COMMAND\n cpu_fractions.append( pct )
for line in test_output.split('\n')[1:]:
r = r'\d+\s*(\d+\.\d+)'
m = re.search( r, line )
if m is None:
error( '*** Error: could not extract CPU fraction: %s\n' %
line )
return None
cpu_fractions.append( float( m.group( 1 ) ) )
output( '*** Results: %s\n' % cpu_fractions ) output( '*** Results: %s\n' % cpu_fractions )
return cpu_fractions return cpu_fractions
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment