This pair of patches refactors the CPU identification code, and also ensures the last updated timestamp is properly set each time a heartbeat event is fired.
The number of CPUs, the number of cores per CPU, and the CPU model code are consolidated into a single function on Linux.
Signed-off-by: Darryl L. Pierce dpierce@redhat.com --- src/host.cpp | 147 +++++++++++++++++++++++++------------------------ src/host.h | 2 + src/qmf/hostagent.cpp | 1 + 3 files changed, 77 insertions(+), 73 deletions(-)
diff --git a/src/host.cpp b/src/host.cpp index 8b6cd6f..d6955a5 100644 --- a/src/host.cpp +++ b/src/host.cpp @@ -36,6 +36,18 @@ extern "C" {
using namespace std;
+typedef struct cpuinfo_ +{ + static bool initialized; + string model; + unsigned int cpus; + unsigned int cores; +} cpuinfo_t; + +bool cpuinfo_t::initialized = false; + +cpuinfo_t cpuinfo; + set<HostListener*> _listeners; unsigned int _heartbeat_sequence;
@@ -186,105 +198,94 @@ host_get_memory() return memory; }
-string -host_get_cpu_model() +void +host_get_cpu_details() { - static string model; + if(cpuinfo.initialized) return;
- if(model.empty()) - { + cpuinfo.initialized = true;
-#ifdef __linux__ - ifstream* input = new ifstream("/proc/cpuinfo"); +#if __linux__ + ifstream input("/proc/cpuinfo");
- if(input->is_open()) + 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()) { - 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; + string line;
- getline(*input, line); - int match = pcre_exec(regex, NULL, line.c_str(), line.length(), - 0, PCRE_NOTEMPTY,found, expected * 3); + 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(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 we're at a second processor and we've already started, + then we're done. + */ + if (name == "processor") + { + cpuinfo.cpus++; + if (started) { - if (started) - { - done = true; - } - else - { - started = true; - } + done = true; } else { - if(name == "model name") model = value; + started = true; } } + else if (!done) + { + if (name == "cpu cores") cpuinfo.cores = atoi(value.c_str()); + else if (name == "model name") cpuinfo.model = value; + } } - input->close(); - delete input; } -#endif - + input.close(); + cpuinfo.cpus /= cpuinfo.cores; } +#endif
- return model; }
-unsigned int -host_get_number_of_cpu_cores() +string +host_get_cpu_model() { - unsigned int cores = 0; - - if(!cores) - { + host_get_cpu_details();
-#ifdef __linux__ - 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; + return cpuinfo.model; +}
- udev_list_entry_foreach(entry, entries) - { - cores++; - } - } +unsigned int +host_get_number_of_cpus() +{ + host_get_cpu_details();
- udev_enumerate_unref(enumerator); - udev_unref(udev); -#endif + return cpuinfo.cpus; +}
- } +unsigned int +host_get_number_of_cpu_cores() +{ + host_get_cpu_details();
- return cores; + return cpuinfo.cores; }
void diff --git a/src/host.h b/src/host.h index c9e97b2..574e8b5 100644 --- a/src/host.h +++ b/src/host.h @@ -45,6 +45,8 @@ unsigned int host_get_memory();
string host_get_cpu_model();
+unsigned int host_get_number_of_cpus(); + unsigned int host_get_number_of_cpu_cores();
void host_get_load_averages(double& one, double& five, double& fifteen); diff --git a/src/qmf/hostagent.cpp b/src/qmf/hostagent.cpp index cd6d0e6..0c0170c 100644 --- a/src/qmf/hostagent.cpp +++ b/src/qmf/hostagent.cpp @@ -47,6 +47,7 @@ HostAgent::setup(ManagementAgent* agent) _management_object->set_memory(host_get_memory());
_management_object->set_cpu_model(host_get_cpu_model()); + _management_object->set_cpu_count(host_get_number_of_cpus()); _management_object->set_cpu_cores(host_get_number_of_cpu_cores()); }
ACK - looks like you've been having fun ;-)
On Wed, Jul 14, 2010 at 10:15 PM, Darryl L. Pierce dpierce@redhat.com wrote:
The number of CPUs, the number of cores per CPU, and the CPU model code are consolidated into a single function on Linux.
Signed-off-by: Darryl L. Pierce dpierce@redhat.com
src/host.cpp | 147 +++++++++++++++++++++++++------------------------ src/host.h | 2 + src/qmf/hostagent.cpp | 1 + 3 files changed, 77 insertions(+), 73 deletions(-)
diff --git a/src/host.cpp b/src/host.cpp index 8b6cd6f..d6955a5 100644 --- a/src/host.cpp +++ b/src/host.cpp @@ -36,6 +36,18 @@ extern "C" {
using namespace std;
+typedef struct cpuinfo_ +{
- static bool initialized;
- string model;
- unsigned int cpus;
- unsigned int cores;
+} cpuinfo_t;
+bool cpuinfo_t::initialized = false;
+cpuinfo_t cpuinfo;
set<HostListener*> _listeners; unsigned int _heartbeat_sequence;
@@ -186,105 +198,94 @@ host_get_memory() return memory; }
-string -host_get_cpu_model() +void +host_get_cpu_details() {
- static string model;
- if(cpuinfo.initialized) return;
- if(model.empty())
- {
- cpuinfo.initialized = true;
-#ifdef __linux__
- ifstream* input = new ifstream("/proc/cpuinfo");
+#if __linux__
- ifstream input("/proc/cpuinfo");
- if(input->is_open())
- 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())
{
- 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;
- string line;
- getline(*input, line);
- int match = pcre_exec(regex, NULL, line.c_str(), line.length(),
- 0, PCRE_NOTEMPTY,found, expected * 3);
- 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(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 we're at a second processor and we've already started,
- then we're done.
- */
- if (name == "processor")
- {
- cpuinfo.cpus++;
- if (started)
{
- if (started)
- {
- done = true;
- }
- else
- {
- started = true;
- }
- done = true;
} else {
- if(name == "model name") model = value;
- started = true;
} }
- else if (!done)
- {
- if (name == "cpu cores") cpuinfo.cores = atoi(value.c_str());
- else if (name == "model name") cpuinfo.model = value;
- }
}
- input->close();
- delete input;
} -#endif
- input.close();
- cpuinfo.cpus /= cpuinfo.cores;
} +#endif
- return model;
}
-unsigned int -host_get_number_of_cpu_cores() +string +host_get_cpu_model() {
- unsigned int cores = 0;
- if(!cores)
- {
- host_get_cpu_details();
-#ifdef __linux__
- 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;
- return cpuinfo.model;
+}
- udev_list_entry_foreach(entry, entries)
- {
- cores++;
- }
- }
+unsigned int +host_get_number_of_cpus() +{
- host_get_cpu_details();
- udev_enumerate_unref(enumerator);
- udev_unref(udev);
-#endif
- return cpuinfo.cpus;
+}
- }
+unsigned int +host_get_number_of_cpu_cores() +{
- host_get_cpu_details();
- return cores;
- return cpuinfo.cores;
}
void diff --git a/src/host.h b/src/host.h index c9e97b2..574e8b5 100644 --- a/src/host.h +++ b/src/host.h @@ -45,6 +45,8 @@ unsigned int host_get_memory();
string host_get_cpu_model();
+unsigned int host_get_number_of_cpus();
unsigned int host_get_number_of_cpu_cores();
void host_get_load_averages(double& one, double& five, double& fifteen); diff --git a/src/qmf/hostagent.cpp b/src/qmf/hostagent.cpp index cd6d0e6..0c0170c 100644 --- a/src/qmf/hostagent.cpp +++ b/src/qmf/hostagent.cpp @@ -47,6 +47,7 @@ HostAgent::setup(ManagementAgent* agent) _management_object->set_memory(host_get_memory());
_management_object->set_cpu_model(host_get_cpu_model());
- _management_object->set_cpu_count(host_get_number_of_cpus());
_management_object->set_cpu_cores(host_get_number_of_cpu_cores()); }
-- 1.7.1.1
Matahari mailing list Matahari@lists.fedorahosted.org https://fedorahosted.org/mailman/listinfo/matahari
On Mon, Jul 26, 2010 at 02:03:33PM +0200, Andrew Beekhof wrote:
ACK - looks like you've been having fun ;-)
Thanks, this is now pushed upstream.
And, yeah, I'm having fun with this. C++ rawks. :)
It's now being set when each heartbeat even occurs.
Signed-off-by: Darryl L. Pierce dpierce@redhat.com --- src/qmf/hostagent.cpp | 1 + 1 files changed, 1 insertions(+), 0 deletions(-)
diff --git a/src/qmf/hostagent.cpp b/src/qmf/hostagent.cpp index 0c0170c..350323f 100644 --- a/src/qmf/hostagent.cpp +++ b/src/qmf/hostagent.cpp @@ -71,6 +71,7 @@ void HostAgent::heartbeat(unsigned long timestamp, unsigned int sequence) { this->_agent->raiseEvent(_qmf::EventHeartbeat(timestamp, sequence)); + _management_object->set_last_updated(timestamp * 1000000000L); }
void
Do I want to ask why you have to multiply by 1000000000L ?
On Wed, Jul 14, 2010 at 10:15 PM, Darryl L. Pierce dpierce@redhat.com wrote:
It's now being set when each heartbeat even occurs.
Signed-off-by: Darryl L. Pierce dpierce@redhat.com
src/qmf/hostagent.cpp | 1 + 1 files changed, 1 insertions(+), 0 deletions(-)
diff --git a/src/qmf/hostagent.cpp b/src/qmf/hostagent.cpp index 0c0170c..350323f 100644 --- a/src/qmf/hostagent.cpp +++ b/src/qmf/hostagent.cpp @@ -71,6 +71,7 @@ void HostAgent::heartbeat(unsigned long timestamp, unsigned int sequence) { this->_agent->raiseEvent(_qmf::EventHeartbeat(timestamp, sequence));
- _management_object->set_last_updated(timestamp * 1000000000L);
}
void
1.7.1.1
Matahari mailing list Matahari@lists.fedorahosted.org https://fedorahosted.org/mailman/listinfo/matahari
On Mon, Jul 26, 2010 at 02:05:30PM +0200, Andrew Beekhof wrote:
Do I want to ask why you have to multiply by 1000000000L ?
QMF's granularity - absolute timestamps are in nanoseconds.
On Mon, Jul 26, 2010 at 3:30 PM, Darryl L. Pierce dpierce@redhat.com wrote:
On Mon, Jul 26, 2010 at 02:05:30PM +0200, Andrew Beekhof wrote:
Do I want to ask why you have to multiply by 1000000000L ?
QMF's granularity - absolute timestamps are in nanoseconds.
Fair enough.
On Wed, Jul 14, 2010 at 04:15:13PM -0400, Darryl L. Pierce wrote:
This pair of patches refactors the CPU identification code, and also ensures the last updated timestamp is properly set each time a heartbeat event is fired.
Matahari mailing list Matahari@lists.fedorahosted.org https://fedorahosted.org/mailman/listinfo/matahari
Can I get feedback or an ACK on this set of patches?
matahari@lists.fedorahosted.org