Valerie
Valerie

Reputation: 43

How to get boot time and show it on terminal wtih kernel programming in Linux?

What I'm currently thinking about is getting the uptime and the current date and time, and then subtracting these. I'll then convert the output to an output that resembles the output of the "date" command in Linux.

But I think that this takes too long? And I think there might be a better solution than this since it looks too brute force.

-- beginner on kernel programming

Upvotes: 0

Views: 674

Answers (2)

Chris Tsui
Chris Tsui

Reputation: 472

There is a kernel option called PRINTK_TIME which can turn on a useful feature of adding timestamp before every line of printk.

Upvotes: 1

Arthur
Arthur

Reputation: 578

Try the following code

u64 nsec = local_clock();
unsigned long rem_nsec = do_div(nsec, 100000000000);
printk("time from boot is %5lu.%06lu ", (unsigned long)nsec, rem_nsec / 1000);

Upvotes: 0

Related Questions