Return-Path: Delivered-To: apmail-db-derby-commits-archive@www.apache.org Received: (qmail 28661 invoked from network); 25 Jul 2008 07:41:33 -0000 Received: from hermes.apache.org (HELO mail.apache.org) (140.211.11.2) by minotaur.apache.org with SMTP; 25 Jul 2008 07:41:33 -0000 Received: (qmail 88688 invoked by uid 500); 25 Jul 2008 07:41:33 -0000 Delivered-To: apmail-db-derby-commits-archive@db.apache.org Received: (qmail 88570 invoked by uid 500); 25 Jul 2008 07:41:32 -0000 Mailing-List: contact derby-commits-help@db.apache.org; run by ezmlm Precedence: bulk list-help: list-unsubscribe: List-Post: Reply-To: "Derby Development" List-Id: Delivered-To: mailing list derby-commits@db.apache.org Received: (qmail 88518 invoked by uid 99); 25 Jul 2008 07:41:32 -0000 Received: from athena.apache.org (HELO athena.apache.org) (140.211.11.136) by apache.org (qpsmtpd/0.29) with ESMTP; Fri, 25 Jul 2008 00:41:32 -0700 X-ASF-Spam-Status: No, hits=-2000.0 required=10.0 tests=ALL_TRUSTED X-Spam-Check-By: apache.org Received: from [140.211.11.4] (HELO eris.apache.org) (140.211.11.4) by apache.org (qpsmtpd/0.29) with ESMTP; Fri, 25 Jul 2008 07:40:46 +0000 Received: by eris.apache.org (Postfix, from userid 65534) id 30C082388892; Fri, 25 Jul 2008 00:41:12 -0700 (PDT) Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Subject: svn commit: r679690 - in /db/derby/code/trunk/java: drda/org/apache/derby/drda/server.policy engine/org/apache/derby/iapi/services/cache/ClassSize.java testing/org/apache/derbyTesting/functionTests/util/derby_tests.policy Date: Fri, 25 Jul 2008 07:41:11 -0000 To: derby-commits@db.apache.org From: kristwaa@apache.org X-Mailer: svnmailer-1.0.8 Message-Id: <20080725074112.30C082388892@eris.apache.org> X-Virus-Checked: Checked by ClamAV on apache.org Author: kristwaa Date: Fri Jul 25 00:41:10 2008 New Revision: 679690 URL: http://svn.apache.org/viewvc?rev=679690&view=rev Log: DERBY-3731: Improve calculation of refSize in ClassSize.java. Changed implementation to get the value for refSize through system properties. If that doesn't work due to missing privileges or unknown values, we fall back to the heuristic. The system properties consulted are "sun.arch.data.model" and "os.arch". Patch file: derby-3731-1c-refsize_from_properties.diff Modified: db/derby/code/trunk/java/drda/org/apache/derby/drda/server.policy db/derby/code/trunk/java/engine/org/apache/derby/iapi/services/cache/ClassSize.java db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/util/derby_tests.policy Modified: db/derby/code/trunk/java/drda/org/apache/derby/drda/server.policy URL: http://svn.apache.org/viewvc/db/derby/code/trunk/java/drda/org/apache/derby/drda/server.policy?rev=679690&r1=679689&r2=679690&view=diff ============================================================================== --- db/derby/code/trunk/java/drda/org/apache/derby/drda/server.policy (original) +++ db/derby/code/trunk/java/drda/org/apache/derby/drda/server.policy Fri Jul 25 00:41:10 2008 @@ -8,6 +8,9 @@ permission java.util.PropertyPermission "user.dir", "read"; permission java.util.PropertyPermission "derby.storage.jvmInstanceId", "write"; + // The next two properties are used to determine if the VM is 32 or 64 bit. + permission java.util.PropertyPermission "sun.arch.data.model", "read"; + permission java.util.PropertyPermission "os.arch", "read"; permission java.io.FilePermission "${derby.system.home}","read"; permission java.io.FilePermission "${derby.system.home}${/}-", "read,write,delete"; Modified: db/derby/code/trunk/java/engine/org/apache/derby/iapi/services/cache/ClassSize.java URL: http://svn.apache.org/viewvc/db/derby/code/trunk/java/engine/org/apache/derby/iapi/services/cache/ClassSize.java?rev=679690&r1=679689&r2=679690&view=diff ============================================================================== --- db/derby/code/trunk/java/engine/org/apache/derby/iapi/services/cache/ClassSize.java (original) +++ db/derby/code/trunk/java/engine/org/apache/derby/iapi/services/cache/ClassSize.java Fri Jul 25 00:41:10 2008 @@ -23,11 +23,11 @@ import org.apache.derby.iapi.services.sanity.SanityManager; -import java.lang.Class; import java.lang.reflect.Field; -import java.lang.Runtime; -import java.lang.InterruptedException; import java.lang.reflect.Modifier; +import java.security.AccessController; +import java.security.PrivilegedAction; +import java.util.Arrays; public class ClassSize { @@ -67,19 +67,26 @@ catalog = (java.util.Hashtable) Class.forName( "org.apache.derby.iapi.services.cache.ClassSizeCatalog").newInstance(); } - catch( Exception e){}; + catch( Exception e){} // Figure out whether this is a 32 or 64 bit machine. - Runtime runtime = Runtime.getRuntime(); - runtime.gc(); - runtime.runFinalization(); - long memBase = runtime.totalMemory() - runtime.freeMemory(); - Object[] junk = new Object[10000]; - runtime.gc(); - runtime.runFinalization(); - long memUsed = runtime.totalMemory() - runtime.freeMemory() - memBase; - int sz = (int)((memUsed + junk.length/2)/junk.length); - refSize = ( 4 > sz) ? 4 : sz; + int tmpRefSize = fetchRefSizeFromSystemProperties(); + // If we didn't understand the properties, or were not allowed to read + // them, use a heuristic. + if (tmpRefSize < 4) { + Runtime runtime = Runtime.getRuntime(); + runtime.gc(); + runtime.runFinalization(); + long memBase = runtime.totalMemory() - runtime.freeMemory(); + Object[] junk = new Object[10000]; + runtime.gc(); + runtime.runFinalization(); + long memUsed = runtime.totalMemory() - runtime.freeMemory() - memBase; + int sz = (int)((memUsed + junk.length/2)/junk.length); + tmpRefSize = ( 4 > sz) ? 4 : sz; + } + // Assign what we have found to the final variable. + refSize = tmpRefSize; minObjectSize = 4*refSize; } @@ -299,4 +306,60 @@ // Since Java uses Unicode assume that each character takes 2 bytes return 2*str.length(); } + + /** + * Tries to determine the reference size in bytes by checking whether the + * VM we're running in is 32 or 64 bit by looking at the system properties. + * + * @return The reference size in bytes as specified or implied by the VM, + * or {@code -1} if the reference size couldn't be determined. + */ + private static final int fetchRefSizeFromSystemProperties() { + // Try the direct way first, by looking for 'sun.arch.data.model' + String dataModel = getSystemProperty("sun.arch.data.model"); + try { + return (new Integer(dataModel).intValue() / 8); + } catch (NumberFormatException ignoreNFE) {} + + // Try 'os.arch' + String arch = getSystemProperty("os.arch"); + // See if we recognize the property value. + if (arch != null) { + // Is it a known 32 bit architecture? + String[] b32 = new String[] {"i386", "x86", "sparc"}; + if (Arrays.asList(b32).contains(arch)) return 4; // 4 bytes per ref + // Is it a known 64 bit architecture? + String[] b64 = new String[] {"amd64", "x86_64", "sparcv9"}; + if (Arrays.asList(b64).contains(arch)) return 8; // 8 bytes per ref + } + + // Didn't find out anything. + if (SanityManager.DEBUG) { + SanityManager.DEBUG_PRINT( + "REFSIZE", "Bitness undetermined, sun.arch.data.model='" + + dataModel + "', os.arch='" + arch + "'"); + } + return -1; + } + + /** + * Attempts to read the specified system property. + * + * @param propName name of the system property to read + * @return The property value, or {@code null} if it doesn't exist or the + * required permission to read the property is missing. + */ + private static final String getSystemProperty(final String propName) { + try { + return (String)AccessController.doPrivileged( + new PrivilegedAction() { + public Object run() { + return System.getProperty(propName, null); + } + }); + } catch (SecurityException se) { + // Ignore exception and return null. + return null; + } + } } Modified: db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/util/derby_tests.policy URL: http://svn.apache.org/viewvc/db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/util/derby_tests.policy?rev=679690&r1=679689&r2=679690&view=diff ============================================================================== --- db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/util/derby_tests.policy (original) +++ db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/util/derby_tests.policy Fri Jul 25 00:41:10 2008 @@ -49,6 +49,9 @@ permission java.util.PropertyPermission "derby.*", "read"; permission java.util.PropertyPermission "derby.storage.jvmInstanceId", "write"; + // The next two properties are used to determine if the VM is 32 or 64 bit. + permission java.util.PropertyPermission "sun.arch.data.model", "read"; + permission java.util.PropertyPermission "os.arch", "read"; permission java.util.PropertyPermission "java.class.path", "read";//sysinfo // unit tests (e.g. store/T_RecoverFullLog) set this property