Fixed a tsan warning in db_stress.cc

Summary:
Fixed the following tsan warning in db_stress.cc

  WARNING: ThreadSanitizer: data race (pid=3163194)
  Read of size 8 at 0x7fd1797cb518 by thread T32:
    #0 VerifyDb tools/db_stress.cc:1731 (db_stress+0x000000040674)
    #1 rocksdb::StressTest::ThreadBody(void*) tools/db_stress.cc:1191 (db_stress+0x0000000625a9)
    #2 StartThreadWrapper util/env_posix.cc:1648 (db_stress+0x00000028bbbd)

  Previous write of size 8 at 0x7fd1797cb518 by thread T31:
    #0 VerifyDb tools/db_stress.cc:1726 (db_stress+0x00000004072a)
    #1 rocksdb::StressTest::ThreadBody(void*) tools/db_stress.cc:1191 (db_stress+0x0000000625a9)
    #2 StartThreadWrapper util/env_posix.cc:1648 (db_stress+0x00000028bbbd)

The cause is that in VerifyDb(), the static local const variable long max_key
can be read and written at the same time.  This patch fixed it by making it
non-static.

Test Plan: db_stress

Reviewers: igor, sdong, IslamAbdelRahman, anthony

Reviewed By: anthony

Subscribers: dhruba, leveldb

Differential Revision: https://reviews.facebook.net/D47703
This commit is contained in:
Yueh-Hsuan Chiang 2015-09-28 12:06:43 -07:00
parent e4861e7d68
commit a263002a36

View File

@ -1723,10 +1723,10 @@ class StressTest {
void VerifyDb(ThreadState* thread) const { void VerifyDb(ThreadState* thread) const {
ReadOptions options(FLAGS_verify_checksum, true); ReadOptions options(FLAGS_verify_checksum, true);
auto shared = thread->shared; auto shared = thread->shared;
static const long max_key = shared->GetMaxKey(); const int64_t max_key = shared->GetMaxKey();
static const long keys_per_thread = max_key / shared->GetNumThreads(); const int64_t keys_per_thread = max_key / shared->GetNumThreads();
long start = keys_per_thread * thread->tid; int64_t start = keys_per_thread * thread->tid;
long end = start + keys_per_thread; int64_t end = start + keys_per_thread;
if (thread->tid == shared->GetNumThreads() - 1) { if (thread->tid == shared->GetNumThreads() - 1) {
end = max_key; end = max_key;
} }