c2d0b8397299b5f0453bcb2550873e108aee28ea
12 from tempfile
import TemporaryDirectory
14 import lttng_ivc
.settings
as Settings
15 _logger
= logging
.getLogger("Runtime")
18 @contextlib.contextmanager
19 def get_runtime(runtime_dir
):
20 runtime
= Runtime(runtime_dir
)
27 class Runtime(object):
28 def __init__(self
, runtime_dir
):
30 A dictionary of popen object eg. lttng-sessiond, relayd,
31 anything really. Key is a uuid.
33 self
.__subprocess
= {}
34 self
.__stdout
_stderr
= {}
37 self
.__runtime
_log
= os
.path
.join(runtime_dir
, "log")
38 self
.__runtime
_log
_sub
= os
.path
.join(self
.__runtime
_log
, "subprocess")
41 Path of the copy of lttng_home folder after Runtime.close() is issued. This is
42 to be used for post runtime analysis and mostly debugging on error.
44 self
.__post
_runtime
_lttng
_home
_path
= os
.path
.join(runtime_dir
,
47 self
._runtime
_log
_aggregation
= os
.path
.join(self
.__runtime
_log
, "runtime.log")
49 self
._run
_command
_count
= 0
51 self
.special_env_variables
= {"LTTNG_UST_DEBUG": "1",
52 #"LTTNG_APP_SOCKET_TIMEOUT": "-1",
53 #"LTTNG_UST_REGISTER_TIMEOUT": "-1",
54 "LTTNG_NETWORK_SOCKET_TIMEOUT": "-1"}
56 # Keep a reference on the object to keep it alive. It will close/clean on
58 self
.__lttng
_home
_dir
= TemporaryDirectory(prefix
=Settings
.tmp_object_prefix
)
59 self
.lttng_home
= self
.__lttng
_home
_dir
.name
61 if len(self
.lttng_home
) > 88:
62 raise Exception("TemporaryDirectory for lttng_home is to long. Use a short TMPDIR")
64 os
.makedirs(self
.__runtime
_log
)
65 os
.makedirs(self
.__runtime
_log
_sub
)
67 def add_project(self
, project
):
68 self
.__projects
.append(project
)
70 def subprocess_signal(self
, subprocess_uuid
, signal
):
71 self
.__subproces
[subprocess_uuid
].send_signal(signal
)
73 def subprocess_terminate(self
, subprocess_uuid
, timeout
=60):
74 process
= self
.__subprocess
[subprocess_uuid
]
77 stdout
, stderr
= self
.__stdout
_stderr
[subprocess_uuid
]
82 def subprocess_kill(self
, subprocess_uuid
):
83 process
= self
.__subprocess
[subprocess_uuid
]
86 stdout
, stderr
= self
.__stdout
_stderr
[subprocess_uuid
]
91 def get_subprocess_stdout_path(self
, subprocess_uuid
):
92 stdout
, stderr
= self
.__stdout
_stderr
[subprocess_uuid
]
95 def get_subprocess_stderr_path(self
, subprocess_uuid
):
96 stdout
, stderr
= self
.__stdout
_stderr
[subprocess_uuid
]
99 def spawn_subprocess(self
, command_line
, cwd
=None):
100 args
= shlex
.split(command_line
)
103 if not os
.path
.isdir(self
.lttng_home
):
104 raise Exception("lttng home does not exist")
106 tmp_id
= uuid
.uuid1()
107 out_path
= os
.path
.join(self
.__runtime
_log
_sub
, str(tmp_id
) + ".out")
108 err_path
= os
.path
.join(self
.__runtime
_log
_sub
, str(tmp_id
) + ".err")
110 stdout
= open(out_path
, 'w')
111 stderr
= open(err_path
, 'w')
113 env_path
= os
.path
.join(self
.__runtime
_log
_sub
, str(tmp_id
) + ".env")
114 with
open(env_path
, 'w') as env_out
:
115 pprint
.pprint(env
, stream
=env_out
)
117 p
= subprocess
.Popen(args
, stdout
=stdout
, stderr
=stderr
, env
=env
)
118 self
.__subprocess
[tmp_id
] = p
119 self
.__stdout
_stderr
[tmp_id
] = (stdout
, stderr
)
120 _logger
.debug("Spawned sub pid: {} args: {} stdout: {} stderr{}".format(p
.pid
, p
.args
, out_path
, err_path
))
123 def run(self
, command_line
, cwd
=None, check_return
=True):
125 Run the command and return a tuple of a (CompletedProcess, stdout_path,
126 stderr_path). The subprocess is already executed and returned. The
127 callecaller is responsible for checking for errors.
129 args
= shlex
.split(command_line
)
132 tmp_id
= self
._run
_command
_count
133 self
._run
_command
_count
+= 1
135 cmd_map
= os
.path
.join(self
.__runtime
_log
, "cmd.map")
136 with
open(cmd_map
, 'a') as out
:
137 out
.write("{}: {}\n".format(tmp_id
, args
))
139 out_path
= os
.path
.join(self
.__runtime
_log
, str(tmp_id
) + ".out")
140 err_path
= os
.path
.join(self
.__runtime
_log
, str(tmp_id
) + ".err")
141 stdout
= open(out_path
, "w")
142 stderr
= open(err_path
, "w")
144 env_path
= os
.path
.join(self
.__runtime
_log
, str(tmp_id
) + ".env")
145 with
open(env_path
, 'w') as env_out
:
146 pprint
.pprint(env
, stream
=env_out
)
148 cp
= subprocess
.run(args
, stdout
=stdout
, stderr
=stderr
, env
=env
, cwd
=cwd
)
149 _logger
.debug("Command #{} args: {} stdout: {} stderr{}".format(tmp_id
, cp
.args
, out_path
, err_path
))
151 # Add to the global log file. This can help a little. Leave the other
152 # file available for per-run analysis
153 with
open(self
._runtime
_log
_aggregation
, "a") as log
:
154 with
open(out_path
, "r") as out
:
155 log
.write("Output for command #{} {}\n".format(tmp_id
, command_line
))
156 log
.write("Start >>>>>>>>>>>>>>>>\n")
157 log
.write(out
.read())
158 log
.write("End <<<<<<<<<<<<<<<<\n")
159 with
open(err_path
, "r") as out
:
160 log
.write("Error for command #{} {}\n".format(tmp_id
, command_line
))
161 log
.write("Start >>>>>>>>>>>>>>>>\n")
162 log
.write(out
.read())
163 log
.write("End <<<<<<<<<<<<<<<<\n")
166 cp
.check_returncode()
168 return (cp
, out_path
, err_path
)
170 def get_cppflags(self
):
172 for project
in self
.__projects
:
173 cppflags
.append(project
.get_cppflags())
174 return " ".join(cppflags
)
176 def get_ldflags(self
):
178 for project
in self
.__projects
:
179 ldflags
.append(project
.get_ldflags())
180 return " ".join(ldflags
)
182 def get_ld_library_path(self
):
184 for project
in self
.__projects
:
185 library_path
.append(project
.get_ld_library_path())
186 return " ".join(library_path
)
188 def get_bin_path(self
):
190 for project
in self
.__projects
:
191 path
.append(project
.get_bin_path())
192 return ":".join(path
)
195 env
= os
.environ
.copy()
197 env
["LTTNG_HOME"] = self
.lttng_home
199 env_fetch
= {"CPPFLAGS": (self
.get_cppflags(), " "),
200 "LDFLAGS": (self
.get_ldflags(), " "),
201 "LD_LIBRARY_PATH": (self
.get_ld_library_path(), ":"),
202 "PATH": (self
.get_bin_path(), ":"),
204 for key
, (value
, delimiter
) in env_fetch
.items():
208 env
[key
] = delimiter
.join([value
, tmp_var
])
210 for var
, value
in self
.special_env_variables
.items():
212 # Raise for now since no special cases is known
213 _logger
.warning("% Special var % is already defined",
215 raise Exception("Multiple definition of a special environment variable")
219 for project
in self
.__projects
:
220 for var
, value
in project
.special_env_variables
.items():
222 # Raise for now since no special cases is known
223 _logger
.warning("% Special var % is already defined",
225 raise Exception("Multiple definition of a special environment variable")
231 for key
, subp
in self
.__subprocess
.items():
233 for key
, subp
in self
.__subprocess
.items():
234 # TODO move timeout to settings
235 subp
.wait(timeout
=60)
236 for key
, (stdout
, stderr
) in self
.__stdout
_stderr
.items():
240 # Copy the lttng_home used at runtime using hardlink to prevent useless
242 shutil
.copytree(self
.lttng_home
, self
.__post
_runtime
_lttng
_home
_path
, copy_function
=os
.link
)
This page took 0.038368 seconds and 5 git commands to generate.