For Linux systems, this is how I do it in the psutil library:
proc cpu_count_logical*(): int =
## Return the number of logical CPUs in the system.
try:
return sysconf( SC_NPROCESSORS_ONLN )
except ValueError:
# as a second fallback we try to parse /proc/cpuinfo
for line in lines(PROCFS_PATH / "cpuinfo"):
if line.toLowerAscii().startswith("processor"):
result += 1
# unknown format (e.g. amrel/sparc architectures), see:
# https://github.com/giampaolo/psutil/issues/200
# try to parse /proc/stat as a last resort
if result == 0:
for line in lines(PROCFS_PATH / "stat"):
if line.toLowerAscii().startswith("cpu"):
result += 1
# Remove one from the count for the top "cpu" line (with no digit)
# Saves us the regular expression used in the python impl
if result != 0: result -= 1
return result
proc cpu_count_physical*(): int =
## Return the number of physical cores in the system.
var mapping = initTable[int, int]()
var current_info = initTable[string, int]()
for raw_line in lines(PROCFS_PATH / "cpuinfo"):
let line = raw_line.strip().toLowerAscii()
if line == "":
# new section
if "physical id" in current_info and "cpu cores" in current_info:
mapping[current_info["physical id"]] = current_info["cpu cores"]
current_info = initTable[string, int]()
else:
# ongoing section
if line.startswith("physical id") or line.startswith("cpu cores"):
let parts = line.split("\t:")
current_info[parts[0].strip()] = parseInt(parts[1].strip())
let values = toSeq(mapping.values())
return sum(values)
I haven't implemented the windows side yet, but take a look here for similar C code.