Changeset 2c3be29


Ignore:
Timestamp:
May 27, 2011 3:04:40 PM (9 years ago)
Author:
zzz <zzz@…>
Branches:
master
Children:
6a9f73b
Parents:
8eb1087
Message:
  • JBigI:
    • Refactor to handle 64 bit libs and a list of fallback libs
    • Logging tweaks
    • Only check for one file name in library path
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • core/java/src/net/i2p/util/NativeBigInteger.java

    r8eb1087 r2c3be29  
    1616import java.net.URL;
    1717import java.security.SecureRandom;
     18import java.util.ArrayList;
     19import java.util.List;
    1820import java.util.Random;
    1921
     
    128130    private final static String JBIGI_OPTIMIZATION_GEODE      = "geode";
    129131    private final static String JBIGI_OPTIMIZATION_NANO       = "nano";
     132    private final static String JBIGI_OPTIMIZATION_PENTIUMM   = "pentiumm";
    130133    private final static String JBIGI_OPTIMIZATION_VIAC32     = "viac32";
    131134
     
    135138    private static final boolean _isLinux = System.getProperty("os.name").toLowerCase().contains("linux");
    136139    private static final boolean _isFreebsd = System.getProperty("os.name").toLowerCase().contains("freebsd");
     140
     141    /*
     142     * This isn't always correct.
     143     * http://stackoverflow.com/questions/807263/how-do-i-detect-which-kind-of-jre-is-installed-32bit-vs-64bit
     144     * http://mark.koli.ch/2009/10/javas-osarch-system-property-is-the-bitness-of-the-jre-not-the-operating-system.html
     145     * http://mark.koli.ch/2009/10/reliably-checking-os-bitness-32-or-64-bit-on-windows-with-a-tiny-c-app.html
     146     * sun.arch.data.model not on all JVMs
     147     * sun.arch.data.model == 64 => 64 bit processor
     148     * sun.arch.data.model == 32 => A 32 bit JVM but could be either 32 or 64 bit processor or libs
     149     * os.arch contains "64" could be 32 or 64 bit libs
     150     */
     151    private static final boolean _is64 = "64".equals(System.getProperty("sun.arch.data.model")) ||
     152                                         System.getProperty("os.arch").contains("64");
    137153
    138154    /* libjbigi.so vs jbigi.dll */
     
    154170      */
    155171    private static String resolveCPUType() {
    156         /*
    157          * This isn't always correct.
    158          * http://stackoverflow.com/questions/807263/how-do-i-detect-which-kind-of-jre-is-installed-32bit-vs-64bit
    159          * http://mark.koli.ch/2009/10/javas-osarch-system-property-is-the-bitness-of-the-jre-not-the-operating-system.html
    160          * http://mark.koli.ch/2009/10/reliably-checking-os-bitness-32-or-64-bit-on-windows-with-a-tiny-c-app.html
    161          * sun.arch.data.model not on all JVMs
    162          * sun.arch.data.model == 64 => 64 bit processor
    163          * sun.arch.data.model == 32 => A 32 bit JVM but could be either 32 or 64 bit processor or libs
    164          * os.arch contains "64" could be 32 or 64 bit libs
    165          */
    166         boolean is64 = "64".equals(System.getProperty("sun.arch.data.model")) ||
    167                        System.getProperty("os.arch").contains("64");
    168         if (is64) {
     172        if (_is64) {
    169173            // Test the 64 bit libjcpuid, even though we don't use it yet
    170174            try {
     
    256260        super(val, radix);
    257261    }
     262
    258263    /**Creates a new NativeBigInteger with the same value
    259264    *  as the supplied BigInteger. Warning!, not very efficent
     
    391396     * If the user specifies -Djbigi.enable=false it'll skip all of this.</p>
    392397     *
     398     * <pre>
     399     * Load order (using linux naming with cpu type "xxx")
     400     * Old order 0.8.6 and earlier:
     401     *   - filesystem libjbigi.so
     402     *   - jbigi.jar libjbigi.so
     403     *   - jbigi.jar libjbigi-linux-xxx.so
     404     *   - filesystem libjbigi-linux-xxx.so
     405     *   - jbigi.jar libjbigi-linux-none.so
     406     *   - filesystem libjbigi-linux-none.so
     407     *
     408     * New order as of 0.8.7:
     409     *   - filesystem libjbigi.so
     410     *   - jbigi.jar libjbigi-linux-xxx_64.so if it may be 64 bit
     411     *   - jbigi.jar libjbigi-linux-athlon64_64.so if it may be 64 bit
     412     *   - jbigi.jar libjbigi-linux-xxx.so
     413     *   - jbigi.jar libjbigi-linux-athlon64.so if it may be 64 bit
     414     *   - jbigi.jar libjbigi-linux-yyy.so 0 or more other alternates
     415     *   - jbigi.jar libjbigi-linux-none_64.so if it may be 64 bit
     416     *   - jbigi.jar libjbigi-linux-none.so
     417     * </pre>
    393418     */
    394419    private static final void loadNative() {
    395420        try{
    396         String wantedProp = System.getProperty("jbigi.enable", "true");
    397         boolean wantNative = "true".equalsIgnoreCase(wantedProp);
    398         if (wantNative) {
    399             boolean loaded = loadGeneric("jbigi");
    400             if (loaded) {
    401                 _nativeOk = true;
    402                 info("Locally optimized native BigInteger library loaded from the library path");
    403             } else {
    404                 loaded = loadFromResource("jbigi");
     421            String wantedProp = System.getProperty("jbigi.enable", "true");
     422            boolean wantNative = "true".equalsIgnoreCase(wantedProp);
     423            if (wantNative) {
     424                debug("trying loadGeneric");
     425                boolean loaded = loadGeneric("jbigi");
    405426                if (loaded) {
    406427                    _nativeOk = true;
    407                     info("Locally optimized native BigInteger library loaded from resource");
     428                    info("Locally optimized native BigInteger library loaded from file");
    408429                } else {
    409                     loaded = loadFromResource(true);
    410                     if (loaded) {
    411                         _nativeOk = true;
    412                         info("Optimized native BigInteger library '"+getResourceName(true)+"' loaded from resource");
    413                     } else {
    414                         loaded = loadGeneric(true);
    415                         if (loaded) {
     430                    List<String> toTry = getResourceList();
     431                    debug("loadResource list to try is: " + toTry);
     432                    for (String s : toTry) {
     433                        debug("trying loadResource " + s);
     434                        if (loadFromResource(s)) {
    416435                            _nativeOk = true;
    417                             info("Optimized native BigInteger library '"+getMiddleName(true)+"' loaded from somewhere in the path");
    418                         } else {
    419                             loaded = loadFromResource(false);
    420                             if (loaded) {
    421                                 _nativeOk = true;
    422                                 info("Non-optimized native BigInteger library '"+getResourceName(false)+"' loaded from resource");
    423                             } else {
    424                                 loaded = loadGeneric(false);
    425                                 if (loaded) {
    426                                     _nativeOk = true;
    427                                     info("Non-optimized native BigInteger library '"+getMiddleName(false)+"' loaded from somewhere in the path");
    428                                 } else {
    429                                     _nativeOk = false;         
    430                                 }
    431                             }       
     436                            info("Native BigInteger library " + s + " loaded from resource");
     437                            break;
    432438                        }
    433439                    }
    434440                }
    435441            }
    436         }
    437         if (!_nativeOk) {
    438             warn("Native BigInteger library jbigi not loaded - using pure Java - " +
    439                  "poor performance may result - see http://www.i2p2.i2p/jbigi.html for help");
    440         }
    441         }catch(Exception e){
    442             warn("Native BigInteger library jbigi not loaded, reason: '"+e.getMessage()+"' - using pure java");
    443         }
    444     }
     442            if (!_nativeOk) {
     443                warn("Native BigInteger library jbigi not loaded - using pure Java - " +
     444                     "poor performance may result - see http://www.i2p2.i2p/jbigi for help");
     445            }
     446        } catch(Exception e) {
     447            warn("Native BigInteger library jbigi not loaded, using pure java", e);
     448        }
     449    }
     450   
     451    /** @since 0.8.7 */
     452    private static void debug(String s) {
     453        I2PAppContext.getGlobalContext().logManager().getLog(NativeBigInteger.class).debug(s);
     454    }
     455
    445456   
    446457    private static void info(String s) {
     
    452463
    453464    private static void warn(String s) {
    454         if(_doLog)
     465        warn(s, null);
     466    }
     467
     468    /** @since 0.8.7 */
     469    private static void warn(String s, Throwable t) {
     470        if(_doLog) {
    455471            System.err.println("WARNING: " + s);
    456         I2PAppContext.getGlobalContext().logManager().getLog(NativeBigInteger.class).warn(s);
    457         _loadStatus = s;
     472            if (t != null)
     473                t.printStackTrace();
     474        }
     475        I2PAppContext.getGlobalContext().logManager().getLog(NativeBigInteger.class).warn(s, t);
     476        if (t != null)
     477            _loadStatus = s + ' ' + t;
     478        else
     479            _loadStatus = s;
    458480    }
    459481
     
    510532        URL resource = ClassLoader.getSystemResource(resourceName);
    511533        if (resource == null) {
    512             if (_doLog)
    513                 System.err.println("NOTICE: Resource name [" + resourceName + "] was not found");
     534            info("Resource name [" + resourceName + "] was not found");
    514535            return false;
    515536        }
     
    532553            System.load(outFile.getAbsolutePath()); //System.load requires an absolute path to the lib
    533554        } catch (UnsatisfiedLinkError ule) {
    534             if (_doLog) {
    535                 System.err.println("ERROR: The resource " + resourceName
    536                                    + " was not a valid library for this platform");
    537                 ule.printStackTrace();
    538             }
     555            // don't include the exception in the message - too much
     556            warn("Failed to load the resource " + resourceName + " - not a valid library for this platform");
    539557            if (outFile != null)
    540558                outFile.delete();
    541559            return false;
    542560        } catch (IOException ioe) {
    543             if (_doLog) {
    544                 System.err.println("ERROR: Problem writing out the temporary native library data");
    545                 ioe.printStackTrace();
    546             }
     561            warn("Problem writing out the temporary native library data", ioe);
    547562            if (outFile != null)
    548563                outFile.delete();
     
    560575   
    561576    /**
     577     *  Generate a list of resources to search for, in-order.
     578     *  See loadNative() comments for more info.
     579     *  @return non-null
     580     *  @since 0.8.7
     581     */
     582    private static List<String> getResourceList() {
     583        List<String> rv = new ArrayList(8);
     584        String primary = getMiddleName2(true);
     585        if (primary != null) {
     586            if (_is64) {
     587                // add 64 bit variants at the front
     588                if (!primary.equals(JBIGI_OPTIMIZATION_ATHLON64))
     589                    rv.add(_libPrefix + getMiddleName1() + primary + "_64" + _libSuffix);
     590                // athlon64_64 is always a fallback for 64 bit
     591                rv.add(_libPrefix + getMiddleName1() + JBIGI_OPTIMIZATION_ATHLON64 + "_64" + _libSuffix);
     592            }
     593            // the preferred selection
     594            rv.add(_libPrefix + getMiddleName1() + primary + _libSuffix);
     595            // athlon64 is always a fallback for 64 bit
     596            if (_is64 && !primary.equals(JBIGI_OPTIMIZATION_ATHLON64))
     597                rv.add(_libPrefix + getMiddleName1() + JBIGI_OPTIMIZATION_ATHLON64 + _libSuffix);
     598            // Add fallbacks for any 32-bit that were added 0.8.7 or later here
     599            if (primary.equals(JBIGI_OPTIMIZATION_ATOM))
     600                rv.add(_libPrefix + getMiddleName1() + JBIGI_OPTIMIZATION_PENTIUM3 + _libSuffix);
     601
     602        } else {
     603            if (_is64) {
     604                rv.add(_libPrefix + getMiddleName1() + JBIGI_OPTIMIZATION_ATHLON64 + "_64" + _libSuffix);
     605                rv.add(_libPrefix + getMiddleName1() + JBIGI_OPTIMIZATION_ATHLON64 + _libSuffix);
     606            }
     607        }
     608        // add libjbigi-xxx-none.so
     609        if (_is64)
     610            rv.add(_libPrefix + getMiddleName1() + "none_64" + _libSuffix);
     611        rv.add(getResourceName(false));
     612        return rv;
     613    }
     614
     615    /**
    562616     *  @return may be null if optimized is true
    563617     */
     
    573627     */
    574628    private static final String getMiddleName(boolean optimized) {
     629        String m2 = getMiddleName2(optimized);
     630        if (m2 == null)
     631            return null;
     632        return getMiddleName1() + m2;
     633    }
     634
     635    /**
     636     *  @return may be null if optimized is true; returns "none" if optimize is false
     637     *  @since 0.8.7
     638     */
     639    private static final String getMiddleName2(boolean optimized) {
    575640        String sAppend;
    576641        if (optimized) {
     
    581646            if (sCPUType.equals(JBIGI_OPTIMIZATION_K6_3) && !_isWin)
    582647                // k62 and k63 identical except on windows
    583                 sAppend = "-" + JBIGI_OPTIMIZATION_K6_2;
     648                sAppend = JBIGI_OPTIMIZATION_K6_2;
    584649            else if (sCPUType.equals(JBIGI_OPTIMIZATION_VIAC32))
    585650                // viac32 and pentium3 identical
    586                 sAppend = "-" + JBIGI_OPTIMIZATION_PENTIUM3;
     651                sAppend = JBIGI_OPTIMIZATION_PENTIUM3;
    587652            //else if (sCPUType.equals(JBIGI_OPTIMIZATION_VIAC3) && _isWin)
    588653                // FIXME no viac3 available for windows, what to use instead?
    589654            else
    590                 sAppend = "-" + sCPUType;       
     655                sAppend = sCPUType;       
    591656        } else {
    592             sAppend = "-none";
    593         }
    594 
     657            sAppend = "none";
     658        }
     659        return sAppend;
     660    }
     661
     662    /**
     663     *  @return "jbigi-xxx-"
     664     *  @since 0.8.7
     665     */
     666    private static final String getMiddleName1() {
    595667        if(_isWin)
    596              return "jbigi-windows"+sAppend; // The convention on Windows
     668             return "jbigi-windows-";
    597669        if(_isFreebsd)
    598             return "jbigi-freebsd"+sAppend; // The convention on freebsd...
     670            return "jbigi-freebsd-";
    599671        if(_isMac)
    600             return "jbigi-osx"+sAppend;
     672            return "jbigi-osx-";
    601673        if(_isOS2)
    602             return "jbigi-os2"+sAppend;
     674            return "jbigi-os2-";
    603675        //throw new RuntimeException("Dont know jbigi library name for os type '"+System.getProperty("os.name")+"'");
    604676        // use linux as the default, don't throw exception
    605         return "jbigi-linux" + sAppend;
     677        return "jbigi-linux-";
    606678    }
    607679}
  • history.txt

    r8eb1087 r2c3be29  
     12011-05-27 zzz
     2    * Console: Fix router.updateThroughProxy being set to false after saving
     3               change on /configupdate when install dir is read-only.
     4               (ticket #466)
     5    * JBigI:
     6      - Refactor to handle 64 bit libs and a list of fallback libs
     7      - Logging tweaks
     8      - Only check for one file name in library path
     9    * CPUID:
     10      - Recognize amd64 as x86
     11      - Only check for one file name in library path
     12
    1132011-05-25 zzz
    214    * CPUID: Load 64-bit libcpuid if available
  • router/java/src/net/i2p/router/RouterVersion.java

    r8eb1087 r2c3be29  
    1919    public final static String ID = "Monotone";
    2020    public final static String VERSION = CoreVersion.VERSION;
    21     public final static long BUILD = 8;
     21    public final static long BUILD = 9;
    2222
    2323    /** for example "-test" */
Note: See TracChangeset for help on using the changeset viewer.