Skip to content
64 changes: 22 additions & 42 deletions src/Resources/CPU.vala
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@ public class Monitor.CPU : Object {
public uint ? revision;

public string ? model_name;
public uint physical_cpus;
public uint physical_cores_per_cpu;
public uint logical_threads_per_core;
public string ? model;
public string ? family;
public string ? microcode;
Expand Down Expand Up @@ -77,7 +80,7 @@ public class Monitor.CPU : Object {

parse_cpuinfo ();

model_name = get_cpu_info ();
model_name = get_cpu_info (out physical_cpus, out physical_cores_per_cpu, out logical_threads_per_core);

debug ("CPU name: %s", model_name);

Expand Down Expand Up @@ -256,27 +259,26 @@ public class Monitor.CPU : Object {
}
}

// straight from elementary about-plug
private string ? get_cpu_info () {
private string ? get_cpu_info (out uint cpus, out uint cores_per_cpu, out uint threads_per_core) {
unowned GTop.SysInfo ? info = GTop.glibtop_get_sysinfo ();

if (info == null) {
return null;
}

var counts = new Gee.HashMap<string, uint> ();
const string[] KEYS = { "model name", "cpu", "Processor" };
cpus = 1;
cores_per_cpu = 1;
threads_per_core = 1;

string ? model = null;

var core_ids = new Gee.HashSet<string> ();
var physical_ids = new Gee.HashSet<string> ();

for (int i = 0; i < info.ncpu; i++) {
unowned GLib.HashTable<string, string> values = info.cpuinfo[i].values;
string ? model = null;
foreach (var key in KEYS) {
model = values.lookup (key);

if (model != null) {
break;
}
}
model = values.lookup ("model name");

if (model == null) {
debug ("Try ARM decoding for CPU %d", i);
Expand All @@ -286,40 +288,18 @@ public class Monitor.CPU : Object {
}
}

string ? core_count = values.lookup ("cpu cores");
if (core_count != null) {
counts.@set (model, int.parse (core_count));
continue;
}

if (!counts.has_key (model)) {
counts.@set (model, 1);
} else {
counts.@set (model, counts.@get (model) + 1);
}
core_ids.add (values.lookup ("core id"));
physical_ids.add (values.lookup ("physical id"));
}

if (counts.size == 0) {
return null;
if (model == null) {
return "unknown";
}

string result = "";
foreach (var cpu in counts.entries) {
if (result.length > 0) {
result += "\n";
}

if (cpu.@value == 2) {
result += _("Dual-Core %s").printf ((cpu.key));
} else if (cpu.@value == 4) {
result += _("Quad-Core %s").printf ((cpu.key));
} else if (cpu.@value == 6) {
result += _("Hexa-Core %s").printf ((cpu.key));
} else {
result += "%u\u00D7 %s ".printf (cpu.@value, (cpu.key));
}
}
cpus = physical_ids.size;
cores_per_cpu = core_ids.size;
threads_per_core = (uint) info.ncpu / cores_per_cpu / cpus;

return Utils.Strings.beautify (result);
return Utils.Strings.beautify (model);
}
}
5 changes: 5 additions & 0 deletions src/Views/SystemView/SystemCPUInfoPopover.vala
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,11 @@ public class Monitor.SystemCPUInfoPopover : Gtk.Box {
activate_on_single_click = false
};

var total_cores = cpu.physical_cores_per_cpu * cpu.physical_cpus;
var total_threads = cpu.logical_threads_per_core * total_cores;
listbox.append (label (_("Total CPUs: %u").printf (cpu.physical_cpus)));
listbox.append (label (_("Total cores: %u").printf (total_cores)));
listbox.append (label (_("Total threads: %u").printf (total_threads)));
listbox.append (label (_("Model:") + " " + cpu.model));
listbox.append (label (_("Family:") + " " + cpu.family));
listbox.append (label (_("Microcode ver.:") + " " + cpu.microcode));
Expand Down
Loading