Commit b75576a1 authored by Yedidya Feldblum's avatar Yedidya Feldblum Committed by Facebook Github Bot

Apply clang-format to folly/system/

Summary: [Folly] Apply `clang-format` to `folly/system/`.

Reviewed By: igorsugak

Differential Revision: D9625730

fbshipit-source-id: 88f93231f77497d933233d4267c6983558baeb25
parent c5b81a8d
......@@ -45,9 +45,11 @@ static constexpr ssize_t kDefaultMlockChunkSize =
#endif // _MSC_VER
;
DEFINE_int64(mlock_chunk_size, kDefaultMlockChunkSize,
"Maximum bytes to mlock/munlock/munmap at once "
"(will be rounded up to PAGESIZE). Ignored if negative.");
DEFINE_int64(
mlock_chunk_size,
kDefaultMlockChunkSize,
"Maximum bytes to mlock/munlock/munmap at once "
"(will be rounded up to PAGESIZE). Ignored if negative.");
#ifndef MAP_POPULATE
#define MAP_POPULATE 0
......@@ -59,27 +61,36 @@ MemoryMapping::MemoryMapping(MemoryMapping&& other) noexcept {
swap(other);
}
MemoryMapping::MemoryMapping(File file, off_t offset, off_t length,
Options options)
: file_(std::move(file)),
options_(std::move(options)) {
MemoryMapping::MemoryMapping(
File file,
off_t offset,
off_t length,
Options options)
: file_(std::move(file)), options_(std::move(options)) {
CHECK(file_);
init(offset, length);
}
MemoryMapping::MemoryMapping(const char* name, off_t offset, off_t length,
Options options)
: MemoryMapping(File(name, options.writable ? O_RDWR : O_RDONLY),
offset,
length,
options) { }
MemoryMapping::MemoryMapping(int fd, off_t offset, off_t length,
Options options)
: MemoryMapping(File(fd), offset, length, options) { }
MemoryMapping::MemoryMapping(
const char* name,
off_t offset,
off_t length,
Options options)
: MemoryMapping(
File(name, options.writable ? O_RDWR : O_RDONLY),
offset,
length,
options) {}
MemoryMapping::MemoryMapping(
int fd,
off_t offset,
off_t length,
Options options)
: MemoryMapping(File(fd), offset, length, options) {}
MemoryMapping::MemoryMapping(AnonymousType, off_t length, Options options)
: options_(std::move(options)) {
: options_(std::move(options)) {
init(0, length);
}
......@@ -132,7 +143,7 @@ void MemoryMapping::init(off_t offset, off_t length) {
}
CHECK_GT(pageSize, 0);
CHECK_EQ(pageSize & (pageSize - 1), 0); // power of two
CHECK_EQ(pageSize & (pageSize - 1), 0); // power of two
CHECK_GE(offset, 0);
// Round down the start of the mapped region
......@@ -156,8 +167,8 @@ void MemoryMapping::init(off_t offset, off_t length) {
if (grow) {
if (!autoExtend) {
PCHECK(0 == ftruncate(file_.fd(), offset + length))
<< "ftruncate() failed, couldn't grow file to "
<< offset + length;
<< "ftruncate() failed, couldn't grow file to "
<< offset + length;
remaining = length;
} else {
// Extend mapping to multiple of page size, don't use ftruncate
......@@ -187,15 +198,15 @@ void MemoryMapping::init(off_t offset, off_t length) {
// The standard doesn't actually require PROT_NONE to be zero...
int prot = PROT_NONE;
if (options_.readable || options_.writable) {
prot = ((options_.readable ? PROT_READ : 0) |
(options_.writable ? PROT_WRITE : 0));
prot =
((options_.readable ? PROT_READ : 0) |
(options_.writable ? PROT_WRITE : 0));
}
unsigned char* start = static_cast<unsigned char*>(mmap(
options_.address, size_t(mapLength_), prot, flags, file_.fd(), offset));
PCHECK(start != MAP_FAILED)
<< " offset=" << offset
<< " length=" << mapLength_;
<< " offset=" << offset << " length=" << mapLength_;
mapStart_ = start;
data_.reset(start + skipStart, size_t(length));
}
......@@ -224,9 +235,12 @@ off_t memOpChunkSize(off_t length, off_t pageSize) {
* - success: true + amountSucceeded == bufSize (op success on whole buffer)
* - failure: false + amountSucceeded == nr bytes on which op succeeded.
*/
bool memOpInChunks(std::function<int(void*, size_t)> op,
void* mem, size_t bufSize, off_t pageSize,
size_t& amountSucceeded) {
bool memOpInChunks(
std::function<int(void*, size_t)> op,
void* mem,
size_t bufSize,
off_t pageSize,
size_t& amountSucceeded) {
// Linux' unmap/mlock/munlock take a kernel semaphore and block other threads
// from doing other memory operations. If the size of the buffer is big the
// semaphore can be down for seconds (for benchmarks see
......@@ -275,8 +289,12 @@ bool MemoryMapping::mlock(LockMode lock) {
}
// only part of the buffer was mlocked, unlock it back
if (!memOpInChunks(::munlock, mapStart_, amountSucceeded, options_.pageSize,
amountSucceeded)) {
if (!memOpInChunks(
::munlock,
mapStart_,
amountSucceeded,
options_.pageSize,
amountSucceeded)) {
PLOG(WARNING) << "munlock()";
}
......@@ -317,8 +335,8 @@ MemoryMapping::~MemoryMapping() {
size_t(mapLength_),
options_.pageSize,
amountSucceeded)) {
PLOG(FATAL) << folly::format("munmap({}) failed at {}",
mapLength_, amountSucceeded);
PLOG(FATAL) << folly::format(
"munmap({}) failed at {}", mapLength_, amountSucceeded);
}
}
}
......@@ -329,9 +347,8 @@ void MemoryMapping::advise(int advice) const {
void MemoryMapping::advise(int advice, size_t offset, size_t length) const {
CHECK_LE(offset + length, size_t(mapLength_))
<< " offset: " << offset
<< " length: " << length
<< " mapLength_: " << mapLength_;
<< " offset: " << offset << " length: " << length
<< " mapLength_: " << mapLength_;
// Include the entire start page: round down to page boundary.
const auto offMisalign = offset % options_.pageSize;
......@@ -366,7 +383,9 @@ void MemoryMapping::swap(MemoryMapping& other) noexcept {
swap(this->data_, other.data_);
}
void swap(MemoryMapping& a, MemoryMapping& b) noexcept { a.swap(b); }
void swap(MemoryMapping& a, MemoryMapping& b) noexcept {
a.swap(b);
}
void alignedForwardMemcpy(void* dst, const void* src, size_t size) {
assert(reinterpret_cast<uintptr_t>(src) % alignof(unsigned long) == 0);
......@@ -399,9 +418,10 @@ void mmapFileCopy(const char* src, const char* dest, mode_t mode) {
off_t(srcMap.range().size()),
MemoryMapping::writable());
alignedForwardMemcpy(destMap.writableRange().data(),
srcMap.range().data(),
srcMap.range().size());
alignedForwardMemcpy(
destMap.writableRange().data(),
srcMap.range().data(),
srcMap.range().size());
}
} // namespace folly
......@@ -38,7 +38,7 @@ class MemoryMapping : boost::noncopyable {
*/
enum class LockMode {
TRY_LOCK,
MUST_LOCK
MUST_LOCK,
};
/**
* Map a portion of the file indicated by filename in memory, causing a CHECK
......@@ -55,12 +55,30 @@ class MemoryMapping : boost::noncopyable {
Options() {}
// Convenience methods; return *this for chaining.
Options& setPageSize(off_t v) { pageSize = v; return *this; }
Options& setShared(bool v) { shared = v; return *this; }
Options& setPrefault(bool v) { prefault = v; return *this; }
Options& setReadable(bool v) { readable = v; return *this; }
Options& setWritable(bool v) { writable = v; return *this; }
Options& setGrow(bool v) { grow = v; return *this; }
Options& setPageSize(off_t v) {
pageSize = v;
return *this;
}
Options& setShared(bool v) {
shared = v;
return *this;
}
Options& setPrefault(bool v) {
prefault = v;
return *this;
}
Options& setReadable(bool v) {
readable = v;
return *this;
}
Options& setWritable(bool v) {
writable = v;
return *this;
}
Options& setGrow(bool v) {
grow = v;
return *this;
}
// Page size. 0 = use appropriate page size.
// (On Linux, we use a huge page size if the file is on a hugetlbfs
......@@ -103,28 +121,31 @@ class MemoryMapping : boost::noncopyable {
}
enum AnonymousType {
kAnonymous
kAnonymous,
};
/**
* Create an anonymous mapping.
*/
MemoryMapping(AnonymousType, off_t length, Options options=Options());
explicit MemoryMapping(File file,
off_t offset=0,
off_t length=-1,
Options options=Options());
explicit MemoryMapping(const char* name,
off_t offset=0,
off_t length=-1,
Options options=Options());
explicit MemoryMapping(int fd,
off_t offset=0,
off_t length=-1,
Options options=Options());
MemoryMapping(AnonymousType, off_t length, Options options = Options());
explicit MemoryMapping(
File file,
off_t offset = 0,
off_t length = -1,
Options options = Options());
explicit MemoryMapping(
const char* name,
off_t offset = 0,
off_t length = -1,
Options options = Options());
explicit MemoryMapping(
int fd,
off_t offset = 0,
off_t length = -1,
Options options = Options());
MemoryMapping(MemoryMapping&&) noexcept;
......@@ -165,9 +186,8 @@ class MemoryMapping : boost::noncopyable {
template <class T>
Range<const T*> asRange() const {
size_t count = data_.size() / sizeof(T);
return Range<const T*>(static_cast<const T*>(
static_cast<const void*>(data_.data())),
count);
return Range<const T*>(
static_cast<const T*>(static_cast<const void*>(data_.data())), count);
}
/**
......@@ -183,18 +203,16 @@ class MemoryMapping : boost::noncopyable {
*/
template <class T>
Range<T*> asWritableRange() const {
DCHECK(options_.writable); // you'll segfault anyway...
DCHECK(options_.writable); // you'll segfault anyway...
size_t count = data_.size() / sizeof(T);
return Range<T*>(static_cast<T*>(
static_cast<void*>(data_.data())),
count);
return Range<T*>(static_cast<T*>(static_cast<void*>(data_.data())), count);
}
/**
* A range of mutable bytes mapped by this mapping.
*/
MutableByteRange writableRange() const {
DCHECK(options_.writable); // you'll segfault anyway...
DCHECK(options_.writable); // you'll segfault anyway...
return data_;
}
......@@ -210,7 +228,9 @@ class MemoryMapping : boost::noncopyable {
return locked_;
}
int fd() const { return file_.fd(); }
int fd() const {
return file_.fd();
}
private:
MemoryMapping();
......
......@@ -71,15 +71,16 @@ constexpr detail::ShellCmdFormat operator"" _shellify(
} // namespace literals
/**
* Create argument array for `Subprocess()` for a process running in a
* shell.
*
* The shell to use is always going to be `/bin/sh`.
*
* This is deprecated in favour of the user-defined-literal `_shellify`
* from namespace `folly::shell_literals` because that requires that the format
* string is a compile-time constant which can be inspected during code reviews
*/
* Create argument array for `Subprocess()` for a process running in a
* shell.
*
* The shell to use is always going to be `/bin/sh`.
*
* This is deprecated in favour of the user-defined-literal `_shellify`
* from namespace `folly::shell_literals` because that requires that the format
* string is a compile-time constant which can be inspected during code reviews
*/
// clang-format off
template <typename... Arguments>
[[deprecated(
"Use `\"command {} {} ...\"_shellify(argument1, argument2 ...)` from "
......@@ -89,5 +90,6 @@ std::vector<std::string> shellify(
Arguments&&... arguments) {
return detail::shellify(format, std::forward<Arguments>(arguments)...);
}
// clang-format on
} // namespace folly
......@@ -154,7 +154,7 @@ TEST(MemoryMapping, ZeroLength) {
TEST(MemoryMapping, Advise) {
File f = File::temporary();
size_t kPageSize = 4096;
size_t size = kPageSize + 10; // unaligned file size
size_t size = kPageSize + 10; // unaligned file size
PCHECK(ftruncateNoInt(f.fd(), size) == 0) << size;
MemoryMapping m(File(f.fd()));
......
......@@ -39,7 +39,9 @@ TEST(ThreadName, getCurrentThreadName) {
EXPECT_EQ(kThreadName.toString(), *getCurrentThreadName());
}
});
SCOPE_EXIT { th.join(); };
SCOPE_EXIT {
th.join();
};
}
#if FOLLY_HAVE_PTHREAD
......@@ -48,13 +50,17 @@ TEST(ThreadName, setThreadName_other_pthread) {
Baton<> let_thread_end;
pthread_t handle;
thread th([&] {
handle = pthread_self();
handle_set.post();
let_thread_end.wait();
handle = pthread_self();
handle_set.post();
let_thread_end.wait();
});
SCOPE_EXIT { th.join(); };
SCOPE_EXIT {
th.join();
};
handle_set.wait();
SCOPE_EXIT { let_thread_end.post(); };
SCOPE_EXIT {
let_thread_end.post();
};
EXPECT_EQ(
expectedSetOtherThreadNameResult, setThreadName(handle, kThreadName));
}
......@@ -62,11 +68,13 @@ TEST(ThreadName, setThreadName_other_pthread) {
TEST(ThreadName, setThreadName_other_id) {
Baton<> let_thread_end;
thread th([&] {
let_thread_end.wait();
});
SCOPE_EXIT { th.join(); };
SCOPE_EXIT { let_thread_end.post(); };
thread th([&] { let_thread_end.wait(); });
SCOPE_EXIT {
th.join();
};
SCOPE_EXIT {
let_thread_end.post();
};
EXPECT_EQ(
expectedSetOtherThreadNameResult,
setThreadName(th.get_id(), kThreadName));
......
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