8 Remove from the arguments the presence of the profiler and its output in
9 order to relaunch the script w/o infinite loops.
11 >>> getArgsWithoutoProfilerInfo(['--profilerName', 'igprof', 'myopts.py'])
14 >>> getArgsWithoutoProfilerInfo(['--profilerName=igprof', 'myopts.py'])
17 >>> getArgsWithoutoProfilerInfo(['--profilerName', 'igprof', '--profilerExtraOptions', 'a b c', 'myopts.py'])
20 >>> getArgsWithoutoProfilerInfo(['--profilerName', 'igprof', '--options', 'a b c', 'myopts.py'])
21 ['--options', 'a b c', 'myopts.py']
27 if o.startswith(
'--profile'):
36 Convert the given path to a real path if the pointed file exists, otherwise
39 path = os.path.normpath(os.path.expandvars(path))
40 if os.path.exists(path):
41 path = os.path.realpath(path)
45 _qmt_tmp_opt_files = []
48 Given a .qmt file, return the command line arguments of the corresponding
51 from xml.etree
import ElementTree
as ET
52 global _qmt_tmp_opt_files
54 qmt = ET.parse(qmtfile)
55 args = [a.text
for a
in qmt.findall(
"argument[@name='args']//text")]
56 options = qmt.find(
"argument[@name='options']/text")
58 if options
is not None:
59 from tempfile
import NamedTemporaryFile
61 if re.search(
r"from\s+Gaudi.Configuration\s+import\s+\*"
62 r"|from\s+Configurables\s+import", options.text):
63 tmp_opts = NamedTemporaryFile(suffix=
'.py')
65 tmp_opts = NamedTemporaryFile(suffix=
'.opts')
66 tmp_opts.write(options.text)
68 args.append(tmp_opts.name)
69 _qmt_tmp_opt_files.append(tmp_opts)
73 qmtfile = os.path.abspath(qmtfile)
74 if 'qmtest' in qmtfile.split(os.path.sep):
77 while os.path.basename(testdir) !=
'qmtest':
78 testdir = os.path.dirname(testdir)
84 args =
map(rationalizepath, args)
90 if __name__ ==
"__main__":
92 if os.environ.get(
'LC_ALL') !=
'C':
93 print '# setting LC_ALL to "C"'
94 os.environ[
'LC_ALL'] =
'C'
96 from optparse
import OptionParser
97 parser = OptionParser(usage =
"%prog [options] <opts_file> ...")
98 parser.add_option(
"-n",
"--dry-run", action=
"store_true",
99 help=
"do not run the application, just parse option files")
100 parser.add_option(
"-p",
"--pickle-output", action=
"store", type=
"string",
102 help=
"DEPRECATED: use '--output file.pkl' instead. Write "
103 "the parsed options as a pickle file (static option "
105 parser.add_option(
"-v",
"--verbose", action=
"store_true",
106 help=
"print the parsed options")
107 parser.add_option(
"--old-opts", action=
"store_true",
108 help=
"format printed options in old option files style")
109 parser.add_option(
"--all-opts", action=
"store_true",
110 help=
"print all the option (even if equal to default)")
116 parser.add_option(
"--ncpus", action=
"store", type=
"int", default=0,
117 help=
"start the application in parallel mode using NCPUS processes. "
118 "0 => serial mode (default), -1 => use all CPUs")
121 """Add the option line to a list together with its position in the
124 parser.values.options.append((len(parser.largs), value))
125 parser.add_option(
"--option", action=
"callback", callback=option_cb,
126 type =
"string", nargs = 1,
127 help=
"add a single line (Python) option to the configuration. "
128 "All options lines are executed, one after the other, in "
130 parser.add_option(
"--no-conf-user-apply", action=
"store_true",
131 help=
"disable the automatic application of configurable "
132 "users (for backward compatibility)")
133 parser.add_option(
"-o",
"--output", action =
"store", type =
"string",
134 help =
"dump the configuration to a file. The format of "
135 "the options is determined by the extension of the "
136 "file name: .pkl = pickle, .py = python, .opts = "
137 "old style options. The python format cannot be "
138 "used to run the application and it contains the "
139 "same dictionary printed with -v")
140 parser.add_option(
"--post-option", action=
"append", type=
"string",
142 help=
"Python options to be executed after the ConfigurableUser "
144 "All options lines are executed, one after the other, in "
146 parser.add_option(
"--debug", action=
"store_true",
147 help=
"enable some debug print-out")
148 parser.add_option(
"--printsequence", action=
"store_true",
149 help=
"print the sequence")
150 if not sys.platform.startswith(
"win"):
152 parser.add_option(
"-T",
"--tcmalloc", action=
"store_true",
153 help=
"Use the Google malloc replacement. The environment "
154 "variable TCMALLOCLIB can be used to specify a different "
155 "name for the library (the default is libtcmalloc.so)")
156 parser.add_option(
"--preload", action=
"append",
157 help=
"Allow pre-loading of special libraries (e.g. Google "
158 "profiling libraries).")
161 parser.add_option(
"--profilerName", type=
"string",
162 help=
"Select one profiler among: igprofPerf, igprofMem and valgrind<toolname>")
165 parser.add_option(
"--profilerOutput", type=
"string",
166 help=
"Specify the name of the output file for the profiler output")
169 parser.add_option(
"--profilerExtraOptions", type=
"string",
170 help=
"Specify additional options for the profiler. The '--' string should be expressed as '__' (--my-opt becomes __my-opt)")
172 parser.set_defaults(options = [],
176 profilerExtraOptions =
'',
182 for a
in sys.argv[1:]:
183 if a.endswith(
'.qmt')
and os.path.exists(a):
187 if argv != sys.argv[1:]:
188 print '# Running', sys.argv[0],
'with arguments', argv
190 opts, args = parser.parse_args(args=argv)
196 from multiprocessing
import cpu_count
197 sys_cpus = cpu_count()
198 if opts.ncpus > sys_cpus:
199 s =
"Invalid value : --ncpus : only %i cpus available" % sys_cpus
201 elif opts.ncpus < -1 :
202 s =
"Invalid value : --ncpus must be integer >= -1"
212 if opts.old_opts: prefix =
"// "
216 level = logging.DEBUG
218 root_logger = logging.getLogger()
222 opts.preload.insert(0, os.environ.get(
"TCMALLOCLIB",
"libtcmalloc.so"))
225 preload = os.environ.get(
"LD_PRELOAD",
"")
227 preload = preload.replace(
" ",
":").split(
":")
230 for libname
in set(preload).intersection(opts.preload):
231 logging.warning(
"Ignoring preload of library %s because it is "
232 "already in LD_PRELOAD.", libname)
234 for libname
in opts.preload
235 if libname
not in set(preload)]
238 preload =
":".join(preload)
239 os.environ[
"LD_PRELOAD"] = preload
240 logging.info(
"Restarting with LD_PRELOAD='%s'", preload)
243 args = [ a
for a
in sys.argv
if a !=
'-T' and not '--tcmalloc'.startswith(a) ]
244 os.execv(sys.executable, [sys.executable] + args)
247 if opts.profilerName:
248 profilerName = opts.profilerName
249 profilerExecName =
""
250 profilerOutput = opts.profilerOutput
or (profilerName +
".output")
255 igprofPerfOptions =
"-d -pp -z -o igprof.pp.gz".split()
258 if profilerName ==
"igprof":
259 if not opts.profilerOutput:
260 profilerOutput +=
".profile.gz"
261 profilerOptions =
"-d -z -o %s" % profilerOutput
262 profilerExecName =
"igprof"
264 elif profilerName ==
"igprofPerf":
265 if not opts.profilerOutput:
266 profilerOutput +=
".pp.gz"
267 profilerOptions =
"-d -pp -z -o %s" % profilerOutput
268 profilerExecName =
"igprof"
270 elif profilerName ==
"igprofMem":
271 if not opts.profilerOutput:
272 profilerOutput +=
".mp.gz"
273 profilerOptions =
"-d -mp -z -o %s" % profilerOutput
274 profilerExecName =
"igprof"
276 elif "valgrind" in profilerName:
278 if not opts.profilerOutput:
279 profilerOutput +=
".log"
280 toolname = profilerName.replace(
'valgrind',
'')
281 outoption =
"--log-file"
282 if toolname
in (
"massif",
"callgrind",
"cachegrind"):
283 outoption =
"--%s-out-file" % toolname
284 profilerOptions =
"--tool=%s %s=%s" % (toolname, outoption, profilerOutput)
285 profilerExecName =
"valgrind"
288 root_logger.warning(
"Profiler %s not recognized!" % profilerName)
291 if opts.profilerExtraOptions!=
"":
292 profilerExtraOptions = opts.profilerExtraOptions
293 profilerExtraOptions = profilerExtraOptions.replace(
"__",
"--")
294 profilerOptions +=
" %s" % profilerExtraOptions
297 import distutils.spawn
298 profilerPath = distutils.spawn.find_executable(profilerExecName)
300 root_logger.error(
"Cannot locate profiler %s" % profilerExecName)
303 root_logger.info(
"------ Profiling options are on ------ \n"\
305 " o Options: '%s'.\n"\
306 " o Output: %s" % (profilerExecName, profilerOptions, profilerOutput))
309 profilerOptions +=
" python"
312 arglist = [profilerPath] + profilerOptions.split() + args
313 arglist = [ a
for a
in arglist
if a!=
'' ]
317 os.execv(profilerPath, arglist)
321 if opts.pickle_output:
323 root_logger.error(
"Conflicting options: use only --pickle-output or --output")
326 root_logger.warning(
"--pickle-output is deprecated, use --output instead")
327 opts.output = opts.pickle_output
335 options = [
"importOptions(%r)" % f
for f
in args ]
338 optlines = list(opts.options)
340 for pos, l
in optlines:
341 options.insert(pos,l)
349 sys.modules[
"GaudiPython"] =
FakeModule(RuntimeError(
"GaudiPython cannot be used in option files"))
355 exec
"from Gaudi.Configuration import *" in g, l
360 import GaudiKernel.Proxy.Configurable
361 if opts.no_conf_user_apply:
362 logging.info(
"Disabling automatic apply of ConfigurableUser")
364 GaudiKernel.Proxy.Configurable._appliedConfigurableUsers_ =
True
367 from GaudiKernel.Proxy.Configurable
import applyConfigurableUsers
371 if opts.post_options:
374 exec
"from Gaudi.Configuration import *" in g, l
375 for o
in opts.post_options:
380 c.printconfig(opts.old_opts, opts.all_opts)
382 c.writeconfig(opts.output, opts.all_opts)
384 c.printsequence = opts.printsequence
385 if opts.printsequence:
387 logging.warning(
"--printsequence not supported with --ncpus: ignored")
389 logging.warning(
"--printsequence not supported with --dry-run: ignored")
392 del sys.modules[
"GaudiPython"]
396 sys.exit(c.run(opts.ncpus))