惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

Microsoft Azure Blog
Microsoft Azure Blog
A
Arctic Wolf
Hacker News - Newest:
Hacker News - Newest: "LLM"
T
Threatpost
P
Proofpoint News Feed
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
www.infosecurity-magazine.com
www.infosecurity-magazine.com
K
KPMG report finds enterprise disconnect between AI and its ROI | CIO
V
Vulnerabilities – Threatpost
V
V2EX
Webroot Blog
Webroot Blog
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
P
Privacy & Cybersecurity Law Blog
P
Privacy International News Feed
T
Tor Project blog
P
Proofpoint News Feed
T
Tailwind CSS Blog
C
Cyber Attacks, Cyber Crime and Cyber Security
Recent Commits to openclaw:main
Recent Commits to openclaw:main
Vercel News
Vercel News
Security Archives - TechRepublic
Security Archives - TechRepublic
MongoDB | Blog
MongoDB | Blog
T
Troy Hunt's Blog
Google DeepMind News
Google DeepMind News
NISL@THU
NISL@THU
C
CERT Recently Published Vulnerability Notes
W
WeLiveSecurity
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
GbyAI
GbyAI
Y
Y Combinator Blog
T
Threat Research - Cisco Blogs
S
Security Affairs
Google Online Security Blog
Google Online Security Blog
S
Securelist
Spread Privacy
Spread Privacy
Recent Announcements
Recent Announcements
The Register - Security
The Register - Security
C
Cybersecurity and Infrastructure Security Agency CISA
爱范儿
爱范儿
H
Help Net Security
Microsoft Security Blog
Microsoft Security Blog
酷 壳 – CoolShell
酷 壳 – CoolShell
Scott Helme
Scott Helme
The Cloudflare Blog
T
The Blog of Author Tim Ferriss
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
B
Blog RSS Feed
AWS News Blog
AWS News Blog

博客园 - 赵青青

AI模型Claude的Haiku、Sonnet、Opus 怎么选? obsidian(md笔记管理)使用实践 python中可变参数与装饰器的例子 C++ lambda 表达式 3ds max的obj文件格式说明 Python3类型安全type hint Python3虚拟机和对象 vs编译cpp时设置排除项 与ChatGPT的对话在windows上获取mac地址 Python311新特性-特化指令specializing adaptive interpreter-typing-asyncio 最小体积拉取git仓库并保持可更新 unity .net8 suppot comming pycharm一些减少代码warning的拼写检查设置 JavaScript速查表 Python cheatsheet 速查表 DirectX9(D3D9)游戏开发:高光时刻录制和共享纹理的踩坑 FFmpeg在游戏视频录制中的应用:画质与文件大小的综合比较 c++中字符串之string和char IMGUI快速入门
CPython调试和性能分析
赵青青 · 2024-12-15 · via 博客园 - 赵青青

cpython解释器

在源码ceval.c 的 _PyEval_EvalFrameDefault函数有一个大的 switch (opcdoe) 就是字节码解释器的主要部分,示例代码如下:

switch (opcode) {
    case LOAD_FAST:
        // 实现 LOAD_FAST 操作的代码
        break;
    case STORE_FAST:
        // 实现 STORE_FAST 操作的代码
        break;
    case CALL_FUNCTION:
        // 实现 CALL_FUNCTION 操作的代码
        break;
    // 其他字节码操作
}

所有的opcode都可以在opcode.h这里查询到

所有的特化指令都可以在opcode.py查询到

调试工具

python自带pdb命令行下的调试工具

gdb unix下的命令行调试,图形化的使用vs和vscode

GDB是一个强大的Unix下的源代码级调试器。主要用于调试C和C++程序,也有Python的扩展(pythongdb.py) ,可以用来调试Python程序
• 可以attach到运行中进程,断点调试
• 可以加载coredump进行调试

vscode debug的核心实现

sys.settrace(tracefunc)

在cpython源码中进行插桩

PyEval_SetTrace->

  • trace_function_entry,进入函数
  • trace_function_exit,退出函数

cpython源码支持debug

在源码中支持行调试,实现方法:opcode->do_tracing,在ceval.c

0、 Opcode变成do_tracing
1、重新获得正确的opcode,并trace
2、 真正执行opcode
3、 下一个opcode变成do_tracing

3.11之后无法在引擎看到脚本堆栈

natvis就是一个配置文件,只需要拖动到vs的窗口中

原理:natvis定义了如何显示一个结构体

可以看到一些简单类型的变量,tuple/dict可展开

可以看到当前帧

注意:3.11之后,PyFrameObject大改,现在不能用natvis直接拿到脚本栈了,需要在堆栈窗口多次查看对应

常见调试工具-小技巧

qa的机器才能重现问题,怎么办?
开发过程中,想要不重启,实时让自己写的代码生效,怎么办?
线上问题没法调试,怎么办

qa的机器才能重现问题,怎么办? ——hunter
本质:用exec让游戏动态运行一段代码 ,可以把老代码替换成新代码

2 热更,github有插件,适配自己的项目

3 写log

常见性能分析工具

cpython提供内置的

tracy 各游戏引擎都接入

tracy如何profile python的
• 方法一:在ceval.c/_PyEval_EvalFrameDefault()里插桩

常见性能分析工具——maze

自研的,postman内置的内存profile工具

GPM

技术中心自研的

Python3新特性 :调试与性能分析

monitoring

sys.monitoring (since py3.12)

同一段代码跑trace和不跑trace的消耗差20倍,因为trace每行的消耗特别高(overhead)

通过少调用trace function来减少不必要的开销

faulthandler

faulthandler(since 3.3)
当python挂掉时, 能打印堆栈: 为这些信号注册处理
SIGSEGV, SIGFPE, SIGABRT, SIGBUS, SIGILL