/usr/lib/python3/dist-packages/memprof/mp_utils.py is in python3-memprof 0.3.4-1+b2.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
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 | # Copyright (c) 2013 Jose M. Dana
#
# This file is part of memprof.
#
# memprof is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation (version 3 of the License only).
#
# memprof is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with memprof. If not, see <http://www.gnu.org/licenses/>.
import os
import sys
import operator
import matplotlib
matplotlib.use('Agg')
import matplotlib.pyplot as plt
from matplotlib.backends.backend_agg import FigureCanvasAgg
PY3 = sys.version > '3'
KB = 1024.
MB = KB * 1024
GB = MB * 1024
default_threshold = MB
def get_units_factor(threshold):
if threshold < KB:
return ("B", 1.)
elif threshold < MB:
return ("KB", KB)
elif threshold < GB:
return ("MB", MB)
else:
return ("GB", GB)
def gen_plot(logfile, threshold):
cache = {}
times = []
figure = plt.Figure()
canvas = FigureCanvasAgg(figure)
name = os.path.splitext(logfile)[0]
units, factor = get_units_factor(threshold)
ax = figure.add_subplot(1, 1, 1)
ax.set_xlabel('time (s)')
ax.set_ylabel('memory (%s)' % units)
ax.set_title('%s - memprof' % (name))
ax.grid(True)
f = open(logfile, "r")
for line in f:
line = line.strip()
try:
item, size = line.split('\t')
size = float(size)
if size >= threshold:
size /= factor
cache.setdefault(item, []).append(size)
if len(cache[item]) < len(times):
cache[item][-1:-1] = [0] * (len(times) - len(cache[item]))
except ValueError as e:
if line == "RESTART":
ax.axvspan(times[-2], times[-1], facecolor='#000000', alpha=0.5)
ax.axvline(x=times[-2], color='#000000')
ax.axvline(x=times[-1], color='#000000')
else:
times.append(float(line))
f.close()
if not cache.items():
print("\nNothing with more than %.2f %ss found!" % (threshold / factor, units))
print("There is nothing to plot... Exiting")
return
for item, val in cache.items():
# len(s) == len(t) has to be true
s = val + [0] * (len(times) - len(val))
ax.plot(times, s, linewidth=1.5, label=item)
handles, labels = ax.get_legend_handles_labels()
hl = sorted(zip(handles, labels), key=operator.itemgetter(1))
handles, labels = zip(*hl)
maxy = max(map(max, cache.values()))
gapy = (maxy - threshold/factor)/40
ax.set_ylim(max(0, threshold/factor - gapy), maxy + gapy)
gapx = (times[-1] - times[0])/40
ax.set_xlim(times[0], times[-1] + gapx)
box = ax.get_position()
ax.set_position([box.x0, box.y0 + box.height * 0.2, box.width, box.height * 0.8])
figure.legend(handles, labels, bbox_to_anchor=(0.5, -0.12), loc="upper center", ncol=5, borderaxespad=0.)
canvas.print_figure("%s.png" % name)
return figure
|