`
duoerbasilu
  • 浏览: 1483168 次
文章分类
社区版块
存档分类
最新评论

page_cache和buffer_cache的直观理解

 
阅读更多

在这篇博客上http://blog.csdn.net/cheungjustin/archive/2010/08/31/5853399.aspx

看到关于page cachebuffer cache的解释。

针对博客里面一开始提出的问题:一个程序,需要读入文件,它第一次执行时间和第二次执行时间一样吗?将文件改名后呢,umount文件系统后再mount上呢?

这篇文章的作者的理解是程序第一次执行时,读取文件过程,从磁盘读取到buffer cache page cache再到应用程序的进程空间。第二次执行时由于buffer cache中已经有了部分或全部的文件内容,这样执行时间就要减少些。当然这也要视情景而定,具体有很多。当文件改名后,其数据的物理地址未变,程序执行时通过文件指针仍能在内存上找到正确数据所以执行时间应该和第二次相同,但是umount文件系统后,这些buffer cache page cache信息应该会删除,因为若重新挂载另一文件系统时,同样的文件名可能不对应同一个文件,所以这时执行时间应该和第一次一样。

为了验证这种理解是否正确,写了一个读文件的程序来测试。程序代码如下:

#include <stdio.h>

#include <stdlib.h>

#include <sys/time.h>

#include <time.h>

int main(void)

{

FILE *fp=fopen("/mnt/dw.c","r+b");

struct timeval t1, t2;

long spend_us;

char buf[5];

int size;

gettimeofday(&t1, NULL);

while((size = fread(buf, sizeof(char), 5, fp)) > 0);

gettimeofday(&t2, NULL);

spend_us = 1000000 * (t2.tv_sec - t1.tv_sec) + (t2.tv_usec - t1.tv_usec);

printf("spend time:%ld/n", spend_us);

fclose(fp);

return 0;

}

其中dw.c是一个大小为107k的源代码文件。/mnt/下挂载的是/dev/sda5分区

第一次运行程序:./cache_test

程序运行时间:spend time:5596

第二次运行:./cache_test

程序运行时间:spend time:2241

dw.c改了名字之后再运行时

程序运行时间:spend time:2062

umount /mnt,然后再mount /dev/sda5 /mnt/

然后再运行程序,

运行时间:spend time:5584

根据实际的测试效果来看,按照以上的理解是正确的。

分享到:
评论

相关推荐

    ORACLE DBA 手册

    设置db_cache_advice: 12 调整优化模式 optimizer_mode 12 调整优化模式optimizer_index_caching 13 调整优化模式optimizer_index_cost_adj 13 调整优化模式optimizer_max_permutations 14 并行优化 14 parallel_...

    ARM_Linux启动分析.pdf

    创建页cache(内存页hash表初始化,page_cache_init()) 创建信号队列cache("signal_queue",signals_init()) 初始化内存inode表(inode_init()) 创建内存文件描述符表("filp_cache",file_table_init()) ...

    微软内部资料-SQL性能优化2

    The system address space is where the kernel, executive, HAL, boot drivers, page tables, pool, and system cache reside. For specific information regarding address space layout, refer to Inside ...

    php.ini-development

    user_ini.cache_ttl = 300 ;;;;;;;;;;;;;;;;;;;; ; Language Options ; ;;;;;;;;;;;;;;;;;;;; ; Enable the PHP scripting language engine under Apache. ; http://php.net/engine engine = On ; This directive...

    springmodules-cache.xsd&springmodules-ehcache.xsd.rar

    解决web.xml中 &lt;page-encoding&gt;UTF-8&lt;/page-encoding&gt;报错。错误提示: cvc-complex-type.2.4.a: Invalid content was found starting with element 'page-encoding'. One of '{"http:// java.sun....

    PHP 实现页面静态化的几种方法

    1、通过buffer来实现 需要用file_put_contents ob_get_clean()...$pageCache = str_replace('submit2','login',ob_get_contents());//将缓存去中的内容替换 ob_end_clean(); echo $mtime; echo $pageCache; 2、通过$_SE

    uboott移植实验手册及技术文档

    实验三 移植U-Boot-1.3.1 实验 【实验目的】 了解 U-Boot-1.3.1 的代码结构,掌握其移植方法。 【实验环境】 1、Ubuntu 7.0.4发行版 2、u-boot-1.3.1 3、FS2410平台 4、交叉编译器 arm-softfloat-linux-gnu-...

    asp.net 开发的一些常用技巧

    不使用页面缓存: 你可以在不想被缓存的页面Page_Load事件中加上如下代码 代码如下:Response.Expires = 0; Response.Buffer = true; Response.ExpiresAbsolute = DateTime.Now.AddSeconds(-1); Response.AddHeader...

    Wrox.Professional.Linux.Kernel.Architecture.

    Chapter 16: Page and Buffer Cache949 Chapter 17: Data Synchronization . 989 Chapter 18: Page Reclaim and Swapping. 1023 Chapter 19: Auditing 1097 Appendix A: Architecture Specifics . 1117 Appendix B:...

    SimIt-ARM-3.0 ARM指令模拟器

    SimIt-ARM-3.0 给予命令行ARM指令模拟器,短小精悍,是研究ARM处理器的好工具,该模拟器既可以运行用户级别的ELF程序,又可以模拟运行Linux操作系统;提供了简单易用的调试命令,可以逐条跟踪指令的执行。...

    Oracle DBA的UNIX袖珍参考手册之服务器监控

    Buffer cache hit ratio (for the Unix JFS buffer cache) for write requests pread/s Number of reads per second from disk pwrit/s Number of writes per second to disk 3. 使用 vmstat vmstat 是通用的 UNIX...

    Oracle9i的init.ora参数中文说明

    说明: 与 NLS_TIME_TZ_FORMAT 相似, 其中的一对值指定 TIMESTAMP 数据类型的默认值, 该类型除存储 YEAR, MONTH 和 DAY 日期值, HOUR, MINUTE 和 SECOND 时间值, 还存储 TIMEZONE_HOUR 和 TIMEZONE_MINUTE。...

    SQL Server性能调优之缓存

    在执行任何查询时,SQL Server都会将数据...Data Cache是存储数据页(Data Page)的缓冲区,当SQL Server需要读取数据文件(File)中的数据页(Data Page)时,SQL Server会把整个Page都调入内存(内存中的一个Page叫

    验证码下载jsp

    &lt;%@ page language="java" import="java.awt.*,java.awt.image.*,java.util.*,javax.imageio.*" pageEncoding="utf-8"%&gt; String path = request.getContextPath(); String basePath = request.getScheme()+"://"+...

    JLink_Windows_V648.zip

    DLL: Under some circumstances Flash Cache was not cleaned after erase operations. DLL: Unsecure read protection for STM32L151xx series devices, fixed. DLL: Unsecure write protection for STM32L151xxx ...

    深入理解计算机系统(英文版)

    Contents Preface i 1 Introduction 1 1.1 Information isBits inContext . . . . . . ....1.2 Programs areTranslated byOtherPrograms intoDifferent Forms ....1.3 ItPays toUnderstandHowCompilation SystemsWork ....

    Wiley.Publishing.Professional.Linux.Kernel.Architecture.2008.pdf

    Chapter 16: Page and Buffer Cache..... . . . . . 949 Chapter 17: Data Synchronization ..... . . . . . . 989 Chapter 18: Page Reclaim and Swapping.... . . . . . . . 1023 Chapter 19: Auditing ....... . ...

    Bochs - The cross platform IA-32 (x86) emulator

    - NE2K: Fixed "send buffer" command issue on big endian hosts - USB - converted common USB code plus devices to the new 'usb_common' plugin Now the USB device classes no longer exist twice if both...

    微软内部资料-SQL性能优化3

    Page An 8-KB data page or index page. Key Row lock within an index. Key-range A key-range. Used to lock ranges between records in a table to prevent phantom insertions or deletions into a set of ...

    SQLServer2008查询性能优化 2/2

    2.4.4 Buffer Cache Hit Ratio 24 2.4.5 Page Life Expectancy 24 2.4.6 Checkpoint Pages/sec 24 2.4.7 Lazy writes/sec 24 2.4.8 Memory Grants Pending 25 2.4.9 Target Server Memory(KB)和Total Server ...

Global site tag (gtag.js) - Google Analytics