Skip to content

Ability to tell how many physical CPU's are on a system #353

Closed

Description

@saintGH

Apologies as this is my first foray onto Github after lurking for many many years, so if this is a really dumb question, I'll take my licks.

I'm trying to gather a bunch of information about our testing machines so that when we record performance results, we can also include the server configuration of the test machine to see a complete picture.

Ideally I'd like to be able to determine that there are:

1 CPU / 4 Cores for my work station and
2 CPU's / 12 Cores for my test machine that I'm currently working with.

Note that I'm specifically avoiding the use of the term "Processor".

Currently I'm hung up on trying to determine if a test machine has more than one physical CPU (as in more than one CPU on the motherboard).

When I run the following code on my single quad-core CPU workstation, I get 4 for both:
int cpuCount = centralProcessor.getPhysicalProcessorCount(); -- returns 4
int coreCount = centralProcessor.getLogicalProcessorCount(); -- returns 4

We have test machines that have 1-2 CPU's installed, and we want to make that determination. Is there anyway to do this with OSHI?

Activity

  1. YoshiEnVerde commented on May 5, 2017

    @YoshiEnVerde
    Collaborator

    There's no stupid questions, only stupid people that won't learn when advised :P
    That said, the problem is that the stats recovered by OSHI are for physical and logical processors.

    And, just like you avoided using that term for your needs (which is perfect for your situation), these values are only peripherally related to the ones you need.

    Just as a simple run down:
    A QuadCore CPU has 4 Physical Processors.
    Every Physical Processor, depending on their internal architecture, can be used as 1 or more Logical Processors.

    For example, AMD MultiCore Archs were notorious for giving you 2 Logical Processors for every Physical Processor.

    So, Physical Processors is actually your Core Count.
    For the Microprocessor Count you'd need to check if your system has more than one Processor. I'm pretty sure that most of the SO functionalities OSHI checks for processor data does give you separate data for each Microprocessor, so it shouldn't be impossible

  2. YoshiEnVerde commented on May 5, 2017

    @YoshiEnVerde
    Collaborator

    After checking the code, we can see:

    At oshi/oshi-core/src/main/java/oshi/hardware/platform/windows/WindowsCentralProcessor.java
    private void initVars() lines ? to ?:

     {
        final String cpuRegistryRoot = "HARDWARE\\DESCRIPTION\\System\\CentralProcessor";
        String[] processorIds = Advapi32Util.registryGetKeys(WinReg.HKEY_LOCAL_MACHINE, cpuRegistryRoot);
        if (processorIds.length > 0) {
            String cpuRegistryPath = cpuRegistryRoot + "\\" + processorIds[0];
            //All the processor var setting is here
        }
        //... Rest of the method
    

    As you can see, when running on Windows, we get all the processors, but discard everyone but the first one if there's more than one.

    Maybe, we could refactor the code to some kind of linked list?
    To avoid modifying th API too much, at least until the next major release, we could have each processor have a field linking to the next processor.

    I'll check on the other classes, but I think I'll find something pretty similar

  3. saintGH commented on May 5, 2017

    @saintGH
    Author

    Cheers for your reply, the terminology was giving me fits just writing my question. ;)

    Having looked at the Windows Registry information that the code you shared is referencing, it just lists all the Processors/Cores on the system. The only difference in the registry is the Component Information binary value, which isn't very helpful.

    It'd be nice if there were unique identifiers to tie each processor to the physical Microprocessor, but I don't think I see one at that spot in the registry.

    There must be some way to get at that information, perhaps via some mechanism like "wmis".

  4. YoshiEnVerde commented on May 5, 2017

    @YoshiEnVerde
    Collaborator

    OSHI's MO for data recovery is always to go directly to the SO's data first, then (if that wasn't possible) call one of the SO's APIs for the data, and then go for any util library that might be common to find preinstalled.

    Also, the tool gives precedence to solutions that won't require admin/root equivalent privileges to ones that would require them.

    That's why most Windows stats are recovered directly from the registry if possible.
    However, some info is recovered from WMI...

  5. saintGH commented on May 5, 2017

    @saintGH
    Author

    I did notice WMIUtil, and also noticed the command is "wmic" not "wmis" as I had previous stated. Getting my acronyms mixed up.

    I just noticed that you can run:

    C:\Users\Administrator>wmic cpu get SocketDesignation

    From the command line and get:

    SocketDesignation
    Proc 1
    Proc 2

    Meaning, there's two processors on my server. When I run against my single-CPU workstation I get:

    SocketDesignation
    SOCKET 1

  6. saintGH commented on May 5, 2017

    @saintGH
    Author

    Just playing around, looks like the following would work:

    public int getCpuCount() {
    return WmiUtil.selectStringsFrom((String)null, "Win32_Processor", "SocketDesignation", (String)null).size();
    }

    Coincidentally I also need the Core Speed, which I was able to do with the following:

    public long getCPUSpeed() {
    return WmiUtil.selectUint32From((String)null, "Win32_Processor", "MaxClockSpeed", (String)null).longValue();
    }

    My naming conventions aren't the best (was just playing around), but this does seem to work...

  7. dbwiddis commented on May 5, 2017

    @dbwiddis
    Member

    I'm happy to grab processor info from WMI rather than the registry.

    But as I understand we're actually looking for 3 layers of data here? Chip, core, and virtual processor (which really is only intended to model hyperthreading)?

  8. YoshiEnVerde commented on May 5, 2017

    @YoshiEnVerde
    Collaborator

    Yup. That's pretty much it

  9. dbwiddis commented on May 5, 2017

    @dbwiddis
    Member

    We could probably add a getSocketCount() method.

  10. added
    new feature
    PR welcome
    on May 5, 2017
  11. YoshiEnVerde commented on May 5, 2017

    @YoshiEnVerde
    Collaborator

    As a stopgap method, that would work pretty well.
    I'd love to come back to this for the next major release, and actually try to parse WMI's data on the processors to have the Hardware Interface actually show each processor as a separate CentralProcessor object

  12. hakanai commented on May 5, 2017

    @hakanai

    The MS terminology for this was something like "physical package", presumably because some CPUs aren't socketed.

  13. dbwiddis commented on May 5, 2017

    @dbwiddis
    Member

    Here's how you can count it on Linux (not suggesting we use awk but pointing out the line prefixes involved!

    cat /proc/cpuinfo | \
    awk -v FS=':' '                                       \
      /^physical id/ { if(nb_cpu<$2)  { nb_cpu=$2 } }     \
      /^cpu cores/   { if(nb_cores<$2){ nb_cores=$2 } }   \
      /^processor/   { if(nb_units<$2){ nb_units=$2 } }   \
      /^model name/  { model=$2 }                         \
                                                          \
      END{                                                \
       nb_cpu=(nb_cpu+1);                                 \
       nb_units=(nb_units+1);                             \
                                                          \
       print "CPU model:",model;                          \
       print nb_cpu,"CPU,",nb_cores,"physical cores per CPU, total",nb_units,"logical CPU units" \
     }'
    

    Output:

    CPU model:  Intel(R) Xeon(R) CPU           X5650  @ 2.67GHz
    2 CPU,  6 physical cores per CPU, total 24 logical CPU units
    
  14. saintGH commented on May 8, 2017

    @saintGH
    Author

    Beauty. For my immediate purposes, I'll just utilize the WmiUtil class to poll the information I need.

    Thanks everyone!

  15. dbwiddis commented on May 9, 2017

    @dbwiddis
    Member

    Taking notes in case this ever gets done. For macOS, system_profiler SPHardwareDataType has an entry Number of Processors that represents the physical package. (My system has 1 processor, 4 cores (physical cpu), and 8 logical cpu.)

  16. dbwiddis commented on May 9, 2017

    @dbwiddis
    Member

    FreeBSD, we can parse the dmesg log:

    FreeBSD/SMP: Multiprocessor System Detected: 4 CPUs
    FreeBSD/SMP: 1 package(s) x 4 core(s)
    

    Also it appears our current parsing of sysctl kern.sched.topology_spec may have that -- count the number of group level 2's. The below is an example of a 2-package system:

    kern.sched.topology_spec: <groups>
     <group level="1" cache-level="0">
      <cpu count="8" mask="ff">0, 1, 2, 3, 4, 5, 6, 7</cpu>
      <children>
       <group level="2" cache-level="2">
        <cpu count="4" mask="f">0, 1, 2, 3</cpu>
       </group>
       <group level="2" cache-level="2">
        <cpu count="4" mask="f0">4, 5, 6, 7</cpu>
       </group>
      </children>
     </group>
    </groups>
    
  17. dbwiddis commented on May 9, 2017

    @dbwiddis
    Member

    Solaris: existing kstat on cpu_info, look for chip_id

  18. added
    good first issue
    on Oct 21, 2017
  19. added this to the 3.5.0 milestone on Apr 14, 2018
  20. self-assigned this
    on Apr 15, 2018
  21. added
    in progress
    on Apr 15, 2018
  22. saintGH commented on Apr 16, 2018

    @saintGH
    Author

    Fantastic, thanks!

  23. removed
    in progress
    on Jul 13, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

PR welcomegood first issuenew feature

Type

No type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions