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(
"--old-conf-user-apply", action=
"store_true",
134 help=
"use the old logic when applying ConfigurableUsers "
135 "(with bug #103803) [default]")
136 parser.add_option(
"--new-conf-user-apply", action=
"store_false",
137 dest=
"old_conf_user_apply",
138 help=
"use the new (correct) logic when applying "
139 "ConfigurableUsers (fixed bug #103803), can be "
140 "turned on also with the environment variable "
141 "GAUDI_FIXED_APPLY_CONF")
142 parser.add_option(
"-o",
"--output", action =
"store", type =
"string",
143 help =
"dump the configuration to a file. The format of "
144 "the options is determined by the extension of the "
145 "file name: .pkl = pickle, .py = python, .opts = "
146 "old style options. The python format cannot be "
147 "used to run the application and it contains the "
148 "same dictionary printed with -v")
149 parser.add_option(
"--post-option", action=
"append", type=
"string",
151 help=
"Python options to be executed after the ConfigurableUser "
153 "All options lines are executed, one after the other, in "
155 parser.add_option(
"--debug", action=
"store_true",
156 help=
"enable some debug print-out")
157 parser.add_option(
"--printsequence", action=
"store_true",
158 help=
"print the sequence")
159 if not sys.platform.startswith(
"win"):
161 parser.add_option(
"-T",
"--tcmalloc", action=
"store_true",
162 help=
"Use the Google malloc replacement. The environment "
163 "variable TCMALLOCLIB can be used to specify a different "
164 "name for the library (the default is libtcmalloc.so)")
165 parser.add_option(
"--preload", action=
"append",
166 help=
"Allow pre-loading of special libraries (e.g. Google "
167 "profiling libraries).")
170 parser.add_option(
"--profilerName", type=
"string",
171 help=
"Select one profiler among: igprofPerf, igprofMem and valgrind<toolname>")
174 parser.add_option(
"--profilerOutput", type=
"string",
175 help=
"Specify the name of the output file for the profiler output")
178 parser.add_option(
"--profilerExtraOptions", type=
"string",
179 help=
"Specify additional options for the profiler. The '--' string should be expressed as '__' (--my-opt becomes __my-opt)")
181 parser.set_defaults(options = [],
185 profilerExtraOptions =
'',
189 old_conf_user_apply=
'GAUDI_FIXED_APPLY_CONF' not in os.environ)
193 for a
in sys.argv[1:]:
194 if a.endswith(
'.qmt')
and os.path.exists(a):
198 if argv != sys.argv[1:]:
199 print '# Running', sys.argv[0],
'with arguments', argv
201 opts, args = parser.parse_args(args=argv)
207 from multiprocessing
import cpu_count
208 sys_cpus = cpu_count()
209 if opts.ncpus > sys_cpus:
210 s =
"Invalid value : --ncpus : only %i cpus available" % sys_cpus
212 elif opts.ncpus < -1 :
213 s =
"Invalid value : --ncpus must be integer >= -1"
223 if opts.old_opts: prefix =
"// "
227 level = logging.DEBUG
229 root_logger = logging.getLogger()
233 opts.preload.insert(0, os.environ.get(
"TCMALLOCLIB",
"libtcmalloc.so"))
236 preload = os.environ.get(
"LD_PRELOAD",
"")
238 preload = preload.replace(
" ",
":").split(
":")
241 for libname
in set(preload).intersection(opts.preload):
242 logging.warning(
"Ignoring preload of library %s because it is "
243 "already in LD_PRELOAD.", libname)
245 for libname
in opts.preload
246 if libname
not in set(preload)]
249 preload =
":".join(preload)
250 os.environ[
"LD_PRELOAD"] = preload
251 logging.info(
"Restarting with LD_PRELOAD='%s'", preload)
254 args = [ a
for a
in sys.argv
if a !=
'-T' and not '--tcmalloc'.startswith(a) ]
255 os.execv(sys.executable, [sys.executable] + args)
258 if opts.profilerName:
259 profilerName = opts.profilerName
260 profilerExecName =
""
261 profilerOutput = opts.profilerOutput
or (profilerName +
".output")
266 igprofPerfOptions =
"-d -pp -z -o igprof.pp.gz".split()
269 if profilerName ==
"igprof":
270 if not opts.profilerOutput:
271 profilerOutput +=
".profile.gz"
272 profilerOptions =
"-d -z -o %s" % profilerOutput
273 profilerExecName =
"igprof"
275 elif profilerName ==
"igprofPerf":
276 if not opts.profilerOutput:
277 profilerOutput +=
".pp.gz"
278 profilerOptions =
"-d -pp -z -o %s" % profilerOutput
279 profilerExecName =
"igprof"
281 elif profilerName ==
"igprofMem":
282 if not opts.profilerOutput:
283 profilerOutput +=
".mp.gz"
284 profilerOptions =
"-d -mp -z -o %s" % profilerOutput
285 profilerExecName =
"igprof"
287 elif "valgrind" in profilerName:
289 if not opts.profilerOutput:
290 profilerOutput +=
".log"
291 toolname = profilerName.replace(
'valgrind',
'')
292 outoption =
"--log-file"
293 if toolname
in (
"massif",
"callgrind",
"cachegrind"):
294 outoption =
"--%s-out-file" % toolname
295 profilerOptions =
"--tool=%s %s=%s" % (toolname, outoption, profilerOutput)
296 profilerExecName =
"valgrind"
299 root_logger.warning(
"Profiler %s not recognized!" % profilerName)
302 if opts.profilerExtraOptions!=
"":
303 profilerExtraOptions = opts.profilerExtraOptions
304 profilerExtraOptions = profilerExtraOptions.replace(
"__",
"--")
305 profilerOptions +=
" %s" % profilerExtraOptions
308 import distutils.spawn
309 profilerPath = distutils.spawn.find_executable(profilerExecName)
311 root_logger.error(
"Cannot locate profiler %s" % profilerExecName)
314 root_logger.info(
"------ Profiling options are on ------ \n"\
316 " o Options: '%s'.\n"\
317 " o Output: %s" % (profilerExecName, profilerOptions, profilerOutput))
320 profilerOptions +=
" python"
323 arglist = [profilerPath] + profilerOptions.split() + args
324 arglist = [ a
for a
in arglist
if a!=
'' ]
328 os.execv(profilerPath, arglist)
332 if opts.pickle_output:
334 root_logger.error(
"Conflicting options: use only --pickle-output or --output")
337 root_logger.warning(
"--pickle-output is deprecated, use --output instead")
338 opts.output = opts.pickle_output
346 options = [
"importOptions(%r)" % f
for f
in args ]
349 optlines = list(opts.options)
351 for pos, l
in optlines:
352 options.insert(pos,l)
360 sys.modules[
"GaudiPython"] =
FakeModule(RuntimeError(
"GaudiPython cannot be used in option files"))
366 exec
"from Gaudi.Configuration import *" in g, l
371 import GaudiKernel.Proxy.Configurable
372 if opts.no_conf_user_apply:
373 logging.info(
"Disabling automatic apply of ConfigurableUser")
375 GaudiKernel.Proxy.Configurable._appliedConfigurableUsers_ =
True
378 if opts.old_conf_user_apply:
379 from GaudiKernel.Proxy.Configurable
import applyConfigurableUsers_old
as applyConfigurableUsers
381 from GaudiKernel.Proxy.Configurable
import applyConfigurableUsers
385 if opts.post_options:
388 exec
"from Gaudi.Configuration import *" in g, l
389 for o
in opts.post_options:
394 c.printconfig(opts.old_opts, opts.all_opts)
396 c.writeconfig(opts.output, opts.all_opts)
398 c.printsequence = opts.printsequence
399 if opts.printsequence:
401 logging.warning(
"--printsequence not supported with --ncpus: ignored")
403 logging.warning(
"--printsequence not supported with --dry-run: ignored")
406 del sys.modules[
"GaudiPython"]
410 sys.exit(c.run(opts.ncpus))
def getArgsWithoutoProfilerInfo
def applyConfigurableUsers
struct GAUDI_API map
Parametrisation class for map-like implementation.
def InstallRootLoggingHandler