patches to Ricardo Lopes' low level profiler: enable if SIGPROF is

there, do sorting, init PROFPREDS when you start counters, keep time
stamps for eventual assert/retract, and use qsort (3) to do less work.


git-svn-id: https://yap.svn.sf.net/svnroot/yap/trunk@829 b08c6af1-5177-4d33-ba66-4b1c6b8b522a
This commit is contained in:
vsc 2003-05-20 19:11:59 +00:00
parent d22fe2107c
commit 0afb07931f
14 changed files with 2736 additions and 1655 deletions

View File

@ -100,7 +100,7 @@ STATIC_PROTO(void a_fetch_vc, (void));
STATIC_PROTO(void a_f2, (int)); STATIC_PROTO(void a_f2, (int));
#ifdef LOW_PROF #ifdef LOW_PROF
int PROFSIZE; yamop *prof_end;
#endif #endif
#define CELLSIZE sizeof(CELL) #define CELLSIZE sizeof(CELL)
@ -2546,7 +2546,7 @@ Yap_assemble(int mode)
entry_code = do_pass(); entry_code = do_pass();
YAPLeaveCriticalSection(); YAPLeaveCriticalSection();
#ifdef LOW_PROF #ifdef LOW_PROF
PROFSIZE=code_p; Yap_prof_end=code_p;
#endif #endif
return entry_code; return entry_code;
} }
@ -2590,4 +2590,7 @@ Yap_InitComma(void)
code_p->opc = opcode(_p_execute_tail); code_p->opc = opcode(_p_execute_tail);
GONEXT(e); GONEXT(e);
} }
#ifdef LOW_PROF
Yap_inform_profiler_of_clause(COMMA_CODE, code_p, RepPredProp(Yap_GetPredPropByFunc(FunctorComma,2)));
#endif
} }

View File

@ -2540,6 +2540,83 @@ p_hidden_predicate(void)
return(pe->PredFlags & HiddenPredFlag); return(pe->PredFlags & HiddenPredFlag);
} }
#ifdef LOW_PROF
static void
inform_profiler_of_clause(yamop *code_start, yamop *code_end, PredEntry *pe) {
/*
I can only open once, otherwise I'll have heaps of trouble
whenever Yap changes directory
*/
ProfPreds++;
if (FPreds != NULL) {
fprintf(FPreds,"+%p %p %p %ld\n",code_start,code_end, pe, ProfCalls);
}
}
void
Yap_inform_profiler_of_clause(yamop *code_start, yamop *code_end, PredEntry *pe) {
inform_profiler_of_clause(code_start, code_end, pe);
}
static void
add_code_in_pred(PredEntry *pp) {
yamop *clcode;
READ_LOCK(pp->PRWLock);
/* check if the codeptr comes from the indexing code */
clcode = pp->cs.p_code.TrueCodeOfPred;
if (pp->PredFlags & IndexedPredFlag) {
char *code_end;
if (pp->PredFlags & LogUpdatePredFlag) {
LogUpdClause *cl = ClauseCodeToLogUpdClause(clcode);
code_end = (char *)cl + Yap_SizeOfBlock((CODEADDR)cl);
} else {
StaticClause *cl = ClauseCodeToStaticClause(clcode);
code_end = (char *)cl + Yap_SizeOfBlock((CODEADDR)cl);
}
inform_profiler_of_clause(clcode, (yamop *)code_end, pp);
}
clcode = pp->cs.p_code.FirstClause;
if (clcode != NULL) {
do {
CODEADDR cl;
char *code_end;
if (pp->PredFlags & LogUpdatePredFlag) {
cl = (CODEADDR)ClauseCodeToLogUpdClause(clcode);
} else if (!(pp->PredFlags & DynamicPredFlag)) {
cl = (CODEADDR)ClauseCodeToDynamicClause(clcode);
} else {
cl = (CODEADDR)ClauseCodeToStaticClause(clcode);
}
code_end = (char *)cl + Yap_SizeOfBlock((CODEADDR)cl);
inform_profiler_of_clause(clcode, (yamop *)code_end, pp);
if (clcode == pp->cs.p_code.LastClause)
break;
clcode = NextClause(clcode);
} while (TRUE);
}
READ_UNLOCK(pp->PRWLock);
}
void
Yap_dump_code_area_for_profiler(void) {
Int i_table;
for (i_table = NoOfModules-1; i_table >= 0; --i_table) {
PredEntry *pp = ModulePred[i_table];
while (pp != NULL) {
add_code_in_pred(pp);
pp = pp->NextPredOfModule;
}
}
}
#endif /* LOW_PROF */
void void
Yap_InitCdMgr(void) Yap_InitCdMgr(void)
{ {

View File

@ -2834,36 +2834,9 @@ Yap_cclause(Term inp_clause, int NOfArgs, int mod)
/* phase 3: assemble code */ /* phase 3: assemble code */
acode = Yap_assemble(ASSEMBLING_CLAUSE); acode = Yap_assemble(ASSEMBLING_CLAUSE);
#ifdef LOW_PROF
{
static int pred_count=0;
FILE *f,*init;
extern int PROFSIZE;
if (!pred_count++) {
f=fopen("PROFPREDS","w");
init=fopen("PROFINIT","r");
if (init!=NULL) {
size_t nc; char buffer[4100];
do {
nc=fread(buffer,1,4096,init);
fwrite(buffer,1,nc,f);
} while(nc>0);
fclose(init);
}
} else {
f=fopen("PROFPREDS","a");
}
if (f!=NULL) {
fprintf(f,"%x - %x - Pred(%ld) - %s/%d\n",acode,PROFSIZE, CodeStart->rnd1, RepAtom(AtomOfTerm(MkAtomTerm((Atom) CodeStart->rnd1)))->StrOfAE, CodeStart->rnd2);
fclose(f);
}
}
#endif
/* check first if there was space for us */ /* check first if there was space for us */
if (acode == NIL) { if (acode == NULL) {
/* make sure we have enough space */ /* make sure we have enough space */
reset_vars(); reset_vars();
if (!Yap_growheap(FALSE, Yap_Error_Size)) { if (!Yap_growheap(FALSE, Yap_Error_Size)) {
@ -2875,7 +2848,13 @@ Yap_cclause(Term inp_clause, int NOfArgs, int mod)
my_clause = Deref(ARG1); my_clause = Deref(ARG1);
goto restart_compilation; goto restart_compilation;
} }
} else } else {
#ifdef LOW_PROF
if (ProfilerOn) {
Yap_inform_profiler_of_clause(acode, Yap_prof_end, CurrentPred);
}
#endif
return(acode); return(acode);
}
} }

View File

@ -2741,5 +2741,10 @@ Yap_PredIsIndexable(PredEntry *ap)
} }
goto restart_index; goto restart_index;
} }
#ifdef LOW_PROF
if (ProfilerOn) {
Yap_inform_profiler_of_clause(indx_out, Yap_prof_end, ap);
}
#endif
return(indx_out); return(indx_out);
} }

View File

@ -107,161 +107,208 @@ STD_PROTO(static Int profres, (void));
STD_PROTO(static Int profres2, (void)); STD_PROTO(static Int profres2, (void));
#define TIMER_DEFAULT 1000 #define TIMER_DEFAULT 1000
static FILE *fprof;
void prof_alrm(int signo) static void
prof_alrm(int signo)
{ {
fprintf(fprof,"%x\n",Yap_regp->P_); ProfCalls++;
fprintf(FProf,"%p\n", P);
return; return;
} }
static Int set_prof_timer(int msec) { static Int set_prof_timer(int msec) {
static int n=-1; static int n=-1;
struct itimerval t; struct itimerval t;
if (msec==0 || n>0) { setitimer(ITIMER_REAL,NULL,NULL); fclose(fprof); n=0; return (TRUE); } if (msec==0 || n>0) {
if (signal(SIGALRM,prof_alrm) == SIG_ERR) { return (FALSE); } setitimer(ITIMER_PROF,NULL,NULL);
n=0;
if (n==-1) fprof=fopen("PROFILING","w"); return TRUE;
else fprof=fopen("PROFILING","a"); }
if (fprof==NULL) { return(FALSE); } if (signal(SIGPROF,prof_alrm) == SIG_ERR) {
return FALSE;
}
n=1; n=1;
t.it_interval.tv_sec=0; t.it_interval.tv_sec=0;
t.it_interval.tv_usec=msec; t.it_interval.tv_usec=msec;
t.it_value.tv_sec=0; t.it_value.tv_sec=0;
t.it_value.tv_usec=msec; t.it_value.tv_usec=msec;
setitimer(ITIMER_REAL,&t,NULL); setitimer(ITIMER_PROF,&t,NULL);
return(TRUE); return(TRUE);
}
static void
start_profilers(void)
{
ProfCalls = 0L;
if (FPreds == NULL) {
FPreds=fopen("PROFPREDS","w+");
}
if (FPreds == NULL)
return;
Yap_dump_code_area_for_profiler();
if (FProf != NULL) {
/* close previous profiling session */
fclose(FProf);
}
FProf=fopen("PROFILING","w+");
if (FProf==NULL) {
return;
}
ProfilerOn = TRUE;
} }
static Int useprof(void) { static Int useprof(void) {
Term p; Term p;
p=Deref(ARG1);
p=Deref(ARG1);
start_profilers();
return (set_prof_timer(IntOfTerm(p))); return (set_prof_timer(IntOfTerm(p)));
} }
static Int useprof0(void) { static Int useprof0(void) {
start_profilers();
return(set_prof_timer(TIMER_DEFAULT)); return(set_prof_timer(TIMER_DEFAULT));
} }
typedef struct clause_entry {
yamop *beg, *end;
PredEntry *pp;
UInt pcs;
UInt ts, tf; /* start end timestamp towards retracts, eventually */
} clauseentry;
static int
cl_cmp(const void *c1, const void *c2)
{
const clauseentry *cl1 = (const clauseentry *)c1;
const clauseentry *cl2 = (const clauseentry *)c2;
if (cl1->beg > cl2->beg) return 1;
if (cl1->beg < cl2->beg) return -1;
return 0;
}
static int
p_cmp(const void *c1, const void *c2)
{
const clauseentry *cl1 = (const clauseentry *)c1;
const clauseentry *cl2 = (const clauseentry *)c2;
if (cl1->pp > cl2->pp) return 1;
if (cl1->pp < cl2->pp) return -1;
return 0;
}
static void
search_pc_pred(yamop *pc_ptr,clauseentry *beg, clauseentry *end) {
/* binary search, dynamic clauses not supported yet */
Int i, j, f, l;
f = 0; l = (end-beg)-1;
i = l/2;
while (TRUE) {
printf("i %d\n", i);
if (beg[i].beg > pc_ptr) {
l = i-1;
if (l < f) {
printf("Could not find %p\n", pc_ptr);
return;
}
j = i;
i = (f+l)/2;
} else if (beg[i].end < pc_ptr) {
f = i+1;
if (f > l) {
printf("Could not find %p\n", pc_ptr);
return;
}
i = (f+l)/2;
} else if (beg[i].beg <= pc_ptr && beg[i].end >= pc_ptr) {
beg[i].pcs++;
return;
} else {
printf("Could not find %p\n", pc_ptr);
return;
}
}
}
static int
showprofres(int tipo) {
clauseentry *pr = (clauseentry *)TR, *t;
UInt i = ProfPreds;
if (FPreds == NULL ||
FProf == NULL) return FALSE;
(void)fseek(FPreds, 0L, SEEK_SET);
while (i) {
if (fscanf(FPreds,"+%p %p %p %ld\n",&(pr->beg),&(pr->end),&(pr->pp),&(pr->ts)) == 0){
/* error */
return FALSE;
}
pr->pcs = 0L;
pr++;
if (pr > (clauseentry *)Yap_TrailTop - 1024) {
Yap_growtrail(64 * 1024L);
}
i--;
}
/* so that we can write again */
(void)fseek(FPreds, 0L, SEEK_END);
qsort((void *)TR, ProfPreds, sizeof(clauseentry), cl_cmp);
(void)fseek(FProf, 0L, SEEK_SET);
for (; i < ProfCalls; i++) {
yamop *pc_ptr;
if (fscanf(FProf,"%p\n",&pc_ptr) == 0){
/* error */
return FALSE;
}
search_pc_pred(pc_ptr,(clauseentry *)TR,pr);
}
(void)fseek(FProf, 0L, SEEK_END);
qsort((void *)TR, ProfPreds, sizeof(clauseentry), p_cmp);
t = (clauseentry *)TR;
while (t < pr) {
UInt calls = t->pcs;
PredEntry *myp = t->pp;
t++;
while (t->pp == myp) {
calls += t->pcs;
t++;
}
if (calls) {
if (myp->ArityOfPE) {
printf("%s:%s/%d -> %ld\n",
RepAtom(AtomOfTerm(ModuleName[myp->ModuleOfPred]))->StrOfAE,
RepAtom(NameOfFunctor(myp->FunctorOfPred))->StrOfAE,
myp->ArityOfPE,
calls);
} else {
printf("%s:%s -> %ld\n",
RepAtom(AtomOfTerm(ModuleName[myp->ModuleOfPred]))->StrOfAE,
RepAtom((Atom)(myp->FunctorOfPred))->StrOfAE,
calls);
}
}
}
return TRUE;
}
static Int profres(void) { static Int profres(void) {
showprofres(0); return showprofres(0);
} }
static Int profres2(void) { static Int profres2(void) {
showprofres(1); return showprofres(1);
}
int showprofres(int tipo) {
FILE *f;
long p, p1,p2, total,total2,count,fora, i;
unsigned long es,ee,e1,e2,e;
long *end;
char nome[200];
f=fopen("PROFPREDS","r");
if (f==NULL) return (FALSE);
i=fscanf(f,"%x - %x - Pred(%ld) - %s",&es,&ee,&p,nome);
p1=p2=p;
e1=es;
e2=ee;
while(i>0) {
if (p<p1) p1=p;
else if (p>p2) p2=p;
if (e1>es) e1=es;
if (e2<ee) e2=ee;
i=fscanf(f,"%x - %x - Pred(%ld) - %s",&es,&ee,&p,nome);
}
fclose(f);
f=fopen("PROFINIT","r");
if (f!=NULL) {
while(i>0) {
if (p<p1) p1=p;
else if (p>p2) p2=p;
if (e1>es) e1=es;
if (e2<ee) e2=ee;
i=fscanf(f,"%x - %x - Pred(%ld) - %s",&es,&ee,&p,nome);
}
}
fclose(f);
printf("%ld Addresses from [%x] to [%x]\n",e2-e1,e1,e2);
printf("%ld Predicates from (%ld) to (%ld) \n",p2-p1,p1,p2);
end=(long *) malloc((e2-e1+1)*sizeof(long));
if (end==NULL) { printf("Not enought mem to process results...\n"); return (FALSE); }
memset(end,0, (e2-e1+1)*sizeof(long));
f=fopen("PROFILING","r");
if (f==NULL) return (FALSE);
i=fscanf(f,"%x",&e);
total=0; fora=0;
while(i>0) {
total++;
if (e<e1 || e>e2) fora++;
else end[e-e1]++;
i=fscanf(f,"%x",&e);
}
fclose(f);
printf("Total count %ld (other code %ld)\n",total,fora);
total2=0;
p1=0; p2=0;
count=0;
f=fopen("PROFPREDS","r");
if (f==NULL) return(FALSE);
if (tipo==0) {
do {
i=fscanf(f,"%x - %x - Pred(%ld) - %s",&es,&ee,&p,nome);
if (i<=0) break;
if (p1!=p) { p2=1; p1=p; } else p2++;
count=0;
while(es<=ee) { count+=end[es-e1]; es++; }
total2+=count;
if (count) printf("Pred(%ld) - %s (%d) - %ld (%f\%)\n", p,nome,p2,count, ((float) count/total)*100.0);
} while(i>0);
} else {
char buffer[300];
buffer[0]=0;
do {
i=fscanf(f,"%x - %x - Pred(%ld) - %s",&es,&ee,&p,nome);
if (i<=0) break;
if (p1!=p) {
p2=1;
p1=p;
if (count) printf("%s",buffer);
total2+=count;
count=0;
} else
p2++;
while(es<=ee) { count+=end[es-e1]; es++; }
sprintf(buffer,"Pred(%ld) - %s (%d) - %ld (%f\%)\n", p,nome,p2,count, ((float) count/total)*100.0);
} while(i>0);
total2+=count;
if (count) printf("%s",buffer);
}
fclose(f);
printf("Total counted %ld (other code %ld)\n",total2,total-total2);
free(end);
return (TRUE);
} }
#endif /* LOW_PROF */ #endif /* LOW_PROF */

View File

@ -1323,10 +1323,6 @@ MSCHandleSignal(DWORD dwCtrlType) {
static void static void
InitSignals (void) InitSignals (void)
{ {
#ifdef LOW_PROF
return;
#endif
#if !defined(LIGHT) && !_MSC_VER && !defined(__MINGW32__) && !defined(LIGHT) #if !defined(LIGHT) && !_MSC_VER && !defined(__MINGW32__) && !defined(LIGHT)
my_signal (SIGQUIT, ReceiveSignal); my_signal (SIGQUIT, ReceiveSignal);
my_signal (SIGKILL, ReceiveSignal); my_signal (SIGKILL, ReceiveSignal);
@ -2005,11 +2001,7 @@ p_alarm(void)
Int left; Int left;
Term tout; Term tout;
#ifdef LOW_PROF
left=0;
#else
left = alarm(IntegerOfTerm(t)); left = alarm(IntegerOfTerm(t));
#endif
tout = MkIntegerTerm(left); tout = MkIntegerTerm(left);
return(Yap_unify(ARG2,tout)); return(Yap_unify(ARG2,tout));
} }

View File

@ -10,7 +10,7 @@
* File: Heap.h * * File: Heap.h *
* mods: * * mods: *
* comments: Heap Init Structure * * comments: Heap Init Structure *
* version: $Id: Heap.h,v 1.40 2003-04-30 17:45:53 vsc Exp $ * * version: $Id: Heap.h,v 1.41 2003-05-20 19:11:59 vsc Exp $ *
*************************************************************************/ *************************************************************************/
/* information that can be stored in Code Space */ /* information that can be stored in Code Space */
@ -300,6 +300,13 @@ typedef struct various_codes {
UInt n_of_file_aliases; UInt n_of_file_aliases;
UInt sz_of_file_aliases; UInt sz_of_file_aliases;
struct AliasDescS * file_aliases; struct AliasDescS * file_aliases;
#if LOW_PROF
int profiler_on;
FILE *f_prof, *f_preds;
int profiler_pred_count;
UInt prof_calls;
UInt prof_preds;
#endif /* LOW_PROF */
struct reduction_counters call_counters; struct reduction_counters call_counters;
char *yap_lib_dir; char *yap_lib_dir;
Agc_hook agc_hook; Agc_hook agc_hook;
@ -517,6 +524,14 @@ typedef struct various_codes {
#define NOfFileAliases heap_regs->n_of_file_aliases #define NOfFileAliases heap_regs->n_of_file_aliases
#define SzOfFileAliases heap_regs->sz_of_file_aliases #define SzOfFileAliases heap_regs->sz_of_file_aliases
#define FileAliases heap_regs->file_aliases #define FileAliases heap_regs->file_aliases
#if LOW_PROF
#define ProfilerOn heap_regs->profiler_on
#define FProf heap_regs->f_prof
#define FPreds heap_regs->f_preds
#define ProfilerPredCount heap_regs->profiler_pred_count
#define ProfCalls heap_regs->prof_calls
#define ProfPreds heap_regs->prof_preds
#endif /* LOW_PROF */
#define ReductionsCounter heap_regs->call_counters.reductions #define ReductionsCounter heap_regs->call_counters.reductions
#define PredEntriesCounter heap_regs->call_counters.reductions_retries #define PredEntriesCounter heap_regs->call_counters.reductions_retries
#define RetriesCounter heap_regs->call_counters.retries #define RetriesCounter heap_regs->call_counters.retries

View File

@ -10,7 +10,7 @@
* File: Yap.proto * * File: Yap.proto *
* mods: * * mods: *
* comments: Function declarations for YAP * * comments: Function declarations for YAP *
* version: $Id: Yapproto.h,v 1.34 2003-05-19 13:04:08 vsc Exp $ * * version: $Id: Yapproto.h,v 1.35 2003-05-20 19:11:59 vsc Exp $ *
*************************************************************************/ *************************************************************************/
/* prototype file for Yap */ /* prototype file for Yap */
@ -297,3 +297,6 @@ void STD_PROTO(Yap_init_socks,(char *, long));
void STD_PROTO(Yap_init_optyap_preds,(void)); void STD_PROTO(Yap_init_optyap_preds,(void));
#if LOW_PROF
void STD_PROTO(Yap_dump_code_area_for_profiler,(void));
#endif /* LOW_PROF */

View File

@ -145,3 +145,9 @@ Term STD_PROTO(Yap_cp_as_integer,(choiceptr));
yamop *STD_PROTO(Yap_PredIsIndexable,(PredEntry *)); yamop *STD_PROTO(Yap_PredIsIndexable,(PredEntry *));
#if LOW_PROF
/* profiling */
yamop *Yap_prof_end;
void STD_PROTO(Yap_inform_profiler_of_clause,(yamop *,yamop *, PredEntry *));
#endif /* LOW_PROF */

View File

@ -211,6 +211,7 @@
#undef HAVE_SIGINFO #undef HAVE_SIGINFO
#undef HAVE_SIGSEGV #undef HAVE_SIGSEGV
#undef HAVE_SIGPROF
#undef HAVE_ENVIRON #undef HAVE_ENVIRON

3904
configure vendored

File diff suppressed because it is too large Load Diff

View File

@ -831,6 +831,24 @@ else
AC_DEFINE(HAVE_SIGSEGV,0) AC_DEFINE(HAVE_SIGSEGV,0)
fi fi
dnl check for sigsegv
AC_MSG_CHECKING(for sigprof)
AC_CACHE_VAL(yap_sigprof,[
AC_TRY_COMPILE(
#include <signal.h>
#include <stdio.h>
,
printf("Signal value is %d\n", SIGPROF);
,
yap_sigprof=yes,yap_sigprof=no)])
AC_MSG_RESULT($yap_sigprof)
if test "$yap_sigsegv" = yes
then
AC_DEFINE(HAVE_SIGPROF,1)
else
AC_DEFINE(HAVE_SIGPROF,0)
fi
dnl check for siginfo dnl check for siginfo
AC_MSG_CHECKING(for siginfo) AC_MSG_CHECKING(for siginfo)
AC_CACHE_VAL(yap_siginfo,[ AC_CACHE_VAL(yap_siginfo,[

View File

@ -105,7 +105,8 @@ static int eof_found = FALSE;
static int yap_lineno = 0; static int yap_lineno = 0;
#ifdef DEBUG #ifdef DEBUG
static void myputc (int ch) static void
myputc (int ch)
{ {
putc(ch,stderr); putc(ch,stderr);
} }

View File

@ -10,7 +10,7 @@
* File: Yap.h.m4 * * File: Yap.h.m4 *
* mods: * * mods: *
* comments: main header file for YAP * * comments: main header file for YAP *
* version: $Id: Yap.h.m4,v 1.45 2003-05-19 13:04:09 vsc Exp $ * * version: $Id: Yap.h.m4,v 1.46 2003-05-20 19:11:59 vsc Exp $ *
*************************************************************************/ *************************************************************************/
#include "config.h" #include "config.h"
@ -228,6 +228,10 @@ typedef long int YAP_LONG_LONG;
typedef unsigned long int YAP_ULONG_LONG; typedef unsigned long int YAP_ULONG_LONG;
#endif #endif
#if HAVE_SIGPROF
#define LOW_PROF 1
#endif
#if DEBUG #if DEBUG
extern char Yap_Option[20]; extern char Yap_Option[20];
#endif #endif