Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

This assignment will involve designing two kernel modules: 1. Design a kernel mo

ID: 3743710 • Letter: T

Question

This assignment will involve designing two kernel modules:
1. Design a kernel module that creates a /proc file named /proc/jiffies that reports the current value of jiffies when the /proc/jiffies file is read, such as with the command
cat /proc/jiffies
Be sure to remove /proc/jiffies when the module is removed.
2. Design a kernel module that creates a proc file named /proc/seconds that reports the number of elapsed seconds since the kernel module was loaded. This will involve using the value of jiffies as well as the HZ rate. When a user enters the command
cat /proc/seconds
your kernel module will report the number of seconds that have elapsed since the kernel module was first loaded. Be sure to remove /proc/seconds when the module is removed.

Explanation / Answer

Answer:

Design a kernel module-(file named/proc/seconds).

Jiffies In Linux Kernel :-

It is therefore appropriate to introduce jiffies in linux kernel.

Jiffies is a global variable declared in <Inux/jiffies.h>as:

extern unsigned long volatile jiffies;

Its only usage is to store the number of tickes occurred since system start-up. On kernel boot-up, jiffers is initialized to a special initial value, and it is incremented vy one for each timer interrupt. As discussed in the post, since there are HZ ticks occurred in second, and thus there are HZ jiffies in a second.

jiffies and HZ can be used for time conversion. From discussion, we know that HZ is defined as number of ticks in one second:

HZ = no. ticks/sec

and jiffies is number of ticks occurred:

Therefore, it is not surprise that the following expresssion can be used to convert from second to unit jiffes:

seconds * HZ

and to convert from jiffies to seconds:

jiffies / HZ

The conversion from jiffies to seconds is typically implemented in user program, where the absolute time is needed to provide a more meaningful unit to user. On the other hand, since kernel rarely cares about the absolute time, conversion from seconds to ticks is commonly used in kernel. Considering. the importance of this conversion in kernel, an example of setting a value for value for some time inthe future is illustrated here:

unsigned long now_tick = jiffies; // now

unsigned long next_tick = jiffies + 1; // one tick from now

unsigned long timer_later = jiffier + (10*HZ); // 10s from now

unsigned long time_fraction = jiffies + (HZ/10); // 0.1s from now

with the knowledge of HZ and jiffies, the code above is self-explanatory. All variables in the code declared as usigned long, which is the data type of jiffies. The first variable, now_tick is assigned with jiffies, which contains number of ticks occured at the time of assigment. Likewise, the next tick event can be assigned by incrementing the current jiffies by one, as shown in second line of the code.

for time conversion, a time unit is first multiplied with HZ, and summed with hiffies. This is exampled in third line of the code, where jiffies is summed with product of 10 and HZ to result a delay of 10 seconds. In a similar approach, 1/10 is multplied with HZ, and summed with jiffies to result a fraction of time, which is 0.1 second as, shown in last line of code.

Besically most of the jobs in time management are handled by a dedicated function called timer handler.

In Microblaze, the kernel registers timer_interrupt as the interrupt handler:

inqetum_t TIMER_TEXT timer_interrupt(int irq, void*dev_id)

{

timer_ack();

write_seqlock(&xtime_lock);

do_timer(1);

update_process_times(user_mode(get_irq_regs()));

profile_tick(CPU_PROFILING);

write_sequlock(&xtime_lock):

return IRQ_HANDLED;

}

When kernel is executing timer handler, the timer is first acknowledged. followed by obtainnin the xtime_ lock lock, which protects aceess to jiffies and the wall time value, xtime. Most of the important work is performed in do_timer and update_process_times.

NOTE:- /proc/timer_stats is a file in the / proc pseudo file system which allows you to see information about the routines that are requesting timers of the kernel. By cat'ing this file, you can see which routines are using lots of timers, and frequently they are requesting them.

Example:-

commands:-

<count>, <pid> <command> <start_func> (<expire_func>)