Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add causal profiling with coz #414

Merged
merged 3 commits into from
Apr 30, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ message(STATUS "compiler: " ${CMAKE_CXX_COMPILER_ID})
option(BUILD_CTL "Build drogon_ctl" ON)
option(BUILD_EXAMPLES "Build examples" ON)
option(BUILD_ORM "Build orm" ON)
option(COZ_PROFILING "Use coz for profiling" OFF)
option(LIBPQ_BATCH_MODE "Use batch mode for libpq" ON)
option(BUILD_DROGON_SHARED "Build drogon as a shared lib" OFF)

Expand Down Expand Up @@ -237,6 +238,16 @@ if(BUILD_CTL)
add_subdirectory(drogon_ctl)
endif(BUILD_CTL)

if(COZ_PROFILING)
find_package(coz-profiler REQUIRED)
target_compile_definitions(${PROJECT_NAME} PRIVATE -DCOZ_PROFILING=1)
# If linked will not need to be ran with `coz run --- [executable]` to run the
# profiler, but drogon_ctl currently won't build because it doesn't find debug
# information while trying to generate it's own sources
# target_link_libraries(${PROJECT_NAME} PUBLIC coz::coz)
target_include_directories(${PROJECT_NAME} PUBLIC ${COZ_INCLUDE_DIRS})
endif(COZ_PROFILING)

if(pg_FOUND OR MySQL_FOUND OR SQLite3_FOUND)
set(DROGON_SOURCES
${DROGON_SOURCES}
Expand Down Expand Up @@ -439,6 +450,7 @@ install(
"${CMAKE_CURRENT_SOURCE_DIR}/cmake_modules/FindMySQL.cmake"
"${CMAKE_CURRENT_SOURCE_DIR}/cmake_modules/Findpg.cmake"
"${CMAKE_CURRENT_SOURCE_DIR}/cmake_modules/FindBrotli.cmake"
"${CMAKE_CURRENT_SOURCE_DIR}/cmake_modules/Findcoz-profiler.cmake"
DESTINATION "${INSTALL_DROGON_CMAKE_DIR}"
COMPONENT dev)

Expand Down
3 changes: 3 additions & 0 deletions cmake/templates/DrogonConfig.cmake.in
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,9 @@ endif()
if(@Brotli_FOUND@)
find_dependency(Brotli)
endif()
if(@COZ-PROFILER_FOUND@)
find_dependency(coz-profiler)
endif()

# Our library dependencies (contains definitions for IMPORTED targets)

Expand Down
23 changes: 23 additions & 0 deletions cmake_modules/Findcoz-profiler.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
find_path(COZ_INCLUDE_DIRS NAMES coz.h)

find_library(COZ_LIBRARIES NAMES coz PATH_SUFFIXES coz-profiler)

include(FindPackageHandleStandardArgs)
find_package_handle_standard_args(coz-profiler
DEFAULT_MSG
COZ_LIBRARIES
COZ_INCLUDE_DIRS)

if(COZ-PROFILER_FOUND)
add_library(coz::coz INTERFACE IMPORTED)
set_target_properties(coz::coz
PROPERTIES INTERFACE_INCLUDE_DIRECTORIES
${COZ_INCLUDE_DIRS}
INTERFACE_LINK_LIBRARIES
${COZ_LIBRARIES})
else(COZ-PROFILER_FOUND)
set(COZ_LIBRARIES)
set(COZ_INCLUDE_DIRS)
endif(COZ-PROFILER_FOUND)

mark_as_advanced(COZ_INCLUDE_DIRS COZ_LIBRARIES)
16 changes: 16 additions & 0 deletions lib/src/HttpServer.cc
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,15 @@
#include <functional>
#include <trantor/utils/Logger.h>

#if COZ_PROFILING
#include <coz.h>
#else
#define COZ_PROGRESS
#define COZ_PROGRESS_NAMED(name)
#define COZ_BEGIN(name)
#define COZ_END(name)
#endif

using namespace std::placeholders;
using namespace drogon;
using namespace trantor;
Expand Down Expand Up @@ -258,6 +267,7 @@ void HttpServer::onMessage(const TcpConnectionPtr &conn, MsgBuffer *buf)
((HttpResponseImpl *)resp.get())
->renderToBuffer();
conn->send(httpString);
COZ_PROGRESS
}
},
wsConn);
Expand Down Expand Up @@ -495,16 +505,19 @@ void HttpServer::sendResponse(const TcpConnectionPtr &conn,
{
conn->sendFile(sendfileName.c_str());
}
COZ_PROGRESS
}
else
{
auto httpString = respImplPtr->renderHeaderForHeadMethod();
conn->send(std::move(*httpString));
COZ_PROGRESS
}

if (response->ifCloseConnection())
{
conn->shutdown();
COZ_PROGRESS
}
}

Expand Down Expand Up @@ -534,6 +547,7 @@ void HttpServer::sendResponses(
conn->send(buffer);
buffer.retrieveAll();
conn->sendFile(sendfileName.c_str());
COZ_PROGRESS
}
}
else
Expand All @@ -547,6 +561,7 @@ void HttpServer::sendResponses(
{
conn->send(buffer);
buffer.retrieveAll();
COZ_PROGRESS
}
conn->shutdown();
return;
Expand All @@ -555,6 +570,7 @@ void HttpServer::sendResponses(
if (conn->connected() && buffer.readableBytes() > 0)
{
conn->send(buffer);
COZ_PROGRESS
}
buffer.retrieveAll();
}