Skip to content

CPU Cache #2190

Closed
Closed

Description

@lian-hsc

Hey,
is it possible to find out the L1, L2 and L3 cache of a processor via the API?

Activity

  1. mprins commented on Sep 20, 2022

    @mprins
    Member

    I don't think it's currently supported, (can't find it in CentralProcessor/PhysicalProcessor)
    It could be added by parsing eg. lscpu (Linux) output eg. so you are welcome to provide a pull request

    └─ $ ▶ lscpu
    ...
    Caches (sum of all):     
      L1d:                   256 KiB (8 instances)
      L1i:                   256 KiB (8 instances)
      L2:                    4 MiB (8 instances)
      L3:                    32 MiB (2 instances)
    
    

    or

    └─ $ ▶ getconf -a | grep CACHE
    LEVEL1_ICACHE_SIZE                 32768
    LEVEL1_ICACHE_ASSOC                
    LEVEL1_ICACHE_LINESIZE             64
    LEVEL1_DCACHE_SIZE                 32768
    LEVEL1_DCACHE_ASSOC                8
    LEVEL1_DCACHE_LINESIZE             64
    LEVEL2_CACHE_SIZE                  524288
    LEVEL2_CACHE_ASSOC                 8
    LEVEL2_CACHE_LINESIZE              64
    LEVEL3_CACHE_SIZE                  33554432
    LEVEL3_CACHE_ASSOC                 0
    LEVEL3_CACHE_LINESIZE              64
    LEVEL4_CACHE_SIZE                  
    LEVEL4_CACHE_ASSOC                 
    LEVEL4_CACHE_LINESIZE    
    

    I don't think there's a sysctl for OpenBSD

    @dbwiddis Might have better/other suggestions

  2. lian-hsc commented on Sep 20, 2022

    @lian-hsc
    Author

    But with that solution i need to implement it for every os in an slightly other way, right?
    At least at macOS there are sysctl's called hw.l1icachesize, hw.l1dcachesize, hw.l2cachesize and hw.l3cachesize. I don't know if the exist on OpenBSD also...

  3. dbwiddis commented on Sep 20, 2022

    @dbwiddis
    Member

    Yes, we'd need to find the information in other OS's, but I believe that should be available. The bigger question is what sort of data structure applies and how to model this. Are these caches common to all the processors? Can there be more than one of them?

    I think the answer is yes. We currently fetch logical/physical processor info on Windows from GetLogicalProcessorInformationEx and in its returned objects it includes a RelationCache identifying processors which share a cache. That provides a CACHE_RELATIONSHIP structure with data... probably a good template for an OSHI object.

    Results on Winodws, mac, and Linux are probably enough to add a cross-platform feature. We can add them from the BSDs, solaris, and AIX if they are reasonably easy to obtain there as well.

    Can you get started with the new interface method (probably getCaches() on the CentralProcessor interface) and probably a new ProcessorCache class (similar to PhysicalProcessor) that contains the data in that CACHE_RELATIONSHIP structure? If you can implement on any OS the community can help with the other ones.

  4. added
    new feature
    PR welcome
    on Sep 20, 2022
  5. dbwiddis commented on Sep 20, 2022

    @dbwiddis
    Member

    Looks like on Solaris you can parse prtpicl output.

  6. lian-hsc commented on Sep 20, 2022

    @lian-hsc
    Author

    I will start working and will try to implemnt windows, linux and macOS for the start, maby i will add some other OS later

  7. mprins commented on Sep 20, 2022

    @mprins
    Member

    OpenBSD provides it in dmesg I see, so it is probably available programmatically somewhere.

    afaik each cpu core can have these eg.

    OpenBSD 6.2 (GENERIC.MP) #0: Wed Oct  4 05:08:15 UTC 2017
        visa@octeon:/usr/src/sys/arch/octeon/compile/GENERIC.MP
    ....
    mainbus0 at root
    cpu0 at mainbus0: CN50xx CPU rev 0.1 500 MHz, Software FP emulation
    cpu0: cache L1-I 32KB 4 way D 8KB 64 way, L2 128KB 8 way
    cpu1 at mainbus0: CN50xx CPU rev 0.1 500 MHz, Software FP emulation
    cpu1: cache L1-I 32KB 4 way D 8KB 64 way, L2 128KB 8 way
    

    I bet that at some point there will/may be CPU's that have different values for different cores as well eg. big/little ARM...

  8. dbwiddis commented on Sep 20, 2022

    @dbwiddis
    Member

    @mprins it's in dmidecode -t cache (or 7) but that requires elevated permissions. But dmesg is a good backup.

  9. dbwiddis commented on Sep 20, 2022

    @dbwiddis
    Member

    afaik each cpu core can have these eg.

    Logical or physical core? And is that the same cache shared across multiple cores?

    The Windows implementation will associate each cache with one or more logical processors, so that seems reasonable to do elsewhere, if possible.

  10. dbwiddis commented on Sep 20, 2022

    @dbwiddis
    Member

    Yep, so different level caches associated with different groups of processors.... Windows handles that with the groupmasks.

    Suggestion:

    • have a Cache object with fields for level, associativity, lineSize and cacheSize. Give them an index number based on the order they are found (for lack of any other numbering system)
    • in the LogicalProcessor class add a field with a list of cache index numbers it's associated with
  11. dbwiddis commented on Sep 20, 2022

    @dbwiddis
    Member

    Linux has the data stored in sysfs, e.g., /sys/devices/system/cpu/cpu0/cache/index2/size. I think this is the best source. It may be fetchable via udev.

  12. lian-hsc commented on Sep 20, 2022

    @lian-hsc
    Author

    How can i use udev? Is there a documentation of it anywhere?

  13. dbwiddis commented on Sep 21, 2022

    @dbwiddis
    Member

    How can i use udev? Is there a documentation of it anywhere?

    Docs for Udev are at JNA: http://java-native-access.github.io/jna/5.12.1/javadoc/com/sun/jna/platform/linux/Udev.html

    For an example in OSHI, Check out readTopologyFromUdev() in the LinuxCentralProcessor class. It gets you as far as enumerating the CPUs (so up to cpu0 in the example above). You may need to do another layer of enumeration of CACHE below that to get the index #s, and then get the size property from them.

    There's also a fallback non-UDEV option to just navigate the sysfs files in readTopologyFromSysfs(). You'll probably have to do both. :)

    You can probably piggyback on these existing methods and insert your new code within them.

  14. 1 remaining item

  15. lian-hsc commented on Sep 21, 2022

    @lian-hsc
    Author

    My MacBook (M2) has with sysctl the following output:

    hw.perflevel0.l1icachesize: 196608
    hw.perflevel0.l1dcachesize: 131072
    hw.perflevel0.l2cachesize: 16777216
    hw.perflevel1.l1icachesize: 131072
    hw.perflevel1.l1dcachesize: 65536
    hw.perflevel1.l2cachesize: 4194304
    hw.cacheconfig: 8 1 4 0 0 0 0 0 0 0
    hw.cachesize: 3703324672 65536 4194304 0 0 0 0 0 0 0
    hw.cachelinesize: 128
    hw.l1icachesize: 131072
    hw.l1dcachesize: 65536
    hw.l2cachesize: 4194304
    

    system_profiler doesn't include Cache at all.

  16. dbwiddis commented on Sep 21, 2022

    @dbwiddis
    Member

    Glad you've got an M2. I'll buy one as soon as it's available in 16" size :)

    Looks like plenty to go with here. machdep isn't available, nor system profiler. Don't see anything about an L3 cache though.

    I looked in the IO Registry and didn't see anything there, either.

  17. lian-hsc commented on Sep 21, 2022

    @lian-hsc
    Author

    So it seems like there are many differences between each operating system and even different processors. Therefore, it is very likely that some information is only available on some operating systems or some processors.

  18. dbwiddis commented on Sep 21, 2022

    @dbwiddis
    Member

    That's generally true of most things on OSHI. :)

  19. lian-hsc commented on Sep 25, 2022

    @lian-hsc
    Author

    Hey @dbwiddis,

    I haven't had much time lately and I won't have much time in the near future either.
    So it won't be possible for me to really work on this.
    So it would make sense to add the "up for grabs" tag.

  20. added
    up for grabs
    hacktoberfest
    on Sep 25, 2022
  21. dbwiddis commented on Sep 28, 2022

    @dbwiddis
    Member

    I created an API and implemented on Windows in #2198.

    Originally mapped all the processors to cores which is accurate in Windows but duplicates data on Linux, and some platforms (macOS, AIX) only list summary values. So I decided to just list those summary values. Will probably just add a second line (per cache type) for the (unique) efficiency core data for hybrid cores.

    I'm not sure where cache size of 56 bytes is coming from (I got 52 running in my local VM). I'm guessing that the hypervisors aren't populating the appropriate virtual CPUID bits.

    The L3 associativity of 11 is also really strange, but that's what the data says...

     Caches:
      ProcessorCache [L3 Unified, cacheSize=56, 11-way associativity, lineSize=64]
      ProcessorCache [L2 Unified, cacheSize=56, 16-way associativity, lineSize=64] (per core)
      ProcessorCache [L1 Data, cacheSize=56, 8-way associativity, lineSize=64] (per core)
      ProcessorCache [L1 Instruction, cacheSize=56, 8-way associativity, lineSize=64] (per core)
    
  22. dbwiddis commented on Oct 2, 2022

    @dbwiddis
    Member

    Linux is done.

     Caches:
      ProcessorCache [L3 Unified, cacheSize=49152, 12-way associativity, lineSize=64]
      ProcessorCache [L2 Unified, cacheSize=1280, 20-way associativity, lineSize=64] (per core)
      ProcessorCache [L1 Data, cacheSize=48, 12-way associativity, lineSize=64] (per core)
      ProcessorCache [L1 Instruction, cacheSize=32, 8-way associativity, lineSize=64] (per core)
    
  23. dbwiddis commented on Oct 2, 2022

    @dbwiddis
    Member

    AIX seems to only show L2 cache...

    > lsattr -EOl L2cache0
    #size
    512
    

    So I just hardcoded the values

     Caches:
      ProcessorCache [L4 Unified, cacheSize=268435456, 8-way associativity, lineSize=128]
      ProcessorCache [L3 Unified, cacheSize=41943040, 8-way associativity, lineSize=128]
      ProcessorCache [L2 Unified, cacheSize=524288, 8-way associativity, lineSize=128] (per core)
      ProcessorCache [L1 Data, cacheSize=65536, 8-way associativity, lineSize=128] (per core)
      ProcessorCache [L1 Instruction, cacheSize=32768, 8-way associativity, lineSize=128] (per core)
    
  24. dbwiddis commented on Oct 3, 2022

    @dbwiddis
    Member

    Current status:

    • Windows: Done, but weird values from VM/Hypervisor. Need testing on metal.
    • Linux: Done. Some weird values on VM but enough metal testing to validate.
    • macOS: Can be easily done with sysctl, probably do that last
    • Solaris: Apparently possible with prtpicl -v on x86 but I can't get any output on my VM. Nothing on SPARC.
    • FreeBSD: If lscpu is installed we can get cache sizes but no other info
    • OpenBSD: Done using dmesg
    • AIX: Only L2 size available in OS; hardcoded values for POWER7, POWER8, and POWER9 architectures.
  25. removed
    up for grabs
    PR welcome
    hacktoberfest
    on Oct 4, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    new feature

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions