-3

This is my first post on StackOverflow so I apologize if I have broken some rules.

My Professor, (not a great one) has just told us we have 24 hours to write a program under 10 lines of code that verifies if your processor architecture provides support for hyperthreading operation.

To say the least, I don't know what he wants as well as even how to begin. I believe he wants it in C++ but got upset when I asked that question and didn't tell me what language to do it in.

When I told him I didn't understand what he was asking he got very upset and told me that I must not have been paying attention. I honestly have been and even have been taking notes. I guess this Operating Systems course just isn't sinking in with me.

I have tried my own research by reading the intel manual, but no luck (Also it is 5000+ pages). I have googled around to understand the basics of hyper-threading, that you can run 2 processes at the same time (If that is correct), but have no idea how to turn that into code.

Any help would be amazing!

  • 2
    All the critics about your professor and the situation add little to the question (That is probably the reason of some negative score). Also, your understanding of hyperthreading is wrong: hyperthreading is about a processor able to run several threads from a single process sharing resources. Your explanation is related to multi-cores, not hyperthreading. Also: Is this question really related with "Intel" (AMD/ARM/etc also have hyper-threading)? Could you share some tries of code you did for this problem? – Adrian Maire Sep 25 '20 at 14:05
  • 1
    It doesn't matter that the ISDM is big. You only need the page that says hyper threading feature flag is bit 28 of EDX after executing CPUID with an input of 1. Though, it is unclear whether doing that is the intended solution. – harold Sep 25 '20 at 14:06
  • 2
    Did Professor mention the maximum length of each line ? ;-) – Jeffrey Sep 25 '20 at 14:07
  • 2
    Did your professor specify an OS? On Linux you can just read the CPU feature flags from `/proc/cpuinfo`, and other OSs may have other ways of querying this. – Useless Sep 25 '20 at 15:13
  • @harold Tbh, in asm checking that would be exactly 9-10 lines of code. – Swift - Friday Pie Sep 25 '20 at 15:15

1 Answers1

2

How to verify if your processor supports hyper-threading? ... in C++

There is no standard way to do this in C++.

If you know the CPU that the program will run on, then you may find a way described in the documentation of the CPU. For example, 486 and later x86 processors have the CPUID instruction. The relevant information that you need is "Number of logical processors per physical processor package (CPUID.1.EBX[23:16])". If this value is greater than 1, then Hyper-Threading or similar is enabled.

To execute instructions such as CPUID, you need to write in assembly language rather than C++. That said, if you are within an operating system, then it may offer a nice API to access this functionality (see comments for examples).

A less CPU-specific way to implement this is to read the SMBIOS if that is supported on your target system. Here, you can divide processor thread count by core count to get the same value as above. You can, for example, see the implementation of the dmidecode program on Linux to see how that can be read.

eerorika
  • 232,697
  • 12
  • 197
  • 326
  • 1
    On Windows we have (with MS or Intel) an intrinsic for cpuid (__cpuid/__cpuidex?) – Swift - Friday Pie Sep 25 '20 at 15:18
  • On Windows, you can also use [`GetLogicalProcessorInformation()`](https://learn.microsoft.com/en-us/windows/win32/api/winnt/ns-winnt-system_logical_processor_information) or [`GetLogicalProcessorInformationEx()`](https://learn.microsoft.com/en-us/windows/win32/api/sysinfoapi/nf-sysinfoapi-getlogicalprocessorinformationex) to check if any physical CPUs have multiple logical cores on them. – Remy Lebeau Sep 25 '20 at 16:37
  • @Swift-FridayPie: With GNU C, there's `cpuid.h` for `__get_cpuid`. [How do I call "cpuid" in Linux?](https://stackoverflow.com/q/14266772). Or also a plain `__cpuid()` function that doesn't check the requested leaf number against max-leaf first. [Intrinsics for CPUID like informations?](https://stackoverflow.com/q/17758409). And also a `__cpuidex` that takes an array pointer as input. So probably compatible with the MS interface. – Peter Cordes Sep 25 '20 at 17:47
  • Note that GCC's `cpuid.h` functions don't require you to be "within an operating system". They compile to `asm("":::)` statements that inline the `cpuid` instruction into your code, and would work perfectly fine in freestanding code such as a kernel. The Windows (or MSVC?) library functions of the same names might compile to function calls and would only work under Windows, though. Perhaps, "some C++ implementations offer a nice API...", leaving the hosted vs. freestanding part of "C++ implementation" unspecified. – Peter Cordes Sep 26 '20 at 01:00