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/
33 changes: 33 additions & 0 deletions unix/watcher_linux.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
*/

#include <cassert>
#include <limits.h>
#include <csignal>
#include <cstdio>
#include <cstdlib>
Expand All @@ -24,12 +25,44 @@ void cleanUp(int /*dummy*/) {
exit(0);
}

int calculateStaticMemoryUsage(const char* executableName) {
char command[PATH_MAX];
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') {
Comment on lines +36 to +41
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.

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

// 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
34 changes: 34 additions & 0 deletions unix/watcher_macos.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
*
*/

#include <limits.h>
#include <algorithm>
#include <cassert>
#include <csignal>
Expand All @@ -16,6 +17,7 @@
#include <string>
#include <sys/resource.h>
#include <sys/sysctl.h>
#include <sys/syslimits.h>
#include <sys/time.h>
#include <sys/types.h>
#include <sys/wait.h>
Expand All @@ -35,6 +37,32 @@ std::string getCpuBrandString() {
return std::string(buf, buflen);
}

int calculateStaticMemoryUsage(const char* executableName) {
char command[PATH_MAX];
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;
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 +71,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
Loading