aboutsummaryrefslogtreecommitdiffstats
path: root/util/system/direct_io.cpp
diff options
context:
space:
mode:
authorAnton Samokhvalov <pg83@yandex.ru>2022-02-10 16:45:15 +0300
committerDaniil Cherednik <dcherednik@yandex-team.ru>2022-02-10 16:45:15 +0300
commit72cb13b4aff9bc9cf22e49251bc8fd143f82538f (patch)
treeda2c34829458c7d4e74bdfbdf85dff449e9e7fb8 /util/system/direct_io.cpp
parent778e51ba091dc39e7b7fcab2b9cf4dbedfb6f2b5 (diff)
downloadydb-72cb13b4aff9bc9cf22e49251bc8fd143f82538f.tar.gz
Restoring authorship annotation for Anton Samokhvalov <pg83@yandex.ru>. Commit 1 of 2.
Diffstat (limited to 'util/system/direct_io.cpp')
-rw-r--r--util/system/direct_io.cpp102
1 files changed, 51 insertions, 51 deletions
diff --git a/util/system/direct_io.cpp b/util/system/direct_io.cpp
index f59c54b0cb..38b5e7ba28 100644
--- a/util/system/direct_io.cpp
+++ b/util/system/direct_io.cpp
@@ -3,42 +3,42 @@
#include <util/generic/singleton.h>
#include <util/generic/yexception.h>
#include <util/system/info.h>
-#include "align.h"
+#include "align.h"
#ifdef _linux_
- #include <util/string/cast.h>
- #include <linux/version.h>
- #include <sys/utsname.h>
+ #include <util/string/cast.h>
+ #include <linux/version.h>
+ #include <sys/utsname.h>
#endif
-
+
namespace {
- struct TAlignmentCalcer {
- inline TAlignmentCalcer()
+ struct TAlignmentCalcer {
+ inline TAlignmentCalcer()
: Alignment(0)
{
#ifdef _linux_
utsname sysInfo;
-
+
Y_VERIFY(!uname(&sysInfo), "Error while call uname: %s", LastSystemErrorText());
-
+
TStringBuf release(sysInfo.release);
release = release.substr(0, release.find_first_not_of(".0123456789"));
-
+
int v1 = FromString<int>(release.NextTok('.'));
int v2 = FromString<int>(release.NextTok('.'));
int v3 = FromString<int>(release.NextTok('.'));
int linuxVersionCode = KERNEL_VERSION(v1, v2, v3);
-
- if (linuxVersionCode < KERNEL_VERSION(2, 4, 10)) {
+
+ if (linuxVersionCode < KERNEL_VERSION(2, 4, 10)) {
Alignment = 0;
- } else if (linuxVersionCode < KERNEL_VERSION(2, 6, 0)) {
+ } else if (linuxVersionCode < KERNEL_VERSION(2, 6, 0)) {
Alignment = NSystemInfo::GetPageSize();
- } else {
+ } else {
// Default alignment used to be 512, but most modern devices rely on 4k physical blocks.
// 4k alignment works well for both 512 and 4k blocks and doesn't require 512e support in the kernel.
// See IGNIETFERRO-946.
Alignment = 4096;
- }
+ }
#endif
}
@@ -59,7 +59,7 @@ TDirectIOBufferedFile::TDirectIOBufferedFile(const TString& path, EOpenMode oMod
}
if (oMode & Direct) {
- Alignment = Singleton<TAlignmentCalcer>()->Alignment;
+ Alignment = Singleton<TAlignmentCalcer>()->Alignment;
SetDirectIO(true);
}
@@ -75,16 +75,16 @@ TDirectIOBufferedFile::TDirectIOBufferedFile(const TString& path, EOpenMode oMod
void TDirectIOBufferedFile::SetDirectIO(bool value) {
#ifdef _linux_
- if (DirectIO == value) {
+ if (DirectIO == value) {
return;
- }
-
- if (!!Alignment && value) {
+ }
+
+ if (!!Alignment && value) {
(void)fcntl(File.GetHandle(), F_SETFL, fcntl(File.GetHandle(), F_GETFL) | DIRECT_IO_FLAGS);
- } else {
+ } else {
(void)fcntl(File.GetHandle(), F_SETFL, fcntl(File.GetHandle(), F_GETFL) & ~DIRECT_IO_FLAGS);
- }
-
+ }
+
DirectIO = value;
#else
DirectIO = value;
@@ -103,7 +103,7 @@ void TDirectIOBufferedFile::FlushData() {
DataLen = 0;
File.FlushData();
}
-
+
void TDirectIOBufferedFile::Finish() {
FlushData();
File.Flush();
@@ -118,15 +118,15 @@ void TDirectIOBufferedFile::Write(const void* buffer, size_t byteCount) {
void TDirectIOBufferedFile::WriteToBuffer(const void* buf, size_t len, ui64 position) {
while (len > 0) {
size_t writeLen = Min<size_t>(BufLen - position, len);
-
+
if (writeLen > 0) {
memcpy((char*)Buffer + position, buf, writeLen);
buf = (char*)buf + writeLen;
len -= writeLen;
- DataLen = (size_t)Max(position + writeLen, (ui64)DataLen);
+ DataLen = (size_t)Max(position + writeLen, (ui64)DataLen);
position += writeLen;
}
-
+
if (DataLen == BufLen) {
WriteToFile(Buffer, DataLen, FlushedBytes);
DataLen = 0;
@@ -138,9 +138,9 @@ void TDirectIOBufferedFile::WriteToBuffer(const void* buf, size_t len, ui64 posi
void TDirectIOBufferedFile::WriteToFile(const void* buf, size_t len, ui64 position) {
if (!!len) {
SetDirectIO(IsAligned(buf) && IsAligned(len) && IsAligned(position));
-
+
File.Pwrite(buf, len, position);
-
+
FlushedBytes = Max(FlushedBytes, position + len);
FlushedToDisk = Min(FlushedToDisk, position);
}
@@ -157,11 +157,11 @@ size_t TDirectIOBufferedFile::PreadSafe(void* buffer, size_t byteCount, ui64 off
do {
bytesRead = pread(File.GetHandle(), buffer, byteCount, offset);
} while (bytesRead == -1 && errno == EINTR);
-
+
if (bytesRead < 0) {
ythrow yexception() << "error while pread file: " << LastSystemError() << "(" << LastSystemErrorText() << ")";
- }
-
+ }
+
return bytesRead;
#else
return File.Pread(buffer, byteCount, offset);
@@ -186,29 +186,29 @@ size_t TDirectIOBufferedFile::ReadFromFile(void* buffer, size_t byteCount, ui64
} else {
break;
}
- }
+ }
if (!byteCount) {
return bytesRead;
}
-
+
ui64 bufSize = AlignUp(Min<size_t>(BufferStorage.Size(), byteCount + (Alignment << 1)), Alignment);
TBuffer readBufferStorage(bufSize + Alignment);
char* readBuffer = AlignUp((char*)readBufferStorage.Data(), Alignment);
-
+
while (byteCount) {
- ui64 begin = AlignDown(offset, (ui64)Alignment);
- ui64 end = AlignUp(offset + byteCount, (ui64)Alignment);
+ ui64 begin = AlignDown(offset, (ui64)Alignment);
+ ui64 end = AlignUp(offset + byteCount, (ui64)Alignment);
ui64 toRead = Min(end - begin, bufSize);
ui64 fromFile = PreadSafe(readBuffer, toRead, begin);
-
- if (!fromFile) {
+
+ if (!fromFile) {
break;
- }
-
+ }
+
ui64 delta = offset - begin;
ui64 count = Min<ui64>(fromFile - delta, byteCount);
-
+
memcpy(buffer, readBuffer + delta, count);
buffer = (char*)buffer + count;
byteCount -= count;
@@ -225,23 +225,23 @@ size_t TDirectIOBufferedFile::Read(void* buffer, size_t byteCount) {
}
size_t TDirectIOBufferedFile::Pread(void* buffer, size_t byteCount, ui64 offset) {
- if (!byteCount) {
+ if (!byteCount) {
return 0;
- }
-
+ }
+
size_t readFromFile = 0;
if (offset < FlushedBytes) {
readFromFile = Min<ui64>(byteCount, FlushedBytes - offset);
size_t bytesRead = ReadFromFile(buffer, readFromFile, offset);
if (bytesRead != readFromFile || readFromFile == byteCount) {
return bytesRead;
- }
+ }
}
ui64 start = offset > FlushedBytes ? offset - FlushedBytes : 0;
ui64 count = Min<ui64>(DataLen - start, byteCount - readFromFile);
- if (count) {
+ if (count) {
memcpy((char*)buffer + readFromFile, (const char*)Buffer + start, count);
- }
+ }
return count + readFromFile;
}
@@ -249,16 +249,16 @@ void TDirectIOBufferedFile::Pwrite(const void* buffer, size_t byteCount, ui64 of
if (offset > WritePosition) {
ythrow yexception() << "cannot frite to position" << offset;
}
-
+
size_t writeToBufer = byteCount;
size_t writeToFile = 0;
-
+
if (FlushedBytes > offset) {
writeToFile = Min<ui64>(byteCount, FlushedBytes - offset);
WriteToFile(buffer, writeToFile, offset);
writeToBufer -= writeToFile;
}
-
+
if (writeToBufer > 0) {
ui64 bufferOffset = offset + writeToFile - FlushedBytes;
WriteToBuffer((const char*)buffer + writeToFile, writeToBufer, bufferOffset);