1 |
antarus 08/04/21 21:07:16 |
2 |
|
3 |
Added: process_timer |
4 |
Log: |
5 |
add scriptlet to find walltime of PROCESSES and if the walltime is above a certain limit yield the pids on stdout. |
6 |
|
7 |
Revision Changes Path |
8 |
1.1 users/antarus/projects/infra/process_timer |
9 |
|
10 |
file : http://sources.gentoo.org/viewcvs.py/gentoo/users/antarus/projects/infra/process_timer?rev=1.1&view=markup |
11 |
plain: http://sources.gentoo.org/viewcvs.py/gentoo/users/antarus/projects/infra/process_timer?rev=1.1&content-type=text/plain |
12 |
|
13 |
Index: process_timer |
14 |
=================================================================== |
15 |
#!/usr/bin/python |
16 |
# Copyright Alec Warner 2008 |
17 |
# Released in the public domain. |
18 |
|
19 |
"""Find tasks that have been running over X time. |
20 |
|
21 |
Look in /proc, match on some regexes, offer to print pids that match |
22 |
options given by the user. |
23 |
""" |
24 |
|
25 |
|
26 |
__author__ = """Alec Warner <antarus@g.o>""" |
27 |
|
28 |
import logging |
29 |
import optparse |
30 |
import os |
31 |
import re |
32 |
|
33 |
PROC_PATH='/proc' |
34 |
|
35 |
def GetOpts(): |
36 |
"""Parse the options.""" |
37 |
|
38 |
parser = optparse.OptionParser() |
39 |
parser.add_option('-p', '--prog', help='program to search for') |
40 |
parser.add_option('-t', '--time', help='max time prog can run') |
41 |
parser.add_option('-w', '--walltime', help='match time against walltime') |
42 |
parser.add_option('-v', '--verbose', help='increase loglevel') |
43 |
opts, args = parser.parse_args() |
44 |
|
45 |
if not opts.prog: |
46 |
parser.error('--prog is required') |
47 |
|
48 |
if not opts.walltime: |
49 |
opts.walltime = True |
50 |
|
51 |
if opts.verbose: |
52 |
logging.getLogger().setLevel(10) |
53 |
|
54 |
logging.debug('opts are %s' % opts) |
55 |
|
56 |
return opts, args |
57 |
|
58 |
def FindPids(search_str): |
59 |
"""Return PIDS for all processes that have match search_str. |
60 |
Args: |
61 |
search_str: match /proc/*/cmdline to this string. |
62 |
Returns: |
63 |
(pid, cmdline), PIDS of processes that match search_str |
64 |
""" |
65 |
matches = set() |
66 |
|
67 |
pid_re = re.compile(r'[0-9]+') |
68 |
|
69 |
files = os.listdir(PROC_PATH) |
70 |
files = [f for f in files if pid_re.match(f)] |
71 |
|
72 |
# we shouldn't output ourselves |
73 |
my_pid = str(os.getpid()) |
74 |
if my_pid in files: |
75 |
files.remove(my_pid) |
76 |
|
77 |
logging.debug('pids avaialble: %s' % files) |
78 |
|
79 |
for pid in files: |
80 |
PID_DIR_PATH = os.path.join(PROC_PATH, pid) |
81 |
cmdline = open(os.path.join(PID_DIR_PATH, 'cmdline')).read() |
82 |
logging.debug('cmdline: %s' % cmdline) |
83 |
if search_str in cmdline: |
84 |
logging.debug('matched %s' % pid) |
85 |
matches.add((pid, cmdline)) |
86 |
|
87 |
logging.debug('pids matched: %s' % matches) |
88 |
|
89 |
return matches |
90 |
|
91 |
|
92 |
def CalculateTime(pid): |
93 |
"""Calculate walltime, runtime, and other random time bits for PID. |
94 |
Args: |
95 |
pid: pid to make time calculations for. |
96 |
Returns: |
97 |
A tuple of random crap. |
98 |
|
99 |
These times are in 'jiffies' or as far as I can tell USER_HZ or 1/100ths |
100 |
of a second slices. |
101 |
""" |
102 |
|
103 |
PID_DIR_PATH = os.path.join(PROC_PATH, pid) |
104 |
data = open(os.path.join(PID_DIR_PATH, 'stat')).read().split(' ') |
105 |
utime, stime, cutime, cstime = data[13:17] |
106 |
starttime = data[21] |
107 |
|
108 |
wall = int(utime) + int(stime) |
109 |
cwall = int(cutime) + int(cstime) |
110 |
|
111 |
return (utime, stime, cutime, cstime, wall, cwall, starttime) |
112 |
|
113 |
|
114 |
def Main(): |
115 |
"""Main Driver function.""" |
116 |
|
117 |
opts, args = GetOpts() |
118 |
pids = FindPids(opts.prog) |
119 |
for pid in pids: |
120 |
times = CalculateTime(pid[0]) |
121 |
if opts.walltime: |
122 |
if times[4] / 100 > opts.time: |
123 |
print pid[0] |
124 |
|
125 |
if __name__ == "__main__": |
126 |
Main() |
127 |
|
128 |
|
129 |
|
130 |
-- |
131 |
gentoo-commits@l.g.o mailing list |