Merge pull request #960 from ermshiperete/ShowHelp
[mono.git] / tools / sgen / gcpausevis.py
1 #!/usr/bin/env python
2 import matplotlib.pyplot as plt
3 from matplotlib.dates import DateFormatter, MinuteLocator, SecondLocator
4 import numpy as np
5 from StringIO import StringIO
6 import os
7 import re
8 import sys
9 from optparse import OptionParser
10 import subprocess
11
12 parser = OptionParser (usage = "Usage: %prog [options] BINARY-PROTOCOL")
13 parser.add_option ('--histogram', action = 'store_true', dest = 'histogram', help = "pause time histogram")
14 parser.add_option ('--minor', action = 'store_true', dest = 'minor', help = "only show minor collections in histogram")
15 parser.add_option ('--major', action = 'store_true', dest = 'major', help = "only show major collections in histogram")
16 (options, files) = parser.parse_args ()
17
18 show_histogram = False
19 show_minor = True
20 show_major = True
21 if options.minor:
22     show_histogram = True
23     show_major = False
24 if options.major:
25     show_histogram = True
26     show_minor = False
27 if options.histogram:
28     show_histogram = True
29
30 script_path = os.path.realpath (__file__)
31 sgen_grep_path = os.path.join (os.path.dirname (script_path), 'sgen-grep-binprot')
32
33 if not os.path.isfile (sgen_grep_path):
34     sys.stderr.write ('Error: `%s` does not exist.\n' % sgen_grep_path)
35     sys.exit (1)
36
37 if len (files) != 1:
38     parser.print_help ()
39     sys.exit (1)
40
41 data = []
42 minor_pausetimes = []
43 major_pausetimes = []
44
45 grep_input = open (files [0])
46 proc = subprocess.Popen ([sgen_grep_path, '--pause-times'], stdin = grep_input, stdout = subprocess.PIPE)
47 for line in iter (proc.stdout.readline, ''):
48     m = re.match ('^pause-time (\d+) (\d+) (\d+) (\d+)', line)
49     if m:
50         generation = int (m.group (1))
51         concurrent = int (m.group (2))
52         msecs = int (m.group (3)) / 10.0 / 1000.0
53         start = int (m.group (4)) / 10.0 / 1000.0
54         if generation == 0:
55             generation = "minor"
56             minor_pausetimes.append (msecs)
57         else:
58             generation = "major"
59             major_pausetimes.append (msecs)
60         if concurrent == 1:
61             kind = "CONC"
62         else:
63             kind = "SYNC"
64         rec = (generation, start, start + msecs, kind)
65         print rec
66         data.append (rec)
67
68 if show_histogram:
69     pausetimes = []
70     if show_minor:
71         pausetimes += minor_pausetimes
72     if show_major:
73         pausetimes += major_pausetimes
74     plt.hist (pausetimes, 100)
75     plt.xlabel ('Pause time in msec')
76 else:
77     data = np.array (data, dtype = [('caption', '|S20'), ('start', int), ('stop', int), ('kind', '|S20')])
78     cap, start, stop=data['caption'], data['start'], data['stop']
79
80     #Check the status, because we paint all lines with the same color
81     #together
82     is_sync= (data['kind']=='SYNC')
83     not_sync=np.logical_not(is_sync)
84
85     #Get unique captions and there indices and the inverse mapping
86     captions, unique_idx, caption_inv=np.unique(cap, 1,1)
87     print captions
88
89     #Build y values from the number of unique captions.
90     y=(caption_inv+1)/float(len(captions)+1)
91
92     #Plot function
93     def timelines(y, xstart, xstop,color='b'):
94         """Plot timelines at y from xstart to xstop with given color."""
95         plt.hlines(y,xstart,xstop,color,lw=4)
96         plt.vlines(xstart, y+0.03,y-0.03,color,lw=2)
97         plt.vlines(xstop, y+0.03,y-0.03,color,lw=2)
98
99     #Plot ok tl black
100     timelines(y[is_sync],start[is_sync],stop[is_sync],'r')
101     #Plot fail tl red
102     timelines(y[not_sync],start[not_sync],stop[not_sync],'k')
103
104     #Setup the plot
105     ax=plt.gca()
106     #ax.xaxis_date()
107     #myFmt = DateFormatter('%H:%M:%S')
108     #ax.xaxis.set_major_formatter(myFmt)
109     #ax.xaxis.set_major_locator(SecondLocator(0,interval=20))
110
111     #To adjust the xlimits a timedelta is needed.
112     delta=(stop.max()-start.min())/10
113
114     plt.yticks(y[unique_idx],captions)
115     plt.ylim(0,1)
116     plt.xlim(start.min()-delta, stop.max()+delta)
117     plt.xlabel('Time')
118
119 plt.show()