cache - # of bytes of page cache memory. rss - # of bytes of anonymous and swap cache memory (includes transparent hugepages).
static void mem_cgroup_charge_statistics(struct mem_cgroup *mem, struct page_cgroup *pc, long size) { ... cpustat = &stat->cpustat[cpu]; if (PageCgroupCache(pc)) __mem_cgroup_stat_add_safe(cpustat, MEM_CGROUP_STAT_CACHE, numpages); else __mem_cgroup_stat_add_safe(cpustat, MEM_CGROUP_STAT_RSS, numpages); static void __mem_cgroup_commit_charge(struct mem_cgroup *mem, struct page_cgroup *pc, enum charge_type ctype, int page_size) { switch (ctype) { case MEM_CGROUP_CHARGE_TYPE_CACHE: case MEM_CGROUP_CHARGE_TYPE_SHMEM: //file cache + shm SetPageCgroupCache(pc); SetPageCgroupUsed(pc); break; case MEM_CGROUP_CHARGE_TYPE_MAPPED: ClearPageCgroupCache(pc); SetPageCgroupUsed(pc); break; default: break; } ///更新统计值 mem_cgroup_charge_statistics(mem, pc, page_size); int mem_cgroup_cache_charge(struct page *page, struct mm_struct *mm, gfp_t gfp_mask) { ... if (page_is_file_cache(page)) return mem_cgroup_charge_common(page, mm, gfp_mask, MEM_CGROUP_CHARGE_TYPE_CACHE, NULL); ///file cache /* shmem */ if (PageSwapCache(page)) { ret = mem_cgroup_try_charge_swapin(mm, page, gfp_mask, &mem); if (!ret) __mem_cgroup_commit_charge_swapin(page, mem, MEM_CGROUP_CHARGE_TYPE_SHMEM); } else ret = mem_cgroup_charge_common(page, mm, gfp_mask, ///shm memory MEM_CGROUP_CHARGE_TYPE_SHMEM, mem);可以看到,cache包含共享内存和file cache
mapped_filemapped_file - # of bytes of mapped file (includes tmpfs/shmem)
void mem_cgroup_update_file_mapped(struct page *page, int val) { ... __mem_cgroup_stat_add_safe(cpustat, MEM_CGROUP_STAT_FILE_MAPPED, val);__do_fault --> page_add_file_rmap --> mem_cgroup_update_file_mapped。
inactive_anoninactive_anon - # of bytes of anonymous and swap cache memory on inactive LRU list.
static int shmem_getpage_gfp(struct inode *inode, pgoff_t index, struct page **pagep, enum sgp_type sgp, gfp_t gfp, int *fault_type) { ... lru_cache_add_anon(page); /** * lru_cache_add: add a page to the page lists * @page: the page to add */ static inline void lru_cache_add_anon(struct page *page) { __lru_cache_add(page, LRU_INACTIVE_ANON); }从这里可以看到,共享内存会增加到INACTIVE_ANON。
inactive_fileinactive_file - # of bytes of file-backed memory on inactive LRU list.文件使用的page cache(不包含共享内存)
static inline void lru_cache_add_file(struct page *page) { __lru_cache_add(page, LRU_INACTIVE_FILE); }add_to_page_cache_lru --> lru_cache_add_file.
示例程序 #include <stdio.h> #include <string.h> #include <stdlib.h> #include <errno.h> #include <unistd.h> #include <sys/stat.h> #include <sys/types.h> #include <sys/ipc.h> #include <sys/shm.h> #define BUF_SIZE 4000000000 #define MYKEY 26 int main(int argc,char **argv){ int shmid; char *shmptr; if((shmid = shmget(MYKEY,BUF_SIZE,IPC_CREAT)) ==-1){ fprintf(stderr,"Create Share Memory Error0m~Z%s\n\a",strerror(errno)); exit(1); } if((shmptr =shmat(shmid,0,0))==(void *)-1){ printf("shmat error!\n"); exit(1); } memset(shmptr,'\0',1000000000); printf("sleep...\n"); while(1) sleep(1); exit(0); }执行程序前后,cgroup memory.stat的值: