# HG changeset patch # User Masataka Kohagura # Date 1386896938 -32400 # Node ID 57b2c00b9dc663014257ed7abdcf1ee4bce020d2 # Parent cd6ac4ce5c3cc6862a1907d198c5e77fc8be181a file rename read_lseek.cc to main.cc & remove lseek diff -r cd6ac4ce5c3c -r 57b2c00b9dc6 c/read_lseek/Makefile --- a/c/read_lseek/Makefile Wed Dec 11 02:40:58 2013 +0900 +++ b/c/read_lseek/Makefile Fri Dec 13 10:08:58 2013 +0900 @@ -1,8 +1,8 @@ TARGET=read_lseek OPTION= -Wall -O0 -g -$(TARGET):$(TARGET).cc - clang $(OPTION) -o $(TARGET) $(TARGET).cc +$(TARGET):main.cc + clang $(OPTION) -o $(TARGET) main.cc clean: rm -f $(TARGET) diff -r cd6ac4ce5c3c -r 57b2c00b9dc6 c/read_lseek/main.cc --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/c/read_lseek/main.cc Fri Dec 13 10:08:58 2013 +0900 @@ -0,0 +1,119 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include "read.h" + +static double st_time; +static double ed_time; + +/* + * -b : bulk read 一括read + * -d : divide read 分割read (default) + */ +const char *usr_help_str = "Usage : ./read_lseek [-file filename] [-b -d]"; +const int ONE_TASK_READ_SIZE = 4096 * 1; +bool bulk_flag = true; +bool divide_flag = false; + +void result_printf(int,char *); +void divide_read(int,char*,read_t); +void bulk_read(int,char*,read_t); + +static double +get_time(){ + struct timeval tv; + gettimeofday(&tv,NULL); + return tv.tv_sec + (double)tv.tv_usec*1e-6; +} + +int main(int argc, char *argv[]){ + + struct stat sb; + read_t r; + + char *filename = 0; + for (int i = 1; argv[i]; ++i) { + if (strcmp(argv[i], "-file") == 0){ + filename = argv[i+1]; + }else if(strcmp(argv[i], "-b") == 0){ + bulk_flag = true; + divide_flag = false; + }else if(strcmp(argv[i], "-d") == 0){ + bulk_flag = false; + divide_flag = true; + } + } + + if (filename == 0){ + puts(usr_help_str); + exit(1); + } + + st_time = get_time(); + + int fd = -1; + if ((fd=open(filename,O_RDONLY,0666))==0){ + fprintf(stderr,"can't open %s\n",filename); + } + + if (fstat(fd,&sb)){ + fprintf(stderr,"can't open %s\n",filename); + } + + r.text_size = sb.st_size; + char *text = (char *)malloc(sizeof(char)*r.text_size); + + r.one_task_read_size = ONE_TASK_READ_SIZE; + + r.task_num = r.text_size / r.one_task_read_size; + r.task_num += ((r.text_size % r.one_task_read_size) != 0); + + if(bulk_flag == true){ + bulk_read(fd,text,r); + }else if(divide_flag == true){ + divide_read(fd,text,r); + } + + free(text); + close(fd); + + ed_time = get_time(); + printf("Time: %0.6f\n",ed_time-st_time); + + return 0; +} + +/* lseekで読む場所を指定してあげないといけないと思ったが、 + * readで読み込むだけでも読み込む場所が変わってくれるらしい。 + */ +void divide_read(int _fd,char* _text,read_t _r){ + for(int i = 0; i < _r.task_num; i++){ + //lseek(_fd, i * _r.one_task_read_size,SEEK_SET); + read(_fd,_text,_r.one_task_read_size); + result_printf(i,_text); + } + puts("divide read"); + printf("text size :%lu\n",_r.text_size); + printf("one task size:%d\n",_r.one_task_read_size); + printf("task num :%d\n",_r.task_num); +} + +void bulk_read(int _fd,char* _text,read_t _r){ + int i = 0; + read(_fd,_text,_r.text_size); + result_printf(i,_text); + puts("bulk read"); + printf("text size :%lu\n",_r.text_size); +} + +void result_printf(int _i,char *_text){ + //printf("-------%d--------\n",_i + 1); + printf("[task No. %d]%s\n",_i,_text); + //printf("-----------------\n\n"); +} diff -r cd6ac4ce5c3c -r 57b2c00b9dc6 c/read_lseek/read_lseek.cc --- a/c/read_lseek/read_lseek.cc Wed Dec 11 02:40:58 2013 +0900 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,116 +0,0 @@ -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include "read.h" - -static double st_time; -static double ed_time; - -/* - * -b : bulk read 一括read - * -d : divide read 分割read (default) - */ -const char *usr_help_str = "Usage : ./read_lseek [-file filename] [-b -d]"; -const int ONE_TASK_READ_SIZE = 4096 * 1; -bool bulk_flag = true; -bool divide_flag = false; - -void result_printf(int,char *); -void divide_read(int,char*,read_t); -void bulk_read(int,char*,read_t); - -static double -get_time(){ - struct timeval tv; - gettimeofday(&tv,NULL); - return tv.tv_sec + (double)tv.tv_usec*1e-6; -} - -int main(int argc, char *argv[]){ - - struct stat sb; - read_t r; - - char *filename = 0; - for (int i = 1; argv[i]; ++i) { - if (strcmp(argv[i], "-file") == 0){ - filename = argv[i+1]; - }else if(strcmp(argv[i], "-b") == 0){ - bulk_flag = true; - divide_flag = false; - }else if(strcmp(argv[i], "-d") == 0){ - bulk_flag = false; - divide_flag = true; - } - } - - if (filename == 0){ - puts(usr_help_str); - exit(1); - } - - st_time = get_time(); - - int fd = -1; - if ((fd=open(filename,O_RDONLY,0666))==0){ - fprintf(stderr,"can't open %s\n",filename); - } - - if (fstat(fd,&sb)){ - fprintf(stderr,"can't open %s\n",filename); - } - - r.text_size = sb.st_size; - char *text = (char *)malloc(sizeof(char)*r.text_size); - - r.one_task_read_size = ONE_TASK_READ_SIZE; - - r.task_num = r.text_size / r.one_task_read_size; - r.task_num += ((r.text_size % r.one_task_read_size) != 0); - - if(bulk_flag == true){ - bulk_read(fd,text,r); - }else if(divide_flag == true){ - divide_read(fd,text,r); - } - - free(text); - close(fd); - - ed_time = get_time(); - printf("Time: %0.6f\n",ed_time-st_time); - - return 0; -} - -void divide_read(int _fd,char* _text,read_t _r){ - for(int i = 0; i < _r.task_num; i++){ - lseek(_fd, i * _r.one_task_read_size,SEEK_SET); - read(_fd,_text,_r.one_task_read_size); - result_printf(i,_text); - } - puts("divide read"); - printf("text size :%lu\n",_r.text_size); - printf("one task size:%d\n",_r.one_task_read_size); - printf("task num :%d\n",_r.task_num); -} - -void bulk_read(int _fd,char* _text,read_t _r){ - int i = 0; - read(_fd,_text,_r.text_size); - result_printf(i,_text); - puts("bulk read"); - printf("text size :%lu\n",_r.text_size); -} - -void result_printf(int _i,char *_text){ - //printf("-------%d--------\n",_i + 1); - printf("[task No. %d]%s\n",_i,_text); - //printf("-----------------\n\n"); -}