#define NTRIALS 100000
#define BUFF_LEN 100
#define FILE_NAME "/home/nirendra/test.html"
#define measure_time(code) \
for(i=0; i < NTRIALS; i++) { \
rdtscl(ini); \
code; \
rdtscl(end); \
now = end - ini; \
if(now < best) best = now; \
}static void parse_html(char *html, char *text)
{
int hcount = 0, tcount = 0;
int tag=0;
/*Parse buffer to remove html tags*/
for(hcount = 0; html[hcount] != '\0' && html[hcount]; hcount++)
{
if(html[hcount] == '<') /*HTML tag begins*/
tag = 1;
if(!tag)
text[tcount++] = html[hcount];
if(html[hcount] == '>') /*HTML tag ends*/
tag = 0;
}
text[tcount] = '\0';
}
Nirendra
common.h
uhtmlparser.c
#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <asm/msr.h>#include "common.h"
int main()
{
unsigned long ini, end, now, best, tsc;
int i;
int fd;
int nhtml = 0, ntext = 0;
char *html, *text;
html = malloc(BUFF_LEN);
text = malloc(BUFF_LEN);
memset(html, 0, BUFF_LEN);
memset(text, 0, BUFF_LEN);/*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);
}
khtmlparser.c
#include <linux/init.h>
#include <linux/module.h>
#include <linux/init.h>
#include <linux/syscalls.h>
#include <linux/fcntl.h>
#include <linux/fs.h>
#include <linux/file.h>
#include <linux/unistd.h>
#include <asm/uaccess.h>
MODULE_LICENSE("Dual BSD/GPL");#include "common.h"
static int khtmlparser_init(void)
{
mm_segment_t fs;
unsigned long ini, end, now, best, tsc;
int i;
struct file *fd;
char *html;
char *text;
html = kzalloc(BUFF_LEN, GFP_KERNEL);
text = kzalloc(BUFF_LEN, GFP_KERNEL);
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(IS_ERR(fd)){
printk(KERN_INFO "flip_open returned an error\n");
return(-EINVAL);
}
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);
}
if(fd) filp_close(fd, NULL);
set_fs(fs); /*restore before returning to userspace*/
return -EINVAL;
}/*Never used*/
static void khtmlparser_exit(void)
{
}module_init(khtmlparser_init);
module_exit(khtmlparser_exit);
