使用 Python 的 Valgrind在 callgrind 输出中定位函数

问题描述

我正在使用 valgrind 的 callgrind 工具来分析 python 程序(同样适用于 cachegrind)。使用它来分析 C++ 可执行文件,可以搜索转储的 callgrind.out.id(或使用正则表达式),以查找特定函数的信息。也就是说,他们可以按名称搜索这些功能。问题是,当与 python 一起使用时,输出很大,包含低级函数的信息,如 pthread_mutex_unlock_Py_GetErrorHandler,或(函数的?)内存地址,如 {{1 }},并且函数名无处可寻。

那么,问题是,如何在 callgrind 输出中找到特定的函数

示例:

0x0000000004a75190

(示例中可以省略valgrind --tool=callgrind --branch-sim=yes --cache-sim=yes python foobar.py

foobar.py

--branch-sim=yes --cache-sim=yes

唯一有 547 个调用的条目:

def add_one(foo):
  return foo + 1

def main():
  # Using an arbitrary number of iterations,to locate it in the output.
  for i in range(547):
    bar = add_one(i)

if __name__ == "__main__":
  main()

调用函数(cfn) id 1394 被发现111次,cfn=(1394),1次fn=(1394),但是旁边有函数名的那个是:

fn=(5568)
725 1644 1096 0 1 0 0 1 0 0 548 1
+4 547
-1 547 547
+1 1094 547 547
-1 1094 0 0 1 0 0 1
cfi=(305)
cfn=(1394)
calls=547 318 
* 31563 7214 6925 0 0 0 0 0 0 4316 6
+3 2 1

cfi=(305) 被发现多次。带有文件名声明的那个:

fn=(5382) _buffered_raw_seek
691 6 0 3 1 0 0 1
+4 3
-4 15 0 9 1 0 0 1
+4 3 0 3
cfi=(305)
cfn=(1480) PyLong_FromSsize_t
calls=3 1221 
* 124 22 22 4 0 1 1 0 0 18 5
+1 9 0 0 0 0 0 0 0 0 3
+2 6 0 3
cfi=(305)
cfn=(1394) PyLong_FromLong       <---- Here
calls=3 318 
* 30 3 3 0 0 0 0 0 0 3
* 3
+1 6 0 0 0 0 0 0 0 0 3
+4 21 6 3 1 0 0 1
cfi=(342)
cfn=(3554)
calls=3 1200 
* 2963 823 511 14 3 1 6 0 0 484 40 40 11
fi=(293)
470 3 0 3
fe=(424)
703 3
fi=(293)
470 9 0 3 0 0 0 0 0 0 6 1
fe=(424)
707 6 0 0 0 0 0 0 0 0 3
+2 9 3 3
cfi=(364) /tmp/build/80754af9/python_1599203911753/work/Modules/_io/_iomodule.c
cfn=(3568) PyNumber_AsOff_t
calls=3 532 
* 147 41 27 0 0 0 0 0 0 26 1
fi=(293)
470 3 0 3
fe=(424)
709 3
fi=(293)
470 3 0 0 0 0 0 0 0 0 3
fe=(424)
711 6 0 0 0 0 0 0 0 0 3
+7 3 0 3
+2 21 18
fi=(293)
478 2 0 1 1 0 0 1
cfi=(289)
cfn=(1262)
calls=1 2209 
* 34 13 5 0 0 0 0 0 0 6 2 3 1
* 1
fe=(424)

最相关的信息是 cfn=(5568):

cfi=(305) /tmp/build/80754af9/python_1599203911753/work/Objects/longobject.c
cfn=(2588) _PyLong_AsInt
calls=4 +60 
* 134 38 12 10 4 0 0 0 0 26 4
+1 12 4 0 0 0 0 0 0 0 4
fi=(430)
286 20 4 0 4 0 0 1
-1 8 0 4
fe=(431)

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)