OLD | NEW |
(Empty) | |
| 1 # Copyright (c) 2010 The Chromium OS Authors. All rights reserved. |
| 2 # Use of this source code is governed by a BSD-style license that can be |
| 3 # found in the LICENSE file. |
| 4 |
| 5 from autotest_lib.client.bin import test, utils |
| 6 |
| 7 def get_pids(program_name): |
| 8 """ |
| 9 Collect a list of pids for all the instances of a program. |
| 10 |
| 11 @param program_name the name of the program |
| 12 @return list of pids |
| 13 """ |
| 14 pidlist = utils.system_output("pgrep -f \'%s\'" % program_name) |
| 15 return pidlist.splitlines() |
| 16 |
| 17 |
| 18 def get_number_of_logical_cpu(): |
| 19 """ |
| 20 From /proc/stat/. |
| 21 |
| 22 @return number of logic cpu |
| 23 """ |
| 24 ret = utils.system_output("cat /proc/stat | grep ^cpu[0-9+] | wc -l") |
| 25 return int(ret) |
| 26 |
| 27 |
| 28 def get_utime_stime(pids): |
| 29 """ |
| 30 Snapshot the sum of utime and the sum of stime for a list of processes. |
| 31 |
| 32 @param pids a list of pid |
| 33 @return [sum_of_utime, sum_of_stime] |
| 34 """ |
| 35 timelist = [0, 0] |
| 36 for p in pids: |
| 37 statFile = file("/proc/%s/stat" % p, "r") |
| 38 T = statFile.readline().split(" ")[13:15] |
| 39 statFile.close() |
| 40 for i in range(len(timelist)): |
| 41 timelist[i] = timelist[i] + int(T[i]) |
| 42 return timelist |
| 43 |
| 44 |
| 45 def get_cpu_usage(duration, time): |
| 46 """ |
| 47 Calculate cpu usage based on duration and time on cpu. |
| 48 |
| 49 @param duration |
| 50 @param time on cpu |
| 51 @return cpu usage |
| 52 """ |
| 53 return float(time) / float(duration * get_number_of_logical_cpu()) |
| 54 |
OLD | NEW |