Tk Library Source Code

View Ticket
Login
Ticket UUID: 1193581
Title: Time consumed in C/C++ calbacks is not collected
Type: Bug Version: None
Submitter: mcocosila Created on: 2005-05-02 03:20:04
Subsystem: profiler Assigned To: ericm
Priority: 5 Medium Severity:
Status: Closed Last Modified: 2005-05-03 07:06:19
Resolution: Works For Me Closed By: ericm
    Closed on: 2005-05-02 17:43:50
Description:
I tried to use the profiler included in the tcllib. It
seems to be a very useful thing. Still, I would like to
present my current situation and the related questions.

We have a C/C++ server with tcl interpreters embedded.
We developed a specific application with a part of the
business rules implemented in tcl.
Right now we want to improve the overall performance of
the application (to have acceptable execution times for
specific tasks - this means time consumed in C/C++ plus
time consumed in tcl).

As I noticed from using the profiler in the tcl
standard library, it seems that only time spent in pure
tcl code in collected. To get a good feeling on the
overall performance we need to measure both time
components: the one spent in C and the one spent in
pure tcl.

Let's consider a simple app skeleton as an example.

Let Tcl1 be a higher level tcl proc.
Let Tcl11 be a lower level tcl proc that is called by Tcl1.
Let C1 be a C/C++ callback implemented in C and exposed
to external tcl code. Tcl11 has minimal pure tcl inside
and invokes C1.

Running the profiler, I noticed that the time reported
for Tcl11 is 0. The fact the time is 0 makes me think
of two concurrent possible reasons: (1) the pure tcl
code in Tcl11 runs too fast and is approximated as 0
and (2) the C1 is not profiled. This is not too useful
and I will show you a simple scenario for this.

Let's consider that Tcl11 invokes C1 (possible a costly
calculating operation) hundreds times instead of only
once and then caching the result.
By not collecting the time consumed in C1, I miss an
opportunity to improve the Tcl11 proc. I hope you got
my point.

In conclusion, please tell me if there is some option
to configure the existing tcl profiler to do what I
need. Or, maybe, you can direct me somehow to reach my
goal. Anyway, it seems a general need for others to enjoy.

Thank you very much for your time and consideration.
User Comments: nobody added on 2005-05-03 07:06:19:
Logged In: NO 

Thank you very much Eric for your careful handling. I will
work more with the profiler and let you know if I meet some
special behaviors sometime.

ericm added on 2005-05-03 00:43:50:
Logged In: YES 
user_id=86324

I think you must be misunderstanding something.  The
profiler makes no distinction between time spent "executing
Tcl code" versus "executing C code".  In fact, practically
speaking there is no way that it could do so.  I offer the
following counter example as proof:

  package require profiler 1.6
  ::profiler::init

  proc testProf {} {
      after 3000
  }

  testProf
  testProf
  testProf

  puts [::profiler::print ::testProf]


Here I have used the [after] command as a stand-in for your
[C1] command.  Keep in mind that just like [C1], [after] is
implemented in C code, and by definition, [after] takes some
significant amount of time to execute.  When I run this in
my interpreter, I get the following output:

Profiling information for ::testProf
============================================================
            Total calls:  3
    Caller distribution:
  GLOBAL:  3
           Compile time:  3001000
          Total runtime:  9021000
        Average runtime:  3007000
          Runtime StDev:  5292
         Runtime cov(%):  0.2
  Total descendant time:  0
Average descendant time:  0
Descendants:
  none

You can see that the time spent in the [after] command
itself is clearly accounted for and billed to the testProf
procedure.

I happened to have my own Tcl extension here that implements
several new commands, and I repeated this test calling those
commands from testProf instead of [after], just to be sure
that the profiler was not somehow treating built-in commands
specially.  I obtained similar results with this experiment
as well.