aboutsummaryrefslogtreecommitdiffstats
path: root/util/system/direct_io.h
blob: 0fdde3a847d6eaac7d3832ea50e021857da111b6 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
#pragma once

#include "align.h"

#include "file.h"
#include <util/generic/buffer.h>

// Supports Linux Direct-IO:
// - Simple buffering logic.
// - Default buffer size of 128KB matches VM page writeback granularity, to maximize IO throughput.
// - Supports writing odd sized files by turning off direct IO for the last chunk.
class TDirectIOBufferedFile {
public:
    TDirectIOBufferedFile(const TString& path, EOpenMode oMode, size_t buflen = 1 << 17);
    ~TDirectIOBufferedFile();

    void FlushData();
    void Finish();
    size_t Read(void* buffer, size_t byteCount);
    void Write(const void* buffer, size_t byteCount);
    size_t Pread(void* buffer, size_t byteCount, ui64 offset);
    void Pwrite(const void* buffer, size_t byteCount, ui64 offset);

    inline bool IsOpen() const {
        return true;
    }

    inline ui64 GetWritePosition() const {
        return WritePosition;
    }

    inline ui64 GetLength() const {
        return FlushedBytes + DataLen;
    }

    inline FHANDLE GetHandle() {
        return File.GetHandle();
    }

    inline void FallocateNoResize(ui64 length) {
        File.FallocateNoResize(length);
    }

    inline void ShrinkToFit() {
        File.ShrinkToFit();
    }

private:
    inline bool IsAligned(i64 value) {
        return Alignment ? value == AlignDown<i64>(value, Alignment) : true; 
    }

    inline bool IsAligned(const void* value) {
        return Alignment ? value == AlignDown(value, Alignment) : true; 
    }

    size_t PreadSafe(void* buffer, size_t byteCount, ui64 offset);
    size_t ReadFromFile(void* buffer, size_t byteCount, ui64 offset);
    void WriteToFile(const void* buf, size_t len, ui64 position);
    void WriteToBuffer(const void* buf, size_t len, ui64 position);
    void SetDirectIO(bool value);

private:
    TFile File;
    size_t Alignment;
    size_t BufLen;
    size_t DataLen;
    void* Buffer;
    TBuffer BufferStorage;
    ui64 ReadPosition;
    ui64 WritePosition;
    ui64 FlushedBytes;
    ui64 FlushedToDisk;
    bool DirectIO;
};