Darryl L. Pierce
2010-Apr-19 18:42 UTC
[Ovirt-devel] [PATCH matahari] Replaces the existing HAL code for ProcessorAgent with udev.
Stripped out the HAL support code and replaced with calls to udev. The remainder of the code to extract CPU details parses through the /proc/cpuinfo file since udev/sysfs will not return such information. Signed-off-by: Darryl L. Pierce <dpierce at redhat.com> --- configure.ac | 3 +- src/Makefile.am | 4 +- src/processors.cpp | 94 +++++++++++++++++++++++++++++++++++++++++---------- 3 files changed, 79 insertions(+), 22 deletions(-) diff --git a/configure.ac b/configure.ac index 9102c66..c51c23e 100644 --- a/configure.ac +++ b/configure.ac @@ -8,7 +8,7 @@ AM_INIT_AUTOMAKE AC_PROG_CXX AC_PROG_CC AC_PROG_CPP -AC_CHECK_HEADERS([arpa/inet.h netinet/in.h sys/ioctl.h sys/socket.h unistd.h]) +AC_CHECK_HEADERS([arpa/inet.h netinet/in.h sys/ioctl.h sys/socket.h unistd.h libudev.h]) # Checks for typedefs, structures, and compiler characteristics. AC_HEADER_STDBOOL @@ -19,6 +19,7 @@ PKG_CHECK_MODULES(DBUS, dbus-1 >= 1.2.12) PKG_CHECK_MODULES(HAL, hal >= 0.5.12) PKG_CHECK_MODULES(LIBVIRT, libvirt >= 0.6.2) PKG_CHECK_MODULES(PCRE, libpcre >= 7.8) +PKG_CHECK_MODULES(UDEV, libudev >= 145) # Checks for library functions. AC_CHECK_FUNCS([gethostname inet_ntoa socket]) diff --git a/src/Makefile.am b/src/Makefile.am index ee30443..b4668b9 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -1,4 +1,4 @@ -INCLUDES = -I$(top_srcdir)/src/qmf/com/redhat/matahari $(HAL_CFLAGS) $(LIBVIRT_CFLAGS) $(PCRE_CFLAGS) +INCLUDES = -I$(top_srcdir)/src/qmf/com/redhat/matahari $(HAL_CFLAGS) $(LIBVIRT_CFLAGS) $(PCRE_CFLAGS) $(UDEV_CFLAGS) sbin_PROGRAMS = matahari @@ -41,6 +41,6 @@ CLEANFILES = $(generated_file_list) $(first) matahari_CPPFLAGS = -fno-strict-aliasing matahari_LDFLAGS = -L/usr/local/lib -matahari_LDADD = -lqmf $(HAL_LIBS) $(LIBVIRT_LIBS) $(PCRE_LIBS) +matahari_LDADD = -lqmf $(HAL_LIBS) $(LIBVIRT_LIBS) $(PCRE_LIBS) $(UDEV_LIBS) dist_pkgdata_DATA = schema.xml diff --git a/src/processors.cpp b/src/processors.cpp index 4317b1f..30a2837 100644 --- a/src/processors.cpp +++ b/src/processors.cpp @@ -17,25 +17,19 @@ * also available at http://www.gnu.org/copyleft/gpl.html. */ -#include <iostream> +#include "processors.h" #include <fstream> -#include <sstream> -#include <string> -#include <vector> -#include <stdexcept> - -#include <hal/libhal.h> - +#include <iostream> #include <pcre.h> -#include "hal.h" -#include "processors.h" +// TODO remove this wrapper once rhbz#583747 is fixed +extern "C" { +#include <libudev.h> +} using namespace std; namespace _qmf = qmf::com::redhat::matahari; -extern DBusError dbus_error; - void ProcessorsAgent::setup(ManagementAgent* agent, Manageable* parent) { @@ -43,17 +37,79 @@ ProcessorsAgent::setup(ManagementAgent* agent, Manageable* parent) management_object = new _qmf::Processors(agent, this, parent); agent->addObject(management_object); - LibHalContext* context = get_hal_ctx(); + int core_count = 0; + string model = "unknown"; + + struct udev* udev = udev_new(); + struct udev_enumerate* enumerator = udev_enumerate_new(udev); + + udev_enumerate_add_match_property(enumerator, "DRIVER", "processor"); + if(!udev_enumerate_scan_devices(enumerator)) + { + struct udev_list_entry* entries = udev_enumerate_get_list_entry(enumerator); + struct udev_list_entry* entry; + + udev_list_entry_foreach(entry, entries) + { + core_count++; + } + } + + udev_enumerate_unref(enumerator); + udev_unref(udev); + + ifstream input("/proc/cpuinfo"); + if(input.is_open()) + { + string regexstr = "(.*\\S)\\s*:\\s*(\\S.*)"; + int expected = 3; + int found[expected * 3]; + const char* pcre_error; + int pcre_error_offset; + pcre* regex; + bool done = false; + bool started = false; + + regex = pcre_compile(regexstr.c_str(), 0, &pcre_error, &pcre_error_offset, NULL); + if(!regex) { throw runtime_error("Unable to compile regular expression."); } + + while(!input.eof() && !done) + { + string line; + + getline(input, line); + int match = pcre_exec(regex, NULL, line.c_str(), line.length(), + 0, PCRE_NOTEMPTY,found, expected * 3); - int num_results; - char** processors = libhal_find_device_by_capability(context,"processor", &num_results, &dbus_error); + if(match == expected) + { + string name = line.substr(found[2], found[3] - found[2]); + string value = line.substr(found[4], found[5] - found[4]); - if (!processors) - throw runtime_error("Error: could not query processors via HAL."); + // if we're at a second processor and we've already started, then we're done + if (name == "processor") + { + if (started) + { + done = true; + } + else + { + started = true; + } + } + else + { + if(name == "model name") model = value; + } + } + } + input.close(); + } // populate the managed object's values - management_object->set_model(libhal_device_get_property_string(context, processors[0], "info.product", &dbus_error)); - management_object->set_cores(num_results); + management_object->set_model(model); + management_object->set_cores(core_count); } void -- 1.6.6.1
Ian Main
2010-Apr-20 16:23 UTC
[Ovirt-devel] [Matahari] [PATCH matahari] Replaces the existing HAL code for ProcessorAgent with udev.
On Mon, 2010-04-19 at 14:42 -0400, Darryl L. Pierce wrote:> Stripped out the HAL support code and replaced with calls to udev. The > remainder of the code to extract CPU details parses through the > /proc/cpuinfo file since udev/sysfs will not return such information.Your formatting is different from the rest of the codebase.. it's getting all mixed up in there.. Otherwise ACK. :) Ian> Signed-off-by: Darryl L. Pierce <dpierce at redhat.com> > --- > configure.ac | 3 +- > src/Makefile.am | 4 +- > src/processors.cpp | 94 +++++++++++++++++++++++++++++++++++++++++---------- > 3 files changed, 79 insertions(+), 22 deletions(-) > > diff --git a/configure.ac b/configure.ac > index 9102c66..c51c23e 100644 > --- a/configure.ac > +++ b/configure.ac > @@ -8,7 +8,7 @@ AM_INIT_AUTOMAKE > AC_PROG_CXX > AC_PROG_CC > AC_PROG_CPP > -AC_CHECK_HEADERS([arpa/inet.h netinet/in.h sys/ioctl.h sys/socket.h unistd.h]) > +AC_CHECK_HEADERS([arpa/inet.h netinet/in.h sys/ioctl.h sys/socket.h unistd.h libudev.h]) > > # Checks for typedefs, structures, and compiler characteristics. > AC_HEADER_STDBOOL > @@ -19,6 +19,7 @@ PKG_CHECK_MODULES(DBUS, dbus-1 >= 1.2.12) > PKG_CHECK_MODULES(HAL, hal >= 0.5.12) > PKG_CHECK_MODULES(LIBVIRT, libvirt >= 0.6.2) > PKG_CHECK_MODULES(PCRE, libpcre >= 7.8) > +PKG_CHECK_MODULES(UDEV, libudev >= 145) > > # Checks for library functions. > AC_CHECK_FUNCS([gethostname inet_ntoa socket]) > diff --git a/src/Makefile.am b/src/Makefile.am > index ee30443..b4668b9 100644 > --- a/src/Makefile.am > +++ b/src/Makefile.am > @@ -1,4 +1,4 @@ > -INCLUDES = -I$(top_srcdir)/src/qmf/com/redhat/matahari $(HAL_CFLAGS) $(LIBVIRT_CFLAGS) $(PCRE_CFLAGS) > +INCLUDES = -I$(top_srcdir)/src/qmf/com/redhat/matahari $(HAL_CFLAGS) $(LIBVIRT_CFLAGS) $(PCRE_CFLAGS) $(UDEV_CFLAGS) > > sbin_PROGRAMS = matahari > > @@ -41,6 +41,6 @@ CLEANFILES = $(generated_file_list) $(first) > > matahari_CPPFLAGS = -fno-strict-aliasing > matahari_LDFLAGS = -L/usr/local/lib > -matahari_LDADD = -lqmf $(HAL_LIBS) $(LIBVIRT_LIBS) $(PCRE_LIBS) > +matahari_LDADD = -lqmf $(HAL_LIBS) $(LIBVIRT_LIBS) $(PCRE_LIBS) $(UDEV_LIBS) > > dist_pkgdata_DATA = schema.xml > diff --git a/src/processors.cpp b/src/processors.cpp > index 4317b1f..30a2837 100644 > --- a/src/processors.cpp > +++ b/src/processors.cpp > @@ -17,25 +17,19 @@ > * also available at http://www.gnu.org/copyleft/gpl.html. > */ > > -#include <iostream> > +#include "processors.h" > #include <fstream> > -#include <sstream> > -#include <string> > -#include <vector> > -#include <stdexcept> > - > -#include <hal/libhal.h> > - > +#include <iostream> > #include <pcre.h> > > -#include "hal.h" > -#include "processors.h" > +// TODO remove this wrapper once rhbz#583747 is fixed > +extern "C" { > +#include <libudev.h> > +} > > using namespace std; > namespace _qmf = qmf::com::redhat::matahari; > > -extern DBusError dbus_error; > - > void > ProcessorsAgent::setup(ManagementAgent* agent, Manageable* parent) > { > @@ -43,17 +37,79 @@ ProcessorsAgent::setup(ManagementAgent* agent, Manageable* parent) > management_object = new _qmf::Processors(agent, this, parent); > agent->addObject(management_object); > > - LibHalContext* context = get_hal_ctx(); > + int core_count = 0; > + string model = "unknown"; > + > + struct udev* udev = udev_new(); > + struct udev_enumerate* enumerator = udev_enumerate_new(udev); > + > + udev_enumerate_add_match_property(enumerator, "DRIVER", "processor"); > + if(!udev_enumerate_scan_devices(enumerator)) > + { > + struct udev_list_entry* entries = udev_enumerate_get_list_entry(enumerator); > + struct udev_list_entry* entry; > + > + udev_list_entry_foreach(entry, entries) > + { > + core_count++; > + } > + } > + > + udev_enumerate_unref(enumerator); > + udev_unref(udev); > + > + ifstream input("/proc/cpuinfo"); > + if(input.is_open()) > + { > + string regexstr = "(.*\\S)\\s*:\\s*(\\S.*)"; > + int expected = 3; > + int found[expected * 3]; > + const char* pcre_error; > + int pcre_error_offset; > + pcre* regex; > + bool done = false; > + bool started = false; > + > + regex = pcre_compile(regexstr.c_str(), 0, &pcre_error, &pcre_error_offset, NULL); > + if(!regex) { throw runtime_error("Unable to compile regular expression."); } > + > + while(!input.eof() && !done) > + { > + string line; > + > + getline(input, line); > + int match = pcre_exec(regex, NULL, line.c_str(), line.length(), > + 0, PCRE_NOTEMPTY,found, expected * 3); > > - int num_results; > - char** processors = libhal_find_device_by_capability(context,"processor", &num_results, &dbus_error); > + if(match == expected) > + { > + string name = line.substr(found[2], found[3] - found[2]); > + string value = line.substr(found[4], found[5] - found[4]); > > - if (!processors) > - throw runtime_error("Error: could not query processors via HAL."); > + // if we're at a second processor and we've already started, then we're done > + if (name == "processor") > + { > + if (started) > + { > + done = true; > + } > + else > + { > + started = true; > + } > + } > + else > + { > + if(name == "model name") model = value; > + } > + } > + } > + input.close(); > + } > > // populate the managed object's values > - management_object->set_model(libhal_device_get_property_string(context, processors[0], "info.product", &dbus_error)); > - management_object->set_cores(num_results); > + management_object->set_model(model); > + management_object->set_cores(core_count); > } > > void