1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251
|
/* Part of SWI-Prolog
Author: Jan Wielemaker
E-mail: J.Wielemaker@vu.nl
WWW: http://www.swi-prolog.org
Copyright (c) 1985-2013, University of Amsterdam
VU University Amsterdam
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions
are met:
1. Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in
the documentation and/or other materials provided with the
distribution.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
POSSIBILITY OF SUCH DAMAGE.
*/
/*#define O_DEBUG 1*/
#include "pl-incl.h"
#include "pl-prof.h"
#undef LD
#define LD LOCAL_LD
#ifdef O_PROFILE
#define PROFTYPE_MAGIC 0x639a2fb1
static int identify_def(term_t t, void *handle);
static int get_def(term_t t, void **handle);
static void profile(intptr_t count, PL_local_data_t *__PL_ld);
static PL_prof_type_t prof_default_type =
{ identify_def, /* unify a Definition */
get_def,
NULL, /* dummy */
PROFTYPE_MAGIC
};
#define MAX_PROF_TYPES 10
static PL_prof_type_t *types[MAX_PROF_TYPES] = { &prof_default_type };
#define PROFNODE_MAGIC 0x7ae38f24
typedef struct call_node
{ intptr_t magic; /* PROFNODE_MAGIC */
struct call_node *parent;
void * handle; /* handle to procedure-id */
PL_prof_type_t *type;
uintptr_t calls; /* Calls from the parent */
uintptr_t redos; /* redos while here */
uintptr_t exits; /* exits to the parent */
uintptr_t recur; /* recursive calls */
uintptr_t ticks; /* time-statistics */
uintptr_t sibling_ticks; /* ticks in a siblings */
struct call_node *next; /* next in siblings chain */
struct call_node *siblings; /* my offspring */
} call_node;
static void freeProfileData(void);
static void collectSiblingsTime(void);
int
activateProfiler(prof_status active ARG_LD)
{
int i;
PL_local_data_t *profiling;
PL_LOCK(L_THREAD);
if ( active && (profiling=GD->profile.thread) )
{ term_t tid = PL_new_term_ref();
char msg[100];
PL_unify_thread_id(tid, LD->thread.info->pl_tid);
Ssprintf(msg, "Already profiling thread %d",
profiling->thread.info->pl_tid);
PL_UNLOCK(L_THREAD);
return PL_error(NULL, 0, msg, ERR_PERMISSION,
ATOM_profile, ATOM_thread, tid);
}
LD->profile.active = active;
for(i=0; i<MAX_PROF_TYPES; i++)
{ if ( types[i] && types[i]->activate )
(*types[i]->activate)(active);
}
if ( active )
{ LD->profile.time_at_last_tick =
LD->profile.time_at_start = active == PROF_CPU
? ThreadCPUTime(LD, CPU_USER)
: WallTime();
GD->profile.thread = LD;
} else
{ GD->profile.thread = NULL;
}
PL_UNLOCK(L_THREAD);
updateAlerted(LD);
LD->profile.sum_ok = FALSE;
return TRUE;
}
static int
thread_prof_ticks(PL_local_data_t *ld)
{ double t0 = ld->profile.time_at_last_tick;
double t1 = ld->profile.active == PROF_CPU ? ThreadCPUTime(ld, CPU_USER)
: WallTime();
ld->profile.time_at_last_tick = t1;
DEBUG(MSG_PROF_TICKS,
Sdprintf("%d ms\n", (int)((t1-t0)*1000.0)));
return (int)((t1-t0)*1000.0); /* milliseconds */
}
#ifdef __WINDOWS__
static UINT timer; /* our MM timer */
/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
MS-Windows version
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
#if (_MSC_VER < 1400) && !defined(__MINGW32__)
typedef DWORD DWORD_PTR;
#endif
static void CALLBACK
callTimer(UINT id, UINT msg, DWORD_PTR dwuser, DWORD_PTR dw1, DWORD_PTR dw2)
{ PL_local_data_t *ld;
if ( (ld=GD->profile.thread) )
{ int newticks;
if ( (newticks = thread_prof_ticks(ld)) )
{ if ( newticks < 0 ) /* Windows 95/98/... */
newticks = 1;
}
profile(newticks, ld);
}
}
static bool
startProfiler(prof_status how)
{ GET_LD
MMRESULT rval;
(void)how;
rval = timeSetEvent(10,
5, /* resolution (milliseconds) */
callTimer,
(DWORD_PTR)0,
TIME_PERIODIC);
if ( rval )
timer = rval;
else
return PL_error(NULL, 0, NULL, ERR_SYSCALL, "timeSetEvent");
return activateProfiler(PROF_CPU PASS_LD);
}
void
stopItimer(void)
{ if ( timer )
{ timeKillEvent(timer);
timer = 0;
}
}
#else /*__WINDOWS__*/
/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
POSIX version
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
#ifdef TIME_WITH_SYS_TIME
#include <sys/time.h>
#include <time.h>
#else
#ifdef HAVE_SYS_TIME_H
#include <sys/time.h>
#else
#include <time.h>
#endif
#endif
static struct itimerval value, ovalue; /* itimer controlling structures */
static int itimer = -1; /* ITIMER_* */
static int timer_signal; /* SIG* */
static void
sig_profile(int sig)
{ PL_local_data_t *ld;
(void)sig;
#if !defined(BSD_SIGNALS) && !defined(HAVE_SIGACTION)
signal(SIGPROF, sig_profile);
#endif
if ( (ld=GD->profile.thread) )
{ int newticks;
if ( (newticks = thread_prof_ticks(ld)) )
{ if ( newticks < 0 ) /* Windows 95/98/... */
newticks = 1;
}
profile(newticks, ld);
}
}
static bool
startProfiler(prof_status how)
{ GET_LD
int sig, timer;
if ( how == PROF_CPU )
{ sig = SIGPROF;
timer = ITIMER_PROF;
} else
{ sig = SIGALRM;
timer = ITIMER_REAL;
}
set_sighandler(sig, sig_profile);
timer_signal = sig;
value.it_interval.tv_sec = 0;
value.it_interval.tv_usec = 5000; /* 5ms for real; also ok for cpu */
value.it_value.tv_sec = 0; /* on systems where 0 means now */
value.it_value.tv_usec = 5000;
if ( setitimer(timer, &value, &ovalue) != 0 )
return PL_error(NULL, 0, MSG_ERRNO, ERR_SYSCALL, setitimer);
itimer = timer;
return activateProfiler(how PASS_LD);
}
void
stopItimer(void)
{ if ( itimer != -1 )
{ value.it_interval.tv_sec = 0;
value.it_interval.tv_usec = 0;
value.it_value.tv_sec = 0;
value.it_value.tv_usec = 0;
if ( setitimer(itimer, &value, &ovalue) != 0 )
{ warning("Failed to stop interval timer: %s", OsError());
return;
}
itimer = -1;
}
}
#endif /*__WINDOWS__*/
static int
stopProfiler(void)
{ PL_local_data_t *ld;
if ( (ld=GD->profile.thread) &&
ld->profile.active )
{ double tend = ld->profile.active == PROF_CPU ? ThreadCPUTime(ld, CPU_USER)
: WallTime();
ld->profile.time += tend - ld->profile.time_at_start;
stopItimer();
activateProfiler(PROF_INACTIVE PASS_LDARG(ld));
#ifndef __WINDOWS__
set_sighandler(timer_signal, SIG_IGN);
timer_signal = 0;
#endif
}
return TRUE;
}
/** profiler(-Old, +New)
Unify Old with the state of the profiler and set it according to New.
*/
static int
get_prof_status(term_t t, prof_status *s)
{ GET_LD
atom_t a;
if ( PL_get_atom_ex(t, &a) )
{ switch(a)
{ case ATOM_false:
*s = PROF_INACTIVE;
return TRUE;
case ATOM_true:
case ATOM_cputime:
*s = PROF_CPU;
return TRUE;
case ATOM_walltime:
*s = PROF_WALL;
return TRUE;
default:
PL_domain_error("profile_status", t);
return FALSE;
}
}
return FALSE;
}
static
PRED_IMPL("profiler", 2, profiler, 0)
{ PRED_LD
prof_status val;
if ( !PL_unify_atom(A1,
LD->profile.active == PROF_INACTIVE ? ATOM_false :
LD->profile.active == PROF_CPU ? ATOM_cputime :
ATOM_walltime) )
return FALSE;
if ( PL_compare(A1, A2) == 0 )
return TRUE;
if ( !get_prof_status(A2, &val) )
return FALSE;
if ( val == LD->profile.active )
succeed;
if ( val )
return startProfiler(val);
else
return stopProfiler();
}
/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Prolog query API:
$prof_sibling_of(?Child, ?Parent)
Generate hierachy. If Parent is '-', generate the roots
$prof_node(+Node, -Pred, -Calls, -Redos, -Exits, -TickFraction)
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
static int
get_node(term_t t, call_node **node ARG_LD)
{ if ( PL_is_functor(t, FUNCTOR_dprof_node1) )
{ term_t a = PL_new_term_ref();
void *ptr;
_PL_get_arg(1, t, a);
if ( PL_get_pointer(a, &ptr) )
{ call_node *n = ptr;
if ( n->magic == PROFNODE_MAGIC )
{ *node = n;
succeed;
}
}
}
return PL_error(NULL, 0, NULL, ERR_TYPE, ATOM_profile_node, t);
}
static int
unify_node(term_t t, call_node *node ARG_LD)
{ return PL_unify_term(t,
PL_FUNCTOR, FUNCTOR_dprof_node1,
PL_POINTER, node);
}
static
PRED_IMPL("$prof_sibling_of", 2, prof_sibling_of, PL_FA_NONDETERMINISTIC)
{ PRED_LD
call_node *parent = NULL;
call_node *sibling = NULL;
switch( CTX_CNTRL )
{ case FRG_FIRST_CALL:
{ atom_t a;
if ( !PL_is_variable(A1) )
{ if ( get_node(A1, &sibling PASS_LD) )
{ if ( sibling->parent )
return unify_node(A2, sibling->parent PASS_LD);
}
fail;
} else
{ if ( PL_get_atom(A2, &a) && a == ATOM_minus )
sibling = LD->profile.roots;
else if ( get_node(A2, &parent PASS_LD) )
sibling = parent->siblings;
else
fail;
}
if ( !sibling )
fail;
goto return_sibling;
}
case FRG_REDO:
{ sibling = CTX_PTR;
return_sibling:
if ( !unify_node(A1, sibling PASS_LD) )
fail;
if ( sibling->next )
ForeignRedoPtr(sibling->next);
succeed;
}
case FRG_CUTTED:
default:
succeed;
}
}
static int
identify_def(term_t t, void *handle)
{ return unify_definition(MODULE_user, t, handle, 0, GP_QUALIFY);
}
static int
unify_node_id(term_t t, call_node *n)
{ if ( n->type->magic == PROFTYPE_MAGIC )
{ return (*n->type->unify)(t, n->handle);
} else
{ GET_LD
return PL_unify_pointer(t, n->handle);
}
}
static
PRED_IMPL("$prof_node", 7, prof_node, 0)
{ PRED_LD
call_node *n = NULL;
if ( !get_node(A1, &n PASS_LD) )
fail;
if ( unify_node_id(A2, n) &&
PL_unify_integer(A3, n->calls) &&
PL_unify_integer(A4, n->redos) &&
PL_unify_integer(A5, n->exits) &&
PL_unify_integer(A6, n->recur) &&
PL_unify_integer(A7, n->ticks) )
succeed;
fail;
}
/*******************************
* COLLECT DATA *
*******************************/
/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
prof_procedure_data(+Head,
-TimeSelf, -TimeSiblings, -Parents, -Siblings)
Where Parents = list_of(reference(Head, Calls, Redos))
And Siblings = list_of(reference(Head, Calls, Redos))
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
typedef struct prof_ref
{ struct prof_ref *next; /* next in chain */
void * handle; /* Procedure handle */
PL_prof_type_t *type;
int cycle;
uintptr_t ticks;
uintptr_t sibling_ticks;
uintptr_t calls; /* calls to/from this predicate */
uintptr_t redos; /* redos to/from this predicate */
} prof_ref;
typedef struct
{ Definition def;
uintptr_t ticks;
uintptr_t sibling_ticks;
uintptr_t calls;
uintptr_t redos;
uintptr_t recur;
prof_ref *callers;
prof_ref *callees;
} node_sum;
static void
free_relatives(prof_ref *r)
{ prof_ref *n;
for( ; r; r=n)
{ n = r->next;
freeHeap(r, sizeof(*r));
}
}
#define DEF_SPONTANEOUS (Definition)0
#define DEF_RECURSIVE (Definition)1
static void
add_parent_ref(node_sum *sum,
call_node *self,
void *handle, PL_prof_type_t *type,
int cycle)
{ prof_ref *r;
sum->calls += self->calls;
sum->redos += self->redos;
for(r=sum->callers; r; r=r->next)
{ if ( r->handle == handle && r->cycle == cycle )
{ r->calls += self->calls;
r->redos += self->redos;
r->ticks += self->ticks;
r->sibling_ticks += self->sibling_ticks;
return;
}
}
r = allocHeapOrHalt(sizeof(*r));
r->calls = self->calls;
r->redos = self->redos;
r->ticks = self->ticks;
r->sibling_ticks = self->sibling_ticks;
r->handle = handle;
r->type = type;
r->cycle = cycle;
r->next = sum->callers;
sum->callers = r;
}
static void
add_recursive_ref(node_sum *sum, uintptr_t count, int cycle)
{ prof_ref *r;
for(r=sum->callers; r; r=r->next)
{ if ( r->handle == DEF_RECURSIVE && r->cycle == cycle )
{ r->calls += count;
return;
}
}
r = allocHeapOrHalt(sizeof(*r));
memset(r, 0, sizeof(*r));
r->calls = count;
r->handle = DEF_RECURSIVE;
r->cycle = cycle;
r->next = sum->callers;
sum->callers = r;
}
static void
add_sibling_ref(node_sum *sum, call_node *sibling, int cycle)
{ prof_ref *r;
for(r=sum->callees; r; r=r->next)
{ if ( r->handle == sibling->handle && r->cycle == cycle )
{ r->calls += sibling->calls;
r->redos += sibling->redos;
r->ticks += sibling->ticks;
r->sibling_ticks += sibling->sibling_ticks;
return;
}
}
r = allocHeapOrHalt(sizeof(*r));
r->calls = sibling->calls;
r->redos = sibling->redos;
r->ticks = sibling->ticks;
r->sibling_ticks = sibling->sibling_ticks;
r->handle = sibling->handle;
r->type = sibling->type;
r->cycle = cycle;
r->next = sum->callees;
sum->callees = r;
}
static int
sumProfile(call_node *n, void *handle, PL_prof_type_t *type,
node_sum *sum, int seen ARG_LD)
{ call_node *s;
int count = 0;
if ( n->handle == handle )
{ count++;
if ( !seen )
{ sum->ticks += n->ticks;
sum->sibling_ticks += n->sibling_ticks;
}
if ( n->parent )
add_parent_ref(sum, n, n->parent->handle, n->parent->type, seen);
else
add_parent_ref(sum, n, DEF_SPONTANEOUS, NULL, seen);
if ( n->recur )
add_recursive_ref(sum, n->recur, seen);
for(s=n->siblings; s; s = s->next)
add_sibling_ref(sum, s, seen);
seen++;
}
for(s=n->siblings; s; s = s->next)
count += sumProfile(s, handle, type, sum, seen PASS_LD);
return count;
}
static int
unify_relatives(term_t list, prof_ref *r ARG_LD)
{ term_t tail = PL_copy_term_ref(list);
term_t head = PL_new_term_ref();
term_t tmp = PL_new_term_ref();
static functor_t FUNCTOR_node6;
if ( !FUNCTOR_node6 )
FUNCTOR_node6 = PL_new_functor(PL_new_atom("node"), 6);
for( ; r; r=r->next)
{ int rc;
if ( !PL_unify_list(tail, head, tail) )
fail;
PL_put_variable(tmp);
if ( r->handle == DEF_SPONTANEOUS )
rc=PL_unify_atom_chars(tmp, "<spontaneous>");
else if ( r->handle == DEF_RECURSIVE )
rc=PL_unify_atom_chars(tmp, "<recursive>");
else
rc=(*r->type->unify)(tmp, r->handle);
if ( !rc ||
!PL_unify_term(head, PL_FUNCTOR, FUNCTOR_node6,
PL_TERM, tmp,
PL_INT, r->cycle,
PL_LONG, r->ticks,
PL_LONG, r->sibling_ticks,
PL_LONG, r->calls,
PL_LONG, r->redos) )
fail;
}
return PL_unify_nil(tail);
}
static int
get_def(term_t t, void **handle)
{ Procedure proc;
if ( get_procedure(t, &proc, 0, GP_FIND) )
{ *handle = proc->definition;
succeed;
}
fail;
}
static int
get_handle(term_t t, void **handle)
{ int i;
for(i=0; i<MAX_PROF_TYPES; i++)
{ if ( types[i] && types[i]->get )
{ switch( (*types[i]->get)(t, handle) )
{ case TRUE:
succeed;
case FALSE:
break;
default:
assert(0);
}
}
}
fail;
}
/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
$prof_procedure_data(+Procedure,
-Ticks, -TicksSiblings,
-Calls, -Redos,
-Callers, -Callees)
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
static
PRED_IMPL("$prof_procedure_data", 7, prof_procedure_data, PL_FA_TRANSPARENT)
{ PRED_LD
void *handle;
node_sum sum;
call_node *n;
int rc;
int count = 0;
if ( !get_handle(A1, &handle) )
fail;
collectSiblingsTime();
memset(&sum, 0, sizeof(sum));
for(n=LD->profile.roots; n; n=n->next)
count += sumProfile(n, handle, &prof_default_type, &sum, 0 PASS_LD);
if ( count == 0 )
fail; /* nothing known about this one */
rc = ( PL_unify_integer(A2, sum.ticks) &&
PL_unify_integer(A3, sum.sibling_ticks) &&
PL_unify_integer(A4, sum.calls) &&
PL_unify_integer(A5, sum.redos) &&
unify_relatives(A6, sum.callers PASS_LD) &&
unify_relatives(A7, sum.callees PASS_LD)
);
free_relatives(sum.callers);
free_relatives(sum.callees);
return rc ? TRUE : FALSE;
}
/** '$prof_statistics'(-Samples, -Ticks, -AccountingTicks, -Time, -Nodes)
@arg Samples is the number of times the statistical profiler was called.
@arg Ticks is the number of virtual ticks during profiling
@arg AccountingTicks are tick spent on accounting
@arg Time is the total CPU time spent profiling
@arg Nodes is the number of nodes in the call tree
*/
static
PRED_IMPL("$prof_statistics", 5, prof_statistics, 0)
{ PRED_LD
if ( PL_unify_integer(A1, LD->profile.samples) &&
PL_unify_integer(A2, LD->profile.ticks) &&
PL_unify_integer(A3, LD->profile.accounting_ticks) &&
PL_unify_float( A4, LD->profile.time) &&
PL_unify_integer(A5, LD->profile.nodes) )
succeed;
fail;
}
/*******************************
* RESET *
*******************************/
bool
resetProfiler(void)
{ GET_LD
stopProfiler();
freeProfileData();
LD->profile.samples = 0;
LD->profile.ticks = 0;
LD->profile.accounting_ticks = 0;
LD->profile.time = 0.0;
LD->profile.accounting = FALSE;
succeed;
}
static
PRED_IMPL("reset_profiler", 0, reset_profiler, 0)
{ resetProfiler();
succeed;
}
/*******************************
* TOPLEVEL *
*******************************/
static
PRED_IMPL("$profile", 2, profile, PL_FA_TRANSPARENT)
{ int rc;
prof_status val;
if ( !get_prof_status(A2, &val) )
return FALSE;
resetProfiler();
startProfiler(val);
rc = callProlog(NULL, A1, PL_Q_PASS_EXCEPTION, NULL);
stopProfiler();
return rc;
}
/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
This function is responsible for collection the profiling statistics at
run time. It is called by the UNIX interval timer on each clock tick of
the machine (every 20 milli seconds). If profiling is plain we just
increment the profiling tick of the procedure on top of the stack. For
cumulative profiling we have to scan the entire local stack. As we
don't want to increment each invokation of recursive functions on the
stack we maintain a flag on each function. This flag is set the first
time the function is found on the stack. If is is found set the profile
counter will not be incremented. We do a second pass over the frames to
clear the flags again.
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
static void
profile(intptr_t count, PL_local_data_t *__PL_ld)
{ call_node *node;
if ( !HAS_LD )
return;
LD->profile.samples++;
LD->profile.ticks += count;
if ( LD->profile.accounting ) /* we are updating nodes */
{ LD->profile.accounting_ticks += count;
} else if ( (node=LD->profile.current) && node->magic == PROFNODE_MAGIC )
{ node->ticks += count;
}
}
/*******************************
* HIERARCHY ACCOUNTING *
*******************************/
/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
profCall(Definition handle)
Make a call from the current node to handle.
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
static call_node *
prof_call(void *handle, PL_prof_type_t *type ARG_LD)
{ call_node *node = LD->profile.current;
LD->profile.accounting = TRUE;
if ( !node ) /* root-node of the profile */
{ for(node = LD->profile.roots; node; node=node->next)
{ if ( node->handle == handle )
{ node->calls++;
LD->profile.current = node;
DEBUG(MSG_PROF_CALLTREE,
Sdprintf("existing root %p\n", LD->profile.current));
LD->profile.accounting = FALSE;
return node;
}
}
node = allocHeapOrHalt(sizeof(*node));
memset(node, 0, sizeof(*node));
LD->profile.nodes++;
node->magic = PROFNODE_MAGIC;
node->handle = handle;
node->type = type;
node->calls++;
node->next = LD->profile.roots;
LD->profile.roots = node;
LD->profile.current = node;
LD->profile.accounting = FALSE;
DEBUG(MSG_PROF_CALLTREE, Sdprintf("new root %p\n", node));
return node;
}
/* straight recursion */
if ( node->handle == handle )
{ node->recur++;
DEBUG(MSG_PROF_CALLTREE, Sdprintf("direct recursion\n"));
LD->profile.accounting = FALSE;
return node;
} else /* from same parent */
{ void *parent = node->handle;
for(node=node->parent; node; node = node->parent)
{ if ( node->handle == handle &&
node->parent &&
node->parent->handle == parent )
{ node->recur++;
LD->profile.current = node;
DEBUG(MSG_PROF_CALLTREE, Sdprintf("indirect recursion\n"));
LD->profile.accounting = FALSE;
return node;
}
}
}
for(node=LD->profile.current->siblings; node; node=node->next)
{ if ( node->handle == handle )
{ LD->profile.current = node;
node->calls++;
DEBUG(MSG_PROF_CALLTREE, Sdprintf("existing child\n"));
LD->profile.accounting = FALSE;
return node;
}
}
node = allocHeapOrHalt(sizeof(*node));
memset(node, 0, sizeof(*node));
LD->profile.nodes++;
node->magic = PROFNODE_MAGIC;
node->handle = handle;
node->type = type;
node->parent = LD->profile.current;
node->calls++;
node->next = LD->profile.current->siblings;
LD->profile.current->siblings = node;
LD->profile.current = node;
DEBUG(MSG_PROF_CALLTREE, Sdprintf("new child\n"));
LD->profile.accounting = FALSE;
return node;
}
call_node *
profCall(Definition def ARG_LD)
{ if ( true(def, P_NOPROFILE) )
return LD->profile.current;
return prof_call(def, &prof_default_type PASS_LD);
}
/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Exit, resuming execution in node. Note that we ignore the node if the
magic doesn't fit. That can happen using tprofile/1 because the stop
doesn't need to match the start. Actually, we should clear prof_node
references when clearing the data, but this is rather complicated (must
be synchornised with atom-gc) and it still doesn't cope with foreign
code. Considering this is development only, we'll leave this for now.
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
void
profResumeParent(struct call_node *node ARG_LD)
{ call_node *n;
if ( node && node->magic != PROFNODE_MAGIC )
return;
LD->profile.accounting = TRUE;
for(n=LD->profile.current; n && n != node; n=n->parent)
{ n->exits++;
}
LD->profile.accounting = FALSE;
LD->profile.current = node;
}
void
profExit(struct call_node *node ARG_LD)
{ if ( !node || node->magic != PROFNODE_MAGIC )
return;
profResumeParent(node->parent PASS_LD);
}
void
profRedo(struct call_node *node ARG_LD)
{ if ( node && node->magic != PROFNODE_MAGIC )
return;
if ( node )
{ node->redos++;
}
LD->profile.current = node;
}
void
profSetHandle(struct call_node *node, void *handle)
{ node->handle = handle;
}
/*******************************
* FOREIGN ACCESS *
*******************************/
int
PL_register_profile_type(PL_prof_type_t *type)
{ int i;
for(i=0; i<MAX_PROF_TYPES; i++)
{ if ( types[i] == type )
return TRUE;
}
for(i=0; i<MAX_PROF_TYPES; i++)
{ if ( !types[i] )
{ types[i] = type;
type->magic = PROFTYPE_MAGIC;
return TRUE;
}
}
assert(0);
return FALSE;
}
void *
PL_prof_call(void *handle, PL_prof_type_t *type)
{ GET_LD
return prof_call(handle, type PASS_LD);
}
void
PL_prof_exit(void *node)
{ GET_LD
struct call_node *n = node;
profResumeParent(n->parent PASS_LD);
}
/*******************************
* COLLECT *
*******************************/
static uintptr_t
collectSiblingsNode(call_node *n)
{ call_node *s;
uintptr_t count = 0;
for(s=n->siblings; s; s=s->next)
{ count += collectSiblingsNode(s);
n->sibling_ticks = count;
}
return count+n->ticks;
}
static void
collectSiblingsTime(void)
{ GET_LD
if ( !LD->profile.sum_ok )
{ call_node *n;
for(n=LD->profile.roots; n; n=n->next)
collectSiblingsNode(n);
LD->profile.sum_ok = TRUE;
}
}
static void
freeProfileNode(call_node *node ARG_LD)
{ call_node *n, *next;
assert(node->magic == PROFNODE_MAGIC);
for(n=node->siblings; n; n=next)
{ next = n->next;
freeProfileNode(n PASS_LD);
}
node->magic = 0;
freeHeap(node, sizeof(*node));
LD->profile.nodes--;
}
static void
freeProfileData(void)
{ GET_LD
call_node *n, *next;
n = LD->profile.roots;
LD->profile.roots = NULL;
LD->profile.current = NULL;
for(; n; n=next)
{ next = n->next;
freeProfileNode(n PASS_LD);
}
assert(LD->profile.nodes == 0);
}
#else /* O_PROFILE */
/*******************************
* NO PROFILER *
*******************************/
void
stopItimer(void)
{
}
static
PRED_IMPL("profiler", 2, profiler, 0)
{ return notImplemented("profile", 2);
}
static
PRED_IMPL("reset_profiler", 0, reset_profiler, 0)
{ return notImplemented("reset_profile", 0);
}
static
PRED_IMPL("$prof_node", 7, prof_node, 0)
{ return notImplemented("profile_node", 7);
}
static
PRED_IMPL("$prof_sibling_of", 2, prof_sibling_of, PL_FA_NONDETERMINISTIC)
{ return notImplemented("profile_sibling_of", 2);
}
static
PRED_IMPL("$profile", 2, profile, PL_FA_TRANSPARENT)
{ return notImplemented("$profile", 2);
}
static
PRED_IMPL("$prof_procedure_data", 7, prof_procedure_data, PL_FA_TRANSPARENT)
{ return notImplemented("$prof_procedure_data", 7);
}
static
PRED_IMPL("$prof_statistics", 5, prof_statistics, 0)
{ return notImplemented("$prof_statistics", 5);
}
/* Foreign interface of the profiler
*/
int
PL_register_profile_type(PL_prof_type_t *type)
{ return FALSE; /* not supported */
}
void *
PL_prof_call(void *handle, PL_prof_type_t *type)
{ return NULL;
}
void
PL_prof_exit(void *node)
{
}
#endif /* O_PROFILE */
#ifdef O_PROF_PENTIUM
#include "pentium.c"
PRED_IMPL("show_pentium_profile", 0, show_pentium_profile, 0)
{ prof_report();
succeed;
}
PRED_IMPL("reset_pentium_profile", 0, reset_pentium_profile, 0)
{ prof_reset();
succeed;
}
#endif
/*******************************
* PUBLISH PREDICATES *
*******************************/
BeginPredDefs(profile)
PRED_DEF("$profile", 2, profile, PL_FA_TRANSPARENT)
PRED_DEF("profiler", 2, profiler, 0)
PRED_DEF("reset_profiler", 0, reset_profiler, 0)
PRED_DEF("$prof_node", 7, prof_node, 0)
PRED_DEF("$prof_sibling_of", 2, prof_sibling_of, PL_FA_NONDETERMINISTIC)
PRED_DEF("$prof_procedure_data", 7, prof_procedure_data, PL_FA_TRANSPARENT)
PRED_DEF("$prof_statistics", 5, prof_statistics, 0)
#ifdef O_PROF_PENTIUM
PRED_DEF("show_pentium_profile", 0, show_pentium_profile, 0)
PRED_DEF("reset_pentium_profile", 0, reset_pentium_profile, 0)
#endif
EndPredDefs
|