Skip to content

Instantly share code, notes, and snippets.

@kosmolot
Created January 23, 2018 13:10
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kosmolot/87612f430c29141c3dd37ce23f2db1b2 to your computer and use it in GitHub Desktop.
Save kosmolot/87612f430c29141c3dd37ce23f2db1b2 to your computer and use it in GitHub Desktop.
/* cpu-info.vala
*
* requirements:
* * lscpu (if not exists, query() always throws an error)
*
* deps:
* * glib-2.0
* * gobject-2.0
*
* Written in 2018 by kosmolot (kosmolot17@yandex.com)
*
* To the extent possible under law, the author have dedicated all copyright
* and related and neighboring rights to this software to the public domain
* worldwide. This software is distributed without any warranty.
*
* You should have received a copy of the CC0 legalcode along with this
* work. If not, see <http://creativecommons.org/publicdomain/zero/1.0/>.
*/
public class CpuInfo : Object {
private static string ARCHITECTURE = "Architecture";
private static string MODEL_NAME = "Model name";
private static string SOCKETS = "Socket(s)";
private static string CORES_PER_SOCKET = "Core(s) per socket";
private static string THREADS_PER_CORE = "Thread(s) per core";
private static string MAX_SPEED = "CPU max MHz";
public static CpuInfo query () throws QueryError {
string output;
try {
Process.spawn_command_line_sync("lscpu", out output);
} catch (SpawnError err) {
throw new QueryError.FAILED(@"Failed to spawn process: $(err.message)");
}
CpuInfo? result = new CpuInfo();
result.architecture = extract_value(ARCHITECTURE, output);
result.model = extract_value(MODEL_NAME, output);
uint64 socks = uint64.parse( extract_value(SOCKETS, output) );
uint64 cps = uint64.parse( extract_value(CORES_PER_SOCKET, output) );
uint64 tpc = uint64.parse( extract_value(THREADS_PER_CORE, output) );
double speed = double.parse( extract_value(MAX_SPEED, output) );
result.physical_cores = socks * cps;
result.logical_cores = result.physical_cores * tpc;
result.max_speed = speed;
return result;
}
private static string extract_value (string target, string str) throws QueryError {
Regex regex;
try {
string escaped = Regex.escape_string(target);
regex = new Regex(@"^$escaped:[ \\t]*(.+)[ \\t]*$$", RegexCompileFlags.MULTILINE);
} catch (RegexError err) {
throw new QueryError.FAILED(@"Failed to create regex: $(err.message)");
}
MatchInfo match;
bool matched = regex.match(str, 0, out match);
if (!matched) throw new QueryError.FAILED(@"Failed to fetch info '$target'");
string? sub_match = match.fetch(1);
if (sub_match == null) throw new QueryError.FAILED(@"Failed to fetch info '$target'");
return sub_match;
}
public string architecture { get; set; }
public string model { get; set; }
public uint64 physical_cores { get; set; }
public uint64 logical_cores { get; set; }
public double max_speed { get; set; }
public void print () {
GLib.print("> Architecture: %s\n", architecture);
GLib.print("> CPU model: %s\n", model);
GLib.print("> Physical cores: %u\n", (uint) physical_cores);
GLib.print("> Logical cores: %u\n", (uint) logical_cores);
GLib.print("> Max clock rate: %.2f MHz\n", max_speed);
}
}
public errordomain QueryError {
FAILED
}
void main () {
try {
CpuInfo.query().print();
} catch (QueryError err) {
print("Failed to fetch cpu information: %s\n", err.message);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment