Skip to content

Commit 1dd7114

Browse files
authored
Merge pull request #303 from tatref/PhysicalPageFlags-to-core
feat: move PhysicalPageFlags to procfs-core
2 parents e734c74 + 546f574 commit 1dd7114

File tree

4 files changed

+95
-78
lines changed

4 files changed

+95
-78
lines changed

procfs-core/src/kpageflags.rs

+90
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
use bitflags::bitflags;
2+
3+
#[cfg(feature = "serde1")]
4+
use serde::{Deserialize, Serialize};
5+
6+
//const fn genmask(high: usize, low: usize) -> u64 {
7+
// let mask_bits = size_of::<u64>() * 8;
8+
// (!0 - (1 << low) + 1) & (!0 >> (mask_bits - 1 - high))
9+
//}
10+
11+
bitflags! {
12+
/// Represents the fields and flags in a page table entry for a memory page.
13+
#[cfg_attr(feature = "serde1", derive(Serialize, Deserialize))]
14+
#[derive(Copy, Clone, Debug, Hash, Eq, PartialEq, PartialOrd, Ord)]
15+
pub struct PhysicalPageFlags: u64 {
16+
/// The page is being locked for exclusive access, e.g. by undergoing read/write IO
17+
const LOCKED = 1 << 0;
18+
/// IO error occurred
19+
const ERROR = 1 << 1;
20+
/// The page has been referenced since last LRU list enqueue/requeue
21+
const REFERENCED = 1 << 2;
22+
/// The page has up-to-date data. ie. for file backed page: (in-memory data revision >= on-disk one)
23+
const UPTODATE = 1 << 3;
24+
/// The page has been written to, hence contains new data. i.e. for file backed page: (in-memory data revision > on-disk one)
25+
const DIRTY = 1 << 4;
26+
/// The page is in one of the LRU lists
27+
const LRU = 1 << 5;
28+
/// The page is in the active LRU list
29+
const ACTIVE = 1 << 6;
30+
/// The page is managed by the SLAB/SLOB/SLUB/SLQB kernel memory allocator. When compound page is used, SLUB/SLQB will only set this flag on the head page; SLOB will not flag it at all
31+
const SLAB = 1 << 7;
32+
/// The page is being synced to disk
33+
const WRITEBACK = 1 << 8;
34+
/// The page will be reclaimed soon after its pageout IO completed
35+
const RECLAIM = 1 << 9;
36+
/// A free memory block managed by the buddy system allocator. The buddy system organizes free memory in blocks of various orders. An order N block has 2^N physically contiguous pages, with the BUDDY flag set for and _only_ for the first page
37+
const BUDDY = 1 << 10;
38+
/// A memory mapped page
39+
const MMAP = 1 << 11;
40+
/// A memory mapped page that is not part of a file
41+
const ANON = 1 << 12;
42+
/// The page is mapped to swap space, i.e. has an associated swap entry
43+
const SWAPCACHE = 1 << 13;
44+
/// The page is backed by swap/RAM
45+
const SWAPBACKED = 1 << 14;
46+
/// A compound page with order N consists of 2^N physically contiguous pages. A compound page with order 2 takes the form of “HTTT”, where H donates its head page and T donates its tail page(s). The major consumers of compound pages are hugeTLB pages (<https://www.kernel.org/doc/html/latest/admin-guide/mm/hugetlbpage.html#hugetlbpage>), the SLUB etc. memory allocators and various device drivers. However in this interface, only huge/giga pages are made visible to end users
47+
const COMPOUND_HEAD = 1 << 15;
48+
/// A compound page tail (see description above)
49+
const COMPOUND_TAIL = 1 << 16;
50+
/// This is an integral part of a HugeTLB page
51+
const HUGE = 1 << 17;
52+
/// The page is in the unevictable (non-)LRU list It is somehow pinned and not a candidate for LRU page reclaims, e.g. ramfs pages, shmctl(SHM_LOCK) and mlock() memory segments
53+
const UNEVICTABLE = 1 << 18;
54+
/// Hardware detected memory corruption on this page: don’t touch the data!
55+
const HWPOISON = 1 << 19;
56+
/// No page frame exists at the requested address
57+
const NOPAGE = 1 << 20;
58+
/// Identical memory pages dynamically shared between one or more processes
59+
const KSM = 1 << 21;
60+
/// Contiguous pages which construct transparent hugepages
61+
const THP = 1 << 22;
62+
/// The page is logically offline
63+
const OFFLINE = 1 << 23;
64+
/// Zero page for pfn_zero or huge_zero page
65+
const ZERO_PAGE = 1 << 24;
66+
/// The page has not been accessed since it was marked idle (see <https://www.kernel.org/doc/html/latest/admin-guide/mm/idle_page_tracking.html#idle-page-tracking>). Note that this flag may be stale in case the page was accessed via a PTE. To make sure the flag is up-to-date one has to read /sys/kernel/mm/page_idle/bitmap first
67+
const IDLE = 1 << 25;
68+
/// The page is in use as a page table
69+
const PGTABLE = 1 << 26;
70+
71+
}
72+
}
73+
74+
impl PhysicalPageFlags {
75+
pub fn parse_info(info: u64) -> Self {
76+
PhysicalPageFlags::from_bits_truncate(info)
77+
}
78+
}
79+
80+
#[cfg(test)]
81+
mod tests {
82+
use super::*;
83+
84+
#[test]
85+
fn test_kpageflags_parsing() {
86+
let pagemap_entry: u64 = 0b0000000000000000000000000000000000000000000000000000000000000001;
87+
let info = PhysicalPageFlags::parse_info(pagemap_entry);
88+
assert!(info == PhysicalPageFlags::LOCKED);
89+
}
90+
}

procfs-core/src/lib.rs

+3
Original file line numberDiff line numberDiff line change
@@ -378,6 +378,9 @@ pub use pressure::*;
378378

379379
pub mod process;
380380

381+
mod kpageflags;
382+
pub use kpageflags::*;
383+
381384
pub mod sys;
382385
pub use sys::kernel::Version as KernelVersion;
383386

procfs/src/cgroups.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use super::process::Process;
22
use crate::{Current, ProcResult};
33
use procfs_core::CGroupControllers;
4-
pub use procfs_core::{CGroupController, ProcessCGroups};
4+
pub use procfs_core::ProcessCGroups;
55

66
impl Current for CGroupControllers {
77
const PATH: &'static str = "/proc/cgroups";

procfs/src/kpageflags.rs

+1-77
Original file line numberDiff line numberDiff line change
@@ -1,88 +1,12 @@
11
use crate::{process::Pfn, FileWrapper, ProcResult};
22

3-
use bitflags::bitflags;
43
use std::{
54
io::{BufReader, Read, Seek, SeekFrom},
65
mem::size_of,
76
path::Path,
87
};
98

10-
#[cfg(feature = "serde1")]
11-
use serde::{Deserialize, Serialize};
12-
13-
//const fn genmask(high: usize, low: usize) -> u64 {
14-
// let mask_bits = size_of::<u64>() * 8;
15-
// (!0 - (1 << low) + 1) & (!0 >> (mask_bits - 1 - high))
16-
//}
17-
18-
bitflags! {
19-
/// Represents the fields and flags in a page table entry for a memory page.
20-
#[cfg_attr(feature = "serde1", derive(Serialize, Deserialize))]
21-
#[derive(Copy, Clone, Debug, Hash, Eq, PartialEq, PartialOrd, Ord)]
22-
pub struct PhysicalPageFlags: u64 {
23-
/// The page is being locked for exclusive access, e.g. by undergoing read/write IO
24-
const LOCKED = 1 << 0;
25-
/// IO error occurred
26-
const ERROR = 1 << 1;
27-
/// The page has been referenced since last LRU list enqueue/requeue
28-
const REFERENCED = 1 << 2;
29-
/// The page has up-to-date data. ie. for file backed page: (in-memory data revision >= on-disk one)
30-
const UPTODATE = 1 << 3;
31-
/// The page has been written to, hence contains new data. i.e. for file backed page: (in-memory data revision > on-disk one)
32-
const DIRTY = 1 << 4;
33-
/// The page is in one of the LRU lists
34-
const LRU = 1 << 5;
35-
/// The page is in the active LRU list
36-
const ACTIVE = 1 << 6;
37-
/// The page is managed by the SLAB/SLOB/SLUB/SLQB kernel memory allocator. When compound page is used, SLUB/SLQB will only set this flag on the head page; SLOB will not flag it at all
38-
const SLAB = 1 << 7;
39-
/// The page is being synced to disk
40-
const WRITEBACK = 1 << 8;
41-
/// The page will be reclaimed soon after its pageout IO completed
42-
const RECLAIM = 1 << 9;
43-
/// A free memory block managed by the buddy system allocator. The buddy system organizes free memory in blocks of various orders. An order N block has 2^N physically contiguous pages, with the BUDDY flag set for and _only_ for the first page
44-
const BUDDY = 1 << 10;
45-
/// A memory mapped page
46-
const MMAP = 1 << 11;
47-
/// A memory mapped page that is not part of a file
48-
const ANON = 1 << 12;
49-
/// The page is mapped to swap space, i.e. has an associated swap entry
50-
const SWAPCACHE = 1 << 13;
51-
/// The page is backed by swap/RAM
52-
const SWAPBACKED = 1 << 14;
53-
/// A compound page with order N consists of 2^N physically contiguous pages. A compound page with order 2 takes the form of “HTTT”, where H donates its head page and T donates its tail page(s). The major consumers of compound pages are hugeTLB pages (<https://www.kernel.org/doc/html/latest/admin-guide/mm/hugetlbpage.html#hugetlbpage>), the SLUB etc. memory allocators and various device drivers. However in this interface, only huge/giga pages are made visible to end users
54-
const COMPOUND_HEAD = 1 << 15;
55-
/// A compound page tail (see description above)
56-
const COMPOUND_TAIL = 1 << 16;
57-
/// This is an integral part of a HugeTLB page
58-
const HUGE = 1 << 17;
59-
/// The page is in the unevictable (non-)LRU list It is somehow pinned and not a candidate for LRU page reclaims, e.g. ramfs pages, shmctl(SHM_LOCK) and mlock() memory segments
60-
const UNEVICTABLE = 1 << 18;
61-
/// Hardware detected memory corruption on this page: don’t touch the data!
62-
const HWPOISON = 1 << 19;
63-
/// No page frame exists at the requested address
64-
const NOPAGE = 1 << 20;
65-
/// Identical memory pages dynamically shared between one or more processes
66-
const KSM = 1 << 21;
67-
/// Contiguous pages which construct transparent hugepages
68-
const THP = 1 << 22;
69-
/// The page is logically offline
70-
const OFFLINE = 1 << 23;
71-
/// Zero page for pfn_zero or huge_zero page
72-
const ZERO_PAGE = 1 << 24;
73-
/// The page has not been accessed since it was marked idle (see <https://www.kernel.org/doc/html/latest/admin-guide/mm/idle_page_tracking.html#idle-page-tracking>). Note that this flag may be stale in case the page was accessed via a PTE. To make sure the flag is up-to-date one has to read /sys/kernel/mm/page_idle/bitmap first
74-
const IDLE = 1 << 25;
75-
/// The page is in use as a page table
76-
const PGTABLE = 1 << 26;
77-
78-
}
79-
}
80-
81-
impl PhysicalPageFlags {
82-
pub(crate) fn parse_info(info: u64) -> Self {
83-
PhysicalPageFlags::from_bits_truncate(info)
84-
}
85-
}
9+
pub use procfs_core::PhysicalPageFlags;
8610

8711
/// Parse physical memory flags accessing `/proc/kpageflags`.
8812
///

0 commit comments

Comments
 (0)