CMakeLists.txt 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. # Smartbotic Database Service
  2. # Find OpenSSL for encryption
  3. find_package(OpenSSL REQUIRED)
  4. # Find optional LZ4 for compression
  5. find_package(PkgConfig)
  6. if(PKG_CONFIG_FOUND)
  7. pkg_check_modules(LZ4 QUIET liblz4)
  8. # v1.9.4 — jemalloc replaces glibc malloc for the smartbotic-database
  9. # binary. The fragmentation pattern from per-RPC nlohmann::json node
  10. # churn (107k 33-byte allocations observed on Zoe) defeats glibc's
  11. # malloc_trim because the trailing region of each arena always has a
  12. # live chunk; jemalloc's background-thread dirty-page decay sidesteps
  13. # the contiguity requirement and reclaims unused pages on a wallclock
  14. # timer regardless of allocation activity. Linked as a library so
  15. # the binary picks up jemalloc's malloc/free symbols at link time;
  16. # no LD_PRELOAD needed.
  17. pkg_check_modules(JEMALLOC QUIET jemalloc)
  18. endif()
  19. # Source files
  20. set(DATABASE_SERVICE_SOURCES
  21. src/main.cpp
  22. src/database_service.cpp
  23. src/database_grpc_impl.cpp
  24. src/memory_store.cpp
  25. src/persistence/persistence_manager.cpp
  26. src/persistence/wal.cpp
  27. src/persistence/snapshot.cpp
  28. src/persistence/history_store.cpp
  29. src/encryption/encryption_manager.cpp
  30. src/events/event_manager.cpp
  31. src/files/file_manager.cpp
  32. src/files/file_store.cpp
  33. src/replication/replication_manager.cpp
  34. src/replication/sync_protocol.cpp
  35. src/replication/conflict_resolver.cpp
  36. src/migrations/migration_runner.cpp
  37. src/views/projection.cpp
  38. src/views/view_manager.cpp
  39. src/config/collection_config_manager.cpp
  40. src/config/config_loader.cpp
  41. )
  42. # Create executable
  43. add_executable(smartbotic-database ${DATABASE_SERVICE_SOURCES})
  44. target_include_directories(smartbotic-database PRIVATE
  45. ${CMAKE_CURRENT_SOURCE_DIR}/src
  46. )
  47. # Link dependencies
  48. if(BUILD_SHARED_LIBS)
  49. target_link_libraries(smartbotic-database PRIVATE smartbotic-db-client)
  50. else()
  51. target_link_libraries(smartbotic-database PRIVATE smartbotic_db_proto)
  52. endif()
  53. target_link_libraries(smartbotic-database PRIVATE
  54. OpenSSL::SSL
  55. OpenSSL::Crypto
  56. )
  57. if(TARGET nlohmann_json::nlohmann_json)
  58. target_link_libraries(smartbotic-database PRIVATE nlohmann_json::nlohmann_json)
  59. else()
  60. target_include_directories(smartbotic-database PRIVATE ${NLOHMANN_JSON_INCLUDE_DIRS})
  61. endif()
  62. if(TARGET spdlog::spdlog)
  63. target_link_libraries(smartbotic-database PRIVATE spdlog::spdlog)
  64. else()
  65. target_link_libraries(smartbotic-database PRIVATE ${SPDLOG_LIBRARIES})
  66. target_include_directories(smartbotic-database PRIVATE ${SPDLOG_INCLUDE_DIRS})
  67. endif()
  68. # LZ4 compression support
  69. if(LZ4_FOUND)
  70. target_compile_definitions(smartbotic-database PRIVATE HAVE_LZ4)
  71. target_link_libraries(smartbotic-database PRIVATE ${LZ4_LIBRARIES})
  72. target_include_directories(smartbotic-database PRIVATE ${LZ4_INCLUDE_DIRS})
  73. endif()
  74. # v1.9.4 — jemalloc allocator (overrides glibc malloc/free)
  75. if(JEMALLOC_FOUND)
  76. target_compile_definitions(smartbotic-database PRIVATE HAVE_JEMALLOC)
  77. target_link_libraries(smartbotic-database PRIVATE ${JEMALLOC_LIBRARIES})
  78. target_include_directories(smartbotic-database PRIVATE ${JEMALLOC_INCLUDE_DIRS})
  79. # Position-sensitive: jemalloc must be linked early so its symbols win
  80. # over libc's. -Wl,-no-as-needed forces the linker to record the
  81. # dependency even though we don't reference any jemalloc symbol
  82. # directly (we just want its malloc to override).
  83. target_link_options(smartbotic-database PRIVATE -Wl,-no-as-needed)
  84. message(STATUS "Linking with jemalloc (${JEMALLOC_VERSION})")
  85. else()
  86. message(STATUS "jemalloc not found — falling back to glibc malloc (degraded RSS reclaim under load)")
  87. endif()
  88. # Systemd support
  89. if(SYSTEMD_FOUND)
  90. target_compile_definitions(smartbotic-database PRIVATE HAVE_SYSTEMD)
  91. target_link_libraries(smartbotic-database PRIVATE ${SYSTEMD_LIBRARIES})
  92. target_include_directories(smartbotic-database PRIVATE ${SYSTEMD_INCLUDE_DIRS})
  93. endif()
  94. # Installation
  95. install(TARGETS smartbotic-database
  96. RUNTIME DESTINATION bin
  97. )