Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Kernel Compatibility API

The compat module provides sysfs interface compatibility with kernel zram.

SysfsInterface

Emulates the kernel zram sysfs interface:

#![allow(unused)]
fn main() {
use trueno_zram_core::compat::SysfsInterface;

let mut interface = SysfsInterface::new();

// Configure like kernel zram
interface.write_attr("disksize", "4G")?;
interface.write_attr("comp_algorithm", "lz4")?;
interface.write_attr("mem_limit", "2G")?;

// Read attributes
let disksize = interface.read_attr("disksize")?;
let algorithm = interface.read_attr("comp_algorithm")?;
}

Supported Attributes

AttributeReadWriteDescription
disksizeYesYesDisk size in bytes
comp_algorithmYesYesCompression algorithm
mem_limitYesYesMemory limit
mem_used_maxYesNoPeak memory usage
mem_used_totalYesNoCurrent memory usage
orig_data_sizeYesNoOriginal data size
compr_data_sizeYesNoCompressed data size
num_readsYesNoRead count
num_writesYesNoWrite count
invalid_ioYesNoInvalid I/O count
notify_freeYesNoFree notifications
resetNoYesReset device

Algorithm Support

#![allow(unused)]
fn main() {
use trueno_zram_core::compat::{ZramAlgorithm, is_algorithm_supported};

// Check algorithm support
assert!(is_algorithm_supported("lz4"));
assert!(is_algorithm_supported("zstd"));
assert!(is_algorithm_supported("lzo"));  // Compatibility alias

// Parse algorithm
let algo = "lz4".parse::<ZramAlgorithm>()?;
}

Supported Algorithms

NameAliasDescription
lz4-LZ4 fast compression
lz4hc-LZ4 high compression
zstd-Zstandard
lzolzo-rleLZO (mapped to LZ4)
842deflate842/deflate (mapped to ZSTD)

Statistics

MmStat

Memory statistics (like /sys/block/zram0/mm_stat):

#![allow(unused)]
fn main() {
use trueno_zram_core::compat::MmStat;

let stat = interface.mm_stat();

println!("Original size: {} bytes", stat.orig_data_size);
println!("Compressed size: {} bytes", stat.compr_data_size);
println!("Memory used: {} bytes", stat.mem_used_total);
println!("Memory limit: {} bytes", stat.mem_limit);
println!("Memory max: {} bytes", stat.mem_used_max);
println!("Same pages: {}", stat.same_pages);
println!("Pages stored: {}", stat.pages_compacted);
println!("Huge pages: {}", stat.huge_pages);
}

IoStat

I/O statistics (like /sys/block/zram0/io_stat):

#![allow(unused)]
fn main() {
use trueno_zram_core::compat::IoStat;

let stat = interface.io_stat();

println!("Reads: {}", stat.num_reads);
println!("Writes: {}", stat.num_writes);
println!("Invalid I/O: {}", stat.invalid_io);
println!("Notify free: {}", stat.notify_free);
}

Device Reset

#![allow(unused)]
fn main() {
// Reset all statistics and data
interface.write_attr("reset", "1")?;
}

Integration Example

#![allow(unused)]
fn main() {
use trueno_zram_core::compat::SysfsInterface;
use trueno_zram_core::PAGE_SIZE;

let mut interface = SysfsInterface::new();

// Configure
interface.write_attr("disksize", "1G")?;
interface.write_attr("comp_algorithm", "lz4")?;

// Simulate writes
let page = [0xAA; PAGE_SIZE];
interface.write_page(0, &page)?;
interface.write_page(1, &page)?;

// Check statistics
let mm = interface.mm_stat();
println!("Compression ratio: {:.2}x",
    mm.orig_data_size as f64 / mm.compr_data_size as f64);
}