DSL Ideas and Suggestions :: Speeding boot with readahead



Quote
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/syscall.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <sched.h>

void process_file(char *filename)
{
int fd;
struct stat buf;

if (!filename)
return;

fd = open(filename,O_RDONLY);
if (fd<0)
return;

if (fstat(fd, &buf)<0)
return;

#if 0
/* don't readahead on nfs */
if (!gnu_dev_major(buf.st_dev))
return;
#endif
       
readahead(fd, (loff_t)0, (size_t)buf.st_size);
/*        posix_fadvise(fd, (off_t) 0, (off_t) 0, POSIX_FADV_NORMAL|POSIX_FADV_WILLNEED);
*/
         close(fd);
/* be nice to other processes now */
sched_yield();
}


int main(int argc, char **argv)
{
int i;
for (i=1; i<argc; i++)
process_file(argv[i]);
return 0;
}


Here's the c code of the most recent version of readahead I found (1.01-2 - ubuntu1), from Ubuntu's Gutsy repository.
It takes only 3kb when compiled with -Os and stripped, and needs only glibc and the kernel headers.

I can't right now compile it on DSL, and the ones compiled on my Aero are most certainly not DSL compatible..

Readahead is commonly used to speed up the boot, by using it in the background during a cpu-headed process (like the hwconfig with the green bar, it doesn't read anything and takes some time that could be used) to load the files needed by the next part of the boot into cache.

It works, I tested with Mplayer (reading that 8mb binary takes some seconds, but when I loaded it into cache with readahead, surprise it started instantly) and remembered to clean the cache between tests.

Here's a compiled version

I will see how this could be best used during DSL boot. Comments anyone?

I'll tell the Ubuntu philosophy of using it:

They have a list of files accessed during boot. They generated it with the boot watcher program, the one that also creates graphs of cpu usage per process etc.
Then they ordered it to the order they are on the disk, for faster reading.

They then launch it as a background process near the very start of booting. While this still has a nice effect on boot speed, it's not ideal.

Because it continuously accesses the media, other processes will have to wait or they go first and it slows down. Also if the process that needs a file is run before readahead reads the file, there will be no effect.

So, with DSL I plan to use it in carefully selected parts, like during cpu-using processes. And in pieces, not in one long run. This will make it more effective.

[shameless plug]

Readahead is available in busybox.

>It takes only 3kb when compiled with -Os and stripped

In busybox, it takes 104 bytes - 3.5% of 3 Kb. Not bad, eh? :)

By including busybox, DSL will get readahead and ~200 other programs in 700kb of code.

[end shameless plug]

DSL already has busybox, and readahead was added in 1.7 I think.
Next Page...
original here.