simple_plotter.py 8.56 KB
Newer Older
Mario Hock's avatar
Mario Hock committed
1
2
3
4
#!/usr/bin/env python3
# -*- coding:utf-8 -*-

import sys
5
import matplotlib
Mario Hock's avatar
Mario Hock committed
6

7
from cnl_library import CNLParser, calc_ema, merge_lists, pretty_json
8
9
from plot_cpu import plot_top_cpus

Mario Hock's avatar
Mario Hock committed
10

Mario Hock's avatar
Mario Hock committed
11
12
13
## matplotlib.use('QT4Agg')  # override matplotlibrc
import matplotlib.pyplot as plt

Mario Hock's avatar
Mario Hock committed
14

Mario Hock's avatar
Mario Hock committed
15
16
17
18
19
20
21
22
23
24
25
def append_twice(base_list, extend_list):
    if ( isinstance(extend_list, list) ):
        for x in extend_list:
            base_list.append(x)
            base_list.append(x)
    else:
        base_list.append(extend_list)
        base_list.append(extend_list)



Mario Hock's avatar
Mario Hock committed
26
27


Mario Hock's avatar
Mario Hock committed
28
def parse_cnl_file(filename):
Mario Hock's avatar
Mario Hock committed
29
30
31
    ## * Parse input file. *
    cnl_file = CNLParser(filename)

32
    ## Prepare data for matplotlib
Mario Hock's avatar
Mario Hock committed
33

Mario Hock's avatar
Mario Hock committed
34
    #nics = cnl_file.get_nics()
Mario Hock's avatar
Mario Hock committed
35
    nics = ("eth1", "eth2")  ## XXX
36
37
    net_cols = list()
    nic_fields = [".send", ".receive"]
Mario Hock's avatar
Mario Hock committed
38
    for nic_name in nics:
39
40
        for nic_field in nic_fields:
            net_cols.append( nic_name + nic_field )
Mario Hock's avatar
Mario Hock committed
41

42
    cpu_cols = [ cpu_name + ".util" for cpu_name in cnl_file.get_cpus() ]
43
    #cpu_cols = [ cpu_name + ".irq" for cpu_name in cnl_file.get_cpus() ]   ## XXX
44
45

    cols = cnl_file.get_csv_columns()
Mario Hock's avatar
Mario Hock committed
46
    #x_values = cols["end"]
Mario Hock's avatar
Mario Hock committed
47
    #print( cols )   ## XXX
Mario Hock's avatar
Mario Hock committed
48
49


Mario Hock's avatar
Mario Hock committed
50
51
52
53
    ## Augment cnl_file with processed data.
    cnl_file.cols = cols
    cnl_file.net_col_names = net_cols
    cnl_file.cpu_col_names = cpu_cols
Mario Hock's avatar
Mario Hock committed
54
    #cnl_file.x_values = x_values
Mario Hock's avatar
Mario Hock committed
55

Mario Hock's avatar
Mario Hock committed
56
    return cnl_file
Mario Hock's avatar
Mario Hock committed
57

Mario Hock's avatar
Mario Hock committed
58

Mario Hock's avatar
Mario Hock committed
59
60
def get_min_max_x(cnl_file):
    return ( cnl_file.cols["begin"][0], cnl_file.cols["end"][-1] )
Mario Hock's avatar
Mario Hock committed
61
62


63
def plot(ax, x_values, cols, active_cols, alpha, **kwargs):
Mario Hock's avatar
Mario Hock committed
64
    #use_ema = kwargs.get("use_ema")
Mario Hock's avatar
Mario Hock committed
65
66
    ema_only = kwargs.get("ema_only")
    smooth = kwargs.get("smooth")
Mario Hock's avatar
Mario Hock committed
67
68
69
70
71
72
73

    for col_name in active_cols:
        data = cols[col_name]
        if ( len(x_values) == len(data)*2 ):
            data = merge_lists( data, data )

        # * plot *
Mario Hock's avatar
Mario Hock committed
74
75
        if ( not ema_only ):
            ax.plot(x_values , data, label=col_name, alpha=alpha)
Mario Hock's avatar
Mario Hock committed
76
77

        ## plot ema
Mario Hock's avatar
Mario Hock committed
78
79
        if ( ema_only and smooth ):
            ax.plot(x_values , calc_ema(data, smooth), label=col_name)
Mario Hock's avatar
Mario Hock committed
80
81


82
def plot_net(ax, cnl_file, alpha, legend_outside=True):
Mario Hock's avatar
Mario Hock committed
83
84
    ax.set_ylim(0,10**10)
    ax.set_ylabel('Throughput (Bit/s)')
85

86
    plot(ax, cnl_file.x_values, cnl_file.cols, cnl_file.net_col_names, alpha)
87

88
89
90
91
92
93
94
95
96
97
    # Legend
    if ( legend_outside ):
        offset = matplotlib.transforms.ScaledTranslation(0, -20, matplotlib.transforms.IdentityTransform())
        trans = ax.transAxes + offset

        l = ax.legend( loc='upper left', bbox_to_anchor=(0, 0), ncol=int(len(cnl_file.net_col_names)/2),
                      bbox_transform = trans,
                      fancybox=False, shadow=False)
    else:
        l = ax.legend(loc=0)
Mario Hock's avatar
Mario Hock committed
98

99

Mario Hock's avatar
Mario Hock committed
100
101
102
103
104
105
106
def plot_cpu(ax, cnl_file, args):
    # parameters
    legend_outside = True
    alpha = args.opacity if args.transparent_cpu else 1.0
    smooth = args.smooth_cpu

    # axes
Mario Hock's avatar
Mario Hock committed
107
108
    ax.set_ylim(0,100)
    ax.set_ylabel('CPU util (%)')
Mario Hock's avatar
Mario Hock committed
109

Mario Hock's avatar
Mario Hock committed
110
111
112
    # * plot *
    plot(ax, cnl_file.x_values, cnl_file.cols, cnl_file.cpu_col_names, alpha,
         ema_only=True if smooth else False, smooth=smooth)
Mario Hock's avatar
Mario Hock committed
113

114
115
116
117
118
119
120
121
122
123
124
    # Legend
    if ( legend_outside ):
        offset = matplotlib.transforms.ScaledTranslation(0, -20, matplotlib.transforms.IdentityTransform())
        trans = ax.transAxes + offset

        l = ax.legend( loc='upper left', bbox_to_anchor=(0, 0), ncol=int(len(cnl_file.cpu_col_names)/2),
                      bbox_transform = trans,
                      fancybox=False, shadow=False)
    else:
        l = ax.legend(loc=0)

Mario Hock's avatar
Mario Hock committed
125
    #ax.set_label("Testlabel")
Mario Hock's avatar
Mario Hock committed
126

127
    l.draggable(True)
Mario Hock's avatar
Mario Hock committed
128
129
130
131
132
133



## MAIN ##
if __name__ == "__main__":

Mario Hock's avatar
Mario Hock committed
134
135
136
    ## Command line arguments
    import argparse

Mario Hock's avatar
Mario Hock committed
137
138
139
140

    DEFAULT_OPACITY = 0.7
    DEFAULT_ALPHA = 0.1             # alpha for ema, the smaller the smoother

Mario Hock's avatar
Mario Hock committed
141
142
143
    parser = argparse.ArgumentParser()

    parser.add_argument("files", nargs='*')
144
145
146
    parser.add_argument("-tn", "--transparent-net", action="store_true")
    parser.add_argument("-tc", "--transparent-cpu", action="store_true")
    parser.add_argument("-t", "--transparent", action="store_true",
Mario Hock's avatar
Mario Hock committed
147
148
                        help="Implies --transparent-net and --transparent-cpu")
    parser.add_argument("--opacity", type=float, default=DEFAULT_OPACITY,
149
                        help="Default: 0.7")
Mario Hock's avatar
Mario Hock committed
150
    parser.add_argument("-nc", "--no-comment", action="store_true")
Mario Hock's avatar
Mario Hock committed
151
    parser.add_argument("-p", "--publication", action="store_true",
Mario Hock's avatar
Mario Hock committed
152
153
                        help="Reduces the margins so that the output is more suitable for publications and presentations. (Implies --no-comment)")

Mario Hock's avatar
Mario Hock committed
154
155
156
157
    parser.add_argument("-sc", "--smooth-cpu", nargs='?', const=DEFAULT_ALPHA, type=float,
                        metavar="ALPHA",
                        help = "Smooth CPU values with exponential moving average. (Disabled by default. When specified without parameter: ALPHA=0.1)" )

Mario Hock's avatar
Mario Hock committed
158
159
160
161
162
    ## TODO implement (maybe set as default)
    parser.add_argument("-a", "--all-matches", action="store_true",
                        help="Finds all matches current directory (or in --files, if specified) and plots them pairwise.")


Mario Hock's avatar
Mario Hock committed
163
164
165
    args = parser.parse_args()


Mario Hock's avatar
Mario Hock committed
166
167
168
169
170
171
    ## set implicated options
    # --transparent
    if ( args.transparent ):
        args.transparent_cpu = True
        args.transparent_net = True

Mario Hock's avatar
Mario Hock committed
172
173
174
175
    # --publication
    if ( args.publication ):
        args.no_comment = True

Mario Hock's avatar
Mario Hock committed
176

Mario Hock's avatar
Mario Hock committed
177
    num_files = len(args.files)
Mario Hock's avatar
Mario Hock committed
178
179
180
181
182

    ## Create figure (window/file)
    fig = plt.figure()
    fig.canvas.set_window_title('CPUnetPlot')

183
184
    num_cols = 2

Mario Hock's avatar
Mario Hock committed
185
186
187
    min_x = None
    max_x = None

Mario Hock's avatar
Mario Hock committed
188
189
    old_ax_net = None
    old_ax_cpu = None
Mario Hock's avatar
Mario Hock committed
190
    for i in range(0, num_files):
191
        ## Read file
Mario Hock's avatar
Mario Hock committed
192
        filename = args.files[i]
193
194
        cnl_file = parse_cnl_file(filename)

Mario Hock's avatar
Mario Hock committed
195
196
197
198
199
200
201
202
203
204
205
        ## update min_x / max_x
        min_max = get_min_max_x(cnl_file)

        if ( not min_x or min_x > min_max[0] ):
            min_x = min_max[0]

        if ( not max_x or max_x < min_max[1] ):
            max_x = min_max[1]


        ## show some output
206
207
208
209
        print( filename )
        print( pretty_json(cnl_file.get_general_header()) )
        print()

Mario Hock's avatar
Mario Hock committed
210

211
212
213
        ## Plot with matplotlib.

        ## Draw comment on the figure (use absolute positioning).
Mario Hock's avatar
Mario Hock committed
214
215
216
        if ( not args.no_comment ):
            t = matplotlib.text.Text(10,10, "Comment: " + cnl_file.get_comment(), figure=fig)
            fig.texts.append(t)
Mario Hock's avatar
Mario Hock committed
217

Mario Hock's avatar
Mario Hock committed
218

219
        ## Prepare subplots
Mario Hock's avatar
Mario Hock committed
220
221
        ax_net = fig.add_subplot(2, num_cols, i+1, sharex=old_ax_net, sharey=old_ax_net)
        ax_cpu = fig.add_subplot(2, num_cols, i+num_cols+1, sharex=ax_net, sharey=old_ax_cpu)
Mario Hock's avatar
Mario Hock committed
222
223
224
225
226
227
228
229
230
231
        #ax_net = fig.add_subplot(111)  ## twin axis
        #ax_cpu = ax_net.twinx()        ## twin axis


        ## Prepare x_values
        plateau = True      ## XXX
        if ( plateau ):
            cnl_file.x_values = merge_lists( cnl_file.cols["begin"], cnl_file.cols["end"] )
        else:
            cnl_file.x_values = cnl_file.cols["end"]
232

233
        ## Plot
234
        plot_net(ax_net, cnl_file, args.opacity if args.transparent_net else 1.0)
Mario Hock's avatar
Mario Hock committed
235
        plot_cpu(ax_cpu, cnl_file, args)
Mario Hock's avatar
Mario Hock committed
236

Mario Hock's avatar
Mario Hock committed
237
238
239
240
        old_ax_net = ax_net
        old_ax_cpu = ax_cpu


Mario Hock's avatar
Mario Hock committed
241
242
243
244
245
        ## set min/max (remember: The x-axis is shared.)
        margin = max( (max_x - min_x) * 0.03, 10 )
        ax_net.set_xlim(min_x - margin, max_x + margin)


246
247
248
249
250
251
252
253
    ## If we have only one input file, plot CPU area charts.
    if ( num_files == 1 ):
        ax1 = fig.add_subplot(2, num_cols, 2, sharex=old_ax_net, sharey=old_ax_cpu)
        ax2 = fig.add_subplot(2, num_cols, 4, sharex=ax_net, sharey=old_ax_cpu)

        plot_top_cpus( cnl_file, (ax1, ax2), (0,1) )


Mario Hock's avatar
Mario Hock committed
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
    ## Subplot-Layout (margins)
    #  NOTE: This actually works great with a screen resolution of 1920x1200.
    #        Since all space here are in relative size, this might have to be adjusted for other screen resolutions.
    if ( args.publication ):
        # Narrow layout for publications and presentations
        if ( num_files == 1 ):
            # CPU area charts
            fig.subplots_adjust(left=0.03, wspace=0.15, right=0.93, top=0.97, hspace=0.3, bottom=0.08)
        else:
            # double plot
            fig.subplots_adjust(left=0.03, wspace=0.15, right=0.99, top=0.97, hspace=0.3, bottom=0.08)
    else:
        # Regular layout (for good readability on screen)
        fig.subplots_adjust(left=0.1, wspace=0.2, right=0.9, top=0.92, hspace=0.4, bottom=0.12)


270

271
272
273
    ## Set the default format for the save-botton to PDF.
    try:
        fig.canvas.get_default_filetype = lambda: "pdf"
Mario Hock's avatar
..    
Mario Hock committed
274
        fig.canvas.get_default_filename = lambda: "cpunetlog.pdf"   ## TODO suggest a filename
275
276
277
    except:
        pass

Mario Hock's avatar
Mario Hock committed
278
    ## maximize window
Mario Hock's avatar
Mario Hock committed
279
280
281
282
283
    try:
        figManager = plt.get_current_fig_manager()
        figManager.window.showMaximized()
    except:
        pass
Mario Hock's avatar
Mario Hock committed
284
285

    # show plot
Mario Hock's avatar
Mario Hock committed
286
    plt.show()