@@ -34,8 +34,8 @@ public class MicroJitterSampler {
3434 };
3535 private static final double UTIL = Double .parseDouble (System .getProperty ("util" , "50" ));
3636 private static final boolean BUSYWAIT = Boolean .parseBoolean (System .getProperty ("busywait" , "false" ));
37+ private static final int CPU = Integer .parseInt (System .getProperty ("cpu" , "-1" ));
3738
38- // static final int CPU = Integer.getInteger("cpu", 0);
3939 private final int [] count = new int [DELAY .length ];
4040 private long totalTime = 0 ;
4141
@@ -50,25 +50,46 @@ private static void pause() throws InterruptedException
5050
5151 }
5252 public static void main (String ... ignored ) throws InterruptedException {
53- // AffinityLock al = AffinityLock.acquireLock ();
54-
55- // warmup.
56- new MicroJitterSampler (). sample ( 1000 * 1000 * 1000 );
57-
58- MicroJitterSampler microJitterSampler = new MicroJitterSampler ();
59- while (! Thread . currentThread (). isInterrupted ()) {
60- if ( UTIL >= 100 ) {
61- microJitterSampler . sample ( 30L * 1000 * 1000 * 1000 );
62- } else {
63- long sampleLength = ( long ) (( 1 / ( 1 - UTIL / 100 ) - 1 ) * 1000 * 1000 );
64- for ( int i = 0 ; i < 30 * 1000 ; i += 2 ) {
65- microJitterSampler . sample ( sampleLength );
66- //noinspection BusyWait
67- pause ();
68- }
53+ MicroJitterSampler sampler = new MicroJitterSampler ();
54+
55+ Thread t = new Thread ( sampler :: run );
56+ t . start ( );
57+ t . join ();
58+ }
59+
60+ private void once () throws InterruptedException {
61+ if ( UTIL >= 100 ) {
62+ sample ( 30L * 1000 * 1000 * 1000 );
63+ } else {
64+ long sampleLength = ( long ) (( 1 / ( 1 - UTIL / 100 ) - 1 ) * 1000 * 1000 );
65+ for ( int i = 0 ; i < 30 * 1000 ; i += 2 ) {
66+ sample ( sampleLength );
67+ //noinspection BusyWait
68+ pause ();
6969 }
70+ }
71+ }
72+
73+ public void run () {
74+ if (CPU != -1 )
75+ Affinity .setAffinity (CPU );
76+
77+ try {
78+ boolean first = true ;
79+ System .out .println ("Warming up..." );
80+ while (!Thread .currentThread ().isInterrupted ()) {
81+ once ();
82+
83+ if (first ) {
84+ reset ();
85+ first = false ;
86+ System .out .println ("Warmup complete. Running jitter tests..." );
87+ continue ;
88+ }
7089
71- microJitterSampler .print (System .out );
90+ print (System .out );
91+ }
92+ } catch (InterruptedException e ) {
7293 }
7394 }
7495
@@ -79,6 +100,12 @@ private static String asString(long timeNS) {
79100 timeNS / 1000000000 + "sec" ;
80101 }
81102
103+ void reset () {
104+ for (int i =0 ; i <DELAY .length ; ++i )
105+ count [i ] = 0 ;
106+ totalTime = 0 ;
107+ }
108+
82109 void sample (long intervalNS ) {
83110 long prev = System .nanoTime ();
84111 long end = prev + intervalNS ;
0 commit comments