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