免费注册 查看新帖 |

Chinaunix

广告
  平台 论坛 博客 文库
最近访问板块 发新帖
楼主: 南海一钓叟
打印 上一主题 下一主题

那为大侠给俺讲讲cache和volatie变量的关系 [复制链接]

论坛徽章:
0
51 [报告]
发表于 2008-03-04 16:29 |只看该作者
刚才在sun网站上找到一个关于volatile变量的文章,感觉说的挺好的了。


Declaring a Variable Volatile


volatile is a keyword that must be applied when declaring any variable that will reference a device register. Without the use of volatile, the compile-time optimizer can inadvertently delete important accesses. Neglecting to use volatile might result in bugs that are difficult to track down.

The correct use of volatile is necessary to prevent elusive bugs. The volatile keyword instructs the compiler to use exact semantics for the declared objects, in particular, not to remove or reorder accesses to the object. Two instances where device drivers must use the volatile qualifier are:

When data refers to an external hardware device register, that is, memory that has side effects other than just storage. Note, however, that if the DDI data access functions are used to access device registers, you do not have to use volatile.

When data refers to global memory that is accessible by more than one thread, that is not protected by locks, and that relies on the sequencing of memory accesses. Using volatileconsumes fewer resources than using lock.

The following example uses volatile. A busy flag is used to prevent a thread from continuing while the device is busy and the flag is not protected by a lock:

    while (busy) {
      /* do something else */
      }The testing thread will continue when another thread turns off the busy flag:

    busy = 0;Because busy is accessed frequently in the testing thread, the compiler can potentially optimize the test by placing the value of busy in a register and test the contents of the register without reading the value of busy in memory before every test. The testing thread would never see busy change and the other thread would only change the value of busy in memory, resulting in deadlock. Declaring the busy flag as volatile forces its value to be read before each test.


--------------------------------------------------------------------------------
Note –
An alternative to the busy flag is to use a condition variable. See Condition Variables in Thread Synchronization.


--------------------------------------------------------------------------------

When using the volatile qualifier, avoid the risk of accidental omission. For example, the following code

    struct device_reg {
     volatile uint8_t csr;
     volatile uint8_t data;
     };
     struct device_reg *regp;is preferable to the next example:

    struct device_reg {
     uint8_t csr;
     uint8_t data;
     };
     volatile struct device_reg *regp;Although the two examples are functionally equivalent, the second one requires the writer to ensure that volatile is used in every declaration of type struct device_reg. The first example results in the data being treated as volatile in all declarations and is therefore preferred. As mentioned above, using the DDI data access functions to access device registers makes qualifying variables as volatile unnecessary.

论坛徽章:
0
52 [报告]
发表于 2008-03-04 16:38 |只看该作者
mark一下,zx_wing讲的不错

论坛徽章:
0
53 [报告]
发表于 2008-03-04 20:44 |只看该作者
高手真多啊!
以前俺只知道大意是让编译器不要进行优化(即不要使用寄存器已经缓存的值, 而要访问内存).
典型的例子是IO端口映射的地址, 共享内存等.

论坛徽章:
0
54 [报告]
发表于 2008-03-04 21:54 |只看该作者

回复 #30 zx_wing 的帖子

cache一致不一定要写回内存,甚至不需要通过总线,比如双核系统共享写回式 L2 cache

论坛徽章:
0
55 [报告]
发表于 2008-03-04 21:58 |只看该作者
原帖由 yangtou 于 2008-3-4 21:54 发表
cache一致不一定要写回内存,甚至不需要通过总线,比如双核系统共享写回式 L2 cache

如两核共享L2 cache,是无需写回内存。如果两核的L2 cache是分开的,则必写回内存
这个是intel前端总线架构决定的,参考资料我只知道mindshare那边讲P4前端总线的书,我的观点也是从那里得来的。
如果真有这种cache分开不同步回内存的CPU,希望能看到有关资料。

论坛徽章:
0
56 [报告]
发表于 2008-03-04 21:59 |只看该作者
原帖由 yangtou 于 2008-3-4 21:54 发表
cache一致不一定要写回内存,甚至不需要通过总线,比如双核系统共享写回式 L2 cache

哦,我没看到兄台说的是共享写回式 L2 cache
你说的是对的,这种情况不需要同步回内存,多谢指出。

论坛徽章:
1
2015年辞旧岁徽章
日期:2015-03-03 16:54:15
57 [报告]
发表于 2008-03-05 11:38 |只看该作者
volatile明显是要和NUMA架构过不去

论坛徽章:
0
58 [报告]
发表于 2008-03-05 13:54 |只看该作者
强,zx_wing 分析的透彻

论坛徽章:
0
59 [报告]
发表于 2008-03-05 15:45 |只看该作者
好文章,做个记号!

论坛徽章:
0
60 [报告]
发表于 2008-03-06 22:12 |只看该作者
原帖由 南海一钓叟 于 2008-2-29 13:44 发表
上次面试被考官问到volatie变量,他说这个要结合cache来讲才能说的清,那位大侠给讲一下。


这里说的“cache”不是通常意义上的缓存,即CPU内部缓存或磁盘缓存等。它是一种更加广义上的缓存概念:以提高访问效率为目的,把数据或处理后的数据保存起来加以利用的现象都可以称为缓存。

题目中的缓存实际上指的是寄存器,与CPU缓存无关。编译器的优化可以把本来在内存中进行访问的数据装入(缓存)到寄存器中以提高访问效率。然而这种优化在一些情况下可带来问题;此时,应该用 volatile 告诉编译器不要进行这种优化以避免问题的出现。

[ 本帖最后由 whyglinux 于 2008-3-6 23:02 编辑 ]
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP