Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
M
mininet
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Requirements
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Locked files
Deploy
Releases
Package Registry
Model registry
Operate
Terraform modules
Monitor
Incidents
Service Desk
Analyze
Value stream analytics
Contributor analytics
Repository analytics
Code review analytics
Issue analytics
Insights
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Olaf Bergmann
mininet
Commits
ce781a18
Commit
ce781a18
authored
10 years ago
by
cody burkard
Browse files
Options
Downloads
Patches
Plain Diff
use cgroups to calculate percentage of cpu used
parent
b85943dc
No related branches found
Branches containing commit
No related tags found
Tags containing commit
No related merge requests found
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
mininet/net.py
+31
-22
31 additions, 22 deletions
mininet/net.py
with
31 additions
and
22 deletions
mininet/net.py
+
31
−
22
View file @
ce781a18
...
...
@@ -94,6 +94,7 @@
import
copy
from
time
import
sleep
from
itertools
import
chain
,
groupby
from
math
import
ceil
from
mininet.cli
import
CLI
from
mininet.log
import
info
,
error
,
debug
,
output
,
warn
...
...
@@ -726,33 +727,41 @@ def runCpuLimitTest( self, cpu, duration=5 ):
duration: test duration in seconds
returns a single list of measured CPU fractions as floats.
"""
cores
=
int
(
quietRun
(
'
nproc
'
)
)
pct
=
cpu
*
100
info
(
'
*** Testing CPU %.0f%% bandwidth limit
\n
'
%
pct
)
info
(
'
*** Testing CPU %.0f%% bandwidth limit
\n
'
%
pct
)
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
:
h
.
cmd
(
'
while true; do a=1; done &
'
)
pids
=
[
h
.
cmd
(
'
echo $!
'
).
strip
()
for
h
in
hosts
]
pids_str
=
"
,
"
.
join
([
"
%s
"
%
pid
for
pid
in
pids
])
cmd
=
'
ps -p %s -o pid,%%cpu,args
'
%
pids_str
# It's a shame that this is what pylint prefers
outputs
=
[]
for
_
in
range
(
duration
):
pids
[
h
]
=
[]
for
_core
in
range
(
num_procs
):
h
.
cmd
(
'
while true; do a=1; done &
'
)
pids
[
h
].
append
(
h
.
cmd
(
'
echo $!
'
).
strip
()
)
outputs
=
{}
time
=
{}
# 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
)
outputs
.
append
(
quietRun
(
cmd
).
strip
()
)
for
h
in
hosts
:
h
.
cmd
(
'
kill $!
'
)
for
host
in
hosts
:
with
open
(
'
/sys/fs/cgroup/cpuacct/%s/cpuacct.usage
'
%
host
,
'
r
'
)
as
f
:
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
=
[]
for
test_output
in
outputs
:
# Split by line. Ignore first line, which looks like this:
# PID %CPU COMMAND\n
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
)
)
)
for
_host
,
outputs
in
outputs
.
items
():
for
pct
in
outputs
:
cpu_fractions
.
append
(
pct
)
output
(
'
*** Results: %s
\n
'
%
cpu_fractions
)
return
cpu_fractions
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment