| Index: scripts/slave/recipe_modules/auto_bisect/resources/cpu_usage.py
|
| diff --git a/scripts/slave/recipe_modules/auto_bisect/resources/cpu_usage.py b/scripts/slave/recipe_modules/auto_bisect/resources/cpu_usage.py
|
| new file mode 100644
|
| index 0000000000000000000000000000000000000000..8cb4848883407b3b1e27b201759f5ff8d5b0fd08
|
| --- /dev/null
|
| +++ b/scripts/slave/recipe_modules/auto_bisect/resources/cpu_usage.py
|
| @@ -0,0 +1,79 @@
|
| +#!/usr/bin/python
|
| +# Copyright 2015 The Chromium Authors. All rights reserved.
|
| +# Use of this source code is governed by a BSD-style license that can be
|
| +# found in the LICENSE file.
|
| +
|
| +"""Prints out the most active processes running in the background.
|
| +
|
| +Example usage:
|
| + ./cpu_usage.py --nproc==[THE NUMBER OF PROCESSES TO PRINT OUT]
|
| +"""
|
| +
|
| +
|
| +import argparse
|
| +import json
|
| +import platform
|
| +import subprocess
|
| +import sys
|
| +
|
| +LINUX_START_LINE_NUM = 7
|
| +WINDOWS_START_LINE_NUM = 3
|
| +
|
| +def _parse_line(line):
|
| + """Parses a line from top output
|
| +
|
| + Args:
|
| + line(str): a line from top output that contains the
|
| + information of a process
|
| +
|
| + Returns:
|
| + An dictionary with useful information about the process
|
| +
|
| + """
|
| + token_list = line.strip().split()
|
| + if platform.system() == "Windows":
|
| + process_object = {'pid':token_list[6],
|
| + 'pCPU':token_list[5],
|
| + 'command':token_list[7]}
|
| + else:
|
| + process_object = {'pid':token_list[0], 'user':token_list[1],
|
| + 'pCPU':token_list[8], 'pMEM':token_list[9],
|
| + 'command':token_list[11]}
|
| + return process_object
|
| +
|
| +def _get_top_processes(n):
|
| + """Fetches the top processes returned by top command
|
| +
|
| + Args:
|
| + n(int): the number of processes to get
|
| +
|
| + Returns:
|
| + A list of dictionaries, each representing one of the top processes
|
| +
|
| + """
|
| + if platform.system() == "Windows":
|
| + processes = subprocess.check_output('powershell "ps | sort -desc cpu"',
|
| + shell=True).split('\n')
|
| + top_processes = processes[WINDOWS_START_LINE_NUM:
|
| + WINDOWS_START_LINE_NUM + n]
|
| + else:
|
| + processes = subprocess.check_output(["top","-n 1","-b"]).split('\n')
|
| + top_processes = processes[LINUX_START_LINE_NUM:
|
| + LINUX_START_LINE_NUM + n]
|
| + parsed_top_processes = [_parse_line(process) \
|
| + for process in top_processes]
|
| + return parsed_top_processes
|
| +
|
| +
|
| +def main():
|
| + parser = argparse.ArgumentParser(
|
| + description='Take a snapshot of CPU usage.')
|
| + parser.add_argument('--nproc', dest='nproc', default=10,
|
| + help='the number of processes to display')
|
| + args = parser.parse_args()
|
| + top_processes = _get_top_processes(int(args.nproc))
|
| + print(json.dumps(top_processes))
|
| + return 0
|
| +
|
| +if __name__ == "__main__":
|
| + main()
|
|
|