- Add copyright notices to existing builtin python policies

- Implement system to dinamically pass plugins and policies search paths to
the sgpemv2 binary and tests (class GlobalSettings)
- Drastically reduce usage of hardcoded paths in code except as 
default overridable values


git-svn-id: svn://svn.gna.org/svn/sgpemv2/trunk@514 3ecf2c5c-341e-0410-92b4-d18e462d057c
This commit is contained in:
tchernobog 2006-03-09 10:49:41 +00:00
parent 24a0194368
commit 92e6f3be2b
9 changed files with 353 additions and 88 deletions

View File

@ -38,7 +38,7 @@ bin_PROGRAMS =
plugin_LTLIBRARIES = plugin_LTLIBRARIES =
noinst_HEADERS = noinst_HEADERS =
pkglib_LTLIBRARIES = pkglib_LTLIBRARIES =
tests_PYTHON = noinst_PYTHON =
EXTRA_DIST = EXTRA_DIST =
MAINTAINERCLEANFILES = MAINTAINERCLEANFILES =
MOSTLYCLEANFILES = MOSTLYCLEANFILES =
@ -120,7 +120,6 @@ src_backend_libbackend_la_CPPFLAGS = \
-I@top_srcdir@ \ -I@top_srcdir@ \
-DPOLDIR="\"$(policiesdir)\"" \ -DPOLDIR="\"$(policiesdir)\"" \
-DPLUGDIR="\"$(plugindir)\"" \ -DPLUGDIR="\"$(plugindir)\"" \
-DMODDIR="\"$(moduledir)\"" \
-DLOCALEDIR="\"$(localedir)\"" \ -DLOCALEDIR="\"$(localedir)\"" \
$(PYTHON_CPPFLAGS) \ $(PYTHON_CPPFLAGS) \
$(GLIBMM_CFLAGS) $(GLIBMM_CFLAGS)
@ -134,6 +133,7 @@ src_backend_libbackend_la_LDFLAGS = \
# Please keep this in sorted order: # Please keep this in sorted order:
src_backend_libbackend_la_SOURCES = \ src_backend_libbackend_la_SOURCES = \
src/backend/global_settings.cc \
src/backend/history.cc \ src/backend/history.cc \
src/backend/observed_subject.cc \ src/backend/observed_subject.cc \
src/backend/policy.cc \ src/backend/policy.cc \
@ -150,6 +150,7 @@ src_backend_libbackend_la_SOURCES = \
pkginclude_HEADERS = \ pkginclude_HEADERS = \
config.h \ config.h \
src/backend/global_settings.hh \
src/backend/history.hh \ src/backend/history.hh \
src/backend/observed_subject.hh \ src/backend/observed_subject.hh \
src/backend/plugin.hh \ src/backend/plugin.hh \
@ -176,8 +177,6 @@ bin_PROGRAMS += sgpemv2
sgpemv2_CPPFLAGS = \ sgpemv2_CPPFLAGS = \
-I@top_srcdir@ \ -I@top_srcdir@ \
-DPLUGDIR="\"$(plugindir)\"" \
-DMODDIR="\"$(moduledir)\"" \
-DLOCALEDIR="\"$(localedir)\"" \ -DLOCALEDIR="\"$(localedir)\"" \
$(CAIRO_CFLAGS) \ $(CAIRO_CFLAGS) \
$(GTKMM_CFLAGS) \ $(GTKMM_CFLAGS) \
@ -228,7 +227,6 @@ plugin_LTLIBRARIES += src/backend/pyloader/libpyloader.la
src_backend_pyloader_libpyloader_la_CPPFLAGS = \ src_backend_pyloader_libpyloader_la_CPPFLAGS = \
-I@top_srcdir@ \ -I@top_srcdir@ \
-DPOLDIR="\"$(policiesdir)\"" \
-DMODDIR="\"$(pyloaderdir)\"" \ -DMODDIR="\"$(pyloaderdir)\"" \
-DLOCALEDIR="\"$(localedir)\"" \ -DLOCALEDIR="\"$(localedir)\"" \
$(PYTHON_CPPFLAGS) \ $(PYTHON_CPPFLAGS) \
@ -326,12 +324,10 @@ policies_PYTHON = \
# DEJATOOL = src/testsuite/example-test.exp # DEJATOOL = src/testsuite/example-test.exp
tests_PROGRAMS = src/testsuite/test-python_loader noinst_PROGRAMS = src/testsuite/test-python_loader
src_testsuite_test_python_loader_CPPFLAGS = \ src_testsuite_test_python_loader_CPPFLAGS = \
-I@top_srcdir@/src \ -I@top_srcdir@/src \
-DPOLDIR="\"$(testsdir)\"" \
-DMODDIR="\"$(pyloaderdir)\"" \
$(PYTHON_CPPFLAGS) \ $(PYTHON_CPPFLAGS) \
$(GLIBMM_CFLAGS) \ $(GLIBMM_CFLAGS) \
$(GTHREAD_CFLAGS) $(GTHREAD_CFLAGS)
@ -348,7 +344,7 @@ src_testsuite_test_python_loader_SOURCES = \
src/observer.cc \ src/observer.cc \
src/simulation.cc src/simulation.cc
tests_PYTHON += src/testsuite/python_loader_configure.py \ noinst_PYTHON += src/testsuite/python_loader_configure.py \
src/testsuite/python_loader_sort_queue.py \ src/testsuite/python_loader_sort_queue.py \
src/testsuite/python_loader_is_preemptive.py \ src/testsuite/python_loader_is_preemptive.py \
src/testsuite/python_loader_get_time_slice.py src/testsuite/python_loader_get_time_slice.py

View File

@ -0,0 +1,82 @@
// src/global_settings.cc - Copyright 2005, 2006, University
// 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
#include "config.h"
#include "global_settings.hh"
using namespace sgpem;
GlobalSettings* GlobalSettings::_instance = 0;
GlobalSettings&
GlobalSettings::instance()
{
if(!_instance)
_instance = new GlobalSettings();
return *_instance;
}
GlobalSettings::GlobalSettings()
: _mod_dirs(1, PLUGDIR), _pol_dirs(1, POLDIR)
{}
GlobalSettings::dir_iterator
GlobalSettings::policies_dir_begin() const
{
return _pol_dirs.begin();
}
GlobalSettings::dir_iterator
GlobalSettings::policies_dir_end() const
{
return _pol_dirs.end();
}
GlobalSettings::dir_iterator
GlobalSettings::modules_dir_begin() const
{
return _mod_dirs.begin();
}
GlobalSettings::dir_iterator
GlobalSettings::modules_dir_end() const
{
return _mod_dirs.end();
}
void
GlobalSettings::add_modules_dir(const Glib::ustring& moddir)
{
_mod_dirs.insert(_mod_dirs.begin(), moddir);
}
void
GlobalSettings::add_policies_dir(const Glib::ustring& poldir)
{
_pol_dirs.insert(_pol_dirs.begin(), poldir);
}

View File

@ -0,0 +1,62 @@
// src/global_settings.hh - Copyright 2005, 2006, University
// 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
#ifndef GLOBAL_SETTINGS_HH
#define GLOBAL_SETTINGS_HH 1
#include "config.h"
#include <glibmm/ustring.h>
#include <vector>
namespace sgpem {
class GlobalSettings;
}
#include "config.h"
namespace sgpem {
class SG_DLLEXPORT GlobalSettings {
public:
typedef std::vector<Glib::ustring>::const_iterator dir_iterator;
static GlobalSettings& instance();
dir_iterator modules_dir_begin() const;
dir_iterator modules_dir_end() const;
dir_iterator policies_dir_begin() const;
dir_iterator policies_dir_end() const;
void add_modules_dir(const Glib::ustring& moddir);
void add_policies_dir(const Glib::ustring& poldir);
private:
GlobalSettings();
GlobalSettings(const GlobalSettings&);
GlobalSettings& operator=(const GlobalSettings&);
static GlobalSettings* _instance;
std::vector<Glib::ustring> _mod_dirs;
std::vector<Glib::ustring> _pol_dirs;
};
}
#endif

View File

@ -19,25 +19,50 @@
// Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA // Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
#include "python_policy_manager.hh" #include "python_policy_manager.hh"
#include "../global_settings.hh"
#include <Python.h> #include <Python.h>
#include <glibmm/ustring.h>
#include <glibmm/timer.h> #include <glibmm/timer.h>
#include <string> #include <algorithm>
#include <cassert> #include <cassert>
#include <functional>
#include <iostream> #include <iostream>
#include <string>
#include <unistd.h> #include <unistd.h>
using namespace sgpem; using namespace sgpem;
// Concatenate a string with all the policies directories
struct pol_dirs_concat : public std::unary_function<void, const Glib::ustring&>
{
public:
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 + "', ";
}
private:
Glib::ustring& _cat;
};
//static object //static object
PythonPolicyManager* PythonPolicyManager::_instance = NULL; PythonPolicyManager* PythonPolicyManager::_instance = NULL;
PythonPolicyManager::PythonPolicyManager() PythonPolicyManager::PythonPolicyManager()
: _initialized(false) : _initialized(false)
{ {
PyEval_InitThreads(); PyEval_InitThreads();
} }
PythonPolicyManager* const PythonPolicyManager* const
PythonPolicyManager::get_instance() PythonPolicyManager::get_instance()
{ {
@ -46,6 +71,7 @@ PythonPolicyManager::get_instance()
return _instance; return _instance;
} }
Policy& Policy&
PythonPolicyManager::get_policy() PythonPolicyManager::get_policy()
{ {
@ -53,6 +79,7 @@ PythonPolicyManager::get_policy()
return *_python_policy; return *_python_policy;
} }
void void
PythonPolicyManager::init() PythonPolicyManager::init()
{ {
@ -63,14 +90,20 @@ PythonPolicyManager::init()
Py_Initialize(); Py_Initialize();
_initialized = true; _initialized = true;
// The following line is ugly, but necessary if we use // The following lines are ugly, but necessary if we use
// non-standard installation directories. Theoretically, // non-standard installation directories. Theoretically,
// it should be up to the user to set correct // it should be up to the user to set correct
// environment variables. // environment variables.
// FIXME: find better way to achieve this. // FIXME: find better way to achieve this.
PyRun_SimpleString("import sys\n" Glib::ustring importdirs = "import sys\n"
"sys.path[:0] = [ '" MODDIR "', '" POLDIR "' ]\n"); "sys.path[:0] = [ ";
for_each(GlobalSettings::instance().policies_dir_begin(),
GlobalSettings::instance().policies_dir_end(),
pol_dirs_concat(importdirs));
importdirs += " '" MODDIR "' ]\n";
PyRun_SimpleString(importdirs.c_str());
// Okay, here we go. // Okay, here we go.
// Black magic at work. // Black magic at work.

View File

@ -1,3 +1,24 @@
# src/builtin-policies/fcfs.py - Copyright 2005, 2006, University
# 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
from Policy import Policy from Policy import Policy
import sys import sys
@ -15,8 +36,6 @@ class fcfs(Policy) :
return -1 return -1
def sort_queue(self, event, queue): def sort_queue(self, event, queue):
#while True:
# pass
cmpf = lambda a, b: \ cmpf = lambda a, b: \
a.get_schedulable().get_arrival_time() < \ a.get_schedulable().get_arrival_time() < \
b.get_schedulable().get_arrival_time() b.get_schedulable().get_arrival_time()

View File

@ -1,3 +1,24 @@
# src/builtin-policies/sjf.py - Copyright 2005, 2006, University
# 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
from Policy import Policy from Policy import Policy
import sys import sys

View File

@ -34,13 +34,16 @@
#include "backend/process.hh" #include "backend/process.hh"
#include "backend/policy.hh" #include "backend/policy.hh"
#include "backend/policy_parameters.hh" #include "backend/policy_parameters.hh"
#include "backend/global_settings.hh"
#include "standard_io.hh" #include "standard_io.hh"
#include "text_simulation.hh" #include "text_simulation.hh"
#include <glibmm/module.h> #include <glibmm/module.h>
#include <glibmm/ustring.h> #include <glibmm/ustring.h>
#include <algorithm>
#include <cassert> #include <cassert>
#include <functional>
#include <iostream> #include <iostream>
#include <string> #include <string>
#include <vector> #include <vector>
@ -50,6 +53,32 @@ using namespace sgpem;
using namespace memory; using namespace memory;
using Glib::ustring; using Glib::ustring;
static void load_pyloader_plugin() {
// FIXME: this will need to be moved to an
// appropriate PluginManager class in the backend,
// and the Makefile fixed accordingly (partly done).
using Glib::Module;
// Leaks willingly:
Module* pyloader = 0;
GlobalSettings::dir_iterator it = GlobalSettings::instance().modules_dir_begin();
while(it != GlobalSettings::instance().modules_dir_end()) {
std::string pyloader_path = Module::build_path(*it, "pyloader");
pyloader = new Module(pyloader_path);
if(*pyloader) break;
else delete pyloader;
it++;
}
if(!*pyloader)
std::cerr << Module::get_last_error() << std::endl;
// For the moment, we want to be sure it has been loaded:
assert(*pyloader);
}
int int
main(int argc, char* argv[]) main(int argc, char* argv[])
{ {
@ -60,19 +89,9 @@ main(int argc, char* argv[])
bindtextdomain(PACKAGE, LOCALEDIR); bindtextdomain(PACKAGE, LOCALEDIR);
textdomain(PACKAGE); textdomain(PACKAGE);
// FIXME: this will need to be moved to an
// appropriate PluginManager class in the backend,
// and the Makefile fixed accordingly
using Glib::Module;
std::string pyloader_path = Module::build_path(PLUGDIR, "pyloader");
Module pyloader(pyloader_path);
std::cerr << Module::get_last_error() << std::endl;
assert(pyloader);
// Set up Glib thread support // Set up Glib thread support
Glib::thread_init(); Glib::thread_init();
// Parses options and prepares vector with // Parses options and prepares vector with
// filenames of documents to be opened // filenames of documents to be opened
vector<string> filenames; vector<string> filenames;
@ -83,6 +102,7 @@ main(int argc, char* argv[])
filenames.insert(filenames.begin(), a_ptr, a_ptr+a_count); filenames.insert(filenames.begin(), a_ptr, a_ptr+a_count);
} }
load_pyloader_plugin();
// Create an INITIAL STATE // Create an INITIAL STATE
Process p1("P1", 0,5,1); Process p1("P1", 0,5,1);

View File

@ -21,7 +21,7 @@
#include "config.h" #include "config.h"
#include "gettext.h" #include "gettext.h"
#include "main.hh" #include "backend/global_settings.hh"
#include "parse_opts.hh" #include "parse_opts.hh"
#ifdef _GNU_SOURCE #ifdef _GNU_SOURCE
@ -39,81 +39,102 @@ static void display_help();
void void
parse_options(int& argc, char**& argv) parse_options(int& argc, char**& argv)
{ {
print_license(); using sgpem::GlobalSettings;
static const char* short_options = "nh"; print_license();
#ifdef _GNU_SOURCE static const char* short_options = "NhP:M:";
// Initialize the array for GNU long options
static struct option long_options[] =
{
{"no-gui", no_argument, NULL, 'n' },
{"help", no_argument, NULL, 'h' },
};
int option_index = 0;
#endif
int opt; #ifdef _GNU_SOURCE
do // Initialize the array for GNU long options
{ static struct option long_options[] =
#ifdef _GNU_SOURCE {
opt = getopt_long(argc, argv, short_options, {"no-gui", no_argument, NULL, 'N' },
long_options, &option_index); {"help", no_argument, NULL, 'h' },
#else {"policies-dir", required_argument, NULL, 'P'},
opt = getopt(argc, argv, short_options); {"modules-dir", required_argument, NULL, 'M'}
#endif };
int option_index = 0;
#endif
switch(opt) int opt;
{ do
case -1: {
// We have finished normally #ifdef _GNU_SOURCE
break; opt = getopt_long(argc, argv, short_options,
case 'n' : long_options, &option_index);
// We don't return to main, instead we #else
// initialize the command line version opt = getopt(argc, argv, short_options);
// of sgpemv2 (?) #endif
// FIXME : to be written! switch(opt)
break; {
case 'h' : case -1:
default : // We have finished normally
display_help(); break;
} case 'N' :
} while( opt != -1 ); // We don't return to main, instead we
// initialize the command line version
// of sgpemv2 (?)
argc -= optind; // FIXME : to be written!
argv += optind; break;
case 'P':
GlobalSettings::instance().add_policies_dir(optarg);
break;
case 'M':
GlobalSettings::instance().add_modules_dir(optarg);
break;
case ':':
printf(_("[EE] Wrong number of parameters. Please see \n"
"%s --help\n"), argv[0]);
exit(-1);
case 'h' :
default :
display_help();
}
} while( opt != -1 );
// Set these two to start from additional filenames on the cmdline:
argc -= optind;
argv += optind;
} }
void void
display_help() display_help()
{ {
printf( _("SGPEMv2 is an educational software acting as a process scheduling simulator\n" printf( _("SGPEMv2 is an educational software acting as a process scheduling simulator\n"
"\n\nUsage : sgpemv2 [options]" // filenames" "\n\nUsage : sgpemv2 [options]" // filenames"
"\n\nOptions:\n" "\n\nOptions:\n"
"\t-h, --help this help you're reading\n" "\t-h, --help this help you're reading\n"
"\t-n, --no-gui starts the program in command line mode\n" "\t-N, --no-gui starts the program in command line mode\n"
// "\nFilenames:\n" "\t-P dir, --policies-dir=dir\n"
// "\t a list of any valid SGPEMv2 XML file\n" "\t add this directory to the default modules\n"
// "\t to be opened, space-separated.\n" "\t search path\n"
"\nLong options are available only on GNU systems.\n\n" ) ); "\t-M dir, --modules-dir=dir\n"
exit(0); "\t add this directory to default plugin\n"
"\t search path\n"
"\nFilenames:\n"
"\t a list of any valid SGPEMv2 XML file\n"
"\t to be opened, space-separated.\n"
"\nLong options are available only on GNU systems.\n\n" ) );
exit(0);
} }
void void
print_license() print_license()
{ {
// Do _NOT_ translate this text. // Do _NOT_ translate this text.
std::cerr << std::cerr <<
"SGPEMv2, Copyright (C) 2005, 2006 University of Padova,\n" "SGPEMv2, Copyright (C) 2005, 2006 University of Padova,\n"
" dept. of Pure and Applied Mathematics.\n" " dept. of Pure and Applied Mathematics.\n"
"SGPEMv2 comes with ABSOLUTELY NO WARRANTY. This is free \n" "SGPEMv2 comes with ABSOLUTELY NO WARRANTY. This is free \n"
"software, and you are welcome to redistribute it under \n" "software, and you are welcome to redistribute it under \n"
"the terms of the GNU General Public License; for details\n" "the terms of the GNU General Public License; for details\n"
"see file COPYING contained in the source package. \n" "see file COPYING contained in the source package. \n"
<< std::endl; << std::endl;
} }

View File

@ -25,6 +25,7 @@
#include "backend/pyloader/python_policy_manager.hh" #include "backend/pyloader/python_policy_manager.hh"
#include "backend/pyloader/python_policy.hh" #include "backend/pyloader/python_policy.hh"
#include "backend/process.hh" #include "backend/process.hh"
#include "backend/global_settings.hh"
#include "backend/schedulable_status.hh" #include "backend/schedulable_status.hh"
#include "backend/schedulable_list.hh" #include "backend/schedulable_list.hh"
#include "backend/scheduler.hh" #include "backend/scheduler.hh"
@ -36,14 +37,16 @@
#include <glibmm/module.h> #include <glibmm/module.h>
#include <cassert> #include <cassert>
#include <iostream>
#include <string> #include <string>
// FIXME: Eeeeh? Why does this work without explicit namespace resolving?
// Is there some using declaration in included HEADERS?? Aaaaagh!
class TestPythonPolicyManager : public PythonPolicyManager { class TestPythonPolicyManager : public PythonPolicyManager {
public: public:
void test_init(const char* policy_name) { void test_init(const char* policy_name) {
init(); init();
PyRun_SimpleString("import sys\n"
"sys.path[:0] = [ '" MODDIR "', '" POLDIR "' ]\n");
_python_policy = std::auto_ptr<PythonPolicy>(new PythonPolicy(policy_name)); _python_policy = std::auto_ptr<PythonPolicy>(new PythonPolicy(policy_name));
} }
}; };
@ -52,7 +55,15 @@ public:
int int
main(int argc, char** argv) { main(int argc, char** argv) {
using namespace sgpem; using namespace sgpem;
using Glib::Module;
if(argc != 2) {
std::cout << "[EE] Usage:\n\t" << argv[0] <<
" path/to/uninstalled/policies" << std::endl;
exit(-1);
}
else
// Add current directory to search for uninstalled policies
sgpem::GlobalSettings::instance().add_policies_dir(argv[1]);
Glib::thread_init(); Glib::thread_init();