您的位置:首页 > 运维架构 > Linux

How to programmatically clear the filesystem memory cache in C++ on a Linux system?

2014-04-11 20:43 2631 查看
I'm writing a benchmark tool in C++ where I want to clear the filesystem memory cache between experiments. I'm aware of the following console commands:
sync
echo 3 > /proc/sys/vm/drop_caches


My question is how can i do this programmatically directly within C++?

Any help is appreciated!

Just write to it :
sync();

std::ofstream ofs("/proc/sys/vm/drop_caches");
ofs << "3" << std::endl;


int fd;
char* data = "3";

sync();
fd = open("/proc/sys/vm/drop_caches", O_WRONLY);
write(fd, data, sizeof(char));
close(fd);


This is a better answer because it includes a sync. Which is important because drop_caches is a debuggingtool
and it does not always get the data on disk before dropping it. – Zan
Lynx Jul
25 '11 at 15:54
A better answer that what? Both of the answers I see include a
sync
. – James
Kanze Jul
25 '11 at 16:12
@James : Mine was edited. – kbok Jul
25 '11 at 22:09
If that's true then this is a bit dangerous - what happens if a process writes a file in the background? Is the data written between
sync() and open() just lost? – Malvineous Jun
17 '12 at 12:33
@ZanLynx: drop_caches is non-destructive and will only free clean caches (ones that have not been synced to disk). Thus calling sync()
before doesn't make the operation any more safe, but rather increases the number of caches that can be dropped. See: /usr/src/linux/Documentation/sysctl/vm.txt – kalaxy Oct
10 '12 at 22:40
@kalaxy: When I used to run -mm kernels in 2008 it was definitely not
safe. – Zan
Lynx Oct
10 '12 at 22:51
If drop_caches is unsafe, your kernel is bugged. No modern kernel will have a problem with this. – Paul
Betts Oct
10 '12 at 23:50
自己编写代码如下:

#include <stdio.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>

#define TRUE 1
#define FALSE 0

typedef unsigned int INT32U;
typedef unsigned char INT8U;

INT8U Get_SysCachedMem(INT32U *cachedmem_size);
INT8U Clean_SysCachedMem(void);

int main(void)
{
INT32U cachedmem_size;
Get_SysCachedMem(&cachedmem_size);
printf("cachedmem_size = %d\n", cachedmem_size);

Clean_SysCachedMem();

Get_SysCachedMem(&cachedmem_size);
printf("cachedmem_size = %d\n", cachedmem_size);

return 0;
}

INT8U Get_SysCachedMem(INT32U *cachedmem_size)
{
char 	buffer[128];
char 	args01[64];
INT32U  args02;
char 	args03[64];
FILE 	*fp = NULL;
INT8U	isGetCachedArgs = FALSE;

fp = fopen("/proc/meminfo", "r");
fseek(fp, 0, SEEK_SET);

//从/proc/meminfo文件中一行一行过滤出cached参数
memset(buffer, 0, sizeof(buffer));
while (fgets(buffer, sizeof(buffer), fp) != NULL)
{
sscanf(buffer, "%s\t%ld%s", args01, &args02, args03);
printf("buffer = %s %d %s\n", args01, args02, args03);

if(strncmp(args01, "Cached:", strlen("Cached:")) == 0)
{
isGetCachedArgs = TRUE;
break;
}

memset(buffer, 0, sizeof(buffer));
}
fclose(fp);

if( !isGetCachedArgs )
{
*cachedmem_size = 0;
return FALSE;
}

*cachedmem_size = args02;

return TRUE;
}

INT8U Clean_SysCachedMem(void)
{
int fd;
const char* data = "3";

sync();
fd = open("/proc/sys/vm/drop_caches", O_WRONLY);
if(fd < 0)
{
return FALSE;
}

write(fd, data, sizeof(char));
close(fd);

return TRUE;
}


测试已通过
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: