Commit 8c357b33 authored by Adam Simpkins's avatar Adam Simpkins Committed by Facebook GitHub Bot

update AsyncFileWriter to avoid writevFull() on Windows

Summary:
Change folly/logging's AsyncFileWriter code to avoid calling writevFull() on
Windows.  On POSIX system we use this function to minimize the number of
system calls we make.  However, on Windows there is no `writev()` function
provided by the system, so folly emulates this by performing multiple separate
`write()` calls.  Given that we are just going to perform multiple `write()`
calls there is no reason to construct the iobuf data structure here.

Differential Revision: D21367424

fbshipit-source-id: 8f6ba9d5fb36318a33d293a416008cd7fa03cb70
parent f121101a
...@@ -38,13 +38,18 @@ bool AsyncFileWriter::ttyOutput() const { ...@@ -38,13 +38,18 @@ bool AsyncFileWriter::ttyOutput() const {
void AsyncFileWriter::writeToFile( void AsyncFileWriter::writeToFile(
const std::vector<std::string>& ioQueue, const std::vector<std::string>& ioQueue,
size_t numDiscarded) { size_t numDiscarded) {
#ifndef _WIN32
// kNumIovecs controls the maximum number of strings we write at once in a // kNumIovecs controls the maximum number of strings we write at once in a
// single writev() call. // single writev() call.
constexpr int kNumIovecs = 64; constexpr int kNumIovecs = 64;
std::array<iovec, kNumIovecs> iovecs; std::array<iovec, kNumIovecs> iovecs;
#endif // !_WIN32
size_t idx = 0; size_t idx = 0;
while (idx < ioQueue.size()) { while (idx < ioQueue.size()) {
#ifndef _WIN32
// On POSIX platforms use writev() to minimize the number of system calls
// we use to write the data.
int numIovecs = 0; int numIovecs = 0;
while (numIovecs < kNumIovecs && idx < ioQueue.size()) { while (numIovecs < kNumIovecs && idx < ioQueue.size()) {
const auto& str = ioQueue[idx]; const auto& str = ioQueue[idx];
...@@ -55,7 +60,17 @@ void AsyncFileWriter::writeToFile( ...@@ -55,7 +60,17 @@ void AsyncFileWriter::writeToFile(
} }
auto ret = folly::writevFull(file_.fd(), iovecs.data(), numIovecs); auto ret = folly::writevFull(file_.fd(), iovecs.data(), numIovecs);
folly::checkUnixError(ret, "writevFull() failed");
#else // _WIN32
// On Windows folly's writevFull() function is just a wrapper that calls
// write() multiple times. Go ahead and do that ourselves here, since there
// is no point constructing the iovec data structure.
auto ret =
folly::writeFull(file_.fd(), ioQueue[idx].data(), ioQueue[idx].size());
folly::checkUnixError(ret, "writeFull() failed"); folly::checkUnixError(ret, "writeFull() failed");
CHECK_EQ(ret, ioQueue[idx].size());
++idx;
#endif // _WIN32
} }
if (numDiscarded > 0) { if (numDiscarded > 0) {
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment