Skip to content

Instantly share code, notes, and snippets.

@svanoort
Last active October 20, 2016 18:41
Show Gist options
  • Save svanoort/0eb8a33f43d7e6466997e502b7d83e8b to your computer and use it in GitHub Desktop.
Save svanoort/0eb8a33f43d7e6466997e502b7d83e8b to your computer and use it in GitHub Desktop.
Analyze success pstools dumps to find threads eating a lot of system time
#!/bin/python
import sys
import re
# You'll need to extract JUST the table of pstools output (no headers!) from your pstools thread dumps for windows
print(sys.argv)
if not sys.argv[1] or not sys.argv[2]:
print("Need to supply the path to the pstools before and after filenames")
exit()
def convertTime(time_string):
""" Convert elapsed time in HH:MM:SS.ms to millis """
fields = re.compile('[:.]+').split(time_string)
return int(fields[0])*3600*1000 + int(fields[1])*60*1000 + int(fields[2])*1000 + int(fields[3])
def parseline(line_text):
""" Extract thread ID and the user, kernel, and elapsed time in millis """
fields = re.compile('[ ]+').split(line_text.strip())
output = list()
output = [fields[0], convertTime(fields[4]), convertTime(fields[5]), convertTime(fields[6])]
return output
beforefile = sys.argv[1]
afterfile = sys.argv[2]
beforethreads = dict()
afterthreads = dict()
# Read threads to dict
with open(beforefile) as f:
for line in f:
cleanedup = parseline(line)
beforethreads[int(cleanedup[0])] = cleanedup[1:4]
# Read threads to dict
with open(afterfile) as f:
for line in f:
cleanedup = parseline(line)
afterthreads[int(cleanedup[0])] = cleanedup[1:4]
# Associate threads and subtract time
elapsed = dict()
for threadid in afterthreads.keys():
before = beforethreads.get(threadid)
if before:
after = afterthreads.get(threadid)
delta = [after[i] - before[i] for i in xrange(0,3)]
elapsed[threadid] = delta
# print("Elapsed times for thread {0} are {1}".format(threadid, [after[i]-before[i] for i in range(0:len(after))]))
sorted_by_user_time = sorted(elapsed.items(), key=lambda x: x[1][0], reverse=True)
print ("List of top 20 threads in decreasing user time spent")
print("id (hex), user time (ms), kernel time (ms), and overall time (ms)")
for row in sorted_by_user_time[0:20]:
print("{0} ({1}), {2}, {3}, {4}".format(row[0], hex(row[0]), row[1][0], row[1][1], row[1][2]))
print("--------------------------------")
print("--------------------------------")
sorted_by_kernel_time = sorted(elapsed.items(), key=lambda x: x[1][1], reverse=True)
print ("List of top 20 threads in decreasing kernel time spent")
print("id (hex), user time (ms), kernel time (ms), and overall time (ms)")
for row in sorted_by_kernel_time[0:20]:
print("{0} ({1}), {2}, {3}, {4}".format(row[0], hex(row[0]), row[1][0], row[1][1], row[1][2]))
# done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment