Skip to content
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

Closed
wants to merge 14 commits into from

Conversation

tofucurd
Copy link

@tofucurd tofucurd commented Jun 10, 2023

在我的机子上经过验证,#185 #169 #142中macOS与Linux解决.
在两个watcher源码内加入使用size命令的片段,并在fork()之前就判定静态内存,实测可以防止以上issue的问题出现。
我的机子:

  • macOS 13.1, Apple M1 Pro, 16 GB
  • (VMware虚拟机)RHEL 9.1 Aarch64, 8 GB

@tofucurd tofucurd changed the title New watcher using size in macOS and Linux to analyze static memory usage before running. 改进的watcher,使用macOS与Linux中的size命令进行静态内存分析。 Jun 10, 2023
@tofucurd tofucurd changed the title 改进的watcher,使用macOS与Linux中的size命令进行静态内存分析。 Try#185,改进的watcher,使用macOS与Linux中的size命令进行静态内存分析。 Jun 10, 2023
@tofucurd
Copy link
Author

Windows没有环境实现。

// Find the line containing the relevant sections
if (std::isdigit(buffer[0])) {
// Parse the sizes from the line
size_t size__TEXT, size__DATA;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

怎么变成双下划线了(

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

因为size输出就是__TEXT __DATA, 是可执行文件的文件头标识。

@@ -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);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

没处理长度超过256的情况来着。

Copy link
Author

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 */

@@ -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];
Copy link
Member

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,是系统最长的路径长度。

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

改成了系统的max_path

Copy link
Member

@Dust1404 Dust1404 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

hard code 是不建议的做法。另外注意一下缩进的统一。

@tofucurd
Copy link
Author

fix with using PATH_MAX in limits.h

Copy link
Member

@Dust1404 Dust1404 left a 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 文件自己统计可能是更好的做法。

Comment on lines +36 to +41
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') {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[256] 是否足够?buffer[3] 是否适合所有情况?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

256是够的

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

其实主要的难点是跨平台的解析,以及第三方库的安装配置

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

其实主要的难点是跨平台的解析,以及第三方库的安装配置

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants