gc_profile.h

00001 /* File:      gc_profile.h
00002 ** Author(s): Luis Castro
00003 ** Contact:   xsb-contact@cs.sunysb.edu
00004 ** 
00005 ** Copyright (C) The Research Foundation of SUNY, 1986, 1993-1998
00006 ** Copyright (C) ECRC, Germany, 1990
00007 ** 
00008 ** XSB is free software; you can redistribute it and/or modify it under the
00009 ** terms of the GNU Library General Public License as published by the Free
00010 ** Software Foundation; either version 2 of the License, or (at your option)
00011 ** any later version.
00012 ** 
00013 ** XSB is distributed in the hope that it will be useful, but WITHOUT ANY
00014 ** WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
00015 ** FOR A PARTICULAR PURPOSE.  See the GNU Library General Public License for
00016 ** more details.
00017 ** 
00018 ** You should have received a copy of the GNU Library General Public License
00019 ** along with XSB; if not, write to the Free Software Foundation,
00020 ** Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
00021 **
00022 ** $Id: gc_profile.h,v 1.9 2006/04/27 21:08:47 tswift Exp $
00023 ** 
00024 */
00025 
00026 #ifdef GC_PROFILE
00027 #define GC_PROFILE_PRE_REPORT \
00028 do { \
00029   /* first, run the heap & collect info */ \
00030   if (examine_data) { \
00031     CPtr cell_ptr; \
00032     unsigned long heap_traversed=0; \
00033     int i,tag; \
00034     functor = 0; \
00035     for (i=0; i<9; i++)  \
00036       tag_examined[i] = 0; \
00037     \
00038     stack_boundaries; \
00039     \
00040     for (cell_ptr = (CPtr) glstack.low; cell_ptr < hreg; cell_ptr++) { \
00041       heap_traversed++; \
00042       tag = cell_tag(*cell_ptr); \
00043       if (tag == XSB_REF || tag == XSB_REF1) { \
00044         if (points_into_heap((CPtr) *cell_ptr)) { \
00045           if (cell_ptr == (CPtr) *cell_ptr) { \
00046             ++tag_examined[8]; \
00047           } else { \
00048             tag_examined[tag]++; \
00049           } \
00050         } else if (points_into_heap((CPtr) cell_ptr)) { \
00051           functor++; \
00052         } else { \
00053           fprintf(stddbg,"+++ outside pointer %p in %p.\n", \
00054                   *(CPtr*)cell_ptr, (CPtr)cell_ptr); \
00055         } \
00056       } else { \
00057         ++tag_examined[tag]; \
00058       } \
00059     } \
00060     \
00061     fprintf(stddbg,"\n\n\n{GC} Prior to GC:\n"); \
00062     fprintf(stddbg,"{GC} Cells visited:\n"); \
00063     fprintf(stddbg, "{GC}    %ld variables.\n", tag_examined[8]); \
00064     fprintf(stddbg, "{GC}    %ld reference cells.\n",  \
00065             tag_examined[XSB_REF] + tag_examined[XSB_REF1]); \
00066     fprintf(stddbg, "{GC}    %ld references from the local stack.\n", \
00067             chain_from_ls); \
00068     fprintf(stddbg, "{GC}    %ld atom cells.\n", tag_examined[XSB_STRING]); \
00069     fprintf(stddbg, "{GC}    %ld integer cells.\n", tag_examined[XSB_INT]); \
00070     fprintf(stddbg, "{GC}    %ld float cells.\n", tag_examined[XSB_FLOAT]); \
00071     fprintf(stddbg, "{GC}    %ld list cells.\n", tag_examined[XSB_LIST]); \
00072     fprintf(stddbg, "{GC}    %ld structure cells.\n",  \
00073             tag_examined[XSB_STRUCT]); \
00074     fprintf(stddbg, "{GC}    %ld functor cells.\n",functor); \
00075     fprintf(stddbg, "{GC}    %ld attributed variable cells.\n",  \
00076             tag_examined[XSB_ATTV]); \
00077     fprintf(stddbg, "{GC}    %ld heap cells traversed.\n", \
00078             heap_traversed); \
00079     \
00080   } \
00081   \
00082   if (count_chains) { \
00083     int i; \
00084     for (i=0; i<64; i++) \
00085       chains[i]=0; \
00086   } \
00087   \
00088   if (examine_data){  \
00089     int i; \
00090     for (i=0; i<9; i++) { \
00091       tag_examined[i]=0; \
00092       /*        tag_examined_current[i]=0; */ \
00093     }\
00094     chain_from_ls = functor = 0; \
00095     current_mark = deep_mark=0; \
00096     start_hbreg = cp_hreg(breg); \
00097     old_gens = ((unsigned long) start_hbreg - (unsigned long) glstack.low) /  \
00098       sizeof(CPtr); \
00099     current_gen = ((unsigned long) hreg - (unsigned long) start_hbreg) /  \
00100       sizeof(CPtr); \
00101     active_cps = 0; \
00102     frozen_cps = 0; \
00103   }\
00104 } while(0)
00105 
00106 #define INIT_GC_PROFILE \
00107   verbose_gc=pflags[VERBOSE_GC]; \
00108   examine_data=pflags[EXAMINE_DATA]; \
00109   count_chains=pflags[COUNT_CHAINS]
00110 
00111 #define DECL_GC_PROFILE \
00112   unsigned long begin_slidetime, begin_copy_time
00113 
00114 #define GC_PROFILE_START_SUMMARY \
00115 do { \
00116     if (verbose_gc) { \
00117       xsb_dbgmsg((LOG_GC,"{GC} Heap gc - arity = %d - used = %d - left = %d - #gc = %d\n", \
00118                  arity,hreg+1-(CPtr)glstack.low,ereg-hreg,num_gc)); \
00119     } \
00120 } while(0)
00121 
00122 #define GC_PROFILE_MARK_SUMMARY \
00123 do { \
00124     if (verbose_gc) { \
00125       xsb_dbgmsg((LOG_GC, "{GC} Heap gc - marking finished - #marked = %d - start compact\n", \
00126                  marked)); \
00127     }  \
00128 } while (0)
00129 
00130 #define GC_PROFILE_QUIT_MSG \
00131 do { \
00132         if (verbose_gc) { \
00133           xsb_dbgmsg((LOG_GC, "{GC} Heap gc - marked too much - quitting gc\n")); \
00134         } \
00135 } while (0)
00136 
00137 #define GC_PROFILE_SLIDE_START_TIME begin_slidetime = end_marktime
00138 
00139 #define GC_PROFILE_SLIDE_FINAL_SUMMARY \
00140 do { \
00141         if (verbose_gc) { \
00142           xsb_dbgmsg((LOG_GC, "{GC} Heap gc end - mark time = %f; slide time = %f; total = %f\n", \
00143           (double)(end_marktime - begin_marktime)*1000/CLOCKS_PER_SEC, \
00144           (double)(end_slidetime - begin_slidetime)*1000/CLOCKS_PER_SEC, \
00145           total_time_gc)) ; \
00146         } \
00147 } while(0)
00148 
00149 #define GC_PROFILE_COPY_START_TIME begin_copy_time = end_marktime
00150 
00151 #define GC_PROFILE_COPY_FINAL_SUMMARY \
00152 do { \
00153         if (verbose_gc) { \
00154           fprintf(stddbg,"{GC} Heap gc end - mark time = %f; copy_time = %f; total = %f\n", \
00155            (double)(end_marktime - begin_marktime)*1000/CLOCKS_PER_SEC, \
00156            (double)(end_copy_time - begin_copy_time)*1000/CLOCKS_PER_SEC, \
00157            total_time_gc) ; \
00158         } \
00159 } while(0)
00160 
00161 #define GC_PROFILE_POST_REPORT \
00162 do { \
00163   if (count_chains|examine_data) { \
00164     fprintf(stddbg, "\n{GC} Heap Garbage Collection #%d\n",num_gc); \
00165     fprintf(stddbg, "{GC} Heap early reset reclaimed %d cells.\n", \
00166             heap_early_reset); \
00167     fprintf(stddbg, "{GC} Local early reset reclaimed %d cells.\n", \
00168             ls_early_reset); \
00169   } \
00170 \
00171   if (count_chains) { \
00172     int i; \
00173     fprintf(stddbg,"{GC} Reference Chains: \n"); \
00174     for (i=0; i<64; i++) \
00175       if (chains[i]) \
00176         fprintf(stddbg, "{GC}  chain[%d]=%ld\n",i,chains[i]); \
00177   } \
00178 \
00179   if (examine_data) { \
00180     fprintf(stddbg,"{GC} Active Choice-points: %ld\n", active_cps); \
00181     fprintf(stddbg,"{GC} Frozen Choice-points: %ld\n",frozen_cps); \
00182     fprintf(stddbg,"{GC} Local stack size: %d\n", ls_bot - ls_top); \
00183     fprintf(stddbg,"{GC} CP stack size: %d\n", cp_bot - cp_top); \
00184     fprintf(stddbg,"{GC} Trail stack size: %d\n", tr_top - tr_bot); \
00185     fprintf(stddbg,"{GC} Cells visited:\n"); \
00186     fprintf(stddbg, "{GC}    %ld variables.\n", tag_examined[8]); \
00187     fprintf(stddbg, "{GC}    %ld reference cells.\n",  \
00188             tag_examined[XSB_REF] + tag_examined[XSB_REF1]); \
00189     fprintf(stddbg, "{GC}    %ld references from the local stack.\n", \
00190             chain_from_ls); \
00191     fprintf(stddbg, "{GC}    %ld atom cells.\n", tag_examined[XSB_STRING]); \
00192     fprintf(stddbg, "{GC}    %ld integer cells.\n", tag_examined[XSB_INT]); \
00193     fprintf(stddbg, "{GC}    %ld float cells.\n", tag_examined[XSB_FLOAT]); \
00194     fprintf(stddbg, "{GC}    %ld list cells.\n", tag_examined[XSB_LIST]); \
00195     fprintf(stddbg, "{GC}    %ld structure cells.\n",  \
00196             tag_examined[XSB_STRUCT]); \
00197     fprintf(stddbg, "{GC}    %ld functor cells.\n",functor); \
00198     fprintf(stddbg, "{GC}    %ld attributed variable cells.\n",  \
00199             tag_examined[XSB_ATTV]); \
00200 \
00201     if (current_gen > 0) \
00202       fprintf(stddbg, "{GC} Cells marked on current generation: %ld / %ld = %ld / 100\n", \
00203               current_mark, current_gen, (current_mark*100/current_gen)); \
00204     if (old_gens > 0) \
00205       fprintf(stddbg, "{GC} Cells marked on deep generations: %ld / %ld = %ld / 100\n", \
00206               deep_mark, old_gens, (deep_mark*100/old_gens)); \
00207     if (current_gen + old_gens > 0) \
00208       fprintf(stddbg, "{GC} Total cells marked: %ld / %ld = %ld / 100\n", \
00209               deep_mark + current_mark, current_gen + old_gens, \
00210               ((deep_mark+current_mark)*100/(current_gen+old_gens))); \
00211   } \
00212 } while (0)
00213 #else
00214 #define GC_PROFILE_PRE_REPORT
00215 #define INIT_GC_PROFILE
00216 #define DECL_GC_PROFILE
00217 #define GC_PROFILE_START_SUMMARY
00218 #define GC_PROFILE_MARK_SUMMARY 
00219 #define GC_PROFILE_QUIT_MSG
00220 #define GC_PROFILE_SLIDE_START_TIME
00221 #define GC_PROFILE_SLIDE_FINAL_SUMMARY
00222 #define GC_PROFILE_COPY_START_TIME
00223 #define GC_PROFILE_COPY_FINAL_SUMMARY
00224 #define GC_PROFILE_POST_REPORT
00225 #endif
00226 
00227 
00228 #ifdef GC_PROFILE
00229 inline static void inspect_chain(CPtr cell_ptr)
00230 {
00231   int tag;
00232   tag = cell_tag(*cell_ptr);
00233 
00234   if (count_chains) {
00235     if ((tag == XSB_REF || tag == XSB_REF1) &&
00236         points_into_heap((CPtr)*cell_ptr)) {
00237       int temp=0;
00238       CPtr ptr = (CPtr) *cell_ptr;
00239       if (points_into_ls(cell_ptr)) {
00240         temp++;
00241         ptr = (CPtr) follow(ptr);
00242         chain_from_ls++;
00243       }
00244       while (isref(ptr) && points_into_heap(ptr) && 
00245              ptr != (CPtr) follow(ptr)) {
00246         temp++;
00247         ptr = (CPtr) follow(ptr);
00248       }
00249       if (temp > 64)
00250         xsb_abort("Chain too long when inspecting cell in %p.\n", cell_ptr);
00251       ++chains[temp];
00252     }
00253   }
00254 }
00255     
00256 inline static void inspect_ptr(CPtr cell_ptr)
00257 {
00258   int tag;
00259   tag = cell_tag(*cell_ptr);
00260   
00261   inspect_chain(cell_ptr);
00262 
00263   if (examine_data) {
00264     if (tag == XSB_REF || tag == XSB_REF1) {
00265       if (points_into_heap((CPtr) *cell_ptr)) {
00266         if (cell_ptr == (CPtr) *cell_ptr)
00267           ++tag_examined[8];
00268         else
00269           tag_examined[tag]++;
00270       }
00271       else if (points_into_heap((CPtr) cell_ptr))
00272         functor++;
00273       else
00274         fprintf(stddbg,"+++ outside pointer %p in %p.\n",
00275                 *(CPtr*)cell_ptr, (CPtr)cell_ptr);
00276     } else
00277       ++tag_examined[tag];
00278     if ((unsigned long) cell_ptr < (unsigned long) start_hbreg)
00279       ++deep_mark;
00280     else
00281       ++current_mark;
00282   }
00283 }  
00284 
00285 #endif
00286 
00287 /* the following cannot be "static inline" since it's used in
00288    emu/trace_xsb.c and also uses global variables in heap_xsb.c
00289    --lfcastro */
00290 void print_gc_statistics(CTXTdecl)
00291 {
00292   char *which = (slide) ? "sliding" : "copying" ;
00293 
00294   printf("%4d heap garbage collections by %s: collected %ld cells in %lf secs\n\n",
00295          num_gc, which, total_collected, total_time_gc);
00296 }
00297 
00298 

Generated on Wed Jul 26 13:30:41 2006 for XSB by  doxygen 1.4.5