Skip to content
This repository has been archived by the owner on Oct 7, 2020. It is now read-only.

Commit

Permalink
Merge pull request #33 from sergey-dryabzhinsky/container-cpu-usage-b…
Browse files Browse the repository at this point in the history
…y-cgroup-cpuacct

Container cpu usage by cgroup cpuacct
  • Loading branch information
Sergey committed Jul 15, 2013
2 parents 9e928d9 + 4865c0c commit e47bc57
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 2 deletions.
4 changes: 2 additions & 2 deletions lwp.py
Original file line number Diff line number Diff line change
Expand Up @@ -767,13 +767,13 @@ def refresh_cpu_containers(name=None):
container = container.replace(' (auto)', '')
containers.append({
'name': container,
'cpu': lwp.container_cpu_percent(container),
'cpu': lwp.container_cpu_percent_cgroup(container),
})
return jsonify(data=containers)
elif name == 'host':
return lwp.host_cpu_percent()
return jsonify({
'cpu': lwp.container_cpu_percent(name),
'cpu': lwp.container_cpu_percent_cgroup(name),
})


Expand Down
26 changes: 26 additions & 0 deletions lwp/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,32 @@ def container_cpu_percent(name):
return '0'
return out

def container_cpu_percent_cgroup(name):
'''
returns CPU usage in percent
'''
if name in stopped():
return '0'

cont_usage_file = "/sys/fs/cgroup/cpuacct/lxc/%s/cpuacct.usage" % name
if not os.path.isfile(cont_usage_file):
return '-1'

f = open(cont_usage_file, 'r')
line = f.readline().strip()
prev_cont_usage_ns = float(line) * 10**-9
f.close()

time.sleep(0.1)

f = open(cont_usage_file, 'r')
line = f.readline().strip()
current_cont_usage_ns = float(line) * 10**-9
f.close()

percent = 100 * (current_cont_usage_ns - prev_cont_usage_ns) / 0.1
return str('%.1f' % percent)


def get_template_help(name):
cmd = ["lxc-create -t %s -h" % name]
Expand Down

0 comments on commit e47bc57

Please sign in to comment.