From 406ddab3953b604f7f37e83307b8c3db5a3c04dd Mon Sep 17 00:00:00 2001 From: joka921 Date: Sun, 14 Aug 2022 00:48:35 +0200 Subject: [PATCH] Store and display git hash used to compile QLever (#734) Use CMake magic to let the QLever server and index builder know about the git hash of the compiled code. Show this git hash in the first line of the log when starting the server or the index builder. When building an index, store this git hash in the index metadata and display it when starting a server with that index. --- .dockerignore | 2 ++ CMakeLists.txt | 13 ++++++++++++ CompilationInfo.cmake | 26 +++++++++++++++++++++++ src/CompilationInfo.h | 17 +++++++++++++++ src/ServerMain.cpp | 6 ++++-- src/index/CMakeLists.txt | 2 +- src/index/Index.cpp | 38 ++++++++++++++++++++++------------ src/index/IndexBuilderMain.cpp | 6 ++++-- 8 files changed, 92 insertions(+), 18 deletions(-) create mode 100644 CompilationInfo.cmake create mode 100644 src/CompilationInfo.h diff --git a/.dockerignore b/.dockerignore index 0809a9920e..a9d91953ac 100644 --- a/.dockerignore +++ b/.dockerignore @@ -1,6 +1,8 @@ * !.clang-format !CMakeLists.txt +!CompilationInfo.cmake +!.git !LICENSE !README.md !e2e diff --git a/CMakeLists.txt b/CMakeLists.txt index be0af91423..354a2ac77a 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -206,6 +206,16 @@ message(STATUS ---) ############################################################################### ##### Actual project configuration ##### ############################################################################### + +include_directories(src) + +# Run the script `CompilationInfo.cmake` that creates the file `CompilationInfo.cpp` +# with the current git hash and the curent time and date. The first output which is +# never created is necessary s.t. the command is never cached and always rerun. +add_custom_command(OUTPUT "${CMAKE_CURRENT_BINARY_DIR}/FileThatNeverExists.cpp" + "${CMAKE_CURRENT_BINARY_DIR}/CompilationInfo.cpp" + COMMAND cmake -P ${CMAKE_CURRENT_SOURCE_DIR}/CompilationInfo.cmake) + set(LOG_LEVEL_FATAL 0) set(LOG_LEVEL_ERROR 1) set(LOG_LEVEL_WARN 2) @@ -234,6 +244,9 @@ configure_file(src/web/index.html index.html) configure_file(src/web/style.css style.css) configure_file(src/web/script.js script.js) +# Add the library with the constants declared in `CompilationInfo.h` and defined +# in `CompilationInfo.cpp` created by `CompilationInfo.cmake`. +add_library(compilationInfo ${CMAKE_CURRENT_BINARY_DIR}/CompilationInfo.cpp) add_executable(IndexBuilderMain src/index/IndexBuilderMain.cpp) target_link_libraries(IndexBuilderMain index ${CMAKE_THREAD_LIBS_INIT}) diff --git a/CompilationInfo.cmake b/CompilationInfo.cmake new file mode 100644 index 0000000000..27d3cc363b --- /dev/null +++ b/CompilationInfo.cmake @@ -0,0 +1,26 @@ +# A small cmake script that writes the current git hash and time to a +# .cpp file + +# Get the current time, remove the trailing newline and add quotes. +execute_process(COMMAND date OUTPUT_VARIABLE DATETIME_OF_COMPILATION) +string(REPLACE "\n" "" DATETIME_OF_COMPILATION "${DATETIME_OF_COMPILATION}") +set(DATETIME_OF_COMPILATION "\"${DATETIME_OF_COMPILATION}\"") +message(STATUS "DATETIME_OF_COMPILATION is ${DATETIME_OF_COMPILATION}" ) + +# Get the current git hash. +execute_process(COMMAND git log -1 --format="%H" OUTPUT_VARIABLE GIT_HASH) +if ((NOT DEFINED GIT_HASH) OR (GIT_HASH STREQUAL "")) + set(GIT_HASH "\"QLever compilation not taking place in a git repository\"") +endif() +message(STATUS "GIT_HASH is ${GIT_HASH}") + +# Write the .cpp file. +set(CONSTANTS "#include +namespace qlever::version { +const char* GitHash = ${GIT_HASH}; +const char* DatetimeOfCompilation = ${DATETIME_OF_COMPILATION}; +}") + +# For some reason `CMAKE_CURRENT_SOURCE_DIR` inside this script is +# `CMAKE_CURRENT_BINARY_DIR` in the main `CMakeLists.txt`. +file(WRITE ${CMAKE_CURRENT_SOURCE_DIR}/CompilationInfo.cpp "${CONSTANTS}") diff --git a/src/CompilationInfo.h b/src/CompilationInfo.h new file mode 100644 index 0000000000..566428126e --- /dev/null +++ b/src/CompilationInfo.h @@ -0,0 +1,17 @@ +// Copyright 2022, University of Freiburg, +// Chair of Algorithms and Data Structures. +// Author: Johannes Kalmbach (johannes.kalmbach@gmail.com) + +// Several constants. The values of these constants reside in the +// File `CompilationInfo.cpp` which is created and linked by CMake. + +#pragma once +#include +namespace qlever::version { +// The git hash of the commit that was used to QLever. +extern const char* GitHash; +// The date and time at which QLever was compiled. +extern const char* DatetimeOfCompilation; + +inline std::string GitShortHash() { return std::string{GitHash}.substr(0, 6); } +} // namespace qlever::version diff --git a/src/ServerMain.cpp b/src/ServerMain.cpp index a717e67c0b..2a91849ba9 100644 --- a/src/ServerMain.cpp +++ b/src/ServerMain.cpp @@ -8,6 +8,7 @@ #include #include +#include "./CompilationInfo.h" #include "engine/Server.h" #include "global/Constants.h" #include "util/ProgramOptionsHelpers.h" @@ -121,8 +122,9 @@ int main(int argc, char** argv) { return EXIT_FAILURE; } - LOG(INFO) << EMPH_ON << "QLever Server, compiled on " << __DATE__ << " " - << __TIME__ << EMPH_OFF << std::endl; + LOG(INFO) << EMPH_ON << "QLever Server, compiled on " + << qlever::version::DatetimeOfCompilation << " using git hash " + << qlever::version::GitShortHash() << EMPH_OFF << std::endl; try { Server server(port, static_cast(numSimultaneousQueries), diff --git a/src/index/CMakeLists.txt b/src/index/CMakeLists.txt index b34e2537da..288bfd1aad 100644 --- a/src/index/CMakeLists.txt +++ b/src/index/CMakeLists.txt @@ -15,4 +15,4 @@ add_library(index CompressedRelation.h CompressedRelation.cpp PatternCreator.h PatternCreator.cpp) -target_link_libraries(index parser vocabulary ${STXXL_LIBRARIES} ${ICU_LIBRARIES} absl::flat_hash_map absl::flat_hash_set zstd) +target_link_libraries(index parser vocabulary compilationInfo ${STXXL_LIBRARIES} ${ICU_LIBRARIES} absl::flat_hash_map absl::flat_hash_set zstd) diff --git a/src/index/Index.cpp b/src/index/Index.cpp index 4bffb0297b..5ae755dec2 100644 --- a/src/index/Index.cpp +++ b/src/index/Index.cpp @@ -1,10 +1,22 @@ // Copyright 2014, University of Freiburg, // Chair of Algorithms and Data Structures. -// Author: Björn Buchhold (buchhold@informatik.uni-freiburg.de) +// Authors: +// 2014-2017 Björn Buchhold (buchhold@informatik.uni-freiburg.de) +// 2018- Johannes Kalmbach (kalmbach@informatik.uni-freiburg.de) #include "./Index.h" +#include #include +#include +#include +#include +#include +#include +#include +#include +#include +#include #include #include @@ -15,17 +27,6 @@ #include #include -#include "../parser/ParallelParseBuffer.h" -#include "../util/BatchedPipeline.h" -#include "../util/CompressionUsingZstd/ZstdWrapper.h" -#include "../util/Conversions.h" -#include "../util/HashMap.h" -#include "../util/Serializer/FileSerializer.h" -#include "../util/TupleHelpers.h" -#include "./PrefixHeuristic.h" -#include "./VocabularyGenerator.h" -#include "TriplesView.h" - using std::array; // _____________________________________________________________________________ @@ -852,14 +853,25 @@ void Index::setPrefixCompression(bool compressed) { // ____________________________________________________________________________ void Index::writeConfiguration() const { + // Copy the configuration and add the current commit hash. + auto configuration = _configurationJson; + configuration["git_hash"] = std::string(qlever::version::GitHash); auto f = ad_utility::makeOfstream(_onDiskBase + CONFIGURATION_FILE); - f << _configurationJson; + f << configuration; } // ___________________________________________________________________________ void Index::readConfiguration() { auto f = ad_utility::makeIfstream(_onDiskBase + CONFIGURATION_FILE); f >> _configurationJson; + if (_configurationJson.find("git_hash") != _configurationJson.end()) { + LOG(INFO) << "The git hash used to build this index was " + << _configurationJson["git_hash"] << std::endl; + } else { + LOG(INFO) << "The index was built before git commit hashes were stored in " + "the index meta data" + << std::endl; + } if (_configurationJson.find("external-literals") != _configurationJson.end()) { _onDiskLiterals = diff --git a/src/index/IndexBuilderMain.cpp b/src/index/IndexBuilderMain.cpp index 319d56cb90..60ff67d5dc 100644 --- a/src/index/IndexBuilderMain.cpp +++ b/src/index/IndexBuilderMain.cpp @@ -13,6 +13,7 @@ #include #include +#include "../CompilationInfo.h" #include "../global/Constants.h" #include "../util/File.h" #include "../util/ReadableNumberFact.h" @@ -274,8 +275,9 @@ int main(int argc, char** argv) { exit(1); } - LOG(INFO) << EMPH_ON << "QLever IndexBuilder, compiled on " << __DATE__ << " " - << __TIME__ << EMPH_OFF << std::endl; + LOG(INFO) << EMPH_ON << "QLever IndexBuilder, compiled on " + << qlever::version::DatetimeOfCompilation << " using git hash " + << qlever::version::GitShortHash() << EMPH_OFF << std::endl; try { LOG(TRACE) << "Configuring STXXL..." << std::endl;