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:
My question is how can i do this programmatically directly within C++?
Any help is appreciated!
Just write to it :
自己编写代码如下:
测试已通过
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; }
测试已通过
相关文章推荐
- Robbie's Notes on File System Security in Linux (and comparison to Windows NT)
- How to Mount and Unmount Filesystem / Partition in Linux (Mount/Umount Command Examples)
- How to Use `strace` to Trace the System Call in Linux
- How to know is the file exist in C++ - 在C++中如何判断文件是否存在
- ZZ How to Flush Memory Cache on Linux Server
- How to count the number of threads in a process on Linux
- How to Use the Dynamic Link Library in C++ Linux (C++调用Delphi写的.so文件)
- How can I force Python's file.write() to use the same newline format in Windows as in Linux (“\r\n”
- 【Ant】How to print all the system properties in Ant build file
- How to use the System Restore API to save and to restore system data in Visual C++
- How to get the size of file in C++ - 在C++中如何获取文件的大小
- 《How to Use the TimeDistributed Layer for Long Short-Term Memory Networks in Python》学习笔记
- This tip shows how to extract the width and height from a bitmap source file on disk.
- How To Get The Size of RAW Devices On Linux
- How to get the kernel memory range on OpenSolaris
- How to Modify the web.config file in SharePoint using SPWebConfigModification?
- Linux - How to remove the new line at the end of file when using vim
- How to Mount NTFS File System in CentOS 7 / RHEL 7
- How to check the usage of net ports in linux?
- How to clear the views which are held in the ListView's RecycleBin?