3.3 Using Aggregations with User-Space Applications

You can use aggregations to collect statistical data for user-space applications. For example, the following D program, aggfunc.d, aggregates the number of function calls across all PHP instances as well as by individual instances:

Example 3.2 aggfunc.d: Aggregate counts for a PHP program

#!/usr/sbin/dtrace -Zqs

/* aggfunc.d -- Aggregate counts for a PHP program */

php*:::function-entry
{
  @bypid[pid] = count();
  @byfunc[copyinstr(arg0)] = count();
  @bypidandfunc[pid,copyinstr(arg0)] = count();
}

END
{
  printf("Counts by pid\n");
  printa("  %-40d %@d\n", @bypid);
  printf("\nCounts by function\n");
  printa("  %-40s %@d\n", @byfunc);
  printf("\nCounts by pid and function\n");
  printa("  %-9d %-30s %@d\n", @bypidandfunc);
}

The -Z option allows you to start dtrace before any instances of php are running and no matching probes are available.

Run aggfunc.d in one window and start several instances of php func.php from other windows. Allow aggfunc.d to run for several seconds before typing Ctrl-C. The program returns output such as the following:

# ./aggfunc.d
^C
Counts by pid
  5646                                     15
  5287                                     28
  5645                                     36

Counts by function
  initialize                               15
  heads                                    23
  tails                                    41

Counts by pid and function
  5646      heads                          4
  5646      initialize                     4
  5645      initialize                     5
  5287      initialize                     6
  5287      heads                          7
  5646      tails                          7
  5645      heads                          12
  5287      tails                          15
  5645      tails                          19