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
Closed
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -68,4 +68,4 @@ build/
build-debug/
.idea/
cmake-build-debug/

.vscode/
32 changes: 32 additions & 0 deletions unix/watcher_linux.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,44 @@ void cleanUp(int /*dummy*/) {
exit(0);
}

int calculateStaticMemoryUsage(const char* 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

snprintf(command, sizeof(command), "size %s", executableName);

FILE* output = popen(command, "r");
if (output == NULL)
return -1;

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') {
// Parse the sizes from the line
size_t size_text, size_data, size_bss;
sscanf(buffer, "%zu%zu%zu", &size_text, &size_data, &size_bss);
staticMemoryUsage += size_text + size_data + size_bss;
}
}

pclose(output);

return staticMemoryUsage;
}

auto main(int /*argc*/, char *argv[]) -> int {
int timeLimit = 0, memoryLimit = 0;
sscanf(argv[5], "%d", &timeLimit);
timeLimit = (timeLimit - 1) / 1000 + 1;
sscanf(argv[6], "%d", &memoryLimit);
memoryLimit *= 1024 * 1024;
int staticMemoryUsage = calculateStaticMemoryUsage(argv[1]);
if (staticMemoryUsage != -1 && staticMemoryUsage > memoryLimit) {
printf("0\n");
printf("%d\n", staticMemoryUsage);
return 0;
}
pid = fork();

if (pid > 0) {
Expand Down
32 changes: 32 additions & 0 deletions unix/watcher_macos.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,32 @@ std::string getCpuBrandString() {
return std::string(buf, buflen);
}

int calculateStaticMemoryUsage(const char* executableName) {
char command[sizeof(executableName) + 128];
snprintf(command, sizeof(command), "size %s", executableName);

FILE* output = popen(command, "r");
if (output == NULL)
return -1;

char buffer[256];
size_t staticMemoryUsage = 0;

while (fgets(buffer, sizeof(buffer), output) != NULL) {
// 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, 是可执行文件的文件头标识。

sscanf(buffer, "%zu%zu", &size__TEXT, &size__DATA);
staticMemoryUsage += size__TEXT + size__DATA;
}
}

pclose(output);

return staticMemoryUsage;
}

int main(int argc, char *argv[]) {
int isAppleSilicon = getCpuBrandString().find("Apple") != std::string::npos;

Expand All @@ -43,6 +69,12 @@ int main(int argc, char *argv[]) {
timeLimit = (timeLimit - 1) / 1000 + 1;
sscanf(argv[6], "%d", &memoryLimit);
memoryLimit *= 1024 * (isAppleSilicon ? 4 : 1);
int staticMemoryUsage = calculateStaticMemoryUsage(argv[1]);
if (staticMemoryUsage != -1 && staticMemoryUsage > memoryLimit) {
printf("0\n");
printf("%d\n", staticMemoryUsage);
return 0;
}
pid = fork();

if (pid > 0) {
Expand Down