5 from tempfile
import mkstemp
10 Remove from the arguments the presence of the profiler and its output in 11 order to relaunch the script w/o infinite loops. 13 >>> getArgsWithoutoProfilerInfo(['--profilerName', 'igprof', 'myopts.py']) 16 >>> getArgsWithoutoProfilerInfo(['--profilerName=igprof', 'myopts.py']) 19 >>> getArgsWithoutoProfilerInfo(['--profilerName', 'igprof', '--profilerExtraOptions', 'a b c', 'myopts.py']) 22 >>> getArgsWithoutoProfilerInfo(['--profilerName', 'igprof', '--options', 'a b c', 'myopts.py']) 23 ['--options', 'a b c', 'myopts.py'] 29 if o.startswith(
'--profile'):
38 ''' Adds a list of libraries to LD_PRELOAD ''' 39 preload = os.environ.get(
"LD_PRELOAD",
"")
41 preload = preload.replace(
" ",
":").split(
":")
45 for libname
in set(preload).intersection(newpreload):
47 "Ignoring preload of library %s because it is " 48 "already in LD_PRELOAD.", libname)
51 libname
for libname
in newpreload
if libname
not in set(preload)
56 preload =
":".join(preload)
57 os.environ[
"LD_PRELOAD"] = preload
58 logging.info(
"Setting LD_PRELOAD='%s'", preload)
65 Convert the given path to a real path if the pointed file exists, otherwise 68 path = os.path.normpath(os.path.expandvars(path))
69 if os.path.exists(path):
70 path = os.path.realpath(path)
76 _qmt_tmp_opt_files = []
81 Given a .qmt file, return the command line arguments of the corresponding 84 from xml.etree
import ElementTree
as ET
85 global _qmt_tmp_opt_files
87 qmt = ET.parse(qmtfile)
88 args = [a.text
for a
in qmt.findall(
"argument[@name='args']//text")]
89 options = qmt.find(
"argument[@name='options']/text")
91 if options
is not None:
92 from tempfile
import NamedTemporaryFile
95 r"from\s+Gaudi.Configuration\s+import\s+\*" 96 r"|from\s+Configurables\s+import", options.text):
97 tmp_opts = NamedTemporaryFile(suffix=
'.py')
99 tmp_opts = NamedTemporaryFile(suffix=
'.opts')
100 tmp_opts.write(options.text)
102 args.append(tmp_opts.name)
103 _qmt_tmp_opt_files.append(tmp_opts)
107 qmtfile = os.path.abspath(qmtfile)
108 if 'qmtest' in qmtfile.split(os.path.sep):
111 while os.path.basename(testdir) !=
'qmtest':
112 testdir = os.path.dirname(testdir)
116 old_cwd = os.getcwd()
118 args =
map(rationalizepath, args)
125 if __name__ ==
"__main__":
127 if os.environ.get(
'LC_ALL') !=
'C':
128 print '# setting LC_ALL to "C"' 129 os.environ[
'LC_ALL'] =
'C' 131 from optparse
import OptionParser
132 parser = OptionParser(usage=
"%prog [options] <opts_file> ...")
137 help=
"do not run the application, just parse option files")
144 help=
"DEPRECATED: use '--output file.pkl' instead. Write " 145 "the parsed options as a pickle file (static option " 151 help=
"print the parsed options")
155 help=
"format printed options in old option files style")
159 help=
"print all the option (even if equal to default)")
170 help=
"start the application in parallel mode using NCPUS processes. " 171 "0 => serial mode (default), -1 => use all CPUs")
174 """Add the option line to a list together with its position in the 177 parser.values.options.append((len(parser.largs), value))
185 help=
"add a single line (Python) option to the configuration. " 186 "All options lines are executed, one after the other, in " 189 "--no-conf-user-apply",
191 help=
"disable the automatic application of configurable " 192 "users (for backward compatibility)")
194 "--old-conf-user-apply",
196 help=
"use the old logic when applying ConfigurableUsers " 197 "(with bug #103803) [default]")
199 "--new-conf-user-apply",
200 action=
"store_false",
201 dest=
"old_conf_user_apply",
202 help=
"use the new (correct) logic when applying " 203 "ConfigurableUsers (fixed bug #103803), can be " 204 "turned on also with the environment variable " 205 "GAUDI_FIXED_APPLY_CONF")
211 help=
"dump the configuration to a file. The format of " 212 "the options is determined by the extension of the " 213 "file name: .pkl = pickle, .py = python, .opts = " 214 "old style options. The python format cannot be " 215 "used to run the application and it contains the " 216 "same dictionary printed with -v")
222 help=
"Python options to be executed after the ConfigurableUser " 224 "All options lines are executed, one after the other, in " 227 "--debug", action=
"store_true", help=
"enable some debug print-out")
228 parser.add_option(
"--gdb", action=
"store_true", help=
"attach gdb")
230 "--printsequence", action=
"store_true", help=
"print the sequence")
231 if not sys.platform.startswith(
"win"):
237 help=
"Use the Google malloc replacement. The environment " 238 "variable TCMALLOCLIB can be used to specify a different " 239 "name for the library (the default is libtcmalloc.so)")
243 help=
"Allow pre-loading of special libraries (e.g. Google " 244 "profiling libraries).")
250 "Select one profiler among: igprofPerf, igprofMem and valgrind<toolname>" 257 help=
"Specify the name of the output file for the profiler output")
261 "--profilerExtraOptions",
264 "Specify additional options for the profiler. The '--' string should be expressed as '__' (--my-opt becomes __my-opt)" 270 help=
'when this option is enabled, the options are parsed' 271 ' and stored in a temporary file, then the job is ' 272 'restarted using that file as input (to save ' 278 "Save gaudi process information to the file specified (in JSON format)" 282 help=
'name of the Gaudi::Application to use [default: %default]')
289 profilerExtraOptions=
'',
293 old_conf_user_apply=
'GAUDI_FIXED_APPLY_CONF' not in os.environ,
295 application=
'Gaudi::Application')
299 for a
in sys.argv[1:]:
300 if a.endswith(
'.qmt')
and os.path.exists(a):
304 if argv != sys.argv[1:]:
305 print '# Running', sys.argv[0],
'with arguments', argv
307 opts, args = parser.parse_args(args=argv)
313 from multiprocessing
import cpu_count
314 sys_cpus = cpu_count()
315 if opts.ncpus > sys_cpus:
316 s =
"Invalid value : --ncpus : only %i cpus available" % sys_cpus
318 elif opts.ncpus < -1:
319 s =
"Invalid value : --ncpus must be integer >= -1" 336 level = logging.DEBUG
338 root_logger = logging.getLogger()
341 sanitizer = os.environ.get(
"PRELOAD_SANITIZER_LIB",
"")
343 if sanitizer
not in os.environ.get(
"LD_PRELOAD",
""):
344 opts.preload.insert(0, sanitizer)
345 os.environ[
"PRELOAD_SANITIZER_LIB"] =
"" 351 logging.warning(
"tcmalloc preload disabled when using a sanitizer")
354 0, os.environ.get(
"TCMALLOCLIB",
"libtcmalloc.so"))
358 preload = os.environ.get(
"LD_PRELOAD",
"")
360 preload = preload.replace(
" ",
":").split(
":")
363 for libname
in set(preload).intersection(opts.preload):
365 "Ignoring preload of library %s because it is " 366 "already in LD_PRELOAD.", libname)
368 libname
for libname
in opts.preload
if libname
not in set(preload)
372 preload =
":".join(preload)
373 os.environ[
"LD_PRELOAD"] = preload
374 logging.info(
"Restarting with LD_PRELOAD='%s'", preload)
379 if a !=
'-T' and not '--tcmalloc'.startswith(a)
381 os.execv(sys.executable, [sys.executable] + args)
384 if opts.profilerName:
385 profilerName = opts.profilerName
386 profilerExecName =
"" 387 profilerOutput = opts.profilerOutput
or (profilerName +
".output")
392 igprofPerfOptions =
"-d -pp -z -o igprof.pp.gz".split()
395 if profilerName ==
"igprof":
396 if not opts.profilerOutput:
397 profilerOutput +=
".profile.gz" 398 profilerOptions =
"-d -z -o %s" % profilerOutput
399 profilerExecName =
"igprof" 401 elif profilerName ==
"igprofPerf":
402 if not opts.profilerOutput:
403 profilerOutput +=
".pp.gz" 404 profilerOptions =
"-d -pp -z -o %s" % profilerOutput
405 profilerExecName =
"igprof" 407 elif profilerName ==
"igprofMem":
408 if not opts.profilerOutput:
409 profilerOutput +=
".mp.gz" 410 profilerOptions =
"-d -mp -z -o %s" % profilerOutput
411 profilerExecName =
"igprof" 413 elif "valgrind" in profilerName:
415 if not opts.profilerOutput:
416 profilerOutput +=
".log" 417 toolname = profilerName.replace(
'valgrind',
'')
418 outoption =
"--log-file" 419 if toolname
in (
"massif",
"callgrind",
"cachegrind"):
420 outoption =
"--%s-out-file" % toolname
421 profilerOptions =
"--tool=%s %s=%s" % (toolname, outoption,
423 profilerExecName =
"valgrind" 425 elif profilerName ==
"jemalloc":
427 0, os.environ.get(
"JEMALLOCLIB",
"libjemalloc.so"))
428 os.environ[
'MALLOC_CONF'] =
"prof:true,prof_leak:true" 430 root_logger.warning(
"Profiler %s not recognized!" % profilerName)
433 if opts.profilerExtraOptions !=
"":
434 profilerExtraOptions = opts.profilerExtraOptions
435 profilerExtraOptions = profilerExtraOptions.replace(
"__",
"--")
436 profilerOptions +=
" %s" % profilerExtraOptions
440 import distutils.spawn
441 profilerPath = distutils.spawn.find_executable(profilerExecName)
444 "Cannot locate profiler %s" % profilerExecName)
447 root_logger.info(
"------ Profiling options are on ------ \n" 449 " o Options: '%s'.\n" 450 " o Output: %s" % (profilerExecName
or profilerName,
451 profilerOptions, profilerOutput))
461 profilerOptions +=
" python" 464 arglist = [profilerPath] + profilerOptions.split() + args
465 arglist = [a
for a
in arglist
if a !=
'']
469 os.execv(profilerPath, arglist)
471 arglist = [a
for a
in sys.argv
if not a.startswith(
"--profiler")]
472 os.execv(sys.executable, [sys.executable] + arglist)
476 if opts.pickle_output:
479 "Conflicting options: use only --pickle-output or --output")
483 "--pickle-output is deprecated, use --output instead")
484 opts.output = opts.pickle_output
492 options = [
"importOptions(%r)" % f
for f
in args]
495 optlines = list(opts.options)
498 for pos, l
in optlines:
499 options.insert(pos, l)
510 RuntimeError(
"GaudiPython cannot be used in option files"))
514 if 'GAUDI_TEMP_OPTS_FILE' in os.environ:
515 options = [
'importOptions(%r)' % os.environ[
'GAUDI_TEMP_OPTS_FILE']]
522 exec (
"from Gaudi.Configuration import *", g, l)
527 import GaudiKernel.Proxy.Configurable
528 if opts.no_conf_user_apply:
529 logging.info(
"Disabling automatic apply of ConfigurableUser")
531 GaudiKernel.Proxy.Configurable._appliedConfigurableUsers_ =
True 534 if opts.old_conf_user_apply:
535 from GaudiKernel.Proxy.Configurable
import applyConfigurableUsers_old
as applyConfigurableUsers
537 from GaudiKernel.Proxy.Configurable
import applyConfigurableUsers
541 if opts.post_options:
544 exec (
"from Gaudi.Configuration import *", g, l)
545 for o
in opts.post_options:
549 if 'GAUDI_TEMP_OPTS_FILE' in os.environ:
550 os.remove(os.environ[
'GAUDI_TEMP_OPTS_FILE'])
551 opts.use_temp_opts =
False 553 if opts.verbose
and not opts.use_temp_opts:
554 c.printconfig(opts.old_opts, opts.all_opts)
556 c.writeconfig(opts.output, opts.all_opts)
558 if opts.use_temp_opts:
559 fd, tmpfile = mkstemp(
'.opts')
561 c.writeconfig(tmpfile, opts.all_opts)
562 os.environ[
'GAUDI_TEMP_OPTS_FILE'] = tmpfile
563 logging.info(
'Restarting from pre-parsed options')
564 os.execv(sys.executable, [sys.executable] + sys.argv)
566 c.printsequence = opts.printsequence
567 if opts.printsequence:
570 "--printsequence not supported with --ncpus: ignored")
573 "--printsequence not supported with --dry-run: ignored")
575 c.application = opts.application
578 del sys.modules[
"GaudiPython"]
582 retcode = c.run(opts.gdb, opts.ncpus)
586 if opts.run_info_file:
590 run_info[
"pid"] = os.getpid()
591 run_info[
"retcode"] = retcode
592 if os.path.exists(
'/proc/self/exe'):
594 run_info[
"exe"] = os.readlink(
'/proc/self/exe')
596 logging.info(
"Saving run info to: %s" % opts.run_info_file)
597 with open(opts.run_info_file,
"w")
as f:
598 json.dump(run_info, f)
def __init__(self, exception)
def option_cb(option, opt, value, parser)
def rationalizepath(path)
def applyConfigurableUsers()
def getArgsFromQmt(qmtfile)
def InstallRootLoggingHandler(prefix=None, level=None, stream=None, with_time=False)
struct GAUDI_API map
Parametrisation class for map-like implementation.
def getArgsWithoutoProfilerInfo(args)
def __getattr__(self, args, kwargs)
def setLibraryPreload(newpreload)