内存加压 | mem-pressure

内存加压 | mem-pressure

介绍内存加压小工具:mem-pressure

本文代码基于:Android 12

使用

usage 信息。

emulator_x86_64:/ # mem-pressure  -h                                                                                                                                                                      
Usage: [OPTIONS]

  -d N: Duration in microsecond to sleep between each allocation.
  -i N: Number of iterations to run the alloc process.
  -o N: The oom_score to set the child process to before alloc.
  -s N: Number of bytes to allocate in an alloc process loop.
Aborted 

其实不需要配置参数,这个程序同样运行的很好,有配置默认参数只是没放在 help 中,代码分析时介绍。直接运行 mem-pressure 二进制程序,会有如下的终端输出和 logcat 日志。

  • 终端输出
130|emulator_x86_64:/ # mem-pressure                                                                                                                                                                      
Child 0 allocated 2654 MB
Child 1 allocated 3985 MB
Child 2 allocated 4614 MB
Child 3 allocated 4907 MB
Child 4 allocated 5097 MB
Child 5 allocated 5140 MB
......
接收 Ctrl + C 信号退出
  • logcat
04-04 20:16:24.229   174   174 I lowmemorykiller: Kill 'com.android.inputmethod.latin' (3294), uid 10086, oom_score_adj 200 to free 13172kB rss, 31664kB swap; reason: device is low on swap (0kB < 151184kB) and thrashing (134%)
04-04 20:16:24.431   174   174 I lowmemorykiller: Kill 'android.ext.services' (3275), uid 10100, oom_score_adj 100 to free 11536kB rss, 33032kB swap; reason: device is low on swap (0kB < 151184kB) and thrashing (220%)
04-04 20:16:24.497   174   174 I lowmemorykiller: Kill 'com.android.launcher3' (3244), uid 10090, oom_score_adj 0 to free 22360kB rss, 49604kB swap; reason: device is low on swap (0kB < 151184kB) and thrashing (273%)
04-04 20:16:30.117   174   174 I lowmemorykiller: Kill 'android.process.acore' (3395), uid 10039, oom_score_adj 200 to free 22840kB rss, 33072kB swap; reason: device is low on swap (2372kB < 151184kB) and thrashing (208%)
04-04 20:16:30.324   174   174 I lowmemorykiller: Kill 'com.android.inputmethod.latin' (3376), uid 10086, oom_score_adj 200 to free 13412kB rss, 31656kB swap; reason: device is low on swap (0kB < 151184kB) and thrashing (152%)
04-04 20:16:30.546   174   174 I lowmemorykiller: Kill 'com.android.launcher3' (3339), uid 10090, oom_score_adj 0 to free 22880kB rss, 49428kB swap; reason: device is low on swap (0kB < 151184kB) and thrashing (200%)

从上面的运行反馈来看,可以很好的理解程序的目的: fork 子进程申请尽可能多的内存。由此会触发 lowmemorykilleroom_adj_score 杀进程的动作。

代码分析

关键参数

system/extras/alloc-stress/mem-pressure.cpp

int main(int argc, char* argv[]) {
    pid_t pid;
    size_t* shared;
    int c, i = 0;

    size_t duration = 1000; //注释 1:子进程申请内存的时间间隔
    int iterations = 0; //注释 2:子进程申请内存交互次数
    const char* oom_score = "899"; //注释 3:子进程 oom_score_adj 的数值
    size_t step_size = 2 * 1024 * 1024;  // 2 MB //注释 4:子进程每次申请的内存大小
    size_t size = step_size; //注释 5:子进程基础内存申请大小

    while ((c = getopt(argc, argv, "hi:d:o:s:")) != -1) {
        switch (c) {
            case 'i':
                iterations = atoi(optarg);
                break;
            case 'd':
                duration = atoi(optarg);
                break;
            case 'o':
                oom_score = optarg;
                break;
            case 's':
                step_size = atoi(optarg);
                break;
            case 'h':
                usage();
                abort();
            default:
                abort();
        }
    }

程序的初始化有这样的默认值设置,见 //注释 1 - 5。然后是入参的解析赋值。

主循环

system/extras/alloc-stress/mem-pressure.cpp

    //注释 1:一个通道,用于获取子进程所分配的内存大小
    shared = (size_t*)mmap(NULL, sizeof(size_t), PROT_READ | PROT_WRITE, MAP_ANONYMOUS | MAP_SHARED,
                           0, 0);

    while (iterations == 0 || i < iterations) {//注释 2:默认无限循环
        *shared = 0;
        pid = fork();
        if (!pid) {
            /* Child *///注释 3:子进程开始内存加压
            add_pressure(shared, size, step_size, duration, oom_score);
            /* Shoud not get here */
            exit(0);
        } else {
            wait(NULL);
            //注释 4:打印子进程的内存分配大小
            printf("Child %d allocated %zd MB\n", i, *shared / 1024 / 1024);
            //注释 5:更新基础分配值,用于加速接近内存边界。
            size = *shared / 2;
        }
        i++;
    }

注释 2,默认参数下,iterations == 0 所以会持续 fork 并申请内存。

注释 3,在子进程中申请内存,开始加压。

注释 4:压力爆表后展示申请的大小。

注释 5:用于加速下一次的内存申请。

下面看注释 3 的 add_pressure 方法。

system/extras/alloc-stress/mem-pressure.cpp

void* alloc_set(size_t size) {
    void* addr = NULL;

    addr = malloc(size);
    if (!addr) {
        printf("Allocating %zd MB failed\n", size / 1024 / 1024);
    } else {
        memset(addr, 0, size);
    }
    return addr;
}

void add_pressure(size_t* shared, size_t size, size_t step_size, size_t duration,
                  const char* oom_score) {
    int fd, ret;
    //注释 1:设置优先级
    fd = open("/proc/self/oom_score_adj", O_WRONLY);
    ret = write(fd, oom_score, strlen(oom_score));
    if (ret < 0) {
        printf("Writing oom_score_adj failed with err %s\n", strerror(errno));
    }
    close(fd);
    //注释 2:申请基础内存
    if (alloc_set(size)) {
        *shared = size;
    }
    //注释 3:持续申请内存
    while (alloc_set(step_size)) {
        size += step_size;
        *shared = size;
        usleep(duration);
    }
}

人如其名,有三步动作,见注释 1 - 3。持续加压的后果就是内存压力爆炸,触发 lmk 杀进程,杀到多少呢?(理论上)杀到子进程的 oom_score_adj 即默认值 899 使得内存得到释放。

总结

这个工具有什么用?

就这个工具本身来说,是通过不断申请内存,直至开始 lmk 杀进程,杀的优先级极限(理论上)是我们设置的 -o 参数。其价值更多在于提供了一种思路,如何模拟 Android 系统内存压力场景。比如说,想得知 launcher 进程在大约多少的内存压力下会被杀?设置 -o 参数为 launcher 优先级

emulator_x86_64:/ # cat /proc/`pidof com.android.launcher3`/oom_score_adj
0

又比如说,我们想指定消耗掉多少内存后,做某种测试等等。

另外还它的姊妹篇可自行阅读:alloc-stress

posted @ 2022-04-04 14:03  秋城  阅读(868)  评论(0编辑  收藏  举报