Skip to content

Commit

Permalink
sb_file_str: use thread local
Browse files Browse the repository at this point in the history
  • Loading branch information
rockeet committed Sep 23, 2022
1 parent da9d074 commit 5b9dc3c
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 37 deletions.
15 changes: 10 additions & 5 deletions src/lua/internal/sysbench.rand.lua
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,14 @@ uint32_t sb_rand_unique(void);
void sb_rand_str(const char *, char *);
uint32_t sb_rand_varstr(char *, uint32_t, uint32_t);
double sb_rand_uniform_double(void);
void sb_file_str(char *, char *);
struct SbKeyVal {
size_t cap;
size_t klen;
size_t vlen;
char* str; // also key
char* val; // ptr after tab char
};
struct SbKeyVal* sb_file_str(void);
]]

function sysbench.rand.uniform_uint64()
Expand Down Expand Up @@ -85,8 +92,6 @@ function sysbench.rand.uniform_double()
end

function sysbench.rand.filestr()
local buf1 = ffi.new("char[?]", 1024)
local buf2 = ffi.new("char[?]", 4194304)
ffi.C.sb_file_str(buf1, buf2)
return ffi.string(buf1), ffi.string(buf2)
local ms = ffi.C.sb_file_str()
return ffi.string(ms.str, ms.klen), ffi.string(ms.val, ms.vlen)
end
73 changes: 42 additions & 31 deletions src/sb_rand.c
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@
#include "sb_logger.h"

#include "sb_ck_pr.h"
#include "pthread.h"
#include <ctype.h>

TLS sb_rng_state_t sb_rng_state CK_CC_CACHELINE;

Expand Down Expand Up @@ -460,10 +460,16 @@ uint32_t sb_rand_zipfian(uint32_t a, uint32_t b)
sb_rand_zipfian_int(b - a + 1, zipf_exp, zipf_s, zipf_hIntegralX1) - 1;
}

struct SbKeyVal {
size_t cap;
size_t klen;
size_t vlen;
char* str; // also key
char* val; // ptr after tab char
};
static __thread struct SbKeyVal* g_kv = NULL;

static FILE* file = NULL;
static char* str_buf;
static size_t len;
static pthread_mutex_t file_mtx;

int sb_file_init()
{
Expand All @@ -475,38 +481,49 @@ int sb_file_init()
log_text(LOG_FATAL, "Invalid filename: %s", sb_globals.filename);
return 1;
}
pthread_mutex_init(&file_mtx, NULL);

len = 4194304 * sizeof(char);
str_buf = (char *)malloc(len);
return 0;
}

void sb_file_str(char* buf1, char* buf2)
struct SbKeyVal* sb_file_str()
{
assert(file != NULL);

pthread_mutex_lock(&file_mtx);
ssize_t read = getline(&str_buf, &len, file);
if (read != -1)
{
int pos = 0;
while (pos < read) {
if (str_buf[pos] == '\t') {
memcpy(buf1, str_buf, pos);
buf1[pos] = 0;
memcpy(buf2, str_buf + pos + 1, read - pos - 2);
buf2[read - pos - 2] = 0;
break;
if (NULL == g_kv) {
g_kv = (struct SbKeyVal*)malloc(sizeof(struct SbKeyVal));
g_kv->klen = 0;
g_kv->vlen = 0;
g_kv->cap = 4*1024*1024;
g_kv->str = (char*)malloc(g_kv->cap);
g_kv->val = NULL;
}
while (true) {
ssize_t read = getline(&g_kv->str, &g_kv->cap, file);
if (read != -1) {
char* str = g_kv->str;
while (read > 0 && isspace((unsigned char)str[read])) {
str[--read] = '\0'; // trim trailing spaces
}
char* tab = (char*)memchr(str, '\t', read);
if (NULL == tab) {
g_kv->val = str + read; // empty c string
g_kv->vlen = 0;
g_kv->klen = read;
}
pos++;
else {
tab[0] = '\0';
g_kv->klen = tab - str;
g_kv->vlen = read - (tab - str) - 1;
g_kv->val = tab + 1;
}
break;
}

if (pos >= read) {
memcpy(buf2, str_buf, read);
else {
rewind(file);
fprintf(stderr, "sb_file_str: read eof, rewind(%s)\n", sb_globals.filename);
}
}
pthread_mutex_unlock(&file_mtx);
return g_kv;
}

void sb_file_done()
Expand All @@ -515,12 +532,6 @@ void sb_file_done()
{
fclose(file);
}

if (str_buf)
{
free(str_buf);
}
pthread_mutex_destroy(&file_mtx);
}


Expand Down
2 changes: 1 addition & 1 deletion src/sb_rand.h
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ void sb_rand_str(const char *, char *);
uint32_t sb_rand_varstr(char *, uint32_t, uint32_t);

int sb_file_init(void);
void sb_file_str(char *, char *);
struct SbKeyVal* sb_file_str(void);
void sb_file_done(void);

#endif /* SB_RAND_H */

0 comments on commit 5b9dc3c

Please sign in to comment.