1#include <IO/createWriteBufferFromFileBase.h>
2#include <IO/WriteBufferFromFile.h>
3#if defined(__linux__) || defined(__FreeBSD__)
4#include <IO/WriteBufferAIO.h>
5#endif
6#include <Common/ProfileEvents.h>
7
8
9namespace ProfileEvents
10{
11 extern const Event CreatedWriteBufferOrdinary;
12 extern const Event CreatedWriteBufferAIO;
13 extern const Event CreatedWriteBufferAIOFailed;
14}
15
16namespace DB
17{
18
19std::unique_ptr<WriteBufferFromFileBase> createWriteBufferFromFileBase(const std::string & filename_, size_t estimated_size,
20 size_t aio_threshold, size_t buffer_size_, int flags_, mode_t mode, char * existing_memory_,
21 size_t alignment)
22{
23#if defined(__linux__) || defined(__FreeBSD__)
24 if (aio_threshold && estimated_size >= aio_threshold)
25 {
26 /// Attempt to open a file with O_DIRECT
27 try
28 {
29 auto res = std::make_unique<WriteBufferAIO>(filename_, buffer_size_, flags_, mode, existing_memory_);
30 ProfileEvents::increment(ProfileEvents::CreatedWriteBufferAIO);
31 return res;
32 }
33 catch (const ErrnoException &)
34 {
35 /// Fallback to cached IO if O_DIRECT is not supported.
36 ProfileEvents::increment(ProfileEvents::CreatedWriteBufferAIOFailed);
37 }
38 }
39#else
40 (void)aio_threshold;
41 (void)estimated_size;
42#endif
43
44 ProfileEvents::increment(ProfileEvents::CreatedWriteBufferOrdinary);
45 return std::make_unique<WriteBufferFromFile>(filename_, buffer_size_, flags_, mode, existing_memory_, alignment);
46}
47
48}
49