Skip to content

Commit

Permalink
debug: add hsgrpc_dmem flag
Browse files Browse the repository at this point in the history
  • Loading branch information
4eUeP committed Aug 31, 2023
1 parent 3b2be4b commit a140cc9
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 0 deletions.
39 changes: 39 additions & 0 deletions hs-grpc-server/cbits/hs_grpc_server.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,19 @@
#include <sanitizer/lsan_interface.h>
#endif

#ifdef HSGRPC_DMEM
static std::atomic_int bidistream_in_chan_counter = 0;
static std::atomic_int bidistream_out_chan_counter = 0;

void print_dmem(const char* fmt, ...) {
auto fmt_ = std::string("=== [DMEM] ") + std::string(fmt);
va_list args;
va_start(args, fmt);
vfprintf(stderr, fmt_.c_str(), args);
va_end(args);
}
#endif

namespace hsgrpc {
// ----------------------------------------------------------------------------

Expand Down Expand Up @@ -338,10 +351,36 @@ struct HsAsioHandler {
// function on the cpp side, another is the haskell side. Thus, the stored
// obj in the shared_ptr is freed while both handleBidiStreaming and haskell
// ForeignPtr are exited.
#ifdef HSGRPC_DMEM
auto channelIn_ =
new ChannelIn{co_await asio::this_coro::executor, max_buffer_size};
++bidistream_in_chan_counter;
print_dmem("new bidistream_in_chan_counter %p %d\n", channelIn_,
bidistream_in_chan_counter.load());
std::shared_ptr<ChannelIn> cpp_channel_in(channelIn_, [&](ChannelIn* ptr) {
delete ptr;
--bidistream_in_chan_counter;
print_dmem("delete bidistream_in_chan_counter %p %d\n", ptr,
bidistream_in_chan_counter.load());
});
auto channelOut_ =
new ChannelOut{co_await asio::this_coro::executor, max_buffer_size};
++bidistream_out_chan_counter;
print_dmem("new bidistream_out_chan_counter %p %d\n", channelOut_,
bidistream_out_chan_counter.load());
std::shared_ptr<ChannelOut> cpp_channel_out(
channelOut_, [&](ChannelOut* ptr) {
--bidistream_out_chan_counter;
print_dmem("delete bidistream_out_chan_counter %p %d\n", ptr,
bidistream_out_chan_counter.load());
delete ptr;
});
#else
auto cpp_channel_in = std::make_shared<ChannelIn>(
co_await asio::this_coro::executor, max_buffer_size);
auto cpp_channel_out = std::make_shared<ChannelOut>(
co_await asio::this_coro::executor, max_buffer_size);
#endif
auto hs_channel_in =
new channel_in_t{cpp_channel_in}; // delete by haskell gc
auto hs_channel_out =
Expand Down
8 changes: 8 additions & 0 deletions hs-grpc-server/hs-grpc-server.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,10 @@ flag hsgrpc_enable_asan
description:
Enable AddressSanitizer. See hs-grpc-example for an example usage.

flag hsgrpc_dmem
default: False
description: Some internal statistics and self-checks for memory usage.

common common-lang
default-language: Haskell2010
default-extensions:
Expand Down Expand Up @@ -126,6 +130,10 @@ library
-fsanitize=address -fno-omit-frame-pointer -DHSGRPC_ENABLE_ASAN
-static-libasan

if flag(hsgrpc_dmem)
cpp-options: -DHSGRPC_DMEM
cxx-options: -DHSGRPC_DMEM

test-suite hs-grpc-server-test
import: common-lang
type: exitcode-stdio-1.0
Expand Down

0 comments on commit a140cc9

Please sign in to comment.