gpt4 book ai didi

python - 如何列出在 python 脚本运行时加载的 .so 文件?

转载 作者:塔克拉玛干 更新时间:2023-11-03 00:31:19 32 4
gpt4 key购买 nike

如何列出在 python 脚本运行时加载的所有 .so 文件(完整路径)?

是否可以获得(例如)libcudart.so.10.1 的完整路径?是否可以获取已加载的 .so(完整路径)列表?

例如我正在运行:

python -c "import tensorflow as tf; tf.test.is_gpu_available()"

输出:

2019-09-27 15:02:27.186029: I tensorflow/stream_executor/platform/default/dso_loader.cc:42] Successfully opened dynamic library libcuda.so.1
2019-09-27 15:02:27.657901: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1640] Found device 0 with properties:
name: TITAN RTX major: 7 minor: 5 memoryClockRate(GHz): 1.77
pciBusID: 0000:17:00.0
2019-09-27 15:02:27.658580: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1640] Found device 1 with properties:
name: TITAN RTX major: 7 minor: 5 memoryClockRate(GHz): 1.77
pciBusID: 0000:65:00.0
2019-09-27 15:02:27.658766: I tensorflow/stream_executor/platform/default/dso_loader.cc:42] Successfully opened dynamic library libcudart.so.10.1
2019-09-27 15:02:27.659868: I tensorflow/stream_executor/platform/default/dso_loader.cc:42] Successfully opened dynamic library libcublas.so.10
2019-09-27 15:02:27.661073: I tensorflow/stream_executor/platform/default/dso_loader.cc:42] Successfully opened dynamic library libcufft.so.10
2019-09-27 15:02:27.661305: I tensorflow/stream_executor/platform/default/dso_loader.cc:42] Successfully opened dynamic library libcurand.so.10
2019-09-27 15:02:27.662477: I tensorflow/stream_executor/platform/default/dso_loader.cc:42] Successfully opened dynamic library libcusolver.so.10
2019-09-27 15:02:27.663054: I tensorflow/stream_executor/platform/default/dso_loader.cc:42] Successfully opened dynamic library libcusparse.so.10
2019-09-27 15:02:27.665455: I tensorflow/stream_executor/platform/default/dso_loader.cc:42] Successfully opened dynamic library libcudnn.so.7
2019-09-27 15:02:27.667986: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1763] Adding visible gpu devices: 0, 1
2019-09-27 15:02:27.668031: I tensorflow/stream_executor/platform/default/dso_loader.cc:42] Successfully opened dynamic library libcudart.so.10.1
2019-09-27 15:02:27.669523: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1181] Device interconnect StreamExecutor with strength 1 edge matrix:
2019-09-27 15:02:27.669535: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1187] 0 1
2019-09-27 15:02:27.669542: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1200] 0: N N
2019-09-27 15:02:27.669546: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1200] 1: N N
2019-09-27 15:02:27.674204: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1326] Created TensorFlow device (/device:GPU:0 with 22845 MB memory) -> physical GPU (device: 0, name: TITAN RTX, pci bus id: 0000:17:00.0, compute capability: 7.5)
2019-09-27 15:02:27.675838: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1326] Created TensorFlow device (/device:GPU:1 with 22823 MB memory) -> physical GPU (device: 1, name: TITAN RTX, pci bus id: 0000:65:00.0, compute capability: 7.5)

最佳答案

在 Linux 下运行时,根据您的环境,您可以使用以下替代方法之一获取信息:

  1. ld.so 调试标志
  2. 跟踪

使用 ld.so 调试标志,您可以在运行 python 代码(取决于您的 shell:export LD_DEBUG=libs,或setenv LD_DEBUG 库)。动态加载的库搜索路径将显示到 stderr。

查看 ld.so 手册页以了解其他选项。

示例输出 - 来自 LD_DEBUG=libs(使用 python 'import json')

owner@vm1:~/Project/stackoverflow/58152351$ python
28796: find library=libc.so.6 [0]; searching
28796: search cache=/etc/ld.so.cache
28796: trying file=/lib/x86_64-linux-gnu/libc.so.6
28796:
28796: find library=libpthread.so.0 [0]; searching
28796: search cache=/etc/ld.so.cache
28796: trying file=/lib/x86_64-linux-gnu/libpthread.so.0
28796:
28796: find library=libdl.so.2 [0]; searching
28796: search cache=/etc/ld.so.cache
28796: trying file=/lib/x86_64-linux-gnu/libdl.so.2
...
>>> import json
28796:
28796: calling init: /usr/lib/python2.7/lib-dynload/_json.x86_64-linux-gnu.so
28796:

作为替代方案,可以使用 strace,但解析它的输出需要更多的努力。 “stat”行可以提供有关每个库的搜索路径的信息。

关于python - 如何列出在 python 脚本运行时加载的 .so 文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58134293/

32 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com