Darryl L. Pierce
2010-Apr-21 20:10 UTC
[Ovirt-devel] [PATCH matahari] Created a new platform-abstraction layer named Platform.
It provides an initial implementation, LinuxPlatform, which is by default included in the build. Later code will refactor the build process to include the appropriate implementation depending on the target platform. Signed-off-by: Darryl L. Pierce <dpierce at redhat.com> --- src/Makefile.am | 4 ++ src/linux_platform.cpp | 117 ++++++++++++++++++++++++++++++++++++++++++++++++ src/linux_platform.h | 35 ++++++++++++++ src/platform.cpp | 35 ++++++++++++++ src/platform.h | 62 +++++++++++++++++++++++++ src/processors.cpp | 93 ++------------------------------------ 6 files changed, 258 insertions(+), 88 deletions(-) create mode 100644 src/linux_platform.cpp create mode 100644 src/linux_platform.h create mode 100644 src/platform.cpp create mode 100644 src/platform.h diff --git a/src/Makefile.am b/src/Makefile.am index 7606e20..e4f394d 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -21,9 +21,13 @@ matahari_SOURCES = \ hal.h \ host.cpp \ host.h \ + linux_platform.cpp \ + linux_platform.h \ main.cpp \ nic.cpp \ nic.h \ + platform.cpp \ + platform.h \ processors.cpp \ processors.h diff --git a/src/linux_platform.cpp b/src/linux_platform.cpp new file mode 100644 index 0000000..26418f5 --- /dev/null +++ b/src/linux_platform.cpp @@ -0,0 +1,117 @@ +/* linux_platform.cpp - Copyright (C) 2010 Red Hat, Inc. + * Written by Darryl L. Pierce <dpierce at redhat.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 of the License. + * + * 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 Street, Fifth Floor, Boston, + * MA 02110-1301, USA. A copy of the GNU General Public License is + * also available at http://www.gnu.org/copyleft/gpl.html. + */ + +#include <fstream> +#include <iostream> +#include <pcre.h> +#include <stdexcept> + +// TODO remove this wrapper once rhbz#583747 is fixed +extern "C" { +#include <libudev.h> +} + +#include "linux_platform.h" + +LinuxPlatform::LinuxPlatform() +{ + 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++; + } + set_number_of_cores(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); + + if(match == expected) + { + string name = line.substr(found[2], found[3] - found[2]); + string value = line.substr(found[4], found[5] - found[4]); + + // 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") set_processor_model(value); + } + } + } + input.close(); + } +} + +double +LinuxPlatform::get_load_average() const +{ + double load_average; + ifstream input; + + input.open("/proc/loadavg", ios::in); + input >> load_average; + input.close(); + + return load_average; +} diff --git a/src/linux_platform.h b/src/linux_platform.h new file mode 100644 index 0000000..13116df --- /dev/null +++ b/src/linux_platform.h @@ -0,0 +1,35 @@ +#ifndef __LINUX_PLATFORM_H +#define __LINUX_PLATFORM_H + +/* linux_platform.h - Copyright (C) 2010 Red Hat, Inc. + * Written by Darryl L. Pierce <dpierce at redhat.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 of the License. + * + * 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 Street, Fifth Floor, Boston, + * MA 02110-1301, USA. A copy of the GNU General Public License is + * also available at http://www.gnu.org/copyleft/gpl.html. + */ + +#include <string> +#include "platform.h" + +class LinuxPlatform : public Platform +{ + public: + LinuxPlatform(); + virtual ~LinuxPlatform() {} + + virtual double get_load_average() const; +}; + +#endif diff --git a/src/platform.cpp b/src/platform.cpp new file mode 100644 index 0000000..69f528b --- /dev/null +++ b/src/platform.cpp @@ -0,0 +1,35 @@ +/* platform.cpp - Copyright (C) 2010 Red Hat, Inc. + * Written by Darryl L. Pierce <dpierce at redhat.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 of the License. + * + * 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 Street, Fifth Floor, Boston, + * MA 02110-1301, USA. A copy of the GNU General Public License is + * also available at http://www.gnu.org/copyleft/gpl.html. + */ + +#include "platform.h" +#include "linux_platform.h" + +Platform* Platform::_instance = 0; + +Platform* +Platform::instance() +{ + // TODO this is where we decide which implementation to use + if(!_instance) + { + _instance = new LinuxPlatform; + } + + return _instance; +} diff --git a/src/platform.h b/src/platform.h new file mode 100644 index 0000000..514ad20 --- /dev/null +++ b/src/platform.h @@ -0,0 +1,62 @@ +#ifndef __PLATFORM_H +#define __PLATFORM_H + +/* platform.h - Copyright (C) 2010 Red Hat, Inc. + * Written by Darryl L. Pierce <dpierce at redhat.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 of the License. + * + * 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 Street, Fifth Floor, Boston, + * MA 02110-1301, USA. A copy of the GNU General Public License is + * also available at http://www.gnu.org/copyleft/gpl.html. + */ + +#include <string> + +using namespace std; + +/* + * Platform defines a type that provides platform-specific details. + * + * Implementations provide the specific details needed by the + * various agents at runtime. + */ +class Platform +{ + private: + static Platform* _instance; + + string processor_model; + unsigned int number_of_cores; + + protected: + Platform() {} + virtual~ Platform() {} + + void set_processor_model(const string model) { processor_model = model; } + void set_number_of_cores(const int number) { number_of_cores = number; } + + public: + // the singleton instance + static Platform* instance(); + + // returns text describing the processor model. + string get_processor_model() const { return processor_model; } + + // returns the number of cores in the processor. + int get_number_of_cores() const { return number_of_cores; } + + // returns the load average for the platform + virtual double get_load_average() const = 0; +}; + +#endif diff --git a/src/processors.cpp b/src/processors.cpp index cbc2979..13c492d 100644 --- a/src/processors.cpp +++ b/src/processors.cpp @@ -18,14 +18,7 @@ */ #include "processors.h" -#include <fstream> -#include <iostream> -#include <pcre.h> - -// TODO remove this wrapper once rhbz#583747 is fixed -extern "C" { -#include <libudev.h> -} +#include "platform.h" using namespace std; namespace _qmf = qmf::com::redhat::matahari; @@ -37,79 +30,10 @@ ProcessorsAgent::setup(ManagementAgent* agent, Manageable* parent) management_object = new _qmf::Processors(agent, this, parent); agent->addObject(management_object); - 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; + Platform* platform = Platform::instance(); - 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); - - if(match == expected) - { - string name = line.substr(found[2], found[3] - found[2]); - string value = line.substr(found[4], found[5] - found[4]); - - // 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(model); - management_object->set_cores(core_count); + management_object->set_model(platform->get_processor_model()); + management_object->set_cores(platform->get_number_of_cores()); } void @@ -121,12 +45,5 @@ ProcessorsAgent::update(void) const void ProcessorsAgent::update_load_averages(void) const { - double load_average; - ifstream input; - - input.open("/proc/loadavg", ios::in); - input >> load_average; - input.close(); - - management_object->set_load_average(load_average); + management_object->set_load_average(Platform::instance()->get_load_average()); } -- 1.6.6.1
Ian Main
2010-Apr-23 18:15 UTC
[Ovirt-devel] [PATCH matahari] Created a new platform-abstraction layer named Platform.
On Wed, 2010-04-21 at 16:10 -0400, Darryl L. Pierce wrote:> It provides an initial implementation, LinuxPlatform, which is by > default included in the build. Later code will refactor the build > process to include the appropriate implementation depending on the > target platform. > > Signed-off-by: Darryl L. Pierce <dpierce at redhat.com>Spiffy, I like it. ACK Ian
Possibly Parallel Threads
- [PATCH matahari] Replaces the existing HAL code for ProcessorAgent with udev.
- [PATCH matahari] Moves the CPU properties into the Host API space.
- [PATCH matahari] Created the NetworkDevice agent.
- [PATCH matahari] Moving QMF functionality into a transport layer.
- [PATCH matahari] Refactored the Host agent.