2006-08-03 00:06:20 +02:00
|
|
|
// src/python_cpu_policy.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.hh"
|
2006-08-02 23:57:36 +02:00
|
|
|
|
2006-02-24 09:28:18 +01:00
|
|
|
#include <limits>
|
2006-02-22 16:16:08 +01:00
|
|
|
#include <unistd.h>
|
2006-02-24 12:17:37 +01:00
|
|
|
#include <iostream>
|
2006-08-02 23:57:36 +02:00
|
|
|
|
2006-02-17 17:08:54 +01:00
|
|
|
using namespace sgpem;
|
|
|
|
using namespace std;
|
|
|
|
|
2006-03-10 12:38:10 +01:00
|
|
|
#define WAIT_FOR (250000)
|
|
|
|
|
2006-02-22 16:16:08 +01:00
|
|
|
// WARNING : this class needs extensive and above all
|
|
|
|
// *strong* exception checking / handling!
|
|
|
|
|
2006-08-08 02:20:56 +02:00
|
|
|
PythonCPUPolicy::PythonCPUPolicy(const char* name) throw(MalformedPolicyException)
|
2006-06-29 10:44:30 +02:00
|
|
|
: _upolicy_dict(NULL), _adapter(NULL), _name(name)
|
2006-02-22 16:16:08 +01:00
|
|
|
{
|
|
|
|
PyObject* pLoadmeStr = PyString_FromString(name);
|
2006-08-02 23:57:36 +02:00
|
|
|
PyObject* pUserCPUPolicyModule = PyImport_Import(pLoadmeStr);
|
2006-02-22 16:16:08 +01:00
|
|
|
Py_DECREF(pLoadmeStr);
|
2006-06-29 10:44:30 +02:00
|
|
|
|
2006-08-09 16:38:45 +02:00
|
|
|
if (pUserCPUPolicyModule == NULL)
|
2006-08-09 15:24:42 +02:00
|
|
|
throw MalformedPolicyException(get_exception_information().c_str());
|
2006-08-09 16:38:45 +02:00
|
|
|
|
2006-02-22 16:16:08 +01:00
|
|
|
// Dictionary with defined ``symbols'' for .pyc file
|
2006-08-02 23:57:36 +02:00
|
|
|
_upolicy_dict = PyModule_GetDict(pUserCPUPolicyModule);
|
2006-05-14 17:35:27 +02:00
|
|
|
assert(_upolicy_dict);
|
2006-02-22 16:16:08 +01:00
|
|
|
|
|
|
|
// Loads ScriptAdapter module and get its dictionary
|
|
|
|
pLoadmeStr = PyString_FromString("ScriptAdapter");
|
|
|
|
PyObject* pScriptAdapterModule = PyImport_Import(pLoadmeStr);
|
|
|
|
Py_DECREF(pLoadmeStr);
|
|
|
|
assert(pScriptAdapterModule);
|
2006-05-14 17:35:27 +02:00
|
|
|
PyObject* pAdapterDict = PyModule_GetDict(pScriptAdapterModule);
|
|
|
|
assert(pAdapterDict);
|
2006-06-29 10:44:30 +02:00
|
|
|
|
2006-08-02 23:57:36 +02:00
|
|
|
// Now takes the user-defined policy class from pUserCPUPolicyDict
|
|
|
|
PyObject* pCPUPolicyClass = PyDict_GetItemString(_upolicy_dict, name);
|
|
|
|
assert(pCPUPolicyClass); // FIXME needs stricter checking and exception throwing
|
2006-06-29 10:44:30 +02:00
|
|
|
|
2006-02-22 16:16:08 +01:00
|
|
|
// Creates a new object of type ScriptAdapter :
|
|
|
|
// takes init function from ScriptAdapter class
|
2006-05-14 17:35:27 +02:00
|
|
|
PyObject* pAdapterClass = PyDict_GetItemString(pAdapterDict, "ScriptAdapter");
|
2006-02-22 16:16:08 +01:00
|
|
|
PyObject* pAdapterCtorParam = PyTuple_New(1);
|
2006-08-02 23:57:36 +02:00
|
|
|
Py_INCREF(pCPUPolicyClass); // PyTuple_SetItem steals a reference
|
|
|
|
PyTuple_SetItem(pAdapterCtorParam, 0, pCPUPolicyClass);
|
2006-02-22 16:16:08 +01:00
|
|
|
_adapter = PyInstance_New(pAdapterClass, pAdapterCtorParam, NULL);
|
|
|
|
Py_DECREF(pAdapterCtorParam);
|
2006-08-09 16:38:45 +02:00
|
|
|
|
2006-02-22 16:16:08 +01:00
|
|
|
Py_DECREF(pScriptAdapterModule);
|
2006-08-09 16:38:45 +02:00
|
|
|
|
|
|
|
if (_adapter == NULL)
|
2006-08-09 15:24:42 +02:00
|
|
|
throw MalformedPolicyException(get_exception_information().c_str());
|
2006-08-09 16:38:45 +02:00
|
|
|
|
2006-02-22 16:16:08 +01:00
|
|
|
// And now, who's your daddy, huh?
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2006-08-02 23:57:36 +02:00
|
|
|
PythonCPUPolicy::~PythonCPUPolicy()
|
2006-02-17 17:08:54 +01:00
|
|
|
{
|
2006-08-09 16:38:45 +02:00
|
|
|
if (_adapter) Py_DECREF(_adapter);
|
2006-05-14 17:35:27 +02:00
|
|
|
|
2006-06-29 10:44:30 +02:00
|
|
|
// We keep this alive until dtor time, because
|
|
|
|
// the user may have defined some static global-space
|
|
|
|
// variables and they make use of them.
|
2006-08-09 16:38:45 +02:00
|
|
|
if (_upolicy_dict) Py_DECREF(_upolicy_dict);
|
2006-02-22 16:16:08 +01:00
|
|
|
}
|
|
|
|
|
2006-08-09 15:37:42 +02:00
|
|
|
void
|
|
|
|
PythonCPUPolicy::activate()
|
|
|
|
{
|
|
|
|
// FIXME write the rest, taking away code from constructor
|
|
|
|
|
|
|
|
configure();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void
|
|
|
|
PythonCPUPolicy::deactivate()
|
|
|
|
{
|
|
|
|
// FIXME See if some code has to be moved here from the
|
|
|
|
// destructor, AFTER having written activate()
|
|
|
|
|
|
|
|
_parameters.clear();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2006-02-22 16:16:08 +01:00
|
|
|
|
|
|
|
void
|
2006-08-02 23:57:36 +02:00
|
|
|
PythonCPUPolicy::configure() throw(UserInterruptException)
|
2006-02-22 16:16:08 +01:00
|
|
|
{
|
|
|
|
PyObject* retval = PyObject_CallMethod(_adapter, "async_configure", NULL);
|
|
|
|
Py_DECREF(retval);
|
2006-02-23 12:29:25 +01:00
|
|
|
|
2006-02-22 16:16:08 +01:00
|
|
|
wait_unlock();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void
|
2006-08-02 23:57:36 +02:00
|
|
|
PythonCPUPolicy::sort_queue() const throw(UserInterruptException)
|
2006-02-22 16:16:08 +01:00
|
|
|
{
|
2006-07-28 14:21:49 +02:00
|
|
|
PyObject* retval = PyObject_CallMethod(_adapter, "async_sort_queue", NULL);
|
2006-06-29 10:44:30 +02:00
|
|
|
|
2006-02-23 12:29:25 +01:00
|
|
|
// Do minimal debugging
|
2006-08-09 16:38:45 +02:00
|
|
|
if (!retval) PyErr_Print();
|
2006-02-23 11:12:27 +01:00
|
|
|
else Py_DECREF(retval);
|
2006-06-29 10:44:30 +02:00
|
|
|
|
2006-02-22 16:16:08 +01:00
|
|
|
wait_unlock();
|
2006-02-17 17:08:54 +01:00
|
|
|
}
|
2006-02-19 23:36:24 +01:00
|
|
|
|
2006-02-22 16:16:08 +01:00
|
|
|
|
|
|
|
Glib::ustring
|
2006-08-02 23:57:36 +02:00
|
|
|
PythonCPUPolicy::get_description() const
|
2006-02-22 16:16:08 +01:00
|
|
|
{
|
|
|
|
return _name;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2006-06-15 22:07:03 +02:00
|
|
|
Glib::ustring
|
2006-08-02 23:57:36 +02:00
|
|
|
PythonCPUPolicy::get_name() const
|
2006-06-15 22:07:03 +02:00
|
|
|
{
|
|
|
|
return _name;
|
|
|
|
}
|
|
|
|
|
2006-02-22 16:16:08 +01:00
|
|
|
bool
|
2006-08-02 23:57:36 +02:00
|
|
|
PythonCPUPolicy::is_pre_emptive() const throw(UserInterruptException)
|
2006-02-23 12:29:25 +01:00
|
|
|
{
|
2006-02-22 16:16:08 +01:00
|
|
|
PyObject* retval = PyObject_CallMethod(_adapter, "async_is_preemptive", NULL);
|
|
|
|
Py_DECREF(retval);
|
2006-06-29 10:44:30 +02:00
|
|
|
|
2006-02-22 16:16:08 +01:00
|
|
|
wait_unlock();
|
2006-06-29 10:44:30 +02:00
|
|
|
|
2006-02-22 16:16:08 +01:00
|
|
|
// Parse return value stored in global Python object
|
2006-03-10 15:54:24 +01:00
|
|
|
retval = PyObject_CallMethod(_adapter, "get_return_value", NULL);
|
2006-03-10 16:21:44 +01:00
|
|
|
assert(retval);
|
2006-03-10 15:54:24 +01:00
|
|
|
bool ret = PyObject_IsTrue(retval);
|
|
|
|
Py_DECREF(retval);
|
|
|
|
return ret;
|
2006-02-22 16:16:08 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int
|
2006-08-02 23:57:36 +02:00
|
|
|
PythonCPUPolicy::get_time_slice() const throw(UserInterruptException)
|
2006-06-09 18:51:53 +02:00
|
|
|
{
|
2006-02-22 16:16:08 +01:00
|
|
|
PyObject* retval = PyObject_CallMethod(_adapter, "async_get_time_slice", NULL);
|
2006-08-09 16:38:45 +02:00
|
|
|
|
2006-07-22 16:47:39 +02:00
|
|
|
// Do minimal debugging
|
2006-08-09 16:38:45 +02:00
|
|
|
if (!retval) PyErr_Print();
|
2006-07-22 16:47:39 +02:00
|
|
|
else Py_DECREF(retval);
|
2006-06-29 10:44:30 +02:00
|
|
|
|
2006-02-22 16:16:08 +01:00
|
|
|
wait_unlock();
|
2006-06-29 10:44:30 +02:00
|
|
|
|
2006-02-22 16:16:08 +01:00
|
|
|
// Parse return value stored in global Python object
|
2006-03-10 15:54:24 +01:00
|
|
|
retval = PyObject_CallMethod(_adapter, "get_return_value", NULL);
|
2006-03-10 16:21:44 +01:00
|
|
|
assert(retval);
|
2006-02-24 09:28:18 +01:00
|
|
|
long tmp = PyInt_AsLong(retval);
|
2006-03-10 15:54:24 +01:00
|
|
|
Py_DECREF(retval);
|
|
|
|
|
2006-02-24 09:28:18 +01:00
|
|
|
return tmp < 0 ? numeric_limits<int>::max() : static_cast<int>(tmp);
|
2006-02-22 16:16:08 +01:00
|
|
|
}
|
|
|
|
|
2006-06-29 10:44:30 +02:00
|
|
|
|
2006-02-22 16:16:08 +01:00
|
|
|
void
|
2006-08-02 23:57:36 +02:00
|
|
|
PythonCPUPolicy::wait_unlock() const throw(UserInterruptException)
|
2006-02-22 16:16:08 +01:00
|
|
|
{
|
2006-02-27 00:38:25 +01:00
|
|
|
PyThreadState* _save;
|
2006-02-23 22:50:43 +01:00
|
|
|
int i = 0; // We give the sort_queue() three seconds max time, then...
|
2006-06-29 10:44:30 +02:00
|
|
|
// we shot it stone dead! Bang.
|
2006-02-23 22:50:43 +01:00
|
|
|
|
|
|
|
bool still_locked;
|
2006-06-29 10:44:30 +02:00
|
|
|
do
|
|
|
|
{
|
|
|
|
Py_UNBLOCK_THREADS;
|
|
|
|
usleep(WAIT_FOR); // hack'a'ton! magggggiccc nummmbeeerrrrrs!!
|
|
|
|
Py_BLOCK_THREADS;
|
2006-08-09 16:38:45 +02:00
|
|
|
|
2006-06-29 10:44:30 +02:00
|
|
|
PyObject* retval = PyObject_CallMethod(_adapter, "mutex_test_lock", NULL);
|
|
|
|
assert(retval);
|
|
|
|
still_locked = PyObject_IsTrue(retval);
|
|
|
|
Py_DECREF(retval);
|
|
|
|
|
2006-08-09 16:38:45 +02:00
|
|
|
if (i++ > 12) /* waits for WAIT_FOR * 12 microseconds == 3 secs */
|
2006-02-27 00:38:25 +01:00
|
|
|
{
|
2006-06-29 10:44:30 +02:00
|
|
|
PyThreadState_Clear(_save);
|
|
|
|
// As the API documentation says, the caller of PyEval_RestoreThread
|
|
|
|
// should NOT possess the interpreter lock
|
2006-02-23 22:50:43 +01:00
|
|
|
Py_UNBLOCK_THREADS;
|
2006-06-29 10:44:30 +02:00
|
|
|
PyEval_RestoreThread(_save);
|
|
|
|
|
|
|
|
|
2006-08-09 15:24:42 +02:00
|
|
|
throw UserInterruptException(_("User-defined policy is "
|
2006-08-09 16:38:45 +02:00
|
|
|
"taking too long to terminate."));
|
2006-06-29 10:44:30 +02:00
|
|
|
}
|
|
|
|
}
|
2006-08-09 16:38:45 +02:00
|
|
|
while (still_locked);
|
2006-02-23 12:29:25 +01:00
|
|
|
|
2006-02-22 16:16:08 +01:00
|
|
|
// What we should really do here:
|
|
|
|
/* do {
|
|
|
|
enable python threads
|
|
|
|
wait for some time...
|
|
|
|
disable python threads
|
|
|
|
...check if user asked for interruption, reading a
|
|
|
|
syncronized variable...
|
|
|
|
...if he has, break
|
|
|
|
...else:
|
|
|
|
if the global lock is set:
|
2006-06-29 10:44:30 +02:00
|
|
|
stay in this loop
|
|
|
|
else:
|
|
|
|
all's went okay, can exit loop
|
2006-02-22 16:16:08 +01:00
|
|
|
} */
|
|
|
|
|
|
|
|
}
|
2006-08-09 15:24:42 +02:00
|
|
|
|
|
|
|
string
|
|
|
|
PythonCPUPolicy::get_exception_information()
|
|
|
|
{
|
2006-08-09 16:38:45 +02:00
|
|
|
if (PyErr_Occurred() == NULL)
|
2006-08-09 15:24:42 +02:00
|
|
|
return _("no error");
|
2006-08-09 16:38:45 +02:00
|
|
|
|
2006-08-09 15:24:42 +02:00
|
|
|
PyObject* pType = NULL;
|
|
|
|
PyObject* pValue = NULL;
|
|
|
|
PyObject* pTraceback = NULL;
|
|
|
|
|
|
|
|
PyErr_Fetch(&pType, &pValue, &pTraceback);
|
|
|
|
|
|
|
|
string msg;
|
|
|
|
|
2006-08-09 16:38:45 +02:00
|
|
|
if (pValue != NULL)
|
2006-08-09 15:24:42 +02:00
|
|
|
{
|
|
|
|
PyObject* pValueStr = PyObject_Str(pValue);
|
|
|
|
|
|
|
|
msg = PyString_AsString(pValueStr);
|
|
|
|
|
|
|
|
Py_DECREF(pValueStr);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
msg = string(_("no available information for this error"));
|
|
|
|
|
2006-08-09 16:38:45 +02:00
|
|
|
if (pType != NULL) Py_DECREF(pType);
|
|
|
|
if (pValue != NULL) Py_DECREF(pValue);
|
|
|
|
if (pTraceback != NULL) Py_DECREF(pTraceback);
|
2006-08-09 15:24:42 +02:00
|
|
|
|
|
|
|
return msg;
|
|
|
|
}
|
|
|
|
|