~burner/xsb/debianized-xsb

« back to all changes in this revision

Viewing changes to emu/gc_profile.h

  • Committer: Michael R. Head
  • Date: 2006-09-06 22:11:55 UTC
  • Revision ID: burner@n23-20060906221155-7e398d23438a7ee4
Add the files from the 3.0.1 release package

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* File:      gc_profile.h
 
2
** Author(s): Luis Castro
 
3
** Contact:   xsb-contact@cs.sunysb.edu
 
4
** 
 
5
** Copyright (C) The Research Foundation of SUNY, 1986, 1993-1998
 
6
** Copyright (C) ECRC, Germany, 1990
 
7
** 
 
8
** XSB is free software; you can redistribute it and/or modify it under the
 
9
** terms of the GNU Library General Public License as published by the Free
 
10
** Software Foundation; either version 2 of the License, or (at your option)
 
11
** any later version.
 
12
** 
 
13
** XSB is distributed in the hope that it will be useful, but WITHOUT ANY
 
14
** WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 
15
** FOR A PARTICULAR PURPOSE.  See the GNU Library General Public License for
 
16
** more details.
 
17
** 
 
18
** You should have received a copy of the GNU Library General Public License
 
19
** along with XSB; if not, write to the Free Software Foundation,
 
20
** Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 
21
**
 
22
** $Id: gc_profile.h,v 1.9 2006/04/27 21:08:47 tswift Exp $
 
23
** 
 
24
*/
 
25
 
 
26
#ifdef GC_PROFILE
 
27
#define GC_PROFILE_PRE_REPORT \
 
28
do { \
 
29
  /* first, run the heap & collect info */ \
 
30
  if (examine_data) { \
 
31
    CPtr cell_ptr; \
 
32
    unsigned long heap_traversed=0; \
 
33
    int i,tag; \
 
34
    functor = 0; \
 
35
    for (i=0; i<9; i++)  \
 
36
      tag_examined[i] = 0; \
 
37
    \
 
38
    stack_boundaries; \
 
39
    \
 
40
    for (cell_ptr = (CPtr) glstack.low; cell_ptr < hreg; cell_ptr++) { \
 
41
      heap_traversed++; \
 
42
      tag = cell_tag(*cell_ptr); \
 
43
      if (tag == XSB_REF || tag == XSB_REF1) { \
 
44
        if (points_into_heap((CPtr) *cell_ptr)) { \
 
45
          if (cell_ptr == (CPtr) *cell_ptr) { \
 
46
            ++tag_examined[8]; \
 
47
          } else { \
 
48
            tag_examined[tag]++; \
 
49
          } \
 
50
        } else if (points_into_heap((CPtr) cell_ptr)) { \
 
51
          functor++; \
 
52
        } else { \
 
53
          fprintf(stddbg,"+++ outside pointer %p in %p.\n", \
 
54
                  *(CPtr*)cell_ptr, (CPtr)cell_ptr); \
 
55
        } \
 
56
      } else { \
 
57
        ++tag_examined[tag]; \
 
58
      } \
 
59
    } \
 
60
    \
 
61
    fprintf(stddbg,"\n\n\n{GC} Prior to GC:\n"); \
 
62
    fprintf(stddbg,"{GC} Cells visited:\n"); \
 
63
    fprintf(stddbg, "{GC}    %ld variables.\n", tag_examined[8]); \
 
64
    fprintf(stddbg, "{GC}    %ld reference cells.\n",  \
 
65
            tag_examined[XSB_REF] + tag_examined[XSB_REF1]); \
 
66
    fprintf(stddbg, "{GC}    %ld references from the local stack.\n", \
 
67
            chain_from_ls); \
 
68
    fprintf(stddbg, "{GC}    %ld atom cells.\n", tag_examined[XSB_STRING]); \
 
69
    fprintf(stddbg, "{GC}    %ld integer cells.\n", tag_examined[XSB_INT]); \
 
70
    fprintf(stddbg, "{GC}    %ld float cells.\n", tag_examined[XSB_FLOAT]); \
 
71
    fprintf(stddbg, "{GC}    %ld list cells.\n", tag_examined[XSB_LIST]); \
 
72
    fprintf(stddbg, "{GC}    %ld structure cells.\n",  \
 
73
            tag_examined[XSB_STRUCT]); \
 
74
    fprintf(stddbg, "{GC}    %ld functor cells.\n",functor); \
 
75
    fprintf(stddbg, "{GC}    %ld attributed variable cells.\n",  \
 
76
            tag_examined[XSB_ATTV]); \
 
77
    fprintf(stddbg, "{GC}    %ld heap cells traversed.\n", \
 
78
            heap_traversed); \
 
79
    \
 
80
  } \
 
81
  \
 
82
  if (count_chains) { \
 
83
    int i; \
 
84
    for (i=0; i<64; i++) \
 
85
      chains[i]=0; \
 
86
  } \
 
87
  \
 
88
  if (examine_data){  \
 
89
    int i; \
 
90
    for (i=0; i<9; i++) { \
 
91
      tag_examined[i]=0; \
 
92
      /*        tag_examined_current[i]=0; */ \
 
93
    }\
 
94
    chain_from_ls = functor = 0; \
 
95
    current_mark = deep_mark=0; \
 
96
    start_hbreg = cp_hreg(breg); \
 
97
    old_gens = ((unsigned long) start_hbreg - (unsigned long) glstack.low) /  \
 
98
      sizeof(CPtr); \
 
99
    current_gen = ((unsigned long) hreg - (unsigned long) start_hbreg) /  \
 
100
      sizeof(CPtr); \
 
101
    active_cps = 0; \
 
102
    frozen_cps = 0; \
 
103
  }\
 
104
} while(0)
 
105
 
 
106
#define INIT_GC_PROFILE \
 
107
  verbose_gc=pflags[VERBOSE_GC]; \
 
108
  examine_data=pflags[EXAMINE_DATA]; \
 
109
  count_chains=pflags[COUNT_CHAINS]
 
110
 
 
111
#define DECL_GC_PROFILE \
 
112
  unsigned long begin_slidetime, begin_copy_time
 
113
 
 
114
#define GC_PROFILE_START_SUMMARY \
 
115
do { \
 
116
    if (verbose_gc) { \
 
117
      xsb_dbgmsg((LOG_GC,"{GC} Heap gc - arity = %d - used = %d - left = %d - #gc = %d\n", \
 
118
                 arity,hreg+1-(CPtr)glstack.low,ereg-hreg,num_gc)); \
 
119
    } \
 
120
} while(0)
 
121
 
 
122
#define GC_PROFILE_MARK_SUMMARY \
 
123
do { \
 
124
    if (verbose_gc) { \
 
125
      xsb_dbgmsg((LOG_GC, "{GC} Heap gc - marking finished - #marked = %d - start compact\n", \
 
126
                 marked)); \
 
127
    }  \
 
128
} while (0)
 
129
 
 
130
#define GC_PROFILE_QUIT_MSG \
 
131
do { \
 
132
        if (verbose_gc) { \
 
133
          xsb_dbgmsg((LOG_GC, "{GC} Heap gc - marked too much - quitting gc\n")); \
 
134
        } \
 
135
} while (0)
 
136
 
 
137
#define GC_PROFILE_SLIDE_START_TIME begin_slidetime = end_marktime
 
138
 
 
139
#define GC_PROFILE_SLIDE_FINAL_SUMMARY \
 
140
do { \
 
141
        if (verbose_gc) { \
 
142
          xsb_dbgmsg((LOG_GC, "{GC} Heap gc end - mark time = %f; slide time = %f; total = %f\n", \
 
143
          (double)(end_marktime - begin_marktime)*1000/CLOCKS_PER_SEC, \
 
144
          (double)(end_slidetime - begin_slidetime)*1000/CLOCKS_PER_SEC, \
 
145
          total_time_gc)) ; \
 
146
        } \
 
147
} while(0)
 
148
 
 
149
#define GC_PROFILE_COPY_START_TIME begin_copy_time = end_marktime
 
150
 
 
151
#define GC_PROFILE_COPY_FINAL_SUMMARY \
 
152
do { \
 
153
        if (verbose_gc) { \
 
154
          fprintf(stddbg,"{GC} Heap gc end - mark time = %f; copy_time = %f; total = %f\n", \
 
155
           (double)(end_marktime - begin_marktime)*1000/CLOCKS_PER_SEC, \
 
156
           (double)(end_copy_time - begin_copy_time)*1000/CLOCKS_PER_SEC, \
 
157
           total_time_gc) ; \
 
158
        } \
 
159
} while(0)
 
160
 
 
161
#define GC_PROFILE_POST_REPORT \
 
162
do { \
 
163
  if (count_chains|examine_data) { \
 
164
    fprintf(stddbg, "\n{GC} Heap Garbage Collection #%d\n",num_gc); \
 
165
    fprintf(stddbg, "{GC} Heap early reset reclaimed %d cells.\n", \
 
166
            heap_early_reset); \
 
167
    fprintf(stddbg, "{GC} Local early reset reclaimed %d cells.\n", \
 
168
            ls_early_reset); \
 
169
  } \
 
170
\
 
171
  if (count_chains) { \
 
172
    int i; \
 
173
    fprintf(stddbg,"{GC} Reference Chains: \n"); \
 
174
    for (i=0; i<64; i++) \
 
175
      if (chains[i]) \
 
176
        fprintf(stddbg, "{GC}  chain[%d]=%ld\n",i,chains[i]); \
 
177
  } \
 
178
\
 
179
  if (examine_data) { \
 
180
    fprintf(stddbg,"{GC} Active Choice-points: %ld\n", active_cps); \
 
181
    fprintf(stddbg,"{GC} Frozen Choice-points: %ld\n",frozen_cps); \
 
182
    fprintf(stddbg,"{GC} Local stack size: %d\n", ls_bot - ls_top); \
 
183
    fprintf(stddbg,"{GC} CP stack size: %d\n", cp_bot - cp_top); \
 
184
    fprintf(stddbg,"{GC} Trail stack size: %d\n", tr_top - tr_bot); \
 
185
    fprintf(stddbg,"{GC} Cells visited:\n"); \
 
186
    fprintf(stddbg, "{GC}    %ld variables.\n", tag_examined[8]); \
 
187
    fprintf(stddbg, "{GC}    %ld reference cells.\n",  \
 
188
            tag_examined[XSB_REF] + tag_examined[XSB_REF1]); \
 
189
    fprintf(stddbg, "{GC}    %ld references from the local stack.\n", \
 
190
            chain_from_ls); \
 
191
    fprintf(stddbg, "{GC}    %ld atom cells.\n", tag_examined[XSB_STRING]); \
 
192
    fprintf(stddbg, "{GC}    %ld integer cells.\n", tag_examined[XSB_INT]); \
 
193
    fprintf(stddbg, "{GC}    %ld float cells.\n", tag_examined[XSB_FLOAT]); \
 
194
    fprintf(stddbg, "{GC}    %ld list cells.\n", tag_examined[XSB_LIST]); \
 
195
    fprintf(stddbg, "{GC}    %ld structure cells.\n",  \
 
196
            tag_examined[XSB_STRUCT]); \
 
197
    fprintf(stddbg, "{GC}    %ld functor cells.\n",functor); \
 
198
    fprintf(stddbg, "{GC}    %ld attributed variable cells.\n",  \
 
199
            tag_examined[XSB_ATTV]); \
 
200
\
 
201
    if (current_gen > 0) \
 
202
      fprintf(stddbg, "{GC} Cells marked on current generation: %ld / %ld = %ld / 100\n", \
 
203
              current_mark, current_gen, (current_mark*100/current_gen)); \
 
204
    if (old_gens > 0) \
 
205
      fprintf(stddbg, "{GC} Cells marked on deep generations: %ld / %ld = %ld / 100\n", \
 
206
              deep_mark, old_gens, (deep_mark*100/old_gens)); \
 
207
    if (current_gen + old_gens > 0) \
 
208
      fprintf(stddbg, "{GC} Total cells marked: %ld / %ld = %ld / 100\n", \
 
209
              deep_mark + current_mark, current_gen + old_gens, \
 
210
              ((deep_mark+current_mark)*100/(current_gen+old_gens))); \
 
211
  } \
 
212
} while (0)
 
213
#else
 
214
#define GC_PROFILE_PRE_REPORT
 
215
#define INIT_GC_PROFILE
 
216
#define DECL_GC_PROFILE
 
217
#define GC_PROFILE_START_SUMMARY
 
218
#define GC_PROFILE_MARK_SUMMARY 
 
219
#define GC_PROFILE_QUIT_MSG
 
220
#define GC_PROFILE_SLIDE_START_TIME
 
221
#define GC_PROFILE_SLIDE_FINAL_SUMMARY
 
222
#define GC_PROFILE_COPY_START_TIME
 
223
#define GC_PROFILE_COPY_FINAL_SUMMARY
 
224
#define GC_PROFILE_POST_REPORT
 
225
#endif
 
226
 
 
227
 
 
228
#ifdef GC_PROFILE
 
229
inline static void inspect_chain(CPtr cell_ptr)
 
230
{
 
231
  int tag;
 
232
  tag = cell_tag(*cell_ptr);
 
233
 
 
234
  if (count_chains) {
 
235
    if ((tag == XSB_REF || tag == XSB_REF1) &&
 
236
        points_into_heap((CPtr)*cell_ptr)) {
 
237
      int temp=0;
 
238
      CPtr ptr = (CPtr) *cell_ptr;
 
239
      if (points_into_ls(cell_ptr)) {
 
240
        temp++;
 
241
        ptr = (CPtr) follow(ptr);
 
242
        chain_from_ls++;
 
243
      }
 
244
      while (isref(ptr) && points_into_heap(ptr) && 
 
245
             ptr != (CPtr) follow(ptr)) {
 
246
        temp++;
 
247
        ptr = (CPtr) follow(ptr);
 
248
      }
 
249
      if (temp > 64)
 
250
        xsb_abort("Chain too long when inspecting cell in %p.\n", cell_ptr);
 
251
      ++chains[temp];
 
252
    }
 
253
  }
 
254
}
 
255
    
 
256
inline static void inspect_ptr(CPtr cell_ptr)
 
257
{
 
258
  int tag;
 
259
  tag = cell_tag(*cell_ptr);
 
260
  
 
261
  inspect_chain(cell_ptr);
 
262
 
 
263
  if (examine_data) {
 
264
    if (tag == XSB_REF || tag == XSB_REF1) {
 
265
      if (points_into_heap((CPtr) *cell_ptr)) {
 
266
        if (cell_ptr == (CPtr) *cell_ptr)
 
267
          ++tag_examined[8];
 
268
        else
 
269
          tag_examined[tag]++;
 
270
      }
 
271
      else if (points_into_heap((CPtr) cell_ptr))
 
272
        functor++;
 
273
      else
 
274
        fprintf(stddbg,"+++ outside pointer %p in %p.\n",
 
275
                *(CPtr*)cell_ptr, (CPtr)cell_ptr);
 
276
    } else
 
277
      ++tag_examined[tag];
 
278
    if ((unsigned long) cell_ptr < (unsigned long) start_hbreg)
 
279
      ++deep_mark;
 
280
    else
 
281
      ++current_mark;
 
282
  }
 
283
}  
 
284
 
 
285
#endif
 
286
 
 
287
/* the following cannot be "static inline" since it's used in
 
288
   emu/trace_xsb.c and also uses global variables in heap_xsb.c
 
289
   --lfcastro */
 
290
void print_gc_statistics(CTXTdecl)
 
291
{
 
292
  char *which = (slide) ? "sliding" : "copying" ;
 
293
 
 
294
  printf("%4d heap garbage collections by %s: collected %ld cells in %lf secs\n\n",
 
295
         num_gc, which, total_collected, total_time_gc);
 
296
}
 
297
 
 
298