1 /*************************************************************************
2 *									 *
3 *	 YAP Prolog 							 *
4 *									 *
5 *	Yap Prolog was developed at NCCUP - Universidade do Porto	 *
6 *									 *
7 * Copyright L.Damas, V.S.Costa and Universidade do Porto 1985-1997	 *
8 *									 *
9 **************************************************************************
10 *									 *
11 * File:		analyst.c						 *
12 * Last rev:								 *
13 * mods:									 *
14 * comments:	Tracing the abstract machine				 *
15 *									 *
16 *************************************************************************/
17 #ifdef SCCS
18 static char SccsId[] = "%W% %G%";
19 
20 #endif
21 
22 #include "Yap.h"
23 
24 #ifdef ANALYST
25 #include "Yatom.h"
26 #include "yapio.h"
27 #ifdef HAVE_STRING_H
28 #include <string.h>
29 #endif
30 
31 YAP_ULONG_LONG Yap_opcount[_std_top + 1];
32 
33 YAP_ULONG_LONG Yap_2opcount[_std_top + 1][_std_top + 1];
34 
35 
36 STATIC_PROTO(Int p_reset_op_counters, (void));
37 STATIC_PROTO(Int p_show_op_counters, (void));
38 STATIC_PROTO(Int p_show_ops_by_group, (void));
39 
40 static Int
p_reset_op_counters()41 p_reset_op_counters()
42 {
43   int i;
44 
45   for (i = 0; i <= _std_top; ++i)
46     Yap_opcount[i] = 0;
47   return TRUE;
48 }
49 
50 static void
print_instruction(int inst)51 print_instruction(int inst)
52 {
53   int j;
54 
55   fprintf(Yap_stderr, "%s", Yap_op_names[inst]);
56   for (j = strlen(Yap_op_names[inst]); j < 25; j++)
57     putc(' ', Yap_stderr);
58   j = Yap_opcount[inst];
59   if (j < 100000000) {
60     putc(' ', Yap_stderr);
61     if (j < 10000000) {
62       putc(' ', Yap_stderr);
63       if (j < 1000000) {
64 	putc(' ', Yap_stderr);
65 	if (j < 100000) {
66 	  putc(' ', Yap_stderr);
67 	  if (j < 10000) {
68 	    putc(' ', Yap_stderr);
69 	    if (j < 1000) {
70 	      putc(' ', Yap_stderr);
71 	      if (j < 100) {
72 		putc(' ', Yap_stderr);
73 		if (j < 10) {
74 		  putc(' ', Yap_stderr);
75 		}
76 	      }
77 	    }
78 	  }
79 	}
80       }
81     }
82   }
83   fprintf(Yap_stderr, "%llu\n", Yap_opcount[inst]);
84 }
85 
86 static Int
p_show_op_counters()87 p_show_op_counters()
88 {
89   int i;
90   Term t1 = Deref(ARG1);
91 
92   if (IsVarTerm(t1) || !IsAtomTerm(t1)) {
93     return FALSE;
94   } else {
95     Atom at1 = AtomOfTerm(t1);
96 
97     if (IsWideAtom(at1)) {
98       wchar_t *program;
99 
100       program = RepAtom(at1)->WStrOfAE;
101       fprintf(Yap_stderr, "\n Instructions Executed in %S\n", program);
102     } else {
103       char *program;
104 
105       program = RepAtom(at1)->StrOfAE;
106       fprintf(Yap_stderr, "\n Instructions Executed in %s\n", program);
107     }
108   }
109 
110   for (i = 0; i <= _std_top; ++i)
111     print_instruction(i);
112   fprintf(Yap_stderr, "\n Control Instructions \n");
113   print_instruction(_op_fail);
114   print_instruction(_execute);
115   print_instruction(_dexecute);
116   print_instruction(_call);
117   print_instruction(_fcall);
118   print_instruction(_call_cpred);
119   print_instruction(_call_c_wfail);
120   print_instruction(_procceed);
121   print_instruction(_allocate);
122   print_instruction(_deallocate);
123 
124   fprintf(Yap_stderr, "\n Choice Point Manipulation Instructions\n");
125   print_instruction(_try_me);
126   print_instruction(_retry_me);
127   print_instruction(_trust_me);
128   print_instruction(_try_clause);
129   print_instruction(_try_in);
130   print_instruction(_retry);
131   print_instruction(_trust);
132 
133   fprintf(Yap_stderr, "\n Disjunction Instructions\n");
134   print_instruction(_either);
135   print_instruction(_or_else);
136   print_instruction(_or_last);
137   print_instruction(_jump);
138   print_instruction(_move_back);
139 
140   fprintf(Yap_stderr, "\n Dynamic Predicates Choicepoint Instructions\n");
141   print_instruction(_try_and_mark);
142   print_instruction(_retry_and_mark);
143 
144   fprintf(Yap_stderr, "\n C Predicates Choicepoint Instructions\n");
145   print_instruction(_try_c);
146   print_instruction(_retry_c);
147 
148   fprintf(Yap_stderr, "\n Indexing Instructions\n");
149   fprintf(Yap_stderr, "\n  Switch on Type\n");
150   print_instruction(_switch_on_type);
151   print_instruction(_switch_list_nl);
152   print_instruction(_switch_on_arg_type);
153   print_instruction(_switch_on_sub_arg_type);
154   fprintf(Yap_stderr, "\n  Switch on Value\n");
155   print_instruction(_if_cons);
156   print_instruction(_go_on_cons);
157   print_instruction(_switch_on_cons);
158   print_instruction(_if_func);
159   print_instruction(_go_on_func);
160   print_instruction(_switch_on_func);
161   fprintf(Yap_stderr, "\n  Other Switches\n");
162   print_instruction(_if_not_then);
163 
164   fprintf(Yap_stderr, "\n Get Instructions\n");
165   print_instruction(_get_x_var);
166   print_instruction(_get_y_var);
167   print_instruction(_get_x_val);
168   print_instruction(_get_y_val);
169   print_instruction(_get_atom);
170   print_instruction(_get_2atoms);
171   print_instruction(_get_3atoms);
172   print_instruction(_get_4atoms);
173   print_instruction(_get_5atoms);
174   print_instruction(_get_6atoms);
175   print_instruction(_get_list);
176   print_instruction(_get_struct);
177   fprintf(Yap_stderr, "\n   Optimised Get Instructions\n");
178   print_instruction(_glist_valx);
179   print_instruction(_glist_valy);
180   print_instruction(_gl_void_varx);
181   print_instruction(_gl_void_vary);
182   print_instruction(_gl_void_valx);
183   print_instruction(_gl_void_valy);
184 
185   fprintf(Yap_stderr, "\n Unify Read Instructions\n");
186   print_instruction(_unify_x_var);
187   print_instruction(_unify_x_var2);
188   print_instruction(_unify_y_var);
189   print_instruction(_unify_x_val);
190   print_instruction(_unify_y_val);
191   print_instruction(_unify_x_loc);
192   print_instruction(_unify_y_loc);
193   print_instruction(_unify_atom);
194   print_instruction(_unify_n_atoms);
195   print_instruction(_unify_n_voids);
196   print_instruction(_unify_list);
197   print_instruction(_unify_struct);
198   fprintf(Yap_stderr, "\n   Unify Last Read Instructions\n");
199   print_instruction(_unify_l_x_var);
200   print_instruction(_unify_l_x_var2);
201   print_instruction(_unify_l_y_var);
202   print_instruction(_unify_l_x_val);
203   print_instruction(_unify_l_y_val);
204   print_instruction(_unify_l_x_loc);
205   print_instruction(_unify_l_y_loc);
206   print_instruction(_unify_l_atom);
207   print_instruction(_unify_l_n_voids);
208   print_instruction(_unify_l_list);
209   print_instruction(_unify_l_struc);
210 
211   fprintf(Yap_stderr, "\n Unify Write Instructions\n");
212   print_instruction(_unify_x_var_write);
213   print_instruction(_unify_x_var2_write);
214   print_instruction(_unify_y_var_write);
215   print_instruction(_unify_x_val_write);
216   print_instruction(_unify_y_val_write);
217   print_instruction(_unify_x_loc_write);
218   print_instruction(_unify_y_loc_write);
219   print_instruction(_unify_atom_write);
220   print_instruction(_unify_n_atoms_write);
221   print_instruction(_unify_n_voids_write);
222   print_instruction(_unify_list_write);
223   print_instruction(_unify_struct_write);
224   fprintf(Yap_stderr, "\n   Unify Last Read Instructions\n");
225   print_instruction(_unify_l_x_var_write);
226   print_instruction(_unify_l_x_var2_write);
227   print_instruction(_unify_l_y_var_write);
228   print_instruction(_unify_l_x_val_write);
229   print_instruction(_unify_l_y_val_write);
230   print_instruction(_unify_l_x_loc_write);
231   print_instruction(_unify_l_y_loc_write);
232   print_instruction(_unify_l_atom_write);
233   print_instruction(_unify_l_n_voids_write);
234   print_instruction(_unify_l_list_write);
235   print_instruction(_unify_l_struc_write);
236 
237   fprintf(Yap_stderr, "\n Put Instructions\n");
238   print_instruction(_put_x_var);
239   print_instruction(_put_y_var);
240   print_instruction(_put_x_val);
241   print_instruction(_put_xx_val);
242   print_instruction(_put_y_val);
243   print_instruction(_put_unsafe);
244   print_instruction(_put_atom);
245   print_instruction(_put_list);
246   print_instruction(_put_struct);
247 
248   fprintf(Yap_stderr, "\n Write Instructions\n");
249   print_instruction(_write_x_var);
250   print_instruction(_write_y_var);
251   print_instruction(_write_x_val);
252   print_instruction(_write_y_val);
253   print_instruction(_write_x_loc);
254   print_instruction(_write_y_loc);
255   print_instruction(_write_atom);
256   print_instruction(_write_n_atoms);
257   print_instruction(_write_n_voids);
258   print_instruction(_write_list);
259   print_instruction(_write_struct);
260   fprintf(Yap_stderr, "\n   Last Write Instructions\n");
261   print_instruction(_write_l_list);
262   print_instruction(_write_l_struc);
263 
264   fprintf(Yap_stderr, "\n Miscellaneous Instructions\n");
265   print_instruction(_cut);
266   print_instruction(_cut_t);
267   print_instruction(_cut_e);
268   print_instruction(_skip);
269   print_instruction(_pop);
270   print_instruction(_pop_n);
271   print_instruction(_trust_fail);
272   print_instruction(_index_pred);
273   print_instruction(_lock_pred);
274 #if THREADS
275   print_instruction(_thread_local);
276 #endif
277   print_instruction(_save_b_x);
278   print_instruction(_save_b_y);
279   print_instruction(_save_pair_x);
280   print_instruction(_save_pair_y);
281   print_instruction(_save_pair_x_write);
282   print_instruction(_save_pair_y_write);
283   print_instruction(_save_appl_x);
284   print_instruction(_save_appl_y);
285   print_instruction(_save_appl_x_write);
286   print_instruction(_save_appl_y_write);
287   print_instruction(_Ystop);
288   print_instruction(_Nstop);
289 
290   return TRUE;
291 }
292 
293 typedef struct {
294   int nxvar, nxval, nyvar, nyval, ncons, nlist, nstru, nmisc;
295 } uYap_opcount;
296 
297 typedef struct {
298   int ncalls, nexecs, nproceeds, ncallbips, ncuts, nallocs, ndeallocs;
299 } cYap_opcount;
300 
301 typedef struct {
302   int ntries, nretries, ntrusts;
303 } ccpcount;
304 
305 static Int
p_show_ops_by_group(void)306 p_show_ops_by_group(void)
307 {
308 
309   uYap_opcount c_get, c_unify, c_put, c_write;
310   cYap_opcount c_control;
311   ccpcount c_cp;
312   int gets, unifies, puts, writes, controls, choice_pts, indexes, misc,
313     total;
314   Term t1;
315   Atom at1;
316 
317   t1 = Deref(ARG1);
318   if (IsVarTerm(t1) || !IsAtomTerm(t1))
319     return (FALSE);
320   at1 = AtomOfTerm(t1);
321   if (IsWideAtom(at1)) {
322     wchar_t *program;
323 
324     program = RepAtom(at1)->WStrOfAE;
325     fprintf(Yap_stderr, "\n Instructions Executed in %S\n", program);
326   } else {
327     char *program;
328 
329     program = RepAtom(at1)->StrOfAE;
330     fprintf(Yap_stderr, "\n Instructions Executed in %s\n", program);
331   }
332 
333   c_get.nxvar =
334     Yap_opcount[_get_x_var];
335   c_get.nyvar =
336     Yap_opcount[_get_y_var];
337   c_get.nxval =
338     Yap_opcount[_get_x_val];
339   c_get.nyval =
340     Yap_opcount[_get_y_val];
341   c_get.ncons =
342     Yap_opcount[_get_atom]+
343     Yap_opcount[_get_2atoms]+
344     Yap_opcount[_get_3atoms]+
345     Yap_opcount[_get_4atoms]+
346     Yap_opcount[_get_5atoms]+
347     Yap_opcount[_get_6atoms];
348   c_get.nlist =
349     Yap_opcount[_get_list] +
350     Yap_opcount[_glist_valx] +
351     Yap_opcount[_glist_valy] +
352     Yap_opcount[_gl_void_varx] +
353     Yap_opcount[_gl_void_vary] +
354     Yap_opcount[_gl_void_valx] +
355     Yap_opcount[_gl_void_valy];
356   c_get.nstru =
357     Yap_opcount[_get_struct];
358 
359   gets = c_get.nxvar + c_get.nyvar + c_get.nxval + c_get.nyval +
360     c_get.ncons + c_get.nlist + c_get.nstru;
361 
362   c_unify.nxvar =
363     Yap_opcount[_unify_x_var] +
364     Yap_opcount[_unify_void] +
365     Yap_opcount[_unify_n_voids] +
366     2 * Yap_opcount[_unify_x_var2] +
367     2 * Yap_opcount[_gl_void_varx] +
368     Yap_opcount[_gl_void_vary] +
369     Yap_opcount[_gl_void_valx] +
370     Yap_opcount[_unify_l_x_var] +
371     Yap_opcount[_unify_l_void] +
372     Yap_opcount[_unify_l_n_voids] +
373     2 * Yap_opcount[_unify_l_x_var2] +
374     Yap_opcount[_unify_x_var_write] +
375     Yap_opcount[_unify_void_write] +
376     Yap_opcount[_unify_n_voids_write] +
377     2 * Yap_opcount[_unify_x_var2_write] +
378     Yap_opcount[_unify_l_x_var_write] +
379     Yap_opcount[_unify_l_void_write] +
380     Yap_opcount[_unify_l_n_voids_write] +
381     2 * Yap_opcount[_unify_l_x_var2_write];
382   c_unify.nyvar =
383     Yap_opcount[_unify_y_var] +
384     Yap_opcount[_gl_void_vary] +
385     Yap_opcount[_unify_l_y_var] +
386     Yap_opcount[_unify_y_var_write] +
387     Yap_opcount[_unify_l_y_var_write];
388   c_unify.nxval =
389     Yap_opcount[_unify_x_val] +
390     Yap_opcount[_unify_x_loc] +
391     Yap_opcount[_glist_valx] +
392     Yap_opcount[_gl_void_valx] +
393     Yap_opcount[_unify_l_x_val] +
394     Yap_opcount[_unify_l_x_loc] +
395     Yap_opcount[_unify_x_val_write] +
396     Yap_opcount[_unify_x_loc_write] +
397     Yap_opcount[_unify_l_x_val_write] +
398     Yap_opcount[_unify_l_x_loc_write];
399   c_unify.nyval =
400     Yap_opcount[_unify_y_val] +
401     Yap_opcount[_unify_y_loc] +
402     Yap_opcount[_glist_valy] +
403     Yap_opcount[_gl_void_valy] +
404     Yap_opcount[_unify_l_y_val] +
405     Yap_opcount[_unify_l_y_loc] +
406     Yap_opcount[_unify_y_val_write] +
407     Yap_opcount[_unify_y_loc_write] +
408     Yap_opcount[_unify_l_y_val_write] +
409     Yap_opcount[_unify_l_y_loc_write];
410   c_unify.ncons =
411     Yap_opcount[_unify_atom] +
412     Yap_opcount[_unify_n_atoms] +
413     Yap_opcount[_unify_l_atom] +
414     Yap_opcount[_unify_atom_write] +
415     Yap_opcount[_unify_n_atoms_write] +
416     Yap_opcount[_unify_l_atom_write];
417   c_unify.nlist =
418     Yap_opcount[_unify_list] +
419     Yap_opcount[_unify_l_list] +
420     Yap_opcount[_unify_list_write] +
421     Yap_opcount[_unify_l_list_write];
422   c_unify.nstru =
423     Yap_opcount[_unify_struct] +
424     Yap_opcount[_unify_l_struc] +
425     Yap_opcount[_unify_struct_write] +
426     Yap_opcount[_unify_l_struc_write];
427   c_unify.nmisc =
428     Yap_opcount[_pop] +
429     Yap_opcount[_pop_n];
430 
431   unifies = c_unify.nxvar + c_unify.nyvar + c_unify.nxval + c_unify.nyval +
432     c_unify.ncons + c_unify.nlist + c_unify.nstru + c_unify.nmisc;
433 
434   c_put.nxvar =
435     Yap_opcount[_put_x_var];
436   c_put.nyvar =
437     Yap_opcount[_put_y_var];
438   c_put.nxval =
439     Yap_opcount[_put_x_val]+
440     2*Yap_opcount[_put_xx_val];
441   c_put.nyval =
442     Yap_opcount[_put_y_val];
443   c_put.ncons =
444     Yap_opcount[_put_atom];
445   c_put.nlist =
446     Yap_opcount[_put_list];
447   c_put.nstru =
448     Yap_opcount[_put_struct];
449 
450   puts = c_put.nxvar + c_put.nyvar + c_put.nxval + c_put.nyval +
451     c_put.ncons + c_put.nlist + c_put.nstru;
452 
453   c_write.nxvar =
454     Yap_opcount[_write_x_var] +
455     Yap_opcount[_write_void] +
456     Yap_opcount[_write_n_voids];
457   c_write.nyvar =
458     Yap_opcount[_write_y_var];
459   c_write.nxval =
460     Yap_opcount[_write_x_val];
461   c_write.nyval =
462     Yap_opcount[_write_y_val];
463   c_write.ncons =
464     Yap_opcount[_write_atom];
465   c_write.nlist =
466     Yap_opcount[_write_list];
467   c_write.nstru =
468     Yap_opcount[_write_struct];
469 
470   writes = c_write.nxvar + c_write.nyvar + c_write.nxval + c_write.nyval +
471     c_write.ncons + c_write.nlist + c_write.nstru;
472 
473   c_control.nexecs =
474     Yap_opcount[_execute] +
475     Yap_opcount[_dexecute];
476 
477   c_control.ncalls =
478     Yap_opcount[_call] +
479     Yap_opcount[_fcall];
480 
481   c_control.nproceeds =
482     Yap_opcount[_procceed];
483 
484   c_control.ncallbips =
485     Yap_opcount[_call_cpred] +
486     Yap_opcount[_call_c_wfail] +
487     Yap_opcount[_try_c] +
488     Yap_opcount[_retry_c] +
489     Yap_opcount[_op_fail] +
490     Yap_opcount[_trust_fail] +
491     Yap_opcount[_p_atom_x] +
492     Yap_opcount[_p_atom_y] +
493     Yap_opcount[_p_atomic_x] +
494     Yap_opcount[_p_atomic_y] +
495     Yap_opcount[_p_compound_x] +
496     Yap_opcount[_p_compound_y] +
497     Yap_opcount[_p_float_x] +
498     Yap_opcount[_p_float_y] +
499     Yap_opcount[_p_integer_x] +
500     Yap_opcount[_p_integer_y] +
501     Yap_opcount[_p_nonvar_x] +
502     Yap_opcount[_p_nonvar_y] +
503     Yap_opcount[_p_number_x] +
504     Yap_opcount[_p_number_y] +
505     Yap_opcount[_p_var_x] +
506     Yap_opcount[_p_var_y] +
507     Yap_opcount[_p_db_ref_x] +
508     Yap_opcount[_p_db_ref_y] +
509     Yap_opcount[_p_cut_by_x] +
510     Yap_opcount[_p_cut_by_y] +
511     Yap_opcount[_p_primitive_x] +
512     Yap_opcount[_p_primitive_y] +
513     Yap_opcount[_p_equal] +
514     Yap_opcount[_p_plus_vv] +
515     Yap_opcount[_p_plus_vc] +
516     Yap_opcount[_p_plus_y_vv] +
517     Yap_opcount[_p_plus_y_vc] +
518     Yap_opcount[_p_minus_vv] +
519     Yap_opcount[_p_minus_cv] +
520     Yap_opcount[_p_minus_y_vv] +
521     Yap_opcount[_p_minus_y_cv] +
522     Yap_opcount[_p_times_vv] +
523     Yap_opcount[_p_times_vc] +
524     Yap_opcount[_p_times_y_vv] +
525     Yap_opcount[_p_times_y_vc] +
526     Yap_opcount[_p_div_vv] +
527     Yap_opcount[_p_div_vc] +
528     Yap_opcount[_p_div_cv] +
529     Yap_opcount[_p_div_y_vv] +
530     Yap_opcount[_p_div_y_vc] +
531     Yap_opcount[_p_div_y_cv] +
532     Yap_opcount[_p_or_vv] +
533     Yap_opcount[_p_or_vc] +
534     Yap_opcount[_p_or_y_vv] +
535     Yap_opcount[_p_or_y_vc] +
536     Yap_opcount[_p_and_vv] +
537     Yap_opcount[_p_and_vc] +
538     Yap_opcount[_p_and_y_vv] +
539     Yap_opcount[_p_and_y_vc] +
540     Yap_opcount[_p_sll_vv] +
541     Yap_opcount[_p_sll_vc] +
542     Yap_opcount[_p_sll_y_vv] +
543     Yap_opcount[_p_sll_y_vc] +
544     Yap_opcount[_p_slr_vv] +
545     Yap_opcount[_p_slr_vc] +
546     Yap_opcount[_p_slr_y_vv] +
547     Yap_opcount[_p_slr_y_vc] +
548     Yap_opcount[_p_dif] +
549     Yap_opcount[_p_eq] +
550     Yap_opcount[_p_arg_vv] +
551     Yap_opcount[_p_arg_cv] +
552     Yap_opcount[_p_arg_y_vv] +
553     Yap_opcount[_p_arg_y_cv] +
554     Yap_opcount[_p_functor] +
555     Yap_opcount[_p_func2s_vv] +
556     Yap_opcount[_p_func2s_cv] +
557     Yap_opcount[_p_func2s_vc] +
558     Yap_opcount[_p_func2s_y_vv] +
559     Yap_opcount[_p_func2s_y_cv] +
560     Yap_opcount[_p_func2s_y_vc] +
561     Yap_opcount[_p_func2f_xx] +
562     Yap_opcount[_p_func2f_xy] +
563     Yap_opcount[_p_func2f_yx] +
564     Yap_opcount[_p_func2f_yy];
565 
566   c_control.ncuts =
567     Yap_opcount[_cut] +
568     Yap_opcount[_cut_t] +
569     Yap_opcount[_cut_e] +
570     Yap_opcount[_commit_b_x] +
571     Yap_opcount[_commit_b_y];
572 
573   c_control.nallocs =
574     Yap_opcount[_allocate] +
575     Yap_opcount[_fcall];
576 
577   c_control.ndeallocs =
578     Yap_opcount[_dexecute] +
579     Yap_opcount[_deallocate];
580 
581   controls =
582     c_control.nexecs +
583     c_control.ncalls +
584     c_control.nproceeds +
585     c_control.ncuts +
586     c_control.nallocs +
587     c_control.ndeallocs +
588     Yap_opcount[_jump] +
589     Yap_opcount[_move_back] +
590     Yap_opcount[_try_in];
591 
592 
593 
594   c_cp.ntries =
595     Yap_opcount[_try_me] +
596     Yap_opcount[_try_and_mark] +
597     Yap_opcount[_try_c] +
598     Yap_opcount[_try_clause] +
599     Yap_opcount[_either];
600 
601   c_cp.nretries =
602     Yap_opcount[_retry_me] +
603     Yap_opcount[_retry_and_mark] +
604     Yap_opcount[_retry_c] +
605     Yap_opcount[_retry] +
606     Yap_opcount[_or_else];
607 
608   c_cp.ntrusts =
609     Yap_opcount[_trust_me] +
610     Yap_opcount[_trust] +
611     Yap_opcount[_or_last];
612 
613   choice_pts =
614     c_cp.ntries +
615     c_cp.nretries +
616     c_cp.ntrusts;
617 
618   indexes =
619     Yap_opcount[_jump_if_var] +
620     Yap_opcount[_switch_on_type] +
621     Yap_opcount[_switch_list_nl] +
622     Yap_opcount[_switch_on_arg_type] +
623     Yap_opcount[_switch_on_sub_arg_type] +
624     Yap_opcount[_switch_on_cons] +
625     Yap_opcount[_go_on_cons] +
626     Yap_opcount[_if_cons] +
627     Yap_opcount[_switch_on_func] +
628     Yap_opcount[_go_on_func] +
629     Yap_opcount[_if_func] +
630     Yap_opcount[_if_not_then];
631   misc =
632     c_control.ncallbips +
633     Yap_opcount[_Ystop] +
634     Yap_opcount[_Nstop] +
635     Yap_opcount[_index_pred] +
636     Yap_opcount[_lock_pred] +
637 #if THREADS
638     Yap_opcount[_thread_local] +
639 #endif
640     Yap_opcount[_save_b_x] +
641     Yap_opcount[_save_b_y] +
642     Yap_opcount[_undef_p] +
643     Yap_opcount[_spy_pred] +
644     Yap_opcount[_spy_or_trymark] +
645     Yap_opcount[_save_pair_x] +
646     Yap_opcount[_save_pair_y] +
647     Yap_opcount[_save_pair_x_write] +
648     Yap_opcount[_save_pair_y_write] +
649     Yap_opcount[_save_appl_x] +
650     Yap_opcount[_save_appl_y] +
651     Yap_opcount[_save_appl_x_write] +
652     Yap_opcount[_save_appl_y_write];
653   total = gets + unifies + puts + writes + controls + choice_pts + indexes + misc;
654 
655   /*  for (i = 0; i <= _std_top; ++i)
656    * print_instruction(i);
657    */
658 
659   fprintf(Yap_stderr, "Groups are\n\n");
660   fprintf(Yap_stderr, "  GET               instructions: %8d (%3d%%)\n", gets,
661 	     (gets * 100) / total);
662   fprintf(Yap_stderr, "  UNIFY             instructions: %8d (%3d%%)\n", unifies,
663 	     (unifies * 100) / total);
664   fprintf(Yap_stderr, "  PUT               instructions: %8d (%3d%%)\n", puts,
665 	     (puts * 100) / total);
666   fprintf(Yap_stderr, "  WRITE             instructions: %8d (%3d%%)\n", writes,
667 	     (writes * 100) / total);
668   fprintf(Yap_stderr, "  CONTROL           instructions: %8d (%3d%%)\n", controls,
669 	     (controls * 100) / total);
670   fprintf(Yap_stderr, "  CHOICE POINT      instructions: %8d (%3d%%)\n", choice_pts,
671 	     (choice_pts * 100) / total);
672   fprintf(Yap_stderr, "  INDEXING          instructions: %8d (%3d%%)\n", indexes,
673 	     (indexes * 100) / total);
674   fprintf(Yap_stderr, "  MISCELLANEOUS     instructions: %8d (%3d%%)\n", misc,
675 	     (misc * 100) / total);
676   fprintf(Yap_stderr, "_______________________________________________\n");
677   fprintf(Yap_stderr, "   TOTAL            instructions: %8d (%3d%%)\n\n", total,
678 	     (total * 100) / total);
679 
680   fprintf(Yap_stderr, "\n Analysis of Unification Instructions in %s \n", program);
681   fprintf(Yap_stderr, "           XVAR,   YVAR,    XVAL,    YVAL,     CONS,     LIST,  STRUCT\n");
682   fprintf(Yap_stderr, "  GET: %8d %8d %8d %8d %8d %8d %8d\n",
683 	     c_get.nxvar,
684 	     c_get.nyvar,
685 	     c_get.nxval,
686 	     c_get.nyval,
687 	     c_get.ncons,
688 	     c_get.nlist,
689 	     c_get.nstru);
690   fprintf(Yap_stderr, "UNIFY: %8d %8d %8d %8d %8d %8d %8d\n",
691 	     c_unify.nxvar,
692 	     c_unify.nyvar,
693 	     c_unify.nxval,
694 	     c_unify.nyval,
695 	     c_unify.ncons,
696 	     c_unify.nlist,
697 	     c_unify.nstru);
698   fprintf(Yap_stderr, "  PUT: %8d %8d %8d %8d %8d %8d %8d\n",
699 	     c_put.nxvar,
700 	     c_put.nyvar,
701 	     c_put.nxval,
702 	     c_put.nyval,
703 	     c_put.ncons,
704 	     c_put.nlist,
705 	     c_put.nstru);
706   fprintf(Yap_stderr, "WRITE: %8d %8d %8d %8d %8d %8d %8d\n",
707 	     c_write.nxvar,
708 	     c_write.nyvar,
709 	     c_write.nxval,
710 	     c_write.nyval,
711 	     c_write.ncons,
712 	     c_write.nlist,
713 	     c_write.nstru);
714   fprintf(Yap_stderr, "      ___________________________________________________\n");
715   fprintf(Yap_stderr, "TOTAL: %8d %8d %8d %8d %8d %8d %8d\n",
716 	     c_get.nxvar + c_unify.nxvar + c_put.nxvar + c_write.nxvar,
717 	     c_get.nyvar + c_unify.nyvar + c_put.nyvar + c_write.nyvar,
718 	     c_get.nxval + c_unify.nxval + c_put.nxval + c_write.nxval,
719 	     c_get.nyval + c_unify.nyval + c_put.nyval + c_write.nyval,
720 	     c_get.ncons + c_unify.ncons + c_put.ncons + c_write.ncons,
721 	     c_get.nlist + c_unify.nlist + c_put.nlist + c_write.nlist,
722 	     c_get.nstru + c_unify.nstru + c_put.nstru + c_write.nstru
723     );
724 
725   fprintf(Yap_stderr, "\n Analysis of Unification Instructions in %s \n", program);
726   fprintf(Yap_stderr, "           XVAR,   YVAR,    XVAL,    YVAL,     CONS,     LIST,  STRUCT\n");
727   fprintf(Yap_stderr, "  GET:  %3.2f%%  %3.2f%%  %3.2f%%  %3.2f%%  %3.2f%%  %3.2f%%  %3.2f%%\n",
728 	     (((double) c_get.nxvar) * 100) / total,
729 	     (((double) c_get.nyvar) * 100) / total,
730 	     (((double) c_get.nxval) * 100) / total,
731 	     (((double) c_get.nyval) * 100) / total,
732 	     (((double) c_get.ncons) * 100) / total,
733 	     (((double) c_get.nlist) * 100) / total,
734 	     (((double) c_get.nstru) * 100) / total);
735   fprintf(Yap_stderr, "UNIFY:  %3.2f%%  %3.2f%%  %3.2f%%  %3.2f%%  %3.2f%%  %3.2f%%  %3.2f%%\n",
736 	     (((double) c_unify.nxvar) * 100) / total,
737 	     (((double) c_unify.nyvar) * 100) / total,
738 	     (((double) c_unify.nxval) * 100) / total,
739 	     (((double) c_unify.nyval) * 100) / total,
740 	     (((double) c_unify.ncons) * 100) / total,
741 	     (((double) c_unify.nlist) * 100) / total,
742 	     (((double) c_unify.nstru) * 100) / total);
743   fprintf(Yap_stderr, "  PUT:  %3.2f%%  %3.2f%%  %3.2f%%  %3.2f%%  %3.2f%%  %3.2f%%  %3.2f%%\n",
744 	     (((double) c_put.nxvar) * 100) / total,
745 	     (((double) c_put.nyvar) * 100) / total,
746 	     (((double) c_put.nxval) * 100) / total,
747 	     (((double) c_put.nyval) * 100) / total,
748 	     (((double) c_put.ncons) * 100) / total,
749 	     (((double) c_put.nlist) * 100) / total,
750 	     (((double) c_put.nstru) * 100) / total);
751   fprintf(Yap_stderr, "WRITE:  %3.2f%%  %3.2f%%  %3.2f%%  %3.2f%%  %3.2f%%  %3.2f%%  %3.2f%%\n",
752 	     (((double) c_write.nxvar) * 100) / total,
753 	     (((double) c_write.nyvar) * 100) / total,
754 	     (((double) c_write.nxval) * 100) / total,
755 	     (((double) c_write.nyval) * 100) / total,
756 	     (((double) c_write.ncons) * 100) / total,
757 	     (((double) c_write.nlist) * 100) / total,
758 	     (((double) c_write.nstru) * 100) / total);
759   fprintf(Yap_stderr, "      ___________________________________________________\n");
760   fprintf(Yap_stderr, "TOTAL:  %3.2f%%  %3.2f%%  %3.2f%%  %3.2f%%  %3.2f%%  %3.2f%%  %3.2f%%\n",
761 	     (((double) c_get.nxvar + c_unify.nxvar + c_put.nxvar + c_write.nxvar) * 100) / total,
762 	     (((double) c_get.nyvar + c_unify.nyvar + c_put.nyvar + c_write.nyvar) * 100) / total,
763 	     (((double) c_get.nxval + c_unify.nxval + c_put.nxval + c_write.nxval) * 100) / total,
764 	     (((double) c_get.nyval + c_unify.nyval + c_put.nyval + c_write.nyval) * 100) / total,
765 	     (((double) c_get.ncons + c_unify.ncons + c_put.ncons + c_write.ncons) * 100) / total,
766 	     (((double) c_get.nlist + c_unify.nlist + c_put.nlist + c_write.nlist) * 100) / total,
767 	     (((double) c_get.nstru + c_unify.nstru + c_put.nstru + c_write.nstru) * 100) / total
768     );
769 
770   fprintf(Yap_stderr, "\n Control Instructions Executed in %s \n", program);
771   fprintf(Yap_stderr, "Grouped as\n\n");
772   fprintf(Yap_stderr, "  CALL              instructions: %8d (%3d%%)\n",
773 	     c_control.ncalls, (c_control.ncalls * 100) / total);
774   fprintf(Yap_stderr, "  PROCEED           instructions: %8d (%3d%%)\n",
775 	     c_control.nproceeds, (c_control.nproceeds * 100) / total);
776   fprintf(Yap_stderr, "  EXECUTE           instructions: %8d (%3d%%)\n",
777 	     c_control.nexecs, (c_control.nexecs * 100) / total);
778   fprintf(Yap_stderr, "  CUT               instructions: %8d (%3d%%)\n",
779 	     c_control.ncuts, (c_control.ncuts * 100) / total);
780   fprintf(Yap_stderr, "  CALL_BIP          instructions: %8d (%3d%%)\n",
781 	     c_control.ncallbips, (c_control.ncallbips * 100) / total);
782   fprintf(Yap_stderr, "  ALLOCATE          instructions: %8d (%3d%%)\n",
783 	     c_control.nallocs, (c_control.nallocs * 100) / total);
784   fprintf(Yap_stderr, "  DEALLOCATE        instructions: %8d (%3d%%)\n",
785 	     c_control.ndeallocs, (c_control.ndeallocs * 100) / total);
786   fprintf(Yap_stderr, "_______________________________________________\n");
787   fprintf(Yap_stderr, "   TOTAL            instructions: %8d (%3d%%)\n\n", total,
788 	     (total * 100) / total);
789 
790   fprintf(Yap_stderr, "\n Choice Point Manipulation Instructions Executed in %s \n", program);
791   fprintf(Yap_stderr, "Grouped as\n\n");
792   fprintf(Yap_stderr, "  TRY              instructions: %8d (%3d%%)\n",
793 	     c_cp.ntries, (c_cp.ntries * 100) / total);
794   fprintf(Yap_stderr, "  RETRY            instructions: %8d (%3d%%)\n",
795 	     c_cp.nretries, (c_cp.nretries * 100) / total);
796   fprintf(Yap_stderr, "  TRUST            instructions: %8d (%3d%%)\n",
797 	     c_cp.ntrusts, (c_cp.ntrusts * 100) / total);
798   fprintf(Yap_stderr, "_______________________________________________\n");
799   fprintf(Yap_stderr, "   TOTAL            instructions: %8d (%3d%%)\n\n", total,
800 	     (total * 100) / total);
801 
802   return TRUE;
803 }
804 
805 static Int
p_show_sequences(void)806 p_show_sequences(void)
807 {
808   int i, j;
809   YAP_ULONG_LONG min;
810   YAP_ULONG_LONG sum = 0;
811   Term t = Deref(ARG1);
812 
813   if (IsVarTerm(t)) {
814     Yap_Error(INSTANTIATION_ERROR, t, "shows_sequences/1");
815     return FALSE;
816   }
817   if (!IsIntegerTerm(t)) {
818     Yap_Error(TYPE_ERROR_INTEGER, t, "shows_sequences/1");
819     return FALSE;
820   }
821   min = (YAP_ULONG_LONG)IntegerOfTerm(t);
822   if (min <= 0) {
823     Yap_Error(DOMAIN_ERROR_NOT_LESS_THAN_ZERO, t, "shows_sequences/1");
824     return FALSE;
825   }
826   if (min <= 0) {
827     Yap_Error(DOMAIN_ERROR_NOT_LESS_THAN_ZERO, t, "shows_sequences/1");
828     return FALSE;
829   }
830   for (i = 0; i <= _std_top; ++i) {
831     sum += Yap_opcount[i];
832   }
833   for (i = 0; i <= _std_top; ++i) {
834     for (j = 0; j <= _std_top; ++j) {
835       YAP_ULONG_LONG seqs = Yap_2opcount[i][j];
836       if (seqs && sum/seqs <= min) {
837 	/*
838 	Term t[3], t0;
839 	Functor f =
840 	t[0] = Yap_MkFloatTerm(((double)seqs*100.0)/sum);
841 	t[1] = Yap_LookupAtom(Yap_op_names[i]);
842 	t[2] = Yap_LookupAtom(Yap_op_names[j]);
843 	t0 = MkApplTerm(
844 	Yap_MkPairTerm(Yap_op_names[i]
845 	*/
846 	fprintf(stderr,"%f -> %s,%s\n",((double)seqs*100.0)/sum,Yap_op_names[i],Yap_op_names[j]);
847 	/* we found one */
848       }
849     }
850   }
851   return TRUE;
852 }
853 
854 void
Yap_InitAnalystPreds(void)855 Yap_InitAnalystPreds(void)
856 {
857   Yap_InitCPred("wam_profile_reset_op_counters", 0, p_reset_op_counters, SafePredFlag |SyncPredFlag);
858   Yap_InitCPred("wam_profile_show_op_counters", 1, p_show_op_counters, SafePredFlag|SyncPredFlag);
859   Yap_InitCPred("wam_profile_show_ops_by_group", 1, p_show_ops_by_group, SafePredFlag |SyncPredFlag);
860   Yap_InitCPred("wam_profile_show_sequences", 1, p_show_sequences, SafePredFlag |SyncPredFlag);
861 }
862 
863 #endif /* ANALYST */
864