1 /*
   2  * Copyright 2009 D.E. Shaw.  All Rights Reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  *
  23  */
  24 
  25 /**
  26  * @test
  27  * @bug 6863420
  28  * @summary os::javaTimeNanos() go backward on Solaris x86
  29  *
  30  * Notice the internal timeout in timeout thread Test.TOT.
  31  * @run main/othervm/timeout=300 Test
  32  */
  33 
  34 public class Test {
  35 
  36     static final int INTERNAL_TIMEOUT=240;
  37     static class TOT extends Thread {
  38        public void run() {
  39            try {
  40                Thread.sleep(INTERNAL_TIMEOUT*1000);
  41            } catch (InterruptedException ex) {
  42            }
  43            done = true;
  44        }
  45     }
  46 
  47     static long value = 0;
  48     static boolean got_backward_time = false;
  49     static volatile boolean done = false;
  50 
  51     public static void main(String args[]) {
  52         final int count = 100000;
  53 
  54         TOT tot = new TOT();
  55         tot.setDaemon(true);
  56         tot.start();
  57 
  58         for (int numThreads = 1; !done && numThreads <= 32; numThreads++) {
  59             final int numRuns = 1;
  60             for (int t=1; t <= numRuns; t++) {
  61                 final int curRun = t;
  62 
  63                 System.out.println("Spawning " + numThreads + " threads");
  64                 final Thread threads[] = new Thread[numThreads];
  65                 for (int i = 0; i < threads.length; i++) {
  66                     Runnable thread =
  67                         new Runnable() {
  68                             public void run() {
  69                                 for (long l = 0; !done && l < 100000; l++) {
  70                                     final long start = System.nanoTime();
  71                                     if (value == 12345678) {
  72                                         System.out.println("Wow!");
  73                                     }
  74                                     final long end = System.nanoTime();
  75                                     final long time = end - start;
  76                                     value += time;
  77                                     if (time < 0) {
  78                                         System.out.println(
  79                                             "Backwards: " +
  80                                             "start=" + start + " " +
  81                                             "end=" + end + " " +
  82                                             "time= " + time
  83                                         );
  84                                         got_backward_time = true;
  85                                     }
  86                                 }
  87                             }
  88                         };
  89                     threads[i] = new Thread(thread, "Thread" + i);
  90                 }
  91                 for (int i = 0; i < threads.length; i++) {
  92                     threads[i].start();
  93                 }
  94                 for (int i = 0; i < threads.length; i++) {
  95                     try {
  96                         threads[i].join();
  97                     }
  98                     catch (InterruptedException e) {
  99                         continue;
 100                     }
 101                 }
 102             }
 103         }
 104 
 105         if (got_backward_time) {
 106             System.exit(97);
 107         }
 108     }
 109 }