summaryrefslogtreecommitdiff
path: root/storage/tokudb/PerconaFT/portability
diff options
context:
space:
mode:
Diffstat (limited to 'storage/tokudb/PerconaFT/portability')
-rw-r--r--storage/tokudb/PerconaFT/portability/CMakeLists.txt5
-rw-r--r--storage/tokudb/PerconaFT/portability/file.cc498
-rw-r--r--storage/tokudb/PerconaFT/portability/memory.cc8
-rw-r--r--storage/tokudb/PerconaFT/portability/portability.cc60
-rw-r--r--storage/tokudb/PerconaFT/portability/tests/rwlock_condvar.h4
-rw-r--r--storage/tokudb/PerconaFT/portability/tests/test-pthread-rwlock-rdlock.cc2
-rw-r--r--storage/tokudb/PerconaFT/portability/tests/test-pthread-rwlock-rwr.cc14
-rw-r--r--storage/tokudb/PerconaFT/portability/tests/test-stat.cc9
-rw-r--r--storage/tokudb/PerconaFT/portability/tests/test-toku-malloc.cc8
-rw-r--r--storage/tokudb/PerconaFT/portability/toku_assert.h14
-rw-r--r--storage/tokudb/PerconaFT/portability/toku_instr_mysql.cc365
-rw-r--r--storage/tokudb/PerconaFT/portability/toku_instr_mysql.h249
-rw-r--r--storage/tokudb/PerconaFT/portability/toku_instrumentation.h339
-rw-r--r--storage/tokudb/PerconaFT/portability/toku_os.h10
-rw-r--r--storage/tokudb/PerconaFT/portability/toku_portability.h328
-rw-r--r--storage/tokudb/PerconaFT/portability/toku_pthread.h450
16 files changed, 2037 insertions, 326 deletions
diff --git a/storage/tokudb/PerconaFT/portability/CMakeLists.txt b/storage/tokudb/PerconaFT/portability/CMakeLists.txt
index 4793db63cc1..e5576a5d463 100644
--- a/storage/tokudb/PerconaFT/portability/CMakeLists.txt
+++ b/storage/tokudb/PerconaFT/portability/CMakeLists.txt
@@ -8,6 +8,7 @@ set(tokuportability_srcs
portability
toku_assert
toku_crash
+ toku_instr_mysql
toku_path
toku_pthread
toku_time
@@ -53,6 +54,10 @@ if (NOT DEFINED MYSQL_PROJECT_NAME_DOCSTRING)
DESTINATION ${INSTALL_LIBDIR}
COMPONENT tokukv_libs_shared
)
+else ()
+ set_property(SOURCE toku_pthread portability APPEND PROPERTY
+ COMPILE_DEFINITIONS MYSQL_TOKUDB_ENGINE=1 )
+ target_link_libraries(${LIBTOKUPORTABILITY} LINK_PRIVATE mysys)
endif ()
add_subdirectory(tests)
diff --git a/storage/tokudb/PerconaFT/portability/file.cc b/storage/tokudb/PerconaFT/portability/file.cc
index 0e3efc1a12a..485bfac8514 100644
--- a/storage/tokudb/PerconaFT/portability/file.cc
+++ b/storage/tokudb/PerconaFT/portability/file.cc
@@ -52,9 +52,12 @@ Copyright (c) 2006, 2015, Percona and/or its affiliates. All rights reserved.
#include "toku_path.h"
#include <portability/toku_atomic.h>
+toku_instr_key *tokudb_file_data_key;
+
static int toku_assert_on_write_enospc = 0;
static const int toku_write_enospc_sleep = 1;
-static uint64_t toku_write_enospc_last_report; // timestamp of most recent report to error log
+static uint64_t toku_write_enospc_last_report; // timestamp of most recent
+ // report to error log
static time_t toku_write_enospc_last_time; // timestamp of most recent ENOSPC
static uint32_t toku_write_enospc_current; // number of threads currently blocked on ENOSPC
static uint64_t toku_write_enospc_total; // total number of times ENOSPC was returned from an attempt to write
@@ -142,11 +145,17 @@ static ssize_t (*t_full_pwrite)(int, const void *, size_t, off_t);
static FILE * (*t_fdopen)(int, const char *);
static FILE * (*t_fopen)(const char *, const char *);
static int (*t_open)(const char *, int, int);
-static int (*t_fclose)(FILE *);
+static int (*t_fclose)(FILE *);
static ssize_t (*t_read)(int, void *, size_t);
static ssize_t (*t_pread)(int, void *, size_t, off_t);
+static size_t (*os_fwrite_fun)(const void *, size_t, size_t, FILE *) = nullptr;
+
+void toku_set_func_fwrite(
+ size_t (*fwrite_fun)(const void *, size_t, size_t, FILE *)) {
+ os_fwrite_fun = fwrite_fun;
+}
-void toku_set_func_write (ssize_t (*write_fun)(int, const void *, size_t)) {
+void toku_set_func_write(ssize_t (*write_fun)(int, const void *, size_t)) {
t_write = write_fun;
}
@@ -186,9 +195,63 @@ void toku_set_func_pread (ssize_t (*pread_fun)(int, void *, size_t, off_t)) {
t_pread = pread_fun;
}
-void
-toku_os_full_write (int fd, const void *buf, size_t len) {
- const char *bp = (const char *) buf;
+int toku_os_delete_with_source_location(const char *name,
+ const char *src_file,
+ uint src_line) {
+
+ toku_io_instrumentation io_annotation;
+ toku_instr_file_name_close_begin(io_annotation,
+ *tokudb_file_data_key,
+ toku_instr_file_op::file_delete,
+ name,
+ src_file,
+ src_line);
+ const int result = unlink(name);
+
+ /* Register the result value with the instrumentation system */
+ toku_instr_file_close_end(io_annotation, result);
+
+ return result;
+}
+
+int toku_os_rename_with_source_location(const char *old_name,
+ const char *new_name,
+ const char *src_file,
+ uint src_line) {
+ int result;
+
+ toku_io_instrumentation io_annotation;
+ toku_instr_file_name_io_begin(io_annotation,
+ *tokudb_file_data_key,
+ toku_instr_file_op::file_rename,
+ new_name,
+ 0,
+ src_file,
+ src_line);
+
+ result = rename(old_name, new_name);
+ /* Regsiter the result value with the instrumentation system */
+ toku_instr_file_io_end(io_annotation, 0);
+
+ return result;
+}
+
+void toku_os_full_write_with_source_location(int fd,
+ const void *buf,
+ size_t len,
+ const char *src_file,
+ uint src_line) {
+ const char *bp = (const char *)buf;
+ size_t bytes_written = len;
+
+ toku_io_instrumentation io_annotation;
+ toku_instr_file_io_begin(io_annotation,
+ toku_instr_file_op::file_write,
+ fd,
+ len,
+ src_file,
+ src_line);
+
while (len > 0) {
ssize_t r;
if (t_full_write) {
@@ -205,14 +268,30 @@ toku_os_full_write (int fd, const void *buf, size_t len) {
}
}
assert(len == 0);
+
+ /* Register the result value with the instrumentaion system */
+ toku_instr_file_io_end(io_annotation, bytes_written);
}
-int
-toku_os_write (int fd, const void *buf, size_t len) {
- const char *bp = (const char *) buf;
+int toku_os_write_with_source_location(int fd,
+ const void *buf,
+ size_t len,
+ const char *src_file,
+ uint src_line) {
+ const char *bp = (const char *)buf;
int result = 0;
+ ssize_t r;
+
+ size_t bytes_written = len;
+ toku_io_instrumentation io_annotation;
+ toku_instr_file_io_begin(io_annotation,
+ toku_instr_file_op::file_write,
+ fd,
+ len,
+ src_file,
+ src_line);
+
while (len > 0) {
- ssize_t r;
if (t_write) {
r = t_write(fd, bp, len);
} else {
@@ -222,17 +301,33 @@ toku_os_write (int fd, const void *buf, size_t len) {
result = errno;
break;
}
- len -= r;
- bp += r;
+ len -= r;
+ bp += r;
}
+ /* Register the result value with the instrumentation system */
+ toku_instr_file_io_end(io_annotation, bytes_written - len);
+
return result;
}
-void
-toku_os_full_pwrite (int fd, const void *buf, size_t len, toku_off_t off) {
- assert(0==((long long)buf)%512);
- assert((len%512 == 0) && (off%512)==0); // to make pwrite work.
- const char *bp = (const char *) buf;
+void toku_os_full_pwrite_with_source_location(int fd,
+ const void *buf,
+ size_t len,
+ toku_off_t off,
+ const char *src_file,
+ uint src_line) {
+ assert(0 == ((long long)buf) % 512);
+ assert((len % 512 == 0) && (off % 512) == 0); // to make pwrite work.
+ const char *bp = (const char *)buf;
+
+ size_t bytes_written = len;
+ toku_io_instrumentation io_annotation;
+ toku_instr_file_io_begin(io_annotation,
+ toku_instr_file_op::file_write,
+ fd,
+ len,
+ src_file,
+ src_line);
while (len > 0) {
ssize_t r;
if (t_full_pwrite) {
@@ -250,71 +345,209 @@ toku_os_full_pwrite (int fd, const void *buf, size_t len, toku_off_t off) {
}
}
assert(len == 0);
-}
-ssize_t
-toku_os_pwrite (int fd, const void *buf, size_t len, toku_off_t off) {
- assert(0==((long long)buf)%512); // these asserts are to ensure that direct I/O will work.
- assert(0==len %512);
- assert(0==off %512);
- const char *bp = (const char *) buf;
+ /* Register the result value with the instrumentation system */
+ toku_instr_file_io_end(io_annotation, bytes_written);
+}
+
+ssize_t toku_os_pwrite_with_source_location(int fd,
+ const void *buf,
+ size_t len,
+ toku_off_t off,
+ const char *src_file,
+ uint src_line) {
+ assert(0 ==
+ ((long long)buf) %
+ 512); // these asserts are to ensure that direct I/O will work.
+ assert(0 == len % 512);
+ assert(0 == off % 512);
+ const char *bp = (const char *)buf;
ssize_t result = 0;
+ ssize_t r;
+
+ size_t bytes_written = len;
+ toku_io_instrumentation io_annotation;
+ toku_instr_file_io_begin(io_annotation,
+ toku_instr_file_op::file_write,
+ fd,
+ len,
+ src_file,
+ src_line);
while (len > 0) {
- ssize_t r;
- if (t_pwrite) {
- r = t_pwrite(fd, bp, len, off);
- } else {
- r = pwrite(fd, bp, len, off);
- }
+ r = (t_pwrite) ? t_pwrite(fd, bp, len, off) : pwrite(fd, bp, len, off);
+
if (r < 0) {
result = errno;
break;
}
len -= r;
- bp += r;
- off += r;
+ bp += r;
+ off += r;
+ }
+ /* Register the result value with the instrumentation system */
+ toku_instr_file_io_end(io_annotation, bytes_written - len);
+
+ return result;
+}
+
+int toku_os_fwrite_with_source_location(const void *ptr,
+ size_t size,
+ size_t nmemb,
+ TOKU_FILE *stream,
+ const char *src_file,
+ uint src_line) {
+ int result = 0;
+ size_t bytes_written;
+
+ toku_io_instrumentation io_annotation;
+ toku_instr_file_stream_io_begin(io_annotation,
+ toku_instr_file_op::file_write,
+ *stream,
+ nmemb,
+ src_file,
+ src_line);
+
+ if (os_fwrite_fun) {
+ bytes_written = os_fwrite_fun(ptr, size, nmemb, stream->file);
+ } else {
+ bytes_written = fwrite(ptr, size, nmemb, stream->file);
+ }
+
+ if (bytes_written != nmemb) {
+ if (os_fwrite_fun) // if using hook to induce artificial errors (for
+ // testing) ...
+ result = get_maybe_error_errno(); // ... then there is no error in
+ // the stream, but there is one
+ // in errno
+ else
+ result = ferror(stream->file);
+ invariant(result != 0); // Should we assert here?
+ }
+ /* Register the result value with the instrumentation system */
+ toku_instr_file_io_end(io_annotation, bytes_written);
+
+ return result;
+}
+
+int toku_os_fread_with_source_location(void *ptr,
+ size_t size,
+ size_t nmemb,
+ TOKU_FILE *stream,
+ const char *src_file,
+ uint src_line) {
+ int result = 0;
+ size_t bytes_read;
+
+ toku_io_instrumentation io_annotation;
+ toku_instr_file_stream_io_begin(io_annotation,
+ toku_instr_file_op::file_read,
+ *stream,
+ nmemb,
+ src_file,
+ src_line);
+
+ if ((bytes_read = fread(ptr, size, nmemb, stream->file)) != nmemb) {
+ if ((feof(stream->file)))
+ result = EOF;
+ else
+ result = ferror(stream->file);
+ invariant(result != 0); // Should we assert here?
}
+ /* Register the result value with the instrumentation system */
+ toku_instr_file_io_end(io_annotation, bytes_read);
+
return result;
}
-FILE *
-toku_os_fdopen(int fildes, const char *mode) {
- FILE * rval;
- if (t_fdopen)
- rval = t_fdopen(fildes, mode);
- else
- rval = fdopen(fildes, mode);
+TOKU_FILE *toku_os_fdopen_with_source_location(int fildes,
+ const char *mode,
+ const char *filename,
+ const toku_instr_key &instr_key,
+ const char *src_file,
+ uint src_line) {
+ TOKU_FILE *XMALLOC(rval);
+ if (FT_LIKELY(rval != nullptr)) {
+ toku_io_instrumentation io_annotation;
+ toku_instr_file_open_begin(io_annotation,
+ instr_key,
+ toku_instr_file_op::file_stream_open,
+ filename,
+ src_file,
+ src_line);
+
+ rval->file = (t_fdopen) ? t_fdopen(fildes, mode) : fdopen(fildes, mode);
+ toku_instr_file_stream_open_end(io_annotation, *rval);
+
+ if (FT_UNLIKELY(rval->file == nullptr)) {
+ toku_free(rval);
+ rval = nullptr;
+ }
+ }
return rval;
}
-
-FILE *
-toku_os_fopen(const char *filename, const char *mode){
- FILE * rval;
- if (t_fopen)
- rval = t_fopen(filename, mode);
- else
- rval = fopen(filename, mode);
+TOKU_FILE *toku_os_fopen_with_source_location(const char *filename,
+ const char *mode,
+ const toku_instr_key &instr_key,
+ const char *src_file,
+ uint src_line) {
+ TOKU_FILE *XMALLOC(rval);
+ if (FT_UNLIKELY(rval == nullptr))
+ return nullptr;
+
+ toku_io_instrumentation io_annotation;
+ toku_instr_file_open_begin(io_annotation,
+ instr_key,
+ toku_instr_file_op::file_stream_open,
+ filename,
+ src_file,
+ src_line);
+ rval->file = t_fopen ? t_fopen(filename, mode) : fopen(filename, mode);
+ /* Register the returning "file" value with the system */
+ toku_instr_file_stream_open_end(io_annotation, *rval);
+
+ if (FT_UNLIKELY(rval->file == nullptr)) {
+ toku_free(rval);
+ rval = nullptr;
+ }
return rval;
}
-int
-toku_os_open(const char *path, int oflag, int mode) {
- int rval;
+int toku_os_open_with_source_location(const char *path,
+ int oflag,
+ int mode,
+ const toku_instr_key &instr_key,
+ const char *src_file,
+ uint src_line) {
+ int fd;
+ toku_io_instrumentation io_annotation;
+ /* register a file open or creation depending on "oflag" */
+ toku_instr_file_open_begin(
+ io_annotation,
+ instr_key,
+ ((oflag & O_CREAT) ? toku_instr_file_op::file_create
+ : toku_instr_file_op::file_open),
+ path,
+ src_file,
+ src_line);
if (t_open)
- rval = t_open(path, oflag, mode);
+ fd = t_open(path, oflag, mode);
else
- rval = open(path, oflag, mode);
- return rval;
+ fd = open(path, oflag, mode);
+
+ toku_instr_file_open_end(io_annotation, fd);
+ return fd;
}
-int
-toku_os_open_direct(const char *path, int oflag, int mode) {
+int toku_os_open_direct(const char *path,
+ int oflag,
+ int mode,
+ const toku_instr_key &instr_key) {
int rval;
#if defined(HAVE_O_DIRECT)
- rval = toku_os_open(path, oflag | O_DIRECT, mode);
+ rval = toku_os_open(path, oflag | O_DIRECT, mode, instr_key);
#elif defined(HAVE_F_NOCACHE)
- rval = toku_os_open(path, oflag, mode);
+ rval = toku_os_open(path, oflag, mode, instr_key);
if (rval >= 0) {
int r = fcntl(rval, F_NOCACHE, 1);
if (r == -1) {
@@ -327,63 +560,112 @@ toku_os_open_direct(const char *path, int oflag, int mode) {
return rval;
}
-int
-toku_os_fclose(FILE * stream) {
+int toku_os_fclose_with_source_location(TOKU_FILE *stream,
+ const char *src_file,
+ uint src_line) {
int rval = -1;
- if (t_fclose)
- rval = t_fclose(stream);
- else { // if EINTR, retry until success
- while (rval != 0) {
- rval = fclose(stream);
- if (rval && (errno != EINTR))
- break;
- }
+ if (FT_LIKELY(stream != nullptr)) {
+ /* register a file stream close " */
+ toku_io_instrumentation io_annotation;
+ toku_instr_file_stream_close_begin(
+ io_annotation,
+ toku_instr_file_op::file_stream_close,
+ *stream,
+ src_file,
+ src_line);
+
+ if (t_fclose)
+ rval = t_fclose(stream->file);
+ else { // if EINTR, retry until success
+ while (rval != 0) {
+ rval = fclose(stream->file);
+ if (rval && (errno != EINTR))
+ break;
+ }
+ }
+ /* Register the returning "rval" value with the system */
+ toku_instr_file_close_end(io_annotation, rval);
+ toku_free(stream);
+ stream = nullptr;
}
return rval;
}
-int
-toku_os_close(int fd) { // if EINTR, retry until success
+int toku_os_close_with_source_location(
+ int fd,
+ const char *src_file,
+ uint src_line) { // if EINTR, retry until success
+ /* register the file close */
int r = -1;
+
+ /* register a file descriptor close " */
+ toku_io_instrumentation io_annotation;
+ toku_instr_file_fd_close_begin(
+ io_annotation, toku_instr_file_op::file_close, fd, src_file, src_line);
while (r != 0) {
- r = close(fd);
- if (r) {
- int rr = errno;
- if (rr!=EINTR) printf("rr=%d (%s)\n", rr, strerror(rr));
- assert(rr==EINTR);
- }
+ r = close(fd);
+ if (r) {
+ int rr = errno;
+ if (rr != EINTR)
+ printf("rr=%d (%s)\n", rr, strerror(rr));
+ assert(rr == EINTR);
+ }
}
- return r;
-}
-
-int toku_os_rename(const char *old_name, const char *new_name) {
- return rename(old_name, new_name);
-}
-int toku_os_unlink(const char *path) { return unlink(path); }
+ /* Regsiter the returning value with the system */
+ toku_instr_file_close_end(io_annotation, r);
-ssize_t
-toku_os_read(int fd, void *buf, size_t count) {
- ssize_t r;
- if (t_read)
- r = t_read(fd, buf, count);
- else
- r = read(fd, buf, count);
return r;
}
-ssize_t
-toku_os_pread (int fd, void *buf, size_t count, off_t offset) {
- assert(0==((long long)buf)%512);
- assert(0==count%512);
- assert(0==offset%512);
- ssize_t r;
+ssize_t toku_os_read_with_source_location(int fd,
+ void *buf,
+ size_t count,
+ const char *src_file,
+ uint src_line) {
+ ssize_t bytes_read;
+
+ toku_io_instrumentation io_annotation;
+ toku_instr_file_io_begin(io_annotation,
+ toku_instr_file_op::file_read,
+ fd,
+ count,
+ src_file,
+ src_line);
+
+ bytes_read = (t_read) ? t_read(fd, buf, count) : read(fd, buf, count);
+
+ toku_instr_file_io_end(io_annotation, bytes_read);
+
+ return bytes_read;
+}
+
+ssize_t inline_toku_os_pread_with_source_location(int fd,
+ void *buf,
+ size_t count,
+ off_t offset,
+ const char *src_file,
+ uint src_line) {
+ assert(0 == ((long long)buf) % 512);
+ assert(0 == count % 512);
+ assert(0 == offset % 512);
+ ssize_t bytes_read;
+
+ toku_io_instrumentation io_annotation;
+ toku_instr_file_io_begin(io_annotation,
+ toku_instr_file_op::file_read,
+ fd,
+ count,
+ src_file,
+ src_line);
if (t_pread) {
- r = t_pread(fd, buf, count, offset);
+ bytes_read = t_pread(fd, buf, count, offset);
} else {
- r = pread(fd, buf, count, offset);
+ bytes_read = pread(fd, buf, count, offset);
}
- return r;
+ toku_instr_file_io_end(io_annotation, bytes_read);
+
+ return bytes_read;
}
void toku_os_recursive_delete(const char *path) {
@@ -411,13 +693,24 @@ void toku_set_func_fsync(int (*fsync_function)(int)) {
}
// keep trying if fsync fails because of EINTR
-static void file_fsync_internal (int fd) {
+void file_fsync_internal_with_source_location(int fd,
+ const char *src_file,
+ uint src_line) {
uint64_t tstart = toku_current_time_microsec();
int r = -1;
uint64_t eintr_count = 0;
+
+ toku_io_instrumentation io_annotation;
+ toku_instr_file_io_begin(io_annotation,
+ toku_instr_file_op::file_sync,
+ fd,
+ 0,
+ src_file,
+ src_line);
+
while (r != 0) {
- if (t_fsync) {
- r = t_fsync(fd);
+ if (t_fsync) {
+ r = t_fsync(fd);
} else {
r = fsync(fd);
}
@@ -429,6 +722,9 @@ static void file_fsync_internal (int fd) {
toku_sync_fetch_and_add(&toku_fsync_count, 1);
uint64_t duration = toku_current_time_microsec() - tstart;
toku_sync_fetch_and_add(&toku_fsync_time, duration);
+
+ toku_instr_file_io_end(io_annotation, 0);
+
if (duration >= toku_long_fsync_threshold) {
toku_sync_fetch_and_add(&toku_long_fsync_count, 1);
toku_sync_fetch_and_add(&toku_long_fsync_time, duration);
diff --git a/storage/tokudb/PerconaFT/portability/memory.cc b/storage/tokudb/PerconaFT/portability/memory.cc
index 5430ff84b70..9594158cf38 100644
--- a/storage/tokudb/PerconaFT/portability/memory.cc
+++ b/storage/tokudb/PerconaFT/portability/memory.cc
@@ -104,7 +104,13 @@ toku_memory_startup(void) {
size_t lg_chunk; // log2 of the mmap threshold
size_t lg_chunk_length = sizeof lg_chunk;
result = mallctl_f("opt.lg_chunk", &lg_chunk, &lg_chunk_length, NULL, 0);
- if (result == 0)
+ if (result)
+ {
+ status.mmap_threshold = 1 << 21; // Default value.
+ // Incompatible jemalloc change.
+ result = 0;
+ }
+ else
status.mmap_threshold = 1 << lg_chunk;
}
}
diff --git a/storage/tokudb/PerconaFT/portability/portability.cc b/storage/tokudb/PerconaFT/portability/portability.cc
index 19f445a85d7..dfa5153cc66 100644
--- a/storage/tokudb/PerconaFT/portability/portability.cc
+++ b/storage/tokudb/PerconaFT/portability/portability.cc
@@ -79,6 +79,9 @@ Copyright (c) 2006, 2015, Percona and/or its affiliates. All rights reserved.
#include "toku_os.h"
#include "toku_time.h"
#include "memory.h"
+
+#include "toku_instrumentation.h"
+
#include <portability/toku_atomic.h>
#include <util/partitioned_counter.h>
@@ -172,13 +175,26 @@ toku_os_get_phys_memory_size(void) {
#endif
}
-int
-toku_os_get_file_size(int fildes, int64_t *fsize) {
+int toku_os_get_file_size_with_source_location(int fildes,
+ int64_t *fsize,
+ const char *src_file,
+ uint src_line) {
toku_struct_stat sbuf;
+
+ toku_io_instrumentation io_annotation;
+ toku_instr_file_io_begin(io_annotation,
+ toku_instr_file_op::file_stat,
+ fildes,
+ 0,
+ src_file,
+ src_line);
+
int r = fstat(fildes, &sbuf);
- if (r==0) {
+ if (r == 0) {
*fsize = sbuf.st_size;
}
+ toku_instr_file_io_end(io_annotation, 0);
+
return r;
}
@@ -272,15 +288,39 @@ toku_os_get_max_process_data_size(uint64_t *maxdata) {
return r;
}
-int
-toku_stat(const char *name, toku_struct_stat *buf) {
+int toku_stat_with_source_location(const char *name,
+ toku_struct_stat *buf,
+ const toku_instr_key &instr_key,
+ const char *src_file,
+ uint src_line) {
+ toku_io_instrumentation io_annotation;
+ toku_instr_file_name_io_begin(io_annotation,
+ instr_key,
+ toku_instr_file_op::file_stat,
+ name,
+ 0,
+ src_file,
+ src_line);
int r = stat(name, buf);
+
+ toku_instr_file_io_end(io_annotation, 0);
return r;
}
-int
-toku_fstat(int fd, toku_struct_stat *buf) {
+int toku_os_fstat_with_source_location(int fd,
+ toku_struct_stat *buf,
+ const char *src_file,
+ uint src_line) {
+ toku_io_instrumentation io_annotation;
+ toku_instr_file_io_begin(io_annotation,
+ toku_instr_file_op::file_stat,
+ fd,
+ 0,
+ src_file,
+ src_line);
+
int r = fstat(fd, buf);
+ toku_instr_file_io_end(io_annotation, 0);
return r;
}
@@ -427,5 +467,9 @@ void __attribute__((constructor)) toku_portability_helgrind_ignore(void);
void
toku_portability_helgrind_ignore(void) {
TOKU_VALGRIND_HG_DISABLE_CHECKING(&toku_cached_hz, sizeof toku_cached_hz);
- TOKU_VALGRIND_HG_DISABLE_CHECKING(&toku_cached_pagesize, sizeof toku_cached_pagesize);
+ TOKU_VALGRIND_HG_DISABLE_CHECKING(&toku_cached_pagesize,
+ sizeof toku_cached_pagesize);
}
+
+static const pfs_key_t pfs_not_instrumented = 0xFFFFFFFF;
+toku_instr_key toku_uninstrumented(pfs_not_instrumented);
diff --git a/storage/tokudb/PerconaFT/portability/tests/rwlock_condvar.h b/storage/tokudb/PerconaFT/portability/tests/rwlock_condvar.h
index 92f2fb354f7..d1ebc81e1dc 100644
--- a/storage/tokudb/PerconaFT/portability/tests/rwlock_condvar.h
+++ b/storage/tokudb/PerconaFT/portability/tests/rwlock_condvar.h
@@ -107,10 +107,10 @@ get_waitstate(void)
#endif
int toku_cv_fair_rwlock_init (toku_cv_fair_rwlock_t *rwlock) {
- rwlock->state=0;
+ rwlock->state = 0;
rwlock->waiters_head = NULL;
rwlock->waiters_tail = NULL;
- toku_mutex_init(&rwlock->mutex, NULL);
+ toku_mutex_init(toku_uninstrumented, &rwlock->mutex, nullptr);
return 0;
}
diff --git a/storage/tokudb/PerconaFT/portability/tests/test-pthread-rwlock-rdlock.cc b/storage/tokudb/PerconaFT/portability/tests/test-pthread-rwlock-rdlock.cc
index 364bd7d3766..62aa5205f3c 100644
--- a/storage/tokudb/PerconaFT/portability/tests/test-pthread-rwlock-rdlock.cc
+++ b/storage/tokudb/PerconaFT/portability/tests/test-pthread-rwlock-rdlock.cc
@@ -48,7 +48,7 @@ int test_main(int argc __attribute__((__unused__)), char *const argv[] __attribu
toku_pthread_rwlock_t rwlock;
ZERO_STRUCT(rwlock);
- toku_pthread_rwlock_init(&rwlock, NULL);
+ toku_pthread_rwlock_init(toku_uninstrumented, &rwlock, nullptr);
toku_pthread_rwlock_rdlock(&rwlock);
toku_pthread_rwlock_rdlock(&rwlock);
toku_pthread_rwlock_rdunlock(&rwlock);
diff --git a/storage/tokudb/PerconaFT/portability/tests/test-pthread-rwlock-rwr.cc b/storage/tokudb/PerconaFT/portability/tests/test-pthread-rwlock-rwr.cc
index 3ca9e06bff7..92b30421ba9 100644
--- a/storage/tokudb/PerconaFT/portability/tests/test-pthread-rwlock-rwr.cc
+++ b/storage/tokudb/PerconaFT/portability/tests/test-pthread-rwlock-rwr.cc
@@ -71,13 +71,19 @@ int test_main(int argc , char *const argv[] ) {
toku_pthread_t tid;
void *retptr;
- toku_pthread_rwlock_init(&rwlock, NULL);
- state = 37; if (verbose) printf("%s:%d\n", __FUNCTION__, __LINE__);
+ toku_pthread_rwlock_init(toku_uninstrumented, &rwlock, nullptr);
+ state = 37;
+ if (verbose)
+ printf("%s:%d\n", __FUNCTION__, __LINE__);
toku_pthread_rwlock_rdlock(&rwlock);
- r = toku_pthread_create(&tid, NULL, f, &rwlock); assert(r == 0);
+ r = toku_pthread_create(toku_uninstrumented, &tid, nullptr, f, &rwlock);
+ assert(r == 0);
- assert(state==37); state = 42; if (verbose) printf("%s:%d\n", __FUNCTION__, __LINE__);
+ assert(state == 37);
+ state = 42;
+ if (verbose)
+ printf("%s:%d\n", __FUNCTION__, __LINE__);
sleep(4);
assert(state==16); state = 44; if (verbose) printf("%s:%d\n", __FUNCTION__, __LINE__);
toku_pthread_rwlock_rdlock(&rwlock);
diff --git a/storage/tokudb/PerconaFT/portability/tests/test-stat.cc b/storage/tokudb/PerconaFT/portability/tests/test-stat.cc
index 494a17bd074..57201764afb 100644
--- a/storage/tokudb/PerconaFT/portability/tests/test-stat.cc
+++ b/storage/tokudb/PerconaFT/portability/tests/test-stat.cc
@@ -47,10 +47,11 @@ Copyright (c) 2006, 2015, Percona and/or its affiliates. All rights reserved.
static void test_stat(const char *dirname, int result, int ex_errno) {
int r;
toku_struct_stat buf;
- r = toku_stat(dirname, &buf);
- //printf("stat %s %d %d\n", dirname, r, errno); fflush(stdout);
- assert(r==result);
- if (r!=0) assert(get_maybe_error_errno() == ex_errno);
+ r = toku_stat(dirname, &buf, toku_uninstrumented);
+ // printf("stat %s %d %d\n", dirname, r, errno); fflush(stdout);
+ assert(r == result);
+ if (r != 0)
+ assert(get_maybe_error_errno() == ex_errno);
}
int main(void) {
diff --git a/storage/tokudb/PerconaFT/portability/tests/test-toku-malloc.cc b/storage/tokudb/PerconaFT/portability/tests/test-toku-malloc.cc
index 1e1c7533c15..0d99b36b51a 100644
--- a/storage/tokudb/PerconaFT/portability/tests/test-toku-malloc.cc
+++ b/storage/tokudb/PerconaFT/portability/tests/test-toku-malloc.cc
@@ -53,10 +53,12 @@ int main(void) {
int i;
const int max_threads = 2;
toku_pthread_t tids[max_threads];
- for (i=0; i<max_threads; i++) {
- r = toku_pthread_create(&tids[i], NULL, f, 0); assert(r == 0);
+ for (i = 0; i < max_threads; i++) {
+ r = toku_pthread_create(
+ toku_uninstrumented, &tids[i], nullptr, f, nullptr);
+ assert(r == 0);
}
- for (i=0; i<max_threads; i++) {
+ for (i = 0; i < max_threads; i++) {
void *ret;
r = toku_pthread_join(tids[i], &ret); assert(r == 0);
}
diff --git a/storage/tokudb/PerconaFT/portability/toku_assert.h b/storage/tokudb/PerconaFT/portability/toku_assert.h
index 26a71cede7d..b0a7be3287b 100644
--- a/storage/tokudb/PerconaFT/portability/toku_assert.h
+++ b/storage/tokudb/PerconaFT/portability/toku_assert.h
@@ -53,13 +53,9 @@ Copyright (c) 2006, 2015, Percona and/or its affiliates. All rights reserved.
#error NDEBUG should not be set
#endif
-static inline int get_error_errno(void);
+inline int get_error_errno(void);
-static inline int
-get_maybe_error_errno(void)
-{
- return errno;
-}
+static inline int get_maybe_error_errno(void) { return errno; }
static inline void
set_errno(int new_errno)
@@ -139,12 +135,10 @@ void db_env_do_backtrace(FILE *outf);
#define paranoid_invariant(a) ((void) 0)
#define paranoid_invariant_null(a) ((void) 0)
#define paranoid_invariant_notnull(a) ((void) 0)
-#define paranoid_invariant_zero(a) ((void) 0)
+#define paranoid_invariant_zero(a) ((void)0)
#endif
-static inline int
-get_error_errno(void)
-{
+inline int get_error_errno(void) {
invariant(errno);
return errno;
}
diff --git a/storage/tokudb/PerconaFT/portability/toku_instr_mysql.cc b/storage/tokudb/PerconaFT/portability/toku_instr_mysql.cc
new file mode 100644
index 00000000000..b7b4c0ab233
--- /dev/null
+++ b/storage/tokudb/PerconaFT/portability/toku_instr_mysql.cc
@@ -0,0 +1,365 @@
+#ifdef MYSQL_TOKUDB_ENGINE
+#include "toku_portability.h"
+#include "toku_pthread.h"
+
+toku_instr_probe_pfs::toku_instr_probe_pfs(const toku_instr_key &key)
+ : mutex(new toku_mutex_t) {
+ toku_mutex_init(key, mutex.get(), nullptr);
+}
+
+toku_instr_probe_pfs::~toku_instr_probe_pfs() {
+ toku_mutex_destroy(mutex.get());
+}
+
+// Thread instrumentation
+
+int toku_pthread_create(const toku_instr_key &key,
+ pthread_t *thread,
+ const pthread_attr_t *attr,
+ void *(*start_routine)(void *),
+ void *arg) {
+#if (MYSQL_VERSION_MAJOR >= 5) && (MYSQL_VERSION_MINOR >= 7)
+ return PSI_THREAD_CALL(spawn_thread)(
+ key.id(), reinterpret_cast<my_thread_handle *>(thread),
+ attr, start_routine, arg);
+#else
+ return PSI_THREAD_CALL(spawn_thread)(
+ key.id(), thread, attr, start_routine, arg);
+#endif
+}
+
+void toku_instr_register_current_thread(const toku_instr_key &key) {
+ struct PSI_thread *psi_thread =
+ PSI_THREAD_CALL(new_thread)(key.id(), nullptr, 0);
+ PSI_THREAD_CALL(set_thread)(psi_thread);
+}
+
+void toku_instr_delete_current_thread() {
+ PSI_THREAD_CALL(delete_current_thread)();
+}
+
+// I/O instrumentation
+
+void toku_instr_file_open_begin(toku_io_instrumentation &io_instr,
+ const toku_instr_key &key,
+ toku_instr_file_op op,
+ const char *name,
+ const char *src_file,
+ int src_line) {
+ io_instr.locker =
+ PSI_FILE_CALL(get_thread_file_name_locker)(
+ &io_instr.state, key.id(), static_cast<PSI_file_operation>(op),
+ name, io_instr.locker);
+ if (io_instr.locker != nullptr) {
+ PSI_FILE_CALL(start_file_open_wait)
+ (io_instr.locker, src_file, src_line);
+ }
+}
+
+void toku_instr_file_stream_open_end(toku_io_instrumentation &io_instr,
+ TOKU_FILE &file) {
+ file.key = nullptr;
+ if (FT_LIKELY(io_instr.locker)) {
+ file.key =
+ PSI_FILE_CALL(end_file_open_wait)(io_instr.locker, file.file);
+ }
+}
+
+void toku_instr_file_open_end(toku_io_instrumentation &io_instr, int fd) {
+ if (FT_LIKELY(io_instr.locker))
+ PSI_FILE_CALL(end_file_open_wait_and_bind_to_descriptor)
+ (io_instr.locker, fd);
+}
+
+void toku_instr_file_name_close_begin(toku_io_instrumentation &io_instr,
+ const toku_instr_key &key,
+ toku_instr_file_op op,
+ const char *name,
+ const char *src_file,
+ int src_line) {
+ io_instr.locker =
+ PSI_FILE_CALL(get_thread_file_name_locker)(
+ &io_instr.state, key.id(), static_cast<PSI_file_operation>(op),
+ name,
+ io_instr.locker);
+ if (FT_LIKELY(io_instr.locker)) {
+ PSI_FILE_CALL(start_file_close_wait)
+ (io_instr.locker, src_file, src_line);
+ }
+}
+
+void toku_instr_file_stream_close_begin(toku_io_instrumentation &io_instr,
+ toku_instr_file_op op,
+ const TOKU_FILE &file,
+ const char *src_file,
+ int src_line) {
+ io_instr.locker = nullptr;
+ if (FT_LIKELY(file.key)) {
+ io_instr.locker = PSI_FILE_CALL(get_thread_file_stream_locker)(
+ &io_instr.state, file.key, (PSI_file_operation)op);
+ if (FT_LIKELY(io_instr.locker)) {
+ PSI_FILE_CALL(start_file_close_wait)
+ (io_instr.locker, src_file, src_line);
+ }
+ }
+}
+
+void toku_instr_file_fd_close_begin(toku_io_instrumentation &io_instr,
+ toku_instr_file_op op,
+ int fd,
+ const char *src_file,
+ int src_line) {
+ io_instr.locker = PSI_FILE_CALL(get_thread_file_descriptor_locker)(
+ &io_instr.state, fd, (PSI_file_operation)op);
+ if (FT_LIKELY(io_instr.locker)) {
+ PSI_FILE_CALL(start_file_close_wait)
+ (io_instr.locker, src_file, src_line);
+ }
+}
+
+void toku_instr_file_close_end(const toku_io_instrumentation &io_instr,
+ int result) {
+ if (FT_LIKELY(io_instr.locker))
+ PSI_FILE_CALL(end_file_close_wait)
+ (io_instr.locker, result);
+}
+
+void toku_instr_file_io_begin(toku_io_instrumentation &io_instr,
+ toku_instr_file_op op,
+ int fd,
+ ssize_t count,
+ const char *src_file,
+ int src_line) {
+ io_instr.locker = PSI_FILE_CALL(get_thread_file_descriptor_locker)(
+ &io_instr.state, fd, (PSI_file_operation)op);
+ if (FT_LIKELY(io_instr.locker)) {
+ PSI_FILE_CALL(start_file_wait)
+ (io_instr.locker, count, src_file, src_line);
+ }
+}
+
+void toku_instr_file_name_io_begin(toku_io_instrumentation &io_instr,
+ const toku_instr_key &key,
+ toku_instr_file_op op,
+ const char *name,
+ ssize_t count,
+ const char *src_file,
+ int src_line) {
+ io_instr.locker =
+ PSI_FILE_CALL(get_thread_file_name_locker)(&io_instr.state,
+ key.id(),
+ (PSI_file_operation)op,
+ name,
+ &io_instr.locker);
+ if (FT_LIKELY(io_instr.locker)) {
+ PSI_FILE_CALL(start_file_wait)
+ (io_instr.locker, count, src_file, src_line);
+ }
+}
+
+void toku_instr_file_stream_io_begin(toku_io_instrumentation &io_instr,
+ toku_instr_file_op op,
+ const TOKU_FILE &file,
+ ssize_t count,
+ const char *src_file,
+ int src_line) {
+ io_instr.locker = nullptr;
+ if (FT_LIKELY(file.key)) {
+ io_instr.locker = PSI_FILE_CALL(get_thread_file_stream_locker)(
+ &io_instr.state, file.key, (PSI_file_operation)op);
+ if (FT_LIKELY(io_instr.locker)) {
+ PSI_FILE_CALL(start_file_wait)
+ (io_instr.locker, count, src_file, src_line);
+ }
+ }
+}
+
+void toku_instr_file_io_end(toku_io_instrumentation &io_instr, ssize_t count) {
+ if (FT_LIKELY(io_instr.locker))
+ PSI_FILE_CALL(end_file_wait)
+ (io_instr.locker, count);
+}
+
+// Mutex instrumentation
+
+void toku_instr_mutex_init(const toku_instr_key &key, toku_mutex_t &mutex) {
+ mutex.psi_mutex = PSI_MUTEX_CALL(init_mutex)(key.id(), &mutex.pmutex);
+#if TOKU_PTHREAD_DEBUG
+ mutex.instr_key_id = key.id();
+#endif
+}
+
+void toku_instr_mutex_destroy(PSI_mutex *&mutex_instr) {
+ if (mutex_instr != nullptr) {
+ PSI_MUTEX_CALL(destroy_mutex)(mutex_instr);
+ mutex_instr = nullptr;
+ }
+}
+
+void toku_instr_mutex_lock_start(toku_mutex_instrumentation &mutex_instr,
+ toku_mutex_t &mutex,
+ const char *src_file,
+ int src_line) {
+ mutex_instr.locker = nullptr;
+ if (mutex.psi_mutex) {
+ mutex_instr.locker =
+ PSI_MUTEX_CALL(start_mutex_wait)(&mutex_instr.state,
+ mutex.psi_mutex,
+ PSI_MUTEX_LOCK,
+ src_file,
+ src_line);
+ }
+}
+
+void toku_instr_mutex_trylock_start(toku_mutex_instrumentation &mutex_instr,
+ toku_mutex_t &mutex,
+ const char *src_file,
+ int src_line) {
+ mutex_instr.locker = nullptr;
+ if (mutex.psi_mutex) {
+ mutex_instr.locker =
+ PSI_MUTEX_CALL(start_mutex_wait)(&mutex_instr.state,
+ mutex.psi_mutex,
+ PSI_MUTEX_TRYLOCK,
+ src_file,
+ src_line);
+ }
+}
+
+void toku_instr_mutex_lock_end(toku_mutex_instrumentation &mutex_instr,
+ int pthread_mutex_lock_result) {
+ if (mutex_instr.locker)
+ PSI_MUTEX_CALL(end_mutex_wait)
+ (mutex_instr.locker, pthread_mutex_lock_result);
+}
+
+void toku_instr_mutex_unlock(PSI_mutex *mutex_instr) {
+ if (mutex_instr)
+ PSI_MUTEX_CALL(unlock_mutex)(mutex_instr);
+}
+
+// Condvar instrumentation
+
+void toku_instr_cond_init(const toku_instr_key &key, toku_cond_t &cond) {
+ cond.psi_cond = PSI_COND_CALL(init_cond)(key.id(), &cond.pcond);
+#if TOKU_PTHREAD_DEBUG
+ cond.instr_key_id = key.id();
+#endif
+}
+
+void toku_instr_cond_destroy(PSI_cond *&cond_instr) {
+ if (cond_instr != nullptr) {
+ PSI_COND_CALL(destroy_cond)(cond_instr);
+ cond_instr = nullptr;
+ }
+}
+
+void toku_instr_cond_wait_start(toku_cond_instrumentation &cond_instr,
+ toku_instr_cond_op op,
+ toku_cond_t &cond,
+ toku_mutex_t &mutex,
+ const char *src_file,
+ int src_line) {
+ cond_instr.locker = nullptr;
+ if (cond.psi_cond) {
+ /* Instrumentation start */
+ cond_instr.locker =
+ PSI_COND_CALL(start_cond_wait)(&cond_instr.state,
+ cond.psi_cond,
+ mutex.psi_mutex,
+ (PSI_cond_operation)op,
+ src_file,
+ src_line);
+ }
+}
+
+void toku_instr_cond_wait_end(toku_cond_instrumentation &cond_instr,
+ int pthread_cond_wait_result) {
+ if (cond_instr.locker)
+ PSI_COND_CALL(end_cond_wait)
+ (cond_instr.locker, pthread_cond_wait_result);
+}
+
+void toku_instr_cond_signal(const toku_cond_t &cond) {
+ if (cond.psi_cond)
+ PSI_COND_CALL(signal_cond)(cond.psi_cond);
+}
+
+void toku_instr_cond_broadcast(const toku_cond_t &cond) {
+ if (cond.psi_cond)
+ PSI_COND_CALL(broadcast_cond)(cond.psi_cond);
+}
+
+// rwlock instrumentation
+
+void toku_instr_rwlock_init(const toku_instr_key &key,
+ toku_pthread_rwlock_t &rwlock) {
+ rwlock.psi_rwlock = PSI_RWLOCK_CALL(init_rwlock)(key.id(), &rwlock.rwlock);
+#if TOKU_PTHREAD_DEBUG
+ rwlock.instr_key_id = key.id();
+#endif
+}
+
+void toku_instr_rwlock_destroy(PSI_rwlock *&rwlock_instr) {
+ if (rwlock_instr != nullptr) {
+ PSI_RWLOCK_CALL(destroy_rwlock)(rwlock_instr);
+ rwlock_instr = nullptr;
+ }
+}
+
+void toku_instr_rwlock_rdlock_wait_start(
+ toku_rwlock_instrumentation &rwlock_instr,
+ toku_pthread_rwlock_t &rwlock,
+ const char *src_file,
+ int src_line) {
+ rwlock_instr.locker = nullptr;
+ if (rwlock.psi_rwlock) {
+ /* Instrumentation start */
+ rwlock_instr.locker =
+ PSI_RWLOCK_CALL(start_rwlock_rdwait)(&rwlock_instr.state,
+ rwlock.psi_rwlock,
+ PSI_RWLOCK_READLOCK,
+ src_file,
+ src_line);
+ }
+}
+
+void toku_instr_rwlock_wrlock_wait_start(
+ toku_rwlock_instrumentation &rwlock_instr,
+ toku_pthread_rwlock_t &rwlock,
+ const char *src_file,
+ int src_line) {
+ rwlock_instr.locker = nullptr;
+ if (rwlock.psi_rwlock) {
+ /* Instrumentation start */
+ rwlock_instr.locker =
+ PSI_RWLOCK_CALL(start_rwlock_wrwait)(&rwlock_instr.state,
+ rwlock.psi_rwlock,
+ PSI_RWLOCK_WRITELOCK,
+ src_file,
+ src_line);
+ }
+}
+
+void toku_instr_rwlock_rdlock_wait_end(
+ toku_rwlock_instrumentation &rwlock_instr,
+ int pthread_rwlock_wait_result) {
+ if (rwlock_instr.locker)
+ PSI_RWLOCK_CALL(end_rwlock_rdwait)
+ (rwlock_instr.locker, pthread_rwlock_wait_result);
+}
+
+void toku_instr_rwlock_wrlock_wait_end(
+ toku_rwlock_instrumentation &rwlock_instr,
+ int pthread_rwlock_wait_result) {
+ if (rwlock_instr.locker)
+ PSI_RWLOCK_CALL(end_rwlock_wrwait)
+ (rwlock_instr.locker, pthread_rwlock_wait_result);
+}
+
+void toku_instr_rwlock_unlock(toku_pthread_rwlock_t &rwlock) {
+ if (rwlock.psi_rwlock)
+ PSI_RWLOCK_CALL(unlock_rwlock)(rwlock.psi_rwlock);
+}
+
+#endif // MYSQL_TOKUDB_ENGINE
diff --git a/storage/tokudb/PerconaFT/portability/toku_instr_mysql.h b/storage/tokudb/PerconaFT/portability/toku_instr_mysql.h
new file mode 100644
index 00000000000..d6b0ed35ce9
--- /dev/null
+++ b/storage/tokudb/PerconaFT/portability/toku_instr_mysql.h
@@ -0,0 +1,249 @@
+#ifdef TOKU_INSTR_MYSQL_H
+// This file can be included only from toku_instumentation.h because
+// it replaces the defintitions for the case if MySQL PFS is available
+#error "toku_instr_mysql.h can be included only once"
+#else // TOKU_INSTR_MYSQL_H
+#define TOKU_INSTR_MYSQL_H
+
+#include <memory>
+
+// As these macros are defined in my_global.h
+// and they are also defined in command line
+// undefine them here to avoid compilation errors.
+#undef __STDC_FORMAT_MACROS
+#undef __STDC_LIMIT_MACROS
+#include <mysql/psi/mysql_file.h> // PSI_file
+#include <mysql/psi/mysql_thread.h> // PSI_mutex
+
+#ifndef HAVE_PSI_MUTEX_INTERFACE
+#error HAVE_PSI_MUTEX_INTERFACE required
+#endif
+#ifndef HAVE_PSI_RWLOCK_INTERFACE
+#error HAVE_PSI_RWLOCK_INTERFACE required
+#endif
+#ifndef HAVE_PSI_THREAD_INTERFACE
+#error HAVE_PSI_THREAD_INTERFACE required
+#endif
+
+// Instrumentation keys
+
+class toku_instr_key {
+ private:
+ pfs_key_t m_id;
+
+ public:
+ toku_instr_key(toku_instr_object_type type,
+ const char *group,
+ const char *name) {
+ switch (type) {
+ case toku_instr_object_type::mutex: {
+ PSI_mutex_info mutex_info{&m_id, name, 0};
+ mysql_mutex_register(group, &mutex_info, 1);
+ } break;
+ case toku_instr_object_type::rwlock: {
+ PSI_rwlock_info rwlock_info{&m_id, name, 0};
+ mysql_rwlock_register(group, &rwlock_info, 1);
+ } break;
+ case toku_instr_object_type::cond: {
+ PSI_cond_info cond_info{&m_id, name, 0};
+ mysql_cond_register(group, &cond_info, 1);
+ } break;
+ case toku_instr_object_type::thread: {
+ PSI_thread_info thread_info{&m_id, name, 0};
+ mysql_thread_register(group, &thread_info, 1);
+ } break;
+ case toku_instr_object_type::file: {
+ PSI_file_info file_info{&m_id, name, 0};
+ mysql_file_register(group, &file_info, 1);
+ } break;
+ }
+ }
+
+ explicit toku_instr_key(pfs_key_t key_id) : m_id(key_id) {}
+
+ pfs_key_t id() const { return m_id; }
+};
+
+// Thread instrumentation
+int toku_pthread_create(const toku_instr_key &key,
+ pthread_t *thread,
+ const pthread_attr_t *attr,
+ void *(*start_routine)(void *),
+ void *arg);
+void toku_instr_register_current_thread(const toku_instr_key &key);
+void toku_instr_delete_current_thread();
+
+// I/O instrumentation
+
+enum class toku_instr_file_op {
+ file_stream_open = PSI_FILE_STREAM_OPEN,
+ file_create = PSI_FILE_CREATE,
+ file_open = PSI_FILE_OPEN,
+ file_delete = PSI_FILE_DELETE,
+ file_rename = PSI_FILE_RENAME,
+ file_read = PSI_FILE_READ,
+ file_write = PSI_FILE_WRITE,
+ file_sync = PSI_FILE_SYNC,
+ file_stream_close = PSI_FILE_STREAM_CLOSE,
+ file_close = PSI_FILE_CLOSE,
+ file_stat = PSI_FILE_STAT
+};
+
+struct toku_io_instrumentation {
+ struct PSI_file_locker *locker;
+ PSI_file_locker_state state;
+
+ toku_io_instrumentation() : locker(nullptr) {}
+};
+
+void toku_instr_file_open_begin(toku_io_instrumentation &io_instr,
+ const toku_instr_key &key,
+ toku_instr_file_op op,
+ const char *name,
+ const char *src_file,
+ int src_line);
+void toku_instr_file_stream_open_end(toku_io_instrumentation &io_instr,
+ TOKU_FILE &file);
+void toku_instr_file_open_end(toku_io_instrumentation &io_instr, int fd);
+void toku_instr_file_name_close_begin(toku_io_instrumentation &io_instr,
+ const toku_instr_key &key,
+ toku_instr_file_op op,
+ const char *name,
+ const char *src_file,
+ int src_line);
+void toku_instr_file_stream_close_begin(toku_io_instrumentation &io_instr,
+ toku_instr_file_op op,
+ const TOKU_FILE &file,
+ const char *src_file,
+ int src_line);
+void toku_instr_file_fd_close_begin(toku_io_instrumentation &io_instr,
+ toku_instr_file_op op,
+ int fd,
+ const char *src_file,
+ int src_line);
+void toku_instr_file_close_end(const toku_io_instrumentation &io_instr,
+ int result);
+void toku_instr_file_io_begin(toku_io_instrumentation &io_instr,
+ toku_instr_file_op op,
+ int fd,
+ ssize_t count,
+ const char *src_file,
+ int src_line);
+void toku_instr_file_name_io_begin(toku_io_instrumentation &io_instr,
+ const toku_instr_key &key,
+ toku_instr_file_op op,
+ const char *name,
+ ssize_t count,
+ const char *src_file,
+ int src_line);
+void toku_instr_file_stream_io_begin(toku_io_instrumentation &io_instr,
+ toku_instr_file_op op,
+ const TOKU_FILE &file,
+ ssize_t count,
+ const char *src_file,
+ int src_line);
+void toku_instr_file_io_end(toku_io_instrumentation &io_instr, ssize_t count);
+
+// Mutex instrumentation
+
+struct toku_mutex_instrumentation {
+ struct PSI_mutex_locker *locker;
+ PSI_mutex_locker_state state;
+
+ toku_mutex_instrumentation() : locker(nullptr) {}
+};
+
+void toku_instr_mutex_init(const toku_instr_key &key, toku_mutex_t &mutex);
+void toku_instr_mutex_destroy(PSI_mutex *&mutex_instr);
+void toku_instr_mutex_lock_start(toku_mutex_instrumentation &mutex_instr,
+ toku_mutex_t &mutex,
+ const char *src_file,
+ int src_line);
+void toku_instr_mutex_trylock_start(toku_mutex_instrumentation &mutex_instr,
+ toku_mutex_t &mutex,
+ const char *src_file,
+ int src_line);
+void toku_instr_mutex_lock_end(toku_mutex_instrumentation &mutex_instr,
+ int pthread_mutex_lock_result);
+void toku_instr_mutex_unlock(PSI_mutex *mutex_instr);
+
+// Instrumentation probes
+
+class toku_instr_probe_pfs {
+ private:
+ std::unique_ptr<toku_mutex_t> mutex;
+ toku_mutex_instrumentation mutex_instr;
+
+ public:
+ explicit toku_instr_probe_pfs(const toku_instr_key &key);
+
+ ~toku_instr_probe_pfs();
+
+ void start_with_source_location(const char *src_file, int src_line) {
+ mutex_instr.locker = nullptr;
+ toku_instr_mutex_lock_start(mutex_instr, *mutex, src_file, src_line);
+ }
+
+ void stop() { toku_instr_mutex_lock_end(mutex_instr, 0); }
+};
+
+typedef toku_instr_probe_pfs toku_instr_probe;
+
+// Condvar instrumentation
+
+struct toku_cond_instrumentation {
+ struct PSI_cond_locker *locker;
+ PSI_cond_locker_state state;
+
+ toku_cond_instrumentation() : locker(nullptr) {}
+};
+
+enum class toku_instr_cond_op {
+ cond_wait = PSI_COND_WAIT,
+ cond_timedwait = PSI_COND_TIMEDWAIT,
+};
+
+void toku_instr_cond_init(const toku_instr_key &key, toku_cond_t &cond);
+void toku_instr_cond_destroy(PSI_cond *&cond_instr);
+void toku_instr_cond_wait_start(toku_cond_instrumentation &cond_instr,
+ toku_instr_cond_op op,
+ toku_cond_t &cond,
+ toku_mutex_t &mutex,
+ const char *src_file,
+ int src_line);
+void toku_instr_cond_wait_end(toku_cond_instrumentation &cond_instr,
+ int pthread_cond_wait_result);
+void toku_instr_cond_signal(const toku_cond_t &cond);
+void toku_instr_cond_broadcast(const toku_cond_t &cond);
+
+// rwlock instrumentation
+
+struct toku_rwlock_instrumentation {
+ struct PSI_rwlock_locker *locker;
+ PSI_rwlock_locker_state state;
+
+ toku_rwlock_instrumentation() : locker(nullptr) { }
+};
+
+void toku_instr_rwlock_init(const toku_instr_key &key,
+ toku_pthread_rwlock_t &rwlock);
+void toku_instr_rwlock_destroy(PSI_rwlock *&rwlock_instr);
+void toku_instr_rwlock_rdlock_wait_start(
+ toku_rwlock_instrumentation &rwlock_instr,
+ toku_pthread_rwlock_t &rwlock,
+ const char *src_file,
+ int src_line);
+void toku_instr_rwlock_wrlock_wait_start(
+ toku_rwlock_instrumentation &rwlock_instr,
+ toku_pthread_rwlock_t &rwlock,
+ const char *src_file,
+ int src_line);
+void toku_instr_rwlock_rdlock_wait_end(
+ toku_rwlock_instrumentation &rwlock_instr,
+ int pthread_rwlock_wait_result);
+void toku_instr_rwlock_wrlock_wait_end(
+ toku_rwlock_instrumentation &rwlock_instr,
+ int pthread_rwlock_wait_result);
+void toku_instr_rwlock_unlock(toku_pthread_rwlock_t &rwlock);
+
+#endif // TOKU_INSTR_MYSQL_H
diff --git a/storage/tokudb/PerconaFT/portability/toku_instrumentation.h b/storage/tokudb/PerconaFT/portability/toku_instrumentation.h
new file mode 100644
index 00000000000..8c9390edc0a
--- /dev/null
+++ b/storage/tokudb/PerconaFT/portability/toku_instrumentation.h
@@ -0,0 +1,339 @@
+#pragma once
+
+#include <stdio.h> // FILE
+
+// Performance instrumentation object identifier type
+typedef unsigned int pfs_key_t;
+
+enum class toku_instr_object_type { mutex, rwlock, cond, thread, file };
+
+struct PSI_file;
+
+struct TOKU_FILE {
+ /** The real file. */
+ FILE *file;
+ struct PSI_file *key;
+ TOKU_FILE() : file(nullptr), key(nullptr) {}
+};
+
+struct PSI_mutex;
+struct PSI_cond;
+struct PSI_rwlock;
+
+struct toku_mutex_t;
+struct toku_cond_t;
+struct toku_pthread_rwlock_t;
+
+class toku_instr_key;
+
+class toku_instr_probe_empty {
+ public:
+ explicit toku_instr_probe_empty(UU(const toku_instr_key &key)) {}
+
+ void start_with_source_location(UU(const char *src_file),
+ UU(int src_line)) {}
+
+ void stop() {}
+};
+
+#define TOKU_PROBE_START(p) p->start_with_source_location(__FILE__, __LINE__)
+#define TOKU_PROBE_STOP(p) p->stop
+
+extern toku_instr_key toku_uninstrumented;
+
+#ifndef MYSQL_TOKUDB_ENGINE
+
+#include <pthread.h>
+
+class toku_instr_key {
+ public:
+ toku_instr_key(UU(toku_instr_object_type type),
+ UU(const char *group),
+ UU(const char *name)) {}
+
+ explicit toku_instr_key(UU(pfs_key_t key_id)) {}
+};
+
+typedef toku_instr_probe_empty toku_instr_probe;
+
+enum class toku_instr_file_op {
+ file_stream_open,
+ file_create,
+ file_open,
+ file_delete,
+ file_rename,
+ file_read,
+ file_write,
+ file_sync,
+ file_stream_close,
+ file_close,
+ file_stat
+};
+
+struct PSI_file {};
+struct PSI_mutex {};
+
+struct toku_io_instrumentation {};
+
+inline int toku_pthread_create(UU(const toku_instr_key &key),
+ pthread_t *thread,
+ const pthread_attr_t *attr,
+ void *(*start_routine)(void *),
+ void *arg) {
+ return pthread_create(thread, attr, start_routine, arg);
+}
+
+inline void toku_instr_register_current_thread() {}
+
+inline void toku_instr_delete_current_thread() {}
+
+// Instrument file creation, opening, closing, and renaming
+inline void toku_instr_file_open_begin(UU(toku_io_instrumentation &io_instr),
+ UU(const toku_instr_key &key),
+ UU(toku_instr_file_op op),
+ UU(const char *name),
+ UU(const char *src_file),
+ UU(int src_line)) {}
+
+inline void toku_instr_file_stream_open_end(
+ UU(toku_io_instrumentation &io_instr),
+ UU(TOKU_FILE &file)) {}
+
+inline void toku_instr_file_open_end(UU(toku_io_instrumentation &io_instr),
+ UU(int fd)) {}
+
+inline void toku_instr_file_name_close_begin(
+ UU(toku_io_instrumentation &io_instr),
+ UU(const toku_instr_key &key),
+ UU(toku_instr_file_op op),
+ UU(const char *name),
+ UU(const char *src_file),
+ UU(int src_line)) {}
+
+inline void toku_instr_file_stream_close_begin(
+ UU(toku_io_instrumentation &io_instr),
+ UU(toku_instr_file_op op),
+ UU(TOKU_FILE &file),
+ UU(const char *src_file),
+ UU(int src_line)) {}
+
+inline void toku_instr_file_fd_close_begin(
+ UU(toku_io_instrumentation &io_instr),
+ UU(toku_instr_file_op op),
+ UU(int fd),
+ UU(const char *src_file),
+ UU(int src_line)) {}
+
+inline void toku_instr_file_close_end(UU(toku_io_instrumentation &io_instr),
+ UU(int result)) {}
+
+inline void toku_instr_file_io_begin(UU(toku_io_instrumentation &io_instr),
+ UU(toku_instr_file_op op),
+ UU(int fd),
+ UU(unsigned int count),
+ UU(const char *src_file),
+ UU(int src_line)) {}
+
+inline void toku_instr_file_name_io_begin(UU(toku_io_instrumentation &io_instr),
+ UU(const toku_instr_key &key),
+ UU(toku_instr_file_op op),
+ UU(const char *name),
+ UU(unsigned int count),
+ UU(const char *src_file),
+ UU(int src_line)) {}
+
+inline void toku_instr_file_stream_io_begin(
+ UU(toku_io_instrumentation &io_instr),
+ UU(toku_instr_file_op op),
+ UU(TOKU_FILE &file),
+ UU(unsigned int count),
+ UU(const char *src_file),
+ UU(int src_line)) {}
+
+inline void toku_instr_file_io_end(UU(toku_io_instrumentation &io_instr),
+ UU(unsigned int count)) {}
+
+struct toku_mutex_t;
+
+struct toku_mutex_instrumentation {};
+
+inline PSI_mutex *toku_instr_mutex_init(UU(const toku_instr_key &key),
+ UU(toku_mutex_t &mutex)) {
+ return nullptr;
+}
+
+inline void toku_instr_mutex_destroy(UU(PSI_mutex *&mutex_instr)) {}
+
+inline void toku_instr_mutex_lock_start(
+ UU(toku_mutex_instrumentation &mutex_instr),
+ UU(toku_mutex_t &mutex),
+ UU(const char *src_file),
+ UU(int src_line)) {}
+
+inline void toku_instr_mutex_trylock_start(
+ UU(toku_mutex_instrumentation &mutex_instr),
+ UU(toku_mutex_t &mutex),
+ UU(const char *src_file),
+ UU(int src_line)) {}
+
+inline void toku_instr_mutex_lock_end(
+ UU(toku_mutex_instrumentation &mutex_instr),
+ UU(int pthread_mutex_lock_result)) {}
+
+inline void toku_instr_mutex_unlock(UU(PSI_mutex *mutex_instr)) {}
+
+struct toku_cond_instrumentation {};
+
+enum class toku_instr_cond_op {
+ cond_wait,
+ cond_timedwait,
+};
+
+inline PSI_cond *toku_instr_cond_init(UU(const toku_instr_key &key),
+ UU(toku_cond_t &cond)) {
+ return nullptr;
+}
+
+inline void toku_instr_cond_destroy(UU(PSI_cond *&cond_instr)) {}
+
+inline void toku_instr_cond_wait_start(
+ UU(toku_cond_instrumentation &cond_instr),
+ UU(toku_instr_cond_op op),
+ UU(toku_cond_t &cond),
+ UU(toku_mutex_t &mutex),
+ UU(const char *src_file),
+ UU(int src_line)) {}
+
+inline void toku_instr_cond_wait_end(UU(toku_cond_instrumentation &cond_instr),
+ UU(int pthread_cond_wait_result)) {}
+
+inline void toku_instr_cond_signal(UU(toku_cond_t &cond)) {}
+
+inline void toku_instr_cond_broadcast(UU(toku_cond_t &cond)) {}
+
+// rwlock instrumentation
+struct toku_rwlock_instrumentation {};
+
+inline PSI_rwlock *toku_instr_rwlock_init(UU(const toku_instr_key &key),
+ UU(toku_pthread_rwlock_t &rwlock)) {
+ return nullptr;
+}
+
+inline void toku_instr_rwlock_destroy(UU(PSI_rwlock *&rwlock_instr)) {}
+
+inline void toku_instr_rwlock_rdlock_wait_start(
+ UU(toku_rwlock_instrumentation &rwlock_instr),
+ UU(toku_pthread_rwlock_t &rwlock),
+ UU(const char *src_file),
+ UU(int src_line)) {}
+
+inline void toku_instr_rwlock_wrlock_wait_start(
+ UU(toku_rwlock_instrumentation &rwlock_instr),
+ UU(toku_pthread_rwlock_t &rwlock),
+ UU(const char *src_file),
+ UU(int src_line)) {}
+
+inline void toku_instr_rwlock_rdlock_wait_end(
+ UU(toku_rwlock_instrumentation &rwlock_instr),
+ UU(int pthread_rwlock_wait_result)) {}
+
+inline void toku_instr_rwlock_wrlock_wait_end(
+ UU(toku_rwlock_instrumentation &rwlock_instr),
+ UU(int pthread_rwlock_wait_result)) {}
+
+inline void toku_instr_rwlock_unlock(UU(toku_pthread_rwlock_t &rwlock)) {}
+
+#else // MYSQL_TOKUDB_ENGINE
+// There can be not only mysql but also mongodb or any other PFS stuff
+#include <toku_instr_mysql.h>
+#endif // MYSQL_TOKUDB_ENGINE
+
+extern toku_instr_key toku_uninstrumented;
+
+extern toku_instr_probe *toku_instr_probe_1;
+
+// threads
+extern toku_instr_key *extractor_thread_key;
+extern toku_instr_key *fractal_thread_key;
+extern toku_instr_key *io_thread_key;
+extern toku_instr_key *eviction_thread_key;
+extern toku_instr_key *kibbutz_thread_key;
+extern toku_instr_key *minicron_thread_key;
+extern toku_instr_key *tp_internal_thread_key;
+
+// Files
+extern toku_instr_key *tokudb_file_data_key;
+extern toku_instr_key *tokudb_file_load_key;
+extern toku_instr_key *tokudb_file_tmp_key;
+extern toku_instr_key *tokudb_file_log_key;
+
+// Mutexes
+extern toku_instr_key *kibbutz_mutex_key;
+extern toku_instr_key *minicron_p_mutex_key;
+extern toku_instr_key *queue_result_mutex_key;
+extern toku_instr_key *tpool_lock_mutex_key;
+extern toku_instr_key *workset_lock_mutex_key;
+extern toku_instr_key *bjm_jobs_lock_mutex_key;
+extern toku_instr_key *log_internal_lock_mutex_key;
+extern toku_instr_key *cachetable_ev_thread_lock_mutex_key;
+extern toku_instr_key *cachetable_disk_nb_mutex_key;
+extern toku_instr_key *cachetable_m_mutex_key;
+extern toku_instr_key *safe_file_size_lock_mutex_key;
+extern toku_instr_key *checkpoint_safe_mutex_key;
+extern toku_instr_key *ft_ref_lock_mutex_key;
+extern toku_instr_key *loader_error_mutex_key;
+extern toku_instr_key *bfs_mutex_key;
+extern toku_instr_key *loader_bl_mutex_key;
+extern toku_instr_key *loader_fi_lock_mutex_key;
+extern toku_instr_key *loader_out_mutex_key;
+extern toku_instr_key *result_output_condition_lock_mutex_key;
+extern toku_instr_key *block_table_mutex_key;
+extern toku_instr_key *rollback_log_node_cache_mutex_key;
+extern toku_instr_key *txn_lock_mutex_key;
+extern toku_instr_key *txn_state_lock_mutex_key;
+extern toku_instr_key *txn_child_manager_mutex_key;
+extern toku_instr_key *txn_manager_lock_mutex_key;
+extern toku_instr_key *treenode_mutex_key;
+extern toku_instr_key *manager_mutex_key;
+extern toku_instr_key *manager_escalation_mutex_key;
+extern toku_instr_key *manager_escalator_mutex_key;
+extern toku_instr_key *db_txn_struct_i_txn_mutex_key;
+extern toku_instr_key *indexer_i_indexer_lock_mutex_key;
+extern toku_instr_key *indexer_i_indexer_estimate_lock_mutex_key;
+extern toku_instr_key *locktree_request_info_mutex_key;
+extern toku_instr_key *locktree_request_info_retry_mutex_key;
+
+// condition vars
+extern toku_instr_key *result_state_cond_key;
+extern toku_instr_key *bjm_jobs_wait_key;
+extern toku_instr_key *cachetable_p_refcount_wait_key;
+extern toku_instr_key *cachetable_m_flow_control_cond_key;
+extern toku_instr_key *cachetable_m_ev_thread_cond_key;
+extern toku_instr_key *bfs_cond_key;
+extern toku_instr_key *result_output_condition_key;
+extern toku_instr_key *manager_m_escalator_done_key;
+extern toku_instr_key *lock_request_m_wait_cond_key;
+extern toku_instr_key *queue_result_cond_key;
+extern toku_instr_key *ws_worker_wait_key;
+extern toku_instr_key *rwlock_wait_read_key;
+extern toku_instr_key *rwlock_wait_write_key;
+extern toku_instr_key *rwlock_cond_key;
+extern toku_instr_key *tp_thread_wait_key;
+extern toku_instr_key *tp_pool_wait_free_key;
+extern toku_instr_key *frwlock_m_wait_read_key;
+extern toku_instr_key *kibbutz_k_cond_key;
+extern toku_instr_key *minicron_p_condvar_key;
+extern toku_instr_key *locktree_request_info_retry_cv_key;
+
+// rwlocks
+extern toku_instr_key *multi_operation_lock_key;
+extern toku_instr_key *low_priority_multi_operation_lock_key;
+extern toku_instr_key *cachetable_m_list_lock_key;
+extern toku_instr_key *cachetable_m_pending_lock_expensive_key;
+extern toku_instr_key *cachetable_m_pending_lock_cheap_key;
+extern toku_instr_key *cachetable_m_lock_key;
+extern toku_instr_key *result_i_open_dbs_rwlock_key;
+extern toku_instr_key *checkpoint_safe_rwlock_key;
+extern toku_instr_key *cachetable_value_key;
+extern toku_instr_key *safe_file_size_lock_rwlock_key;
+extern toku_instr_key *cachetable_disk_nb_rwlock_key;
diff --git a/storage/tokudb/PerconaFT/portability/toku_os.h b/storage/tokudb/PerconaFT/portability/toku_os.h
index 3a0e7376971..d7cfcfefb9a 100644
--- a/storage/tokudb/PerconaFT/portability/toku_os.h
+++ b/storage/tokudb/PerconaFT/portability/toku_os.h
@@ -116,12 +116,10 @@ int toku_fsync_dir_by_name_without_accounting(const char *dir_name);
// *free_size is set to the bytes of free space in the file system
// *total_size is set to the total bytes in the file system
// Return 0 on success, otherwise an error number
-int toku_get_filesystem_sizes(const char *path, uint64_t *avail_size, uint64_t *free_size, uint64_t *total_size);
-
-// Portable linux 'stat'
-int toku_stat(const char *name, toku_struct_stat *statbuf) __attribute__((__visibility__("default")));
-// Portable linux 'fstat'
-int toku_fstat(int fd, toku_struct_stat *statbuf) __attribute__((__visibility__("default")));
+int toku_get_filesystem_sizes(const char *path,
+ uint64_t *avail_size,
+ uint64_t *free_size,
+ uint64_t *total_size);
// Portable linux 'dup2'
int toku_dup2(int fd, int fd2) __attribute__((__visibility__("default")));
diff --git a/storage/tokudb/PerconaFT/portability/toku_portability.h b/storage/tokudb/PerconaFT/portability/toku_portability.h
index 28ea8014f53..1096467a35d 100644
--- a/storage/tokudb/PerconaFT/portability/toku_portability.h
+++ b/storage/tokudb/PerconaFT/portability/toku_portability.h
@@ -125,6 +125,33 @@ typedef int64_t toku_off_t;
#define UU(x) x __attribute__((__unused__))
+// Branch prediction macros.
+// If supported by the compiler, will hint in inctruction caching for likely
+// branching. Should only be used where there is a very good idea of the correct
+// branch heuristics as determined by profiling. Mostly copied from InnoDB.
+// Use:
+// "if (FT_LIKELY(x))" where the chances of "x" evaluating true are higher
+// "if (FT_UNLIKELY(x))" where the chances of "x" evaluating false are higher
+#if defined(__GNUC__) && (__GNUC__ > 2) && !defined(__INTEL_COMPILER)
+
+// Tell the compiler that 'expr' probably evaluates to 'constant'.
+#define FT_EXPECT(expr, constant) __builtin_expect(expr, constant)
+
+#else
+
+#warning "No FT branch prediction operations in use!"
+#define FT_EXPECT(expr, constant) (expr)
+
+#endif // defined(__GNUC__) && (__GNUC__ > 2) && ! defined(__INTEL_COMPILER)
+
+// Tell the compiler that cond is likely to hold
+#define FT_LIKELY(cond) FT_EXPECT(bool(cond), true)
+
+// Tell the compiler that cond is unlikely to hold
+#define FT_UNLIKELY(cond) FT_EXPECT(bool(cond), false)
+
+#include "toku_instrumentation.h"
+
#if defined(__cplusplus)
extern "C" {
#endif
@@ -240,28 +267,272 @@ void toku_os_full_write (int fd, const void *buf, size_t len) __attribute__((__v
// os_write returns 0 on success, otherwise an errno.
ssize_t toku_os_pwrite (int fd, const void *buf, size_t len, toku_off_t off) __attribute__((__visibility__("default")));
-int toku_os_write (int fd, const void *buf, size_t len) __attribute__((__visibility__("default")));
+int toku_os_write(int fd, const void *buf, size_t len)
+ __attribute__((__visibility__("default")));
// wrappers around file system calls
-FILE * toku_os_fdopen(int fildes, const char *mode);
-FILE * toku_os_fopen(const char *filename, const char *mode);
-int toku_os_open(const char *path, int oflag, int mode);
-int toku_os_open_direct(const char *path, int oflag, int mode);
-int toku_os_close(int fd);
-int toku_os_fclose(FILE * stream);
-int toku_os_rename(const char *old_name, const char *new_name);
-int toku_os_unlink(const char *path);
-ssize_t toku_os_read(int fd, void *buf, size_t count);
-ssize_t toku_os_pread(int fd, void *buf, size_t count, off_t offset);
void toku_os_recursive_delete(const char *path);
+TOKU_FILE *toku_os_fdopen_with_source_location(int fildes,
+ const char *mode,
+ const char *filename,
+ const toku_instr_key &instr_key,
+ const char *src_file,
+ uint src_line);
+#define toku_os_fdopen(FD, M, FN, K) \
+ toku_os_fdopen_with_source_location(FD, M, FN, K, __FILE__, __LINE__)
+
+TOKU_FILE *toku_os_fopen_with_source_location(const char *filename,
+ const char *mode,
+ const toku_instr_key &instr_key,
+ const char *src_file,
+ uint src_line);
+#define toku_os_fopen(F, M, K) \
+ toku_os_fopen_with_source_location(F, M, K, __FILE__, __LINE__)
+
+int toku_os_open_with_source_location(const char *path,
+ int oflag,
+ int mode,
+ const toku_instr_key &instr_key,
+ const char *src_file,
+ uint src_line);
+#define toku_os_open(FD, F, M, K) \
+ toku_os_open_with_source_location(FD, F, M, K, __FILE__, __LINE__)
+
+int toku_os_open_direct(const char *path,
+ int oflag,
+ int mode,
+ const toku_instr_key &instr_key);
+
+int toku_os_delete_with_source_location(const char *name,
+ const char *src_file,
+ uint src_line);
+#define toku_os_delete(FN) \
+ toku_os_delete_with_source_location(FN, __FILE__, __LINE__)
+
+int toku_os_rename_with_source_location(const char *old_name,
+ const char *new_name,
+ const char *src_file,
+ uint src_line);
+#define toku_os_rename(old_name, new_name) \
+ toku_os_rename_with_source_location(old_name, new_name, __FILE__, __LINE__)
+
+void toku_os_full_write_with_source_location(int fd,
+ const void *buf,
+ size_t len,
+ const char *src_file,
+ uint src_line);
+#define toku_os_full_write(FD, B, L) \
+ toku_os_full_write_with_source_location(FD, B, L, __FILE__, __LINE__)
+
+int toku_os_write_with_source_location(int fd,
+ const void *buf,
+ size_t len,
+ const char *src_file,
+ uint src_line);
+#define toku_os_write(FD, B, L) \
+ toku_os_write_with_source_location(FD, B, L, __FILE__, __LINE__)
+
+void toku_os_full_pwrite_with_source_location(int fd,
+ const void *buf,
+ size_t len,
+ toku_off_t off,
+ const char *src_file,
+ uint src_line);
+#define toku_os_full_pwrite(FD, B, L, O) \
+ toku_os_full_pwrite_with_source_location(FD, B, L, O, __FILE__, __LINE__)
+
+ssize_t toku_os_pwrite_with_source_location(int fd,
+ const void *buf,
+ size_t len,
+ toku_off_t off,
+ const char *src_file,
+ uint src_line);
+
+#define toku_os_pwrite(FD, B, L, O) \
+ toku_os_pwrite_with_source_location(FD, B, L, O, __FILE__, __LINE__)
+
+int toku_os_fwrite_with_source_location(const void *ptr,
+ size_t size,
+ size_t nmemb,
+ TOKU_FILE *stream,
+ const char *src_file,
+ uint src_line);
+
+#define toku_os_fwrite(P, S, N, FS) \
+ toku_os_fwrite_with_source_location(P, S, N, FS, __FILE__, __LINE__)
+
+int toku_os_fread_with_source_location(void *ptr,
+ size_t size,
+ size_t nmemb,
+ TOKU_FILE *stream,
+ const char *src_file,
+ uint src_line);
+#define toku_os_fread(P, S, N, FS) \
+ toku_os_fread_with_source_location(P, S, N, FS, __FILE__, __LINE__)
+
+TOKU_FILE *toku_os_fopen_with_source_location(const char *filename,
+ const char *mode,
+ const toku_instr_key &instr_key,
+ const char *src_file,
+ uint src_line);
+
+int toku_os_fclose_with_source_location(TOKU_FILE *stream,
+ const char *src_file,
+ uint src_line);
+
+#define toku_os_fclose(FS) \
+ toku_os_fclose_with_source_location(FS, __FILE__, __LINE__)
+
+int toku_os_close_with_source_location(int fd,
+ const char *src_file,
+ uint src_line);
+#define toku_os_close(FD) \
+ toku_os_close_with_source_location(FD, __FILE__, __LINE__)
+
+ssize_t toku_os_read_with_source_location(int fd,
+ void *buf,
+ size_t count,
+ const char *src_file,
+ uint src_line);
+
+#define toku_os_read(FD, B, C) \
+ toku_os_read_with_source_location(FD, B, C, __FILE__, __LINE__);
+
+ssize_t inline_toku_os_pread_with_source_location(int fd,
+ void *buf,
+ size_t count,
+ off_t offset,
+ const char *src_file,
+ uint src_line);
+#define toku_os_pread(FD, B, C, O) \
+ inline_toku_os_pread_with_source_location(FD, B, C, O, __FILE__, __LINE__);
+
+void file_fsync_internal_with_source_location(int fd,
+ const char *src_file,
+ uint src_line);
+
+#define file_fsync_internal(FD) \
+ file_fsync_internal_with_source_location(FD, __FILE__, __LINE__);
+
+int toku_os_get_file_size_with_source_location(int fildes,
+ int64_t *fsize,
+ const char *src_file,
+ uint src_line);
+
+#define toku_os_get_file_size(D, S) \
+ toku_os_get_file_size_with_source_location(D, S, __FILE__, __LINE__)
+
+// TODO: should this prototype be moved to toku_os.h?
+int toku_stat_with_source_location(const char *name,
+ toku_struct_stat *buf,
+ const toku_instr_key &instr_key,
+ const char *src_file,
+ uint src_line)
+ __attribute__((__visibility__("default")));
+
+#define toku_stat(N, B, K) \
+ toku_stat_with_source_location(N, B, K, __FILE__, __LINE__)
+
+int toku_os_fstat_with_source_location(int fd,
+ toku_struct_stat *buf,
+ const char *src_file,
+ uint src_line)
+ __attribute__((__visibility__("default")));
+
+#define toku_os_fstat(FD, B) \
+ toku_os_fstat_with_source_location(FD, B, __FILE__, __LINE__)
+
+#ifdef HAVE_PSI_FILE_INTERFACE2
+int inline_toku_os_close(int fd, const char *src_file, uint src_line);
+int inline_toku_os_fclose(TOKU_FILE *stream,
+ const char *src_file,
+ uint src_line);
+ssize_t inline_toku_os_read(int fd,
+ void *buf,
+ size_t count,
+ const char *src_file,
+ uint src_line);
+ssize_t inline_toku_os_pread(int fd,
+ void *buf,
+ size_t count,
+ off_t offset,
+ const char *src_file,
+ uint src_line);
+int inline_toku_os_fwrite(const void *ptr,
+ size_t size,
+ size_t nmemb,
+ TOKU_FILE *stream,
+ const char *src_file,
+ uint src_line);
+int inline_toku_os_fread(void *ptr,
+ size_t size,
+ size_t nmemb,
+ TOKU_FILE *stream,
+ const char *src_file,
+ uint src_line);
+int inline_toku_os_write(int fd,
+ const void *buf,
+ size_t len,
+ const char *src_file,
+ uint src_line);
+ssize_t inline_toku_os_pwrite(int fd,
+ const void *buf,
+ size_t len,
+ toku_off_t off,
+ const char *src_file,
+ uint src_line);
+void inline_toku_os_full_write(int fd,
+ const void *buf,
+ size_t len,
+ const char *src_file,
+ uint src_line);
+void inline_toku_os_full_pwrite(int fd,
+ const void *buf,
+ size_t len,
+ toku_off_t off,
+ const char *src_file,
+ uint src_line);
+int inline_toku_os_delete(const char *name,
+ const char *srv_file,
+ uint src_line);
+//#else
+int inline_toku_os_close(int fd);
+int inline_toku_os_fclose(TOKU_FILE *stream);
+ssize_t inline_toku_os_read(int fd, void *buf, size_t count);
+ssize_t inline_toku_os_pread(int fd, void *buf, size_t count, off_t offset);
+int inline_toku_os_fwrite(const void *ptr,
+ size_t size,
+ size_t nmemb,
+ TOKU_FILE *stream);
+int inline_toku_os_fread(void *ptr,
+ size_t size,
+ size_t nmemb,
+ TOKU_FILE *stream);
+int inline_toku_os_write(int fd, const void *buf, size_t len);
+ssize_t inline_toku_os_pwrite(int fd,
+ const void *buf,
+ size_t len,
+ toku_off_t off);
+void inline_toku_os_full_write(int fd, const void *buf, size_t len);
+void inline_toku_os_full_pwrite(int fd,
+ const void *buf,
+ size_t len,
+ toku_off_t off);
+int inline_toku_os_delete(const char *name);
+#endif
+
// wrapper around fsync
-void toku_file_fsync_without_accounting(int fd);
void toku_file_fsync(int fd);
int toku_fsync_directory(const char *fname);
+void toku_file_fsync_without_accounting(int fd);
// get the number of fsync calls and the fsync times (total)
-void toku_get_fsync_times(uint64_t *fsync_count, uint64_t *fsync_time, uint64_t *long_fsync_threshold, uint64_t *long_fsync_count, uint64_t *long_fsync_time);
+void toku_get_fsync_times(uint64_t *fsync_count,
+ uint64_t *fsync_time,
+ uint64_t *long_fsync_threshold,
+ uint64_t *long_fsync_count,
+ uint64_t *long_fsync_time);
void toku_set_func_fsync (int (*fsync_function)(int));
void toku_set_func_pwrite (ssize_t (*)(int, const void *, size_t, toku_off_t));
@@ -271,9 +542,11 @@ void toku_set_func_full_write (ssize_t (*)(int, const void *, size_t));
void toku_set_func_fdopen (FILE * (*)(int, const char *));
void toku_set_func_fopen (FILE * (*)(const char *, const char *));
void toku_set_func_open (int (*)(const char *, int, int));
-void toku_set_func_fclose(int (*)(FILE*));
+void toku_set_func_fclose(int (*)(FILE *));
void toku_set_func_read(ssize_t (*)(int, void *, size_t));
-void toku_set_func_pread (ssize_t (*)(int, void *, size_t, off_t));
+void toku_set_func_pread(ssize_t (*)(int, void *, size_t, off_t));
+void toku_set_func_fwrite(
+ size_t (*fwrite_fun)(const void *, size_t, size_t, FILE *));
int toku_portability_init(void);
void toku_portability_destroy(void);
@@ -283,28 +556,3 @@ void toku_portability_destroy(void);
static inline uint64_t roundup_to_multiple(uint64_t alignment, uint64_t v) {
return (v + alignment - 1) & ~(alignment - 1);
}
-
-// Branch prediction macros.
-// If supported by the compiler, will hint in inctruction caching for likely
-// branching. Should only be used where there is a very good idea of the correct
-// branch heuristics as determined by profiling. Mostly copied from InnoDB.
-// Use:
-// "if (FT_LIKELY(x))" where the chances of "x" evaluating true are higher
-// "if (FT_UNLIKELY(x))" where the chances of "x" evaluating false are higher
-#if defined(__GNUC__) && (__GNUC__ > 2) && ! defined(__INTEL_COMPILER)
-
-// Tell the compiler that 'expr' probably evaluates to 'constant'.
-#define FT_EXPECT(expr,constant) __builtin_expect(expr, constant)
-
-#else
-
-#warning "No FT branch prediction operations in use!"
-#define FT_EXPECT(expr,constant) (expr)
-
-#endif // defined(__GNUC__) && (__GNUC__ > 2) && ! defined(__INTEL_COMPILER)
-
-// Tell the compiler that cond is likely to hold
-#define FT_LIKELY(cond) FT_EXPECT(cond, 1)
-
-// Tell the compiler that cond is unlikely to hold
-#define FT_UNLIKELY(cond) FT_EXPECT(cond, 0)
diff --git a/storage/tokudb/PerconaFT/portability/toku_pthread.h b/storage/tokudb/PerconaFT/portability/toku_pthread.h
index 84c27736201..44de01244d2 100644
--- a/storage/tokudb/PerconaFT/portability/toku_pthread.h
+++ b/storage/tokudb/PerconaFT/portability/toku_pthread.h
@@ -42,31 +42,62 @@ Copyright (c) 2006, 2015, Percona and/or its affiliates. All rights reserved.
#include <time.h>
#include <stdint.h>
+#include "toku_portability.h"
#include "toku_assert.h"
+// TODO: some things moved toku_instrumentation.h, not necessarily the best
+// place
typedef pthread_attr_t toku_pthread_attr_t;
typedef pthread_t toku_pthread_t;
-typedef pthread_mutexattr_t toku_pthread_mutexattr_t;
typedef pthread_mutex_t toku_pthread_mutex_t;
typedef pthread_condattr_t toku_pthread_condattr_t;
typedef pthread_cond_t toku_pthread_cond_t;
-typedef pthread_rwlock_t toku_pthread_rwlock_t;
-typedef pthread_rwlockattr_t toku_pthread_rwlockattr_t;
+typedef pthread_rwlockattr_t toku_pthread_rwlockattr_t;
typedef pthread_key_t toku_pthread_key_t;
typedef struct timespec toku_timespec_t;
-#ifndef TOKU_PTHREAD_DEBUG
-# define TOKU_PTHREAD_DEBUG 0
-#endif
+// TODO: break this include loop
+#include <pthread.h>
+typedef pthread_mutexattr_t toku_pthread_mutexattr_t;
-typedef struct toku_mutex {
+struct toku_mutex_t {
pthread_mutex_t pmutex;
+ struct PSI_mutex
+ *psi_mutex; /* The performance schema instrumentation hook */
#if TOKU_PTHREAD_DEBUG
- pthread_t owner; // = pthread_self(); // for debugging
+ pthread_t owner; // = pthread_self(); // for debugging
bool locked;
bool valid;
+ pfs_key_t instr_key_id;
+#endif
+};
+
+struct toku_cond_t {
+ pthread_cond_t pcond;
+ struct PSI_cond *psi_cond;
+#if TOKU_PTHREAD_DEBUG
+ pfs_key_t instr_key_id;
+#endif
+};
+
+#ifdef TOKU_PTHREAD_DEBUG
+#define TOKU_COND_INITIALIZER \
+ { \
+ .pcond = PTHREAD_COND_INITIALIZER, .psi_cond = nullptr, \
+ .instr_key_id = 0 \
+ }
+#else
+#define TOKU_COND_INITIALIZER \
+ { .pcond = PTHREAD_COND_INITIALIZER, .psi_cond = nullptr }
#endif
-} toku_mutex_t;
+
+struct toku_pthread_rwlock_t {
+ pthread_rwlock_t rwlock;
+ struct PSI_rwlock *psi_rwlock;
+#if TOKU_PTHREAD_DEBUG
+ pfs_key_t instr_key_id;
+#endif
+};
typedef struct toku_mutex_aligned {
toku_mutex_t aligned_mutex __attribute__((__aligned__(64)));
@@ -83,45 +114,68 @@ typedef struct toku_mutex_aligned {
// In general it will be a lot of busy work to make this codebase compile
// cleanly with -Wmissing-field-initializers
-# define ZERO_MUTEX_INITIALIZER {}
+#define ZERO_MUTEX_INITIALIZER \
+ {}
#if TOKU_PTHREAD_DEBUG
-# define TOKU_MUTEX_INITIALIZER { .pmutex = PTHREAD_MUTEX_INITIALIZER, .owner = 0, .locked = false, .valid = true }
+#define TOKU_MUTEX_INITIALIZER \
+ { \
+ .pmutex = PTHREAD_MUTEX_INITIALIZER, .psi_mutex = nullptr, .owner = 0, \
+ .locked = false, .valid = true, .instr_key_id = 0 \
+ }
#else
-# define TOKU_MUTEX_INITIALIZER { .pmutex = PTHREAD_MUTEX_INITIALIZER }
+#define TOKU_MUTEX_INITIALIZER \
+ { .pmutex = PTHREAD_MUTEX_INITIALIZER, .psi_mutex = nullptr }
#endif
// Darwin doesn't provide adaptive mutexes
#if defined(__APPLE__)
-# define TOKU_MUTEX_ADAPTIVE PTHREAD_MUTEX_DEFAULT
-# if TOKU_PTHREAD_DEBUG
-# define TOKU_ADAPTIVE_MUTEX_INITIALIZER { .pmutex = PTHREAD_MUTEX_INITIALIZER, .owner = 0, .locked = false, .valid = true }
-# else
-# define TOKU_ADAPTIVE_MUTEX_INITIALIZER { .pmutex = PTHREAD_MUTEX_INITIALIZER }
-# endif
-#else // __FreeBSD__, __linux__, at least
-# define TOKU_MUTEX_ADAPTIVE PTHREAD_MUTEX_ADAPTIVE_NP
-# if TOKU_PTHREAD_DEBUG
-# define TOKU_ADAPTIVE_MUTEX_INITIALIZER { .pmutex = PTHREAD_ADAPTIVE_MUTEX_INITIALIZER_NP, .owner = 0, .locked = false, .valid = true }
-# else
-# define TOKU_ADAPTIVE_MUTEX_INITIALIZER { .pmutex = PTHREAD_ADAPTIVE_MUTEX_INITIALIZER_NP }
-# endif
+#define TOKU_MUTEX_ADAPTIVE PTHREAD_MUTEX_DEFAULT
+#if TOKU_PTHREAD_DEBUG
+#define TOKU_ADAPTIVE_MUTEX_INITIALIZER \
+ { \
+ .pmutex = PTHREAD_MUTEX_INITIALIZER, .psi_mutex = nullptr, .owner = 0, \
+ .locked = false, .valid = true, .instr_key_id = 0 \
+ }
+#else
+#define TOKU_ADAPTIVE_MUTEX_INITIALIZER \
+ { .pmutex = PTHREAD_MUTEX_INITIALIZER, .psi_mutex = nullptr }
#endif
-
-static inline void
-toku_mutex_init(toku_mutex_t *mutex, const toku_pthread_mutexattr_t *attr) {
- int r = pthread_mutex_init(&mutex->pmutex, attr);
- assert_zero(r);
+#else // __FreeBSD__, __linux__, at least
+#define TOKU_MUTEX_ADAPTIVE PTHREAD_MUTEX_ADAPTIVE_NP
#if TOKU_PTHREAD_DEBUG
- mutex->locked = false;
- invariant(!mutex->valid);
- mutex->valid = true;
- mutex->owner = 0;
+#define TOKU_ADAPTIVE_MUTEX_INITIALIZER \
+ { \
+ .pmutex = PTHREAD_ADAPTIVE_MUTEX_INITIALIZER_NP, .psi_mutex = nullptr, \
+ .owner = 0, .locked = false, .valid = true, .instr_key_id = 0 \
+ }
+#else
+#define TOKU_ADAPTIVE_MUTEX_INITIALIZER \
+ { .pmutex = PTHREAD_ADAPTIVE_MUTEX_INITIALIZER_NP, .psi_mutex = nullptr }
+#endif
#endif
-}
-static inline void
-toku_mutexattr_init(toku_pthread_mutexattr_t *attr) {
+// Different OSes implement mutexes as different amounts of nested structs.
+// C++ will fill out all missing values with zeroes if you provide at least one
+// zero, but it needs the right amount of nesting.
+#if defined(__FreeBSD__)
+#define ZERO_COND_INITIALIZER \
+ { 0 }
+#elif defined(__APPLE__)
+#define ZERO_COND_INITIALIZER \
+ { \
+ { 0 } \
+ }
+#else // __linux__, at least
+#define ZERO_COND_INITIALIZER \
+ { \
+ { \
+ { 0 } \
+ } \
+ }
+#endif
+
+static inline void toku_mutexattr_init(toku_pthread_mutexattr_t *attr) {
int r = pthread_mutexattr_init(attr);
assert_zero(r);
}
@@ -138,61 +192,8 @@ toku_mutexattr_destroy(toku_pthread_mutexattr_t *attr) {
assert_zero(r);
}
-static inline void
-toku_mutex_destroy(toku_mutex_t *mutex) {
#if TOKU_PTHREAD_DEBUG
- invariant(mutex->valid);
- mutex->valid = false;
- invariant(!mutex->locked);
-#endif
- int r = pthread_mutex_destroy(&mutex->pmutex);
- assert_zero(r);
-}
-
-static inline void
-toku_mutex_lock(toku_mutex_t *mutex) {
- int r = pthread_mutex_lock(&mutex->pmutex);
- assert_zero(r);
-#if TOKU_PTHREAD_DEBUG
- invariant(mutex->valid);
- invariant(!mutex->locked);
- invariant(mutex->owner == 0);
- mutex->locked = true;
- mutex->owner = pthread_self();
-#endif
-}
-
-static inline int
-toku_mutex_trylock(toku_mutex_t *mutex) {
- int r = pthread_mutex_trylock(&mutex->pmutex);
-#if TOKU_PTHREAD_DEBUG
- if (r == 0) {
- invariant(mutex->valid);
- invariant(!mutex->locked);
- invariant(mutex->owner == 0);
- mutex->locked = true;
- mutex->owner = pthread_self();
- }
-#endif
- return r;
-}
-
-static inline void
-toku_mutex_unlock(toku_mutex_t *mutex) {
-#if TOKU_PTHREAD_DEBUG
- invariant(mutex->owner == pthread_self());
- invariant(mutex->valid);
- invariant(mutex->locked);
- mutex->locked = false;
- mutex->owner = 0;
-#endif
- int r = pthread_mutex_unlock(&mutex->pmutex);
- assert_zero(r);
-}
-
-#if TOKU_PTHREAD_DEBUG
-static inline void
-toku_mutex_assert_locked(const toku_mutex_t *mutex) {
+static inline void toku_mutex_assert_locked(const toku_mutex_t *mutex) {
invariant(mutex->locked);
invariant(mutex->owner == pthread_self());
}
@@ -217,40 +218,123 @@ toku_mutex_assert_unlocked(toku_mutex_t *mutex) {
invariant(!mutex->locked);
}
#else
-static inline void
-toku_mutex_assert_unlocked(toku_mutex_t *mutex __attribute__((unused))) {
+static inline void toku_mutex_assert_unlocked(toku_mutex_t *mutex
+ __attribute__((unused))) {}
+#endif
+
+#define toku_mutex_lock(M) \
+ toku_mutex_lock_with_source_location(M, __FILE__, __LINE__)
+
+static inline void toku_cond_init(toku_cond_t *cond,
+ const toku_pthread_condattr_t *attr) {
+ int r = pthread_cond_init(&cond->pcond, attr);
+ assert_zero(r);
+}
+
+#define toku_mutex_trylock(M) \
+ toku_mutex_trylock_with_source_location(M, __FILE__, __LINE__)
+
+inline void toku_mutex_unlock(toku_mutex_t *mutex) {
+#if TOKU_PTHREAD_DEBUG
+ invariant(mutex->owner == pthread_self());
+ invariant(mutex->valid);
+ invariant(mutex->locked);
+ mutex->locked = false;
+ mutex->owner = 0;
+#endif
+ toku_instr_mutex_unlock(mutex->psi_mutex);
+ int r = pthread_mutex_unlock(&mutex->pmutex);
+ assert_zero(r);
}
+
+inline void toku_mutex_lock_with_source_location(toku_mutex_t *mutex,
+ const char *src_file,
+ int src_line) {
+
+ toku_mutex_instrumentation mutex_instr;
+ toku_instr_mutex_lock_start(mutex_instr, *mutex, src_file, src_line);
+
+ const int r = pthread_mutex_lock(&mutex->pmutex);
+ toku_instr_mutex_lock_end(mutex_instr, r);
+
+ assert_zero(r);
+#if TOKU_PTHREAD_DEBUG
+ invariant(mutex->valid);
+ invariant(!mutex->locked);
+ invariant(mutex->owner == 0);
+ mutex->locked = true;
+ mutex->owner = pthread_self();
#endif
+}
-typedef struct toku_cond {
- pthread_cond_t pcond;
-} toku_cond_t;
+inline int toku_mutex_trylock_with_source_location(toku_mutex_t *mutex,
+ const char *src_file,
+ int src_line) {
-// Same considerations as for ZERO_MUTEX_INITIALIZER apply
-#define ZERO_COND_INITIALIZER {}
+ toku_mutex_instrumentation mutex_instr;
+ toku_instr_mutex_trylock_start(mutex_instr, *mutex, src_file, src_line);
-#define TOKU_COND_INITIALIZER {PTHREAD_COND_INITIALIZER}
+ const int r = pthread_mutex_lock(&mutex->pmutex);
+ toku_instr_mutex_lock_end(mutex_instr, r);
-static inline void
-toku_cond_init(toku_cond_t *cond, const toku_pthread_condattr_t *attr) {
+#if TOKU_PTHREAD_DEBUG
+ if (r == 0) {
+ invariant(mutex->valid);
+ invariant(!mutex->locked);
+ invariant(mutex->owner == 0);
+ mutex->locked = true;
+ mutex->owner = pthread_self();
+ }
+#endif
+ return r;
+}
+
+#define toku_cond_wait(C, M) \
+ toku_cond_wait_with_source_location(C, M, __FILE__, __LINE__)
+
+#define toku_cond_timedwait(C, M, W) \
+ toku_cond_timedwait_with_source_location(C, M, W, __FILE__, __LINE__)
+
+inline void toku_cond_init(const toku_instr_key &key,
+ toku_cond_t *cond,
+ const pthread_condattr_t *attr) {
+ toku_instr_cond_init(key, *cond);
int r = pthread_cond_init(&cond->pcond, attr);
assert_zero(r);
}
-static inline void
-toku_cond_destroy(toku_cond_t *cond) {
+inline void toku_cond_destroy(toku_cond_t *cond) {
+ toku_instr_cond_destroy(cond->psi_cond);
int r = pthread_cond_destroy(&cond->pcond);
assert_zero(r);
}
-static inline void
-toku_cond_wait(toku_cond_t *cond, toku_mutex_t *mutex) {
+inline void toku_cond_wait_with_source_location(toku_cond_t *cond,
+ toku_mutex_t *mutex,
+ const char *src_file,
+ uint src_line) {
+
#if TOKU_PTHREAD_DEBUG
invariant(mutex->locked);
mutex->locked = false;
mutex->owner = 0;
#endif
- int r = pthread_cond_wait(&cond->pcond, &mutex->pmutex);
+
+ /* Instrumentation start */
+ toku_cond_instrumentation cond_instr;
+ toku_instr_cond_wait_start(cond_instr,
+ toku_instr_cond_op::cond_wait,
+ *cond,
+ *mutex,
+ src_file,
+ src_line);
+
+ /* Instrumented code */
+ const int r = pthread_cond_wait(&cond->pcond, &mutex->pmutex);
+
+ /* Instrumentation end */
+ toku_instr_cond_wait_end(cond_instr, r);
+
assert_zero(r);
#if TOKU_PTHREAD_DEBUG
invariant(!mutex->locked);
@@ -259,14 +343,33 @@ toku_cond_wait(toku_cond_t *cond, toku_mutex_t *mutex) {
#endif
}
-static inline int
-toku_cond_timedwait(toku_cond_t *cond, toku_mutex_t *mutex, toku_timespec_t *wakeup_at) {
+inline int toku_cond_timedwait_with_source_location(toku_cond_t *cond,
+ toku_mutex_t *mutex,
+ toku_timespec_t *wakeup_at,
+ const char *src_file,
+ uint src_line) {
#if TOKU_PTHREAD_DEBUG
invariant(mutex->locked);
mutex->locked = false;
mutex->owner = 0;
#endif
- int r = pthread_cond_timedwait(&cond->pcond, &mutex->pmutex, wakeup_at);
+
+ /* Instrumentation start */
+ toku_cond_instrumentation cond_instr;
+ toku_instr_cond_wait_start(cond_instr,
+ toku_instr_cond_op::cond_timedwait,
+ *cond,
+ *mutex,
+ src_file,
+ src_line);
+
+ /* Instrumented code */
+ const int r = pthread_cond_timedwait(
+ &cond->pcond, &mutex->pmutex, wakeup_at);
+
+ /* Instrumentation end */
+ toku_instr_cond_wait_end(cond_instr, r);
+
#if TOKU_PTHREAD_DEBUG
invariant(!mutex->locked);
mutex->locked = true;
@@ -275,69 +378,116 @@ toku_cond_timedwait(toku_cond_t *cond, toku_mutex_t *mutex, toku_timespec_t *wak
return r;
}
-static inline void
-toku_cond_signal(toku_cond_t *cond) {
- int r = pthread_cond_signal(&cond->pcond);
+inline void toku_cond_signal(toku_cond_t *cond) {
+ toku_instr_cond_signal(*cond);
+ const int r = pthread_cond_signal(&cond->pcond);
assert_zero(r);
}
-static inline void
-toku_cond_broadcast(toku_cond_t *cond) {
- int r =pthread_cond_broadcast(&cond->pcond);
+inline void toku_cond_broadcast(toku_cond_t *cond) {
+ toku_instr_cond_broadcast(*cond);
+ const int r = pthread_cond_broadcast(&cond->pcond);
assert_zero(r);
}
-int
-toku_pthread_yield(void) __attribute__((__visibility__("default")));
-
-static inline toku_pthread_t
-toku_pthread_self(void) {
- return pthread_self();
+inline void toku_mutex_init(const toku_instr_key &key,
+ toku_mutex_t *mutex,
+ const toku_pthread_mutexattr_t *attr) {
+#if TOKU_PTHREAD_DEBUG
+ mutex->valid = true;
+#endif
+ toku_instr_mutex_init(key, *mutex);
+ const int r = pthread_mutex_init(&mutex->pmutex, attr);
+ assert_zero(r);
+#if TOKU_PTHREAD_DEBUG
+ mutex->locked = false;
+ invariant(mutex->valid);
+ mutex->valid = true;
+ mutex->owner = 0;
+#endif
}
-static inline void
-toku_pthread_rwlock_init(toku_pthread_rwlock_t *__restrict rwlock, const toku_pthread_rwlockattr_t *__restrict attr) {
- int r = pthread_rwlock_init(rwlock, attr);
+inline void toku_mutex_destroy(toku_mutex_t *mutex) {
+#if TOKU_PTHREAD_DEBUG
+ invariant(mutex->valid);
+ mutex->valid = false;
+ invariant(!mutex->locked);
+#endif
+ toku_instr_mutex_destroy(mutex->psi_mutex);
+ int r = pthread_mutex_destroy(&mutex->pmutex);
assert_zero(r);
}
-static inline void
-toku_pthread_rwlock_destroy(toku_pthread_rwlock_t *rwlock) {
- int r = pthread_rwlock_destroy(rwlock);
+#define toku_pthread_rwlock_rdlock(RW) \
+ toku_pthread_rwlock_rdlock_with_source_location(RW, __FILE__, __LINE__)
+
+#define toku_pthread_rwlock_wrlock(RW) \
+ toku_pthread_rwlock_wrlock_with_source_location(RW, __FILE__, __LINE__)
+
+inline void toku_pthread_rwlock_init(
+ const toku_instr_key &key,
+ toku_pthread_rwlock_t *__restrict rwlock,
+ const toku_pthread_rwlockattr_t *__restrict attr) {
+ toku_instr_rwlock_init(key, *rwlock);
+ int r = pthread_rwlock_init(&rwlock->rwlock, attr);
assert_zero(r);
}
-static inline void
-toku_pthread_rwlock_rdlock(toku_pthread_rwlock_t *rwlock) {
- int r = pthread_rwlock_rdlock(rwlock);
+inline void toku_pthread_rwlock_destroy(toku_pthread_rwlock_t *rwlock) {
+ toku_instr_rwlock_destroy(rwlock->psi_rwlock);
+ int r = pthread_rwlock_destroy(&rwlock->rwlock);
assert_zero(r);
}
-static inline void
-toku_pthread_rwlock_rdunlock(toku_pthread_rwlock_t *rwlock) {
- int r = pthread_rwlock_unlock(rwlock);
+inline void toku_pthread_rwlock_rdlock_with_source_location(
+ toku_pthread_rwlock_t *rwlock,
+ const char *src_file,
+ uint src_line) {
+
+ /* Instrumentation start */
+ toku_rwlock_instrumentation rwlock_instr;
+ toku_instr_rwlock_rdlock_wait_start(
+ rwlock_instr, *rwlock, src_file, src_line);
+ /* Instrumented code */
+ const int r = pthread_rwlock_rdlock(&rwlock->rwlock);
+
+ /* Instrumentation end */
+ toku_instr_rwlock_rdlock_wait_end(rwlock_instr, r);
+
assert_zero(r);
}
-static inline void
-toku_pthread_rwlock_wrlock(toku_pthread_rwlock_t *rwlock) {
- int r = pthread_rwlock_wrlock(rwlock);
+inline void toku_pthread_rwlock_wrlock_with_source_location(
+ toku_pthread_rwlock_t *rwlock,
+ const char *src_file,
+ uint src_line) {
+
+ /* Instrumentation start */
+ toku_rwlock_instrumentation rwlock_instr;
+ toku_instr_rwlock_wrlock_wait_start(
+ rwlock_instr, *rwlock, src_file, src_line);
+ /* Instrumented code */
+ const int r = pthread_rwlock_wrlock(&rwlock->rwlock);
+
+ /* Instrumentation end */
+ toku_instr_rwlock_wrlock_wait_end(rwlock_instr, r);
+
assert_zero(r);
}
-static inline void
-toku_pthread_rwlock_wrunlock(toku_pthread_rwlock_t *rwlock) {
- int r = pthread_rwlock_unlock(rwlock);
+inline void toku_pthread_rwlock_rdunlock(toku_pthread_rwlock_t *rwlock) {
+ toku_instr_rwlock_unlock(*rwlock);
+ const int r = pthread_rwlock_unlock(&rwlock->rwlock);
assert_zero(r);
}
-static inline int
-toku_pthread_create(toku_pthread_t *thread, const toku_pthread_attr_t *attr, void *(*start_function)(void *), void *arg) {
- return pthread_create(thread, attr, start_function, arg);
+inline void toku_pthread_rwlock_wrunlock(toku_pthread_rwlock_t *rwlock) {
+ toku_instr_rwlock_unlock(*rwlock);
+ const int r = pthread_rwlock_unlock(&rwlock->rwlock);
+ assert_zero(r);
}
-static inline int
-toku_pthread_join(toku_pthread_t thread, void **value_ptr) {
+static inline int toku_pthread_join(toku_pthread_t thread, void **value_ptr) {
return pthread_join(thread, value_ptr);
}
@@ -361,7 +511,15 @@ toku_pthread_getspecific(toku_pthread_key_t key) {
return pthread_getspecific(key);
}
-static inline int
-toku_pthread_setspecific(toku_pthread_key_t key, void *data) {
+static inline int toku_pthread_setspecific(toku_pthread_key_t key, void *data) {
return pthread_setspecific(key, data);
}
+
+int toku_pthread_yield(void) __attribute__((__visibility__("default")));
+
+static inline toku_pthread_t toku_pthread_self(void) { return pthread_self(); }
+
+static inline void *toku_pthread_done(void *exit_value) {
+ toku_instr_delete_current_thread();
+ pthread_exit(exit_value);
+}