본문 바로가기
Research Engineer

meminfo를 사용을 통한 user level application 구현 (예제)

by 풍야 2018. 2. 11.

1. meminfo 사용 관련 application example
 
링크 : http://wwwcdf.pd.infn.it/MLO/memory.c

#include <ctype.h> #include <fcntl.h> #define BUFFER_LEN 128 static int memory(void) { int retVal = EXIT_FAILURE; int fd; if ((fd = open("/proc/meminfo", O_RDONLY)) < 0) { fputs("Couldn't open /proc/meminfo\n", stderr); perror("open"); } else { char buffer[BUFFER_LEN]; size_t len; if ((len = read(fd, buffer, sizeof(buffer)-1)) < 0) { fputs("Error read from /proc/meminfo\n", stderr); perror("read"); } else { const char *pc = buffer; char *end; unsigned long total, used, free; /* buffer[len]='\0'; puts(buffer); */ while (*pc++ != '\n') {} while (!isspace((unsigned char) *pc)) pc++; total = strtoul(pc, &end, 0) / 1024; used = strtoul(end, &end, 0) / 1024; free = strtoul(end, &end, 0) / 1024; fprintf(stdout, "Total memory: %lu kBytes (%lu kBytes used, %lu kBytes free)\n", total, used, free); retVal = EXIT_SUCCESS; } close(fd); } return retVal; } #endif /* __linux__ */


2. 안드로이드에서 제공되는 오픈소스

링크 : http://www.dre.vanderbilt.edu/~schmidt/android/android-4.0/system/extras/procrank/procrank.c

void print_mem_info() {
    char buffer[256];
    int numFound = 0;

    int fd = open("/proc/meminfo", O_RDONLY);

    if (fd < 0) {
        printf("Unable to open /proc/meminfo: %s\n", strerror(errno));
        return;
    }

    const int len = read(fd, buffer, sizeof(buffer)-1);
    close(fd);

    if (len < 0) {
        printf("Empty /proc/meminfo");
        return;
    }
    buffer[len] = 0;

    static const char* const tags[] = {
            "MemTotal:",
            "MemFree:",
            "Buffers:",
            "Cached:",
            NULL
    };
    static const int tagsLen[] = {
            9,
            8,
            8,
            7,
            0
    };
    long mem[] = { 0, 0, 0, 0 };

    char* p = buffer;
    while (*p && numFound < 4) {
        int i = 0;
        while (tags[i]) {
            if (strncmp(p, tags[i], tagsLen[i]) == 0) {
                p += tagsLen[i];
                while (*p == ' ') p++;
                char* num = p;
                while (*p >= '0' && *p <= '9') p++;
                if (*p != 0) {
                    *p = 0;
                    p++;
                    if (*p == 0) p--;
                }
                mem[i] = atoll(num);
                numFound++;
                break;
            }
            i++;
        }
        p++;
    }

    printf("RAM: %ldK total, %ldK free, %ldK buffers, %ldK cached\n",
            mem[0], mem[1], mem[2], mem[3]); 

} 


3. 안드로이드에서 제공되는 메모리 정보 infomation

(커널)

http://egloos.zum.com/rousalome/v/9966593
http://iloveriver.egloos.com/m/6192450

(안드로이드)

http://www.neuromancer.kr/t/145-proc-meminfo/267

http://jaynstory.tistory.com/42

http://blog.naver.com/PostView.nhn?blogId=bl2019&logNo=10186089770

http://droid-tip.blogspot.kr/2013/06/android-adb-shell.html

http://strongslaves.tistory.com/30

http://ecogeo.tistory.com/255

https://elinux.org/Android_Memory_Usage

https://android.googlesource.com/platform/system/core/+/dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0/toolbox/vmstat.c

http://webkebi.zany.kr:9003/board/bView.asp?bCode=91179649&aCode=2511

https://android.googlesource.com/platform/system/extras/+/android-6.0.1_r61/procrank/procrank.c