linux - 获取正在换出的页面的 pid

标签 linux linux-kernel operating-system kernel systemtap

我的目标是找出正在换出的页面的进程 ID。 Linux 内核函数 swap_writepage() 在后备存储上交换页面时将指向结构页面的指针 作为正式参数的一部分。所有换出操作都由“kswapd”进程完成。我需要找出其页面作为参数传递到 swap_writepage() 函数中的进程的 pid(s)。为了做到这一点,我能够使用 rmap 结构找到与该页面关联的所有页表条目

如何从 ptestruct page 获取 pid?我使用 sytemtap 获取结构页面指针的值,在 swap_writepage() 函数中作为参数接收。此外,pid() 函数打印当前正在运行的进程的 pid,而不是该页面所属的进程的 pid,它总是提供 kswapd 进程。

最佳答案

这是现代 Linux 中如何使用反向映射的示例(从 lxr 复制):

1435 static int try_to_unmap_anon(struct page *page, enum ttu_flags flags)
1436 {
1437         struct anon_vma *anon_vma;
1438         struct anon_vma_chain *avc;
1439         int ret = SWAP_AGAIN;
1440 
1441         anon_vma = page_lock_anon_vma(page);
1442         if (!anon_vma)
1443                 return ret;
1444 
1445         list_for_each_entry(avc, &anon_vma->head, same_anon_vma) {
1446                 struct vm_area_struct *vma = avc->vma;
1447                 unsigned long address;
1448 
1449                 /*
1450                  * During exec, a temporary VMA is setup and later moved.
1451                  * The VMA is moved under the anon_vma lock but not the
1452                  * page tables leading to a race where migration cannot
1453                  * find the migration ptes. Rather than increasing the
1454                  * locking requirements of exec(), migration skips
1455                  * temporary VMAs until after exec() completes.
1456                  */
1457                 if (PAGE_MIGRATION && (flags & TTU_MIGRATION) &&
1458                                 is_vma_temporary_stack(vma))
1459                         continue;
1460 
1461                 address = vma_address(page, vma);
1462                 if (address == -EFAULT)
1463                         continue;
1464                 ret = try_to_unmap_one(page, vma, address, flags);
1465                 if (ret != SWAP_AGAIN || !page_mapped(page))
1466                         break;
1467         }
1468 
1469         page_unlock_anon_vma(anon_vma);
1470         return ret;
1471 }

此示例显示用于取消映射页面的 rmap。所以在 ->mapping 字段中的每个匿名页面都包含一个 anon_vma 对象。 anon_vma 包含页面映射到的 vma 区域列表。有 vma 就有 mm,有 mm 就有 task_struct。就是这样。如果您有任何疑问 - 这是插图 reverse mapping

Daniel P. Bovet、Marco Cesati 了解 Linux 内核第 17.2 章

关于linux - 获取正在换出的页面的 pid,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29652268/

相关文章:

java - 有没有办法从命令行指定 Ant 使用哪个 JVM?

java - 操作系统对 Ruby 开发有重大影响吗?

linux - list_head结构如何在内核中用于调度

linux-kernel - 有人可以帮我替换 block 设备驱动程序上的 “lock_kernel”吗?

java - 错误 : could not find libjava. 所以,错误:找不到 Java 2 运行时环境

operating-system - 如何将NSURL转换为CFURLRef

linux - 安装 Composer (权限被拒绝)Symfony

linux - 引导加载程序(例如 grub、lilo...)如何找到内核镜像?

python - 如何使用gzip有效地将许多小文件压缩为许多小.tar.gz文件?

linux - "make oldconfig"在 Linux 内核 makefile 中究竟做了什么?