simple_plotter.py 3.77 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

Mario Hock's avatar
Mario Hock committed
7
from cnl_library import CNLParser, calc_ema, merge_lists
Mario Hock's avatar
Mario Hock committed
8

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

Mario Hock's avatar
Mario Hock committed
12

Mario Hock's avatar
Mario Hock committed
13
14
15
16
17
18
19
20
21
22
23
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
24
25


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


    ## Display header informations.
    print( cnl_file.get_type() )
Mario Hock's avatar
Mario Hock committed
33
    print( cnl_file.get_comment() )
Mario Hock's avatar
Mario Hock committed
34
35
36
37
38

    print( "CPUs: " + str(cnl_file.get_cpus()) )
    print( "NICs: " + str(cnl_file.get_nics()) )


39
    ## Prepare data for matplotlib
Mario Hock's avatar
Mario Hock committed
40

Mario Hock's avatar
Mario Hock committed
41
    #nics = cnl_file.get_nics()
Mario Hock's avatar
Mario Hock committed
42
    nics = ("eth1", "eth2")  ## XXX
43
44
    net_cols = list()
    nic_fields = [".send", ".receive"]
Mario Hock's avatar
Mario Hock committed
45
    for nic_name in nics:
46
47
        for nic_field in nic_fields:
            net_cols.append( nic_name + nic_field )
Mario Hock's avatar
Mario Hock committed
48

49
50
51
    cpu_cols = [ cpu_name + ".util" for cpu_name in cnl_file.get_cpus() ]

    cols = cnl_file.get_csv_columns()
Mario Hock's avatar
Mario Hock committed
52
    #x_values = cols["end"]
Mario Hock's avatar
Mario Hock committed
53
    #print( cols )   ## XXX
Mario Hock's avatar
Mario Hock committed
54
55


Mario Hock's avatar
Mario Hock committed
56
57
58
59
    ## 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
60
    #cnl_file.x_values = x_values
Mario Hock's avatar
Mario Hock committed
61

Mario Hock's avatar
Mario Hock committed
62
    return cnl_file
Mario Hock's avatar
Mario Hock committed
63

Mario Hock's avatar
Mario Hock committed
64

Mario Hock's avatar
Mario Hock committed
65
66


Mario Hock's avatar
Mario Hock committed
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
def plot(ax, x_values, cols, active_cols, **kwargs):
    #use_ema = kwargs.get("use_ema")

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

        # * plot *
        ax.plot(x_values , data, label=col_name)

        ## plot ema
        #if ( use_ema ):
            #ax.plot(x_values , calc_ema(cols[col_name], 0.2), label=col_name+" (ema)")


def plot_net(ax, cnl_file):
Mario Hock's avatar
Mario Hock committed
84
85
    ax.set_ylim(0,10**10)
    ax.set_ylabel('Throughput (Bit/s)')
86

Mario Hock's avatar
Mario Hock committed
87
    plot(ax, cnl_file.x_values, cnl_file.cols, cnl_file.net_col_names)
88

Mario Hock's avatar
..    
Mario Hock committed
89
90
    ax.legend(loc=0)
    #ax.legend(loc=8)
Mario Hock's avatar
Mario Hock committed
91

92

Mario Hock's avatar
Mario Hock committed
93
def plot_cpu(ax, cnl_file):
Mario Hock's avatar
Mario Hock committed
94
95
    ax.set_ylim(0,100)
    ax.set_ylabel('CPU util (%)')
Mario Hock's avatar
Mario Hock committed
96

Mario Hock's avatar
Mario Hock committed
97
    plot(ax, cnl_file.x_values, cnl_file.cols, cnl_file.cpu_col_names)
Mario Hock's avatar
Mario Hock committed
98

Mario Hock's avatar
..    
Mario Hock committed
99
100
    ax.legend(loc=0)
    #ax.legend(loc=1)
Mario Hock's avatar
Mario Hock committed
101
102
103
104
105
106
107




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

108
    num_files = len(sys.argv) - 1
Mario Hock's avatar
Mario Hock committed
109
110
111
112
113

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

Mario Hock's avatar
Mario Hock committed
114
115
    old_ax_net = None
    old_ax_cpu = None
116
117
118
119
120
121
122
123
124
125
126
    for i in range(1, num_files+1):
        ## Read file
        filename = sys.argv[i]
        print( filename )
        cnl_file = parse_cnl_file(filename)

        ## Plot with matplotlib.

        ## Draw comment on the figure (use absolute positioning).
        t = matplotlib.text.Text(10,10, "Comment: " + cnl_file.get_comment(), figure=fig)
        fig.texts.append(t)
Mario Hock's avatar
Mario Hock committed
127

Mario Hock's avatar
Mario Hock committed
128

129
        ## Prepare subplots
Mario Hock's avatar
Mario Hock committed
130
131
        ax_net = fig.add_subplot(2, num_files, i, sharex=old_ax_net, sharey=old_ax_net)
        ax_cpu = fig.add_subplot(2, num_files, i+num_files, sharex=ax_net, sharey=old_ax_cpu)
Mario Hock's avatar
Mario Hock committed
132
133
134
135
136
137
138
139
140
141
        #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"]
142

143
        ## Plot
Mario Hock's avatar
Mario Hock committed
144
145
        plot_net(ax_net, cnl_file)
        plot_cpu(ax_cpu, cnl_file)
Mario Hock's avatar
Mario Hock committed
146

Mario Hock's avatar
Mario Hock committed
147
148
149
150
        old_ax_net = ax_net
        old_ax_cpu = ax_cpu


Mario Hock's avatar
Mario Hock committed
151
    ## maximize window
Mario Hock's avatar
Mario Hock committed
152
153
154
155
156
157
158
159
    if ( num_files > 1 ):
        try:
            figManager = plt.get_current_fig_manager()
            figManager.window.showMaximized()
        except:
            pass

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