Skip to content

Commit c8e74e9

Browse files
Add detection to warn user of context is used by onetrace was not
onetrace tool on linux uses execvp to start the application to trace. It sets LD_PRELOAD and PTI_ENABLE environment variables https://github.com/intel/pti-gpu/blob/master/loader/loader.cc#L216-L217 The check is added, on Linux, to confirm once that environment does contain expected variables, and emit a warning otherwise.
1 parent 22f2df5 commit c8e74e9

File tree

1 file changed

+27
-5
lines changed

1 file changed

+27
-5
lines changed

dpctl/utils/_onetrace_context.py

Lines changed: 27 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,12 @@
1414
# See the License for the specific language governing permissions and
1515
# limitations under the License.
1616

17-
import os
1817
from contextlib import contextmanager
18+
from os import environ, getenv
19+
from platform import system as sys_platform
20+
21+
_UNCHECKED = sys_platform() == "Linux"
22+
del sys_platform
1923

2024

2125
@contextmanager
@@ -42,13 +46,31 @@ def onetrace_enabled():
4246
dpt.arange(100, dtype='int16')
4347
4448
"""
49+
global _UNCHECKED
50+
51+
if _UNCHECKED:
52+
_UNCHECKED = False
53+
if not (
54+
getenv("PTI_ENABLE", None) == "1"
55+
and "onetrace_tool" in getenv("LD_PRELOAD", "")
56+
):
57+
import warnings
58+
59+
warnings.warn(
60+
"It looks like Python interpreter was not started using "
61+
"`onetrace` utility. Using `onetrace_enabled` may have "
62+
"no effect. See `onetrace_enabled.__doc__` for usage.",
63+
RuntimeWarning,
64+
stacklevel=2,
65+
)
66+
4567
_env_var_name = "PTI_ENABLE_COLLECTION"
46-
saved = os.getenv(_env_var_name, None)
68+
saved = getenv(_env_var_name, None)
4769
try:
48-
os.environ[_env_var_name] = "1"
70+
environ[_env_var_name] = "1"
4971
yield
5072
finally:
5173
if saved is None:
52-
del os.environ[_env_var_name]
74+
del environ[_env_var_name]
5375
else:
54-
os.environ[_env_var_name] = saved
76+
environ[_env_var_name] = saved

0 commit comments

Comments
 (0)