#include "plugin.h"
#include "common.h"
+#include <pthread.h>
+
#if !defined(USE_ITHREADS)
# error "Perl does not support ithreads!"
#endif /* !defined(USE_ITHREADS) */
+/* clear the Perl sub's stack frame
+ * (this should only be used inside an XSUB) */
+#define CLEAR_STACK_FRAME PL_stack_sp = PL_stack_base + *PL_markstack_ptr
+
#define PLUGIN_INIT 0
#define PLUGIN_READ 1
#define PLUGIN_WRITE 2
static XS (Collectd_plugin_unregister_ds);
static XS (Collectd_plugin_dispatch_values);
static XS (Collectd_plugin_log);
+static XS (Collectd_call_by_name);
+
+/*
+ * private data types
+ */
+
+typedef struct c_ithread_s {
+ /* the thread's Perl interpreter */
+ PerlInterpreter *interp;
+
+ /* double linked list of threads */
+ struct c_ithread_s *prev;
+ struct c_ithread_s *next;
+} c_ithread_t;
+
+typedef struct {
+ c_ithread_t *head;
+ c_ithread_t *tail;
+
+#if COLLECT_DEBUG
+ /* some usage stats */
+ int number_of_threads;
+#endif /* COLLECT_DEBUG */
+
+ pthread_mutex_t mutex;
+} c_ithread_list_t;
/*
* private variables
*/
-static PerlInterpreter *perl = NULL;
+/* if perl_threads != NULL perl_threads->head must
+ * point to the "base" thread */
+static c_ithread_list_t *perl_threads = NULL;
-static int perl_argc = 0;
+/* the key used to store each pthread's ithread */
+static pthread_key_t perl_thr_key;
+
+static int perl_argc = 0;
static char **perl_argv = NULL;
static char base_name[DATA_MAX_NAME_LEN] = "";
{ "Collectd::plugin_unregister_data_set", Collectd_plugin_unregister_ds },
{ "Collectd::plugin_dispatch_values", Collectd_plugin_dispatch_values },
{ "Collectd::plugin_log", Collectd_plugin_log },
+ { "Collectd::call_by_name", Collectd_call_by_name },
{ "", NULL }
};
} /* static XS (Collectd_plugin_log) */
/*
+ * Collectd::call_by_name (...).
+ *
+ * Call a Perl sub identified by its name passed through $Collectd::cb_name.
+ */
+static XS (Collectd_call_by_name)
+{
+ SV *tmp = NULL;
+ char *name = NULL;
+
+ if (NULL == (tmp = get_sv ("Collectd::cb_name", 0))) {
+ sv_setpv (get_sv ("@", 1), "cb_name has not been set");
+ CLEAR_STACK_FRAME;
+ return;
+ }
+
+ name = SvPV_nolen (tmp);
+
+ if (NULL == get_cv (name, 0)) {
+ sv_setpvf (get_sv ("@", 1), "unknown callback \"%s\"", name);
+ CLEAR_STACK_FRAME;
+ return;
+ }
+
+ /* simply pass on the subroutine call without touching the stack,
+ * thus leaving any arguments and return values in place */
+ call_pv (name, 0);
+} /* static XS (Collectd_call_by_name) */
+
+/*
+ * collectd's perl interpreter based thread implementation.
+ *
+ * This has been inspired by Perl's ithreads introduced in version 5.6.0.
+ */
+
+/* must be called with perl_threads->mutex locked */
+static void c_ithread_destroy (c_ithread_t *ithread)
+{
+ dTHXa (ithread->interp);
+
+ assert (NULL != perl_threads);
+
+ PERL_SET_CONTEXT (aTHX);
+ log_debug ("Shutting down Perl interpreter %p...", aTHX);
+
+#if COLLECT_DEBUG
+ sv_report_used ();
+
+ --perl_threads->number_of_threads;
+#endif /* COLLECT_DEBUG */
+
+ perl_destruct (aTHX);
+ perl_free (aTHX);
+
+ if (NULL == ithread->prev)
+ perl_threads->head = ithread->next;
+ else
+ ithread->prev->next = ithread->next;
+
+ if (NULL == ithread->next)
+ perl_threads->tail = ithread->prev;
+ else
+ ithread->next->prev = ithread->prev;
+
+ sfree (ithread);
+ return;
+} /* static void c_ithread_destroy (c_ithread_t *) */
+
+static void c_ithread_destructor (void *arg)
+{
+ c_ithread_t *ithread = (c_ithread_t *)arg;
+ c_ithread_t *t = NULL;
+
+ if (NULL == perl_threads)
+ return;
+
+ pthread_mutex_lock (&perl_threads->mutex);
+
+ for (t = perl_threads->head; NULL != t; t = t->next)
+ if (t == ithread)
+ break;
+
+ /* the ithread no longer exists */
+ if (NULL == t)
+ return;
+
+ c_ithread_destroy (ithread);
+
+ pthread_mutex_unlock (&perl_threads->mutex);
+ return;
+} /* static void c_ithread_destructor (void *) */
+
+/* must be called with perl_threads->mutex locked */
+static c_ithread_t *c_ithread_create (PerlInterpreter *base)
+{
+ c_ithread_t *t = NULL;
+ dTHXa (NULL);
+
+ assert (NULL != perl_threads);
+
+ t = (c_ithread_t *)smalloc (sizeof (c_ithread_t));
+ memset (t, 0, sizeof (c_ithread_t));
+
+ t->interp = (NULL == base)
+ ? NULL
+ : perl_clone (base, CLONEf_KEEP_PTR_TABLE);
+
+ aTHX = t->interp;
+
+ if (NULL != base) {
+ av_clear (PL_endav);
+ av_undef (PL_endav);
+ PL_endav = Nullav;
+ }
+
+#if COLLECT_DEBUG
+ ++perl_threads->number_of_threads;
+#endif /* COLLECT_DEBUG */
+
+ t->next = NULL;
+
+ if (NULL == perl_threads->tail) {
+ perl_threads->head = t;
+ t->prev = NULL;
+ }
+ else {
+ perl_threads->tail->next = t;
+ t->prev = perl_threads->tail;
+ }
+
+ perl_threads->tail = t;
+
+ pthread_setspecific (perl_thr_key, (const void *)t);
+ return t;
+} /* static c_ithread_t *c_ithread_create (PerlInterpreter *) */
+
+/*
* Interface to collectd.
*/
static int perl_init (void)
{
- dTHXa (NULL);
+ dTHX;
- if (NULL == perl)
+ if (NULL == perl_threads)
return 0;
- PERL_SET_CONTEXT (perl);
- aTHX = perl;
+ if (NULL == aTHX) {
+ c_ithread_t *t = NULL;
+
+ pthread_mutex_lock (&perl_threads->mutex);
+ t = c_ithread_create (perl_threads->head->interp);
+ pthread_mutex_unlock (&perl_threads->mutex);
+
+ aTHX = t->interp;
+ }
+ log_debug ("perl_init: c_ithread: interp = %p (active threads: %i)",
+ aTHX, perl_threads->number_of_threads);
return pplugin_call_all (aTHX_ PLUGIN_INIT);
} /* static int perl_init (void) */
static int perl_read (void)
{
- dTHXa (NULL);
+ dTHX;
- if (NULL == perl)
+ if (NULL == perl_threads)
return 0;
- PERL_SET_CONTEXT (perl);
- aTHX = perl;
+ if (NULL == aTHX) {
+ c_ithread_t *t = NULL;
+
+ pthread_mutex_lock (&perl_threads->mutex);
+ t = c_ithread_create (perl_threads->head->interp);
+ pthread_mutex_unlock (&perl_threads->mutex);
+ aTHX = t->interp;
+ }
+
+ log_debug ("perl_read: c_ithread: interp = %p (active threads: %i)",
+ aTHX, perl_threads->number_of_threads);
return pplugin_call_all (aTHX_ PLUGIN_READ);
} /* static int perl_read (void) */
static int perl_write (const data_set_t *ds, const value_list_t *vl)
{
- dTHXa (NULL);
+ dTHX;
- if (NULL == perl)
+ if (NULL == perl_threads)
return 0;
- PERL_SET_CONTEXT (perl);
- aTHX = perl;
+ if (NULL == aTHX) {
+ c_ithread_t *t = NULL;
+
+ pthread_mutex_lock (&perl_threads->mutex);
+ t = c_ithread_create (perl_threads->head->interp);
+ pthread_mutex_unlock (&perl_threads->mutex);
+ aTHX = t->interp;
+ }
+
+ log_debug ("perl_write: c_ithread: interp = %p (active threads: %i)",
+ aTHX, perl_threads->number_of_threads);
return pplugin_call_all (aTHX_ PLUGIN_WRITE, ds, vl);
} /* static int perl_write (const data_set_t *, const value_list_t *) */
static void perl_log (int level, const char *msg)
{
- dTHXa (NULL);
+ dTHX;
- if (NULL == perl)
+ if (NULL == perl_threads)
return;
- PERL_SET_CONTEXT (perl);
- aTHX = perl;
+ if (NULL == aTHX) {
+ c_ithread_t *t = NULL;
+
+ pthread_mutex_lock (&perl_threads->mutex);
+ t = c_ithread_create (perl_threads->head->interp);
+ pthread_mutex_unlock (&perl_threads->mutex);
+
+ aTHX = t->interp;
+ }
pplugin_call_all (aTHX_ PLUGIN_LOG, level, msg);
return;
static int perl_shutdown (void)
{
+ c_ithread_t *t = NULL;
+
int ret = 0;
- dTHXa (NULL);
+ dTHX;
plugin_unregister_complex_config ("perl");
- if (NULL == perl)
+ if (NULL == perl_threads)
return 0;
- PERL_SET_CONTEXT (perl);
- aTHX = perl;
+ if (NULL == aTHX) {
+ c_ithread_t *t = NULL;
+
+ pthread_mutex_lock (&perl_threads->mutex);
+ t = c_ithread_create (perl_threads->head->interp);
+ pthread_mutex_unlock (&perl_threads->mutex);
+
+ aTHX = t->interp;
+ }
+
+ log_debug ("perl_shutdown: c_ithread: interp = %p (active threads: %i)",
+ aTHX, perl_threads->number_of_threads);
plugin_unregister_log ("perl");
plugin_unregister_init ("perl");
ret = pplugin_call_all (aTHX_ PLUGIN_SHUTDOWN);
-#if COLLECT_DEBUG
- sv_report_used ();
-#endif /* COLLECT_DEBUG */
+ pthread_mutex_lock (&perl_threads->mutex);
+ t = perl_threads->tail;
+
+ while (NULL != t) {
+ c_ithread_t *thr = t;
- perl_destruct (perl);
- perl_free (perl);
- perl = NULL;
+ /* the pointer has to be advanced before destroying
+ * the thread as this will free the memory */
+ t = t->prev;
+
+ c_ithread_destroy (thr);
+ }
+
+ pthread_mutex_unlock (&perl_threads->mutex);
+ pthread_mutex_destroy (&perl_threads->mutex);
+
+ sfree (perl_threads);
+
+ pthread_key_delete (perl_thr_key);
PERL_SYS_TERM ();
{
dTHXa (NULL);
- if (NULL != perl)
+ if (NULL != perl_threads)
return 0;
log_info ("Initializing Perl interpreter...");
}
#endif /* COLLECT_DEBUG */
+ if (0 != pthread_key_create (&perl_thr_key, c_ithread_destructor)) {
+ log_err ("init_pi: pthread_key_create failed");
+
+ /* this must not happen - cowardly giving up if it does */
+ exit (1);
+ }
+
PERL_SYS_INIT3 (&argc, &argv, &environ);
- if (NULL == (perl = perl_alloc ())) {
+ perl_threads = (c_ithread_list_t *)smalloc (sizeof (c_ithread_list_t));
+ memset (perl_threads, 0, sizeof (c_ithread_list_t));
+
+ pthread_mutex_init (&perl_threads->mutex, NULL);
+ /* locking the mutex should not be necessary at this point
+ * but let's just do it for the sake of completeness */
+ pthread_mutex_lock (&perl_threads->mutex);
+
+ perl_threads->head = c_ithread_create (NULL);
+ perl_threads->tail = perl_threads->head;
+
+ if (NULL == (perl_threads->head->interp = perl_alloc ())) {
log_err ("module_register: Not enough memory.");
exit (3);
}
- aTHX = perl;
- perl_construct (perl);
+ aTHX = perl_threads->head->interp;
+ pthread_mutex_unlock (&perl_threads->mutex);
+
+ perl_construct (aTHX);
PL_exit_flags |= PERL_EXIT_DESTRUCT_END;
- if (0 != perl_parse (perl, xs_init, argc, argv, NULL)) {
+ if (0 != perl_parse (aTHX_ xs_init, argc, argv, NULL)) {
log_err ("module_register: Unable to bootstrap Collectd.");
exit (1);
}
/* Set $0 to "collectd" because perl_parse() has to set it to "-e". */
sv_setpv (get_sv ("0", 0), "collectd");
- perl_run (perl);
+ perl_run (aTHX);
plugin_register_log ("perl", perl_log);
plugin_register_init ("perl", perl_init);
}
init_pi (perl_argc, perl_argv);
- aTHX = perl;
+ assert (NULL != perl_threads);
+ assert (NULL != perl_threads->head);
+
+ aTHX = perl_threads->head->interp;
log_debug ("perl_config: loading perl plugin \"%s\"", value);
load_module (PERL_LOADMOD_NOIMPORT,
{
int i = 0;
- dTHXa (NULL);
+ dTHX;
- PERL_SET_CONTEXT (perl);
- aTHX = perl;
+ /* dTHX does not get any valid values in case Perl
+ * has not been initialized */
+ if (NULL == perl_threads)
+ aTHX = NULL;
for (i = 0; i < ci->children_num; ++i) {
oconfig_item_t *c = ci->children + i;