Skip to content

Commit

Permalink
#779 / proc cpu times: linux impl
Browse files Browse the repository at this point in the history
  • Loading branch information
giampaolo committed Feb 28, 2016
1 parent 737360f commit 8193cb1
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 6 deletions.
2 changes: 1 addition & 1 deletion IDEAS
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ PLATFORMS
FEATURES
========

- (UNIX) process root (different from xwd)
- (UNIX) process root (different from cwd)

- (Linux) locked files via /proc/locks:
https://www.centos.org/docs/5/html/5.2/Deployment_Guide/s2-proc-locks.html
Expand Down
6 changes: 5 additions & 1 deletion psutil/_pslinux.py
Original file line number Diff line number Diff line change
Expand Up @@ -251,6 +251,8 @@ def set_scputimes_ntuple(procfs_path):
['path', 'fd', 'position', 'mode', 'flags'])
pmem = namedtuple('pmem', 'rss vms shared text lib data dirty')
pfullmem = namedtuple('pfullmem', pmem._fields + ('uss', 'pss', 'swap'))
pcputimes = namedtuple('pcputimes',
['user', 'system', 'children_user', 'children_system'])

pmmap_grouped = namedtuple(
'pmmap_grouped', ['path', 'rss', 'size', 'pss', 'shared_clean',
Expand Down Expand Up @@ -989,7 +991,9 @@ def cpu_times(self):
values = st.split(b' ')
utime = float(values[11]) / CLOCK_TICKS
stime = float(values[12]) / CLOCK_TICKS
return _common.pcputimes(utime, stime)
children_utime = float(values[13]) / CLOCK_TICKS
children_stime = float(values[14]) / CLOCK_TICKS
return pcputimes(utime, stime, children_utime, children_stime)

@wrap_exceptions
def wait(self, timeout=None):
Expand Down
11 changes: 7 additions & 4 deletions psutil/tests/test_process.py
Original file line number Diff line number Diff line change
Expand Up @@ -267,9 +267,12 @@ def test_cpu_percent(self):
def test_cpu_times(self):
times = psutil.Process().cpu_times()
assert (times.user > 0.0) or (times.system > 0.0), times
if LINUX:
assert (times.children_user >= 0.0), times
assert (times.children_system >= 0.0), times
# make sure returned values can be pretty printed with strftime
time.strftime("%H:%M:%S", time.localtime(times.user))
time.strftime("%H:%M:%S", time.localtime(times.system))
for name in times._fields:
time.strftime("%H:%M:%S", time.localtime(getattr(times, name)))

# Test Process.cpu_times() against os.times()
# os.times() is broken on Python 2.6
Expand All @@ -279,8 +282,8 @@ def test_cpu_times(self):

@unittest.skipUnless(sys.version_info > (2, 6, 1) and not OSX,
'os.times() is not reliable on this Python version')
def test_cpu_times2(self):
user_time, kernel_time = psutil.Process().cpu_times()
def test_cpu_times_2(self):
user_time, kernel_time = psutil.Process().cpu_times()[:2]
utime, ktime = os.times()[:2]

# Use os.times()[:2] as base values to compare our results
Expand Down

0 comments on commit 8193cb1

Please sign in to comment.