如何强制页面发生故障,即使它已经在 tlb 中?
我正在尝试通过跟踪一段时间内的页面错误来编写一个玩具工作集估算器。每当一个页面出现错误时,我想记录它被触摸的情况。当我尝试跟踪对已存在页面的访问时,该方案失败了。如果读取或写入页面而不触发错误,我将无法跟踪访问。
那么,我希望能够在页面访问时导致发生“轻量级”错误。我曾经听说过一些方法,但我不明白它为什么有效,所以它没有留在我的脑海中。也许有点脏?
I'm trying to write a toy working set estimator, by keeping track of page faults over a period of time. Whenever a page is faulted in, I want to record that it was touched. The scheme breaks down when I try to keep track of accesses to already-present pages. If a page is read from or written to without triggering a fault, I have no way of tracking the access.
So then, I want to be able to cause a "lightweight" fault to occur on a page access. I've heard of some method at some point, but I didn't understand why it worked so it didn't stick in my mind. Dirty bit maybe?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以将
mprotect
与一起使用PROT_NONE
(“无法访问页面”)。那么对给定页面的任何访问都会导致错误。You can use
mprotect
withPROT_NONE
("Page cannot be accessed"). Then any access to the given page will cause a fault.通常的方法是简单地清除页面的“当前”位,同时将页面保留在内存中并将必要的内核数据结构保留在适当的位置,以便内核知道这一点。
然而,根据所讨论的体系结构,您可能有更好的选择 - 例如,在 x86 上,有一个“已访问”标志(PTE 中的第 5 位),只要在线性地址转换中使用 PTE,就会设置该标志。您可以随时清除该位,硬件将设置它来记录页面被触摸。
使用这两种方法中的任何一种,您都需要从 TLB 中清除该页面的缓存翻译 - 在 x86 上,您可以使用
INVLPG
指令。The usual way to do this is to simply clear the "present" bit for the page, while leaving the page in memory and the necessary kernel data structures in place so that the kernel knows this.
However, depending on the architecture in question you may have better options - for example, on x86 there is an "Accessed" flag (bit 5 in the PTE) that is set whenever the PTE is used in a linear address translation. You can simply clear this bit whenever you like, and the hardware will set it to record that the page was touched.
Using either of these methods you will need to clear the cached translation for that page out of the TLB - on x86 you can use the
INVLPG
instruction.