1 """
2 Functions for measuring memory usage, to allow usage to be optimized.
3
4 Examples::
5
6 bin/python -c 'execfile("topo/misc/memuse.py") ; print topsize_mb()'
7
8 ./topographica -c 'from topo.misc import memuse' -c 'print memuse.topsize_mb()'
9
10 ./topographica -c 'from topo.misc import memuse, asizeof' -c 'print memuse.topsize_mb()'
11
12 ./topographica -a -c 'from topo.misc import memuse, asizeof' -c 'print memuse.topsize_mb()'
13
14 ./topographica -a -c 'from topo.misc import memuse, asizeof' examples/tiny.ty -c 'print memuse.allsizes_mb()'
15
16 ./topographica -a -c 'from topo.misc import memuse, asizeof' -c 'memuse.memuse_batch("examples/tiny.ty",cortex_density=20)'
17
18 ./topographica -a -c 'from topo.misc import memuse, asizeof' -c 'memuse.memuse_batch("examples/tiny.ty",times=[0,100],analysis_fn=memuse.plotting_and_saving_analysis_fn,cortex_density=20)'
19
20 $Id: memuse.py 11326 2010-07-30 15:42:45Z jbednar $
21 """
22 __version__='$Revision: 10367 $'
23
24
25
26
27
28
29
30
31
32 import subprocess
33
34
36 """Run a system command as in os.system(), but capture the stdout and return it as a string."""
37 return subprocess.Popen(cmd,shell=True,stdout=subprocess.PIPE).communicate()[0]
38
39
40
42 """Return the RES size of this process as reported by the top(1) command."""
43 import os
44 top_line=cmd_to_string("top -n 1 -b -p %d | grep '^[ ]*%d '" % (os.getpid(),os.getpid()))
45 return top_line.split()[5]
46
47
48
50 """
51 Return the size of topo.sim reported by asizeof.asizeof().
52 This estimate does not currently include any numpy arrays, and
53 may also be missing other important items.
54
55 Python 2.6 supports getsizeof() and a __sizeof__ attribute that user
56 code can implement, which should provide a more accurate estimate.
57 """
58 import asizeof,topo
59 return asizeof.asizeof(topo.sim)
60
61
62
63
64
65
67 """Format the given value in bytes as a string in megabytes"""
68 return "%dMB" % (bytes/1024.0/1024.0)
69
71 """String-formatted version of the RES size of this process reported by top(1)."""
72 return "topsize:%s" % topsize()
73
75 """String-formatted version of the value reported by asizeof(topo.sim)."""
76 return "simsize:%s" % (mb(simsize()))
77
79 """String-formatted version of the memory taken by the weights, from print_sizes()."""
80 from topo.command.basic import n_bytes
81 return "wtsize:%s" % (mb(n_bytes()))
82
84 """
85 Collates results from topsize, simsize, and wtsize.
86
87 Formatted to suggest that the topsize is made up of code (not
88 currently estimated), topo.sim (apart from weights), and weights.
89 """
90 from topo.command.basic import n_bytes
91 return "%s =? code + %s + %s (%s tot)" % (topsize_mb(),simsize_mb(),wtsize_mb(),mb(simsize()+n_bytes()))
92
93
94
95
96
97
102
103
104
118
119
120
122 """
123 Similar to run_batch, but analyzes the memory requirement of a simulation at different times.
124
125 First, the specified script file will be run using the specified parameters.
126 Then at each of the specified times, the given analysis_fn (which
127 calls allsizes_mb() by default) is run. The output is labeled
128 with the script file, time, and parameters so that results from
129 different runs can be compared.
130 """
131 import os,re,__main__,topo
132 from topo.misc.commandline import global_params
133
134
135 scriptbase= re.sub('.ty$','',os.path.basename(script_file))
136 prefix = ""
137
138 prefix += scriptbase
139 simname = prefix
140
141
142 for a,val in params.iteritems():
143
144 valstr= ("_".join([str(i) for i in val]) if isinstance(val,list)
145 else str(val))
146 prefix += "," + a + "=" + valstr
147
148
149 global_params.set_in_context(**params)
150
151
152 try:
153 execfile(script_file,__main__.__dict__)
154 global_params.check_for_unused_names()
155 topo.sim.name=simname
156
157
158 for run_to in times:
159 topo.sim.run(run_to - topo.sim.time())
160 analysis_fn(prefix=prefix+": ")
161
162 except:
163 import traceback,sys
164 traceback.print_exc(file=sys.stdout)
165 sys.stderr.write("Warning -- Error detected: execution halted.\n")
166