Example usage for java.lang System nanoTime

List of usage examples for java.lang System nanoTime

Introduction

In this page you can find the example usage for java.lang System nanoTime.

Prototype

@HotSpotIntrinsicCandidate
public static native long nanoTime();

Source Link

Document

Returns the current value of the running Java Virtual Machine's high-resolution time source, in nanoseconds.

Usage

From source file:de.unisb.cs.st.javaslicer.slicing.Slicer.java

public static void main(String[] args) throws InterruptedException {
    Options options = createOptions();//from  w w w . j a  va2  s.  co  m
    CommandLineParser parser = new GnuParser();
    CommandLine cmdLine;

    try {
        cmdLine = parser.parse(options, args, true);
    } catch (ParseException e) {
        System.err.println("Error parsing the command line arguments: " + e.getMessage());
        return;
    }

    if (cmdLine.hasOption('h')) {
        printHelp(options, System.out);
        System.exit(0);
    }

    String[] additionalArgs = cmdLine.getArgs();
    if (additionalArgs.length != 2) {
        printHelp(options, System.err);
        System.exit(-1);
    }
    // ?? 1. ? 2.?
    File traceFile = new File(additionalArgs[0]);
    String slicingCriterionString = additionalArgs[1];

    Long threadId = null;
    if (cmdLine.hasOption('t')) { // the interesting thread id for slicing 
        try {
            threadId = Long.parseLong(cmdLine.getOptionValue('t'));
        } catch (NumberFormatException e) {
            System.err.println("Illegal thread id: " + cmdLine.getOptionValue('t'));
            System.exit(-1);
        }
    }

    TraceResult trace;
    try {
        trace = TraceResult.readFrom(traceFile);
    } catch (IOException e) {
        System.err.format("Could not read the trace file \"%s\": %s%n", traceFile, e);
        System.exit(-1);
        return;
    }

    List<SlicingCriterion> sc = null; // a list contains the instruction's info corresponds to the slicing criterion
    //slicingCriterionString get from additionalArgs[1]
    try {
        sc = StaticSlicingCriterion.parseAll(slicingCriterionString, trace.getReadClasses());
    } catch (IllegalArgumentException e) {
        System.err.println("Error parsing slicing criterion: " + e.getMessage());
        System.exit(-1);
        return;
    }

    List<ThreadId> threads = trace.getThreads(); // the threads that generate the traces
    if (threads.size() == 0) {
        System.err.println("The trace file contains no tracing information.");
        System.exit(-1);
    }

    // threadID is used to mark the interesting thread
    ThreadId tracing = null;
    for (ThreadId t : threads) {
        if (threadId == null) {
            if ("main".equals(t.getThreadName())
                    && (tracing == null || t.getJavaThreadId() < tracing.getJavaThreadId()))
                tracing = t;
        } else if (t.getJavaThreadId() == threadId.longValue()) {
            tracing = t;
        }
    }

    if (tracing == null) {
        System.err.println(threadId == null ? "Couldn't find the main thread."
                : "The thread you specified was not found.");
        System.exit(-1);
        return;
    }

    long startTime = System.nanoTime();
    Slicer slicer = new Slicer(trace);
    if (cmdLine.hasOption("progress")) // the parameter process indicates that we need to monitor the process of slicing
        slicer.addProgressMonitor(new ConsoleProgressMonitor());
    boolean multithreaded;
    if (cmdLine.hasOption("multithreaded")) {
        String multithreadedStr = cmdLine.getOptionValue("multithreaded");
        multithreaded = ("1".equals(multithreadedStr) || "true".equals(multithreadedStr));
    } else {
        multithreaded = Runtime.getRuntime().availableProcessors() > 1;
    }

    boolean warnUntracedMethods = cmdLine.hasOption("warn-untraced"); // give some warns when encounters untraced functions

    //sliceInstructionCollector implements the interface slice visitor, which travel the dependence graph
    SliceInstructionsCollector collector = new SliceInstructionsCollector(); // the collector is used to collect the instructions in the dependence graph according to the slice criterion. 
    slicer.addSliceVisitor(collector);
    // zhushi by yhb

    if (warnUntracedMethods)
        slicer.addUntracedCallVisitor(new PrintUniqueUntracedMethods()); // the user need the untraced function info, so add untraced call visitor

    slicer.process(tracing, sc, multithreaded); //----------------------the key process of slicing!!!
    Set<InstructionInstance> slice = collector.getDynamicSlice(); // return the slice result from the collector
    long endTime = System.nanoTime();

    Instruction[] sliceArray = slice.toArray(new Instruction[slice.size()]); // convert the set to array
    Arrays.sort(sliceArray); // in order to ensure the sequence of dynamic execution

    // show the slicing result
    System.out.println("The dynamic slice for criterion " + sc + ":");
    for (Instruction insn : sliceArray) {
        System.out.format((Locale) null, "%s.%s:%d %s%n", insn.getMethod().getReadClass().getName(),
                insn.getMethod().getName(), insn.getLineNumber(), insn.toString());
    }
    System.out.format((Locale) null, "%nSlice consists of %d bytecode instructions.%n", sliceArray.length);
    System.out.format((Locale) null, "Computation took %.2f seconds.%n", 1e-9 * (endTime - startTime));
}

From source file:Main.java

public static int getTimeId() {
    int x = (int) System.nanoTime();
    x ^= (x << 21);//from   w  w w .  java 2  s  . com
    x ^= (x >>> 35);
    x ^= (x << 4);
    if (x < 0)
        x = 0 - x;
    return x;
}

From source file:Main.java

public static long nanoTime() {
    return System.nanoTime();
}

From source file:Main.java

public static long getTime() {
    return System.nanoTime() - TIME_OFFSET;
}

From source file:Main.java

public static long durationInMillis(long startNano) {
    final long endNano = System.nanoTime();
    return (endNano - startNano) / 1000000;
}

From source file:Main.java

public static long compute(long timeInNanosecond) {
    final long start = System.nanoTime();
    long seed = start;
    while (System.nanoTime() - start < timeInNanosecond) {
        seed = (long) Math.sqrt(3.15);
    }/*from ww w. j a  v  a2s.  c  o  m*/
    return seed;

    //            try {
    //                TimeUnit.NANOSECONDS.sleep(timeInNanosecond);
    //            } catch (InterruptedException e) {
    //                e.printStackTrace();
    //            }
    //                return 0;
}

From source file:Main.java

public static synchronized String nextUniqueId() {
    long current = System.nanoTime();
    if (current == previousId) {
        try {// w ww  .  j ava2 s.com
            Thread.sleep(0, 1);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        current = System.nanoTime();
    }
    previousId = current;
    return String.valueOf(current);
}

From source file:Main.java

public static long millitime() {
    return TimeUnit.MILLISECONDS.convert(System.nanoTime(), TimeUnit.NANOSECONDS);
}

From source file:Main.java

public static long contains(List list) {
    long start, stop, result = 0;
    for (int i = 0; i < 100; i++) {
        start = System.nanoTime();
        list.contains(list.size() - i);//from   w w  w  . j a v a2  s. co  m
        stop = System.nanoTime();
        result += stop - start;
    }
    return result / 100;
}

From source file:Main.java

public static final void sleep(long pTime, TimeUnit pTimeUnit) {
    final long lStart = System.nanoTime();
    long lDeadlineInNanos = lStart + pTimeUnit.toNanos(pTime);

    boolean lSleepTimeBelowMillisecond = pTimeUnit.toMillis(pTime) == 0;

    long lNanoTime;
    while ((lNanoTime = System.nanoTime()) < lDeadlineInNanos) {

        try {/*  www . j  a  v  a 2  s  . c  o m*/
            if (lSleepTimeBelowMillisecond) {
                long lTimeToWaitInNanos = 3 * (lDeadlineInNanos - lNanoTime) / 4;
                if (lTimeToWaitInNanos > 0)
                    Thread.sleep(0, (int) lTimeToWaitInNanos);
            } else {
                long lTimeToWaitInNanos = 3 * (lDeadlineInNanos - lNanoTime) / 4;
                if (lTimeToWaitInNanos > 0) {
                    long lTimeToWaitInMillis = TimeUnit.NANOSECONDS.toMillis(lTimeToWaitInNanos);

                    Thread.sleep(lTimeToWaitInMillis, (int) (lTimeToWaitInNanos % 1000000L));
                }
            }
        } catch (InterruptedException e) {
        }
    }
}