Skip to content

Commit 5b9dc3c

Browse files
committed
sb_file_str: use thread local
1 parent da9d074 commit 5b9dc3c

3 files changed

Lines changed: 53 additions & 37 deletions

File tree

src/lua/internal/sysbench.rand.lua

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,14 @@ uint32_t sb_rand_unique(void);
3333
void sb_rand_str(const char *, char *);
3434
uint32_t sb_rand_varstr(char *, uint32_t, uint32_t);
3535
double sb_rand_uniform_double(void);
36-
void sb_file_str(char *, char *);
36+
struct SbKeyVal {
37+
size_t cap;
38+
size_t klen;
39+
size_t vlen;
40+
char* str; // also key
41+
char* val; // ptr after tab char
42+
};
43+
struct SbKeyVal* sb_file_str(void);
3744
]]
3845

3946
function sysbench.rand.uniform_uint64()
@@ -85,8 +92,6 @@ function sysbench.rand.uniform_double()
8592
end
8693

8794
function sysbench.rand.filestr()
88-
local buf1 = ffi.new("char[?]", 1024)
89-
local buf2 = ffi.new("char[?]", 4194304)
90-
ffi.C.sb_file_str(buf1, buf2)
91-
return ffi.string(buf1), ffi.string(buf2)
95+
local ms = ffi.C.sb_file_str()
96+
return ffi.string(ms.str, ms.klen), ffi.string(ms.val, ms.vlen)
9297
end

src/sb_rand.c

Lines changed: 42 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@
5858
#include "sb_logger.h"
5959

6060
#include "sb_ck_pr.h"
61-
#include "pthread.h"
61+
#include <ctype.h>
6262

6363
TLS sb_rng_state_t sb_rng_state CK_CC_CACHELINE;
6464

@@ -460,10 +460,16 @@ uint32_t sb_rand_zipfian(uint32_t a, uint32_t b)
460460
sb_rand_zipfian_int(b - a + 1, zipf_exp, zipf_s, zipf_hIntegralX1) - 1;
461461
}
462462

463+
struct SbKeyVal {
464+
size_t cap;
465+
size_t klen;
466+
size_t vlen;
467+
char* str; // also key
468+
char* val; // ptr after tab char
469+
};
470+
static __thread struct SbKeyVal* g_kv = NULL;
471+
463472
static FILE* file = NULL;
464-
static char* str_buf;
465-
static size_t len;
466-
static pthread_mutex_t file_mtx;
467473

468474
int sb_file_init()
469475
{
@@ -475,38 +481,49 @@ int sb_file_init()
475481
log_text(LOG_FATAL, "Invalid filename: %s", sb_globals.filename);
476482
return 1;
477483
}
478-
pthread_mutex_init(&file_mtx, NULL);
479484

480-
len = 4194304 * sizeof(char);
481-
str_buf = (char *)malloc(len);
482485
return 0;
483486
}
484487

485-
void sb_file_str(char* buf1, char* buf2)
488+
struct SbKeyVal* sb_file_str()
486489
{
487490
assert(file != NULL);
488491

489-
pthread_mutex_lock(&file_mtx);
490-
ssize_t read = getline(&str_buf, &len, file);
491-
if (read != -1)
492-
{
493-
int pos = 0;
494-
while (pos < read) {
495-
if (str_buf[pos] == '\t') {
496-
memcpy(buf1, str_buf, pos);
497-
buf1[pos] = 0;
498-
memcpy(buf2, str_buf + pos + 1, read - pos - 2);
499-
buf2[read - pos - 2] = 0;
500-
break;
492+
if (NULL == g_kv) {
493+
g_kv = (struct SbKeyVal*)malloc(sizeof(struct SbKeyVal));
494+
g_kv->klen = 0;
495+
g_kv->vlen = 0;
496+
g_kv->cap = 4*1024*1024;
497+
g_kv->str = (char*)malloc(g_kv->cap);
498+
g_kv->val = NULL;
499+
}
500+
while (true) {
501+
ssize_t read = getline(&g_kv->str, &g_kv->cap, file);
502+
if (read != -1) {
503+
char* str = g_kv->str;
504+
while (read > 0 && isspace((unsigned char)str[read])) {
505+
str[--read] = '\0'; // trim trailing spaces
506+
}
507+
char* tab = (char*)memchr(str, '\t', read);
508+
if (NULL == tab) {
509+
g_kv->val = str + read; // empty c string
510+
g_kv->vlen = 0;
511+
g_kv->klen = read;
501512
}
502-
pos++;
513+
else {
514+
tab[0] = '\0';
515+
g_kv->klen = tab - str;
516+
g_kv->vlen = read - (tab - str) - 1;
517+
g_kv->val = tab + 1;
518+
}
519+
break;
503520
}
504-
505-
if (pos >= read) {
506-
memcpy(buf2, str_buf, read);
521+
else {
522+
rewind(file);
523+
fprintf(stderr, "sb_file_str: read eof, rewind(%s)\n", sb_globals.filename);
507524
}
508525
}
509-
pthread_mutex_unlock(&file_mtx);
526+
return g_kv;
510527
}
511528

512529
void sb_file_done()
@@ -515,12 +532,6 @@ void sb_file_done()
515532
{
516533
fclose(file);
517534
}
518-
519-
if (str_buf)
520-
{
521-
free(str_buf);
522-
}
523-
pthread_mutex_destroy(&file_mtx);
524535
}
525536

526537

src/sb_rand.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ void sb_rand_str(const char *, char *);
7272
uint32_t sb_rand_varstr(char *, uint32_t, uint32_t);
7373

7474
int sb_file_init(void);
75-
void sb_file_str(char *, char *);
75+
struct SbKeyVal* sb_file_str(void);
7676
void sb_file_done(void);
7777

7878
#endif /* SB_RAND_H */

0 commit comments

Comments
 (0)