0N/A/*
4680N/A * Copyright (c) 1998, 2012, Oracle and/or its affiliates. All rights reserved.
0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
0N/A *
0N/A * This code is free software; you can redistribute it and/or modify it
0N/A * under the terms of the GNU General Public License version 2 only, as
2362N/A * published by the Free Software Foundation. Oracle designates this
0N/A * particular file as subject to the "Classpath" exception as provided
2362N/A * by Oracle in the LICENSE file that accompanied this code.
0N/A *
0N/A * This code is distributed in the hope that it will be useful, but WITHOUT
0N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
0N/A * version 2 for more details (a copy is included in the LICENSE file that
0N/A * accompanied this code).
0N/A *
0N/A * You should have received a copy of the GNU General Public License version
0N/A * 2 along with this work; if not, write to the Free Software Foundation,
0N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
0N/A *
2362N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2362N/A * or visit www.oracle.com if you need additional information or have any
2362N/A * questions.
0N/A */
0N/A
0N/A#include "ergo.h"
0N/A
0N/Astatic unsigned long physical_processors(void);
0N/A
0N/A#ifdef __solaris__
0N/A
0N/A/*
0N/A * A utility method for asking the CPU about itself.
0N/A * There's a corresponding version of linux-i586
0N/A * because the compilers are different.
0N/A */
0N/Astatic void
0N/Aget_cpuid(uint32_t arg,
0N/A uint32_t* eaxp,
0N/A uint32_t* ebxp,
0N/A uint32_t* ecxp,
0N/A uint32_t* edxp) {
0N/A#ifdef _LP64
0N/A asm(
0N/A /* rbx is a callee-saved register */
0N/A " movq %rbx, %r11 \n"
0N/A /* rdx and rcx are 3rd and 4th argument registers */
0N/A " movq %rdx, %r10 \n"
0N/A " movq %rcx, %r9 \n"
0N/A " movl %edi, %eax \n"
0N/A " cpuid \n"
0N/A " movl %eax, (%rsi)\n"
0N/A " movl %ebx, (%r10)\n"
0N/A " movl %ecx, (%r9) \n"
0N/A " movl %edx, (%r8) \n"
0N/A /* Restore rbx */
0N/A " movq %r11, %rbx");
0N/A#else
0N/A /* EBX is a callee-saved register */
0N/A asm(" pushl %ebx");
0N/A /* Need ESI for storing through arguments */
0N/A asm(" pushl %esi");
0N/A asm(" movl 8(%ebp), %eax \n"
0N/A " cpuid \n"
0N/A " movl 12(%ebp), %esi \n"
0N/A " movl %eax, (%esi) \n"
0N/A " movl 16(%ebp), %esi \n"
0N/A " movl %ebx, (%esi) \n"
0N/A " movl 20(%ebp), %esi \n"
0N/A " movl %ecx, (%esi) \n"
0N/A " movl 24(%ebp), %esi \n"
0N/A " movl %edx, (%esi) ");
0N/A /* Restore ESI and EBX */
0N/A asm(" popl %esi");
0N/A /* Restore EBX */
0N/A asm(" popl %ebx");
0N/A#endif /* LP64 */
0N/A}
0N/A
0N/A/* The definition of a server-class machine for solaris-i586/amd64 */
0N/Ajboolean
0N/AServerClassMachineImpl(void) {
0N/A jboolean result = JNI_FALSE;
0N/A /* How big is a server class machine? */
0N/A const unsigned long server_processors = 2UL;
0N/A const uint64_t server_memory = 2UL * GB;
0N/A /*
0N/A * We seem not to get our full complement of memory.
0N/A * We allow some part (1/8?) of the memory to be "missing",
0N/A * based on the sizes of DIMMs, and maybe graphics cards.
0N/A */
0N/A const uint64_t missing_memory = 256UL * MB;
0N/A const uint64_t actual_memory = physical_memory();
0N/A
0N/A /* Is this a server class machine? */
0N/A if (actual_memory >= (server_memory - missing_memory)) {
0N/A const unsigned long actual_processors = physical_processors();
0N/A if (actual_processors >= server_processors) {
0N/A result = JNI_TRUE;
0N/A }
0N/A }
0N/A JLI_TraceLauncher("solaris_" LIBARCHNAME "_ServerClassMachine: %s\n",
0N/A (result == JNI_TRUE ? "true" : "false"));
0N/A return result;
0N/A}
0N/A
0N/A#endif /* __solaris__ */
0N/A
4680N/A#ifdef __linux__
0N/A
0N/A/*
0N/A * A utility method for asking the CPU about itself.
0N/A * There's a corresponding version of solaris-i586
0N/A * because the compilers are different.
0N/A */
0N/Astatic void
0N/Aget_cpuid(uint32_t arg,
0N/A uint32_t* eaxp,
0N/A uint32_t* ebxp,
0N/A uint32_t* ecxp,
0N/A uint32_t* edxp) {
0N/A#ifdef _LP64
0N/A __asm__ volatile (/* Instructions */
0N/A " movl %4, %%eax \n"
0N/A " cpuid \n"
0N/A " movl %%eax, (%0)\n"
0N/A " movl %%ebx, (%1)\n"
0N/A " movl %%ecx, (%2)\n"
0N/A " movl %%edx, (%3)\n"
0N/A : /* Outputs */
0N/A : /* Inputs */
0N/A "r" (eaxp),
0N/A "r" (ebxp),
0N/A "r" (ecxp),
0N/A "r" (edxp),
0N/A "r" (arg)
0N/A : /* Clobbers */
0N/A "%rax", "%rbx", "%rcx", "%rdx", "memory"
0N/A );
0N/A#else /* _LP64 */
0N/A uint32_t value_of_eax = 0;
0N/A uint32_t value_of_ebx = 0;
0N/A uint32_t value_of_ecx = 0;
0N/A uint32_t value_of_edx = 0;
0N/A __asm__ volatile (/* Instructions */
0N/A /* ebx is callee-save, so push it */
0N/A " pushl %%ebx \n"
0N/A " movl %4, %%eax \n"
0N/A " cpuid \n"
0N/A " movl %%eax, %0 \n"
0N/A " movl %%ebx, %1 \n"
0N/A " movl %%ecx, %2 \n"
0N/A " movl %%edx, %3 \n"
0N/A /* restore ebx */
0N/A " popl %%ebx \n"
0N/A
0N/A : /* Outputs */
0N/A "=m" (value_of_eax),
0N/A "=m" (value_of_ebx),
0N/A "=m" (value_of_ecx),
0N/A "=m" (value_of_edx)
0N/A : /* Inputs */
0N/A "m" (arg)
0N/A : /* Clobbers */
0N/A "%eax", "%ecx", "%edx"
0N/A );
0N/A *eaxp = value_of_eax;
0N/A *ebxp = value_of_ebx;
0N/A *ecxp = value_of_ecx;
0N/A *edxp = value_of_edx;
0N/A#endif /* _LP64 */
0N/A}
0N/A
0N/A/* The definition of a server-class machine for linux-i586 */
0N/Ajboolean
0N/AServerClassMachineImpl(void) {
0N/A jboolean result = JNI_FALSE;
0N/A /* How big is a server class machine? */
0N/A const unsigned long server_processors = 2UL;
0N/A const uint64_t server_memory = 2UL * GB;
0N/A /*
0N/A * We seem not to get our full complement of memory.
0N/A * We allow some part (1/8?) of the memory to be "missing",
0N/A * based on the sizes of DIMMs, and maybe graphics cards.
0N/A */
0N/A const uint64_t missing_memory = 256UL * MB;
0N/A const uint64_t actual_memory = physical_memory();
0N/A
0N/A /* Is this a server class machine? */
0N/A if (actual_memory >= (server_memory - missing_memory)) {
0N/A const unsigned long actual_processors = physical_processors();
0N/A if (actual_processors >= server_processors) {
0N/A result = JNI_TRUE;
0N/A }
0N/A }
0N/A JLI_TraceLauncher("linux_" LIBARCHNAME "_ServerClassMachine: %s\n",
0N/A (result == JNI_TRUE ? "true" : "false"));
0N/A return result;
0N/A}
0N/A#endif /* __linux__ */
0N/A
0N/A/*
0N/A * Routines shared by solaris-i586 and linux-i586.
0N/A */
0N/A
0N/Aenum HyperThreadingSupport_enum {
0N/A hts_supported = 1,
0N/A hts_too_soon_to_tell = 0,
0N/A hts_not_supported = -1,
0N/A hts_not_pentium4 = -2,
0N/A hts_not_intel = -3
0N/A};
0N/Atypedef enum HyperThreadingSupport_enum HyperThreadingSupport;
0N/A
0N/A/* Determine if hyperthreading is supported */
0N/Astatic HyperThreadingSupport
0N/Ahyperthreading_support(void) {
0N/A HyperThreadingSupport result = hts_too_soon_to_tell;
0N/A /* Bits 11 through 8 is family processor id */
0N/A# define FAMILY_ID_SHIFT 8
0N/A# define FAMILY_ID_MASK 0xf
0N/A /* Bits 23 through 20 is extended family processor id */
0N/A# define EXT_FAMILY_ID_SHIFT 20
0N/A# define EXT_FAMILY_ID_MASK 0xf
0N/A /* Pentium 4 family processor id */
0N/A# define PENTIUM4_FAMILY_ID 0xf
0N/A /* Bit 28 indicates Hyper-Threading Technology support */
0N/A# define HT_BIT_SHIFT 28
0N/A# define HT_BIT_MASK 1
0N/A uint32_t vendor_id[3] = { 0U, 0U, 0U };
0N/A uint32_t value_of_eax = 0U;
0N/A uint32_t value_of_edx = 0U;
0N/A uint32_t dummy = 0U;
0N/A
0N/A /* Yes, this is supposed to be [0], [2], [1] */
0N/A get_cpuid(0, &dummy, &vendor_id[0], &vendor_id[2], &vendor_id[1]);
0N/A JLI_TraceLauncher("vendor: %c %c %c %c %c %c %c %c %c %c %c %c \n",
0N/A ((vendor_id[0] >> 0) & 0xff),
0N/A ((vendor_id[0] >> 8) & 0xff),
0N/A ((vendor_id[0] >> 16) & 0xff),
0N/A ((vendor_id[0] >> 24) & 0xff),
0N/A ((vendor_id[1] >> 0) & 0xff),
0N/A ((vendor_id[1] >> 8) & 0xff),
0N/A ((vendor_id[1] >> 16) & 0xff),
0N/A ((vendor_id[1] >> 24) & 0xff),
0N/A ((vendor_id[2] >> 0) & 0xff),
0N/A ((vendor_id[2] >> 8) & 0xff),
0N/A ((vendor_id[2] >> 16) & 0xff),
0N/A ((vendor_id[2] >> 24) & 0xff));
0N/A get_cpuid(1, &value_of_eax, &dummy, &dummy, &value_of_edx);
0N/A JLI_TraceLauncher("value_of_eax: 0x%x value_of_edx: 0x%x\n",
0N/A value_of_eax, value_of_edx);
0N/A if ((((value_of_eax >> FAMILY_ID_SHIFT) & FAMILY_ID_MASK) == PENTIUM4_FAMILY_ID) ||
0N/A (((value_of_eax >> EXT_FAMILY_ID_SHIFT) & EXT_FAMILY_ID_MASK) != 0)) {
0N/A if ((((vendor_id[0] >> 0) & 0xff) == 'G') &&
0N/A (((vendor_id[0] >> 8) & 0xff) == 'e') &&
0N/A (((vendor_id[0] >> 16) & 0xff) == 'n') &&
0N/A (((vendor_id[0] >> 24) & 0xff) == 'u') &&
0N/A (((vendor_id[1] >> 0) & 0xff) == 'i') &&
0N/A (((vendor_id[1] >> 8) & 0xff) == 'n') &&
0N/A (((vendor_id[1] >> 16) & 0xff) == 'e') &&
0N/A (((vendor_id[1] >> 24) & 0xff) == 'I') &&
0N/A (((vendor_id[2] >> 0) & 0xff) == 'n') &&
0N/A (((vendor_id[2] >> 8) & 0xff) == 't') &&
0N/A (((vendor_id[2] >> 16) & 0xff) == 'e') &&
0N/A (((vendor_id[2] >> 24) & 0xff) == 'l')) {
0N/A if (((value_of_edx >> HT_BIT_SHIFT) & HT_BIT_MASK) == HT_BIT_MASK) {
0N/A JLI_TraceLauncher("Hyperthreading supported\n");
0N/A result = hts_supported;
0N/A } else {
0N/A JLI_TraceLauncher("Hyperthreading not supported\n");
0N/A result = hts_not_supported;
0N/A }
0N/A } else {
0N/A JLI_TraceLauncher("Not GenuineIntel\n");
0N/A result = hts_not_intel;
0N/A }
0N/A } else {
0N/A JLI_TraceLauncher("not Pentium 4 or extended\n");
0N/A result = hts_not_pentium4;
0N/A }
0N/A return result;
0N/A}
0N/A
0N/A/* Determine how many logical processors there are per CPU */
0N/Astatic unsigned int
0N/Alogical_processors_per_package(void) {
0N/A /*
0N/A * After CPUID with EAX==1, register EBX bits 23 through 16
0N/A * indicate the number of logical processors per package
0N/A */
0N/A# define NUM_LOGICAL_SHIFT 16
0N/A# define NUM_LOGICAL_MASK 0xff
0N/A unsigned int result = 1U;
0N/A const HyperThreadingSupport hyperthreading = hyperthreading_support();
0N/A
0N/A if (hyperthreading == hts_supported) {
0N/A uint32_t value_of_ebx = 0U;
0N/A uint32_t dummy = 0U;
0N/A
0N/A get_cpuid(1, &dummy, &value_of_ebx, &dummy, &dummy);
0N/A result = (value_of_ebx >> NUM_LOGICAL_SHIFT) & NUM_LOGICAL_MASK;
0N/A JLI_TraceLauncher("logical processors per package: %u\n", result);
0N/A }
0N/A return result;
0N/A}
0N/A
0N/A/* Compute the number of physical processors, not logical processors */
0N/Astatic unsigned long
0N/Aphysical_processors(void) {
0N/A const long sys_processors = sysconf(_SC_NPROCESSORS_CONF);
0N/A unsigned long result = sys_processors;
0N/A
0N/A JLI_TraceLauncher("sysconf(_SC_NPROCESSORS_CONF): %lu\n", sys_processors);
0N/A if (sys_processors > 1) {
0N/A unsigned int logical_processors = logical_processors_per_package();
0N/A if (logical_processors > 1) {
0N/A result = (unsigned long) sys_processors / logical_processors;
0N/A }
0N/A }
0N/A JLI_TraceLauncher("physical processors: %lu\n", result);
0N/A return result;
0N/A}