001    /*
002    Galois, a framework to exploit amorphous data-parallelism in irregular
003    programs.
004    
005    Copyright (C) 2010, The University of Texas at Austin. All rights reserved.
006    UNIVERSITY EXPRESSLY DISCLAIMS ANY AND ALL WARRANTIES CONCERNING THIS SOFTWARE
007    AND DOCUMENTATION, INCLUDING ANY WARRANTIES OF MERCHANTABILITY, FITNESS FOR ANY
008    PARTICULAR PURPOSE, NON-INFRINGEMENT AND WARRANTIES OF PERFORMANCE, AND ANY
009    WARRANTY THAT MIGHT OTHERWISE ARISE FROM COURSE OF DEALING OR USAGE OF TRADE.
010    NO WARRANTY IS EITHER EXPRESS OR IMPLIED WITH RESPECT TO THE USE OF THE
011    SOFTWARE OR DOCUMENTATION. Under no circumstances shall University be liable
012    for incidental, special, indirect, direct or consequential damages or loss of
013    profits, interruption of business, or related expenses which may arise from use
014    of Software or Documentation, including but not limited to those resulting from
015    defects in Software and/or Documentation, or loss or inaccuracy of data of any
016    kind.
017    
018    File: CPUFunctions.java 
019    
020    */
021    
022    
023    
024    package util;
025    
026    /**
027     * JNI library to certain CPU instructions and functions
028     * 
029     *
030     */
031    public class CPUFunctions {
032      private static boolean loaded;
033      private static final String lib = "util_CPUFunctions";
034    
035      static {
036        try {
037          System.loadLibrary(lib);
038          loaded = true;
039        } catch (Error e) {
040          System.err.println(String.format("Could not load library: %s", e.toString()));
041        }
042      }
043    
044      /**
045       * @return true if the library been successfully loaded
046       */
047      public static boolean isLoaded() {
048        return loaded;
049      }
050    
051      private static native int _getCpuId();
052    
053      /**
054       * @return  the apicid of the currently executing thread  or -1 if the JNI library was
055       *   not loaded
056       */
057      public static int getCpuId() {
058        if (loaded)
059          return _getCpuId();
060        else
061          return -1;
062      }
063    
064      public static void main(String[] args) {
065        System.out.println(getCpuId());
066      }
067    }