3a776cbf2169d348a6f9b78fe6142b80e5802efe
11 from tempfile
import TemporaryDirectory
13 import lttng_ivc
.settings
as Settings
14 _logger
= logging
.getLogger("Runtime")
17 @contextlib.contextmanager
18 def get_runtime(runtime_dir
):
19 runtime
= Runtime(runtime_dir
)
26 class Runtime(object):
27 def __init__(self
, runtime_dir
):
29 A dictionary of popen object eg. lttng-sessiond, relayd,
30 anything really. Key is a uuid.
32 self
.__subprocess
= {}
33 self
.__stdout
_stderr
= {}
36 self
.__runtime
_log
= os
.path
.join(runtime_dir
, "log")
37 self
.__runtime
_log
_sub
= os
.path
.join(self
.__runtime
_log
, "subprocess")
40 Path of the copy of lttng_home folder after Runtime.close() is issued. This is
41 to be used for post runtime analysis and mostly debugging on error.
43 self
.__post
_runtime
_lttng
_home
_path
= os
.path
.join(runtime_dir
,
46 self
._runtime
_log
_aggregation
= os
.path
.join(self
.__runtime
_log
, "runtime.log")
48 self
._run
_command
_count
= 0
50 self
.special_env_variables
= {"LTTNG_UST_DEBUG": "1",
51 "LTTNG_APP_SOCKET_TIMEOUT": "-1",
52 #"LTTNG_UST_REGISTER_TIMEOUT": "-1",
53 "LTTNG_NETWORK_SOCKET_TIMEOUT": "-1"}
55 # Keep a reference on the object to keep it alive. It will close/clean on
57 self
.__lttng
_home
_dir
= TemporaryDirectory(prefix
=Settings
.tmp_object_prefix
)
58 self
.lttng_home
= self
.__lttng
_home
_dir
.name
60 if len(self
.lttng_home
) > 88:
61 raise Exception("TemporaryDirectory for lttng_home is to long. Use a short TMPDIR")
63 os
.makedirs(self
.__runtime
_log
)
64 os
.makedirs(self
.__runtime
_log
_sub
)
66 def add_project(self
, project
):
67 self
.__projects
.append(project
)
69 def remove_project(self
, project
):
70 self
.__projects
.remove(project
)
72 def subprocess_signal(self
, subprocess_uuid
, signal
):
73 self
.__subproces
[subprocess_uuid
].send_signal(signal
)
75 def subprocess_terminate(self
, subprocess_uuid
, timeout
=60):
76 process
= self
.__subprocess
[subprocess_uuid
]
79 stdout
, stderr
= self
.__stdout
_stderr
[subprocess_uuid
]
84 def subprocess_kill(self
, subprocess_uuid
):
85 process
= self
.__subprocess
[subprocess_uuid
]
88 stdout
, stderr
= self
.__stdout
_stderr
[subprocess_uuid
]
93 def get_subprocess_stdout_path(self
, subprocess_uuid
):
94 stdout
, stderr
= self
.__stdout
_stderr
[subprocess_uuid
]
97 def get_subprocess_stderr_path(self
, subprocess_uuid
):
98 stdout
, stderr
= self
.__stdout
_stderr
[subprocess_uuid
]
101 def spawn_subprocess(self
, command_line
, cwd
=None):
102 args
= shlex
.split(command_line
)
105 if not os
.path
.isdir(self
.lttng_home
):
106 raise Exception("lttng home does not exist")
108 tmp_id
= uuid
.uuid1()
109 out_path
= os
.path
.join(self
.__runtime
_log
_sub
, str(tmp_id
) + ".out")
110 err_path
= os
.path
.join(self
.__runtime
_log
_sub
, str(tmp_id
) + ".err")
112 stdout
= open(out_path
, 'w')
113 stderr
= open(err_path
, 'w')
115 env_path
= os
.path
.join(self
.__runtime
_log
_sub
, str(tmp_id
) + ".env")
116 with
open(env_path
, 'w') as env_out
:
117 pprint
.pprint(env
, stream
=env_out
)
119 p
= subprocess
.Popen(args
, stdout
=stdout
, stderr
=stderr
, env
=env
, cwd
=cwd
)
120 self
.__subprocess
[tmp_id
] = p
121 self
.__stdout
_stderr
[tmp_id
] = (stdout
, stderr
)
122 _logger
.debug("Spawned sub pid: {} args: {} stdout: {} stderr{}".format(p
.pid
, p
.args
, out_path
, err_path
))
125 def run(self
, command_line
, cwd
=None, check_return
=True, ld_preload
="", classpath
=""):
127 Run the command and return a tuple of a (CompletedProcess, stdout_path,
128 stderr_path). The subprocess is already executed and returned. The
129 callecaller is responsible for checking for errors.
131 args
= shlex
.split(command_line
)
135 env
['LD_PRELOAD'] = ld_preload
137 env
['CLASSPATH'] = classpath
140 tmp_id
= self
._run
_command
_count
141 self
._run
_command
_count
+= 1
143 cmd_map
= os
.path
.join(self
.__runtime
_log
, "cmd.map")
144 with
open(cmd_map
, 'a') as out
:
145 out
.write("{}: {}\n".format(tmp_id
, args
))
147 out_path
= os
.path
.join(self
.__runtime
_log
, str(tmp_id
) + ".out")
148 err_path
= os
.path
.join(self
.__runtime
_log
, str(tmp_id
) + ".err")
149 stdout
= open(out_path
, "w")
150 stderr
= open(err_path
, "w")
152 env_path
= os
.path
.join(self
.__runtime
_log
, str(tmp_id
) + ".env")
153 with
open(env_path
, 'w') as env_out
:
154 for key
, value
in env
.items():
155 env_out
.write('{}={}\n'.format(key
, value
))
157 cp
= subprocess
.run(args
, stdout
=stdout
, stderr
=stderr
, env
=env
, cwd
=cwd
)
158 _logger
.debug("Command #{} args: {} stdout: {} stderr{}".format(tmp_id
, cp
.args
, out_path
, err_path
))
160 # Add to the global log file. This can help a little. Leave the other
161 # file available for per-run analysis
162 with
open(self
._runtime
_log
_aggregation
, "a") as log
:
163 with
open(out_path
, "r") as out
:
164 log
.write("Output for command #{} {}\n".format(tmp_id
, command_line
))
165 log
.write("Start >>>>>>>>>>>>>>>>\n")
166 log
.write(out
.read())
167 log
.write("End <<<<<<<<<<<<<<<<\n")
168 with
open(err_path
, "r") as out
:
169 log
.write("Error for command #{} {}\n".format(tmp_id
, command_line
))
170 log
.write("Start >>>>>>>>>>>>>>>>\n")
171 log
.write(out
.read())
172 log
.write("End <<<<<<<<<<<<<<<<\n")
175 cp
.check_returncode()
177 return (cp
, out_path
, err_path
)
179 def get_cppflags(self
):
181 for project
in self
.__projects
:
182 cppflags
.append(project
.get_cppflags())
183 return " ".join(cppflags
)
185 def get_ldflags(self
):
187 for project
in self
.__projects
:
188 ldflags
.append(project
.get_ldflags())
189 return " ".join(ldflags
)
191 def get_ld_library_path(self
):
193 for project
in self
.__projects
:
194 library_path
.append(project
.get_ld_library_path())
195 return ":".join(library_path
)
197 def get_bin_path(self
):
199 for project
in self
.__projects
:
200 path
.append(project
.get_bin_path())
201 return ":".join(path
)
204 env
= os
.environ
.copy()
206 env
["LTTNG_HOME"] = self
.lttng_home
208 env_fetch
= {"CPPFLAGS": (self
.get_cppflags(), " "),
209 "LDFLAGS": (self
.get_ldflags(), " "),
210 "LD_LIBRARY_PATH": (self
.get_ld_library_path(), ":"),
211 "PATH": (self
.get_bin_path(), ":"),
213 for key
, (value
, delimiter
) in env_fetch
.items():
217 env
[key
] = delimiter
.join([value
, tmp_var
])
219 for var
, value
in self
.special_env_variables
.items():
221 # Raise for now since no special cases is known
222 _logger
.warning("% Special var % is already defined",
224 raise Exception("Multiple definition of a special environment variable")
228 for project
in self
.__projects
:
229 for var
, value
in project
.special_env_variables
.items():
231 # Raise for now since no special cases is known
232 _logger
.warning("% Special var % is already defined",
234 raise Exception("Multiple definition of a special environment variable")
239 def load_test_module(self
):
240 # Base directory is provided by env
241 self
.run("modprobe lttng-test")
243 def unload_test_module(self
, check_return
=True):
244 # Base directory is provided by env
245 self
.run("modprobe -r lttng-test", check_return
=check_return
)
248 for key
, subp
in self
.__subprocess
.items():
250 for key
, subp
in self
.__subprocess
.items():
251 # TODO move timeout to settings
252 subp
.wait(timeout
=60)
253 for key
, (stdout
, stderr
) in self
.__stdout
_stderr
.items():
257 # Always try to remove test module but do not perform check on return
259 self
.unload_test_module(False)
261 # Copy the lttng_home used at runtime using hardlink to prevent useless
263 shutil
.copytree(self
.lttng_home
, self
.__post
_runtime
_lttng
_home
_path
, copy_function
=os
.link
)
This page took 0.048605 seconds and 4 git commands to generate.