diff --git a/C/stdpreds.c b/C/stdpreds.c index 219fb4c31..54bbb2446 100644 --- a/C/stdpreds.c +++ b/C/stdpreds.c @@ -39,6 +39,12 @@ static char SccsId[] = "%W% %G%"; #include #endif +#ifdef LOW_PROF + #include + #include + #include +#endif + STD_PROTO(static Int p_setval, (void)); STD_PROTO(static Int p_value, (void)); STD_PROTO(static Int p_values, (void)); @@ -94,6 +100,125 @@ STD_PROTO(static Int p_walltime, (void)); STD_PROTO(static Int p_access_yap_flags, (void)); STD_PROTO(static Int p_set_yap_flags, (void)); +#ifdef LOW_PROF +STD_PROTO(static Int useprof, (void)); +STD_PROTO(static Int useprof0, (void)); +STD_PROTO(static Int profres, (void)); + +#define TIMER_DEFAULT 1000 +static FILE *fprof; + +void prof_alrm(int signo) +{ + fprintf(fprof,"%x\n",Yap_regp->P_); + return; +} + +static Int set_prof_timer(int msec) { +static int n=-1; +struct itimerval t; + + if (msec==0 || n>0) { setitimer(ITIMER_REAL,NULL,NULL); fclose(fprof); n=0; return (TRUE); } + if (signal(SIGALRM,prof_alrm) == SIG_ERR) { return (FALSE); } + + if (n==-1) fprof=fopen("PROFILING","w"); + else fprof=fopen("PROFILING","a"); + if (fprof==NULL) { return(FALSE); } + + n=1; + t.it_interval.tv_sec=0; + t.it_interval.tv_usec=msec; + t.it_value.tv_sec=0; + t.it_value.tv_usec=msec; + setitimer(ITIMER_REAL,&t,NULL); + +return(TRUE); +} + +static Int useprof(void) { + Term p; + p=Deref(ARG1); + return (set_prof_timer(IntOfTerm(p))); +} + +static Int useprof0(void) { + return(set_prof_timer(TIMER_DEFAULT)); +} + + +static Int profres(void) { +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 -1; + + + i=fscanf(f,"%x - %x - Pred(%ld) - %s",&es,&ee,&p,nome); + p1=p2=p; + e1=es; + e2=ee; + + while(i>0) { + if (pp2) p2=p; + if (e1>es) e1=es; + if (e20) { + total++; + if (ee2) fora++; + else end[e-e1]++; + i=fscanf(f,"%x",&e); + } + + fclose(f); + + + printf("Total count %ld (other code %ld)\n",total,fora); + + f=fopen("PROFPREDS","r"); + if (f==NULL) return -1; + + total2=0; + do { + i=fscanf(f,"%x - %x - Pred(%ld) - %s",&es,&ee,&p,nome); + if (i<=0) break; + count=0; + while(es<=ee) { count+=end[es-e1]; es++; } + total2+=count; + if (count) printf("Pred(%ld) - %s - %ld (%f\%)\n", p,nome,count, ((float) count/total)*100.0); + } while(i>0); + + fclose(f); + printf("Total counted %ld (other code %ld)\n",total2,total-total2); + free(end); + +} + + +#endif /* LOW_PROF */ + static Int p_setval(void) @@ -2318,6 +2443,11 @@ Yap_InitCPreds(void) Yap_InitCPred("unhide", 1, p_unhide, SafePredFlag|SyncPredFlag); Yap_InitCPred("$hidden", 1, p_hidden, SafePredFlag|SyncPredFlag); Yap_InitCPred("$has_yap_or", 0, p_has_yap_or, SafePredFlag|SyncPredFlag); +#ifdef LOW_PROF + Yap_InitCPred("useprof", 1, useprof, SafePredFlag); + Yap_InitCPred("useprof", 0, useprof0, SafePredFlag); + Yap_InitCPred("profres", 0, profres, SafePredFlag); +#endif #ifndef YAPOR Yap_InitCPred("$default_sequential", 1, p_default_sequential, SafePredFlag|SyncPredFlag); #endif