Skip to content

Commit fc16375

Browse files
committed
Add an option to plot virtual memory instead of real memory
1 parent be9581f commit fc16375

File tree

1 file changed

+21
-12
lines changed

1 file changed

+21
-12
lines changed

psrecord/main.py

Lines changed: 21 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,10 @@ def main():
7575
parser.add_argument('--plot', type=str,
7676
help='output the statistics to a plot')
7777

78+
parser.add_argument('--plot-virtual-memory',
79+
help='plot virtual memory also along with real memory',
80+
action='store_true')
81+
7882
parser.add_argument('--duration', type=float,
7983
help='how long to record for (in seconds). If not '
8084
'specified, the recording is continuous until '
@@ -105,15 +109,17 @@ def main():
105109
sprocess = subprocess.Popen(command, shell=True)
106110
pid = sprocess.pid
107111

108-
monitor(pid, logfile=args.log, plot=args.plot, duration=args.duration,
109-
interval=args.interval, include_children=args.include_children)
112+
monitor(pid, logfile=args.log, plot=args.plot,
113+
plot_virtual_memory=args.plot_virtual_memory,
114+
duration=args.duration, interval=args.interval,
115+
include_children=args.include_children)
110116

111117
if sprocess is not None:
112118
sprocess.kill()
113119

114120

115-
def monitor(pid, logfile=None, plot=None, duration=None, interval=None,
116-
include_children=False):
121+
def monitor(pid, logfile=None, plot=None, plot_virtual_memory=None,
122+
duration=None, interval=None, include_children=False):
117123

118124
pr = psutil.Process(pid)
119125

@@ -209,20 +215,23 @@ def monitor(pid, logfile=None, plot=None, duration=None, interval=None,
209215
import matplotlib.pyplot as plt
210216

211217
fig = plt.figure()
212-
ax = fig.add_subplot(1, 1, 1)
213-
214-
ax.plot(log['times'], log['cpu'], '-', lw=1, color='r')
215218

219+
ax = fig.add_subplot(1, 1, 1)
220+
ax.plot(log['times'], log['cpu'], '-', lw=1, color='r', label='cpu%')
216221
ax.set_ylabel('CPU (%)', color='r')
217222
ax.set_xlabel('time (s)')
218223
ax.set_ylim(0., max(log['cpu']) * 1.2)
224+
ax.legend(loc=2)
219225

220226
ax2 = ax.twinx()
221-
222-
ax2.plot(log['times'], log['mem_real'], '-', lw=1, color='b')
223-
ax2.set_ylim(0., max(log['mem_real']) * 1.2)
224-
225-
ax2.set_ylabel('Real Memory (MB)', color='b')
227+
ax2.plot(log['times'], log['mem_real'], '-', lw=1, color='b', label='real')
228+
if plot_virtual_memory:
229+
ax2.plot(log['times'], log['mem_virtual'], '-', lw=1, color='g', label='virt')
230+
ax2.set_ylim(0., max(log['mem_virtual']) * 1.0)
231+
else:
232+
ax2.set_ylim(0., max(log['mem_real']) * 1.2)
233+
ax2.set_ylabel('Memory (MB)', color='b')
234+
ax2.legend(loc=1)
226235

227236
ax.grid()
228237

0 commit comments

Comments
 (0)