diff options
author | iddqd <iddqd@yandex-team.ru> | 2022-02-10 16:49:45 +0300 |
---|---|---|
committer | Daniil Cherednik <dcherednik@yandex-team.ru> | 2022-02-10 16:49:45 +0300 |
commit | 07fce9c5f7771600d0b3d70e1f88fd8a7e164d85 (patch) | |
tree | e4aa4750fbb864d70f8c06cf03d2750e979ea3bf /util/system/direct_io.cpp | |
parent | af42068bf6cd93c976b80dd0388fa48cdf65da11 (diff) | |
download | ydb-07fce9c5f7771600d0b3d70e1f88fd8a7e164d85.tar.gz |
Restoring authorship annotation for <iddqd@yandex-team.ru>. Commit 1 of 2.
Diffstat (limited to 'util/system/direct_io.cpp')
-rw-r--r-- | util/system/direct_io.cpp | 256 |
1 files changed, 128 insertions, 128 deletions
diff --git a/util/system/direct_io.cpp b/util/system/direct_io.cpp index f59c54b0cbd..3ace0bfc573 100644 --- a/util/system/direct_io.cpp +++ b/util/system/direct_io.cpp @@ -1,36 +1,36 @@ -#include "direct_io.h" - -#include <util/generic/singleton.h> -#include <util/generic/yexception.h> +#include "direct_io.h" + +#include <util/generic/singleton.h> +#include <util/generic/yexception.h> #include <util/system/info.h> #include "align.h" - -#ifdef _linux_ + +#ifdef _linux_ #include <util/string/cast.h> #include <linux/version.h> #include <sys/utsname.h> -#endif +#endif -namespace { +namespace { struct TAlignmentCalcer { inline TAlignmentCalcer() - : Alignment(0) - { -#ifdef _linux_ - utsname sysInfo; + : 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")); + 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); + 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)) { - Alignment = 0; + Alignment = 0; } else if (linuxVersionCode < KERNEL_VERSION(2, 6, 0)) { Alignment = NSystemInfo::GetPageSize(); } else { @@ -39,44 +39,44 @@ namespace { // See IGNIETFERRO-946. Alignment = 4096; } -#endif - } - - size_t Alignment; - }; -} - +#endif + } + + size_t Alignment; + }; +} + TDirectIOBufferedFile::TDirectIOBufferedFile(const TString& path, EOpenMode oMode, size_t buflen /*= 1 << 17*/) - : File(path, oMode) - , Alignment(0) - , DataLen(0) - , ReadPosition(0) - , WritePosition(0) - , DirectIO(false) -{ + : File(path, oMode) + , Alignment(0) + , DataLen(0) + , ReadPosition(0) + , WritePosition(0) + , DirectIO(false) +{ if (buflen == 0) { ythrow TFileError() << "unbuffered usage is not supported"; - } - - if (oMode & Direct) { + } + + if (oMode & Direct) { Alignment = Singleton<TAlignmentCalcer>()->Alignment; - SetDirectIO(true); - } - - WritePosition = File.GetLength(); - FlushedBytes = WritePosition; - FlushedToDisk = FlushedBytes; - BufLen = (!!Alignment) ? AlignUp(buflen, Alignment) : buflen; - BufferStorage.Resize(BufLen + Alignment); - Buffer = (!!Alignment) ? AlignUp(BufferStorage.Data(), Alignment) : BufferStorage.Data(); -} - -#define DIRECT_IO_FLAGS (O_DIRECT | O_SYNC) - -void TDirectIOBufferedFile::SetDirectIO(bool value) { -#ifdef _linux_ + SetDirectIO(true); + } + + WritePosition = File.GetLength(); + FlushedBytes = WritePosition; + FlushedToDisk = FlushedBytes; + BufLen = (!!Alignment) ? AlignUp(buflen, Alignment) : buflen; + BufferStorage.Resize(BufLen + Alignment); + Buffer = (!!Alignment) ? AlignUp(BufferStorage.Data(), Alignment) : BufferStorage.Data(); +} + +#define DIRECT_IO_FLAGS (O_DIRECT | O_SYNC) + +void TDirectIOBufferedFile::SetDirectIO(bool value) { +#ifdef _linux_ if (DirectIO == value) { - return; + return; } if (!!Alignment && value) { @@ -85,73 +85,73 @@ void TDirectIOBufferedFile::SetDirectIO(bool value) { (void)fcntl(File.GetHandle(), F_SETFL, fcntl(File.GetHandle(), F_GETFL) & ~DIRECT_IO_FLAGS); } + DirectIO = value; +#else DirectIO = value; -#else - DirectIO = value; -#endif -} - -TDirectIOBufferedFile::~TDirectIOBufferedFile() { +#endif +} + +TDirectIOBufferedFile::~TDirectIOBufferedFile() { try { Finish(); } catch (...) { } -} - -void TDirectIOBufferedFile::FlushData() { +} + +void TDirectIOBufferedFile::FlushData() { WriteToFile(Buffer, DataLen, FlushedBytes); DataLen = 0; File.FlushData(); -} +} -void TDirectIOBufferedFile::Finish() { +void TDirectIOBufferedFile::Finish() { FlushData(); File.Flush(); File.Close(); -} - +} + void TDirectIOBufferedFile::Write(const void* buffer, size_t byteCount) { WriteToBuffer(buffer, byteCount, DataLen); WritePosition += 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; +} + +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); - position += writeLen; - } - - if (DataLen == BufLen) { - WriteToFile(Buffer, DataLen, FlushedBytes); - DataLen = 0; - position = 0; - } - } -} - -void TDirectIOBufferedFile::WriteToFile(const void* buf, size_t len, ui64 position) { - if (!!len) { - SetDirectIO(IsAligned(buf) && IsAligned(len) && IsAligned(position)); + position += writeLen; + } + + if (DataLen == BufLen) { + WriteToFile(Buffer, DataLen, FlushedBytes); + DataLen = 0; + position = 0; + } + } +} + +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); - } -} - + FlushedBytes = Max(FlushedBytes, position + len); + FlushedToDisk = Min(FlushedToDisk, position); + } +} + size_t TDirectIOBufferedFile::PreadSafe(void* buffer, size_t byteCount, ui64 offset) { - if (FlushedToDisk < offset + byteCount) { - File.FlushData(); - FlushedToDisk = FlushedBytes; - } - + if (FlushedToDisk < offset + byteCount) { + File.FlushData(); + FlushedToDisk = FlushedBytes; + } + #ifdef _linux_ ssize_t bytesRead = 0; do { @@ -159,15 +159,15 @@ size_t TDirectIOBufferedFile::PreadSafe(void* buffer, size_t byteCount, ui64 off } while (bytesRead == -1 && errno == EINTR); if (bytesRead < 0) { - ythrow yexception() << "error while pread file: " << LastSystemError() << "(" << LastSystemErrorText() << ")"; + ythrow yexception() << "error while pread file: " << LastSystemError() << "(" << LastSystemErrorText() << ")"; } return bytesRead; #else return File.Pread(buffer, byteCount, offset); #endif -} - +} + size_t TDirectIOBufferedFile::ReadFromFile(void* buffer, size_t byteCount, ui64 offset) { SetDirectIO(true); @@ -187,64 +187,64 @@ size_t TDirectIOBufferedFile::ReadFromFile(void* buffer, size_t byteCount, ui64 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); + 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) { + while (byteCount) { 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) { - break; + break; } ui64 delta = offset - begin; ui64 count = Min<ui64>(fromFile - delta, byteCount); - memcpy(buffer, readBuffer + delta, count); - buffer = (char*)buffer + count; - byteCount -= count; - offset += count; + memcpy(buffer, readBuffer + delta, count); + buffer = (char*)buffer + count; + byteCount -= count; + offset += count; bytesRead += count; - } + } return bytesRead; -} - +} + size_t TDirectIOBufferedFile::Read(void* buffer, size_t byteCount) { size_t bytesRead = Pread(buffer, byteCount, ReadPosition); ReadPosition += bytesRead; return bytesRead; -} - +} + size_t TDirectIOBufferedFile::Pread(void* buffer, size_t byteCount, ui64 offset) { if (!byteCount) { - return 0; + return 0; } size_t readFromFile = 0; - if (offset < FlushedBytes) { - readFromFile = Min<ui64>(byteCount, FlushedBytes - offset); + 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 start = offset > FlushedBytes ? offset - FlushedBytes : 0; ui64 count = Min<ui64>(DataLen - start, byteCount - readFromFile); if (count) { - memcpy((char*)buffer + readFromFile, (const char*)Buffer + start, count); + memcpy((char*)buffer + readFromFile, (const char*)Buffer + start, count); } - return count + readFromFile; -} - + return count + readFromFile; +} + void TDirectIOBufferedFile::Pwrite(const void* buffer, size_t byteCount, ui64 offset) { if (offset > WritePosition) { ythrow yexception() << "cannot frite to position" << offset; @@ -262,5 +262,5 @@ void TDirectIOBufferedFile::Pwrite(const void* buffer, size_t byteCount, ui64 of if (writeToBufer > 0) { ui64 bufferOffset = offset + writeToFile - FlushedBytes; WriteToBuffer((const char*)buffer + writeToFile, writeToBufer, bufferOffset); - } -} + } +} |