aboutsummaryrefslogtreecommitdiff
path: root/modules/devices
diff options
context:
space:
mode:
Diffstat (limited to 'modules/devices')
-rw-r--r--modules/devices/arm/processor.c106
-rw-r--r--modules/devices/cpu_util.c162
-rw-r--r--modules/devices/ppc/processor.c94
-rw-r--r--modules/devices/x86/processor.c107
4 files changed, 189 insertions, 280 deletions
diff --git a/modules/devices/arm/processor.c b/modules/devices/arm/processor.c
index d83be9ec..b1923a74 100644
--- a/modules/devices/arm/processor.c
+++ b/modules/devices/arm/processor.c
@@ -18,6 +18,7 @@
#include "hardinfo.h"
#include "devices.h"
+#include "cpu_util.h"
#include "arm_data.h"
#include "arm_data.c"
@@ -34,50 +35,6 @@ static const gchar *arm_mode_str[] = {
"A32 on A64",
};
-static gchar* get_cpu_str(const gchar* file, gint cpuid) {
- gchar *tmp0 = NULL;
- gchar *tmp1 = NULL;
- tmp0 = g_strdup_printf("/sys/devices/system/cpu/cpu%d/%s", cpuid, file);
- g_file_get_contents(tmp0, &tmp1, NULL, NULL);
- g_free(tmp0);
- return tmp1;
-}
-
-static gint get_cpu_int(const char* item, int cpuid) {
- gchar *fc = NULL;
- int ret = 0;
- fc = get_cpu_str(item, cpuid);
- if (fc) {
- ret = atol(fc);
- g_free(fc);
- }
- return ret;
-}
-
-gchar *byte_order_str() {
-#if G_BYTE_ORDER == G_LITTLE_ENDIAN
- return _("Little Endian");
-#else
- return _("Big Endian");
-#endif
-}
-
-int processor_has_flag(gchar * strflags, gchar * strflag)
-{
- gchar **flags;
- gint ret = 0;
- if (strflags == NULL || strflag == NULL)
- return 0;
- flags = g_strsplit(strflags, " ", 0);
- ret = g_strv_contains((const gchar * const *)flags, strflag);
- g_strfreev(flags);
- return ret;
-}
-
-#ifndef PROC_CPUINFO
-#define PROC_CPUINFO "/proc/cpuinfo"
-#endif
-
GSList *
processor_scan(void)
{
@@ -182,9 +139,6 @@ processor_scan(void)
processor = (Processor *) pi->data;
/* strings can't be null or segfault later */
-#define STRIFNULL(f,cs) if (processor->f == NULL) processor->f = g_strdup(cs);
-#define UNKIFNULL(f) STRIFNULL(f, _("(Unknown)") )
-#define EMPIFNULL(f) STRIFNULL(f, "")
STRIFNULL(model_name, _("ARM Processor") );
EMPIFNULL(flags);
UNKIFNULL(cpu_implementer);
@@ -199,23 +153,12 @@ processor_scan(void)
processor->cpu_architecture, processor->model_name);
UNKIFNULL(decoded_name);
- /* topo */
- processor->package_id = get_cpu_str("topology/physical_package_id", processor->id);
- processor->core_id = get_cpu_str("topology/core_id", processor->id);
- UNKIFNULL(package_id);
- UNKIFNULL(core_id);
-
- /* freq */
- processor->scaling_driver = get_cpu_str("cpufreq/scaling_driver", processor->id);
- processor->scaling_governor = get_cpu_str("cpufreq/scaling_governor", processor->id);
- UNKIFNULL(scaling_driver);
- UNKIFNULL(scaling_governor);
- processor->transition_latency = get_cpu_int("cpufreq/cpuinfo_transition_latency", processor->id);
- processor->cpukhz_cur = get_cpu_int("cpufreq/scaling_cur_freq", processor->id);
- processor->cpukhz_min = get_cpu_int("cpufreq/scaling_min_freq", processor->id);
- processor->cpukhz_max = get_cpu_int("cpufreq/scaling_max_freq", processor->id);
- if (processor->cpukhz_max)
- processor->cpu_mhz = processor->cpukhz_max / 1000;
+ /* topo & freq */
+ processor->cpufreq = cpufreq_new(processor->id);
+ processor->cputopo = cputopo_new(processor->id);
+
+ if (processor->cpufreq->cpukhz_max)
+ processor->cpu_mhz = processor->cpufreq->cpukhz_max / 1000;
else
processor->cpu_mhz = 0.0f;
@@ -269,39 +212,8 @@ processor_get_detailed_info(Processor *processor)
tmp_part = (char*)arm_part(processor->cpu_implementer, processor->cpu_part);
tmp_arch = (char*)arm_arch_more(processor->cpu_architecture);
- tmp_topology = g_strdup_printf(
- "[%s]\n"
- "%s=%d\n"
- "%s=%s\n"
- "%s=%s\n",
- _("Topology"),
- _("ID"), processor->id,
- _("Socket"), processor->package_id,
- _("Core"), processor->core_id);
-
- if (processor->cpukhz_min || processor->cpukhz_max || processor->cpukhz_cur) {
- tmp_cpufreq = g_strdup_printf(
- "[%s]\n"
- "%s=%d %s\n"
- "%s=%d %s\n"
- "%s=%d %s\n"
- "%s=%d %s\n"
- "%s=%s\n"
- "%s=%s\n",
- _("Frequency Scaling"),
- _("Minimum"), processor->cpukhz_min, _("kHz"),
- _("Maximum"), processor->cpukhz_max, _("kHz"),
- _("Current"), processor->cpukhz_cur, _("kHz"),
- _("Transition Latency"), processor->transition_latency, _("ns"),
- _("Governor"), processor->scaling_governor,
- _("Driver"), processor->scaling_driver);
- } else {
- tmp_cpufreq = g_strdup_printf(
- "[%s]\n"
- "%s=%s\n",
- _("Frequency Scaling"),
- _("Driver"), processor->scaling_driver);
- }
+ tmp_topology = cputopo_section_str(processor->cputopo);
+ tmp_cpufreq = cpufreq_section_str(processor->cpufreq);
ret = g_strdup_printf("[%s]\n"
"%s=%s\n" /* linux name */
diff --git a/modules/devices/cpu_util.c b/modules/devices/cpu_util.c
new file mode 100644
index 00000000..bf03c0ac
--- /dev/null
+++ b/modules/devices/cpu_util.c
@@ -0,0 +1,162 @@
+/*
+ * HardInfo - Displays System Information
+ * Copyright (C) 2003-2006 Leandro A. F. Pereira <leandro@hardinfo.org>
+ * This file by Burt P. <pburt0@gmail.com>
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, version 2.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
+ */
+
+#include <string.h>
+#include "hardinfo.h"
+#include "cpu_util.h"
+
+gchar *byte_order_str() {
+#if G_BYTE_ORDER == G_LITTLE_ENDIAN
+ return _("Little Endian");
+#else
+ return _("Big Endian");
+#endif
+}
+
+int processor_has_flag(gchar * strflags, gchar * strflag)
+{
+ gchar **flags;
+ gint ret = 0;
+ if (strflags == NULL || strflag == NULL)
+ return 0;
+ flags = g_strsplit(strflags, " ", 0);
+ ret = g_strv_contains((const gchar * const *)flags, strflag);
+ g_strfreev(flags);
+ return ret;
+}
+
+gchar* get_cpu_str(const gchar* file, gint cpuid) {
+ gchar *tmp0 = NULL;
+ gchar *tmp1 = NULL;
+ tmp0 = g_strdup_printf("/sys/devices/system/cpu/cpu%d/%s", cpuid, file);
+ g_file_get_contents(tmp0, &tmp1, NULL, NULL);
+ g_free(tmp0);
+ return tmp1;
+}
+
+gint get_cpu_int(const char* item, int cpuid) {
+ gchar *fc = NULL;
+ int ret = 0;
+ fc = get_cpu_str(item, cpuid);
+ if (fc) {
+ ret = atol(fc);
+ g_free(fc);
+ }
+ return ret;
+}
+
+cpufreq_data *cpufreq_new(gint id)
+{
+ cpufreq_data *cpufd;
+ cpufd = malloc(sizeof(cpufreq_data));
+ if (cpufd) {
+ memset(cpufd, 0, sizeof(cpufreq_data));
+ cpufd->id = id;
+ cpufreq_update(cpufd, 0);
+ }
+ return cpufd;
+}
+
+void cpufreq_update(cpufreq_data *cpufd, int cur_only)
+{
+ if (cpufd) {
+ cpufd->cpukhz_cur = get_cpu_int("cpufreq/scaling_cur_freq", cpufd->id);
+ if (cur_only) return;
+ cpufd->scaling_driver = get_cpu_str("cpufreq/scaling_driver", cpufd->id);
+ cpufd->scaling_governor = get_cpu_str("cpufreq/scaling_governor", cpufd->id);
+ cpufd->transition_latency = get_cpu_int("cpufreq/cpuinfo_transition_latency", cpufd->id);
+ cpufd->cpukhz_min = get_cpu_int("cpufreq/scaling_min_freq", cpufd->id);
+ cpufd->cpukhz_max = get_cpu_int("cpufreq/scaling_max_freq", cpufd->id);
+ }
+}
+
+void cpufreq_free(cpufreq_data *cpufd)
+{
+ if (cpufd) {
+ g_free(cpufd->scaling_driver);
+ g_free(cpufd->scaling_governor);
+ }
+ g_free(cpufd);
+}
+
+cpu_topology_data *cputopo_new(gint id)
+{
+ cpu_topology_data *cputd;
+ cputd = malloc(sizeof(cpu_topology_data));
+ if (cputd) {
+ memset(cputd, 0, sizeof(cpu_topology_data));
+ cputd->id = id;
+ cputd->socket_id = get_cpu_int("topology/physical_package_id", id);
+ cputd->core_id = get_cpu_int("topology/core_id", id);
+ }
+ return cputd;
+
+}
+
+void cputopo_free(cpu_topology_data *cputd)
+{
+ g_free(cputd);
+}
+
+
+gchar *cpufreq_section_str(cpufreq_data *cpufd)
+{
+ if (cpufd == NULL)
+ return g_strdup("");
+
+ if (cpufd->cpukhz_min || cpufd->cpukhz_max || cpufd->cpukhz_cur) {
+ return g_strdup_printf(
+ "[%s]\n"
+ "%s=%d %s\n"
+ "%s=%d %s\n"
+ "%s=%d %s\n"
+ "%s=%d %s\n"
+ "%s=%s\n"
+ "%s=%s\n",
+ _("Frequency Scaling"),
+ _("Minimum"), cpufd->cpukhz_min, _("kHz"),
+ _("Maximum"), cpufd->cpukhz_max, _("kHz"),
+ _("Current"), cpufd->cpukhz_cur, _("kHz"),
+ _("Transition Latency"), cpufd->transition_latency, _("ns"),
+ _("Governor"), cpufd->scaling_governor,
+ _("Driver"), cpufd->scaling_driver);
+ } else {
+ return g_strdup_printf(
+ "[%s]\n"
+ "%s=%s\n",
+ _("Frequency Scaling"),
+ _("Driver"), cpufd->scaling_driver);
+ }
+}
+
+gchar *cputopo_section_str(cpu_topology_data *cputd)
+{
+ if (cputd == NULL)
+ return g_strdup("");
+
+ return g_strdup_printf(
+ "[%s]\n"
+ "%s=%d\n"
+ "%s=%d\n"
+ "%s=%d\n",
+ _("Topology"),
+ _("ID"), cputd->id,
+ _("Socket"), cputd->socket_id,
+ _("Core"), cputd->core_id);
+}
diff --git a/modules/devices/ppc/processor.c b/modules/devices/ppc/processor.c
index 59ebab01..4d25c8f8 100644
--- a/modules/devices/ppc/processor.c
+++ b/modules/devices/ppc/processor.c
@@ -18,38 +18,7 @@
#include "hardinfo.h"
#include "devices.h"
-
-static gchar* get_cpu_str(const gchar* file, gint cpuid) {
- gchar *tmp0 = NULL;
- gchar *tmp1 = NULL;
- tmp0 = g_strdup_printf("/sys/devices/system/cpu/cpu%d/%s", cpuid, file);
- g_file_get_contents(tmp0, &tmp1, NULL, NULL);
- g_free(tmp0);
- return tmp1;
-}
-
-static gint get_cpu_int(const char* item, int cpuid) {
- gchar *fc = NULL;
- int ret = 0;
- fc = get_cpu_str(item, cpuid);
- if (fc) {
- ret = atol(fc);
- g_free(fc);
- }
- return ret;
-}
-
-gchar *byte_order_str() {
-#if G_BYTE_ORDER == G_LITTLE_ENDIAN
- return _("Little Endian");
-#else
- return _("Big Endian");
-#endif
-}
-
-#ifndef PROC_CPUINFO
-#define PROC_CPUINFO "/proc/cpuinfo"
-#endif
+#include "cpu_util.h"
GSList *
processor_scan(void)
@@ -146,29 +115,15 @@ processor_scan(void)
processor = (Processor *) pi->data;
/* strings can't be null or segfault later */
-#define STRIFNULL(f,cs) if (processor->f == NULL) processor->f = g_strdup(cs);
-#define UNKIFNULL(f) STRIFNULL(f, _("(Unknown)") )
-#define EMPIFNULL(f) STRIFNULL(f, "")
STRIFNULL(model_name, _("POWER Processor") );
UNKIFNULL(revision);
- /* topo */
- processor->package_id = get_cpu_str("topology/physical_package_id", processor->id);
- processor->core_id = get_cpu_str("topology/core_id", processor->id);
- UNKIFNULL(package_id);
- UNKIFNULL(core_id);
-
- /* freq */
- processor->scaling_driver = get_cpu_str("cpufreq/scaling_driver", processor->id);
- processor->scaling_governor = get_cpu_str("cpufreq/scaling_governor", processor->id);
- UNKIFNULL(scaling_driver);
- UNKIFNULL(scaling_governor);
- processor->transition_latency = get_cpu_int("cpufreq/cpuinfo_transition_latency", processor->id);
- processor->cpukhz_cur = get_cpu_int("cpufreq/scaling_cur_freq", processor->id);
- processor->cpukhz_min = get_cpu_int("cpufreq/scaling_min_freq", processor->id);
- processor->cpukhz_max = get_cpu_int("cpufreq/scaling_max_freq", processor->id);
- if (processor->cpukhz_max)
- processor->cpu_mhz = processor->cpukhz_max / 1000;
+ /* topo & freq */
+ processor->cpufreq = cpufreq_new(processor->id);
+ processor->cputopo = cputopo_new(processor->id);
+
+ if (processor->cpufreq->cpukhz_max)
+ processor->cpu_mhz = processor->cpufreq->cpukhz_max / 1000;
}
@@ -181,39 +136,8 @@ processor_get_detailed_info(Processor *processor)
{
gchar *tmp_cpufreq, *tmp_topology, *ret;
- tmp_topology = g_strdup_printf(
- "[%s]\n"
- "%s=%d\n"
- "%s=%s\n"
- "%s=%s\n",
- _("Topology"),
- _("ID"), processor->id,
- _("Socket"), processor->package_id,
- _("Core"), processor->core_id);
-
- if (processor->cpukhz_min || processor->cpukhz_max || processor->cpukhz_cur) {
- tmp_cpufreq = g_strdup_printf(
- "[%s]\n"
- "%s=%d %s\n"
- "%s=%d %s\n"
- "%s=%d %s\n"
- "%s=%d %s\n"
- "%s=%s\n"
- "%s=%s\n",
- _("Frequency Scaling"),
- _("Minimum"), processor->cpukhz_min, _("kHz"),
- _("Maximum"), processor->cpukhz_max, _("kHz"),
- _("Current"), processor->cpukhz_cur, _("kHz"),
- _("Transition Latency"), processor->transition_latency, _("ns"),
- _("Governor"), processor->scaling_governor,
- _("Driver"), processor->scaling_driver);
- } else {
- tmp_cpufreq = g_strdup_printf(
- "[%s]\n"
- "%s=%s\n",
- _("Frequency Scaling"),
- _("Driver"), processor->scaling_driver);
- }
+ tmp_topology = cputopo_section_str(processor->cputopo);
+ tmp_cpufreq = cpufreq_section_str(processor->cpufreq);
ret = g_strdup_printf("[%s]\n"
"%s=%s\n" /* model */
diff --git a/modules/devices/x86/processor.c b/modules/devices/x86/processor.c
index 2ca40136..1a904216 100644
--- a/modules/devices/x86/processor.c
+++ b/modules/devices/x86/processor.c
@@ -18,6 +18,7 @@
#include "hardinfo.h"
#include "devices.h"
+#include "cpu_util.h"
/*
* This function is partly based on x86cpucaps
@@ -214,50 +215,6 @@ fail:
g_free(endpoint);
}
-int processor_has_flag(gchar * strflags, gchar * strflag)
-{
- gchar **flags;
- gint ret = 0;
- if (strflags == NULL || strflag == NULL)
- return 0;
- flags = g_strsplit(strflags, " ", 0);
- ret = g_strv_contains((const gchar * const *)flags, strflag);
- g_strfreev(flags);
- return ret;
-}
-
-static gchar* get_cpu_str(const gchar* file, gint cpuid) {
- gchar *tmp0 = NULL;
- gchar *tmp1 = NULL;
- tmp0 = g_strdup_printf("/sys/devices/system/cpu/cpu%d/%s", cpuid, file);
- g_file_get_contents(tmp0, &tmp1, NULL, NULL);
- g_free(tmp0);
- return tmp1;
-}
-
-static gint get_cpu_int(const char* item, int cpuid) {
- gchar *fc = NULL;
- int ret = 0;
- fc = get_cpu_str(item, cpuid);
- if (fc) {
- ret = atol(fc);
- g_free(fc);
- }
- return ret;
-}
-
-gchar *byte_order_str() {
-#if G_BYTE_ORDER == G_LITTLE_ENDIAN
- return _("Little Endian");
-#else
- return _("Big Endian");
-#endif
-}
-
-#ifndef PROC_CPUINFO
-#define PROC_CPUINFO "/proc/cpuinfo"
-#endif
-
GSList *processor_scan(void)
{
GSList *procs = NULL, *l = NULL;
@@ -354,27 +311,12 @@ GSList *processor_scan(void)
g_strchug(processor->pm);
}
-#define STRIFNULL(f,cs) if (processor->f == NULL) processor->f = g_strdup(cs);
-#define UNKIFNULL(f) STRIFNULL(f, _("(Unknown)") )
-#define EMPIFNULL(f) STRIFNULL(f, "")
-
- /* topo */
- processor->package_id = get_cpu_str("topology/physical_package_id", processor->id);
- processor->core_id = get_cpu_str("topology/core_id", processor->id);
- UNKIFNULL(package_id);
- UNKIFNULL(core_id);
-
- /* freq */
- processor->scaling_driver = get_cpu_str("cpufreq/scaling_driver", processor->id);
- processor->scaling_governor = get_cpu_str("cpufreq/scaling_governor", processor->id);
- UNKIFNULL(scaling_driver);
- UNKIFNULL(scaling_governor);
- processor->transition_latency = get_cpu_int("cpufreq/cpuinfo_transition_latency", processor->id);
- processor->cpukhz_cur = get_cpu_int("cpufreq/scaling_cur_freq", processor->id);
- processor->cpukhz_min = get_cpu_int("cpufreq/scaling_min_freq", processor->id);
- processor->cpukhz_max = get_cpu_int("cpufreq/scaling_max_freq", processor->id);
- if (processor->cpukhz_max)
- processor->cpu_mhz = processor->cpukhz_max / 1000;
+ /* topo & freq */
+ processor->cpufreq = cpufreq_new(processor->id);
+ processor->cputopo = cputopo_new(processor->id);
+
+ if (processor->cpufreq->cpukhz_max)
+ processor->cpu_mhz = processor->cpufreq->cpukhz_max / 1000;
}
fclose(cpuinfo);
@@ -771,39 +713,8 @@ gchar *processor_get_detailed_info(Processor * processor)
tmp_pm = processor_get_capabilities_from_flags(processor->pm);
cache_info = __cache_get_info_as_string(processor);
- tmp_topology = g_strdup_printf(
- "[%s]\n"
- "%s=%d\n"
- "%s=%s\n"
- "%s=%s\n",
- _("Topology"),
- _("ID"), processor->id,
- _("Socket"), processor->package_id,
- _("Core"), processor->core_id);
-
- if (processor->cpukhz_min || processor->cpukhz_max || processor->cpukhz_cur) {
- tmp_cpufreq = g_strdup_printf(
- "[%s]\n"
- "%s=%d %s\n"
- "%s=%d %s\n"
- "%s=%d %s\n"
- "%s=%d %s\n"
- "%s=%s\n"
- "%s=%s\n",
- _("Frequency Scaling"),
- _("Minimum"), processor->cpukhz_min, _("kHz"),
- _("Maximum"), processor->cpukhz_max, _("kHz"),
- _("Current"), processor->cpukhz_cur, _("kHz"),
- _("Transition Latency"), processor->transition_latency, _("ns"),
- _("Governor"), processor->scaling_governor,
- _("Driver"), processor->scaling_driver);
- } else {
- tmp_cpufreq = g_strdup_printf(
- "[%s]\n"
- "%s=%s\n",
- _("Frequency Scaling"),
- _("Driver"), processor->scaling_driver);
- }
+ tmp_topology = cputopo_section_str(processor->cputopo);
+ tmp_cpufreq = cpufreq_section_str(processor->cpufreq);
ret = g_strdup_printf("[%s]\n"
"%s=%s\n"