forked from pythonprofilers/memory_profiler
-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Fabian Pedregosa
committed
Oct 14, 2011
0 parents
commit e89dd84
Showing
3 changed files
with
109 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
Some utilities to monitor process memory usage | ||
|
||
Do not use it. It's work in progress. | ||
|
||
|
||
Frequently Asqued Questions | ||
=========================== | ||
|
||
Q: I am a screech owl with trouble making sounds. | ||
|
||
A: Yes. | ||
|
||
Author: Fabian Pedregosa <fabian@fseoane.net> | ||
License: Simplified BSD |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
""" | ||
Plot memory usage of a numeric computation using numpy and scipy | ||
""" | ||
import numpy as np | ||
from minimon import memory | ||
from scipy import linalg | ||
|
||
X = np.random.randn(1000, 1000) | ||
y = np.random.randn(1000) | ||
mm = memory("linalg.qr_multiply(X, y)", interval=.01, locals=locals()) | ||
mm2 = memory("R, Q = linalg.qr(X);np.dot(Q.T, y)", interval=.01, locals=locals()) | ||
mm1 = (np.array(mm) * 4.) / 1024 | ||
mm2 = (np.array(mm2) * 4.) / 1024 | ||
mm1.resize(mm2.shape) | ||
|
||
import pylab as pl | ||
x = np.linspace(0, np.max(mm1), len(mm1)) | ||
pl.plot(x, mm1, x, mm2, '+', color='black') | ||
pl.fill_between(x, mm1, alpha=0.2, dashes='dotted', linewidth=.1, label='with qr_multiply') | ||
pl.fill_between(x, mm2, alpha=0.2, color='yellow', label='naive computation') | ||
pl.legend() | ||
pl.show() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
""" | ||
Get process information | ||
""" | ||
import time, sys, os | ||
|
||
if sys.platform.startswith('linux'): | ||
def _get_memory(pid): | ||
try: | ||
with open('/proc/%s/statm' % pid) as f: | ||
return int(f.read().split(' ')[1]) | ||
except IOError: | ||
return 0 | ||
else: | ||
# .. | ||
# .. better to be safe than sorry .. | ||
raise NotImplementedError | ||
|
||
def memory(proc= -1, num= -1, interval=.1, locals={}): | ||
""" | ||
Return the memory usage of a process of piece of code | ||
Parameters | ||
---------- | ||
proc : {int, string} | ||
The process to monitor. Can be given by a PID or by a string | ||
containing a filename or the code to be executed. Set to -1 | ||
(default)for current process. | ||
interval : int, optional | ||
num : int, optional | ||
Number of samples to generate. In the case of | ||
defaults to -1, meaning | ||
to wait until the process has finished if proc is a string or | ||
to get just one if proc is an integer. | ||
locals : dict | ||
Local variables. | ||
Returns | ||
------- | ||
mm : list of integers | ||
memory usage, in MB | ||
""" | ||
ret = [] | ||
|
||
if isinstance(proc, str): | ||
if proc.endswith('.py'): | ||
f = open('proc', 'r') | ||
proc = f.read() | ||
f.close() | ||
# TODO: make sure script's directory is on sys.path | ||
from multiprocessing import Process | ||
def f(x, locals): | ||
# function interface for exec | ||
exec x in globals(), locals | ||
|
||
p = Process(target=f, args=(proc, locals)) | ||
p.start() | ||
while p.is_alive(): # FIXME: or num | ||
ret.append(_get_memory(p.pid)) | ||
time.sleep(interval) | ||
else: | ||
if proc == -1: | ||
proc = os.getpid() | ||
for _ in range(num): | ||
ret.append(_get_memory(p.pid)) | ||
time.sleep(interval) | ||
return ret | ||
|
||
|
||
|