-
Notifications
You must be signed in to change notification settings - Fork 60
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Try#185,改进的watcher,使用macOS与Linux中的size
命令进行静态内存分析。
#211
Conversation
size
in macOS and Linux to analyze static memory usage before running.size
命令进行静态内存分析。
size
命令进行静态内存分析。size
命令进行静态内存分析。
Windows没有环境实现。 |
unix/watcher_macos.cpp
Outdated
// Find the line containing the relevant sections | ||
if (std::isdigit(buffer[0])) { | ||
// Parse the sizes from the line | ||
size_t size__TEXT, size__DATA; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
怎么变成双下划线了(
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
因为size
输出就是__TEXT
__DATA
, 是可执行文件的文件头标识。
unix/watcher_linux.cpp
Outdated
@@ -24,12 +24,44 @@ void cleanUp(int /*dummy*/) { | |||
exit(0); | |||
} | |||
|
|||
int calculateStaticMemoryUsage(const char* executableName) { | |||
char command[256]; | |||
snprintf(command, sizeof(command), "size %s", executableName); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
没处理长度超过256的情况来着。
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
改成了sizeof(executableName) + 128
, macos下最长为1024, linux下最长为4096
linux:
#define PATH_MAX 4096
macos:
#define MAX_INPUT 1024 /* max bytes in terminal input */
unix/watcher_linux.cpp
Outdated
@@ -25,8 +25,8 @@ void cleanUp(int /*dummy*/) { | |||
} | |||
|
|||
int calculateStaticMemoryUsage(const char* executableName) { | |||
char command[256]; | |||
snprintf(command, sizeof(command), "size %s", executableName); | |||
char command[sizeof(executableName) + 128]; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
这里 sizeof(executableName)
是 8,是指针的大小,不是 executableName
的长度。或许可以使用 PATH_MAX
,是系统最长的路径长度。
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
改成了系统的max_path
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
hard code 是不建议的做法。另外注意一下缩进的统一。
fix with using |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
总体来说很有启发性也是可行的检查方法。
但是直接调用 command line tools,然后 parse 输出结果有很多问题。比如不同系统版本不同的 size
命令输出格式是否统一?未来是否会更改?同时 parse 过程也有一些很 tricky 的点,并不能保证对所有场景都一直有效。
直接去解析 elf 文件自己统计可能是更好的做法。
char buffer[256]; | ||
size_t staticMemoryUsage = 0; | ||
|
||
while (fgets(buffer, sizeof(buffer), output) != NULL) { | ||
// Find the line containing the relevant sections | ||
if (buffer[3] >= '0' && buffer[3] <= '9') { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
[256]
是否足够?buffer[3]
是否适合所有情况?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
256是够的
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
其实主要的难点是跨平台的解析,以及第三方库的安装配置
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
其实主要的难点是跨平台的解析,以及第三方库的安装配置
在我的机子上经过验证,#185 #169 #142中macOS与Linux解决.
在两个watcher源码内加入使用
size
命令的片段,并在fork()
之前就判定静态内存,实测可以防止以上issue的问题出现。我的机子: