Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

/ / / / Created by chang on 1 0 / 1 5 / 2 3 . / / #include #include #include / / TODO

//
// Created by chang on 10/15/23.
//
#include
#include
#include
// TODO 1: Define your input parameter (pid - int) here
// Then use module_param to pass them from insmod command line. (--Assignment 2)
struct task_struct *task = NULL;
// Initialize memory statistics variables
unsigned long total_rss =0;
unsigned long total_swap =0;
unsigned long total_wss =0;
static void parse_vma(void)
{
struct vm_area_struct *vma = NULL;
struct mm_struct *mm = NULL;
if(pid >0){
task = pid_task(find_vpid(pid), PIDTYPE_PID);
if(task && task->mm){
mm = task->mm;
// TODO 2: mm_struct to initialize the VMA_ITERATOR (-- Assignment 4)
// Hint: Only one line of code is needed
// TODO 3: Iterate through the VMA linked list with for_each_vma (-- Assignment 4)
// Hint: Only one line of code is needed
// TODO 4: Iterate through each page of the VMA
// declear "page" as unsigned long, start from vma->vm_start to vma->vm_end with PAGE_SIZE step
// Hint: Only one line of code is needed
// TODO 5: Use pgd_offset, p4d_offset, pud_offset, pmd_offset, pte_offset_map to get the page table entry
// Hint: Copy from Background Knowledge in the instruction
// Hint: change the address in the instruction to "page" variable you defined above
// TODO 6: use pte_none(pte) to check if the page table entry is valid
// Hint: one if statement here
// TODO 7: use pte_present(pte) to check if the page is in memory, otherwise it is in swap
// TODO 8: use pte_young(pte) to check if the page is actively used
// if it is actively used, update wss and clear the accessed bit by: "test_and_clear_bit(_PAGE_BIT_ACCESSED,(unsigned long *)ppte);"
}
}
}
}
}
unsigned long timer_interval_ns =10e9; //10 sec timer
static struct hrtimer hr_timer;
enum hrtimer_restart timer_callback( struct hrtimer *timer_for_restart )
{
ktime_t currtime , interval;
currtime = ktime_get();
interval = ktime_set(0,timer_interval_ns);
hrtimer_forward(timer_for_restart, currtime , interval);
total_rss =0;
total_swap =0;
total_wss =0;
parse_vma();
printk("[PID-%i]:[RSS:%lu MB][Swap:%lu MB][WSS:%lu MB]
", pid, total_rss*4/1024, total_swap*4/1024, total_wss*4/1024);
return HRTIMER_RESTART;
}
int memory_init(void){
printk("CSE330 Project 2 Kernel Module Inserted
");
ktime_t ktime;
ktime = ktime_set(0, timer_interval_ns );
hrtimer_init( &hr_timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL );
hr_timer.function = &timer_callback;
hrtimer_start( &hr_timer, ktime, HRTIMER_MODE_REL );
return 0;
}
void memory_cleanup(void){
int ret;
ret = hrtimer_cancel( &hr_timer );
if (ret) printk("HR Timer cancelled ...
");
printk("CSE330 Project 2 Kernel Module Removed
");
}
module_init(memory_init);
module_exit(memory_cleanup);
MODULE_VERSION("0.1");
MODULE_LICENSE("GPL");
MODULE_AUTHOR("Your Name");
MODULE_DESCRIPTION("CSE330 Project 2 Memory Management
");

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Database Concepts

Authors: David M Kroenke, David J Auer

6th Edition

0132742926, 978-0132742924

More Books

Students also viewed these Databases questions

Question

Describe two of Georg Elias Mllers contributions to psychology.

Answered: 1 week ago