2015-07-02 01:13:49 +02:00
|
|
|
// Copyright (c) 2013, Facebook, Inc. All rights reserved.
|
|
|
|
// This source code is licensed under the BSD-style license found in the
|
|
|
|
// LICENSE file in the root directory of this source tree. An additional grant
|
|
|
|
// of patent rights can be found in the PATENTS file in the same directory.
|
|
|
|
//
|
|
|
|
// Copyright (c) 2011 The LevelDB Authors. All rights reserved.
|
|
|
|
// Use of this source code is governed by a BSD-style license that can be
|
|
|
|
// found in the LICENSE file. See the AUTHORS file for names of contributors.
|
|
|
|
|
|
|
|
#if !defined(OS_WIN) && !defined(WIN32) && !defined(_WIN32)
|
|
|
|
#error Windows Specific Code
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#include "port/win/port_win.h"
|
|
|
|
|
|
|
|
#include <io.h>
|
|
|
|
#include "port/dirent.h"
|
|
|
|
#include "port/sys_time.h"
|
|
|
|
|
|
|
|
#include <cstdlib>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <assert.h>
|
|
|
|
#include <string.h>
|
|
|
|
|
|
|
|
#include <memory>
|
|
|
|
#include <exception>
|
|
|
|
#include <chrono>
|
|
|
|
|
|
|
|
#include "util/logging.h"
|
|
|
|
|
2015-07-13 21:11:05 +02:00
|
|
|
namespace rocksdb {
|
|
|
|
namespace port {
|
2015-07-02 01:13:49 +02:00
|
|
|
|
|
|
|
void gettimeofday(struct timeval* tv, struct timezone* /* tz */) {
|
2015-07-13 21:11:05 +02:00
|
|
|
using namespace std::chrono;
|
2015-07-02 01:13:49 +02:00
|
|
|
|
2015-07-13 21:11:05 +02:00
|
|
|
microseconds usNow(
|
|
|
|
duration_cast<microseconds>(system_clock::now().time_since_epoch()));
|
2015-07-02 01:13:49 +02:00
|
|
|
|
2015-07-13 21:11:05 +02:00
|
|
|
seconds secNow(duration_cast<seconds>(usNow));
|
2015-07-02 01:13:49 +02:00
|
|
|
|
2015-07-13 21:11:05 +02:00
|
|
|
tv->tv_sec = secNow.count();
|
|
|
|
tv->tv_usec = usNow.count() - duration_cast<microseconds>(secNow).count();
|
2015-07-02 01:13:49 +02:00
|
|
|
}
|
|
|
|
|
2015-07-13 21:11:05 +02:00
|
|
|
Mutex::Mutex(bool adaptive) : lock(m_mutex, std::defer_lock) {}
|
2015-07-02 01:13:49 +02:00
|
|
|
|
2015-07-13 21:11:05 +02:00
|
|
|
Mutex::~Mutex() {}
|
2015-07-02 01:13:49 +02:00
|
|
|
|
|
|
|
void Mutex::Lock() {
|
2015-07-13 21:11:05 +02:00
|
|
|
lock.lock();
|
2015-07-02 01:13:49 +02:00
|
|
|
#ifndef NDEBUG
|
2015-07-13 21:11:05 +02:00
|
|
|
locked_ = true;
|
2015-07-02 01:13:49 +02:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
void Mutex::Unlock() {
|
|
|
|
#ifndef NDEBUG
|
2015-07-13 21:11:05 +02:00
|
|
|
locked_ = false;
|
2015-07-02 01:13:49 +02:00
|
|
|
#endif
|
2015-07-13 21:11:05 +02:00
|
|
|
lock.unlock();
|
2015-07-02 01:13:49 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void Mutex::AssertHeld() {
|
|
|
|
#ifndef NDEBUG
|
2015-07-13 21:11:05 +02:00
|
|
|
assert(locked_);
|
2015-07-02 01:13:49 +02:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2015-07-13 21:11:05 +02:00
|
|
|
CondVar::CondVar(Mutex* mu) : mu_(mu) {}
|
2015-07-02 01:13:49 +02:00
|
|
|
|
2015-07-13 21:11:05 +02:00
|
|
|
CondVar::~CondVar() {}
|
2015-07-02 01:13:49 +02:00
|
|
|
|
|
|
|
void CondVar::Wait() {
|
|
|
|
#ifndef NDEBUG
|
2015-07-13 21:11:05 +02:00
|
|
|
mu_->locked_ = false;
|
2015-07-02 01:13:49 +02:00
|
|
|
#endif
|
2015-07-13 21:11:05 +02:00
|
|
|
cv_.wait(mu_->getLock());
|
2015-07-02 01:13:49 +02:00
|
|
|
#ifndef NDEBUG
|
2015-07-13 21:11:05 +02:00
|
|
|
mu_->locked_ = true;
|
2015-07-02 01:13:49 +02:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
bool CondVar::TimedWait(uint64_t abs_time_us) {
|
|
|
|
#ifndef NDEBUG
|
2015-07-13 21:11:05 +02:00
|
|
|
mu_->locked_ = false;
|
2015-07-02 01:13:49 +02:00
|
|
|
#endif
|
|
|
|
|
2015-07-13 21:11:05 +02:00
|
|
|
using namespace std::chrono;
|
2015-07-02 01:13:49 +02:00
|
|
|
|
2015-07-13 21:11:05 +02:00
|
|
|
microseconds usAbsTime(abs_time_us);
|
|
|
|
microseconds usNow(
|
|
|
|
duration_cast<microseconds>(system_clock::now().time_since_epoch()));
|
|
|
|
microseconds relTimeUs =
|
|
|
|
(usAbsTime > usNow) ? (usAbsTime - usNow) : microseconds::zero();
|
2015-07-02 01:13:49 +02:00
|
|
|
|
2015-07-13 21:11:05 +02:00
|
|
|
std::_Cv_status cvStatus = cv_.wait_for(mu_->getLock(), relTimeUs);
|
2015-07-02 01:13:49 +02:00
|
|
|
|
|
|
|
#ifndef NDEBUG
|
2015-07-13 21:11:05 +02:00
|
|
|
mu_->locked_ = true;
|
2015-07-02 01:13:49 +02:00
|
|
|
#endif
|
|
|
|
|
2015-07-13 21:11:05 +02:00
|
|
|
if (cvStatus == std::cv_status::timeout) {
|
|
|
|
return true;
|
|
|
|
}
|
2015-07-02 01:13:49 +02:00
|
|
|
|
2015-07-13 21:11:05 +02:00
|
|
|
return false;
|
2015-07-02 01:13:49 +02:00
|
|
|
}
|
|
|
|
|
2015-07-13 21:11:05 +02:00
|
|
|
void CondVar::Signal() { cv_.notify_one(); }
|
2015-07-02 01:13:49 +02:00
|
|
|
|
2015-07-13 21:11:05 +02:00
|
|
|
void CondVar::SignalAll() { cv_.notify_all(); }
|
2015-07-02 01:13:49 +02:00
|
|
|
|
|
|
|
void InitOnce(OnceType* once, void (*initializer)()) {
|
2015-07-13 21:11:05 +02:00
|
|
|
std::call_once(*once, initializer);
|
2015-07-02 01:13:49 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Private structure, exposed only by pointer
|
|
|
|
struct DIR {
|
2015-07-13 21:11:05 +02:00
|
|
|
intptr_t handle_;
|
|
|
|
bool firstread_;
|
|
|
|
struct __finddata64_t data_;
|
|
|
|
dirent entry_;
|
2015-07-02 01:13:49 +02:00
|
|
|
|
2015-07-13 21:11:05 +02:00
|
|
|
DIR() : handle_(-1), firstread_(true) {}
|
2015-07-02 01:13:49 +02:00
|
|
|
|
2015-07-13 21:11:05 +02:00
|
|
|
DIR(const DIR&) = delete;
|
|
|
|
DIR& operator=(const DIR&) = delete;
|
2015-07-02 01:13:49 +02:00
|
|
|
|
2015-07-13 21:11:05 +02:00
|
|
|
~DIR() {
|
|
|
|
if (-1 != handle_) {
|
|
|
|
_findclose(handle_);
|
2015-07-02 01:13:49 +02:00
|
|
|
}
|
2015-07-13 21:11:05 +02:00
|
|
|
}
|
2015-07-02 01:13:49 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
DIR* opendir(const char* name) {
|
2015-07-13 21:11:05 +02:00
|
|
|
if (!name || *name == 0) {
|
|
|
|
errno = ENOENT;
|
|
|
|
return nullptr;
|
|
|
|
}
|
2015-07-02 01:13:49 +02:00
|
|
|
|
2015-07-13 21:11:05 +02:00
|
|
|
std::string pattern(name);
|
|
|
|
pattern.append("\\").append("*");
|
2015-07-02 01:13:49 +02:00
|
|
|
|
2015-07-13 21:11:05 +02:00
|
|
|
std::unique_ptr<DIR> dir(new DIR);
|
2015-07-02 01:13:49 +02:00
|
|
|
|
2015-07-13 21:11:05 +02:00
|
|
|
dir->handle_ = _findfirst64(pattern.c_str(), &dir->data_);
|
2015-07-02 01:13:49 +02:00
|
|
|
|
2015-07-13 21:11:05 +02:00
|
|
|
if (dir->handle_ == -1) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
2015-07-02 01:13:49 +02:00
|
|
|
|
2015-07-13 21:11:05 +02:00
|
|
|
strncpy_s(dir->entry_.d_name, dir->data_.name, strlen(dir->data_.name));
|
2015-07-02 01:13:49 +02:00
|
|
|
|
2015-07-13 21:11:05 +02:00
|
|
|
return dir.release();
|
2015-07-02 01:13:49 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
struct dirent* readdir(DIR* dirp) {
|
2015-07-13 21:11:05 +02:00
|
|
|
if (!dirp || dirp->handle_ == -1) {
|
|
|
|
errno = EBADF;
|
|
|
|
return nullptr;
|
|
|
|
}
|
2015-07-02 01:13:49 +02:00
|
|
|
|
2015-07-13 21:11:05 +02:00
|
|
|
if (dirp->firstread_) {
|
|
|
|
dirp->firstread_ = false;
|
|
|
|
return &dirp->entry_;
|
|
|
|
}
|
2015-07-02 01:13:49 +02:00
|
|
|
|
2015-07-13 21:11:05 +02:00
|
|
|
auto ret = _findnext64(dirp->handle_, &dirp->data_);
|
2015-07-02 01:13:49 +02:00
|
|
|
|
2015-07-13 21:11:05 +02:00
|
|
|
if (ret != 0) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
2015-07-02 01:13:49 +02:00
|
|
|
|
2015-07-13 21:11:05 +02:00
|
|
|
strncpy_s(dirp->entry_.d_name, dirp->data_.name, strlen(dirp->data_.name));
|
2015-07-02 01:13:49 +02:00
|
|
|
|
2015-07-13 21:11:05 +02:00
|
|
|
return &dirp->entry_;
|
2015-07-02 01:13:49 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
int closedir(DIR* dirp) {
|
2015-07-13 21:11:05 +02:00
|
|
|
delete dirp;
|
|
|
|
return 0;
|
2015-07-02 01:13:49 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
int truncate(const char* path, int64_t len) {
|
|
|
|
if (path == nullptr) {
|
|
|
|
errno = EFAULT;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (len < 0) {
|
|
|
|
errno = EINVAL;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2015-07-13 21:11:05 +02:00
|
|
|
HANDLE hFile =
|
|
|
|
CreateFile(path, GENERIC_READ | GENERIC_WRITE,
|
|
|
|
FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE,
|
|
|
|
NULL, // Security attrs
|
|
|
|
OPEN_EXISTING, // Truncate existing file only
|
|
|
|
FILE_ATTRIBUTE_NORMAL, NULL);
|
2015-07-02 01:13:49 +02:00
|
|
|
|
|
|
|
if (INVALID_HANDLE_VALUE == hFile) {
|
|
|
|
auto lastError = GetLastError();
|
|
|
|
if (lastError == ERROR_FILE_NOT_FOUND) {
|
|
|
|
errno = ENOENT;
|
|
|
|
} else if (lastError == ERROR_ACCESS_DENIED) {
|
|
|
|
errno = EACCES;
|
|
|
|
} else {
|
|
|
|
errno = EIO;
|
|
|
|
}
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
int result = 0;
|
|
|
|
FILE_END_OF_FILE_INFO end_of_file;
|
|
|
|
end_of_file.EndOfFile.QuadPart = len;
|
|
|
|
|
2015-07-13 21:11:05 +02:00
|
|
|
if (!SetFileInformationByHandle(hFile, FileEndOfFileInfo, &end_of_file,
|
|
|
|
sizeof(FILE_END_OF_FILE_INFO))) {
|
2015-07-02 01:13:49 +02:00
|
|
|
errno = EIO;
|
|
|
|
result = -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
CloseHandle(hFile);
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2015-07-22 02:20:57 +02:00
|
|
|
void Crash(const std::string& srcfile, int srcline) {
|
|
|
|
fprintf(stdout, "Crashing at %s:%d\n", srcfile.c_str(), srcline);
|
|
|
|
fflush(stdout);
|
|
|
|
abort();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-07-02 01:13:49 +02:00
|
|
|
} // namespace port
|
|
|
|
} // namespace rocksdb
|
|
|
|
|
|
|
|
#ifdef JEMALLOC
|
|
|
|
|
|
|
|
#include "jemalloc/jemalloc.h"
|
|
|
|
|
|
|
|
namespace rocksdb {
|
|
|
|
|
|
|
|
namespace port {
|
|
|
|
|
2015-07-13 21:11:05 +02:00
|
|
|
__declspec(noinline) void WINAPI InitializeJemalloc() {
|
|
|
|
je_init();
|
|
|
|
atexit(je_uninit);
|
2015-07-02 01:13:49 +02:00
|
|
|
}
|
|
|
|
|
2015-07-13 21:11:05 +02:00
|
|
|
} // port
|
|
|
|
} // rocksdb
|
2015-07-02 01:13:49 +02:00
|
|
|
|
|
|
|
extern "C" {
|
|
|
|
|
|
|
|
#ifdef _WIN64
|
|
|
|
|
|
|
|
#pragma comment(linker, "/INCLUDE:p_rocksdb_init_jemalloc")
|
|
|
|
|
2015-07-13 21:11:05 +02:00
|
|
|
typedef void(WINAPI* CRT_Startup_Routine)(void);
|
2015-07-02 01:13:49 +02:00
|
|
|
|
|
|
|
// .CRT section is merged with .rdata on x64 so it must be constant data.
|
|
|
|
// must be of external linkage
|
2015-07-13 21:11:05 +02:00
|
|
|
// We put this into XCT since we want to run this earlier than C++ static
|
|
|
|
// constructors
|
2015-07-02 01:13:49 +02:00
|
|
|
// which are placed into XCU
|
|
|
|
#pragma const_seg(".CRT$XCT")
|
|
|
|
extern const CRT_Startup_Routine p_rocksdb_init_jemalloc;
|
2015-07-13 21:11:05 +02:00
|
|
|
const CRT_Startup_Routine p_rocksdb_init_jemalloc =
|
|
|
|
rocksdb::port::InitializeJemalloc;
|
2015-07-02 01:13:49 +02:00
|
|
|
#pragma const_seg()
|
|
|
|
|
2015-07-13 21:11:05 +02:00
|
|
|
#else // _WIN64
|
2015-07-02 01:13:49 +02:00
|
|
|
|
|
|
|
// x86 untested
|
|
|
|
|
|
|
|
#pragma comment(linker, "/INCLUDE:_p_rocksdb_init_jemalloc")
|
|
|
|
|
|
|
|
#pragma section(".CRT$XCT", read)
|
2015-07-13 21:11:05 +02:00
|
|
|
JEMALLOC_SECTION(".CRT$XCT") JEMALLOC_ATTR(used) static const void(
|
|
|
|
WINAPI* p_rocksdb_init_jemalloc)(void) = rocksdb::port::InitializeJemalloc;
|
2015-07-02 01:13:49 +02:00
|
|
|
|
2015-07-13 21:11:05 +02:00
|
|
|
#endif // _WIN64
|
2015-07-02 01:13:49 +02:00
|
|
|
|
2015-07-13 21:11:05 +02:00
|
|
|
} // extern "C"
|
2015-07-02 01:13:49 +02:00
|
|
|
|
|
|
|
// Global operators to be replaced by a linker
|
|
|
|
|
|
|
|
void* operator new(size_t size) {
|
|
|
|
void* p = je_malloc(size);
|
|
|
|
if (!p) {
|
2015-07-13 21:11:05 +02:00
|
|
|
throw std::bad_alloc();
|
2015-07-02 01:13:49 +02:00
|
|
|
}
|
|
|
|
return p;
|
|
|
|
}
|
|
|
|
|
|
|
|
void* operator new[](size_t size) {
|
|
|
|
void* p = je_malloc(size);
|
|
|
|
if (!p) {
|
|
|
|
throw std::bad_alloc();
|
|
|
|
}
|
|
|
|
return p;
|
|
|
|
}
|
|
|
|
|
2015-07-13 21:11:05 +02:00
|
|
|
void operator delete(void* p) { je_free(p); }
|
2015-07-02 01:13:49 +02:00
|
|
|
|
2015-07-13 21:11:05 +02:00
|
|
|
void operator delete[](void* p) { je_free(p); }
|
2015-07-02 01:13:49 +02:00
|
|
|
|
2015-07-13 21:11:05 +02:00
|
|
|
#endif // JEMALLOC
|