/*----------------------------------------------------------------------------*/ struct thread_context * InitializeServerThread(int core) { struct thread_context *ctx; /* affinitize application thread to a CPU core */ #if HT_SUPPORT mtcp_core_affinitize(core + (num_cores / 2)); #else mtcp_core_affinitize(core); #endif /* HT_SUPPORT */ ctx = (struct thread_context *)calloc(1, sizeof(struct thread_context)); if (!ctx) { TRACE_ERROR("Failed to create thread context!\n"); return NULL; } /* create mtcp context: this will spawn an mtcp thread */ ctx->mctx = mtcp_create_context(core); if (!ctx->mctx) { TRACE_ERROR("Failed to create mtcp context!\n"); return NULL; } /* create epoll descriptor */ ctx->ep = mtcp_epoll_create(ctx->mctx, MAX_EVENTS); if (ctx->ep < 0) { TRACE_ERROR("Failed to create epoll descriptor!\n"); return NULL; } /* allocate memory for server variables */ /* ctx->svars = (struct server_vars *) calloc(MAX_FLOW_NUM, sizeof(struct server_vars)); if (!ctx->svars) { TRACE_ERROR("Failed to create server_vars struct!\n"); return NULL; } */ return ctx; }
static void *start_worker(void *p) { int i; struct worker *worker; struct ctx *ctx; worker = p; worker->sctx_map = (struct ctx **)calloc(SOCKET_CTX_MAP_ENTRIES, sizeof(struct ctx *)); // affinitize application thread to a CPU core mtcp_core_affinitize(worker->cpu); worker->buffer = malloc(msg_size); for (i = 0; i < msg_size; i++) worker->buffer[i] = '0'; // create mTCP context and spawn an mTCP thread worker->mctx = mtcp_create_context(worker->cpu); if (!worker->mctx) { fprintf(stderr, "Failed to create mTCP context on core %d\n", worker->cpu); exit(1); } // create an epoll descriptor worker->ep = mtcp_epoll_create(worker->mctx, MAX_EVENTS); if (worker->ep < 0) { fprintf(stderr, "Error: Failed to create epoll descriptor on core %d\n", worker->cpu); exit(EXIT_FAILURE); } worker->first_ctx = NULL; for (i = 0; i < worker->connections; i++) { ctx = worker->first_ctx; worker->first_ctx = malloc(sizeof(struct ctx)); worker->first_ctx->next = ctx; worker->first_ctx->state = STATE_IDLE; worker->first_ctx->timestamp = 0; worker->first_ctx->worker = worker; worker->first_ctx->sockid = -1; } mtcp_epoll_loop(worker); return 0; }