Skip to content

Commit

Permalink
Store and display git hash used to compile QLever (ad-freiburg#734)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
joka921 authored Aug 13, 2022
1 parent ca7d8e7 commit 406ddab
Show file tree
Hide file tree
Showing 8 changed files with 92 additions and 18 deletions.
2 changes: 2 additions & 0 deletions .dockerignore
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
*
!.clang-format
!CMakeLists.txt
!CompilationInfo.cmake
!.git
!LICENSE
!README.md
!e2e
Expand Down
13 changes: 13 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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})
Expand Down
26 changes: 26 additions & 0 deletions CompilationInfo.cmake
Original file line number Diff line number Diff line change
@@ -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 <string>
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}")
17 changes: 17 additions & 0 deletions src/CompilationInfo.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
// Copyright 2022, University of Freiburg,
// Chair of Algorithms and Data Structures.
// Author: Johannes Kalmbach<joka921> ([email protected])

// Several constants. The values of these constants reside in the
// File `CompilationInfo.cpp` which is created and linked by CMake.

#pragma once
#include <string>
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
6 changes: 4 additions & 2 deletions src/ServerMain.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
#include <string>
#include <vector>

#include "./CompilationInfo.h"
#include "engine/Server.h"
#include "global/Constants.h"
#include "util/ProgramOptionsHelpers.h"
Expand Down Expand Up @@ -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<int>(numSimultaneousQueries),
Expand Down
2 changes: 1 addition & 1 deletion src/index/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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)
38 changes: 25 additions & 13 deletions src/index/Index.cpp
Original file line number Diff line number Diff line change
@@ -1,10 +1,22 @@
// Copyright 2014, University of Freiburg,
// Chair of Algorithms and Data Structures.
// Author: Björn Buchhold ([email protected])
// Authors:
// 2014-2017 Björn Buchhold ([email protected])
// 2018- Johannes Kalmbach ([email protected])

#include "./Index.h"

#include <CompilationInfo.h>
#include <absl/strings/str_join.h>
#include <index/PrefixHeuristic.h>
#include <index/TriplesView.h>
#include <index/VocabularyGenerator.h>
#include <parser/ParallelParseBuffer.h>
#include <util/BatchedPipeline.h>
#include <util/CompressionUsingZstd/ZstdWrapper.h>
#include <util/HashMap.h>
#include <util/Serializer/FileSerializer.h>
#include <util/TupleHelpers.h>

#include <algorithm>
#include <cmath>
Expand All @@ -15,17 +27,6 @@
#include <stxxl/map>
#include <unordered_map>

#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;

// _____________________________________________________________________________
Expand Down Expand Up @@ -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 =
Expand Down
6 changes: 4 additions & 2 deletions src/index/IndexBuilderMain.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
#include <string>
#include <unordered_set>

#include "../CompilationInfo.h"
#include "../global/Constants.h"
#include "../util/File.h"
#include "../util/ReadableNumberFact.h"
Expand Down Expand Up @@ -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;
Expand Down

0 comments on commit 406ddab

Please sign in to comment.