清理/释放大页内存

目录

清理前

清理后

清理大页内存源代码


清理前

HugePages_Total:      20
HugePages_Free:        0
HugePages_Rsvd:        0
HugePages_Surp:        0
Hugepagesize:       2048 kB

清理后

HugePages_Total:      20
HugePages_Free:       20
HugePages_Rsvd:        0
HugePages_Surp:        0
Hugepagesize:       2048 kB

清理大页内存源代码


//clean dir
int clean_hugepages(const char * huge_dir) 
{
	DIR *dir;
	struct dirent *dirent;
	int dir_fd, fd;
	const char filter[] = "*xk_map*";
	int lock_ret;	

	dir = opendir(huge_dir);
	if(!dir){
		perror("Unable to open hugepage dir:");
		return 1;
	}
	dir_fd = dirfd(dir);
	
	dirent = readdir(dir);
	if(!dirent){
		perror("Unable to read hugepage dir:");
		return 1;
	}	
	while(dirent != NULL){
		if( fnmatch(filter, dirent->d_name, 0) > 0){
			dirent = readdir(dir);
			continue;
		}
		//try and lock the file
		fd = openat(dir_fd, dirent->d_name, O_RDONLY);
		
		//go to next file if fail
		if(fd == -1){
			dirent = readdir(dir);
			continue;
		}
		
        //try non_block lock
		lock_ret = flock(fd, LOCK_EX|LOCK_NB);
		
		if(lock_ret != -1){
			//unlock and remove it
			flock(fd, LOCK_UN);
			unlinkat(dir_fd, dirent->d_name, 0);
		}
		close(fd);
		dirent = readdir(dir);			
	}
	return 0;
}

 

unlinkat: 8 ' xk_map19 '
unlinkat: 8 ' xk_map18 '
unlinkat: 8 ' xk_map17 '
unlinkat: 8 ' xk_map16 '
unlinkat: 8 ' xk_map15 '
unlinkat: 8 ' xk_map14 '
unlinkat: 8 ' xk_map13 '
unlinkat: 8 ' xk_map12 '
unlinkat: 8 ' xk_map11 '
unlinkat: 8 ' xk_map10 '
unlinkat: 8 ' xk_map9 '
unlinkat: 8 ' xk_map8 '
unlinkat: 8 ' xk_map7 '
unlinkat: 8 ' xk_map6 '
unlinkat: 8 ' xk_map5 '
unlinkat: 8 ' xk_map4 '
unlinkat: 8 ' xk_map3 '
unlinkat: 8 ' xk_map2 '
unlinkat: 8 ' xk_map1 '
unlinkat: 8 ' xk_map0 '

 

已标记关键词 清除标记
©️2020 CSDN 皮肤主题: 酷酷鲨 设计师:CSDN官方博客 返回首页