Answered step by step
Verified Expert Solution
Question
1 Approved Answer
/ / / / Created by chang on 1 0 / 1 5 / 2 3 . / / #include #include #include / / TODO
Created by chang on
#include
#include
#include
TODO : Define your input parameter pid int here
Then use moduleparam to pass them from insmod command line. Assignment
struct taskstruct task NULL;
Initialize memory statistics variables
unsigned long totalrss ;
unsigned long totalswap ;
unsigned long totalwss ;
static void parsevmavoid
struct vmareastruct vma NULL;
struct mmstruct mm NULL;
ifpid
task pidtaskfindvpidpid PIDTYPEPID;
iftask && taskmm
mm taskmm;
TODO : mmstruct to initialize the VMAITERATOR Assignment
Hint: Only one line of code is needed
TODO : Iterate through the VMA linked list with foreachvma Assignment
Hint: Only one line of code is needed
TODO : Iterate through each page of the VMA
declear "page" as unsigned long, start from vmavmstart to vmavmend with PAGESIZE step
Hint: Only one line of code is needed
TODO : Use pgdoffset, pdoffset, pudoffset, pmdoffset, pteoffsetmap 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 : use ptenonepte to check if the page table entry is valid
Hint: one if statement here
TODO : use ptepresentpte to check if the page is in memory, otherwise it is in swap
TODO : use pteyoungpte to check if the page is actively used
if it is actively used, update wss and clear the accessed bit by: "testandclearbitPAGEBITACCESSED,unsigned long ppte;
unsigned long timerintervalns e; sec timer
static struct hrtimer hrtimer;
enum hrtimerrestart timercallback struct hrtimer timerforrestart
ktimet currtime interval;
currtime ktimeget;
interval ktimesettimerintervalns;
hrtimerforwardtimerforrestart, currtime interval;
totalrss ;
totalswap ;
totalwss ;
parsevma;
printkPIDi:RSS:lu MBSwap:lu MBWSS:lu MB
pid, totalrss totalswap totalwss;
return HRTIMERRESTART;
int memoryinitvoid
printkCSE Project Kernel Module Inserted
;
ktimet ktime;
ktime ktimeset timerintervalns ;
hrtimerinit &hrtimer, CLOCKMONOTONIC, HRTIMERMODEREL ;
hrtimer.function &timercallback;
hrtimerstart &hrtimer, ktime, HRTIMERMODEREL ;
return ;
void memorycleanupvoid
int ret;
ret hrtimercancel &hrtimer ;
if ret printkHR Timer cancelled
;
printkCSE Project Kernel Module Removed
;
moduleinitmemoryinit;
moduleexitmemorycleanup;
MODULEVERSION;
MODULELICENSEGPL;
MODULEAUTHORYour Name";
MODULEDESCRIPTIONCSE Project Memory Management
;
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started