-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtplot.py
More file actions
199 lines (144 loc) · 4.93 KB
/
tplot.py
File metadata and controls
199 lines (144 loc) · 4.93 KB
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
# -*- coding: utf-8 -*-
"""Plotting of top report generated in work bench.
The purpose of this module is to plot the data as a graph/chart for
further analysis.
Usage:
$ python tplot.py
"""
import numpy as np
import os
import sys
import argparse
import socket
import matplotlib.pyplot as plt
from tplotClient import connect_to_server
csv_header = ['PID', 'USER', 'PR', 'NI', 'VIRT', 'RES', 'SHR', 'S',
'%CPU', '%MEM', 'TIME+', 'COMMAND']
csv_dtypes = "i8,|S10,|S3,f8,|S5,|S5,|S5,|S5,f8,f8,|S10,|S10"
def get_data(filename, header, dtypes):
""" get_data function of module top-plot.
This function yields the array of data for each of the fields in csv
header.
Args:
filename - Name of the csv file to be plotted.
header - list of columns/field in the csv header.
dtypes - The type of each field in the csv header.
Returns:
pltdata - data as numpy data array.
"""
pltdata = np.genfromtxt(filename, delimiter=',', names=header,
dtype=dtypes)
pid = pltdata['PID']
user = pltdata['USER']
pr = pltdata['PR']
ni = pltdata['NI']
virt = pltdata['VIRT']
res = pltdata['RES']
shr = pltdata['SHR']
s = pltdata['S']
cpu = pltdata['CPU']
mem = pltdata['MEM']
t = pltdata['TIME']
cmd = pltdata['COMMAND']
pdata = [pid, user, pr, ni, virt, res, shr, s, cpu, mem, t, cmd]
return pltdata, pdata
def parse_data(data, field, entries, item):
"""parse_data function of module top-plot.
splits into different data from the data taken from csv file.
Args:
data - csv data from the file.
field - field to be processed.
item - item to be compared.
Returns:
dlist - list of parsed data.
"""
if item in data['COMMAND']:
ifield = 'COMMAND'
elif item in data['USER']:
ifield = 'USER'
elif int(item) in data['PID']:
item = int(item)
ifield = 'PID'
plist = [piece for piece in enumerate(data[ifield])]
if field is not 'TIME':
for pitem in plist:
if pitem[1] == item:
entries.append(pitem[0])
dlist = [data[field].item(x) for x in entries]
if type(dlist[0]) is str:
axis = [ax[0] for ax in enumerate(dlist)]
plt.xticks(axis, dlist, rotation=45)
else:
axis = dlist
return entries, axis
def plot_graphic(x, y, gtype):
plt.plot(x, y, '-', x, y, gtype)
plt.draw()
def option_parser():
"""option_parser function of module top-plot.
This function parses the arguments passed to the module.
Args:
None.
Returns:
dict: commandline arguments.
"""
parser = argparse.ArgumentParser(
description="Plotting of top report generated in work bench.")
parser.add_argument('-f', '--filename',
dest='filename',
default='top-report.csv',
help='The filename from which plot to be generated.')
parser.add_argument('-y', '--yfield',
dest='yfield',
default='CPU',
choices=['VIRT', 'RES', 'SHR', 'MEM', 'CPU'],
help='The fields to be used for y axis.')
parser.add_argument('-x', '--xfield',
dest='xfield',
default='TIME',
choices=['PID', 'COMMAND', 'TIME'],
help='The fields to be used for x axis.')
parser.add_argument('-i', '--item',
dest='item',
required=True,
help='The item to be parsed.')
args = parser.parse_args()
return args
def main():
"""main function of module top-plot.
The main entry for getting the data and plotting the data of any two
fields.
"""
options = option_parser()
fname = options.filename
xfld = options.xfield
yfld = options.yfield
itm = options.item
aentry = []
plt.xlabel(xfld)
plt.ylabel(yfld)
plt.title("%s vs %s for %s" % (xfld, yfld, itm))
if os.path.exists(fname):
data, pdata = get_data(fname, csv_header, csv_dtypes)
yentry, yplot = parse_data(data, yfld, aentry, itm)
xentry, xplot = parse_data(data, xfld, yentry, itm)
plot_graphic(xplot, yplot, 'ro')
else:
print "%s File not found" % fname
# get local machine name
host = socket.gethostname()
port = 9999
cli = connect_to_server(host, port)
while True:
# Receive no more than 1024 bytes
data = cli.recv(1024)
reply = 'OK'
if not data:
break
data = data.split('END')[0]
yentry, yplot = parse_data(data, yfld, aentry, itm)
xentry, xplot = parse_data(data, xfld, yentry, itm)
plot_graphic(xplot, yplot, 'ro')
cli.close()
if __name__ == "__main__":
main()