Commit 83d462e1 authored by Christian Kellner's avatar Christian Kellner
Browse files

Add cpu information to GbbSystemInfo

parent 7906e506
......@@ -42,6 +42,8 @@ info_txt(int argc, char **argv)
g_autofree char *bios_vendor;
g_autofree char *bios_version;
g_autofree char *bios_date;
g_auto(GStrv) cpu_info;
guint cpu_number;
g_autofree char *os_type;
g_autofree char *os_kernel;
g_autofree char *gnome_version;
......@@ -57,6 +59,8 @@ info_txt(int argc, char **argv)
"bios-date", &bios_date,
"bios-version", &bios_version,
"bios_vendor", &bios_vendor,
"cpu-number", &cpu_number,
"cpu-info", &cpu_info,
"os-type", &os_type,
"os-kernel", &os_kernel,
"gnome-version", &gnome_version,
......@@ -69,6 +73,11 @@ info_txt(int argc, char **argv)
g_print(" Vendor: %s\n", sys_vendor);
g_print(" Version: %s\n", product_version);
g_print(" Name: %s\n", product_name);
g_print(" CPU%s:\n", cpu_number > 1 ? "s" : "");
g_print(" Number: %u\n", cpu_number);
for (int i = 0; i < g_strv_length(cpu_info); i++) {
g_print(" Info [%d]: %s\n", i, cpu_info[i]);
}
g_print(" Bios:\n");
g_print(" Version: %s\n", bios_version);
g_print(" Date: %s\n", bios_date);
......
......@@ -21,6 +21,10 @@ struct _GbbSystemInfo {
char *bios_date;
char *bios_vendor;
/* CPU*/
guint cpu_number;
GStrv cpu_info;
/* Software */
/* OS */
......@@ -44,6 +48,9 @@ enum {
PROP_BIOS_VENDOR,
PROP_BIOS_DATE,
PROP_CPU_NUMBER,
PROP_CPU_INFO,
PROP_OS_TYPE,
PROP_OS_KERNEL,
......@@ -75,6 +82,8 @@ gbb_system_info_finalize(GbbSystemInfo *info)
g_free(info->bios_date);
g_free(info->bios_vendor);
g_strfreev(info->cpu_info);
g_free(info->os_type);
g_free(info->os_kernel);
......@@ -115,6 +124,14 @@ gbb_system_info_get_property (GObject *object, guint prop_id, GValue *value, GPa
g_value_set_string(value, info->bios_vendor);
break;
case PROP_CPU_NUMBER:
g_value_set_uint(value, info->cpu_number);
break;
case PROP_CPU_INFO:
g_value_set_boxed(value, info->cpu_info);
break;
case PROP_OS_TYPE:
g_value_set_string(value, info->os_type);
break;
......@@ -181,6 +198,18 @@ gbb_system_info_class_init (GbbSystemInfoClass *klass)
NULL, NULL,
NULL,
G_PARAM_READABLE));
g_object_class_install_property (gobject_class,
PROP_CPU_NUMBER,
g_param_spec_uint ("cpu-number",
NULL, NULL,
0, G_MAXUINT, 0,
G_PARAM_READABLE));
g_object_class_install_property (gobject_class,
PROP_CPU_INFO,
g_param_spec_boxed("cpu-info",
NULL, NULL,
G_TYPE_STRV,
G_PARAM_READABLE));
g_object_class_install_property (gobject_class,
PROP_OS_KERNEL,
g_param_spec_string ("os-kernel",
......@@ -275,6 +304,70 @@ read_kernel_version(void)
return g_strdup(comps[2]);
}
static GStrv
read_cpu_info(guint *ncpus)
{
g_autofree char *data = read_sysfs_string("/proc/cpuinfo");
g_autoptr(GHashTable) cpus = NULL;
g_auto(GStrv) kv = NULL;
GStrv models = NULL;
gsize i, n = 0;
gpointer key, val;
GHashTableIter iter;
if (data == NULL) {
models = g_new(char *, 2);
models[0] = g_strdup("Unknown");
models[1] = NULL;
*ncpus = 1;
return models;
}
cpus = g_hash_table_new (g_str_hash, g_str_equal);
kv = g_strsplit(data, "\n", -1);
for (i = 0; i < g_strv_length(kv); i++) {
const char *entry = kv[i];
const char *pos;
if (!g_str_has_prefix(entry, "model name")) {
continue;
}
pos = g_strstr_len(entry, -1, ":");
if (pos == NULL) {
g_log_structured (G_LOG_DOMAIN, G_LOG_LEVEL_WARNING,
"MESSAGE_ID", "3a2690a163c5465bb9ba0cab229bf3cf",
"MESSAGE", "Format error: while parsing '/proc/cpuinfo': expected a ':'.");
continue;
}
pos++;
while (*pos == ' ' && *pos != '\n') {
pos++;
}
val = g_hash_table_lookup(cpus, pos);
if (val == NULL) {
g_hash_table_insert(cpus, (gpointer) pos, GINT_TO_POINTER(1));
} else {
val = GINT_TO_POINTER(GPOINTER_TO_INT(val) + 1);
g_hash_table_replace(cpus, (gpointer) pos, val);
}
}
n = i = 0;
g_hash_table_iter_init (&iter, cpus);
models = (GStrv) g_new(char *, g_hash_table_size(cpus) + 1);
while (g_hash_table_iter_next (&iter, &key, &val)) {
int k = GPOINTER_TO_INT(val);
models[i] = g_strdup_printf("%s [%d]", (char *) key, k);
n += k;
i++;
}
models[i] = NULL;
*ncpus = n;
return models;
}
static void gbb_system_info_init (GbbSystemInfo *info)
{
......@@ -284,7 +377,7 @@ static void gbb_system_info_init (GbbSystemInfo *info)
&info->gnome_date);
info->os_type = get_os_type();
info->os_kernel = read_kernel_version();
info->cpu_info = read_cpu_info(&info->cpu_number);
}
GbbSystemInfo *
......@@ -320,6 +413,20 @@ gbb_system_info_to_json (const GbbSystemInfo *info, JsonBuilder *builder)
json_builder_add_string_value(builder, info->bios_vendor);
json_builder_end_object(builder);
}
json_builder_set_member_name(builder, "cpu");
{
json_builder_begin_object(builder);
json_builder_set_member_name(builder, "number");
json_builder_add_int_value(builder, info->cpu_number);
json_builder_set_member_name(builder, "info");
json_builder_begin_array(builder);
for (int i = 0; i < g_strv_length(info->cpu_info); i++) {
json_builder_add_string_value(builder, info->cpu_info[i]);
}
json_builder_end_array(builder);
json_builder_end_object(builder);
}
json_builder_end_object(builder);
}
json_builder_set_member_name(builder, "software");
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment