Commit feaf45d3 authored by Darek Stojaczyk's avatar Darek Stojaczyk
Browse files

vhost: allocate just one ctx per foreach_session chain



We used to allocate a ctx whenever new event had to
be sent, but since all events in foreach_session are
always called in a chain, we could allocate one ctx
at the start and then re-initialize it before sending
each msg.

Change-Id: Ie5477b07242f0c6eb6dc2160055a829da8ba5d11
Signed-off-by: default avatarDarek Stojaczyk <dariusz.stojaczyk@intel.com>
Reviewed-on: https://review.gerrithub.io/c/spdk/spdk/+/459167


Tested-by: default avatarSPDK CI Jenkins <sys_sgci@intel.com>
Reviewed-by: default avatarBen Walker <benjamin.walker@intel.com>
Reviewed-by: default avatarShuhei Matsumoto <shuhei.matsumoto.xt@hitachi.com>
parent 3fb1a956
Loading
Loading
Loading
Loading
+23 −24
Original line number Diff line number Diff line
@@ -981,9 +981,8 @@ spdk_vhost_session_send_event(struct vhost_poll_group *pg,
	return g_dpdk_response;
}

static void foreach_session_continue(struct spdk_vhost_dev *vdev,
				     struct spdk_vhost_session *vsession,
				     spdk_vhost_session_fn fn, void *arg);
static void foreach_session_continue(struct spdk_vhost_session_fn_ctx *ev_ctx,
				     struct spdk_vhost_session *vsession);

static void
foreach_session_finish_cb(void *arg1)
@@ -1042,28 +1041,27 @@ foreach_session_continue_cb(void *arg1)

	rc = ctx->cb_fn(vdev, vsession, ctx->user_ctx);
	if (rc < 0) {
		goto out_unlock;
		pthread_mutex_unlock(&g_spdk_vhost_mutex);
		free(ctx);
		return;
	}

out_unlock_continue:
	vsession = spdk_vhost_session_next(vdev, ctx->vsession_id);
	foreach_session_continue(vdev, vsession, ctx->cb_fn, ctx->user_ctx);
out_unlock:
	foreach_session_continue(ctx, vsession);
	pthread_mutex_unlock(&g_spdk_vhost_mutex);
	free(ctx);
}

static void
foreach_session_continue(struct spdk_vhost_dev *vdev,
			 struct spdk_vhost_session *vsession,
			 spdk_vhost_session_fn fn, void *arg)
foreach_session_continue(struct spdk_vhost_session_fn_ctx *ev_ctx,
			 struct spdk_vhost_session *vsession)
{
	struct spdk_vhost_session_fn_ctx *ev_ctx;
	struct spdk_vhost_dev *vdev = ev_ctx->vdev;
	int rc;

	while (vsession != NULL && !vsession->started) {
		if (vsession->initialized) {
			rc = fn(vdev, vsession, arg);
			rc = ev_ctx->cb_fn(vdev, vsession, ev_ctx->user_ctx);
			if (rc < 0) {
				return;
			}
@@ -1072,17 +1070,6 @@ foreach_session_continue(struct spdk_vhost_dev *vdev,
		vsession = spdk_vhost_session_next(vdev, vsession->id);
	}

	ev_ctx = calloc(1, sizeof(*ev_ctx));
	if (ev_ctx == NULL) {
		SPDK_ERRLOG("Failed to alloc vhost event.\n");
		assert(false);
		return;
	}

	ev_ctx->vdev = vdev;
	ev_ctx->cb_fn = fn;
	ev_ctx->user_ctx = arg;

	if (vsession != NULL) {
		ev_ctx->vsession_id = vsession->id;
		spdk_thread_send_msg(vsession->poll_group->thread,
@@ -1099,10 +1086,22 @@ spdk_vhost_dev_foreach_session(struct spdk_vhost_dev *vdev,
			       spdk_vhost_session_fn fn, void *arg)
{
	struct spdk_vhost_session *vsession = TAILQ_FIRST(&vdev->vsessions);
	struct spdk_vhost_session_fn_ctx *ev_ctx;

	ev_ctx = calloc(1, sizeof(*ev_ctx));
	if (ev_ctx == NULL) {
		SPDK_ERRLOG("Failed to alloc vhost event.\n");
		assert(false);
		return;
	}

	ev_ctx->vdev = vdev;
	ev_ctx->cb_fn = fn;
	ev_ctx->user_ctx = arg;

	assert(vdev->pending_async_op_num < UINT32_MAX);
	vdev->pending_async_op_num++;
	foreach_session_continue(vdev, vsession, fn, arg);
	foreach_session_continue(ev_ctx, vsession);
}

static void