Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Check if hyperthreading enabled on not in a system #231

Open
swatisehgal opened this issue Mar 1, 2021 · 2 comments
Open

Check if hyperthreading enabled on not in a system #231

swatisehgal opened this issue Mar 1, 2021 · 2 comments

Comments

@swatisehgal
Copy link

Currently there is no API to check if hyperthreading is enabled or disabled on a system. There are ways of figuring out if hyperthreading is enabled on a system or not by something like the following:

func isHyperthreadingEnabled(snapShotOptions *option.Option) (bool, error) {
	cpuInfo, err := ghw.CPU(ghwHandler.snapShotOptions)
	if err != nil {
		return false, fmt.Errorf("Error obtaining CPU Info from GHW snapshot: %v", err)
	}
	threadsPerCore := cpuInfo.TotalThreads / cpuInfo.TotalCores
	if threadsPerCore == 1 {
		//Hyperthreading disabled
		return false, nil
	}
	//Hyperthreading is enabled
	return true, nil
}

OR

func (ghwHandler GHWHandler) isHyperthreadingEnabled(snapShotOptions *option.Option) (bool, error) {
	cpuInfo, err := ghw.CPU(ghwHandler.snapShotOptions)
	if err != nil {
		return false, fmt.Errorf("Error obtaining CPU Info from GHW snapshot: %v", err)
	}
	return contains(cpuInfo.Processors[0].Capabilities, "ht"), nil
}

// contains checks if a string is present in a slice
func contains(s []string, str string) bool {
	for _, v := range s {
		if v == str {
			return true
		}
	}

	return false
}

Would be nice if there was an API to obtain this information directly from a ghw snapshot.

@ffromani
Copy link
Collaborator

ffromani commented Mar 2, 2021

Sharing some thoughts as ghw contributor.

Since 2018 the linux kernel exposes a pseudofile (https://www.kernel.org/doc/Documentation/ABI/testing/sysfs-devices-system-cpu) which reports if smt (aka ht) is enabled globally.
OTOH, ghw exposes the ht flag per-processor.

I think this is the key decision here: is (or we should model as) smt a per-processor or per-system setting?
IOW, can we have SMT enabled only on some cpu complexes and not in other?

At best of my knowledge, if smt is disabled via BIOS or kernel settings, then the processor will NOT report the "ht" flag, thus the two methods yield equivalent results.

@ffromani
Copy link
Collaborator

ffromani commented Mar 2, 2021

I did a bit of extra research and what we have so far is: we also need to distinguish between firmware settings (aka HT enabled/disabled in BIOS or UEFI) and kernel settings.

  1. the HT flag, if present, is telling us that the HW is ht capable. This is what ghw reports today, reading from /proc/cpuinfo.
  2. the "nosmt" kernel parameters seems (didn't dive deep enough yet) to do something very close to just offline the hyperthread sibilings. While this is of course relevant, is also always possible to offline cores manually, so we need to dive deeper to understand the difference here.

So in practice:

  1. if the BIOS and the kernel aligns, checking for the presence of HT flags tells us if the system is running with SMT enabled - or not.
  2. if the BIOS and the kernel DO NOT align, things get interesting. The only meaningful case is: HT enabled in firmware, nosmt given. In this case, this function wrongly reports the HT state. For userspace, HT is enabled only if ALL hardware, firmware and kernel allow + enable SMT. So in this case the computation will be wrong.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants