Java System.nanoTime is very slow. Is it possible to implement a high-performance java profiler?

I did a test and found that the overhead of calling the System.nanoTime () function was at least 500 ns on my machine.

It seemed very difficult to have a high-performance java profiler. For enterprise software, suppose a function takes about 350 seconds and has 12.5 billion method calls. Therefore, the number of calls in System.nanoTime (): 12 500 000 000 * 2 = 25 000 000 000 (one for the start time stamp, one for the time stamp) And the total overhead of System.nanoTime: 500 ns * 25 000 000 000 = 500 * 25000 s = 12500000 s.

Note: all data is from the real case.

Best way to get a timestamp?

+5
source share
5 answers

I spent 10 years on commercial profiles of Java professionals, for use in both development and production.

The short answer is yes, you are right. You cannot pull it out. And even if you could, put anything other than trivial tools in a method that is often called:

  • Change the way JIT is handled in such a way
  • distorts your performance indicators in hard to predict (but usually not useful in terms of performance tuning).

    ( , , , JIT , , CPU , L1 .. ..)

(, , " " ?). , , API JDBC, .

Java ( Java, , , ,...), . , , , 7 - .

"" - . JVMTI , , . , (, , , , -- ) - - .

+19

: , System.nanoTime() , , .

: , (), , System.nanoTimes() . , , 35 000 , Systme.nanoTime() (12500000s/350s = ~ 35 000).

. , Nano. , . , , - , , ( ).

, ?

+5

? Windows, Linux Solaris? ?

System.nanoTime() - , . , :

Windows QueryPerformanceCounter(), Linux gettimeofday(), Solaris gethrtime().

- , , Solaris gethrtime() - . , , UltraSPARC 300 " ". , 500ns . DTrace, , .

, . 12,5- ( ), ?

+2
source

If you want to profile, use a profiler that will connect to the JVM quasi-magically using the JVMTI without taking nanoTime () calls into the code.

If you want to use microbenchmarking, just loop many times so that when dividing the result by this number, the nanoTime () overhead will all disappear.

+1
source

And the System.nanoTime overhead as a whole is: 500 ns * 25,000,000,000 = 500 * 25,000 s = 12,500,000.

In my arithmetic:

500 nS * 25,000,000,000 =  12500 S.

Because:

(500 * 10^-9) * (25 * 10^9) = 500 * 25
0
source

All Articles