2017-04-01 09:19:00 +00:00
|
|
|
#include <IO/createReadBufferFromFileBase.h>
|
|
|
|
#include <IO/ReadBufferFromFile.h>
|
2020-07-15 11:16:00 +00:00
|
|
|
#if defined(OS_LINUX) || defined(__FreeBSD__)
|
2017-04-01 09:19:00 +00:00
|
|
|
#include <IO/ReadBufferAIO.h>
|
2016-10-26 22:27:38 +00:00
|
|
|
#endif
|
2020-01-04 05:46:50 +00:00
|
|
|
#include <IO/MMapReadBufferFromFile.h>
|
2017-04-01 09:19:00 +00:00
|
|
|
#include <Common/ProfileEvents.h>
|
2015-03-30 22:10:59 +00:00
|
|
|
|
2016-10-24 02:02:37 +00:00
|
|
|
|
|
|
|
namespace ProfileEvents
|
|
|
|
{
|
2017-04-01 07:20:54 +00:00
|
|
|
extern const Event CreatedReadBufferOrdinary;
|
|
|
|
extern const Event CreatedReadBufferAIO;
|
2019-08-29 15:48:00 +00:00
|
|
|
extern const Event CreatedReadBufferAIOFailed;
|
2020-01-04 05:46:50 +00:00
|
|
|
extern const Event CreatedReadBufferMMap;
|
|
|
|
extern const Event CreatedReadBufferMMapFailed;
|
2016-10-24 02:02:37 +00:00
|
|
|
}
|
|
|
|
|
2015-03-30 22:10:59 +00:00
|
|
|
namespace DB
|
|
|
|
{
|
|
|
|
|
2020-01-04 05:46:50 +00:00
|
|
|
std::unique_ptr<ReadBufferFromFileBase> createReadBufferFromFileBase(
|
|
|
|
const std::string & filename_,
|
|
|
|
size_t estimated_size, size_t aio_threshold, size_t mmap_threshold,
|
|
|
|
size_t buffer_size_, int flags_, char * existing_memory_, size_t alignment)
|
2015-03-30 22:10:59 +00:00
|
|
|
{
|
2020-07-15 11:16:00 +00:00
|
|
|
#if defined(OS_LINUX) || defined(__FreeBSD__)
|
2019-08-29 15:48:00 +00:00
|
|
|
if (aio_threshold && estimated_size >= aio_threshold)
|
2017-04-01 07:20:54 +00:00
|
|
|
{
|
2019-08-29 15:48:00 +00:00
|
|
|
/// Attempt to open a file with O_DIRECT
|
|
|
|
try
|
|
|
|
{
|
|
|
|
auto res = std::make_unique<ReadBufferAIO>(filename_, buffer_size_, flags_, existing_memory_);
|
|
|
|
ProfileEvents::increment(ProfileEvents::CreatedReadBufferAIO);
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
catch (const ErrnoException &)
|
|
|
|
{
|
|
|
|
/// Fallback to cached IO if O_DIRECT is not supported.
|
|
|
|
ProfileEvents::increment(ProfileEvents::CreatedReadBufferAIOFailed);
|
|
|
|
}
|
2017-04-01 07:20:54 +00:00
|
|
|
}
|
2016-10-26 22:27:38 +00:00
|
|
|
#else
|
2019-08-29 15:48:00 +00:00
|
|
|
(void)aio_threshold;
|
|
|
|
(void)estimated_size;
|
2016-10-26 22:27:38 +00:00
|
|
|
#endif
|
2019-08-29 15:48:00 +00:00
|
|
|
|
2020-01-04 05:46:50 +00:00
|
|
|
if (!existing_memory_ && mmap_threshold && estimated_size >= mmap_threshold)
|
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
|
|
|
auto res = std::make_unique<MMapReadBufferFromFile>(filename_, 0);
|
|
|
|
ProfileEvents::increment(ProfileEvents::CreatedReadBufferMMap);
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
catch (const ErrnoException &)
|
|
|
|
{
|
|
|
|
/// Fallback if mmap is not supported (example: pipe).
|
|
|
|
ProfileEvents::increment(ProfileEvents::CreatedReadBufferMMapFailed);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-08-29 15:48:00 +00:00
|
|
|
ProfileEvents::increment(ProfileEvents::CreatedReadBufferOrdinary);
|
|
|
|
return std::make_unique<ReadBufferFromFile>(filename_, buffer_size_, flags_, existing_memory_, alignment);
|
2015-03-30 22:10:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|