5 Remove from the arguments the presence of the profiler and its output in
6 order to relaunch the script w/o infinite loops.
8 >>> getArgsWithoutoProfilerInfo(['--profilerName', 'igprof', 'myopts.py'])
11 >>> getArgsWithoutoProfilerInfo(['--profilerName=igprof', 'myopts.py'])
14 >>> getArgsWithoutoProfilerInfo(['--profilerName', 'igprof', '--profilerExtraOptions', 'a b c', 'myopts.py'])
17 >>> getArgsWithoutoProfilerInfo(['--profilerName', 'igprof', '--options', 'a b c', 'myopts.py'])
18 ['--options', 'a b c', 'myopts.py']
24 if o.startswith(
'--profile'):
33 if __name__ ==
"__main__":
35 from optparse
import OptionParser
36 parser = OptionParser(usage =
"%prog [options] <opts_file> ...")
37 parser.add_option(
"-n",
"--dry-run", action=
"store_true",
38 help=
"do not run the application, just parse option files")
39 parser.add_option(
"-p",
"--pickle-output", action=
"store", type=
"string",
41 help=
"DEPRECATED: use '--output file.pkl' instead. Write "
42 "the parsed options as a pickle file (static option "
44 parser.add_option(
"-v",
"--verbose", action=
"store_true",
45 help=
"print the parsed options")
46 parser.add_option(
"--old-opts", action=
"store_true",
47 help=
"format printed options in old option files style")
48 parser.add_option(
"--all-opts", action=
"store_true",
49 help=
"print all the option (even if equal to default)")
55 parser.add_option(
"--ncpus", action=
"store", type=
"int", default=0,
56 help=
"start the application in parallel mode using NCPUS processes. "
57 "0 => serial mode (default), -1 => use all CPUs")
60 """Add the option line to a list together with its position in the
63 parser.values.options.append((len(parser.largs), value))
64 parser.add_option(
"--option", action=
"callback", callback=option_cb,
65 type =
"string", nargs = 1,
66 help=
"add a single line (Python) option to the configuration. "
67 "All options lines are executed, one after the other, in "
69 parser.add_option(
"--no-conf-user-apply", action=
"store_true",
70 help=
"disable the automatic application of configurable "
71 "users (for backward compatibility)")
72 parser.add_option(
"-o",
"--output", action =
"store", type =
"string",
73 help =
"dump the configuration to a file. The format of "
74 "the options is determined by the extension of the "
75 "file name: .pkl = pickle, .py = python, .opts = "
76 "old style options. The python format cannot be "
77 "used to run the application and it contains the "
78 "same dictionary printed with -v")
79 parser.add_option(
"--post-option", action=
"append", type=
"string",
81 help=
"Python options to be executed after the ConfigurableUser "
83 "All options lines are executed, one after the other, in "
85 parser.add_option(
"--debug", action=
"store_true",
86 help=
"enable some debug print-out")
87 parser.add_option(
"--printsequence", action=
"store_true",
88 help=
"print the sequence")
89 if not sys.platform.startswith(
"win"):
91 parser.add_option(
"-T",
"--tcmalloc", action=
"store_true",
92 help=
"Use the Google malloc replacement. The environment "
93 "variable TCMALLOCLIB can be used to specify a different "
94 "name for the library (the default is libtcmalloc.so)")
95 parser.add_option(
"--preload", action=
"append",
96 help=
"Allow pre-loading of special libraries (e.g. Google "
97 "profiling libraries).")
100 parser.add_option(
"--profilerName", type=
"string",
101 help=
"Select one profiler among: igprofPerf, igprofMem and valgrind<toolname>")
104 parser.add_option(
"--profilerOutput", type=
"string",
105 help=
"Specify the name of the output file for the profiler output")
108 parser.add_option(
"--profilerExtraOptions", type=
"string",
109 help=
"Specify additional options for the profiler. The '--' string should be expressed as '__' (--my-opt becomes __my-opt)")
111 parser.set_defaults(options = [],
115 profilerExtraOptions =
'',
119 opts, args = parser.parse_args()
125 from multiprocessing
import cpu_count
126 sys_cpus = cpu_count()
127 if opts.ncpus > sys_cpus:
128 s =
"Invalid value : --ncpus : only %i cpus available" % sys_cpus
130 elif opts.ncpus < -1 :
131 s =
"Invalid value : --ncpus must be integer >= -1"
141 if opts.old_opts: prefix =
"// "
145 level = logging.DEBUG
147 root_logger = logging.getLogger()
151 opts.preload.insert(0, os.environ.get(
"TCMALLOCLIB",
"libtcmalloc.so"))
154 preload = os.environ.get(
"LD_PRELOAD",
"")
156 preload = preload.replace(
" ",
":").split(
":")
159 for libname
in set(preload).intersection(opts.preload):
160 logging.warning(
"Ignoring preload of library %s because it is "
161 "already in LD_PRELOAD.", libname)
163 for libname
in opts.preload
164 if libname
not in set(preload)]
167 preload =
":".join(preload)
168 os.environ[
"LD_PRELOAD"] = preload
169 logging.info(
"Restarting with LD_PRELOAD='%s'", preload)
172 args = [ a
for a
in sys.argv
if a !=
'-T' and not '--tcmalloc'.startswith(a) ]
173 os.execv(sys.executable, [sys.executable] + args)
176 if opts.profilerName:
177 profilerName = opts.profilerName
178 profilerExecName =
""
179 profilerOutput = opts.profilerOutput
or (profilerName +
".output")
184 igprofPerfOptions =
"-d -pp -z -o igprof.pp.gz".split()
187 if profilerName ==
"igprof":
188 if not opts.profilerOutput:
189 profilerOutput +=
".profile.gz"
190 profilerOptions =
"-d -z -o %s" % profilerOutput
191 profilerExecName =
"igprof"
193 elif profilerName ==
"igprofPerf":
194 if not opts.profilerOutput:
195 profilerOutput +=
".pp.gz"
196 profilerOptions =
"-d -pp -z -o %s" % profilerOutput
197 profilerExecName =
"igprof"
199 elif profilerName ==
"igprofMem":
200 if not opts.profilerOutput:
201 profilerOutput +=
".mp.gz"
202 profilerOptions =
"-d -mp -z -o %s" % profilerOutput
203 profilerExecName =
"igprof"
205 elif "valgrind" in profilerName:
207 if not opts.profilerOutput:
208 profilerOutput +=
".log"
209 toolname = profilerName.replace(
'valgrind',
'')
210 outoption =
"--log-file"
211 if toolname
in (
"massif",
"callgrind",
"cachegrind"):
212 outoption =
"--%s-out-file" % toolname
213 profilerOptions =
"--tool=%s %s=%s" % (toolname, outoption, profilerOutput)
214 profilerExecName =
"valgrind"
217 root_logger.warning(
"Profiler %s not recognized!" % profilerName)
220 if opts.profilerExtraOptions!=
"":
221 profilerExtraOptions = opts.profilerExtraOptions
222 profilerExtraOptions = profilerExtraOptions.replace(
"__",
"--")
223 profilerOptions +=
" %s" % profilerExtraOptions
226 import distutils.spawn
227 profilerPath = distutils.spawn.find_executable(profilerExecName)
229 root_logger.error(
"Cannot locate profiler %s" % profilerExecName)
232 root_logger.info(
"------ Profiling options are on ------ \n"\
234 " o Options: '%s'.\n"\
235 " o Output: %s" % (profilerExecName, profilerOptions, profilerOutput))
238 profilerOptions +=
" python"
241 arglist = [profilerPath] + profilerOptions.split() + args
242 arglist = [ a
for a
in arglist
if a!=
'' ]
246 os.execv(profilerPath, arglist)
250 if opts.pickle_output:
252 root_logger.error(
"Conflicting options: use only --pickle-output or --output")
255 root_logger.warning(
"--pickle-output is deprecated, use --output instead")
256 opts.output = opts.pickle_output
264 options = [
"importOptions(%r)" % f
for f
in args ]
267 optlines = list(opts.options)
269 for pos, l
in optlines:
270 options.insert(pos,l)
278 sys.modules[
"GaudiPython"] =
FakeModule(RuntimeError(
"GaudiPython cannot be used in option files"))
284 exec
"from Gaudi.Configuration import *" in g, l
289 import GaudiKernel.Proxy.Configurable
290 if opts.no_conf_user_apply:
291 logging.info(
"Disabling automatic apply of ConfigurableUser")
293 GaudiKernel.Proxy.Configurable._appliedConfigurableUsers_ =
True
296 from GaudiKernel.Proxy.Configurable
import applyConfigurableUsers
300 if opts.post_options:
303 exec
"from Gaudi.Configuration import *" in g, l
304 for o
in opts.post_options:
309 c.printconfig(opts.old_opts, opts.all_opts)
311 c.writeconfig(opts.output, opts.all_opts)
313 c.printsequence = opts.printsequence
314 if opts.printsequence:
316 logging.warning(
"--printsequence not supported with --ncpus: ignored")
318 logging.warning(
"--printsequence not supported with --dry-run: ignored")
321 del sys.modules[
"GaudiPython"]
325 sys.exit(c.run(opts.ncpus))