/soc/2012/sanket/statscollector-2.x.y: 2c7e5bcb5d8b: Change CPU/...
Sanket Agarwal
sanket at soc.pidgin.im
Tue Jul 10 00:36:22 EDT 2012
Changeset: 2c7e5bcb5d8b67f7c5eddccc75d3a71bb82aad74
Author: Sanket Agarwal <sanket at soc.pidgin.im>
Date: 2012-05-17 11:42 +0000
Branch: soc.2012.statscollector
URL: http://hg.pidgin.im/soc/2012/sanket/statscollector-2.x.y/rev/2c7e5bcb5d8b
Description:
Change CPU/OS info semantics
Instead of using /proc/cpuinfo (which is very LINUX centric) we use a
generic approach to POSIX compliant systems - uname(). It returns us not
only OS name but also version/flavor information. Though it's not very
universally accepted, but that's the tradeoff!
For windows we'll use native functions, and here I have used WOW64 check
to see if a 32 bit application runs on 64 bit windows (major current
concern with pidgin).
Lastly MAC will be handled, and any other OS (though very few left?) will
have to wait for their implementtion.
diffstat:
pidgin/plugins/statscollector.c | 245 +++++++++++++++++++++------------------
1 files changed, 131 insertions(+), 114 deletions(-)
diffs (299 lines):
diff --git a/pidgin/plugins/statscollector.c b/pidgin/plugins/statscollector.c
--- a/pidgin/plugins/statscollector.c
+++ b/pidgin/plugins/statscollector.c
@@ -16,6 +16,10 @@
#include <glib.h>
#include <gtkplugin.h>
+#ifdef _WIN32
+#include<windows.h>
+#endif
+
#define STATS_MAJOR_VERSION 1
#define STATS_MINOR_VERSION 0
@@ -23,6 +27,7 @@
GHashTable *stats_acc_ht, *stats_plugins_ht;
int save_timer = 0;
+/* Types of Operating Systems */
enum OS_TYPES {WINDOWS, APPLE, UNIX};
static void
@@ -86,28 +91,127 @@
}
static xmlnode *
+get_os_xml(){
+
+ xmlnode *os;
+ char *str_os;
+
+#if defined __USE_POSIX
+ struct utsname os_utsname;
+ xmlnode *os_sysname_xml, *os_release_xml, *os_version_xml;
+#elif defined _WIN32
+ OSVERSIONINFO osvi;
+ xmlnode *os_major_version, *os_minor_version, *is_wow_xml;
+#ifdef _WIN64 /* Checking if 32 bit is running over 64 bit */
+ BOOL bIsWow64;
+ typedef BOOL (WINAPI *LPFN_ISWOW64PROCESS) (HANDLE, PBOOL);
+ LPFN_ISWOW64PROCESS fnIsWow64Process;
+#endif
+#endif
+
+ /* OS specific details such as, OS name, OS version */
+ os = xmlnode_new("os");
+
+ /* So we will start with some simple CPU information that are also
+ * necessary.
+ * 1. Architecture type
+ * 2. OS name -- and any specifics if possible
+ * 3. Application/OS bit size
+ */
+
+
+
+#if defined _WIN32 && !(defined _WIN64)
+ str_os = g_strdup("WINDOWS32");
+#elif defined _WIN64
+ str_os = g_strdup("WINDOWS64");
+#elif defined __APPLE__
+ str_os = g_strdup("APPLE");
+#elif defined __linux__ && defined __USE_POSIX
+ str_os = g_strdup("LINUX");
+ /* Use uname to get linux specific information */
+#elif defined __USE_POSIX /* TODO: What defines Posix? */
+ str_os = g_stdup("POSIX");
+#else
+ str_os = g_strdup("OTHER");
+#endif
+
+ xmlnode_set_attrib(os, "id", str_os);
+
+#ifdef _WIN32 /* Detects both WIN32 and WIN 64 here */
+
+ ZeroMemory(&osvi, sizeof(OSVERSIONINFO));
+ osvi.dwOSVersionInfoSize = sizeof(OSVERSIONINFO);
+ GetVersionEx(&osvi);
+
+ os_major_version = xmlnode_new("major-version");
+ os_minor_version = xmlnode_new("minor-version");
+
+ xmlnode_insert_data(os_major_version, \
+ g_strdup_printf("%d", osvi.dwMajorVersion), -1);
+ xmlnode_insert_data(os_minor_version, \
+ g_strdup_printf("%d", osvi.dwMinorVersion), -1);
+
+ xmlnode_insert_child(os, os_major_version);
+ xmlnode_insert_child(os, os_minor_version);
+
+ is_wow_xml = xmlnode_new("is_wow64");
+
+#ifdef _WIN64 /* Check if the application is faking 32 bit on 64 bit */
+ bIsWow64 = FALSE;
+ fnIsWow64Process = (LPFN_ISWOW64PROCESS) GetProcAddress( \
+ GetModuleHandle(TEXT("kernel32")),"IsWow64Process");
+
+ if(NULL != fnIsWow64Process)
+ {
+ if (!fnIsWow64Process(GetCurrentProcess(),&bIsWow64))
+ {
+ xmlnode_insert_data(is_wow_xml, "null", -1); /* Cannot say! */
+ }
+ }
+ if(*bIsWow64)
+ xmlnode_insert_data(is_wow_xml, "true", -1);
+ else xmlnode_insert_data(is_wow_xml, "false", -1);
+#else
+ /* cannot be wow process */
+ xmlnode_insert_data(is_wow_xml, "false", -1);
+#endif /* End checking wow */
+
+ xmlnode_insert_child(os,is_wow_xml);
+
+
+#elif defined __APPLE__
+ /* Kung-foo for apple os specific details */
+#elif defined __USE_POSIX /* Covers linux et. al */
+
+ uname(&os_utsname);
+ os_sysname_xml = xmlnode_new("sysname");
+ os_release_xml = xmlnode_new("release");
+ os_version_xml = xmlnode_new("version");
+
+ xmlnode_insert_data(os_sysname_xml, g_strdup(os_utsname.sysname),-1);
+ xmlnode_insert_data(os_release_xml, g_strdup(os_utsname.release),-1);
+ xmlnode_insert_data(os_version_xml, g_strdup(os_utsname.version),-1);
+
+ xmlnode_insert_child(os, os_sysname_xml);
+ xmlnode_insert_child(os, os_release_xml);
+ xmlnode_insert_child(os, os_version_xml);
+
+#endif
+
+ return os;
+
+}
+
+static xmlnode *
get_cpuinfo_xml(){
/* Obtains a XML node containing the CPU info */
- xmlnode *root, *languages, *node, *ver, *os, *cpu, *app_arch;
+ xmlnode *root, *languages, *node, *ver, *cpu, *app_arch;
const char * const * langs;
- char *str_os, *str_app_arch, *proc_file;
- char **lines;
+ char *str_app_arch;
int i, app_bs;
- enum OS_TYPES e_os;
-
- /* CPU info variables */
- GError *error;
- char *model_name=NULL, *vendor_id=NULL, *cpu_family=NULL;
- int no_cores=0, no_proc=0;
-
- /* Temp var */
- char **t, *p1, *p2;
-
- gsize length;
- xmlnode *model_name_xml, *vendor_id_xml, *cpu_family_xml, *no_cores_xml, \
- *no_proc_xml;
root = xmlnode_new("cpuinfo");
@@ -116,116 +220,30 @@
cpu = xmlnode_new("cpu");
xmlnode_insert_child(root, cpu);
- /* Base OS */
+ xmlnode_insert_child(cpu, get_os_xml());
- os = xmlnode_new("os");
-
-#ifdef _WIN32
- e_os = WINDOWS;
-#elif defined __APPLE__
- e_os = APPLE;
-#elif defined __unix__
- e_os = UNIX;
-#endif
-
- switch(e_os){
-
- case WINDOWS:
- str_os = g_strdup("WINDOWS");
- break;
- case APPLE:
- str_os = g_strdup("APPLE");
- break;
- case UNIX:
- str_os = g_strdup("UNIX");
- break;
-
- }
-
- xmlnode_insert_data(os, str_os, -1);
- xmlnode_insert_child(cpu, os);
-
- /* Application arch */
+ /* Application 32/64 bits */
app_arch = xmlnode_new("app-arch");
app_bs = sizeof(int)*8;
if(app_bs==32) str_app_arch = g_strdup("32");
else if(app_bs==64) str_app_arch = g_strdup("64");
- purple_debug_info("STATS", "ARCH SIZE: %d\n", app_bs);
xmlnode_insert_data(app_arch, str_app_arch, -1);
xmlnode_insert_child(cpu, app_arch);
- /* Processor info */
-#ifdef __unix__
- /* Read the contents */
- g_file_get_contents("/proc/cpuinfo", &proc_file, &length, &error);
-
- /* Split it into lines */
- lines = g_strsplit(proc_file, "\n", -1);
-
- /* Search for relevant CPUinfo in our case
- * 1. Processor Family
- * 2. Vendor id
- * 3. Model Name
- * 4. CPU cores
- */
-
- for(i=0;lines[i];i++){
-
- /* Debug info */
- purple_debug_info("STATS", "CPUINFO: %s\n", lines[i]);
-
- /* Create parts */
- t = g_strsplit(lines[i], ":", -1);
-
- if(t[0] && t[1]){
-
- p1 = g_strstrip(t[0]), p2 = g_strstrip(t[1]);
-
- if(!g_strcmp0(p1, "cpu family"))
- cpu_family = p2;
- else if(!g_strcmp0(p1, "model name"))
- model_name = g_strdup(p2);
- else if(!g_strcmp0(p1, "vendor_id"))
- vendor_id = g_strdup(p2);
- else if(!g_strcmp0(p1, "cpu cores")){
- no_cores += atoi(p2);
- no_proc++;
- }
-
- }
-
- }
-
- cpu_family_xml = xmlnode_new("cpu-family");
- xmlnode_insert_data(cpu_family_xml, cpu_family, -1);
- xmlnode_insert_child(cpu, cpu_family_xml);
-
- model_name_xml = xmlnode_new("model-name");
- xmlnode_insert_data(model_name_xml, model_name, -1);
- xmlnode_insert_child(cpu, model_name_xml);
-
- vendor_id_xml = xmlnode_new("vendor-id");
- xmlnode_insert_data(vendor_id_xml, vendor_id, -1);
- xmlnode_insert_child(cpu, vendor_id_xml);
-
- no_cores_xml = xmlnode_new("cores");
- xmlnode_insert_data(no_cores_xml, g_strdup_printf("%d",no_cores), -1);
- xmlnode_insert_child(cpu, no_cores_xml);
-
- no_proc_xml = xmlnode_new("processors");
- xmlnode_insert_data(no_proc_xml, g_strdup_printf("%d",no_proc), -1);
- xmlnode_insert_child(cpu, no_proc_xml);
-
-
-#endif
-
-
-
+/* #if defined _WIN32 */
+/* */
+/* SYSTEM_INFO sysinfo; */
+/* GetSystemInfo(&sysinfo); */
+/* */
+/* purple_debug_info("STATS_WIN","%u, %u, %u, %u", sysinfo.wProcessorArchitecture, sysinfo.wProcessorLevel, sysinfo.wProcessorRevision, sysinfo.dwPageSize, sysinfo.dwProcessorType, sysinfo.dwNumberOfProcessors); */
+/* */
+/* #endif */
+/* */
/* Languages */
langs = g_get_language_names();
@@ -533,4 +551,3 @@
}
PURPLE_INIT_PLUGIN(statscollector, init_plugin, info)
-
More information about the Commits
mailing list