DTrace - (ab)using aggregations to expose other data

One restriction when using the Java DTrace API is that the only data type that can be collected on demand by a DTrace client is the aggregation.

This is a restriction since there's no way of directly setting the values held in an aggregation - as implied by its name, an aggregation accumulates historical knowledge about a value, so it's not very useful for holding instantaneous values (such as those available from the kernel), or calculated values (such as a calculated moving average).

My initial thought on how to script to put my current data into an aggregation was to simply clear() the aggregation and then add() the current data... thereby ensuring that only my latest sample was taken into account. Unfortunately, this doesn't work at all, the result always stays empty (or at zero). This nonintuitive (to me, anyway) result is because the clear() function is execute asynchronously in the user context, which is after the add() function is called.

I just thought I'd blog about a simple way of working around this limitation should you need to ... just to be able to get data out of dtrace.

Rather than resetting the aggregation and then adding in the new values (which poses a problem anyway, since there's a window where the aggregation doesn't hold a valid value), one can update the value of an aggregation using deltas.

Here's a tiny code snippit to demonstrate this - instead of adding on the new sample to an aggregation, one adds on the delta between the new sample and the previous sample, thereby atomically updating the aggregation to hold the value of the new sample:

long last_result = 0;
profile:::tick-1sec {
    this_result = `freemem;

    @result = sum(this_result - last_result);
    last_result = this_result;

    printa(@result); 
}

 

... as you can see rather intuitively, the value stored in @result will always be that of the current value. Nothing rocket-science about this, it's just simple maths, but this tip can help get data out of the Java DTrace API .

Comments:

Post a Comment:
Comments are closed for this entry.
About

nickstephen

Search

Archives
« April 2014
SunMonTueWedThuFriSat
  
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
   
       
Today