Blame xfer-src/xfer.c

Packit 23ab03
/*
Packit 23ab03
 * Copyright (c) 2008-2012 Zmanda, Inc.  All Rights Reserved.
Packit 23ab03
 * Copyright (c) 2013-2016 Carbonite, Inc.  All Rights Reserved.
Packit 23ab03
 *
Packit 23ab03
 * This program is free software; you can redistribute it and/or
Packit 23ab03
 * modify it under the terms of the GNU General Public License
Packit 23ab03
 * as published by the Free Software Foundation; either version 2
Packit 23ab03
 * of the License, or (at your option) any later version.
Packit 23ab03
 *
Packit 23ab03
 * This program is distributed in the hope that it will be useful, but
Packit 23ab03
 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
Packit 23ab03
 * or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
Packit 23ab03
 * for more details.
Packit 23ab03
 *
Packit 23ab03
 * You should have received a copy of the GNU General Public License along
Packit 23ab03
 * with this program; if not, write to the Free Software Foundation, Inc.,
Packit 23ab03
 * 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
Packit 23ab03
 *
Packit 23ab03
 * Contact information: Carbonite Inc., 756 N Pastoria Ave
Packit 23ab03
 * Sunnyvale, CA 94085, or: http://www.zmanda.com
Packit 23ab03
 */
Packit 23ab03
Packit 23ab03
#include "amanda.h"
Packit 23ab03
#include "amxfer.h"
Packit 23ab03
#include "element-glue.h"
Packit 23ab03
Packit 23ab03
/* XMsgSource objects are GSource "subclasses" which manage
Packit 23ab03
 * a queue of messages, delivering those messages via callback
Packit 23ab03
 * in the mainloop.  Messages can be *sent* from any thread without
Packit 23ab03
 * any concern for locking, but must only be received in the main
Packit 23ab03
 * thread, in the default GMainContext.
Packit 23ab03
 *
Packit 23ab03
 * An XMsgSource pointer can be cast to a GSource pointer as
Packit 23ab03
 * necessary.
Packit 23ab03
 */
Packit 23ab03
typedef struct XMsgSource {
Packit 23ab03
    GSource source; /* must be the first element of the struct */
Packit 23ab03
    Xfer *xfer;
Packit 23ab03
} XMsgSource;
Packit 23ab03
Packit 23ab03
/* forward prototypes */
Packit 23ab03
static void xfer_set_status(Xfer *xfer, xfer_status status);
Packit 23ab03
static XMsgSource *xmsgsource_new(Xfer *xfer);
Packit 23ab03
static void link_elements(Xfer *xfer);
Packit 23ab03
Packit 23ab03
Xfer *
Packit 23ab03
xfer_new(
Packit 23ab03
    XferElement **elements,
Packit 23ab03
    unsigned int nelements)
Packit 23ab03
{
Packit 23ab03
    Xfer *xfer = g_new0(Xfer, 1);
Packit 23ab03
    unsigned int i;
Packit 23ab03
Packit 23ab03
    g_assert(elements);
Packit 23ab03
    g_assert(nelements >= 2);
Packit 23ab03
Packit 23ab03
    xfer->status = XFER_INIT;
Packit 23ab03
    xfer->status_mutex = g_mutex_new();
Packit 23ab03
    xfer->status_cond = g_cond_new();
Packit 23ab03
    xfer->fd_mutex = g_mutex_new();
Packit 23ab03
Packit 23ab03
    xfer->refcount = 1;
Packit 23ab03
    xfer->repr = NULL;
Packit 23ab03
Packit 23ab03
    /* Create our message source and corresponding queue */
Packit 23ab03
    xfer->msg_source = xmsgsource_new(xfer);
Packit 23ab03
    xfer->queue = g_async_queue_new();
Packit 23ab03
Packit 23ab03
    /* copy the elements in, verifying that they're all XferElement objects */
Packit 23ab03
    xfer->elements = g_ptr_array_sized_new(nelements);
Packit 23ab03
    for (i = 0; i < nelements; i++) {
Packit 23ab03
	g_assert(elements[i] != NULL);
Packit 23ab03
	g_assert(IS_XFER_ELEMENT(elements[i]));
Packit 23ab03
	g_assert(elements[i]->xfer == NULL);
Packit 23ab03
Packit 23ab03
	g_ptr_array_add(xfer->elements, (gpointer)elements[i]);
Packit 23ab03
Packit 23ab03
	g_object_ref(elements[i]);
Packit 23ab03
	elements[i]->xfer = xfer;
Packit 23ab03
    }
Packit 23ab03
Packit 23ab03
    return xfer;
Packit 23ab03
}
Packit 23ab03
Packit 23ab03
void
Packit 23ab03
xfer_ref(
Packit 23ab03
    Xfer *xfer)
Packit 23ab03
{
Packit 23ab03
    ++xfer->refcount;
Packit 23ab03
}
Packit 23ab03
Packit 23ab03
void
Packit 23ab03
xfer_unref(
Packit 23ab03
    Xfer *xfer)
Packit 23ab03
{
Packit 23ab03
    unsigned int i;
Packit 23ab03
    XMsg *msg;
Packit 23ab03
Packit 23ab03
    if (!xfer) return; /* be friendly to NULLs */
Packit 23ab03
Packit 23ab03
    if (--xfer->refcount > 0) return;
Packit 23ab03
Packit 23ab03
    g_assert(xfer != NULL);
Packit 23ab03
    g_assert(xfer->status == XFER_INIT || xfer->status == XFER_DONE);
Packit 23ab03
Packit 23ab03
    /* Divorce ourselves from the message source */
Packit 23ab03
    xfer->msg_source->xfer = NULL;
Packit 23ab03
    g_source_unref((GSource *)xfer->msg_source);
Packit 23ab03
    xfer->msg_source = NULL;
Packit 23ab03
Packit 23ab03
    /* Try to empty the message queue */
Packit 23ab03
    while ((msg = (XMsg *)g_async_queue_try_pop(xfer->queue))) {
Packit 23ab03
	g_warning("Dropping XMsg from %s because the XMsgSource is being destroyed", 
Packit 23ab03
	    xfer_element_repr(msg->elt));
Packit 23ab03
	g_debug("MSG: %s", xmsg_repr(msg));
Packit 23ab03
	xmsg_free(msg);
Packit 23ab03
    }
Packit 23ab03
    g_async_queue_unref(xfer->queue);
Packit 23ab03
Packit 23ab03
    g_mutex_free(xfer->status_mutex);
Packit 23ab03
    g_cond_free(xfer->status_cond);
Packit 23ab03
    g_mutex_free(xfer->fd_mutex);
Packit 23ab03
Packit 23ab03
    /* Free our references to the elements, and also set the 'xfer'
Packit 23ab03
     * attribute of each to NULL, making them "unattached" (although 
Packit 23ab03
     * subsequent reuse of elements is untested). */
Packit 23ab03
    for (i = 0; i < xfer->elements->len; i++) {
Packit 23ab03
	XferElement *elt = (XferElement *)g_ptr_array_index(xfer->elements, i);
Packit 23ab03
Packit 23ab03
	elt->xfer = NULL;
Packit 23ab03
	g_object_unref(elt);
Packit 23ab03
    }
Packit 23ab03
    g_ptr_array_free(xfer->elements, TRUE);
Packit 23ab03
Packit 23ab03
    if (xfer->repr)
Packit 23ab03
	g_free(xfer->repr);
Packit 23ab03
Packit 23ab03
    g_free(xfer);
Packit 23ab03
}
Packit 23ab03
Packit 23ab03
GSource *
Packit 23ab03
xfer_get_source(
Packit 23ab03
    Xfer *xfer)
Packit 23ab03
{
Packit 23ab03
    return (GSource *)xfer->msg_source;
Packit 23ab03
}
Packit 23ab03
Packit 23ab03
void
Packit 23ab03
xfer_queue_message(
Packit 23ab03
    Xfer *xfer,
Packit 23ab03
    XMsg *msg)
Packit 23ab03
{
Packit 23ab03
    g_assert(xfer != NULL);
Packit 23ab03
    g_assert(msg != NULL);
Packit 23ab03
Packit 23ab03
    g_debug("xfer_queue_message: MSG: %s", xmsg_repr(msg));
Packit 23ab03
    g_async_queue_push(xfer->queue, (gpointer)msg);
Packit 23ab03
Packit 23ab03
    /* TODO: don't do this if we're in the main thread */
Packit 23ab03
    g_main_context_wakeup(NULL);
Packit 23ab03
}
Packit 23ab03
Packit 23ab03
char *
Packit 23ab03
xfer_repr(
Packit 23ab03
    Xfer *xfer)
Packit 23ab03
{
Packit 23ab03
    char *tmpbuf;
Packit 23ab03
    unsigned int i;
Packit 23ab03
Packit 23ab03
    if (!xfer->repr) {
Packit 23ab03
	g_free(xfer->repr);
Packit 23ab03
	xfer->repr = g_strdup_printf("
Packit 23ab03
	for (i = 0; i < xfer->elements->len; i++) {
Packit 23ab03
	    XferElement *elt = (XferElement *)g_ptr_array_index(xfer->elements, i);
Packit 23ab03
	    tmpbuf = g_strconcat(xfer->repr, (i==0)?"":" -> ", xfer_element_repr(elt), NULL);
Packit 23ab03
	    g_free(xfer->repr);
Packit 23ab03
	    xfer->repr = tmpbuf;
Packit 23ab03
	}
Packit 23ab03
	tmpbuf = g_strconcat(xfer->repr, ")>", NULL);
Packit 23ab03
	g_free(xfer->repr);
Packit 23ab03
	xfer->repr = tmpbuf;
Packit 23ab03
    }
Packit 23ab03
Packit 23ab03
    return xfer->repr;
Packit 23ab03
}
Packit 23ab03
Packit 23ab03
void
Packit 23ab03
xfer_start(
Packit 23ab03
    Xfer *xfer,
Packit 23ab03
    gint64 offset,
Packit 23ab03
    gint64 size)
Packit 23ab03
{
Packit 23ab03
    unsigned int len;
Packit 23ab03
    unsigned int i;
Packit 23ab03
    gboolean setup_ok;
Packit 23ab03
Packit 23ab03
    g_assert(xfer != NULL);
Packit 23ab03
    g_assert(xfer->status == XFER_INIT || xfer->status == XFER_DONE);
Packit 23ab03
    g_assert(xfer->elements->len >= 2);
Packit 23ab03
Packit 23ab03
    g_debug("Starting %s", xfer_repr(xfer));
Packit 23ab03
    /* set the status to XFER_START and add a reference to our count, so that
Packit 23ab03
     * we are not freed while still in operation.  We'll drop this reference
Packit 23ab03
     * when the status becomes XFER_DONE. */
Packit 23ab03
    xfer_ref(xfer);
Packit 23ab03
    xfer->num_active_elements = 0;
Packit 23ab03
    xfer_set_status(xfer, XFER_START);
Packit 23ab03
Packit 23ab03
    /* Link the elements.  This calls error() on failure, and rewrites
Packit 23ab03
     * xfer->elements */
Packit 23ab03
    link_elements(xfer);
Packit 23ab03
Packit 23ab03
    /* Tell all elements to set up.  This is done before upstream and downstream
Packit 23ab03
     * are set so that elements cannot interfere with one another before setup()
Packit 23ab03
     * is completed. */
Packit 23ab03
    setup_ok = TRUE;
Packit 23ab03
    for (i = 0; i < xfer->elements->len; i++) {
Packit 23ab03
	XferElement *xe = (XferElement *)g_ptr_array_index(xfer->elements, i);
Packit 23ab03
	if (!xfer_element_setup(xe)) {
Packit 23ab03
	    setup_ok = FALSE;
Packit 23ab03
	    break;
Packit 23ab03
	}
Packit 23ab03
    }
Packit 23ab03
Packit 23ab03
    /* If setup_ok is false, then there is an XMSG_CANCEL in the message queue
Packit 23ab03
     * already, so skip calling start for any of the elements and send an
Packit 23ab03
     * XMSG_DONE, since none of the elements will do so. */
Packit 23ab03
Packit 23ab03
    if (setup_ok) {
Packit 23ab03
	/* Set the upstream and downstream links between elements */
Packit 23ab03
	len = xfer->elements->len;
Packit 23ab03
	for (i = 0; i < len; i++) {
Packit 23ab03
	    XferElement *elt = g_ptr_array_index(xfer->elements, i);
Packit 23ab03
Packit 23ab03
	    if (i > 0)
Packit 23ab03
		elt->upstream = g_ptr_array_index(xfer->elements, i-1);
Packit 23ab03
	    if (i < len-1)
Packit 23ab03
		elt->downstream = g_ptr_array_index(xfer->elements, i+1);
Packit 23ab03
	}
Packit 23ab03
Packit 23ab03
	/* Set offset and size for first element */
Packit 23ab03
	{
Packit 23ab03
	    XferElement *xe = (XferElement *)g_ptr_array_index(xfer->elements, 0);
Packit 23ab03
	    xfer_element_set_offset(xe, offset);
Packit 23ab03
	    xfer_element_set_size(xe, size);
Packit 23ab03
	}
Packit 23ab03
Packit 23ab03
	/* now tell them all to start, in order from destination to source */
Packit 23ab03
	for (i = xfer->elements->len; i >= 1; i--) {
Packit 23ab03
	    XferElement *xe = (XferElement *)g_ptr_array_index(xfer->elements, i-1);
Packit 23ab03
	    if (xfer_element_start(xe))
Packit 23ab03
		xfer->num_active_elements++;
Packit 23ab03
	}
Packit 23ab03
    }
Packit 23ab03
Packit 23ab03
    /* (note that status can only change in the main thread, so we can be
Packit 23ab03
     * certain that the status is still XFER_START and we have not yet been
Packit 23ab03
     * cancelled.  We may have an XMSG_CANCEL already queued up for us, though) */
Packit 23ab03
    xfer_set_status(xfer, XFER_RUNNING);
Packit 23ab03
Packit 23ab03
    /* If this transfer involves no active processing, then we consider it to
Packit 23ab03
     * be done already.  We send a "fake" XMSG_DONE from the destination element,
Packit 23ab03
     * so that all of the usual processing will take place. */
Packit 23ab03
    if (xfer->num_active_elements == 0) {
Packit 23ab03
	if (setup_ok)
Packit 23ab03
	    g_debug("%s has no active elements; generating fake XMSG_DONE", xfer_repr(xfer));
Packit 23ab03
	xfer->num_active_elements++;
Packit 23ab03
	xfer_queue_message(xfer,
Packit 23ab03
	    xmsg_new((XferElement *)g_ptr_array_index(xfer->elements, xfer->elements->len-1),
Packit 23ab03
		     XMSG_DONE, 0));
Packit 23ab03
    }
Packit 23ab03
}
Packit 23ab03
Packit 23ab03
void
Packit 23ab03
xfer_set_offset_and_size(
Packit 23ab03
    Xfer *xfer,
Packit 23ab03
    gint64 offset,
Packit 23ab03
    gint64 size)
Packit 23ab03
{
Packit 23ab03
    /* Set offset for first element */
Packit 23ab03
    XferElement *xe = (XferElement *)g_ptr_array_index(xfer->elements, 0);
Packit 23ab03
    xfer_element_set_offset(xe, offset);
Packit 23ab03
    xfer_element_set_size(xe, size);
Packit 23ab03
}
Packit 23ab03
Packit 23ab03
void
Packit 23ab03
xfer_cancel(
Packit 23ab03
    Xfer *xfer)
Packit 23ab03
{
Packit 23ab03
    /* Since xfer_cancel can be called from any thread, we just send a message.
Packit 23ab03
     * The action takes place when the message is received. */
Packit 23ab03
    XferElement *src;
Packit 23ab03
    if (xfer->cancelled > 0) return;
Packit 23ab03
    xfer->cancelled++;
Packit 23ab03
    src = g_ptr_array_index(xfer->elements, 0);
Packit 23ab03
    xfer_queue_message(xfer, xmsg_new(src, XMSG_CANCEL, 0));
Packit 23ab03
}
Packit 23ab03
Packit 23ab03
static void
Packit 23ab03
xfer_set_status(
Packit 23ab03
    Xfer *xfer,
Packit 23ab03
    xfer_status status)
Packit 23ab03
{
Packit 23ab03
    if (xfer->status == status) return;
Packit 23ab03
Packit 23ab03
    g_mutex_lock(xfer->status_mutex);
Packit 23ab03
Packit 23ab03
    /* check that this state transition is valid */
Packit 23ab03
    switch (status) {
Packit 23ab03
    case XFER_START:
Packit 23ab03
        g_assert(xfer->status == XFER_INIT || xfer->status == XFER_DONE);
Packit 23ab03
        break;
Packit 23ab03
    case XFER_RUNNING:
Packit 23ab03
        g_assert(xfer->status == XFER_START);
Packit 23ab03
        break;
Packit 23ab03
    case XFER_CANCELLING:
Packit 23ab03
        g_assert(xfer->status == XFER_RUNNING);
Packit 23ab03
        break;
Packit 23ab03
    case XFER_CANCELLED:
Packit 23ab03
        g_assert(xfer->status == XFER_CANCELLING);
Packit 23ab03
        break;
Packit 23ab03
    case XFER_DONE:
Packit 23ab03
        g_assert(xfer->status == XFER_CANCELLED || xfer->status == XFER_RUNNING);
Packit 23ab03
        break;
Packit 23ab03
    case XFER_INIT:
Packit 23ab03
    default:
Packit 23ab03
        g_assert_not_reached();
Packit 23ab03
    }
Packit 23ab03
Packit 23ab03
    xfer->status = status;
Packit 23ab03
    g_cond_broadcast(xfer->status_cond);
Packit 23ab03
    g_mutex_unlock(xfer->status_mutex);
Packit 23ab03
}
Packit 23ab03
Packit 23ab03
/*
Packit 23ab03
 * Element linking
Packit 23ab03
 */
Packit 23ab03
Packit 23ab03
/* How is ELT linked? link_recurse uses an array of these to track its progress
Packit 23ab03
 * and find the optimal overall linkage. */
Packit 23ab03
typedef struct linkage {
Packit 23ab03
    XferElement *elt;
Packit 23ab03
    xfer_element_mech_pair_t *mech_pairs;
Packit 23ab03
    int elt_idx; /* index into elt's mech_pairs */
Packit 23ab03
    int glue_idx; /* index into glue pairs for elt's output; -1 = no glue */
Packit 23ab03
} linkage;
Packit 23ab03
Packit 23ab03
/* Overall state of the recursive linking process */
Packit 23ab03
typedef struct linking_state {
Packit 23ab03
    int nlinks; /* number of linkage objects in each array */
Packit 23ab03
    linkage *cur; /* "current" linkage */
Packit 23ab03
Packit 23ab03
    linkage *best; /* best linkage so far */
Packit 23ab03
    gint32 best_cost; /* cost for best */
Packit 23ab03
} linking_state;
Packit 23ab03
Packit 23ab03
/* used for debugging messages */
Packit 23ab03
static char *
Packit 23ab03
xfer_mech_name(
Packit 23ab03
    xfer_mech mech)
Packit 23ab03
{
Packit 23ab03
    switch (mech) {
Packit 23ab03
	case XFER_MECH_NONE: return "NONE";
Packit 23ab03
	case XFER_MECH_READFD: return "READFD";
Packit 23ab03
	case XFER_MECH_WRITEFD: return "WRITEFD";
Packit 23ab03
	case XFER_MECH_PULL_BUFFER: return "PULL_BUFFER";
Packit 23ab03
	case XFER_MECH_PUSH_BUFFER: return "PUSH_BUFFER";
Packit 23ab03
	case XFER_MECH_PULL_BUFFER_STATIC: return "PULL_BUFFER_STATIC";
Packit 23ab03
	case XFER_MECH_PUSH_BUFFER_STATIC: return "PUSH_BUFFER_STATIC";
Packit 23ab03
	case XFER_MECH_DIRECTTCP_LISTEN: return "DIRECTTCP_LISTEN";
Packit 23ab03
	case XFER_MECH_DIRECTTCP_CONNECT: return "DIRECTTCP_CONNECT";
Packit 23ab03
	case XFER_MECH_MEM_RING: return "MEM_RING";
Packit 23ab03
	case XFER_MECH_SHM_RING: return "SHM_RING";
Packit 23ab03
	default: return "UNKNOWN";
Packit 23ab03
    }
Packit 23ab03
}
Packit 23ab03
Packit 23ab03
/* calculate an integer representing the cost of a mech pair as a
Packit 23ab03
 * single integer.  OPS_PER_BYTE is the most important metric,
Packit 23ab03
 * followed by NTHREADS.
Packit 23ab03
 *
Packit 23ab03
 * PAIR will be evaluated multiple times.
Packit 23ab03
 */
Packit 23ab03
#define PAIR_COST(pair) (((pair).ops_per_byte << 16) + ((pair).nalloc << 8) + (pair).nthreads)
Packit 23ab03
Packit 23ab03
/* maximum cost */
Packit 23ab03
#define MAX_COST 0xffffff
Packit 23ab03
Packit 23ab03
/* Generate all possible linkages of elements [idx:nlinks], where
Packit 23ab03
 * elements [0:idx-1] have cost 'cost' and end with mechanism
Packit 23ab03
 * 'input_mech'. */
Packit 23ab03
static void
Packit 23ab03
link_recurse(
Packit 23ab03
    linking_state *st,
Packit 23ab03
    int idx,
Packit 23ab03
    xfer_mech input_mech,
Packit 23ab03
    gint32 cost)
Packit 23ab03
{
Packit 23ab03
    xfer_element_mech_pair_t *elt_pairs, *glue_pairs;
Packit 23ab03
    linkage *my;
Packit 23ab03
Packit 23ab03
    /* if we've overrun the previous best cost already, then bail out */
Packit 23ab03
    if (cost >= st->best_cost)
Packit 23ab03
	return;
Packit 23ab03
Packit 23ab03
    /* have we linked everything? */
Packit 23ab03
    if (idx == st->nlinks) {
Packit 23ab03
	/* if we ended on other than XFER_MECH_NONE, then this is not a
Packit 23ab03
	 * valid transfer */
Packit 23ab03
	if (input_mech != XFER_MECH_NONE) return;
Packit 23ab03
Packit 23ab03
	/* we already know this has lower cost than the previous best */
Packit 23ab03
	memcpy(st->best, st->cur, st->nlinks * sizeof(linkage));
Packit 23ab03
	st->best_cost = cost;
Packit 23ab03
Packit 23ab03
	return;
Packit 23ab03
    }
Packit 23ab03
Packit 23ab03
    /* recurse for each linkage we can make that starts with input_mech */
Packit 23ab03
    my = &st->cur[idx];
Packit 23ab03
    elt_pairs = my->mech_pairs;
Packit 23ab03
    glue_pairs = xfer_element_glue_mech_pairs;
Packit 23ab03
Packit 23ab03
    for (my->elt_idx = 0;
Packit 23ab03
	 elt_pairs[my->elt_idx].input_mech != XFER_MECH_NONE
Packit 23ab03
	 || elt_pairs[my->elt_idx].output_mech != XFER_MECH_NONE;
Packit 23ab03
	 my->elt_idx++) {
Packit 23ab03
	 /* reject this pair if the input mech does not match */
Packit 23ab03
	 if (elt_pairs[my->elt_idx].input_mech != input_mech)
Packit 23ab03
	    continue;
Packit 23ab03
Packit 23ab03
	 /* recurse with no glue */
Packit 23ab03
	 my->glue_idx = -1;
Packit 23ab03
	 link_recurse(st, idx+1,
Packit 23ab03
		      elt_pairs[my->elt_idx].output_mech,
Packit 23ab03
		      cost + PAIR_COST(elt_pairs[my->elt_idx]));
Packit 23ab03
Packit 23ab03
	/* and recurse with glue */
Packit 23ab03
	for (my->glue_idx = 0;
Packit 23ab03
	     glue_pairs[my->glue_idx].input_mech != XFER_MECH_NONE
Packit 23ab03
	     || glue_pairs[my->glue_idx].output_mech != XFER_MECH_NONE;
Packit 23ab03
	     my->glue_idx++) {
Packit 23ab03
	    /* reject this glue pair if it doesn't match with the element output */
Packit 23ab03
	    if (glue_pairs[my->glue_idx].input_mech != elt_pairs[my->elt_idx].output_mech)
Packit 23ab03
		continue;
Packit 23ab03
Packit 23ab03
	     /* and recurse with the glue */
Packit 23ab03
	     link_recurse(st, idx+1,
Packit 23ab03
			  glue_pairs[my->glue_idx].output_mech,
Packit 23ab03
			  cost + PAIR_COST(elt_pairs[my->elt_idx])
Packit 23ab03
			       + PAIR_COST(glue_pairs[my->glue_idx]));
Packit 23ab03
	}
Packit 23ab03
    }
Packit 23ab03
}
Packit 23ab03
Packit 23ab03
static void
Packit 23ab03
link_elements(
Packit 23ab03
    Xfer *xfer)
Packit 23ab03
{
Packit 23ab03
    char *tmpbuf;
Packit 23ab03
    GPtrArray *new_elements;
Packit 23ab03
    XferElement *elt;
Packit 23ab03
    char *linkage_str;
Packit 23ab03
    linking_state st;
Packit 23ab03
    gint i, len;
Packit 23ab03
Packit 23ab03
    /* Note that this algorithm's running time is polynomial in the length of
Packit 23ab03
     * the transfer, with a fairly high order.  If Amanda is regularly assembling
Packit 23ab03
     * transfers with more than, say, 6 elements, then the algorithm should be
Packit 23ab03
     * redesigned. */
Packit 23ab03
Packit 23ab03
    /* set up the state for recursion */
Packit 23ab03
    st.nlinks = xfer->elements->len;
Packit 23ab03
    st.cur = g_new0(linkage, st.nlinks);
Packit 23ab03
    st.best = g_new0(linkage, st.nlinks);
Packit 23ab03
    st.best_cost = MAX_COST;
Packit 23ab03
    for (i = 0; i < st.nlinks; i++) {
Packit 23ab03
	st.cur[i].elt = (XferElement *)g_ptr_array_index(xfer->elements, i);
Packit 23ab03
	st.cur[i].mech_pairs = xfer_element_get_mech_pairs(st.cur[i].elt);
Packit 23ab03
    }
Packit 23ab03
Packit 23ab03
    /* check that the first element is an XferSource and the last is an XferDest.
Packit 23ab03
     * A source is identified by having no input mechanisms. */
Packit 23ab03
    if (st.cur[0].mech_pairs[0].input_mech != XFER_MECH_NONE)
Packit 23ab03
	error("Transfer element 0 is not a transfer source");
Packit 23ab03
Packit 23ab03
    /* Similarly, a destination has no output mechanisms. */
Packit 23ab03
    if (st.cur[st.nlinks-1].mech_pairs[0].output_mech != XFER_MECH_NONE)
Packit 23ab03
	error("Last transfer element is not a transfer destination");
Packit 23ab03
Packit 23ab03
    /* start recursing with the first element, asserting that its input mech is NONE */
Packit 23ab03
    link_recurse(&st, 0, XFER_MECH_NONE, 0);
Packit 23ab03
Packit 23ab03
    /* check that we got *some* solution */
Packit 23ab03
    if (st.best_cost == MAX_COST) {
Packit 23ab03
	error(_("Xfer %s cannot be linked."), xfer_repr(xfer));
Packit 23ab03
    }
Packit 23ab03
Packit 23ab03
    /* Now create a new list of elements, containing any glue elements
Packit 23ab03
     * that we need to add, and set their input_mech and output_mech fields */
Packit 23ab03
    new_elements = g_ptr_array_sized_new(xfer->elements->len);
Packit 23ab03
    for (i = 0; i < st.nlinks; i++) {
Packit 23ab03
	elt = st.best[i].elt;
Packit 23ab03
	elt->input_mech = st.best[i].mech_pairs[st.best[i].elt_idx].input_mech;
Packit 23ab03
	elt->output_mech = st.best[i].mech_pairs[st.best[i].elt_idx].output_mech;
Packit 23ab03
	g_ptr_array_add(new_elements, elt);
Packit 23ab03
Packit 23ab03
	if (st.best[i].glue_idx != -1) {
Packit 23ab03
	    elt = xfer_element_glue();
Packit 23ab03
	    elt->xfer = xfer;
Packit 23ab03
	    elt->input_mech = xfer_element_glue_mech_pairs[st.best[i].glue_idx].input_mech;
Packit 23ab03
	    elt->output_mech = xfer_element_glue_mech_pairs[st.best[i].glue_idx].output_mech;
Packit 23ab03
	    g_ptr_array_add(new_elements, elt);
Packit 23ab03
	}
Packit 23ab03
    }
Packit 23ab03
Packit 23ab03
    /* install the new list of elements */
Packit 23ab03
    g_ptr_array_free(xfer->elements, FALSE);
Packit 23ab03
    xfer->elements = new_elements;
Packit 23ab03
    new_elements = NULL;
Packit 23ab03
Packit 23ab03
    /* debug-log the xfer's linkage */
Packit 23ab03
    len = xfer->elements->len;
Packit 23ab03
    linkage_str = g_strdup("Final linkage: ");
Packit 23ab03
    for (i = 0; i < len; i++) {
Packit 23ab03
	XferElement *elt = g_ptr_array_index(xfer->elements, i);
Packit 23ab03
Packit 23ab03
	if (i == 0) {
Packit 23ab03
	    tmpbuf = g_strconcat(linkage_str, xfer_element_repr(elt), NULL);
Packit 23ab03
	    g_free(linkage_str);
Packit 23ab03
	    linkage_str = tmpbuf;
Packit 23ab03
	}
Packit 23ab03
	else {
Packit 23ab03
	    tmpbuf = g_strdup_printf("%s -(%s)-> %s",
Packit 23ab03
		linkage_str, xfer_mech_name(elt->input_mech), xfer_element_repr(elt));
Packit 23ab03
	    g_free(linkage_str);
Packit 23ab03
	    linkage_str = tmpbuf;
Packit 23ab03
	}
Packit 23ab03
    }
Packit 23ab03
    g_debug("%s", linkage_str);
Packit 23ab03
    amfree(linkage_str);
Packit 23ab03
Packit 23ab03
    amfree(st.cur);
Packit 23ab03
    amfree(st.best);
Packit 23ab03
}
Packit 23ab03
Packit 23ab03
/*
Packit 23ab03
 * XMsgSource
Packit 23ab03
 */
Packit 23ab03
Packit 23ab03
static gboolean
Packit 23ab03
xmsgsource_prepare(
Packit 23ab03
    GSource *source,
Packit 23ab03
    gint *timeout_)
Packit 23ab03
{
Packit 23ab03
    XMsgSource *xms = (XMsgSource *)source;
Packit 23ab03
Packit 23ab03
    *timeout_ = -1;
Packit 23ab03
    return xms->xfer && g_async_queue_length(xms->xfer->queue) > 0;
Packit 23ab03
}
Packit 23ab03
Packit 23ab03
static gboolean
Packit 23ab03
xmsgsource_check(
Packit 23ab03
    GSource *source)
Packit 23ab03
{
Packit 23ab03
    XMsgSource *xms = (XMsgSource *)source;
Packit 23ab03
Packit 23ab03
    return xms->xfer && g_async_queue_length(xms->xfer->queue) > 0;
Packit 23ab03
}
Packit 23ab03
Packit 23ab03
static gboolean
Packit 23ab03
xmsgsource_dispatch(
Packit 23ab03
    GSource *source G_GNUC_UNUSED,
Packit 23ab03
    GSourceFunc callback,
Packit 23ab03
    gpointer user_data)
Packit 23ab03
{
Packit 23ab03
    XMsgSource *xms = (XMsgSource *)source;
Packit 23ab03
    Xfer *xfer = xms->xfer;
Packit 23ab03
    XMsgCallback my_cb = (XMsgCallback)callback;
Packit 23ab03
    XMsg *msg;
Packit 23ab03
    gboolean deliver_to_caller;
Packit 23ab03
    guint i;
Packit 23ab03
    gboolean xfer_done = FALSE;
Packit 23ab03
Packit 23ab03
    /* we're potentially calling Perl code within this loop, so we have to
Packit 23ab03
     * check that everything is ok on each iteration of the loop. */
Packit 23ab03
    while (xfer
Packit 23ab03
        && xfer->status != XFER_DONE
Packit 23ab03
        && (msg = (XMsg *)g_async_queue_try_pop(xfer->queue))) {
Packit 23ab03
Packit 23ab03
	/* We get first crack at interpreting messages, before calling the
Packit 23ab03
	 * designated callback. */
Packit 23ab03
	deliver_to_caller = TRUE;
Packit 23ab03
	switch (msg->type) {
Packit 23ab03
	    /* Intercept and count DONE messages so that we can determine when
Packit 23ab03
	     * the entire transfer is finished. */
Packit 23ab03
	    case XMSG_DONE:
Packit 23ab03
		if (--xfer->num_active_elements <= 0) {
Packit 23ab03
		    /* mark the transfer as done, and take a note to break out
Packit 23ab03
		     * of this loop after delivering the message to the user */
Packit 23ab03
		    xfer_set_status(xfer, XFER_DONE);
Packit 23ab03
		    xfer_done = TRUE;
Packit 23ab03
		} else {
Packit 23ab03
		    /* eat this XMSG_DONE, since we expect more */
Packit 23ab03
		    deliver_to_caller = FALSE;
Packit 23ab03
		}
Packit 23ab03
		break;
Packit 23ab03
Packit 23ab03
	    case XMSG_CANCEL:
Packit 23ab03
		if (xfer->status == XFER_CANCELLING || xfer->status == XFER_CANCELLED) {
Packit 23ab03
		    /* ignore duplicate cancel messages */
Packit 23ab03
		    deliver_to_caller = FALSE;
Packit 23ab03
		} else {
Packit 23ab03
		    /* call cancel() on each child element */
Packit 23ab03
		    gboolean expect_eof;
Packit 23ab03
Packit 23ab03
		    g_debug("Cancelling %s", xfer_repr(xfer));
Packit 23ab03
		    xfer_set_status(xfer, XFER_CANCELLING);
Packit 23ab03
Packit 23ab03
		    expect_eof = FALSE;
Packit 23ab03
		    for (i = 0; i < xfer->elements->len; i++) {
Packit 23ab03
			XferElement *elt = (XferElement *)
Packit 23ab03
				g_ptr_array_index(xfer->elements, i);
Packit 23ab03
			expect_eof = xfer_element_cancel(elt, expect_eof) || expect_eof;
Packit 23ab03
		    }
Packit 23ab03
Packit 23ab03
		    /* if nothing in the transfer can generate an EOF, then we
Packit 23ab03
		     * can't cancel this transfer, and we'll just have to wait
Packit 23ab03
		     * until it's finished.  This may happen, for example, if
Packit 23ab03
		     * the operating system is copying data for us
Packit 23ab03
		     * asynchronously */
Packit 23ab03
		    if (!expect_eof)
Packit 23ab03
			g_warning("Transfer %s cannot be cancelled.", xfer_repr(xfer));
Packit 23ab03
Packit 23ab03
		    /* and now we're done cancelling */
Packit 23ab03
		    xfer_set_status(xfer, XFER_CANCELLED);
Packit 23ab03
		}
Packit 23ab03
		break;
Packit 23ab03
Packit 23ab03
	    default:
Packit 23ab03
		break;  /* nothing interesting to do */
Packit 23ab03
	}
Packit 23ab03
Packit 23ab03
	if (deliver_to_caller) {
Packit 23ab03
	    if (my_cb) {
Packit 23ab03
		my_cb(user_data, msg, xfer);
Packit 23ab03
	    } else {
Packit 23ab03
		g_warning("Dropping %s because no callback is set", xmsg_repr(msg));
Packit 23ab03
	    }
Packit 23ab03
	}
Packit 23ab03
Packit 23ab03
	xmsg_free(msg);
Packit 23ab03
Packit 23ab03
	/* This transfer is done, so kill it and exit the loop */
Packit 23ab03
	if (xfer_done) {
Packit 23ab03
	    xfer_unref(xfer);
Packit 23ab03
	    xfer = NULL;
Packit 23ab03
	    break;
Packit 23ab03
	}
Packit 23ab03
    }
Packit 23ab03
Packit 23ab03
    /* Never automatically un-queue the event source */
Packit 23ab03
    return TRUE;
Packit 23ab03
}
Packit 23ab03
Packit 23ab03
XMsgSource *
Packit 23ab03
xmsgsource_new(
Packit 23ab03
    Xfer *xfer)
Packit 23ab03
{
Packit 23ab03
    static GSourceFuncs *xmsgsource_funcs = NULL;
Packit 23ab03
    GSource *src;
Packit 23ab03
    XMsgSource *xms;
Packit 23ab03
Packit 23ab03
    /* initialize these here to avoid a compiler warning */
Packit 23ab03
    if (!xmsgsource_funcs) {
Packit 23ab03
	xmsgsource_funcs = g_new0(GSourceFuncs, 1);
Packit 23ab03
	xmsgsource_funcs->prepare = xmsgsource_prepare;
Packit 23ab03
	xmsgsource_funcs->check = xmsgsource_check;
Packit 23ab03
	xmsgsource_funcs->dispatch = xmsgsource_dispatch;
Packit 23ab03
    }
Packit 23ab03
Packit 23ab03
    src = g_source_new(xmsgsource_funcs, sizeof(XMsgSource));
Packit 23ab03
    xms = (XMsgSource *)src;
Packit 23ab03
    xms->xfer = xfer;
Packit 23ab03
Packit 23ab03
    return xms;
Packit 23ab03
}
Packit 23ab03
Packit 23ab03
xfer_status
Packit 23ab03
wait_until_xfer_cancelled(
Packit 23ab03
    Xfer *xfer)
Packit 23ab03
{
Packit 23ab03
    xfer_status seen_status;
Packit 23ab03
    g_assert(xfer != NULL);
Packit 23ab03
Packit 23ab03
    g_mutex_lock(xfer->status_mutex);
Packit 23ab03
    while (xfer->status != XFER_CANCELLED && xfer->status != XFER_DONE) {
Packit 23ab03
	g_cond_wait(xfer->status_cond, xfer->status_mutex);
Packit 23ab03
    }
Packit 23ab03
    seen_status = xfer->status;
Packit 23ab03
    g_mutex_unlock(xfer->status_mutex);
Packit 23ab03
Packit 23ab03
    return seen_status;
Packit 23ab03
}
Packit 23ab03
Packit 23ab03
xfer_status
Packit 23ab03
wait_until_xfer_running(
Packit 23ab03
    Xfer *xfer)
Packit 23ab03
{
Packit 23ab03
    xfer_status seen_status;
Packit 23ab03
    g_assert(xfer != NULL);
Packit 23ab03
Packit 23ab03
    g_mutex_lock(xfer->status_mutex);
Packit 23ab03
    while (xfer->status == XFER_START)
Packit 23ab03
	g_cond_wait(xfer->status_cond, xfer->status_mutex);
Packit 23ab03
    seen_status = xfer->status;
Packit 23ab03
    g_mutex_unlock(xfer->status_mutex);
Packit 23ab03
Packit 23ab03
    return seen_status;
Packit 23ab03
}
Packit 23ab03
Packit 23ab03
void
Packit 23ab03
xfer_cancel_with_error(
Packit 23ab03
    XferElement *elt,
Packit 23ab03
    const char *fmt,
Packit 23ab03
    ...)
Packit 23ab03
{
Packit 23ab03
    va_list argp;
Packit 23ab03
    XMsg *msg;
Packit 23ab03
Packit 23ab03
    g_assert(elt != NULL);
Packit 23ab03
    g_assert(elt->xfer != NULL);
Packit 23ab03
Packit 23ab03
    msg = xmsg_new(elt, XMSG_ERROR, 0);
Packit 23ab03
Packit 23ab03
    arglist_start(argp, fmt);
Packit 23ab03
    msg->message = g_strdup_vprintf(fmt, argp);
Packit 23ab03
    arglist_end(argp);
Packit 23ab03
Packit 23ab03
    g_debug("xfer_cancel_with_error: %s", msg->message);
Packit 23ab03
Packit 23ab03
    /* send the XMSG_ERROR */
Packit 23ab03
    xfer_queue_message(elt->xfer, msg);
Packit 23ab03
Packit 23ab03
    /* cancel the transfer */
Packit 23ab03
    xfer_cancel(elt->xfer);
Packit 23ab03
}
Packit 23ab03
Packit 23ab03
gint
Packit 23ab03
xfer_atomic_swap_fd(Xfer *xfer, gint *fdp, gint newfd)
Packit 23ab03
{
Packit 23ab03
    gint rv;
Packit 23ab03
Packit 23ab03
    if (xfer)
Packit 23ab03
	g_mutex_lock(xfer->fd_mutex);
Packit 23ab03
    rv = *fdp;
Packit 23ab03
    *fdp = newfd;
Packit 23ab03
    if (xfer)
Packit 23ab03
	g_mutex_unlock(xfer->fd_mutex);
Packit 23ab03
Packit 23ab03
    return rv;
Packit 23ab03
}