Merge pull request #993 from ancailliau/fix-datacontract-json-serialization
[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         usecs = int (m.group (3))
53         start = int (m.group (4))
54         if generation == 0:
55             generation = "minor"
56             minor_pausetimes.append (usecs)
57         else:
58             generation = "major"
59             major_pausetimes.append (usecs)
60         if concurrent == 1:
61             kind = "CONC"
62         else:
63             kind = "SYNC"
64         rec = (generation, start, start + usecs, 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 else:
76     data = np.array (data, dtype = [('caption', '|S20'), ('start', int), ('stop', int), ('kind', '|S20')])
77     cap, start, stop=data['caption'], data['start'], data['stop']
78
79     #Check the status, because we paint all lines with the same color
80     #together
81     is_sync= (data['kind']=='SYNC')
82     not_sync=np.logical_not(is_sync)
83
84     #Get unique captions and there indices and the inverse mapping
85     captions, unique_idx, caption_inv=np.unique(cap, 1,1)
86     print captions
87
88     #Build y values from the number of unique captions.
89     y=(caption_inv+1)/float(len(captions)+1)
90
91     #Plot function
92     def timelines(y, xstart, xstop,color='b'):
93         """Plot timelines at y from xstart to xstop with given color."""
94         plt.hlines(y,xstart,xstop,color,lw=4)
95         plt.vlines(xstart, y+0.03,y-0.03,color,lw=2)
96         plt.vlines(xstop, y+0.03,y-0.03,color,lw=2)
97
98     #Plot ok tl black
99     timelines(y[is_sync],start[is_sync],stop[is_sync],'r')
100     #Plot fail tl red
101     timelines(y[not_sync],start[not_sync],stop[not_sync],'k')
102
103     #Setup the plot
104     ax=plt.gca()
105     #ax.xaxis_date()
106     #myFmt = DateFormatter('%H:%M:%S')
107     #ax.xaxis.set_major_formatter(myFmt)
108     #ax.xaxis.set_major_locator(SecondLocator(0,interval=20))
109
110     #To adjust the xlimits a timedelta is needed.
111     delta=(stop.max()-start.min())/10
112
113     plt.yticks(y[unique_idx],captions)
114     plt.ylim(0,1)
115     plt.xlim(start.min()-delta, stop.max()+delta)
116     plt.xlabel('Time')
117
118 plt.show()