2006-08-03 00:06:20 +02:00
|
|
|
// src/python_cpu_policy_manager.cc - Copyright 2005, 2006, University
|
2006-02-17 17:08:54 +01:00
|
|
|
// of Padova, dept. of Pure and Applied
|
|
|
|
// Mathematics
|
|
|
|
//
|
|
|
|
// This file is part of SGPEMv2.
|
|
|
|
//
|
|
|
|
// This 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; either version 2 of the License, or
|
|
|
|
// (at your option) any later version.
|
|
|
|
//
|
|
|
|
// SGPEMv2 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 SGPEMv2; if not, write to the Free Software
|
|
|
|
// Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
|
|
|
|
|
2006-08-03 00:06:20 +02:00
|
|
|
#include "python_cpu_policy_manager.hh"
|
2006-06-12 16:04:06 +02:00
|
|
|
#include "global_preferences.hh"
|
2006-08-02 23:57:36 +02:00
|
|
|
#include "cpu_policies_gatekeeper.hh"
|
2006-02-17 17:08:54 +01:00
|
|
|
|
2006-02-22 13:39:30 +01:00
|
|
|
#include <Python.h>
|
2006-03-09 11:49:41 +01:00
|
|
|
#include <glibmm/ustring.h>
|
2006-02-22 13:39:30 +01:00
|
|
|
#include <glibmm/timer.h>
|
2006-06-12 15:16:10 +02:00
|
|
|
#include <glibmm/fileutils.h>
|
|
|
|
#include <glibmm/pattern.h>
|
2006-02-22 13:39:30 +01:00
|
|
|
|
2006-03-09 11:49:41 +01:00
|
|
|
#include <algorithm>
|
2006-02-21 13:20:14 +01:00
|
|
|
#include <cassert>
|
2006-03-09 11:49:41 +01:00
|
|
|
#include <functional>
|
2006-02-21 23:57:14 +01:00
|
|
|
#include <iostream>
|
2006-03-09 11:49:41 +01:00
|
|
|
#include <string>
|
2006-06-12 15:16:10 +02:00
|
|
|
#include <iostream>
|
2006-02-22 13:39:30 +01:00
|
|
|
#include <unistd.h>
|
2006-08-02 23:57:36 +02:00
|
|
|
|
2006-02-17 17:08:54 +01:00
|
|
|
using namespace sgpem;
|
2006-06-12 15:16:10 +02:00
|
|
|
using std::vector;
|
|
|
|
using std::cout;
|
|
|
|
using std::endl;
|
2006-03-09 11:49:41 +01:00
|
|
|
|
|
|
|
|
|
|
|
// Concatenate a string with all the policies directories
|
|
|
|
struct pol_dirs_concat : public std::unary_function<void, const Glib::ustring&>
|
|
|
|
{
|
|
|
|
public:
|
2006-06-29 10:44:30 +02:00
|
|
|
pol_dirs_concat(Glib::ustring& cat) : _cat(cat) {}
|
|
|
|
void operator()(const Glib::ustring& add)
|
|
|
|
{
|
|
|
|
// Please note that this string will end finishing with
|
|
|
|
// and additional ","!
|
|
|
|
_cat += "'" + add + "', ";
|
|
|
|
}
|
2006-03-09 11:49:41 +01:00
|
|
|
private:
|
2006-06-29 10:44:30 +02:00
|
|
|
Glib::ustring& _cat;
|
2006-03-09 11:49:41 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
|
2006-08-02 23:57:36 +02:00
|
|
|
PythonCPUPolicyManager::PythonCPUPolicyManager()
|
2006-06-29 10:44:30 +02:00
|
|
|
: _initialized(false)
|
2006-02-17 17:08:54 +01:00
|
|
|
{
|
2006-02-22 13:39:30 +01:00
|
|
|
PyEval_InitThreads();
|
2006-02-17 17:08:54 +01:00
|
|
|
}
|
|
|
|
|
2006-03-09 11:49:41 +01:00
|
|
|
|
2006-08-02 23:57:36 +02:00
|
|
|
PythonCPUPolicyManager::~PythonCPUPolicyManager()
|
2006-06-12 15:16:10 +02:00
|
|
|
{
|
|
|
|
for_each(_policies.begin(), _policies.end(), ptr_fun(operator delete));
|
|
|
|
}
|
|
|
|
|
2006-03-09 11:49:41 +01:00
|
|
|
|
2006-02-17 17:08:54 +01:00
|
|
|
void
|
2006-08-02 23:57:36 +02:00
|
|
|
PythonCPUPolicyManager::init()
|
2006-02-17 17:08:54 +01:00
|
|
|
{
|
2006-06-29 10:44:30 +02:00
|
|
|
if(_initialized)
|
|
|
|
// No-op
|
|
|
|
return;
|
|
|
|
|
2006-02-21 11:31:01 +01:00
|
|
|
Py_Initialize();
|
|
|
|
_initialized = true;
|
2006-06-29 10:44:30 +02:00
|
|
|
|
|
|
|
// The following lines are ugly, but necessary if we use
|
2006-02-21 11:31:01 +01:00
|
|
|
// non-standard installation directories. Theoretically,
|
2006-06-29 10:44:30 +02:00
|
|
|
// it should be up to the user to set correct
|
|
|
|
// environment variables.
|
|
|
|
|
2006-06-21 11:09:50 +02:00
|
|
|
GlobalPreferences& prefs = GlobalPreferences::get_instance();
|
2006-06-29 10:44:30 +02:00
|
|
|
Glib::ustring importdirs = "import sys\n"
|
|
|
|
"sys.path[:0] = [ ";
|
2006-06-21 11:09:50 +02:00
|
|
|
for_each(prefs.policies_dir_begin(),
|
2006-06-29 10:44:30 +02:00
|
|
|
prefs.policies_dir_end(),
|
|
|
|
pol_dirs_concat(importdirs));
|
2006-04-06 21:01:13 +02:00
|
|
|
importdirs += " '" SHAREDIR "' ]\n";
|
2006-03-09 11:49:41 +01:00
|
|
|
|
|
|
|
PyRun_SimpleString(importdirs.c_str());
|
2006-02-21 13:20:14 +01:00
|
|
|
|
|
|
|
// Okay, here we go.
|
2006-06-29 10:44:30 +02:00
|
|
|
// Black magic at work.
|
2006-06-12 15:16:10 +02:00
|
|
|
collect_policies();
|
2006-02-21 13:20:14 +01:00
|
|
|
}
|
2006-06-12 15:16:10 +02:00
|
|
|
|
2006-08-02 23:57:36 +02:00
|
|
|
const vector<CPUPolicy*>&
|
|
|
|
PythonCPUPolicyManager::get_avail_policies()
|
2006-06-12 15:16:10 +02:00
|
|
|
{
|
|
|
|
return _policies;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2006-08-02 23:57:36 +02:00
|
|
|
PythonCPUPolicyManager::collect_policies()
|
2006-06-12 15:16:10 +02:00
|
|
|
{
|
2006-06-21 11:09:50 +02:00
|
|
|
GlobalPreferences& prefs = GlobalPreferences::get_instance();
|
|
|
|
GlobalPreferences::dir_iterator dir_it = prefs.policies_dir_begin();
|
|
|
|
GlobalPreferences::dir_iterator dir_end = prefs.policies_dir_end();
|
2006-06-12 15:16:10 +02:00
|
|
|
|
|
|
|
for(; dir_it != dir_end; ++dir_it)
|
|
|
|
{
|
|
|
|
Glib::Dir dir(dir_it->c_str());
|
|
|
|
|
2006-08-03 01:38:52 +02:00
|
|
|
cout << "Opening directory " << *dir_it << " looking for python policies..." << endl;
|
2006-06-29 10:44:30 +02:00
|
|
|
|
2006-06-12 15:16:10 +02:00
|
|
|
for(Glib::DirIterator file_it = dir.begin(); file_it != dir.end(); ++file_it)
|
|
|
|
{
|
2006-08-03 01:38:52 +02:00
|
|
|
cout << "\tChecking if " << *file_it << " is a Python script... ";
|
2006-06-29 10:44:30 +02:00
|
|
|
|
2006-06-12 15:16:10 +02:00
|
|
|
Glib::PatternSpec dot_py("*.py");
|
|
|
|
|
|
|
|
if(dot_py.match(*file_it))
|
|
|
|
{
|
2006-08-03 01:38:52 +02:00
|
|
|
cout << "yes\n";
|
2006-06-29 10:44:30 +02:00
|
|
|
|
|
|
|
//strip extension
|
|
|
|
std::string policy_name = (*file_it).substr(0, (*file_it).size() - 3);
|
|
|
|
|
2006-08-02 23:57:36 +02:00
|
|
|
PythonCPUPolicy *pypolicy = new PythonCPUPolicy(policy_name.c_str());
|
2006-06-29 10:44:30 +02:00
|
|
|
|
|
|
|
_policies.push_back(pypolicy);
|
2006-06-12 15:16:10 +02:00
|
|
|
}
|
2006-08-03 01:38:52 +02:00
|
|
|
else
|
|
|
|
cout << "no\n";
|
2006-06-12 15:16:10 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|