2015-01-10 02:30:03 +00:00
|
|
|
#include <Poco/DirectoryIterator.h>
|
2017-04-01 09:19:00 +00:00
|
|
|
#include <Common/ClickHouseRevision.h>
|
2015-10-05 00:33:43 +00:00
|
|
|
#include <ext/unlock_guard.hpp>
|
2015-01-10 02:30:03 +00:00
|
|
|
|
2017-04-01 09:19:00 +00:00
|
|
|
#include <Common/SipHash.h>
|
|
|
|
#include <Common/ShellCommand.h>
|
|
|
|
#include <Common/StringUtils.h>
|
2015-01-10 02:30:03 +00:00
|
|
|
|
2017-04-01 09:19:00 +00:00
|
|
|
#include <IO/Operators.h>
|
|
|
|
#include <IO/ReadBufferFromString.h>
|
|
|
|
#include <IO/ReadBufferFromFileDescriptor.h>
|
|
|
|
#include <IO/WriteBufferFromFile.h>
|
2015-01-10 02:30:03 +00:00
|
|
|
|
2017-04-01 09:19:00 +00:00
|
|
|
#include <Interpreters/Compiler.h>
|
|
|
|
#include <Interpreters/config_compile.h>
|
2015-01-10 02:30:03 +00:00
|
|
|
|
|
|
|
|
2016-10-24 02:02:37 +00:00
|
|
|
namespace ProfileEvents
|
|
|
|
{
|
2017-04-01 07:20:54 +00:00
|
|
|
extern const Event CompileAttempt;
|
|
|
|
extern const Event CompileSuccess;
|
2016-10-24 02:02:37 +00:00
|
|
|
}
|
|
|
|
|
2015-01-10 02:30:03 +00:00
|
|
|
namespace DB
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
|
|
Compiler::Compiler(const std::string & path_, size_t threads)
|
2017-04-01 07:20:54 +00:00
|
|
|
: path(path_), pool(threads)
|
2015-01-10 02:30:03 +00:00
|
|
|
{
|
2017-04-01 07:20:54 +00:00
|
|
|
Poco::File(path).createDirectory();
|
|
|
|
|
|
|
|
Poco::DirectoryIterator dir_end;
|
|
|
|
for (Poco::DirectoryIterator dir_it(path); dir_end != dir_it; ++dir_it)
|
|
|
|
{
|
|
|
|
std::string name = dir_it.name();
|
|
|
|
if (endsWith(name, ".so"))
|
|
|
|
{
|
|
|
|
files.insert(name.substr(0, name.size() - 3));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
LOG_INFO(log, "Having " << files.size() << " compiled files from previous start.");
|
2015-01-10 02:30:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Compiler::~Compiler()
|
|
|
|
{
|
2017-04-01 07:20:54 +00:00
|
|
|
LOG_DEBUG(log, "Waiting for threads to finish.");
|
|
|
|
pool.wait();
|
2015-01-10 02:30:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static Compiler::HashedKey getHash(const std::string & key)
|
|
|
|
{
|
2017-04-01 07:20:54 +00:00
|
|
|
SipHash hash;
|
2015-01-10 02:30:03 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
auto revision = ClickHouseRevision::get();
|
|
|
|
hash.update(reinterpret_cast<const char *>(&revision), sizeof(revision));
|
|
|
|
hash.update(key.data(), key.size());
|
2015-01-10 02:30:03 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
Compiler::HashedKey res;
|
|
|
|
hash.get128(res.first, res.second);
|
|
|
|
return res;
|
2015-01-10 02:30:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-04-02 17:37:49 +00:00
|
|
|
/// Without .so extension.
|
2015-01-10 02:30:03 +00:00
|
|
|
static std::string hashedKeyToFileName(Compiler::HashedKey hashed_key)
|
|
|
|
{
|
2017-04-01 07:20:54 +00:00
|
|
|
std::string file_name;
|
2015-01-10 02:30:03 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
{
|
|
|
|
WriteBufferFromString out(file_name);
|
|
|
|
out << hashed_key.first << '_' << hashed_key.second;
|
|
|
|
}
|
2015-01-10 02:30:03 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
return file_name;
|
2015-01-10 02:30:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-01-11 02:00:26 +00:00
|
|
|
SharedLibraryPtr Compiler::getOrCount(
|
2017-04-01 07:20:54 +00:00
|
|
|
const std::string & key,
|
|
|
|
UInt32 min_count_to_compile,
|
|
|
|
const std::string & additional_compiler_flags,
|
|
|
|
CodeGenerator get_code,
|
|
|
|
ReadyCallback on_ready)
|
2015-01-10 02:30:03 +00:00
|
|
|
{
|
2017-04-01 07:20:54 +00:00
|
|
|
HashedKey hashed_key = getHash(key);
|
|
|
|
|
|
|
|
std::lock_guard<std::mutex> lock(mutex);
|
|
|
|
|
|
|
|
UInt32 count = ++counts[hashed_key];
|
|
|
|
|
2017-04-02 17:37:49 +00:00
|
|
|
/// Is there a ready open library? Or, if the library is in the process of compiling, there will be nullptr.
|
2017-04-01 07:20:54 +00:00
|
|
|
Libraries::iterator it = libraries.find(hashed_key);
|
|
|
|
if (libraries.end() != it)
|
|
|
|
{
|
|
|
|
if (!it->second)
|
|
|
|
LOG_INFO(log, "Library " << hashedKeyToFileName(hashed_key) << " is already compiling or compilation was failed.");
|
|
|
|
|
2017-04-02 17:37:49 +00:00
|
|
|
/// TODO In this case, after the compilation is finished, the callback will not be called.
|
2017-04-01 07:20:54 +00:00
|
|
|
|
|
|
|
return it->second;
|
|
|
|
}
|
|
|
|
|
2017-04-02 17:37:49 +00:00
|
|
|
/// Is there a file with the library left over from the previous launch?
|
2017-04-01 07:20:54 +00:00
|
|
|
std::string file_name = hashedKeyToFileName(hashed_key);
|
|
|
|
if (files.count(file_name))
|
|
|
|
{
|
|
|
|
std::string so_file_path = path + '/' + file_name + ".so";
|
|
|
|
LOG_INFO(log, "Loading existing library " << so_file_path);
|
|
|
|
|
|
|
|
SharedLibraryPtr lib(new SharedLibrary(so_file_path));
|
|
|
|
libraries[hashed_key] = lib;
|
|
|
|
return lib;
|
|
|
|
}
|
|
|
|
|
2017-04-02 17:37:49 +00:00
|
|
|
/// Has min_count_to_compile been reached?
|
2017-04-01 07:20:54 +00:00
|
|
|
if (count >= min_count_to_compile)
|
|
|
|
{
|
2017-04-02 17:37:49 +00:00
|
|
|
/// The min_count_to_compile value of zero indicates the need for synchronous compilation.
|
2017-04-01 07:20:54 +00:00
|
|
|
|
2017-04-02 17:37:49 +00:00
|
|
|
/// Are there any free threads?
|
2017-04-01 07:20:54 +00:00
|
|
|
if (min_count_to_compile == 0 || pool.active() < pool.size())
|
|
|
|
{
|
2017-04-02 17:37:49 +00:00
|
|
|
/// Indicates that the library is in the process of compiling.
|
2017-04-01 07:20:54 +00:00
|
|
|
libraries[hashed_key] = nullptr;
|
|
|
|
|
|
|
|
LOG_INFO(log, "Compiling code " << file_name << ", key: " << key);
|
|
|
|
|
|
|
|
if (min_count_to_compile == 0)
|
|
|
|
{
|
|
|
|
{
|
|
|
|
ext::unlock_guard<std::mutex> unlock(mutex);
|
|
|
|
compile(hashed_key, file_name, additional_compiler_flags, get_code, on_ready);
|
|
|
|
}
|
|
|
|
|
|
|
|
return libraries[hashed_key];
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
pool.schedule([=]
|
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
|
|
|
compile(hashed_key, file_name, additional_compiler_flags, get_code, on_ready);
|
|
|
|
}
|
|
|
|
catch (...)
|
|
|
|
{
|
|
|
|
tryLogCurrentException("Compiler");
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
LOG_INFO(log, "All threads are busy.");
|
|
|
|
}
|
|
|
|
|
|
|
|
return nullptr;
|
2015-01-10 02:30:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-01-18 01:18:39 +00:00
|
|
|
void Compiler::compile(
|
2017-04-01 07:20:54 +00:00
|
|
|
HashedKey hashed_key,
|
|
|
|
std::string file_name,
|
|
|
|
const std::string & additional_compiler_flags,
|
|
|
|
CodeGenerator get_code,
|
|
|
|
ReadyCallback on_ready)
|
2015-01-10 02:30:03 +00:00
|
|
|
{
|
2017-04-01 07:20:54 +00:00
|
|
|
ProfileEvents::increment(ProfileEvents::CompileAttempt);
|
|
|
|
|
|
|
|
std::string prefix = path + "/" + file_name;
|
|
|
|
std::string cpp_file_path = prefix + ".cpp";
|
|
|
|
std::string so_file_path = prefix + ".so";
|
|
|
|
std::string so_tmp_file_path = prefix + ".so.tmp";
|
|
|
|
|
|
|
|
{
|
|
|
|
WriteBufferFromFile out(cpp_file_path);
|
|
|
|
out << get_code();
|
|
|
|
}
|
|
|
|
|
|
|
|
std::stringstream command;
|
|
|
|
|
2017-04-02 17:37:49 +00:00
|
|
|
/// Slightly uncomfortable.
|
2017-04-01 07:20:54 +00:00
|
|
|
command <<
|
|
|
|
"LD_LIBRARY_PATH=" PATH_SHARE "/clickhouse/bin/"
|
|
|
|
" " INTERNAL_COMPILER_EXECUTABLE
|
|
|
|
" -B " PATH_SHARE "/clickhouse/bin/"
|
|
|
|
" " INTERNAL_COMPILER_FLAGS
|
2017-03-02 13:34:43 +00:00
|
|
|
#if INTERNAL_COMPILER_CUSTOM_ROOT
|
2017-04-01 07:20:54 +00:00
|
|
|
" -isystem " INTERNAL_COMPILER_HEADERS_ROOT "/usr/local/include/"
|
|
|
|
" -isystem " INTERNAL_COMPILER_HEADERS_ROOT "/usr/include/"
|
|
|
|
" -isystem " INTERNAL_COMPILER_HEADERS_ROOT "/usr/include/c++/*/"
|
|
|
|
" -isystem " INTERNAL_COMPILER_HEADERS_ROOT "/usr/include/x86_64-linux-gnu/"
|
|
|
|
" -isystem " INTERNAL_COMPILER_HEADERS_ROOT "/usr/include/x86_64-linux-gnu/c++/*/"
|
|
|
|
" -isystem " INTERNAL_COMPILER_HEADERS_ROOT "/usr/local/lib/clang/*/include/"
|
2017-03-02 13:34:43 +00:00
|
|
|
#endif
|
2017-04-01 10:25:45 +00:00
|
|
|
" -I " INTERNAL_COMPILER_HEADERS "/dbms/src/"
|
2017-04-01 07:20:54 +00:00
|
|
|
" -I " INTERNAL_COMPILER_HEADERS "/contrib/libcityhash/include/"
|
|
|
|
" -I " INTERNAL_DOUBLE_CONVERSION_INCLUDE_DIR
|
|
|
|
" -I " INTERNAL_Poco_Foundation_INCLUDE_DIR
|
|
|
|
" -I " INTERNAL_Boost_INCLUDE_DIRS
|
|
|
|
" -I " INTERNAL_COMPILER_HEADERS "/libs/libcommon/include/"
|
|
|
|
" " << additional_compiler_flags <<
|
|
|
|
" -o " << so_tmp_file_path << " " << cpp_file_path
|
|
|
|
<< " 2>&1 || echo Exit code: $?";
|
|
|
|
|
|
|
|
std::string compile_result;
|
|
|
|
|
|
|
|
{
|
|
|
|
auto process = ShellCommand::execute(command.str());
|
|
|
|
readStringUntilEOF(compile_result, process->out);
|
|
|
|
process->wait();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!compile_result.empty())
|
|
|
|
throw Exception("Cannot compile code:\n\n" + command.str() + "\n\n" + compile_result);
|
|
|
|
|
2017-04-02 17:37:49 +00:00
|
|
|
/// If there was an error before, the file with the code remains for viewing.
|
2017-04-01 07:20:54 +00:00
|
|
|
Poco::File(cpp_file_path).remove();
|
|
|
|
|
|
|
|
Poco::File(so_tmp_file_path).renameTo(so_file_path);
|
|
|
|
SharedLibraryPtr lib(new SharedLibrary(so_file_path));
|
|
|
|
|
|
|
|
{
|
|
|
|
std::lock_guard<std::mutex> lock(mutex);
|
|
|
|
libraries[hashed_key] = lib;
|
|
|
|
}
|
|
|
|
|
|
|
|
LOG_INFO(log, "Compiled code " << file_name);
|
|
|
|
ProfileEvents::increment(ProfileEvents::CompileSuccess);
|
|
|
|
|
|
|
|
on_ready(lib);
|
2015-01-10 02:30:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
}
|