Changeset c4bbcc46


Ignore:
Timestamp:
May 25, 2011 1:48:45 PM (9 years ago)
Author:
zzz <zzz@…>
Branches:
master
Children:
d201a29
Parents:
82a0ac1
Message:
  • CPUID: Load 64-bit libcpuid if available
File:
1 edited

Legend:

Unmodified
Added
Removed
  • core/java/src/freenet/support/CPUInformation/CPUID.java

    r82a0ac1 rc4bbcc46  
    4646                                    I2PAppContext.getGlobalContext().isRouterContext();
    4747
    48     //.matches() is a java 1.4+ addition, using a simplified version for 1.3+
    49     //private static final boolean isX86 = System.getProperty("os.arch").toLowerCase().matches("i?[x0-9]86(_64)?");
    50     private static final boolean isX86 = (-1 != System.getProperty("os.arch").indexOf("86"));
     48    private static final boolean isX86 = System.getProperty("os.arch").contains("86");
    5149    private static final String libPrefix = (System.getProperty("os.name").startsWith("Win") ? "" : "lib");
    5250    private static final String libSuffix = (System.getProperty("os.name").startsWith("Win") ? ".dll" : ".so");
    53    
     51    private static final boolean isWindows = System.getProperty("os.name").toLowerCase().contains("windows");
     52    private static final boolean isLinux = System.getProperty("os.name").toLowerCase().contains("linux");
     53    private static final boolean isFreebsd = System.getProperty("os.name").toLowerCase().contains("freebsd");
     54   
     55    /**
     56     * This isn't always correct.
     57     * http://stackoverflow.com/questions/807263/how-do-i-detect-which-kind-of-jre-is-installed-32bit-vs-64bit
     58     * http://mark.koli.ch/2009/10/javas-osarch-system-property-is-the-bitness-of-the-jre-not-the-operating-system.html
     59     * http://mark.koli.ch/2009/10/reliably-checking-os-bitness-32-or-64-bit-on-windows-with-a-tiny-c-app.html
     60     * sun.arch.data.model not on all JVMs
     61     * sun.arch.data.model == 64 => 64 bit processor
     62     * sun.arch.data.model == 32 => A 32 bit JVM but could be either 32 or 64 bit processor or libs
     63     * os.arch contains "64" could be 32 or 64 bit libs
     64     */
     65    private static final boolean is64 = "64".equals(System.getProperty("sun.arch.data.model")) ||
     66                                        System.getProperty("os.arch").contains("64");
     67
    5468    static
    5569    {
     
    5771    }
    5872   
    59     //A class that can (amongst other things I assume) represent the state of the
    60     //different CPU registers after a call to the CPUID assembly method
     73    /**
     74     * A class that can (amongst other things I assume) represent the state of the
     75     * different CPU registers after a call to the CPUID assembly method
     76     */
    6177    protected static class CPUIDResult {
    6278        final int EAX;
     
    152168    }
    153169   
    154     //Returns a CPUInfo item for the current type of CPU
    155     //If I could I would declare this method in a interface named
    156     //CPUInfoProvider and implement that interface in this class.
    157     //This would make it easier for other people to understand that there
    158     //is nothing preventing them from coding up new providers, probably using
    159     //other detection methods than the x86-only CPUID instruction
     170    /**
     171     * Returns a CPUInfo item for the current type of CPU
     172     * If I could I would declare this method in a interface named
     173     * CPUInfoProvider and implement that interface in this class.
     174     * This would make it easier for other people to understand that there
     175     * is nothing preventing them from coding up new providers, probably using
     176     * other detection methods than the x86-only CPUID instruction
     177     */
    160178    public static CPUInfo getInfo() throws UnknownCPUException
    161179    {
     
    507525    }
    508526   
    509        /**
     527    /**
    510528     * <p>Do whatever we can to load up the native library.
    511529     * If it can find a custom built jcpuid.dll / libjcpuid.so, it'll use that.  Otherwise
     
    548566    /**
    549567     * <p>Try loading it from an explictly built jcpuid.dll / libjcpuid.so</p>
     568     * The file name must be (e.g. on linux) either libjcpuid.so or libjcpuid-x86-linux.so.
     569     * This method does not search for a filename with "_64" in it.
    550570     *
    551571     * @return true if it was loaded successfully, else false
     
    559579            // fallthrough, try the OS-specific filename
    560580        }
     581
     582        // Don't bother trying a 64 bit filename variant.
     583
     584        // Note this is unlikely to succeed on a standard installation, since when we extract the library
     585        // in loadResource() below, we save it as jcpuid.dll / libcupid.so.
     586        // However, a distribution may put the file in, e.g., /usr/lib/jni/
     587        // with the libjcpuid-x86-linux.so name.
    561588        try {
    562589            System.loadLibrary(getLibraryMiddlePart());
     
    579606     * </p>
    580607     *
     608     * This tries the 64 bit version first if we think we may be 64 bit.
     609     * Then it tries the 32 bit version.
     610     *
    581611     * @return true if it was loaded successfully, else false
    582612     *
    583613     */
    584614    private static final boolean loadFromResource() {
    585         String resourceName = getResourceName();
    586         if (resourceName == null) return false;
    587         URL resource = CPUID.class.getClassLoader().getResource(resourceName);
     615        URL resource = null;
     616        // try 64 bit first, if getResourceName64() returns non-null
     617        String resourceName = getResourceName64();
     618        if (resourceName != null)
     619            resource = CPUID.class.getClassLoader().getResource(resourceName);
    588620       
     621        if (resource == null) {
     622            // now try 32 bit
     623            resourceName = getResourceName();
     624            resource = CPUID.class.getClassLoader().getResource(resourceName);
     625        }
     626
    589627        if (resource == null) {
    590628            if (_doLog)
     
    600638            outFile = new File(I2PAppContext.getGlobalContext().getTempDir(), filename);
    601639            fos = new FileOutputStream(outFile);
    602             // wtf this was 4096*1024 which is really excessive for a roughly 4KB file
    603640            byte buf[] = new byte[4096];
    604641            while (true) {
     
    634671    }
    635672   
     673    /** @return non-null */
    636674    private static final String getResourceName()
    637675    {
     
    639677    }
    640678   
     679    /**
     680     * @return null if not on a 64 bit platform
     681     * @since 0.8.7
     682     */
     683    private static final String getResourceName64() {
     684        if (!is64)
     685            return null;
     686        return getLibraryPrefix() + get64LibraryMiddlePart() + "." + getLibrarySuffix();
     687    }
     688   
    641689    private static final String getLibraryPrefix()
    642690    {
    643         boolean isWindows =System.getProperty("os.name").toLowerCase().indexOf("windows") != -1;
    644691        if(isWindows)
    645692            return "";
     
    649696   
    650697    private static final String getLibraryMiddlePart(){
    651         boolean isWindows =(System.getProperty("os.name").toLowerCase().indexOf("windows") != -1);
    652         boolean isLinux =(System.getProperty("os.name").toLowerCase().indexOf("linux") != -1);
    653         boolean isFreebsd =(System.getProperty("os.name").toLowerCase().indexOf("freebsd") != -1);
    654698        if(isWindows)
    655699             return "jcpuid-x86-windows"; // The convention on Windows
    656         if(isLinux)
    657             return "jcpuid-x86-linux"; // The convention on linux...
    658700        if(isFreebsd)
    659701            return "jcpuid-x86-freebsd"; // The convention on freebsd...
    660         throw new RuntimeException("Dont know jcpuid library name for os type '"+System.getProperty("os.name")+"'");
     702        //throw new RuntimeException("Dont know jcpuid library name for os type '"+System.getProperty("os.name")+"'");
     703        // use linux as the default, don't throw exception
     704        return "jcpuid-x86-linux";
     705    }
     706   
     707    /** @since 0.8.7 */
     708    private static final String get64LibraryMiddlePart() {
     709        if(isWindows)
     710             return "jcpuid-x86_64-windows";
     711        if(isFreebsd)
     712            return "jcpuid-x86_64-freebsd";
     713        // use linux as the default, don't throw exception
     714        return "jcpuid-x86_64-linux";
    661715    }
    662716   
    663717    private static final String getLibrarySuffix()
    664718    {
    665         boolean isWindows =System.getProperty("os.name").toLowerCase().indexOf("windows") != -1;
    666719        if(isWindows)
    667720            return "dll";
Note: See TracChangeset for help on using the changeset viewer.