From 0445cbf9b3d465781a39cde5f9d477575cc69a7d Mon Sep 17 00:00:00 2001 From: =?utf8?q?Fr=C3=A9d=C3=A9ric=20Marchal?= Date: Tue, 2 Mar 2010 11:39:40 +0000 Subject: [PATCH] Small optimizations to mitigate the 10% slow down observed while reading the access.log file --- log.c | 19 ++++++++------- util.c | 75 ++++++++++++++++++++++++++++++---------------------------- 2 files changed, 50 insertions(+), 44 deletions(-) diff --git a/log.c b/log.c index 80fb0a2..38e794b 100644 --- a/log.c +++ b/log.c @@ -1298,6 +1298,11 @@ int main(int argc,char *argv[]) if(debugm) printf("DATE=%s IDATA=%d DFROM=%d DUNTIL=%d\n",date,idata,dfrom,duntil); + if(date[0] != '\0'){ + if(idata < dfrom || idata > duntil) continue; + } + if(url[0] == '\0') continue; + if(addr[0] != '\0'){ if(strcmp(addr,ip)!=0) continue; } @@ -1308,19 +1313,14 @@ int main(int argc,char *argv[]) continue; } } - if(url[0] == '\0') continue; - - if(date[0] != '\0'){ - if(idata < dfrom || idata > duntil) continue; - } if(hm[0] != '\0') { - bzero(hmr,sizeof(hmr)); + hmr[0]='\0'; chm++; getword_start(&gwarea,hora); while(chm) { if (getword_multisep(warea,sizeof(warea),&gwarea,':')<0){ - printf("SARG: Maybe you have a broken time in your %s file.\n",arq); + debuga(_("Maybe you have a broken time in your %s file"),arq); exit(1); } strncat(hmr,warea,2); @@ -1436,7 +1436,10 @@ int main(int argc,char *argv[]) } } } - sprintf (tmp3, "%s/sarg/%s.unsort", tmp, ufile->user->filename); + if (snprintf (tmp3, sizeof(tmp3), "%s/sarg/%s.unsort", tmp, ufile->user->filename)>=sizeof(tmp3)) { + debuga(_("Temporary user file name too long: %s/sarg/%s.unsort"), tmp, ufile->user->filename); + exit(1); + } if ((ufile->file = MY_FOPEN (tmp3, "a")) == NULL) { fprintf (stderr, "%s: (log) %s: %s - %s\n", argv[0], text[9], tmp3, strerror(errno)); exit (1); diff --git a/util.c b/util.c index b19e1d7..eb5d668 100644 --- a/util.c +++ b/util.c @@ -1651,24 +1651,50 @@ int longline_prepare(struct longlinestruct *line) char *longline_read(FILE *fp_in,struct longlinestruct *line) { int i; - int skipcr; char *newbuf; size_t nread; if (!line->buffer) return(NULL); + while (1) { + for (i=line->end ; ilength && (line->buffer[i]=='\n' || line->buffer[i]=='\r') ; i++); + if (ilength) { + line->end=i; + break; + } + nread=(feof(fp_in)) ? 0 : fread(line->buffer,1,line->size,fp_in); + if (nread==0) return(NULL); + line->length=nread; + line->end=0; + } + line->start=line->end; - skipcr=1; while (1) { - if (line->end>=line->length) { - if (line->start>0) { - for (i=line->start ; ilength ; i++) line->buffer[i-line->start]=line->buffer[i]; - line->length-=line->start; - line->end-=line->start; - line->start=0; + for (i=line->end ; ilength && line->buffer[i]!='\n' && line->buffer[i]!='\r' ; i++); + line->end=i; + if (line->endlength) break; + + if (line->start>0) { + for (i=line->start ; ilength ; i++) line->buffer[i-line->start]=line->buffer[i]; + line->length-=line->start; + line->end-=line->start; + line->start=0; + } + if (line->length>=line->size) { + line->size+=8192; + newbuf=realloc(line->buffer,line->size); + if (!newbuf) { + debuga(_("Not enough memory to read one more line from the input log file")); + exit(1); } - if (line->length>=line->size) { - line->size+=8192; + line->buffer=newbuf; + } + nread=(feof(fp_in)) ? 0 : fread(line->buffer+line->length,1,line->size-line->length,fp_in); + if (nread==0) { + if (line->end<=line->start) return(NULL); + if (line->end>=line->size) { + line->end=line->size; + line->size++; newbuf=realloc(line->buffer,line->size); if (!newbuf) { debuga(_("Not enough memory to read one more line from the input log file")); @@ -1676,33 +1702,10 @@ char *longline_read(FILE *fp_in,struct longlinestruct *line) } line->buffer=newbuf; } - nread=(feof(fp_in)) ? 0 : fread(line->buffer+line->length,1,line->size-line->length,fp_in); - if (nread==0) { - if (skipcr || line->end<=line->start) return(NULL); - if (line->end>=line->size) { - line->end=line->size; - line->size++; - newbuf=realloc(line->buffer,line->size); - if (!newbuf) { - debuga(_("Not enough memory to read one more line from the input log file")); - exit(1); - } - line->buffer=newbuf; - } - line->buffer[line->end]='\0'; - return(line->buffer+line->start); - } - line->length+=nread; - } - if (skipcr) { - if (line->buffer[line->end]!='\n' && line->buffer[line->end]!='\r') { - skipcr=0; - line->start=line->end; - } - } else { - if (line->buffer[line->end]=='\n' || line->buffer[line->end]=='\r') break; + line->buffer[line->end]='\0'; + return(line->buffer+line->start); } - line->end++; + line->length+=nread; } line->buffer[line->end++]='\0'; return(line->buffer+line->start); -- 2.47.2