1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
|
# Copyright 2015-2016 ARM Limited
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
"""
The EventPlot is used to represent Events with two characteristics:
- A name, which determines the colour on the plot
- A lane, which determines the lane in which the event occurred
In the case of a cpu residency plot, the term lane can be equated to
a CPU and the name attribute can be the PID of the task
"""
from trappy.plotter import AttrConf
import uuid
import json
import os
from trappy.plotter.AbstractDataPlotter import AbstractDataPlotter
from trappy.plotter import IPythonConf
if not IPythonConf.check_ipython():
raise ImportError("Ipython Environment not Found")
from IPython.display import display, HTML
# pylint: disable=R0201
# pylint: disable=R0921
class EventPlot(AbstractDataPlotter):
"""
Input Data should be of the format
::
{ "<name1>" : [
[event_start, event_end, lane],
.
.
[event_start, event_end, lane],
],
.
.
.
"<nameN>" : [
[event_start, event_end, lane],
.
.
[event_start, event_end, lane],
],
}
:param data: Input Data
:type data: dict
:param keys: List of unique names in the data dictionary
:type keys: list
:param domain: Domain of the event data
:type domain: tuple
:param lane_prefix: A string prefix to be used to name each lane
:type lane_prefix: str
:param num_lanes: Total number of expected lanes
:type num_lanes: int
:param summary: Show a mini plot below the main plot with an
overview of where your current view is with respect to the
whole trace
:type summary: bool
:param stride: Stride can be used if the trace is very large.
It results in sampled rendering
:type stride: bool
:param lanes: The sorted order of lanes
:type lanes: list
"""
def __init__(
self,
data,
keys,
domain,
lane_prefix="Lane: ",
num_lanes=0,
summary=True,
stride=False,
lanes=None):
self._html = []
self._fig_name = self._generate_fig_name()
# Function to get the average duration of each event
avgFunc = lambda x: sum([(evt[1] - evt[0]) for evt in x]) / float(len(x) + 1)
avg = {k: avgFunc(v) for k, v in data.iteritems()}
# Filter keys with zero average time
keys = filter(lambda x : avg[x] != 0, avg)
graph = {}
graph["data"] = data
graph["lanes"] = self._get_lanes(lanes, lane_prefix, num_lanes, data)
graph["xDomain"] = domain
graph["keys"] = sorted(keys, key=lambda x: avg[x], reverse=True)
graph["showSummary"] = summary
graph["stride"] = AttrConf.EVENT_PLOT_STRIDE
self._data = json.dumps(graph)
# Initialize the HTML, CSS and JS Components
self._add_css()
self._init_html()
def view(self):
"""Views the Graph Object"""
# Defer installation of IPython components
# to the .view call to avoid any errors at
# when importing the module. This facilitates
# the importing of the module from outside
# an IPython notebook
IPythonConf.iplot_install("EventPlot")
display(HTML(self.html()))
def savefig(self, path):
"""Save the plot in the provided path
.. warning:: Not Implemented for :mod:`trappy.plotter.EventPlot`
"""
raise NotImplementedError(
"Save is not currently implemented for EventPlot")
def _get_lanes(self,
input_lanes,
lane_prefix,
num_lanes,
data):
"""Populate the lanes for the plot"""
# If the user has specified lanes explicitly
lanes = []
if input_lanes:
lane_map = {}
for idx, lane in enumerate(input_lanes):
lane_map[lane] = idx
for name in data:
for event in data[name]:
lane = event[2]
try:
event[2] = lane_map[lane]
except KeyError:
raise RuntimeError("Invalid Lane %s" % lane)
for idx, lane in enumerate(input_lanes):
lanes.append({"id": idx, "label": lane})
else:
if not num_lanes:
raise RuntimeError("Either lanes or num_lanes must be specified")
for idx in range(num_lanes):
lanes.append({"id": idx, "label": "{}{}".format(lane_prefix, idx)})
return lanes
def _generate_fig_name(self):
"""Generate a unqiue name for the figure"""
fig_name = "fig_" + uuid.uuid4().hex
return fig_name
def _init_html(self):
"""Initialize HTML for the plot"""
div_js = """
<script>
var req = require.config( {
paths: {
"EventPlot": '""" + IPythonConf.add_web_base("plotter_scripts/EventPlot/EventPlot") + """',
"d3-tip": '""" + IPythonConf.add_web_base("plotter_scripts/EventPlot/d3.tip.v0.6.3") + """',
"d3-plotter": '""" + IPythonConf.add_web_base("plotter_scripts/EventPlot/d3.min") + """'
},
shim: {
"d3-plotter" : {
"exports" : "d3"
},
"d3-tip": ["d3-plotter"],
"EventPlot": {
"deps": ["d3-tip", "d3-plotter" ],
"exports": "EventPlot"
}
}
});
"""
div_js += "var data = {};\n".format(self._data)
div_js += """
req(["require", "EventPlot"], function() {
EventPlot.generate('""" + self._fig_name + """', '""" + IPythonConf.add_web_base("") + """', data);
});
</script>
"""
self._html.append(
'<div id="{}" class="eventplot">{}</div>'.format(self._fig_name,
div_js))
def _add_css(self):
"""Append the CSS to the HTML code generated"""
base_dir = os.path.dirname(os.path.realpath(__file__))
css_file = os.path.join(base_dir, "css/EventPlot.css")
self._html.append("<style>")
with open(css_file, 'r') as css_fh:
self._html += [l[:-1] for l in css_fh.readlines()]
self._html.append("</style>")
def html(self):
"""Return a Raw HTML string for the plot"""
return "\n".join(self._html)
|