提交 ffa0e974 编写于 作者: D Dmitry Kozlov

core: allocate memory for context in first 2Gb due to bug in glibc-2.7 and probably other versions

上级 a09fdabf
......@@ -135,6 +135,14 @@ void __export log_debug(const char *fmt,...)
}
}
void __export log_debug2(const char *fmt,...)
{
va_list ap;
va_start(ap, fmt);
vfprintf(debug_file, fmt, ap);
va_end(ap);
fflush(debug_file);
}
void __export log_msg(const char *fmt,...)
{
va_list ap;
......
......@@ -2,11 +2,14 @@
#include <stdlib.h>
#include <string.h>
#include <signal.h>
#include <sys/mman.h>
#include "triton_p.h"
#include "memdebug.h"
//#define MEMPOOL_DISABLE
#define MAGIC1 0x2233445566778899llu
struct _mempool_t
......@@ -19,6 +22,7 @@ struct _mempool_t
#endif
spinlock_t lock;
uint64_t magic;
int mmap:1;
};
struct _item_t
......@@ -57,6 +61,15 @@ mempool_t __export *mempool_create(int size)
return (mempool_t *)p;
}
mempool_t __export *mempool_create2(int size)
{
struct _mempool_t *p = (struct _mempool_t *)mempool_create(size);
p->mmap = 1;
return (mempool_t *)p;
}
#ifndef MEMDEBUG
void __export *mempool_alloc(mempool_t *pool)
{
......@@ -78,7 +91,11 @@ void __export *mempool_alloc(mempool_t *pool)
}
spin_unlock(&p->lock);
it = _malloc(size);
if (p->mmap)
it = mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANON | MAP_32BIT, -1, 0);
else
it = _malloc(size);
if (!it) {
triton_log_error("mempool: out of memory\n");
return NULL;
......@@ -118,7 +135,11 @@ void __export *mempool_alloc_md(mempool_t *pool, const char *fname, int line)
}
spin_unlock(&p->lock);
it = md_malloc(size, fname, line);
if (p->mmap)
it = mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANON | MAP_32BIT, -1, 0);
else
it = md_malloc(size, fname, line);
if (!it) {
triton_log_error("mempool: out of memory\n");
return NULL;
......@@ -174,7 +195,10 @@ void __export mempool_free(void *ptr)
spin_unlock(&it->owner->lock);
#ifdef MEMPOOL_DISABLE
_free(it);
if (it->owner->mmap)
munmap(it, size);
else
_free(it);
#endif
triton_stat.mempool_available += size;
......@@ -190,7 +214,10 @@ void __export mempool_clean(mempool_t *pool)
while (!list_empty(&p->items)) {
it = list_entry(p->items.next, typeof(*it), entry);
list_del(&it->entry);
_free(it);
if (p->mmap)
munmap(it, size);
else
_free(it);
triton_stat.mempool_allocated -= size;
triton_stat.mempool_available -= size;
}
......
......@@ -11,6 +11,7 @@ struct mempool_stat_t
typedef void * mempool_t;
mempool_t *mempool_create(int size);
mempool_t *mempool_create2(int size);
void mempool_free(void*);
struct mempool_stat_t mempool_get_stat(void);
......
......@@ -29,9 +29,11 @@ static mempool_t *ctx_stack_pool;
__export struct triton_stat_t triton_stat;
#define log_debug2(fmt, ...)
void triton_thread_wakeup(struct _triton_thread_t *thread)
{
//printf("wake up thread %p\n", thread);
log_debug2("wake up thread %p\n", thread);
pthread_kill(thread->thread, SIGUSR1);
}
......@@ -53,7 +55,7 @@ static void* triton_thread(struct _triton_thread_t *thread)
spin_lock(&threads_lock);
if (!list_empty(&ctx_queue)) {
thread->ctx = list_entry(ctx_queue.next, typeof(*thread->ctx), entry2);
//printf("thread: %p: dequeued ctx %p\n", thread, thread->ctx);
log_debug2("thread: %p: dequeued ctx %p\n", thread, thread->ctx);
list_del(&thread->ctx->entry2);
spin_unlock(&threads_lock);
spin_lock(&thread->ctx->lock);
......@@ -62,7 +64,7 @@ static void* triton_thread(struct _triton_thread_t *thread)
spin_unlock(&thread->ctx->lock);
triton_stat.context_pending--;
} else {
//printf("thread: %p: sleeping\n", thread);
log_debug2("thread: %p: sleeping\n", thread);
if (!terminate)
list_add(&thread->entry2, &sleep_threads);
spin_unlock(&threads_lock);
......@@ -80,11 +82,11 @@ static void* triton_thread(struct _triton_thread_t *thread)
}
cont:
//printf("thread %p: ctx=%p %p\n", thread, thread->ctx, thread->ctx ? thread->ctx->thread : NULL);
log_debug2("thread %p: ctx=%p %p\n", thread, thread->ctx, thread->ctx ? thread->ctx->thread : NULL);
if (thread->ctx->ud->before_switch)
thread->ctx->ud->before_switch(thread->ctx->ud, thread->ctx->bf_arg);
//printf("thread %p: switch to %p\n", thread, thread->ctx);
log_debug2("thread %p: switch to %p\n", thread, thread->ctx);
while (1) {
if (swapcontext(&thread->uctx, &thread->ctx->uctx)) {
if (errno == EINTR)
......@@ -93,7 +95,7 @@ cont:
} else
break;
}
//printf("thread %p: switch from %p %p\n", thread, thread->ctx, thread->ctx->thread);
log_debug2("thread %p: switch from %p %p\n", thread, thread->ctx, thread->ctx->thread);
if (thread->ctx->thread) {
spin_lock(&thread->ctx->lock);
......@@ -105,7 +107,7 @@ cont:
spin_unlock(&thread->ctx->lock);
if (thread->ctx->need_free) {
//printf("- context %p removed\n", thread->ctx);
log_debug2("- context %p removed\n", thread->ctx);
spin_lock(&thread->ctx->lock);
spin_unlock(&thread->ctx->lock);
mempool_free(thread->ctx->uctx.uc_stack.ss_sp);
......@@ -125,7 +127,7 @@ static void ctx_thread(struct _triton_context_t *ctx)
uint64_t tt;
while (1) {
//printf("ctx %p %p: enter\n", ctx, ctx->thread);
log_debug2("ctx %p %p: enter\n", ctx, ctx->thread);
if (ctx->need_close) {
if (ctx->ud->close)
ctx->ud->close(ctx->ud);
......@@ -171,7 +173,7 @@ static void ctx_thread(struct _triton_context_t *ctx)
break;
}
//printf("ctx %p %p: exit\n", ctx, ctx->thread);
log_debug2("ctx %p %p: exit\n", ctx, ctx->thread);
while (1) {
if (swapcontext(&ctx->uctx, &ctx->thread->uctx)) {
if (errno == EINTR)
......@@ -215,14 +217,14 @@ int triton_queue_ctx(struct _triton_context_t *ctx)
list_add_tail(&ctx->entry2, &ctx_queue);
spin_unlock(&threads_lock);
ctx->queued = 1;
//printf("ctx %p: queued\n", ctx);
log_debug2("ctx %p: queued\n", ctx);
triton_stat.context_pending++;
return 0;
}
ctx->thread = list_entry(sleep_threads.next, typeof(*ctx->thread), entry2);
ctx->thread->ctx = ctx;
//printf("ctx %p: assigned to thread %p\n", ctx, ctx->thread);
log_debug2("ctx %p: assigned to thread %p\n", ctx, ctx->thread);
list_del(&ctx->thread->entry2);
spin_unlock(&threads_lock);
......@@ -233,6 +235,7 @@ int __export triton_context_register(struct triton_context_t *ud, void *bf_arg)
{
struct _triton_context_t *ctx = mempool_alloc(ctx_pool);
log_debug2("ctx %p: register\n", ctx);
if (!ctx)
return -1;
......@@ -281,6 +284,8 @@ void __export triton_context_unregister(struct triton_context_t *ud)
struct _triton_ctx_call_t *call;
struct _triton_thread_t *t;
log_debug2("ctx %p: unregister\n");
while (!list_empty(&ctx->pending_calls)) {
call = list_entry(ctx->pending_calls.next, typeof(*call), entry);
list_del(&call->entry);
......@@ -349,8 +354,10 @@ void __export triton_context_schedule(struct triton_context_t *ud)
triton_stat.context_sleeping++;
log_debug2("ctx %p: enter schedule\n", ctx);
if (swapcontext(&ctx->uctx, uctx))
triton_log_error("swaswpntext: %s\n", strerror(errno));
log_debug2("ctx %p: exit schedule\n", ctx);
}
void triton_context_print(void)
......@@ -366,6 +373,8 @@ int __export triton_context_wakeup(struct triton_context_t *ud)
struct _triton_context_t *ctx = (struct _triton_context_t *)ud->tpd;
int r;
log_debug2("ctx %p: wakeup\n", ctx);
spin_lock(&ctx->lock);
if (!ctx->sleeping) {
ctx->wakeup = 1;
......@@ -409,9 +418,9 @@ int __export triton_context_call(struct triton_context_t *ud, void (*func)(void
int __export triton_init(const char *conf_file)
{
ctx_pool = mempool_create(sizeof(struct _triton_context_t));
ctx_pool = mempool_create2(sizeof(struct _triton_context_t));
ctx_stack_pool = mempool_create2(CTX_STACK_SIZE);
call_pool = mempool_create(sizeof(struct _triton_ctx_call_t));
ctx_stack_pool = mempool_create(CTX_STACK_SIZE);
if (conf_load(conf_file))
return -1;
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册