Many hyperlinks are disabled.
Use anonymous login
to enable hyperlinks.
Overview
Comment: | further improvements of tclx's profile command |
---|---|
Timelines: | family | ancestors | descendants | both | trunk |
Files: | files | file ages | folders |
SHA1: |
c6070d8891e3d4a01e7dd389bdd255c2 |
User & Date: | chw 2016-11-25 13:50:09.929 |
Context
2016-11-27
| ||
18:55 | add tk upstream changes check-in: c607156404 user: chw tags: trunk | |
2016-11-25
| ||
13:50 | further improvements of tclx's profile command check-in: c6070d8891 user: chw tags: trunk | |
2016-11-21
| ||
19:27 | simplification of check-in [c29648d90f] check-in: eec9d69bc1 user: chw tags: trunk | |
Changes
Changes to jni/tclx/generic/tclXprofile.c.
︙ | ︙ | |||
14 15 16 17 18 19 20 | *----------------------------------------------------------------------------- * $Id: tclXprofile.c,v 1.4 2009/10/13 19:28:23 kot Exp $ *----------------------------------------------------------------------------- */ #include "tclExtdInt.h" | < < < < < < < < | 14 15 16 17 18 19 20 21 22 23 24 25 26 27 | *----------------------------------------------------------------------------- * $Id: tclXprofile.c,v 1.4 2009/10/13 19:28:23 kot Exp $ *----------------------------------------------------------------------------- */ #include "tclExtdInt.h" /* * For when the level is not known. */ #define UNKNOWN_LEVEL -1 /* * Stack entry used to keep track of an profiling information for procedures |
︙ | ︙ | |||
71 72 73 74 75 76 77 | */ typedef struct profInfo_t { Tcl_Interp *interp; /* Interpreter this is for. */ Tcl_Trace traceHandle; /* Handle to current trace. */ int commandMode; /* Prof all commands? */ int evalMode; /* Use eval stack. */ | | < | < < | < < | 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 | */ typedef struct profInfo_t { Tcl_Interp *interp; /* Interpreter this is for. */ Tcl_Trace traceHandle; /* Handle to current trace. */ int commandMode; /* Prof all commands? */ int evalMode; /* Use eval stack. */ Tcl_Command profileCmd; /* "profile" Tcl command. */ Tcl_Command profExecCmd; /* "profexec" Tcl command. */ Tcl_Command currentCmd; /* Current execution traced cmd. */ int evalLevel; /* Eval level when invoked. */ clock_t realTime; /* Current real and CPU time. */ clock_t cpuTime; clock_t prevRealTime; /* Real and CPU time of previous */ clock_t prevCpuTime; /* trace. */ int updatedTimes; /* Has current times been updated?*/ profEntry_t *stackPtr; /* Proc/command nesting stack. */ |
︙ | ︙ | |||
118 119 120 121 122 123 124 | static void PopEntry _ANSI_ARGS_((profInfo_t *infoPtr)); static void UpdateTOSTimes _ANSI_ARGS_((profInfo_t *infoPtr)); | < < < < < < < < < < < < < < < < < < < < < < < < < < < | | | | | | | | < < < < < < < < < < | 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 | static void PopEntry _ANSI_ARGS_((profInfo_t *infoPtr)); static void UpdateTOSTimes _ANSI_ARGS_((profInfo_t *infoPtr)); static int ProfObjTraceRoutine _ANSI_ARGS_((ClientData clientData, Tcl_Interp *interp, int evalLevel, const char *command, Tcl_Command cmd, int objc, Tcl_Obj *const objv[])); static void CleanDataTable _ANSI_ARGS_((profInfo_t *infoPtr)); static void InitializeProcStack _ANSI_ARGS_((profInfo_t *infoPtr, CallFrame *framePtr)); |
︙ | ︙ | |||
410 411 412 413 414 415 416 | infoPtr->realTime - infoPtr->prevRealTime; infoPtr->scopeChainPtr->scopeCpuTime += infoPtr->cpuTime - infoPtr->prevCpuTime; } } /*----------------------------------------------------------------------------- | < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < | | | > | < > | | < | < | > | | < < < | | > > > | | | | > | > | < < < < < < < < | < < < < < | | | > > | | < | | | < < < < | | > > > > > > > > | > > | > > > > > > > > > | | | | > > > > > > > > > > > > > > > > > > > > | > > > | > > > > | > | | > | > | > > > > > | > > > > > | | | | > > > > | > > > > | > > > > > > | > > > | > > > | > > > > | > | > > > > > | > > | > > > | > > > > > > > > | > > > > > | > > > | | < > > > | 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 | infoPtr->realTime - infoPtr->prevRealTime; infoPtr->scopeChainPtr->scopeCpuTime += infoPtr->cpuTime - infoPtr->prevCpuTime; } } /*----------------------------------------------------------------------------- * ProfObjTraceRoutine -- * Routine called by Tcl_Eval to do profiling. It intercepts the current * command being executed by installing an execution trace. *----------------------------------------------------------------------------- */ static int ProfObjTraceRoutine (clientData, interp, evalLevel, command, cmd, objc, objv) ClientData clientData; Tcl_Interp *interp; int evalLevel; const char *command; Tcl_Command cmd; int objc; Tcl_Obj *const objv[]; { profInfo_t *infoPtr = (profInfo_t *) clientData; Tcl_Obj *listPtr, *namePtr; int result; if (infoPtr->currentCmd != NULL) return TCL_OK; listPtr = Tcl_NewObj (); Tcl_ListObjAppendElement (NULL, listPtr, Tcl_NewStringObj ("::trace", -1)); Tcl_ListObjAppendElement (NULL, listPtr, Tcl_NewStringObj ("add", -1)); Tcl_ListObjAppendElement (NULL, listPtr, Tcl_NewStringObj ("execution", -1)); namePtr = Tcl_NewObj (); Tcl_GetCommandFullName (infoPtr->interp, cmd, namePtr); Tcl_ListObjAppendElement (NULL, listPtr, namePtr); Tcl_ListObjAppendElement (NULL, listPtr, Tcl_NewStringObj ("enter leave enterstep leavestep", -1)); namePtr = Tcl_NewObj (); Tcl_GetCommandFullName (infoPtr->interp, infoPtr->profExecCmd, namePtr); Tcl_ListObjAppendElement (NULL, listPtr, namePtr); result = Tcl_EvalObjEx (interp, listPtr, TCL_EVAL_GLOBAL); if (result == TCL_OK) infoPtr->currentCmd = cmd; return result; } /*----------------------------------------------------------------------------- * ProfExecObjCmd -- * Implements the TCL profexec command which is the callback for * execution traces. Signatures: * profexec cmdstring op "enter" and "enterstep" * profexec cmdstring code result op "leave" and "leavestep" *----------------------------------------------------------------------------- */ static int ProfExecObjCmd (clientData, interp, objc, objv) ClientData clientData; Tcl_Interp *interp; int objc; Tcl_Obj *CONST objv[]; { profInfo_t *infoPtr = (profInfo_t *) clientData; Interp *iPtr = (Interp *) interp; const char *opStr; int cmdObjc, procLevel, scopeLevel, isProc; Tcl_Obj **cmdObjv, *fullCmdNameObj; Tcl_Command cmd; char *cmdName, *fullCmdName; CallFrame *framePtr; if ((objc != 3) && (objc != 5)) return TclX_WrongArgs (interp, objv [0], "cmdstring op|code ?result op?"); if (infoPtr->traceHandle == NULL || infoPtr->currentCmd == NULL) return TCL_OK; /* Ignored when not active */ if (objc == 3) { /* enter/enterstep */ opStr = Tcl_GetStringFromObj (objv[2], NULL); if ((strcmp ("enter", opStr) != 0) && (strcmp ("enterstep", opStr) != 0)) return TCL_OK; /* Ignore unknown ops */ Tcl_ListObjGetElements (NULL, objv[1], &cmdObjc, &cmdObjv); if (cmdObjc > 0) { cmdName = Tcl_GetStringFromObj (cmdObjv[0], NULL); cmd = Tcl_FindCommand (interp, cmdName, NULL, 0); if (cmd == NULL) panic (PROF_PANIC, 1000); fullCmdNameObj = Tcl_NewObj (); Tcl_GetCommandFullName (interp, cmd, fullCmdNameObj); fullCmdName = Tcl_GetStringFromObj (fullCmdNameObj, NULL); /* * Determine current proc and var levels. */ procLevel = 0; for (framePtr = iPtr->framePtr; framePtr != NULL; framePtr = framePtr->callerPtr) { procLevel++; } scopeLevel = (iPtr->varFramePtr == NULL) ? 0 : iPtr->varFramePtr->level; /* * If there are entries on the stack that are at a higher * proc call level than we are, we have exited into the * initial entries that where pushed on the stack before * we started. Pop those entries. */ if (infoPtr->stackPtr->procLevel > procLevel) { UpdateTOSTimes (infoPtr); do { if (infoPtr->stackPtr->evalLevel != UNKNOWN_LEVEL) panic (PROF_PANIC, 2); /* Not an initial entry */ if (infoPtr->stackPtr->prevEntryPtr == NULL) break; /* Keep first entry */ PopEntry (infoPtr); } while (infoPtr->stackPtr->procLevel > procLevel); } /* * If this command is a procedure or if all commands are * being traced, handle the entry. */ isProc = (TclFindProc (iPtr, fullCmdName) != NULL); if (infoPtr->commandMode || isProc) { UpdateTOSTimes (infoPtr); if (isProc) { PushEntry (infoPtr, fullCmdName, TRUE, procLevel + 1, scopeLevel + 1, infoPtr->evalLevel); } else { PushEntry (infoPtr, fullCmdName, FALSE, procLevel, scopeLevel, infoPtr->evalLevel); } } /* * Leaving profiler, must get time again when we reenter. */ infoPtr->updatedTimes = FALSE; Tcl_DecrRefCount (fullCmdNameObj); } } else { /* leave/leavestep */ int done = 0; opStr = Tcl_GetStringFromObj (objv[4], NULL); done = (strcmp ("leave", opStr) == 0); if (!done && (strcmp ("leavestep", opStr) != 0)) return TCL_OK; /* Ignore unknown ops */ Tcl_ListObjGetElements (NULL, objv[1], &cmdObjc, &cmdObjv); if (cmdObjc > 0) { cmdName = Tcl_GetStringFromObj (cmdObjv[0], NULL); cmd = Tcl_FindCommand (interp, cmdName, NULL, 0); if (cmd == NULL) panic (PROF_PANIC, 1001); fullCmdNameObj = Tcl_NewObj (); Tcl_GetCommandFullName (interp, cmd, fullCmdNameObj); fullCmdName = Tcl_GetStringFromObj (fullCmdNameObj, NULL); /* * Pop the entry, recording the information. */ if (infoPtr->commandMode || (TclFindProc (iPtr, fullCmdName) != NULL)) { UpdateTOSTimes (infoPtr); if (infoPtr->stackPtr->prevEntryPtr != NULL) PopEntry (infoPtr); } /* * Leaving profiler, must get time again when we reenter. */ infoPtr->updatedTimes = FALSE; Tcl_DecrRefCount (fullCmdNameObj); } /* * If op was "leave", turn off the execution trace now. */ if (done) { Tcl_Obj *listPtr, *namePtr; int result; listPtr = Tcl_NewObj (); Tcl_ListObjAppendElement (NULL, listPtr, Tcl_NewStringObj ("::trace", -1)); Tcl_ListObjAppendElement (NULL, listPtr, Tcl_NewStringObj ("remove", -1)); Tcl_ListObjAppendElement (NULL, listPtr, Tcl_NewStringObj ("execution", -1)); namePtr = Tcl_NewObj (); Tcl_GetCommandFullName (infoPtr->interp, infoPtr->currentCmd, namePtr); Tcl_ListObjAppendElement (NULL, listPtr, namePtr); Tcl_ListObjAppendElement (NULL, listPtr, Tcl_NewStringObj ("enter leave enterstep leavestep", -1)); namePtr = Tcl_NewObj (); Tcl_GetCommandFullName (infoPtr->interp, infoPtr->profExecCmd, namePtr); Tcl_ListObjAppendElement (NULL, listPtr, namePtr); result = Tcl_EvalObjEx (interp, listPtr, TCL_EVAL_GLOBAL); infoPtr->currentCmd = NULL; } } return TCL_OK; } /*----------------------------------------------------------------------------- * CleanDataTable -- * Clean up the hash data table, releasing all resources and setting it * to the empty state. * * Parameters: |
︙ | ︙ | |||
797 798 799 800 801 802 803 | InitializeProcStack (infoPtr, framePtr) profInfo_t *infoPtr; CallFrame *framePtr; { if (framePtr == NULL || framePtr->objv == NULL) return; InitializeProcStack (infoPtr, framePtr->callerPtr); | < | 618 619 620 621 622 623 624 625 626 627 628 629 630 631 | InitializeProcStack (infoPtr, framePtr) profInfo_t *infoPtr; CallFrame *framePtr; { if (framePtr == NULL || framePtr->objv == NULL) return; InitializeProcStack (infoPtr, framePtr->callerPtr); PushEntry (infoPtr, Tcl_GetStringFromObj (framePtr->objv [0], NULL), TRUE, infoPtr->stackPtr->procLevel + 1, framePtr->level, UNKNOWN_LEVEL); |
︙ | ︙ | |||
831 832 833 834 835 836 837 | { Interp *iPtr = (Interp *) infoPtr->interp; int scopeLevel; profEntry_t *scanPtr; CleanDataTable (infoPtr); | < | < < < < < < < | 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 | { Interp *iPtr = (Interp *) infoPtr->interp; int scopeLevel; profEntry_t *scanPtr; CleanDataTable (infoPtr); infoPtr->traceHandle = Tcl_CreateObjTrace (infoPtr->interp, 0, TCL_ALLOW_INLINE_COMPILATION, (Tcl_CmdObjTraceProc *) ProfObjTraceRoutine, (ClientData) infoPtr, NULL); infoPtr->commandMode = commandMode; infoPtr->evalMode = evalMode; infoPtr->realTime = 0; infoPtr->cpuTime = 0; infoPtr->prevRealTime = 0; infoPtr->prevCpuTime = 0; infoPtr->updatedTimes = FALSE; |
︙ | ︙ | |||
1103 1104 1105 1106 1107 1108 1109 | infoPtr = (profInfo_t *) ckalloc (sizeof (profInfo_t)); infoPtr->interp = interp; infoPtr->traceHandle = NULL; infoPtr->commandMode = FALSE; infoPtr->evalMode = FALSE; | | < < < < < < < | | | | | > > > > > | 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 | infoPtr = (profInfo_t *) ckalloc (sizeof (profInfo_t)); infoPtr->interp = interp; infoPtr->traceHandle = NULL; infoPtr->commandMode = FALSE; infoPtr->evalMode = FALSE; infoPtr->currentCmd = NULL; infoPtr->evalLevel = UNKNOWN_LEVEL; infoPtr->realTime = 0; infoPtr->cpuTime = 0; infoPtr->prevRealTime = 0; infoPtr->prevCpuTime = 0; infoPtr->updatedTimes = FALSE; infoPtr->stackPtr = NULL; infoPtr->stackSize = 0; infoPtr->scopeChainPtr = NULL; Tcl_InitHashTable (&infoPtr->profDataTable, TCL_STRING_KEYS); Tcl_CallWhenDeleted (interp, ProfMonCleanUp, (ClientData) infoPtr); infoPtr->profileCmd = Tcl_CreateObjCommand (interp, "profile", TclX_ProfileObjCmd, (ClientData) infoPtr, (Tcl_CmdDeleteProc *) NULL); infoPtr->profExecCmd = Tcl_CreateObjCommand (interp, "profexec", ProfExecObjCmd, (ClientData) infoPtr, (Tcl_CmdDeleteProc *) NULL); } |
Changes to jni/tclx/generic/tclXstring.c.
︙ | ︙ | |||
672 673 674 675 676 677 678 | Tcl_Obj *classObj, *stringObj; int number; char charBuf[TCL_UTF_MAX]; Tcl_UniChar uniChar; #define IS_8BIT_UNICHAR(c) (c <= 255) | < < < < | 672 673 674 675 676 677 678 679 680 681 682 683 684 685 | Tcl_Obj *classObj, *stringObj; int number; char charBuf[TCL_UTF_MAX]; Tcl_UniChar uniChar; #define IS_8BIT_UNICHAR(c) (c <= 255) /*FIX: Split into multiple procs */ /*FIX: Should use UtfNext to walk string */ if (objc < 3) { goto wrongNumArgs; } |
︙ | ︙ |
Changes to jni/tclx/tests/profile.test.
︙ | ︙ | |||
141 142 143 144 145 146 147 | set sumData {} foreach stack [array names profData] { set newStack [FilterProfStack $stack] if {![lempty $newStack]} { lappend sumData [list $newStack [lindex $profData($stack) 0]] } } | > | > > > > | 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 | set sumData {} foreach stack [array names profData] { set newStack [FilterProfStack $stack] if {![lempty $newStack]} { lappend sumData [list $newStack [lindex $profData($stack) 0]] } } if {$::tcl_version < 8.5} { return [lsort $sumData] } return [lsort [listRemovePrecomp {*}$sumData]] } proc listRemovePrecomp {args} { if {$::tcl_version < 8.4} { return $args } # This removes commands that are now compiled, and thus don't # appear in the profile results set res {} foreach arg $args { if {[regexp {^::(list|string|return)$} [lindex $arg 0 0]]} continue # 8.6 if {[regexp {^::(expr|error|incr|set|list|catch|for|if|tcl::)} [lindex $arg 0 0]]} continue # 8.6, remove unwanted stuff in between, very ugly regsub -all -- {( ::if| ::catch| ::error)} $arg "" arg lappend res $arg } return $res } # # Test of normal procedure calls. |
︙ | ︙ | |||
294 295 296 297 298 299 300 301 302 303 304 305 | } [list {<global> 1} {<global> 1} \ {{::ProcA4 <global>} 1} \ {{::ProcB4 ::ProcA4 <global>} 1} \ {{::ProcC4 ::ProcB4 ::ProcA4 <global>} 1} \ {{::ProcD4 ::ProcC4 ::ProcB4 ::ProcA4 <global>} 1} \ {{::ProcE4 ::ProcB4 ::ProcA4 <global>} 1}] test profile-4.2 {profile count tests} { profile -commands on ProcA4 profile off profData SumCntData profData | > > > > > > > > > > > > > < | < < < < < < < | < < < < < < | 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 | } [list {<global> 1} {<global> 1} \ {{::ProcA4 <global>} 1} \ {{::ProcB4 ::ProcA4 <global>} 1} \ {{::ProcC4 ::ProcB4 ::ProcA4 <global>} 1} \ {{::ProcD4 ::ProcC4 ::ProcB4 ::ProcA4 <global>} 1} \ {{::ProcE4 ::ProcB4 ::ProcA4 <global>} 1}] set anticipate [list {<global> 1} {<global> 1} \ {{::ProcA4 <global>} 1} \ {{::ProcB4 ::ProcA4 <global>} 1} \ {{::ProcC4 ::ProcB4 ::ProcA4 <global>} 1} \ {{::ProcD4 ::ProcC4 ::ProcB4 ::ProcA4 <global>} 1} \ {{::ProcE4 ::ProcB4 ::ProcA4 <global>} 1}] if {$tcl_version < 8.6} { # Error-handler is not subject to profiling under 8.6 lappend anticipate \ {{::error ::ProcD4 ::ProcC4 ::ProcB4 ::ProcA4 <global>} 1} } lappend anticipate {{::profile <global>} 1} test profile-4.2 {profile count tests} { profile -commands on ProcA4 profile off profData SumCntData profData } $anticipate test profile-4.3 {profile count tests} { profile -eval on ProcA4 profile off profData SumCntData profData } [list {<global> 1} {<global> 1} \ {{::ProcA4 <global>} 1} \ {{::ProcB4 ::ProcA4 <global>} 1} \ {{::ProcC4 ::ProcB4 ::ProcA4 <global>} 1} \ {{::ProcD4 ::ProcC4 ::ProcB4 ::ProcA4 <global>} 1} \ {{::ProcE4 ::ProcB4 ::ProcA4 <global>} 1}] test profile-4.4 {profile count tests} { profile -commands -eval on ProcA4 profile off profData SumCntData profData } $anticipate # # Test of a command that calls procedures from different levels. # set ::tcltest::testConstraints(tclx_test_eval) \ [llength [info commands tclx_test_eval]] |
︙ | ︙ | |||
543 544 545 546 547 548 549 | proc EatTime {amount} { set start [lindex [times] 0] set end [expr $start+$amount] set cnt 0 while {[lindex [times] 0] < $end} { format %d 100 ;# kind of slow command. incr cnt | | | 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 | proc EatTime {amount} { set start [lindex [times] 0] set end [expr $start+$amount] set cnt 0 while {[lindex [times] 0] < $end} { format %d 100 ;# kind of slow command. incr cnt if {($cnt > 100000) && ([lindex [times] 0] == $start)} { error "User CPU time does not appear to be accumulating" } } } proc ProcA10 {} {ProcB10;ProcC10;ProcD10} proc ProcB10 {} {EatTime 1} |
︙ | ︙ |