env_win.cc 45 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540
  1. // Copyright (c) 2011-present, Facebook, Inc. All rights reserved.
  2. // This source code is licensed under both the GPLv2 (found in the
  3. // COPYING file in the root directory) and Apache 2.0 License
  4. // (found in the LICENSE.Apache file in the root directory).
  5. //
  6. // Copyright (c) 2011 The LevelDB Authors. All rights reserved.
  7. // Use of this source code is governed by a BSD-style license that can be
  8. // found in the LICENSE file. See the AUTHORS file for names of contributors.
  9. #include "port/win/env_win.h"
  10. #include "port/win/win_thread.h"
  11. #include <algorithm>
  12. #include <ctime>
  13. #include <thread>
  14. #include <errno.h>
  15. #include <process.h> // _getpid
  16. #include <io.h> // _access
  17. #include <direct.h> // _rmdir, _mkdir, _getcwd
  18. #include <sys/types.h>
  19. #include <sys/stat.h>
  20. #include "rocksdb/env.h"
  21. #include "rocksdb/slice.h"
  22. #include "port/port.h"
  23. #include "port/port_dirent.h"
  24. #include "port/win/win_logger.h"
  25. #include "port/win/io_win.h"
  26. #include "monitoring/iostats_context_imp.h"
  27. #include "monitoring/thread_status_updater.h"
  28. #include "monitoring/thread_status_util.h"
  29. #include <rpc.h> // for uuid generation
  30. #include <windows.h>
  31. #include <shlwapi.h>
  32. #include "strsafe.h"
  33. #include <algorithm>
  34. namespace ROCKSDB_NAMESPACE {
  35. ThreadStatusUpdater* CreateThreadStatusUpdater() {
  36. return new ThreadStatusUpdater();
  37. }
  38. namespace {
  39. // Sector size used when physical sector size cannot be obtained from device.
  40. static const size_t kSectorSize = 512;
  41. // RAII helpers for HANDLEs
  42. const auto CloseHandleFunc = [](HANDLE h) { ::CloseHandle(h); };
  43. typedef std::unique_ptr<void, decltype(CloseHandleFunc)> UniqueCloseHandlePtr;
  44. const auto FindCloseFunc = [](HANDLE h) { ::FindClose(h); };
  45. typedef std::unique_ptr<void, decltype(FindCloseFunc)> UniqueFindClosePtr;
  46. void WinthreadCall(const char* label, std::error_code result) {
  47. if (0 != result.value()) {
  48. fprintf(stderr, "pthread %s: %s\n", label, strerror(result.value()));
  49. abort();
  50. }
  51. }
  52. }
  53. namespace port {
  54. WinEnvIO::WinEnvIO(Env* hosted_env)
  55. : hosted_env_(hosted_env),
  56. page_size_(4 * 1024),
  57. allocation_granularity_(page_size_),
  58. perf_counter_frequency_(0),
  59. nano_seconds_per_period_(0),
  60. GetSystemTimePreciseAsFileTime_(NULL) {
  61. SYSTEM_INFO sinfo;
  62. GetSystemInfo(&sinfo);
  63. page_size_ = sinfo.dwPageSize;
  64. allocation_granularity_ = sinfo.dwAllocationGranularity;
  65. {
  66. LARGE_INTEGER qpf;
  67. BOOL ret __attribute__((__unused__));
  68. ret = QueryPerformanceFrequency(&qpf);
  69. assert(ret == TRUE);
  70. perf_counter_frequency_ = qpf.QuadPart;
  71. if (std::nano::den % perf_counter_frequency_ == 0) {
  72. nano_seconds_per_period_ = std::nano::den / perf_counter_frequency_;
  73. }
  74. }
  75. HMODULE module = GetModuleHandle("kernel32.dll");
  76. if (module != NULL) {
  77. GetSystemTimePreciseAsFileTime_ =
  78. (FnGetSystemTimePreciseAsFileTime)GetProcAddress(
  79. module, "GetSystemTimePreciseAsFileTime");
  80. }
  81. }
  82. WinEnvIO::~WinEnvIO() {
  83. }
  84. Status WinEnvIO::DeleteFile(const std::string& fname) {
  85. Status result;
  86. BOOL ret = RX_DeleteFile(RX_FN(fname).c_str());
  87. if(!ret) {
  88. auto lastError = GetLastError();
  89. result = IOErrorFromWindowsError("Failed to delete: " + fname,
  90. lastError);
  91. }
  92. return result;
  93. }
  94. Status WinEnvIO::Truncate(const std::string& fname, size_t size) {
  95. Status s;
  96. int result = ROCKSDB_NAMESPACE::port::Truncate(fname, size);
  97. if (result != 0) {
  98. s = IOError("Failed to truncate: " + fname, errno);
  99. }
  100. return s;
  101. }
  102. Status WinEnvIO::GetCurrentTime(int64_t* unix_time) {
  103. time_t time = std::time(nullptr);
  104. if (time == (time_t)(-1)) {
  105. return Status::NotSupported("Failed to get time");
  106. }
  107. *unix_time = time;
  108. return Status::OK();
  109. }
  110. Status WinEnvIO::NewSequentialFile(const std::string& fname,
  111. std::unique_ptr<SequentialFile>* result,
  112. const EnvOptions& options) {
  113. Status s;
  114. result->reset();
  115. // Corruption test needs to rename and delete files of these kind
  116. // while they are still open with another handle. For that reason we
  117. // allow share_write and delete(allows rename).
  118. HANDLE hFile = INVALID_HANDLE_VALUE;
  119. DWORD fileFlags = FILE_ATTRIBUTE_READONLY;
  120. if (options.use_direct_reads && !options.use_mmap_reads) {
  121. fileFlags |= FILE_FLAG_NO_BUFFERING;
  122. }
  123. {
  124. IOSTATS_TIMER_GUARD(open_nanos);
  125. hFile = RX_CreateFile(
  126. RX_FN(fname).c_str(), GENERIC_READ,
  127. FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE, NULL,
  128. OPEN_EXISTING, // Original fopen mode is "rb"
  129. fileFlags, NULL);
  130. }
  131. if (INVALID_HANDLE_VALUE == hFile) {
  132. auto lastError = GetLastError();
  133. s = IOErrorFromWindowsError("Failed to open NewSequentialFile" + fname,
  134. lastError);
  135. } else {
  136. result->reset(new WinSequentialFile(fname, hFile, options));
  137. }
  138. return s;
  139. }
  140. Status WinEnvIO::NewRandomAccessFile(const std::string& fname,
  141. std::unique_ptr<RandomAccessFile>* result,
  142. const EnvOptions& options) {
  143. result->reset();
  144. Status s;
  145. // Open the file for read-only random access
  146. // Random access is to disable read-ahead as the system reads too much data
  147. DWORD fileFlags = FILE_ATTRIBUTE_READONLY;
  148. if (options.use_direct_reads && !options.use_mmap_reads) {
  149. fileFlags |= FILE_FLAG_NO_BUFFERING;
  150. } else {
  151. fileFlags |= FILE_FLAG_RANDOM_ACCESS;
  152. }
  153. /// Shared access is necessary for corruption test to pass
  154. // almost all tests would work with a possible exception of fault_injection
  155. HANDLE hFile = 0;
  156. {
  157. IOSTATS_TIMER_GUARD(open_nanos);
  158. hFile = RX_CreateFile(
  159. RX_FN(fname).c_str(), GENERIC_READ,
  160. FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE,
  161. NULL, OPEN_EXISTING, fileFlags, NULL);
  162. }
  163. if (INVALID_HANDLE_VALUE == hFile) {
  164. auto lastError = GetLastError();
  165. return IOErrorFromWindowsError(
  166. "NewRandomAccessFile failed to Create/Open: " + fname, lastError);
  167. }
  168. UniqueCloseHandlePtr fileGuard(hFile, CloseHandleFunc);
  169. // CAUTION! This will map the entire file into the process address space
  170. if (options.use_mmap_reads && sizeof(void*) >= 8) {
  171. // Use mmap when virtual address-space is plentiful.
  172. uint64_t fileSize;
  173. s = GetFileSize(fname, &fileSize);
  174. if (s.ok()) {
  175. // Will not map empty files
  176. if (fileSize == 0) {
  177. return IOError(
  178. "NewRandomAccessFile failed to map empty file: " + fname, EINVAL);
  179. }
  180. HANDLE hMap = RX_CreateFileMapping(hFile, NULL, PAGE_READONLY,
  181. 0, // At its present length
  182. 0,
  183. NULL); // Mapping name
  184. if (!hMap) {
  185. auto lastError = GetLastError();
  186. return IOErrorFromWindowsError(
  187. "Failed to create file mapping for NewRandomAccessFile: " + fname,
  188. lastError);
  189. }
  190. UniqueCloseHandlePtr mapGuard(hMap, CloseHandleFunc);
  191. const void* mapped_region =
  192. MapViewOfFileEx(hMap, FILE_MAP_READ,
  193. 0, // High DWORD of access start
  194. 0, // Low DWORD
  195. static_cast<SIZE_T>(fileSize),
  196. NULL); // Let the OS choose the mapping
  197. if (!mapped_region) {
  198. auto lastError = GetLastError();
  199. return IOErrorFromWindowsError(
  200. "Failed to MapViewOfFile for NewRandomAccessFile: " + fname,
  201. lastError);
  202. }
  203. result->reset(new WinMmapReadableFile(fname, hFile, hMap, mapped_region,
  204. static_cast<size_t>(fileSize)));
  205. mapGuard.release();
  206. fileGuard.release();
  207. }
  208. } else {
  209. result->reset(new WinRandomAccessFile(fname, hFile,
  210. std::max(GetSectorSize(fname),
  211. page_size_),
  212. options));
  213. fileGuard.release();
  214. }
  215. return s;
  216. }
  217. Status WinEnvIO::OpenWritableFile(const std::string& fname,
  218. std::unique_ptr<WritableFile>* result,
  219. const EnvOptions& options,
  220. bool reopen) {
  221. const size_t c_BufferCapacity = 64 * 1024;
  222. EnvOptions local_options(options);
  223. result->reset();
  224. Status s;
  225. DWORD fileFlags = FILE_ATTRIBUTE_NORMAL;
  226. if (local_options.use_direct_writes && !local_options.use_mmap_writes) {
  227. fileFlags = FILE_FLAG_NO_BUFFERING | FILE_FLAG_WRITE_THROUGH;
  228. }
  229. // Desired access. We are want to write only here but if we want to memory
  230. // map
  231. // the file then there is no write only mode so we have to create it
  232. // Read/Write
  233. // However, MapViewOfFile specifies only Write only
  234. DWORD desired_access = GENERIC_WRITE;
  235. DWORD shared_mode = FILE_SHARE_READ;
  236. if (local_options.use_mmap_writes) {
  237. desired_access |= GENERIC_READ;
  238. } else {
  239. // Adding this solely for tests to pass (fault_injection_test,
  240. // wal_manager_test).
  241. shared_mode |= (FILE_SHARE_WRITE | FILE_SHARE_DELETE);
  242. }
  243. // This will always truncate the file
  244. DWORD creation_disposition = CREATE_ALWAYS;
  245. if (reopen) {
  246. creation_disposition = OPEN_ALWAYS;
  247. }
  248. HANDLE hFile = 0;
  249. {
  250. IOSTATS_TIMER_GUARD(open_nanos);
  251. hFile = RX_CreateFile(
  252. RX_FN(fname).c_str(),
  253. desired_access, // Access desired
  254. shared_mode,
  255. NULL, // Security attributes
  256. // Posix env says (reopen) ? (O_CREATE | O_APPEND) : O_CREAT | O_TRUNC
  257. creation_disposition,
  258. fileFlags, // Flags
  259. NULL); // Template File
  260. }
  261. if (INVALID_HANDLE_VALUE == hFile) {
  262. auto lastError = GetLastError();
  263. return IOErrorFromWindowsError(
  264. "Failed to create a NewWriteableFile: " + fname, lastError);
  265. }
  266. // We will start writing at the end, appending
  267. if (reopen) {
  268. LARGE_INTEGER zero_move;
  269. zero_move.QuadPart = 0;
  270. BOOL ret = SetFilePointerEx(hFile, zero_move, NULL, FILE_END);
  271. if (!ret) {
  272. auto lastError = GetLastError();
  273. return IOErrorFromWindowsError(
  274. "Failed to create a ReopenWritableFile move to the end: " + fname,
  275. lastError);
  276. }
  277. }
  278. if (options.use_mmap_writes) {
  279. // We usually do not use mmmapping on SSD and thus we pass memory
  280. // page_size
  281. result->reset(new WinMmapFile(fname, hFile, page_size_,
  282. allocation_granularity_, local_options));
  283. } else {
  284. // Here we want the buffer allocation to be aligned by the SSD page size
  285. // and to be a multiple of it
  286. result->reset(new WinWritableFile(fname, hFile,
  287. std::max(GetSectorSize(fname),
  288. GetPageSize()),
  289. c_BufferCapacity, local_options));
  290. }
  291. return s;
  292. }
  293. Status WinEnvIO::NewRandomRWFile(const std::string & fname,
  294. std::unique_ptr<RandomRWFile>* result,
  295. const EnvOptions & options) {
  296. Status s;
  297. // Open the file for read-only random access
  298. // Random access is to disable read-ahead as the system reads too much data
  299. DWORD desired_access = GENERIC_READ | GENERIC_WRITE;
  300. DWORD shared_mode = FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE;
  301. DWORD creation_disposition = OPEN_EXISTING; // Fail if file does not exist
  302. DWORD file_flags = FILE_FLAG_RANDOM_ACCESS;
  303. if (options.use_direct_reads && options.use_direct_writes) {
  304. file_flags |= FILE_FLAG_NO_BUFFERING;
  305. }
  306. /// Shared access is necessary for corruption test to pass
  307. // almost all tests would work with a possible exception of fault_injection
  308. HANDLE hFile = 0;
  309. {
  310. IOSTATS_TIMER_GUARD(open_nanos);
  311. hFile =
  312. RX_CreateFile(RX_FN(fname).c_str(),
  313. desired_access,
  314. shared_mode,
  315. NULL, // Security attributes
  316. creation_disposition,
  317. file_flags,
  318. NULL);
  319. }
  320. if (INVALID_HANDLE_VALUE == hFile) {
  321. auto lastError = GetLastError();
  322. return IOErrorFromWindowsError(
  323. "NewRandomRWFile failed to Create/Open: " + fname, lastError);
  324. }
  325. UniqueCloseHandlePtr fileGuard(hFile, CloseHandleFunc);
  326. result->reset(new WinRandomRWFile(fname, hFile,
  327. std::max(GetSectorSize(fname),
  328. GetPageSize()),
  329. options));
  330. fileGuard.release();
  331. return s;
  332. }
  333. Status WinEnvIO::NewMemoryMappedFileBuffer(
  334. const std::string & fname,
  335. std::unique_ptr<MemoryMappedFileBuffer>* result) {
  336. Status s;
  337. result->reset();
  338. DWORD fileFlags = FILE_ATTRIBUTE_READONLY;
  339. HANDLE hFile = INVALID_HANDLE_VALUE;
  340. {
  341. IOSTATS_TIMER_GUARD(open_nanos);
  342. hFile = RX_CreateFile(
  343. RX_FN(fname).c_str(), GENERIC_READ | GENERIC_WRITE,
  344. FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE,
  345. NULL,
  346. OPEN_EXISTING, // Open only if it exists
  347. fileFlags,
  348. NULL);
  349. }
  350. if (INVALID_HANDLE_VALUE == hFile) {
  351. auto lastError = GetLastError();
  352. s = IOErrorFromWindowsError(
  353. "Failed to open NewMemoryMappedFileBuffer: " + fname, lastError);
  354. return s;
  355. }
  356. UniqueCloseHandlePtr fileGuard(hFile, CloseHandleFunc);
  357. uint64_t fileSize = 0;
  358. s = GetFileSize(fname, &fileSize);
  359. if (!s.ok()) {
  360. return s;
  361. }
  362. // Will not map empty files
  363. if (fileSize == 0) {
  364. return Status::NotSupported(
  365. "NewMemoryMappedFileBuffer can not map zero length files: " + fname);
  366. }
  367. // size_t is 32-bit with 32-bit builds
  368. if (fileSize > std::numeric_limits<size_t>::max()) {
  369. return Status::NotSupported(
  370. "The specified file size does not fit into 32-bit memory addressing: "
  371. + fname);
  372. }
  373. HANDLE hMap = RX_CreateFileMapping(hFile, NULL, PAGE_READWRITE,
  374. 0, // Whole file at its present length
  375. 0,
  376. NULL); // Mapping name
  377. if (!hMap) {
  378. auto lastError = GetLastError();
  379. return IOErrorFromWindowsError(
  380. "Failed to create file mapping for: " + fname, lastError);
  381. }
  382. UniqueCloseHandlePtr mapGuard(hMap, CloseHandleFunc);
  383. void* base = MapViewOfFileEx(hMap, FILE_MAP_WRITE,
  384. 0, // High DWORD of access start
  385. 0, // Low DWORD
  386. static_cast<SIZE_T>(fileSize),
  387. NULL); // Let the OS choose the mapping
  388. if (!base) {
  389. auto lastError = GetLastError();
  390. return IOErrorFromWindowsError(
  391. "Failed to MapViewOfFile for NewMemoryMappedFileBuffer: " + fname,
  392. lastError);
  393. }
  394. result->reset(new WinMemoryMappedBuffer(hFile, hMap, base,
  395. static_cast<size_t>(fileSize)));
  396. mapGuard.release();
  397. fileGuard.release();
  398. return s;
  399. }
  400. Status WinEnvIO::NewDirectory(const std::string& name,
  401. std::unique_ptr<Directory>* result) {
  402. Status s;
  403. // Must be nullptr on failure
  404. result->reset();
  405. if (!DirExists(name)) {
  406. s = IOErrorFromWindowsError(
  407. "open folder: " + name, ERROR_DIRECTORY);
  408. return s;
  409. }
  410. HANDLE handle = INVALID_HANDLE_VALUE;
  411. // 0 - for access means read metadata
  412. {
  413. IOSTATS_TIMER_GUARD(open_nanos);
  414. handle = RX_CreateFile(
  415. RX_FN(name).c_str(), 0,
  416. FILE_SHARE_DELETE | FILE_SHARE_READ | FILE_SHARE_WRITE,
  417. NULL,
  418. OPEN_EXISTING,
  419. FILE_FLAG_BACKUP_SEMANTICS, // make opening folders possible
  420. NULL);
  421. }
  422. if (INVALID_HANDLE_VALUE == handle) {
  423. auto lastError = GetLastError();
  424. s = IOErrorFromWindowsError("open folder: " + name, lastError);
  425. return s;
  426. }
  427. result->reset(new WinDirectory(handle));
  428. return s;
  429. }
  430. Status WinEnvIO::FileExists(const std::string& fname) {
  431. Status s;
  432. // TODO: This does not follow symbolic links at this point
  433. // which is consistent with _access() impl on windows
  434. // but can be added
  435. WIN32_FILE_ATTRIBUTE_DATA attrs;
  436. if (FALSE == RX_GetFileAttributesEx(RX_FN(fname).c_str(),
  437. GetFileExInfoStandard, &attrs)) {
  438. auto lastError = GetLastError();
  439. switch (lastError) {
  440. case ERROR_ACCESS_DENIED:
  441. case ERROR_NOT_FOUND:
  442. case ERROR_FILE_NOT_FOUND:
  443. case ERROR_PATH_NOT_FOUND:
  444. s = Status::NotFound();
  445. break;
  446. default:
  447. s = IOErrorFromWindowsError("Unexpected error for: " + fname,
  448. lastError);
  449. break;
  450. }
  451. }
  452. return s;
  453. }
  454. Status WinEnvIO::GetChildren(const std::string& dir,
  455. std::vector<std::string>* result) {
  456. Status status;
  457. result->clear();
  458. std::vector<std::string> output;
  459. RX_WIN32_FIND_DATA data;
  460. memset(&data, 0, sizeof(data));
  461. std::string pattern(dir);
  462. pattern.append("\\").append("*");
  463. HANDLE handle = RX_FindFirstFileEx(RX_FN(pattern).c_str(),
  464. // Do not want alternative name
  465. FindExInfoBasic,
  466. &data,
  467. FindExSearchNameMatch,
  468. NULL, // lpSearchFilter
  469. 0);
  470. if (handle == INVALID_HANDLE_VALUE) {
  471. auto lastError = GetLastError();
  472. switch (lastError) {
  473. case ERROR_NOT_FOUND:
  474. case ERROR_ACCESS_DENIED:
  475. case ERROR_FILE_NOT_FOUND:
  476. case ERROR_PATH_NOT_FOUND:
  477. status = Status::NotFound();
  478. break;
  479. default:
  480. status = IOErrorFromWindowsError(
  481. "Failed to GetChhildren for: " + dir, lastError);
  482. }
  483. return status;
  484. }
  485. UniqueFindClosePtr fc(handle, FindCloseFunc);
  486. if (result->capacity() > 0) {
  487. output.reserve(result->capacity());
  488. }
  489. // For safety
  490. data.cFileName[MAX_PATH - 1] = 0;
  491. while (true) {
  492. auto x = RX_FILESTRING(data.cFileName, RX_FNLEN(data.cFileName));
  493. output.emplace_back(FN_TO_RX(x));
  494. BOOL ret =- RX_FindNextFile(handle, &data);
  495. // If the function fails the return value is zero
  496. // and non-zero otherwise. Not TRUE or FALSE.
  497. if (ret == FALSE) {
  498. // Posix does not care why we stopped
  499. break;
  500. }
  501. data.cFileName[MAX_PATH - 1] = 0;
  502. }
  503. output.swap(*result);
  504. return status;
  505. }
  506. Status WinEnvIO::CreateDir(const std::string& name) {
  507. Status result;
  508. BOOL ret = RX_CreateDirectory(RX_FN(name).c_str(), NULL);
  509. if (!ret) {
  510. auto lastError = GetLastError();
  511. result = IOErrorFromWindowsError(
  512. "Failed to create a directory: " + name, lastError);
  513. }
  514. return result;
  515. }
  516. Status WinEnvIO::CreateDirIfMissing(const std::string& name) {
  517. Status result;
  518. if (DirExists(name)) {
  519. return result;
  520. }
  521. BOOL ret = RX_CreateDirectory(RX_FN(name).c_str(), NULL);
  522. if (!ret) {
  523. auto lastError = GetLastError();
  524. if (lastError != ERROR_ALREADY_EXISTS) {
  525. result = IOErrorFromWindowsError(
  526. "Failed to create a directory: " + name, lastError);
  527. } else {
  528. result =
  529. Status::IOError(name + ": exists but is not a directory");
  530. }
  531. }
  532. return result;
  533. }
  534. Status WinEnvIO::DeleteDir(const std::string& name) {
  535. Status result;
  536. BOOL ret = RX_RemoveDirectory(RX_FN(name).c_str());
  537. if (!ret) {
  538. auto lastError = GetLastError();
  539. result = IOErrorFromWindowsError("Failed to remove dir: " + name,
  540. lastError);
  541. }
  542. return result;
  543. }
  544. Status WinEnvIO::GetFileSize(const std::string& fname,
  545. uint64_t* size) {
  546. Status s;
  547. WIN32_FILE_ATTRIBUTE_DATA attrs;
  548. if (RX_GetFileAttributesEx(RX_FN(fname).c_str(), GetFileExInfoStandard,
  549. &attrs)) {
  550. ULARGE_INTEGER file_size;
  551. file_size.HighPart = attrs.nFileSizeHigh;
  552. file_size.LowPart = attrs.nFileSizeLow;
  553. *size = file_size.QuadPart;
  554. } else {
  555. auto lastError = GetLastError();
  556. s = IOErrorFromWindowsError("Can not get size for: " + fname, lastError);
  557. }
  558. return s;
  559. }
  560. uint64_t WinEnvIO::FileTimeToUnixTime(const FILETIME& ftTime) {
  561. const uint64_t c_FileTimePerSecond = 10000000U;
  562. // UNIX epoch starts on 1970-01-01T00:00:00Z
  563. // Windows FILETIME starts on 1601-01-01T00:00:00Z
  564. // Therefore, we need to subtract the below number of seconds from
  565. // the seconds that we obtain from FILETIME with an obvious loss of
  566. // precision
  567. const uint64_t c_SecondBeforeUnixEpoch = 11644473600U;
  568. ULARGE_INTEGER li;
  569. li.HighPart = ftTime.dwHighDateTime;
  570. li.LowPart = ftTime.dwLowDateTime;
  571. uint64_t result =
  572. (li.QuadPart / c_FileTimePerSecond) - c_SecondBeforeUnixEpoch;
  573. return result;
  574. }
  575. Status WinEnvIO::GetFileModificationTime(const std::string& fname,
  576. uint64_t* file_mtime) {
  577. Status s;
  578. WIN32_FILE_ATTRIBUTE_DATA attrs;
  579. if (RX_GetFileAttributesEx(RX_FN(fname).c_str(), GetFileExInfoStandard,
  580. &attrs)) {
  581. *file_mtime = FileTimeToUnixTime(attrs.ftLastWriteTime);
  582. } else {
  583. auto lastError = GetLastError();
  584. s = IOErrorFromWindowsError(
  585. "Can not get file modification time for: " + fname, lastError);
  586. *file_mtime = 0;
  587. }
  588. return s;
  589. }
  590. Status WinEnvIO::RenameFile(const std::string& src,
  591. const std::string& target) {
  592. Status result;
  593. // rename() is not capable of replacing the existing file as on Linux
  594. // so use OS API directly
  595. if (!RX_MoveFileEx(RX_FN(src).c_str(), RX_FN(target).c_str(),
  596. MOVEFILE_REPLACE_EXISTING)) {
  597. DWORD lastError = GetLastError();
  598. std::string text("Failed to rename: ");
  599. text.append(src).append(" to: ").append(target);
  600. result = IOErrorFromWindowsError(text, lastError);
  601. }
  602. return result;
  603. }
  604. Status WinEnvIO::LinkFile(const std::string& src,
  605. const std::string& target) {
  606. Status result;
  607. if (!RX_CreateHardLink(RX_FN(target).c_str(), RX_FN(src).c_str(), NULL)) {
  608. DWORD lastError = GetLastError();
  609. if (lastError == ERROR_NOT_SAME_DEVICE) {
  610. return Status::NotSupported("No cross FS links allowed");
  611. }
  612. std::string text("Failed to link: ");
  613. text.append(src).append(" to: ").append(target);
  614. result = IOErrorFromWindowsError(text, lastError);
  615. }
  616. return result;
  617. }
  618. Status WinEnvIO::NumFileLinks(const std::string& fname, uint64_t* count) {
  619. Status s;
  620. HANDLE handle = RX_CreateFile(
  621. RX_FN(fname).c_str(), 0,
  622. FILE_SHARE_DELETE | FILE_SHARE_READ | FILE_SHARE_WRITE,
  623. NULL, OPEN_EXISTING, FILE_FLAG_BACKUP_SEMANTICS, NULL);
  624. if (INVALID_HANDLE_VALUE == handle) {
  625. auto lastError = GetLastError();
  626. s = IOErrorFromWindowsError("NumFileLinks: " + fname, lastError);
  627. return s;
  628. }
  629. UniqueCloseHandlePtr handle_guard(handle, CloseHandleFunc);
  630. FILE_STANDARD_INFO standard_info;
  631. if (0 != GetFileInformationByHandleEx(handle, FileStandardInfo,
  632. &standard_info,
  633. sizeof(standard_info))) {
  634. *count = standard_info.NumberOfLinks;
  635. } else {
  636. auto lastError = GetLastError();
  637. s = IOErrorFromWindowsError("GetFileInformationByHandleEx: " + fname,
  638. lastError);
  639. }
  640. return s;
  641. }
  642. Status WinEnvIO::AreFilesSame(const std::string& first,
  643. const std::string& second, bool* res) {
  644. // For MinGW builds
  645. #if (_WIN32_WINNT == _WIN32_WINNT_VISTA)
  646. Status s = Status::NotSupported();
  647. #else
  648. assert(res != nullptr);
  649. Status s;
  650. if (res == nullptr) {
  651. s = Status::InvalidArgument("res");
  652. return s;
  653. }
  654. // 0 - for access means read metadata
  655. HANDLE file_1 = RX_CreateFile(
  656. RX_FN(first).c_str(), 0,
  657. FILE_SHARE_DELETE | FILE_SHARE_READ | FILE_SHARE_WRITE,
  658. NULL,
  659. OPEN_EXISTING,
  660. FILE_FLAG_BACKUP_SEMANTICS, // make opening folders possible
  661. NULL);
  662. if (INVALID_HANDLE_VALUE == file_1) {
  663. auto lastError = GetLastError();
  664. s = IOErrorFromWindowsError("open file: " + first, lastError);
  665. return s;
  666. }
  667. UniqueCloseHandlePtr g_1(file_1, CloseHandleFunc);
  668. HANDLE file_2 = RX_CreateFile(
  669. RX_FN(second).c_str(), 0,
  670. FILE_SHARE_DELETE | FILE_SHARE_READ | FILE_SHARE_WRITE,
  671. NULL, OPEN_EXISTING,
  672. FILE_FLAG_BACKUP_SEMANTICS, // make opening folders possible
  673. NULL);
  674. if (INVALID_HANDLE_VALUE == file_2) {
  675. auto lastError = GetLastError();
  676. s = IOErrorFromWindowsError("open file: " + second, lastError);
  677. return s;
  678. }
  679. UniqueCloseHandlePtr g_2(file_2, CloseHandleFunc);
  680. FILE_ID_INFO FileInfo_1;
  681. BOOL result = GetFileInformationByHandleEx(file_1, FileIdInfo, &FileInfo_1,
  682. sizeof(FileInfo_1));
  683. if (!result) {
  684. auto lastError = GetLastError();
  685. s = IOErrorFromWindowsError("stat file: " + first, lastError);
  686. return s;
  687. }
  688. FILE_ID_INFO FileInfo_2;
  689. result = GetFileInformationByHandleEx(file_2, FileIdInfo, &FileInfo_2,
  690. sizeof(FileInfo_2));
  691. if (!result) {
  692. auto lastError = GetLastError();
  693. s = IOErrorFromWindowsError("stat file: " + second, lastError);
  694. return s;
  695. }
  696. if (FileInfo_1.VolumeSerialNumber == FileInfo_2.VolumeSerialNumber) {
  697. *res = (0 == memcmp(FileInfo_1.FileId.Identifier,
  698. FileInfo_2.FileId.Identifier,
  699. sizeof(FileInfo_1.FileId.Identifier)));
  700. } else {
  701. *res = false;
  702. }
  703. #endif
  704. return s;
  705. }
  706. Status WinEnvIO::LockFile(const std::string& lockFname,
  707. FileLock** lock) {
  708. assert(lock != nullptr);
  709. *lock = NULL;
  710. Status result;
  711. // No-sharing, this is a LOCK file
  712. const DWORD ExclusiveAccessON = 0;
  713. // Obtain exclusive access to the LOCK file
  714. // Previously, instead of NORMAL attr we set DELETE on close and that worked
  715. // well except with fault_injection test that insists on deleting it.
  716. HANDLE hFile = 0;
  717. {
  718. IOSTATS_TIMER_GUARD(open_nanos);
  719. hFile = RX_CreateFile(RX_FN(lockFname).c_str(),
  720. (GENERIC_READ | GENERIC_WRITE),
  721. ExclusiveAccessON, NULL, CREATE_ALWAYS,
  722. FILE_ATTRIBUTE_NORMAL, NULL);
  723. }
  724. if (INVALID_HANDLE_VALUE == hFile) {
  725. auto lastError = GetLastError();
  726. result = IOErrorFromWindowsError(
  727. "Failed to create lock file: " + lockFname, lastError);
  728. } else {
  729. *lock = new WinFileLock(hFile);
  730. }
  731. return result;
  732. }
  733. Status WinEnvIO::UnlockFile(FileLock* lock) {
  734. Status result;
  735. assert(lock != nullptr);
  736. delete lock;
  737. return result;
  738. }
  739. Status WinEnvIO::GetTestDirectory(std::string* result) {
  740. std::string output;
  741. const char* env = getenv("TEST_TMPDIR");
  742. if (env && env[0] != '\0') {
  743. output = env;
  744. } else {
  745. env = getenv("TMP");
  746. if (env && env[0] != '\0') {
  747. output = env;
  748. } else {
  749. output = "c:\\tmp";
  750. }
  751. }
  752. CreateDir(output);
  753. output.append("\\testrocksdb-");
  754. output.append(std::to_string(_getpid()));
  755. CreateDir(output);
  756. output.swap(*result);
  757. return Status::OK();
  758. }
  759. Status WinEnvIO::NewLogger(const std::string& fname,
  760. std::shared_ptr<Logger>* result) {
  761. Status s;
  762. result->reset();
  763. HANDLE hFile = 0;
  764. {
  765. IOSTATS_TIMER_GUARD(open_nanos);
  766. hFile = RX_CreateFile(
  767. RX_FN(fname).c_str(), GENERIC_WRITE,
  768. FILE_SHARE_READ | FILE_SHARE_DELETE, // In RocksDb log files are
  769. // renamed and deleted before
  770. // they are closed. This enables
  771. // doing so.
  772. NULL,
  773. CREATE_ALWAYS, // Original fopen mode is "w"
  774. FILE_ATTRIBUTE_NORMAL, NULL);
  775. }
  776. if (INVALID_HANDLE_VALUE == hFile) {
  777. auto lastError = GetLastError();
  778. s = IOErrorFromWindowsError("Failed to open LogFile" + fname, lastError);
  779. } else {
  780. {
  781. // With log files we want to set the true creation time as of now
  782. // because the system
  783. // for some reason caches the attributes of the previous file that just
  784. // been renamed from
  785. // this name so auto_roll_logger_test fails
  786. FILETIME ft;
  787. GetSystemTimeAsFileTime(&ft);
  788. // Set creation, last access and last write time to the same value
  789. SetFileTime(hFile, &ft, &ft, &ft);
  790. }
  791. result->reset(new WinLogger(&WinEnvThreads::gettid, hosted_env_, hFile));
  792. }
  793. return s;
  794. }
  795. uint64_t WinEnvIO::NowMicros() {
  796. if (GetSystemTimePreciseAsFileTime_ != NULL) {
  797. // all std::chrono clocks on windows proved to return
  798. // values that may repeat that is not good enough for some uses.
  799. const int64_t c_UnixEpochStartTicks = 116444736000000000LL;
  800. const int64_t c_FtToMicroSec = 10;
  801. // This interface needs to return system time and not
  802. // just any microseconds because it is often used as an argument
  803. // to TimedWait() on condition variable
  804. FILETIME ftSystemTime;
  805. GetSystemTimePreciseAsFileTime_(&ftSystemTime);
  806. LARGE_INTEGER li;
  807. li.LowPart = ftSystemTime.dwLowDateTime;
  808. li.HighPart = ftSystemTime.dwHighDateTime;
  809. // Subtract unix epoch start
  810. li.QuadPart -= c_UnixEpochStartTicks;
  811. // Convert to microsecs
  812. li.QuadPart /= c_FtToMicroSec;
  813. return li.QuadPart;
  814. }
  815. using namespace std::chrono;
  816. return duration_cast<microseconds>(system_clock::now().time_since_epoch())
  817. .count();
  818. }
  819. uint64_t WinEnvIO::NowNanos() {
  820. if (nano_seconds_per_period_ != 0) {
  821. // all std::chrono clocks on windows have the same resolution that is only
  822. // good enough for microseconds but not nanoseconds
  823. // On Windows 8 and Windows 2012 Server
  824. // GetSystemTimePreciseAsFileTime(&current_time) can be used
  825. LARGE_INTEGER li;
  826. QueryPerformanceCounter(&li);
  827. // Convert performance counter to nanoseconds by precomputed ratio.
  828. // Directly multiply nano::den with li.QuadPart causes overflow.
  829. // Only do this when nano::den is divisible by perf_counter_frequency_,
  830. // which most likely is the case in reality. If it's not, fall back to
  831. // high_resolution_clock, which may be less precise under old compilers.
  832. li.QuadPart *= nano_seconds_per_period_;
  833. return li.QuadPart;
  834. }
  835. using namespace std::chrono;
  836. return duration_cast<nanoseconds>(
  837. high_resolution_clock::now().time_since_epoch()).count();
  838. }
  839. Status WinEnvIO::GetHostName(char* name, uint64_t len) {
  840. Status s;
  841. DWORD nSize = static_cast<DWORD>(
  842. std::min<uint64_t>(len, std::numeric_limits<DWORD>::max()));
  843. if (!::GetComputerNameA(name, &nSize)) {
  844. auto lastError = GetLastError();
  845. s = IOErrorFromWindowsError("GetHostName", lastError);
  846. } else {
  847. name[nSize] = 0;
  848. }
  849. return s;
  850. }
  851. Status WinEnvIO::GetAbsolutePath(const std::string& db_path,
  852. std::string* output_path) {
  853. // Check if we already have an absolute path
  854. // For test compatibility we will consider starting slash as an
  855. // absolute path
  856. if ((!db_path.empty() && (db_path[0] == '\\' || db_path[0] == '/')) ||
  857. !RX_PathIsRelative(RX_FN(db_path).c_str())) {
  858. *output_path = db_path;
  859. return Status::OK();
  860. }
  861. RX_FILESTRING result;
  862. result.resize(MAX_PATH);
  863. // Hopefully no changes the current directory while we do this
  864. // however _getcwd also suffers from the same limitation
  865. DWORD len = RX_GetCurrentDirectory(MAX_PATH, &result[0]);
  866. if (len == 0) {
  867. auto lastError = GetLastError();
  868. return IOErrorFromWindowsError("Failed to get current working directory",
  869. lastError);
  870. }
  871. result.resize(len);
  872. std::string res = FN_TO_RX(result);
  873. res.swap(*output_path);
  874. return Status::OK();
  875. }
  876. std::string WinEnvIO::TimeToString(uint64_t secondsSince1970) {
  877. std::string result;
  878. const time_t seconds = secondsSince1970;
  879. const int maxsize = 64;
  880. struct tm t;
  881. errno_t ret = localtime_s(&t, &seconds);
  882. if (ret) {
  883. result = std::to_string(seconds);
  884. } else {
  885. result.resize(maxsize);
  886. char* p = &result[0];
  887. int len = snprintf(p, maxsize, "%04d/%02d/%02d-%02d:%02d:%02d ",
  888. t.tm_year + 1900, t.tm_mon + 1, t.tm_mday, t.tm_hour,
  889. t.tm_min, t.tm_sec);
  890. assert(len > 0);
  891. result.resize(len);
  892. }
  893. return result;
  894. }
  895. Status WinEnvIO::GetFreeSpace(const std::string& path, uint64_t* diskfree) {
  896. assert(diskfree != nullptr);
  897. ULARGE_INTEGER freeBytes;
  898. BOOL f = RX_GetDiskFreeSpaceEx(RX_FN(path).c_str(), &freeBytes, NULL, NULL);
  899. if (f) {
  900. *diskfree = freeBytes.QuadPart;
  901. return Status::OK();
  902. } else {
  903. DWORD lastError = GetLastError();
  904. return IOErrorFromWindowsError("Failed to get free space: " + path,
  905. lastError);
  906. }
  907. }
  908. EnvOptions WinEnvIO::OptimizeForLogWrite(const EnvOptions& env_options,
  909. const DBOptions& db_options) const {
  910. EnvOptions optimized(env_options);
  911. // These two the same as default optimizations
  912. optimized.bytes_per_sync = db_options.wal_bytes_per_sync;
  913. optimized.writable_file_max_buffer_size =
  914. db_options.writable_file_max_buffer_size;
  915. // This adversely affects %999 on windows
  916. optimized.use_mmap_writes = false;
  917. // Direct writes will produce a huge perf impact on
  918. // Windows. Pre-allocate space for WAL.
  919. optimized.use_direct_writes = false;
  920. return optimized;
  921. }
  922. EnvOptions WinEnvIO::OptimizeForManifestWrite(
  923. const EnvOptions& env_options) const {
  924. EnvOptions optimized(env_options);
  925. optimized.use_mmap_writes = false;
  926. optimized.use_direct_reads = false;
  927. return optimized;
  928. }
  929. EnvOptions WinEnvIO::OptimizeForManifestRead(
  930. const EnvOptions& env_options) const {
  931. EnvOptions optimized(env_options);
  932. optimized.use_mmap_writes = false;
  933. optimized.use_direct_reads = false;
  934. return optimized;
  935. }
  936. // Returns true iff the named directory exists and is a directory.
  937. bool WinEnvIO::DirExists(const std::string& dname) {
  938. WIN32_FILE_ATTRIBUTE_DATA attrs;
  939. if (RX_GetFileAttributesEx(RX_FN(dname).c_str(),
  940. GetFileExInfoStandard, &attrs)) {
  941. return 0 != (attrs.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY);
  942. }
  943. return false;
  944. }
  945. size_t WinEnvIO::GetSectorSize(const std::string& fname) {
  946. size_t sector_size = kSectorSize;
  947. if (RX_PathIsRelative(RX_FN(fname).c_str())) {
  948. return sector_size;
  949. }
  950. // obtain device handle
  951. char devicename[7] = "\\\\.\\";
  952. int erresult = strncat_s(devicename, sizeof(devicename), fname.c_str(), 2);
  953. if (erresult) {
  954. assert(false);
  955. return sector_size;
  956. }
  957. HANDLE hDevice = CreateFile(devicename, 0, 0, nullptr, OPEN_EXISTING,
  958. FILE_ATTRIBUTE_NORMAL, nullptr);
  959. if (hDevice == INVALID_HANDLE_VALUE) {
  960. return sector_size;
  961. }
  962. STORAGE_PROPERTY_QUERY spropertyquery;
  963. spropertyquery.PropertyId = StorageAccessAlignmentProperty;
  964. spropertyquery.QueryType = PropertyStandardQuery;
  965. BYTE output_buffer[sizeof(STORAGE_ACCESS_ALIGNMENT_DESCRIPTOR)];
  966. DWORD output_bytes = 0;
  967. BOOL ret = DeviceIoControl(hDevice, IOCTL_STORAGE_QUERY_PROPERTY,
  968. &spropertyquery, sizeof(spropertyquery),
  969. output_buffer,
  970. sizeof(STORAGE_ACCESS_ALIGNMENT_DESCRIPTOR),
  971. &output_bytes, nullptr);
  972. if (ret) {
  973. sector_size = ((STORAGE_ACCESS_ALIGNMENT_DESCRIPTOR *)output_buffer)->BytesPerLogicalSector;
  974. } else {
  975. // many devices do not support StorageProcessAlignmentProperty. Any failure here and we
  976. // fall back to logical alignment
  977. DISK_GEOMETRY_EX geometry = { 0 };
  978. ret = DeviceIoControl(hDevice, IOCTL_DISK_GET_DRIVE_GEOMETRY,
  979. nullptr, 0, &geometry, sizeof(geometry), &output_bytes, nullptr);
  980. if (ret) {
  981. sector_size = geometry.Geometry.BytesPerSector;
  982. }
  983. }
  984. if (hDevice != INVALID_HANDLE_VALUE) {
  985. CloseHandle(hDevice);
  986. }
  987. return sector_size;
  988. }
  989. ////////////////////////////////////////////////////////////////////////
  990. // WinEnvThreads
  991. WinEnvThreads::WinEnvThreads(Env* hosted_env)
  992. : hosted_env_(hosted_env), thread_pools_(Env::Priority::TOTAL) {
  993. for (int pool_id = 0; pool_id < Env::Priority::TOTAL; ++pool_id) {
  994. thread_pools_[pool_id].SetThreadPriority(
  995. static_cast<Env::Priority>(pool_id));
  996. // This allows later initializing the thread-local-env of each thread.
  997. thread_pools_[pool_id].SetHostEnv(hosted_env);
  998. }
  999. }
  1000. WinEnvThreads::~WinEnvThreads() {
  1001. WaitForJoin();
  1002. for (auto& thpool : thread_pools_) {
  1003. thpool.JoinAllThreads();
  1004. }
  1005. }
  1006. void WinEnvThreads::Schedule(void(*function)(void*), void* arg,
  1007. Env::Priority pri, void* tag,
  1008. void(*unschedFunction)(void* arg)) {
  1009. assert(pri >= Env::Priority::BOTTOM && pri <= Env::Priority::HIGH);
  1010. thread_pools_[pri].Schedule(function, arg, tag, unschedFunction);
  1011. }
  1012. int WinEnvThreads::UnSchedule(void* arg, Env::Priority pri) {
  1013. return thread_pools_[pri].UnSchedule(arg);
  1014. }
  1015. namespace {
  1016. struct StartThreadState {
  1017. void(*user_function)(void*);
  1018. void* arg;
  1019. };
  1020. void* StartThreadWrapper(void* arg) {
  1021. std::unique_ptr<StartThreadState> state(
  1022. reinterpret_cast<StartThreadState*>(arg));
  1023. state->user_function(state->arg);
  1024. return nullptr;
  1025. }
  1026. }
  1027. void WinEnvThreads::StartThread(void(*function)(void* arg), void* arg) {
  1028. std::unique_ptr<StartThreadState> state(new StartThreadState);
  1029. state->user_function = function;
  1030. state->arg = arg;
  1031. try {
  1032. ROCKSDB_NAMESPACE::port::WindowsThread th(&StartThreadWrapper, state.get());
  1033. state.release();
  1034. std::lock_guard<std::mutex> lg(mu_);
  1035. threads_to_join_.push_back(std::move(th));
  1036. } catch (const std::system_error& ex) {
  1037. WinthreadCall("start thread", ex.code());
  1038. }
  1039. }
  1040. void WinEnvThreads::WaitForJoin() {
  1041. for (auto& th : threads_to_join_) {
  1042. th.join();
  1043. }
  1044. threads_to_join_.clear();
  1045. }
  1046. unsigned int WinEnvThreads::GetThreadPoolQueueLen(Env::Priority pri) const {
  1047. assert(pri >= Env::Priority::BOTTOM && pri <= Env::Priority::HIGH);
  1048. return thread_pools_[pri].GetQueueLen();
  1049. }
  1050. uint64_t WinEnvThreads::gettid() {
  1051. uint64_t thread_id = GetCurrentThreadId();
  1052. return thread_id;
  1053. }
  1054. uint64_t WinEnvThreads::GetThreadID() const { return gettid(); }
  1055. void WinEnvThreads::SleepForMicroseconds(int micros) {
  1056. std::this_thread::sleep_for(std::chrono::microseconds(micros));
  1057. }
  1058. void WinEnvThreads::SetBackgroundThreads(int num, Env::Priority pri) {
  1059. assert(pri >= Env::Priority::BOTTOM && pri <= Env::Priority::HIGH);
  1060. thread_pools_[pri].SetBackgroundThreads(num);
  1061. }
  1062. int WinEnvThreads::GetBackgroundThreads(Env::Priority pri) {
  1063. assert(pri >= Env::Priority::BOTTOM && pri <= Env::Priority::HIGH);
  1064. return thread_pools_[pri].GetBackgroundThreads();
  1065. }
  1066. void WinEnvThreads::IncBackgroundThreadsIfNeeded(int num, Env::Priority pri) {
  1067. assert(pri >= Env::Priority::BOTTOM && pri <= Env::Priority::HIGH);
  1068. thread_pools_[pri].IncBackgroundThreadsIfNeeded(num);
  1069. }
  1070. /////////////////////////////////////////////////////////////////////////
  1071. // WinEnv
  1072. WinEnv::WinEnv() : winenv_io_(this), winenv_threads_(this) {
  1073. // Protected member of the base class
  1074. thread_status_updater_ = CreateThreadStatusUpdater();
  1075. }
  1076. WinEnv::~WinEnv() {
  1077. // All threads must be joined before the deletion of
  1078. // thread_status_updater_.
  1079. delete thread_status_updater_;
  1080. }
  1081. Status WinEnv::GetThreadList(std::vector<ThreadStatus>* thread_list) {
  1082. assert(thread_status_updater_);
  1083. return thread_status_updater_->GetThreadList(thread_list);
  1084. }
  1085. Status WinEnv::DeleteFile(const std::string& fname) {
  1086. return winenv_io_.DeleteFile(fname);
  1087. }
  1088. Status WinEnv::Truncate(const std::string& fname, size_t size) {
  1089. return winenv_io_.Truncate(fname, size);
  1090. }
  1091. Status WinEnv::GetCurrentTime(int64_t* unix_time) {
  1092. return winenv_io_.GetCurrentTime(unix_time);
  1093. }
  1094. Status WinEnv::NewSequentialFile(const std::string& fname,
  1095. std::unique_ptr<SequentialFile>* result,
  1096. const EnvOptions& options) {
  1097. return winenv_io_.NewSequentialFile(fname, result, options);
  1098. }
  1099. Status WinEnv::NewRandomAccessFile(const std::string& fname,
  1100. std::unique_ptr<RandomAccessFile>* result,
  1101. const EnvOptions& options) {
  1102. return winenv_io_.NewRandomAccessFile(fname, result, options);
  1103. }
  1104. Status WinEnv::NewWritableFile(const std::string& fname,
  1105. std::unique_ptr<WritableFile>* result,
  1106. const EnvOptions& options) {
  1107. return winenv_io_.OpenWritableFile(fname, result, options, false);
  1108. }
  1109. Status WinEnv::ReopenWritableFile(const std::string& fname,
  1110. std::unique_ptr<WritableFile>* result,
  1111. const EnvOptions& options) {
  1112. return winenv_io_.OpenWritableFile(fname, result, options, true);
  1113. }
  1114. Status WinEnv::NewRandomRWFile(const std::string & fname,
  1115. std::unique_ptr<RandomRWFile>* result,
  1116. const EnvOptions & options) {
  1117. return winenv_io_.NewRandomRWFile(fname, result, options);
  1118. }
  1119. Status WinEnv::NewMemoryMappedFileBuffer(
  1120. const std::string& fname,
  1121. std::unique_ptr<MemoryMappedFileBuffer>* result) {
  1122. return winenv_io_.NewMemoryMappedFileBuffer(fname, result);
  1123. }
  1124. Status WinEnv::NewDirectory(const std::string& name,
  1125. std::unique_ptr<Directory>* result) {
  1126. return winenv_io_.NewDirectory(name, result);
  1127. }
  1128. Status WinEnv::FileExists(const std::string& fname) {
  1129. return winenv_io_.FileExists(fname);
  1130. }
  1131. Status WinEnv::GetChildren(const std::string& dir,
  1132. std::vector<std::string>* result) {
  1133. return winenv_io_.GetChildren(dir, result);
  1134. }
  1135. Status WinEnv::CreateDir(const std::string& name) {
  1136. return winenv_io_.CreateDir(name);
  1137. }
  1138. Status WinEnv::CreateDirIfMissing(const std::string& name) {
  1139. return winenv_io_.CreateDirIfMissing(name);
  1140. }
  1141. Status WinEnv::DeleteDir(const std::string& name) {
  1142. return winenv_io_.DeleteDir(name);
  1143. }
  1144. Status WinEnv::GetFileSize(const std::string& fname,
  1145. uint64_t* size) {
  1146. return winenv_io_.GetFileSize(fname, size);
  1147. }
  1148. Status WinEnv::GetFileModificationTime(const std::string& fname,
  1149. uint64_t* file_mtime) {
  1150. return winenv_io_.GetFileModificationTime(fname, file_mtime);
  1151. }
  1152. Status WinEnv::RenameFile(const std::string& src,
  1153. const std::string& target) {
  1154. return winenv_io_.RenameFile(src, target);
  1155. }
  1156. Status WinEnv::LinkFile(const std::string& src,
  1157. const std::string& target) {
  1158. return winenv_io_.LinkFile(src, target);
  1159. }
  1160. Status WinEnv::NumFileLinks(const std::string& fname, uint64_t* count) {
  1161. return winenv_io_.NumFileLinks(fname, count);
  1162. }
  1163. Status WinEnv::AreFilesSame(const std::string& first,
  1164. const std::string& second, bool* res) {
  1165. return winenv_io_.AreFilesSame(first, second, res);
  1166. }
  1167. Status WinEnv::LockFile(const std::string& lockFname,
  1168. FileLock** lock) {
  1169. return winenv_io_.LockFile(lockFname, lock);
  1170. }
  1171. Status WinEnv::UnlockFile(FileLock* lock) {
  1172. return winenv_io_.UnlockFile(lock);
  1173. }
  1174. Status WinEnv::GetTestDirectory(std::string* result) {
  1175. return winenv_io_.GetTestDirectory(result);
  1176. }
  1177. Status WinEnv::NewLogger(const std::string& fname,
  1178. std::shared_ptr<Logger>* result) {
  1179. return winenv_io_.NewLogger(fname, result);
  1180. }
  1181. uint64_t WinEnv::NowMicros() {
  1182. return winenv_io_.NowMicros();
  1183. }
  1184. uint64_t WinEnv::NowNanos() {
  1185. return winenv_io_.NowNanos();
  1186. }
  1187. Status WinEnv::GetHostName(char* name, uint64_t len) {
  1188. return winenv_io_.GetHostName(name, len);
  1189. }
  1190. Status WinEnv::GetAbsolutePath(const std::string& db_path,
  1191. std::string* output_path) {
  1192. return winenv_io_.GetAbsolutePath(db_path, output_path);
  1193. }
  1194. std::string WinEnv::TimeToString(uint64_t secondsSince1970) {
  1195. return winenv_io_.TimeToString(secondsSince1970);
  1196. }
  1197. void WinEnv::Schedule(void(*function)(void*), void* arg, Env::Priority pri,
  1198. void* tag,
  1199. void(*unschedFunction)(void* arg)) {
  1200. return winenv_threads_.Schedule(function, arg, pri, tag, unschedFunction);
  1201. }
  1202. int WinEnv::UnSchedule(void* arg, Env::Priority pri) {
  1203. return winenv_threads_.UnSchedule(arg, pri);
  1204. }
  1205. void WinEnv::StartThread(void(*function)(void* arg), void* arg) {
  1206. return winenv_threads_.StartThread(function, arg);
  1207. }
  1208. void WinEnv::WaitForJoin() {
  1209. return winenv_threads_.WaitForJoin();
  1210. }
  1211. unsigned int WinEnv::GetThreadPoolQueueLen(Env::Priority pri) const {
  1212. return winenv_threads_.GetThreadPoolQueueLen(pri);
  1213. }
  1214. uint64_t WinEnv::GetThreadID() const {
  1215. return winenv_threads_.GetThreadID();
  1216. }
  1217. Status WinEnv::GetFreeSpace(const std::string& path, uint64_t* diskfree) {
  1218. return winenv_io_.GetFreeSpace(path, diskfree);
  1219. }
  1220. void WinEnv::SleepForMicroseconds(int micros) {
  1221. return winenv_threads_.SleepForMicroseconds(micros);
  1222. }
  1223. // Allow increasing the number of worker threads.
  1224. void WinEnv::SetBackgroundThreads(int num, Env::Priority pri) {
  1225. return winenv_threads_.SetBackgroundThreads(num, pri);
  1226. }
  1227. int WinEnv::GetBackgroundThreads(Env::Priority pri) {
  1228. return winenv_threads_.GetBackgroundThreads(pri);
  1229. }
  1230. void WinEnv::IncBackgroundThreadsIfNeeded(int num, Env::Priority pri) {
  1231. return winenv_threads_.IncBackgroundThreadsIfNeeded(num, pri);
  1232. }
  1233. EnvOptions WinEnv::OptimizeForManifestRead(
  1234. const EnvOptions& env_options) const {
  1235. return winenv_io_.OptimizeForManifestRead(env_options);
  1236. }
  1237. EnvOptions WinEnv::OptimizeForLogWrite(const EnvOptions& env_options,
  1238. const DBOptions& db_options) const {
  1239. return winenv_io_.OptimizeForLogWrite(env_options, db_options);
  1240. }
  1241. EnvOptions WinEnv::OptimizeForManifestWrite(
  1242. const EnvOptions& env_options) const {
  1243. return winenv_io_.OptimizeForManifestWrite(env_options);
  1244. }
  1245. } // namespace port
  1246. std::string Env::GenerateUniqueId() {
  1247. std::string result;
  1248. UUID uuid;
  1249. UuidCreateSequential(&uuid);
  1250. RPC_CSTR rpc_str;
  1251. auto status = UuidToStringA(&uuid, &rpc_str);
  1252. (void)status;
  1253. assert(status == RPC_S_OK);
  1254. result = reinterpret_cast<char*>(rpc_str);
  1255. status = RpcStringFreeA(&rpc_str);
  1256. assert(status == RPC_S_OK);
  1257. return result;
  1258. }
  1259. } // namespace ROCKSDB_NAMESPACE