summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorTushar Gohad <tushar.gohad@intel.com>2015-11-22 15:58:01 +0000
committerTushar Gohad <tushar.gohad@intel.com>2015-11-22 15:58:01 +0000
commit6094a5baa533f5d5ddaac4f2c27fdc085cf8f6cd (patch)
treedbfec5381c9860fd19c72652400a159ccb800a71
parentb14ac48319554d1399820268e2d81317e072e552 (diff)
downloadpyeclib-stderr.tar.gz
Redirect stderr during VALID_EC_TYPES evalstderr
-rw-r--r--pyeclib/core.py12
-rw-r--r--pyeclib/ec_iface.py22
-rw-r--r--src/c/pyeclib_c/pyeclib_c.c77
3 files changed, 89 insertions, 22 deletions
diff --git a/pyeclib/core.py b/pyeclib/core.py
index e233915..a0174e1 100644
--- a/pyeclib/core.py
+++ b/pyeclib/core.py
@@ -35,7 +35,8 @@ pyver = float('%s.%s' % sys.version_info[:2])
class ECPyECLibDriver(object):
def __init__(self, k, m, hd, ec_type,
- chksum_type=PyECLib_FRAGHDRCHKSUM_Types.none):
+ chksum_type=PyECLib_FRAGHDRCHKSUM_Types.none,
+ validate=False):
self.k = k
self.m = m
self.hd = hd
@@ -56,7 +57,8 @@ class ECPyECLibDriver(object):
ec_type.value,
self.hd,
self.inline_chksum,
- self.algsig_chksum)
+ self.algsig_chksum,
+ validate)
def encode(self, data_bytes):
return pyeclib_c.encode(self.handle, data_bytes)
@@ -136,7 +138,8 @@ class ECPyECLibDriver(object):
class ECNullDriver(object):
- def __init__(self, k, m, hd, ec_type=None, chksum_type=None):
+ def __init__(self, k, m, hd, ec_type=None, chksum_type=None,
+ validate=False):
self.k = k
self.m = m
self.hd = hd
@@ -173,7 +176,8 @@ class ECNullDriver(object):
#
class ECStripingDriver(object):
- def __init__(self, k, m, hd, ec_type=None, chksum_type=None):
+ def __init__(self, k, m, hd, ec_type=None, chksum_type=None,
+ validate=False):
"""Stripe an arbitrary-sized string into k fragments
:param k: the number of data fragments to stripe
:param m: the number of parity fragments to stripe
diff --git a/pyeclib/ec_iface.py b/pyeclib/ec_iface.py
index 2b459d6..eefc370 100644
--- a/pyeclib/ec_iface.py
+++ b/pyeclib/ec_iface.py
@@ -112,6 +112,7 @@ class ECDriver(object):
self.hd = -1
self.ec_type = None
self.chksum_type = None
+ self.validate = False
for (key, value) in kwargs.items():
if key == "k":
try:
@@ -144,6 +145,9 @@ class ECDriver(object):
else:
raise ECDriverError(
"%s is not a valid checksum type for PyECLib!" % value)
+ elif key == "validate":
+ # validate if the ec type is available (runtime check)
+ self.validate = value
if self.hd == -1:
self.hd = self.m
@@ -159,7 +163,10 @@ class ECDriver(object):
m=self.m,
hd=self.hd,
ec_type=self.ec_type,
- chksum_type=self.chksum_type)
+ chksum_type=self.chksum_type,
+ validate=int(self.validate)
+ )
+
#
# Verify that the imported library implements the required functions
#
@@ -469,14 +476,21 @@ ALL_EC_TYPES = [
def _PyECLibValidECTypes():
available_ec_types = []
for _type in ALL_EC_TYPES:
+ print "== checking type %r" % _type
try:
if _type is 'shss':
- ECDriver(k=10, m=4, ec_type=_type)
+ _m = 4
else:
- ECDriver(k=10, m=5, ec_type=_type)
+ _m = 5
+ driver = ECDriver(k=10, m=_m, ec_type=_type, validate=True)
+ print "driver = %r" % driver
available_ec_types.append(_type)
except Exception:
- pass
+ import sys
+ e = sys.exc_info()[0]
+ print( "****Error: %s" % e )
+ continue
+ print available_ec_types
return available_ec_types
diff --git a/src/c/pyeclib_c/pyeclib_c.c b/src/c/pyeclib_c/pyeclib_c.c
index ea6ac30..05fa015 100644
--- a/src/c/pyeclib_c/pyeclib_c.c
+++ b/src/c/pyeclib_c/pyeclib_c.c
@@ -24,6 +24,8 @@
* ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
+#include <stdio.h>
+#include <paths.h>
#include <Python.h>
#include <math.h>
#include <bytesobject.h>
@@ -188,13 +190,43 @@ pyeclib_c_seterr(int ret, const char * prefix)
PyErr_SetString(eo, err);
}
+static int stderr_fd;
+static fpos_t stderr_fpos;
+#ifndef _PATH_DEVNULL
+#define _PATH_DEVNULL "/dev/null"
+#endif
+
+static void redirect_stderr(void)
+{
+ fprintf(stderr, "%s: START\n", __func__);
+ fflush(stderr);
+ fgetpos(stderr, &stderr_fpos);
+ stderr_fd = dup(fileno(stderr));
+ freopen(_PATH_DEVNULL, "w", stderr);
+ fprintf(stderr, "%s: END\n", __func__);
+}
+
+static void restore_stderr(void)
+{
+ fprintf(stderr, "%s: START\n", __func__);
+ fflush(stderr);
+ dup2(stderr_fd, fileno(stderr));
+ close(stderr_fd);
+ clearerr(stderr);
+ fsetpos(stderr, &stderr_fpos); /* for C9X */
+ fprintf(stderr, "%s: END\n", __func__);
+}
+
/**
* Constructor method for creating a new pyeclib object using the given parameters.
*
* @param k integer number of data elements
* @param m integer number of checksum elements
- * @param w integer word size in bytes
+ * @param hd hamming distance
* @param backend_id erasure coding backend
+ * @param use_inline_chksum type of inline fragment header checksum
+ * @param use_algsig_chksum use algorithmic signature for fragment header checksum
+ * @param validate only validate backend and params, close handle immediately
* @return pointer to PyObject or NULL on error
*/
static PyObject *
@@ -202,12 +234,15 @@ pyeclib_c_init(PyObject *self, PyObject *args)
{
pyeclib_t *pyeclib_handle = NULL;
PyObject *pyeclib_obj_handle = NULL;
- int k, m, hd=0;
+ int k, m, hd = 0, validate = 0;
int use_inline_chksum = 0, use_algsig_chksum = 0;
const ec_backend_id_t backend_id;
/* Obtain and validate the method parameters */
- if (!PyArg_ParseTuple(args, "iii|iii", &k, &m, &backend_id, &hd, &use_inline_chksum, &use_algsig_chksum)) {
+ if (!PyArg_ParseTuple(args, "iii|iiiii",
+ &k, &m, &backend_id, &hd, &use_inline_chksum,
+ &use_algsig_chksum, &validate)) {
+ fprintf (stderr, "%s:%d\n", __func__, __LINE__);
pyeclib_c_seterr(-EINVALIDPARAMS, "pyeclib_c_init ERROR: ");
return NULL;
}
@@ -215,8 +250,9 @@ pyeclib_c_init(PyObject *self, PyObject *args)
/* Allocate and initialize the pyeclib object */
pyeclib_handle = (pyeclib_t *) alloc_zeroed_buffer(sizeof(pyeclib_t));
if (NULL == pyeclib_handle) {
+ fprintf (stderr, "%s:%d\n", __func__, __LINE__);
pyeclib_c_seterr(-ENOMEM, "pyeclib_c_init ERROR: ");
- goto error;
+ goto cleanup;
}
pyeclib_handle->ec_args.k = k;
@@ -224,10 +260,21 @@ pyeclib_c_init(PyObject *self, PyObject *args)
pyeclib_handle->ec_args.hd = hd;
pyeclib_handle->ec_args.ct = use_inline_chksum ? CHKSUM_CRC32 : CHKSUM_NONE;
+ fprintf (stderr, "backend = %d, validate = %d\n", backend_id, validate);
+ if (validate)
+ redirect_stderr();
+
pyeclib_handle->ec_desc = liberasurecode_instance_create(backend_id, &(pyeclib_handle->ec_args));
if (pyeclib_handle->ec_desc <= 0) {
+ if (validate) {
+ restore_stderr();
+ check_and_free_buffer(pyeclib_handle);
+ fprintf (stdout, "NOT AVAILABLE\n");
+ return NULL;
+ }
+
pyeclib_c_seterr(-EINVALIDPARAMS, "pyeclib_c_init ERROR: ");
- goto error;
+ goto cleanup;
}
/* Prepare the python object to return */
@@ -235,27 +282,27 @@ pyeclib_c_init(PyObject *self, PyObject *args)
pyeclib_obj_handle = PyCapsule_New(pyeclib_handle, PYECC_HANDLE_NAME,
pyeclib_c_destructor);
#else
- pyeclib_obj_handle = PyCObject_FromVoidPtrAndDesc(pyeclib_handle,
- (void *) PYECC_HANDLE_NAME,
- pyeclib_c_destructor);
+ pyeclib_obj_handle = PyCObject_FromVoidPtrAndDesc(
+ pyeclib_handle, (void *) PYECC_HANDLE_NAME,
+ pyeclib_c_destructor);
#endif /* Py_CAPSULE_H */
/* Clean up the allocated memory on error, or update the ref count */
if (pyeclib_obj_handle == NULL) {
pyeclib_c_seterr(-EINVALIDPARAMS, "pyeclib_c_init ERROR: ");
- goto error;
+ goto cleanup;
} else {
Py_INCREF(pyeclib_obj_handle);
}
- goto exit;
+exit:
+ fprintf (stderr, "handle: %p\n", pyeclib_obj_handle);
+ return pyeclib_obj_handle;
-error:
+cleanup:
check_and_free_buffer(pyeclib_handle);
pyeclib_obj_handle = NULL;
-
-exit:
- return pyeclib_obj_handle;
+ goto exit;
}
@@ -266,6 +313,7 @@ static void
pyeclib_c_destructor(PyObject *obj)
{
pyeclib_t *pyeclib_handle = NULL; /* pyeclib object to destroy */
+ fprintf(stderr, "%s\n", __func__);
if (!PyCapsule_CheckExact(obj)) {
pyeclib_c_seterr(-1, "pyeclib_c_destructor ERROR: ");
@@ -276,6 +324,7 @@ pyeclib_c_destructor(PyObject *obj)
if (pyeclib_handle == NULL) {
pyeclib_c_seterr(-1, "pyeclib_c_destructor ERROR: ");
} else {
+ fprintf (stderr, "cleaning instance desc = %d\n", pyeclib_handle->ec_desc);
check_and_free_buffer(pyeclib_handle);
}
return;