System calls are means through which user level processes can communicate with kernel. Though Linux kernel allows kernel code to invoke system calls.
This is not generally considered a good idea in terms of debugging, maintaining and porting the code. But if performance or size are absolutely necessary porting applications on kernel seems to have huge benefits.
The gain of performance comes for costly user/kernel space transition and associated data passing.
In order to measure timing benefits I implemented a rudimentary HTML parser in kernel space and a similar parser in userland.
Code snippets from kernel module for reading a html file and removing the html tags is as follows(Complete source available
here):
best = ~0;
measure_time(0);
tsc = best;
printk(KERN_INFO "Time taken for no code: %ld\n", tsc);
/*Measure time of reading a file*/
/*Prepare to invoke system call*/
fs = get_fs(); /*Save previous value*/
set_fs(get_ds()); /*use kernel limit*/
/*Call system call*/
fd = filp_open(FILE_NAME, O_RDONLY, 0600);
if(fd->f_op && fd->f_op->read){
best = ~0;
measure_time(fd->f_op->read(fd, html, 1000, &fd->f_pos));
printk(KERN_INFO "Time taken by read: %ld\n", best-tsc);
parse_html(html, text); /*Parse html to text*/
printk(KERN_INFO "Parsed text: %s", text);
}
This code parses the HTML by calling an ugly parser parse_html(Defined in common.h available
here) which strips out the html tags.
While part of similar userland code is as follows(Complete source available
here):
/*time rdsc, i.e. no code*/
best =~ 0;
measure_time(0);
tsc = best;
printf("Time taken for no code: %ld\n", tsc);
/*Measure time for reading a file*/
fd = open(FILE_NAME, O_RDONLY, 0600);
if(!fd){
printf("Error opening file\n");
exit(1);
}
best = ~0;
measure_time(read(fd, html, 1000));
printf("Time taken by read: %li\n", best - tsc);
parse_html(html, text);
printf("Parsed text: %s\n", text);
I collected following read time for first 5 runs:
| Clock ticks/run |
1st Run |
2nd Run |
3rd Run |
4th Run |
5th Run |
| Kernel HTML Parser |
246 |
366 |
245 |
351 |
246 |
| Userland HTML Parser |
675 |
683 |
561 |
683 |
675 |
Thus, file read time in kernel outperforms userland code by around 3 times.
There are couple of interesting possibilities on porting application requiring high performance to kernel space. There already exists few including a
Kernel mode web server. Ofcourse, the crash for a not properly tested module could cost more than their userland counterparts.