gpt4 book ai didi

C:知道何时读取或写入变量

转载 作者:塔克拉玛干 更新时间:2023-11-03 00:05:40 26 4
gpt4 key购买 nike

目前在 arm Xilinx 开发平台上工作。使用 linux 和 C。

我映射了一些页面。例如a = mmap(...) 。我想知道哪些页面最近最少使用,以便我可以将它们复制到其他地方。

最佳答案

Linux 内核本身会跟踪 LRU 页面,例如,找出哪些页面在内存争用时要交换或不交换。

如果你想在你的库中使用它,你可以从用户空间读取 LRU 标志:

  • 在/proc/pid/pagemap 中遍历要跟踪的虚拟页面条目并记住页面框架编号
  • 从/proc/kpageflags中读取这些页面对应的flags

现在你有几个标志,你可以将它们用于你的逻辑:

 3. UPTODATE  page has up-to-date data
ie. for file backed page: (in-memory data revision >= on-disk one)
4. DIRTY page has been written to, hence contains new data
ie. for file backed page: (in-memory data revision > on-disk one)
8. WRITEBACK page is being synced to disk

[LRU related page flags]
5. LRU page is in one of the LRU lists
6. ACTIVE page is in the active LRU list
18. UNEVICTABLE page is in the unevictable (non-)LRU list
It is somehow pinned and not a candidate for LRU page reclaims,
eg. ramfs pages, shmctl(SHM_LOCK) and mlock() memory segments
2. REFERENCED page has been referenced since last LRU list enqueue/requeue
9. RECLAIM page will be reclaimed soon after its pageout IO completed
11. MMAP a memory mapped page
12. ANON a memory mapped page that is not part of a file
13. SWAPCACHE page is mapped to swap space, ie. has an associated swap entry
14. SWAPBACKED page is backed by swap/RAM

更多标志在 this document 中描述

还有一个名为 soft-dirty 的功能,它专门用于跟踪最近的写入(忽略读取)(请参阅 Soft Dirty 文档)。您可以通过/proc 清除软脏标志,然后通过/proc/pid/pagemap 再次读取它们。也许这对您的应用程序也很有用。

关于C:知道何时读取或写入变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47137413/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com