Memory Leakage with match_file() call in C
Created by: yausteve-ibm
Hi, Jamie, It seems there is a memory leakage in match_file() API. Below is a test case for recreating the problem. Appreciate if you could help to look into it. Many thanks.
-Steve PS: I am using 'Rosie v0.99k-mod5'
################## memleak.c #include <pthread.h> #include <unistd.h> #include <stdio.h> #include <stdlib.h> #include <string.h> #include <librosie.h> #define ROSIE_HOME "/opt/log-analytics/rosie-pattern-language"
struct mytype { int thrd; int start; int end; void *engine; char **p; };
void *rosie_engine(){ struct rosieL_stringArray retvals; void *engine = rosieL_initialize(&(CONST_STRING(ROSIE_HOME)), &retvals); struct rosieL_string *arg=&CONST_STRING("/opt/log-analytics/rosie-pattern-language/MANIFEST"); rosieL_load_manifest(engine,arg); arg = &(CONST_STRING("{"expression": "basic.matchall", "encode": "json"}")); struct rosieL_stringArray r = rosieL_configure_engine(engine, arg); rosieL_free_stringArray(r); return engine; } void translate(struct mytype *s) { char wrkfile[16]; sprintf(wrkfile,"work%i.in",s->thrd); FILE *fp=fopen(wrkfile,"w"); for(int j=s->start;jend;j++){ fprintf(fp, "%s\n", s->p[j]); } fclose(fp); char fin[16]; char fout[16]; char ferr[16]; sprintf(fin,"work%i.in",s->thrd); sprintf(fout,"work%i.out",s->thrd); sprintf(ferr,"work%i.err",s->thrd); printf ("match_file() called...\n"); struct rosieL_stringArray r = rosieL_match_file(s->engine, &(CONST_STRING(fin)) , &(CONST_STRING(fout)), &(CONST_STRING(ferr)), 0); rosieL_free_stringArray(r); }
void process(struct mytype *params) { const int numocpu=4; pthread_t thread[numocpu]; for (int i=0;i<numocpu;i++){ pthread_create(&thread[i], NULL, (void *) translate, (void *) ¶ms[i]); }
for (int i=0;i<numocpu;i++){ pthread_join(thread[i], NULL); } } main() { const int numocpu=4; const int inputsize=10; const * strings[10]={ "This is line1", "This is line2", "This is line3", "This is line4", "This is line5", "This is line6", "This is line7", "This is line8", "This is line9", "This is line10", };
struct mytype params[numocpu]; int mod=inputsize%numocpu; int seg=inputsize/numocpu; if (mod!=0) seg=seg+1; for(int i=0;i<numocpu;i++) { params[i].thrd=i; params[i].engine=rosie_engine(); params[i].p=strings; params[i].start=i*seg; if ((i+1)*seg<inputsize) params[i].end=(i+1)*seg; else params[i].end=inputsize; } for (int i=0;i<5;i++) { process(¶ms); sleep(2); } } <################## memleak.c
################## Makefile CXX=gcc -std=c99 ROSIE_HOME=/opt/log-analytics/rosie-pattern-language
SYSCFLAGS=-DLUA_USE_LINUX SYSLIBS=-Wl,-E -ldl -lm -lreadline -lpthread LIBROSIE= {ROSIE_HOME}/ffi/librosie/librosie.so INC = -I{ROSIE_HOME}/submodules/lua/include -I${ROSIE_HOME}/ffi/librosie CFLAGS = -w -g exes=memleak default: ${exes}
%.o:%.c ${CXX} ${INC} ${CFLAGS} -c -o $@ $<
memleak: memleak.o ${GEN_SRC} ${CXX} -g -o $@ $< ${LIBROSIE} ${SYSCFLAGS} ${SYSLIBS}
clean: rm -f ${exes} <################## Makefile
To recreate:
- make to create executable 'memleak'
- On one window, monitor RSS of memleak process: while [ 1 ]; do ps -eo comm,rss | grep memleak; sleep 1; done
- On another window, ./memleak
The following is output of memleak execution: match_file() called... match_file() called... match_file() called... match_file() called... match_file() called... match_file() called... match_file() called... match_file() called... match_file() called... match_file() called... match_file() called... match_file() called... match_file() called... match_file() called... match_file() called... match_file() called... match_file() called... match_file() called... match_file() called... match_file() called...
The following is the monitor output for RSS: memleak 85136 memleak 169704 memleak 169704 memleak 171072 memleak 171072 memleak 172436 memleak 172436 memleak 173796 memleak 173796 memleak 175160 memleak 175160
If you comment out the match_file() call, there is not increase in RSS.