* lightweight tracing
*/
#include <stdio.h>
-
#include "common.h"
-#include "tls.h"
+#include "runtime_local.h"
#include "lwt.h"
#include "mem.h"
lwt_record_t x[0];
} lwt_buffer_t;
-DECLARE_THREAD_LOCAL(tb_, int);
-
lwt_buffer_t *lwt_buf_[MAX_NUM_THREADS] = {};
uint64_t flag_mask_ = 0;
-static int buf_count_ = 0;
static const char *flags_ = "";
-void lwt_init (void)
-{
- INIT_THREAD_LOCAL(tb_, NULL);
-}
-
void lwt_thread_init (int thread_id)
{
assert(thread_id < MAX_NUM_THREADS);
if (lwt_buf_[thread_id] == NULL) {
lwt_buf_[thread_id] = (lwt_buffer_t *)nbd_malloc(sizeof(lwt_buffer_t) + sizeof(lwt_record_t) * LWT_BUFFER_SIZE);
- SYNC_ADD(&buf_count_, 1);
memset(lwt_buf_[thread_id], 0, sizeof(lwt_buffer_t));
- SET_THREAD_LOCAL(tb_, lwt_buf_[thread_id]);
}
}
static void dump_buffer (FILE *file, int thread_id, uint64_t offset)
{
- assert(thread_id < buf_count_);
-
lwt_buffer_t *tb = lwt_buf_[thread_id];
- int i;
+ assert(tb);
if (tb->head > LWT_BUFFER_SIZE) {
- for (i = tb->head & LWT_BUFFER_MASK; i < LWT_BUFFER_SIZE; ++i) {
+ for (int i = tb->head & LWT_BUFFER_MASK; i < LWT_BUFFER_SIZE; ++i) {
dump_record(file, thread_id, tb->x + i, offset);
}
}
- for (i = 0; i < (tb->head & LWT_BUFFER_MASK); ++i) {
+ for (int i = 0; i < (tb->head & LWT_BUFFER_MASK); ++i) {
dump_record(file, thread_id, tb->x + i, offset);
}
}
void lwt_dump (const char *file_name)
{
uint64_t offset = (uint64_t)-1;
- int i;
- for (i = 0; i < buf_count_; ++i) {
+ for (int i = 0; i < MAX_NUM_THREADS; ++i) {
if (lwt_buf_[i] != NULL && lwt_buf_[i]->head != 0) {
uint64_t x = lwt_buf_[i]->x[0].timestamp;
if (x < offset) {
if (offset != (uint64_t)-1) {
FILE *file = fopen(file_name, "w");
assert(file);
- for (i = 0; i < buf_count_; ++i) {
+ for (int i = 0; i < MAX_NUM_THREADS; ++i) {
if (lwt_buf_[i] != NULL) {
dump_buffer(file, i, offset);
}
}
void lwt_trace_i (const char *format, size_t value1, size_t value2) {
- LOCALIZE_THREAD_LOCAL(tb_, lwt_buffer_t *);
- if (tb_) {
+ LOCALIZE_THREAD_LOCAL(tid_, int);
+ lwt_buffer_t *tb = lwt_buf_[tid_];
+ if (tb != NULL) {
unsigned int u, l;
__asm__ __volatile__("rdtsc" : "=a" (l), "=d" (u));
uint64_t timestamp = ((uint64_t)u << 32) | l;
lwt_record_t temp = { timestamp, format, value1, value2 };
- tb_->x[tb_->head++ & LWT_BUFFER_MASK] = temp;
+
+ tb->x[tb->head++ & LWT_BUFFER_MASK] = temp;
}
}
#include <stdio.h>
#include <errno.h>
#include "common.h"
+#include "runtime_local.h"
#include "lwt.h"
-#include "tls.h"
#define GET_SCALE(n) (sizeof(n)*8-__builtin_clzl((n)-1)) // log2 of <n>, rounded up
#define MAX_SCALE 31 // allocate blocks up to 4GB in size (arbitrary, could be bigger)
assert(((size_t)b >> REGION_SCALE) < ((1 << HEADER_REGION_SCALE) / sizeof(header_t)));
header_t *h = region_header_ + ((size_t)b >> REGION_SCALE);
TRACE("m0", "nbd_free(): block %p scale %llu", x, h->scale);
- block_t *l = &free_list_[(int)h->owner][(int)h->scale][tid_];
+ block_t *l = &free_list_[h->owner][h->scale][tid_];
TRACE("m0", "nbd_free(): free list %p first block %p", l, l->next);
b->next = l->next;
l->next = b;
*/
#include <string.h>
#include "common.h"
+#include "runtime_local.h"
#include "lwt.h"
#include "mem.h"
#include "tls.h"
#include "mem.h"
#include "tls.h"
-#undef malloc
-#undef free
-
DECLARE_THREAD_LOCAL(tid_, int);
typedef struct thread_info {
} thread_info_t;
void nbd_init (void) {
- INIT_THREAD_LOCAL(tid_, NULL);
+ INIT_THREAD_LOCAL(tid_);
+ SET_THREAD_LOCAL(tid_, 0);
mem_init();
- lwt_init();
lwt_thread_init(0);
rcu_thread_init(0);
}
lwt_thread_init(ti->thread_id);
rcu_thread_init(ti->thread_id);
void *ret = ti->start_routine(ti->arg);
- free(ti);
+ nbd_free(ti);
return ret;
}
int nbd_thread_create (pthread_t *restrict thread, int thread_id, void *(*start_routine)(void *), void *restrict arg) {
- thread_info_t *ti = (thread_info_t *)malloc(sizeof(thread_info_t));
+ thread_info_t *ti = (thread_info_t *)nbd_malloc(sizeof(thread_info_t));
ti->thread_id = thread_id;
ti->start_routine = start_routine;
ti->arg = arg;
+#ifndef RUNTIME_LOCAL_H
+#define RUNTIME_LOCAL_H
+#include "tls.h"
+DECLARE_THREAD_LOCAL(tid_, int);
+
void mem_init (void);
-void lwt_init (void);
void rcu_thread_init (int thread_id);
void lwt_thread_init (int thread_id);
+#endif//RUNTIME_LOCAL_H
#define DECLARE_THREAD_LOCAL (name, type) type name
#define INIT_THREAD_LOCAL (name, value) name = value
#define SET_THREAD_LOCAL (name, value) name = value
-#define LOCALIZE_THREAD_LOCAL(name, type) extern __thread type name
+#define LOCALIZE_THREAD_LOCAL(name, type)
#else//!__ELF__
#define DECLARE_THREAD_LOCAL(name, type) pthread_key_t name##_KEY
-#define INIT_THREAD_LOCAL(name, value) \
+#define INIT_THREAD_LOCAL(name) \
do { \
- if (pthread_key_create(&name##_KEY, (void *)(size_t)value) != 0) { assert(FALSE); } \
+ if (pthread_key_create(&name##_KEY, NULL) != 0) { assert(FALSE); } \
} while (0)
#define SET_THREAD_LOCAL(name, value) pthread_setspecific(name##_KEY, (void *)(size_t)value);
-#define LOCALIZE_THREAD_LOCAL(name, type) \
- extern pthread_key_t name##_KEY; type name = (type)(size_t)pthread_getspecific(name##_KEY)
+#define LOCALIZE_THREAD_LOCAL(name, type) type name = (type)(size_t)pthread_getspecific(name##_KEY)
#endif//__ELF__
#endif//TLS_H
wd[i].tc = tc;
wd[i].ht = ht;
wd[i].wait = &wait;
- int rc = pthread_create(thread + i, NULL, simple_worker, wd + i);
- if (rc != 0) { perror("pthread_create"); return; }
+ int rc = nbd_thread_create(thread + i, i, simple_worker, wd + i);
+ if (rc != 0) { perror("nbd_thread_create"); return; }
}
for (i = 0; i < 2; ++i) {
pthread_join(thread[i], NULL);