Skip to content

Commit

Permalink
1) add detect_tcmalloc, so pika could link tcmalloc dynamically; 2) c…
Browse files Browse the repository at this point in the history
…hange memory cmd name to tcmalloc;
  • Loading branch information
KernelMaker committed May 24, 2017
1 parent 5f0d2b5 commit 4f6c214
Show file tree
Hide file tree
Showing 7 changed files with 69 additions and 18 deletions.
11 changes: 8 additions & 3 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,8 @@ OBJECT = pika
SRC_DIR = ./src
THIRD_PATH = ./third
OUTPUT = ./output

dummy := $(shell ("$(CURDIR)/detect_tcmalloc" "$(CURDIR)/make_config.mk"))
include make_config.mk

INCLUDE_PATH = -I./include/ \
-I./src/ \
Expand Down Expand Up @@ -61,8 +62,10 @@ LIBS = -lpthread \
-lz \
-lbz2 \
-lsnappy \
-lrt \
-ltcmalloc
-lrt

LIBS += $(TCMALLOC_LDFLAGS)
CXXFLAGS += $(TCMALLOC_EXTENSION_FLAGS)

NEMO = $(THIRD_PATH)/nemo/output/lib/libnemo.a
GLOG = $(SO_DIR)/libglog.so.0
Expand Down Expand Up @@ -130,11 +133,13 @@ clean:
rm -rf $(SRC_DIR)/*.o
rm -rf $(OUTPUT)/*
rm -rf $(OUTPUT)
rm -rf $(CURDIR)/make_config.mk

distclean:
rm -rf $(SRC_DIR)/*.o
rm -rf $(OUTPUT)/*
rm -rf $(OUTPUT)
rm -rf $(CURDIR)/make_config.mk
make distclean -C $(THIRD_PATH)/nemo/3rdparty/nemo-rocksdb/
make clean -C $(THIRD_PATH)/nemo/
make clean -C $(THIRD_PATH)/pink/
Expand Down
36 changes: 36 additions & 0 deletions detect_tcmalloc
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
#!/bin/sh

OUTPUT=$1
if test -z "$OUTPUT"; then
echo "usage: $0 <output-filename>" >&2
exit 1
fi

# Delete existing output, if it exists
rm -f "$OUTPUT"
touch "$OUTPUT"

if test -z "$CXX"; then
CXX=g++
fi

# Test whether tcmalloc is available
if echo 'int main() {}' | $CXX $CFLAGS -x c++ - -o /dev/null \
-ltcmalloc 2>/dev/null; then
TCMALLOC_LDFLAGS=" -ltcmalloc"
fi

# Test whether malloc_extension is available
$CXX $CFLAGS -x c++ - -o /dev/null -ltcmalloc 2>/dev/null <<EOF
#include <gperftools/malloc_extension.h>
int main() {
MallocExtension::instance()->Initialize();;
return 0;
}
EOF
if [ "$?" = 0 ]; then
TCMALLOC_EXTENSION_FLAGS=" -DTCMALLOC_EXTENSION"
fi

echo "TCMALLOC_EXTENSION_FLAGS=$TCMALLOC_EXTENSION_FLAGS" >> "$OUTPUT"
echo "TCMALLOC_LDFLAGS=$TCMALLOC_LDFLAGS" >> "$OUTPUT"
7 changes: 4 additions & 3 deletions include/pika_admin.h
Original file line number Diff line number Diff line change
Expand Up @@ -228,10 +228,10 @@ class DbsizeCmd : public Cmd {
private:
virtual void DoInitial(PikaCmdArgsType &argv, const CmdInfo* const ptr_info);
};

class MemoryCmd : public Cmd {
#ifdef TCMALLOC_EXTENSION
class TcmallocCmd : public Cmd {
public:
MemoryCmd() {
TcmallocCmd() {
}
virtual void Do();
private:
Expand All @@ -240,3 +240,4 @@ class MemoryCmd : public Cmd {
virtual void DoInitial(PikaCmdArgsType &argv, const CmdInfo* const ptr_info);
};
#endif
#endif
4 changes: 3 additions & 1 deletion include/pika_command.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,9 @@ const std::string kCmdNameInfo = "info";
const std::string kCmdNameConfig = "config";
const std::string kCmdNameMonitor = "monitor";
const std::string kCmdNameDbsize = "dbsize";
const std::string kCmdNameMemory = "memory";
#ifdef TCMALLOC_EXTENSION
const std::string kCmdNameTcmalloc = "tcmalloc";
#endif

//Migrate slot
const std::string kCmdNameSlotsMgrtSlot = "slotsmgrtslot";
Expand Down
2 changes: 1 addition & 1 deletion include/pika_define.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

#define PIKA_MAX_WORKER_THREAD_NUM 24

const std::string kPikaVersion = "2.2.1";
const std::string kPikaVersion = "2.2.2";
const std::string kPikaPidFile = "pika.pid";

struct WorkerCronTask {
Expand Down
15 changes: 9 additions & 6 deletions src/pika_admin.cc
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,9 @@
#include "pika_slot.h"

#include <sys/utsname.h>
#ifdef TCMALLOC_EXTENSION
#include <gperftools/malloc_extension.h>
#endif

extern PikaServer *g_pika_server;
extern PikaConf *g_pika_conf;
Expand Down Expand Up @@ -1176,11 +1178,11 @@ void DbsizeCmd::Do() {
int32_t dbsize = key_nums_v[0] + key_nums_v[1] + key_nums_v[2] + key_nums_v[3] + key_nums_v[4];
res_.AppendInteger(dbsize);
}

void MemoryCmd::DoInitial(PikaCmdArgsType &argv, const CmdInfo* const ptr_info) {
#ifdef TCMALLOC_EXTENSION
void TcmallocCmd::DoInitial(PikaCmdArgsType &argv, const CmdInfo* const ptr_info) {
(void)ptr_info;
if (argv.size() != 2 && argv.size() != 3) {
res_.SetRes(CmdRes::kWrongNum, kCmdNameMemory);
res_.SetRes(CmdRes::kWrongNum, kCmdNameTcmalloc);
return;
}
rate_ = 0;
Expand All @@ -1191,21 +1193,21 @@ void MemoryCmd::DoInitial(PikaCmdArgsType &argv, const CmdInfo* const ptr_info)
type_ = 1;
if (argv.size() == 3) {
if (!slash::string2l(argv[2].data(), argv[2].size(), &rate_)) {
res_.SetRes(CmdRes::kSyntaxErr, kCmdNameMemory);
res_.SetRes(CmdRes::kSyntaxErr, kCmdNameTcmalloc);
}
}
} else if (type == "list") {
type_ = 2;
} else if (type == "free") {
type_ = 3;
} else {
res_.SetRes(CmdRes::kInvalidParameter, kCmdNameMemory);
res_.SetRes(CmdRes::kInvalidParameter, kCmdNameTcmalloc);
return;
}

}

void MemoryCmd::Do() {
void TcmallocCmd::Do() {
std::vector<MallocExtension::FreeListInfo> fli;
std::vector<std::string> elems;
switch(type_) {
Expand Down Expand Up @@ -1237,3 +1239,4 @@ void MemoryCmd::Do() {
res_.SetRes(CmdRes::kOk);
}
}
#endif
12 changes: 8 additions & 4 deletions src/pika_command.cc
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,10 @@ void InitCmdInfoTable() {
cmd_infos.insert(std::pair<std::string, CmdInfo*>(kCmdNameMonitor, monitorptr));
CmdInfo* dbsizeptr = new CmdInfo(kCmdNameDbsize, 1, kCmdFlagsRead | kCmdFlagsAdmin);
cmd_infos.insert(std::pair<std::string, CmdInfo*>(kCmdNameDbsize, dbsizeptr));
CmdInfo* memoryptr = new CmdInfo(kCmdNameMemory, -2, kCmdFlagsRead | kCmdFlagsAdmin);
cmd_infos.insert(std::pair<std::string, CmdInfo*>(kCmdNameMemory, memoryptr));
#ifdef TCMALLOC_EXTENSION
CmdInfo* tcmallocptr = new CmdInfo(kCmdNameTcmalloc, -2, kCmdFlagsRead | kCmdFlagsAdmin);
cmd_infos.insert(std::pair<std::string, CmdInfo*>(kCmdNameTcmalloc, tcmallocptr));
#endif


//migrate slot
Expand Down Expand Up @@ -463,8 +465,10 @@ void InitCmdTable(std::unordered_map<std::string, Cmd*> *cmd_table) {
cmd_table->insert(std::pair<std::string, Cmd*>(kCmdNameMonitor, monitorptr));
Cmd* dbsizeptr = new DbsizeCmd();
cmd_table->insert(std::pair<std::string, Cmd*>(kCmdNameDbsize, dbsizeptr));
Cmd* memoryptr = new MemoryCmd();
cmd_table->insert(std::pair<std::string, Cmd*>(kCmdNameMemory, memoryptr));
#ifdef TCMALLOC_EXTENSION
Cmd* tcmallocptr = new TcmallocCmd();
cmd_table->insert(std::pair<std::string, Cmd*>(kCmdNameTcmalloc, tcmallocptr));
#endif

//migrate slot
Cmd* slotmgrtslotptr = new SlotsMgrtTagSlotCmd();
Expand Down

0 comments on commit 4f6c214

Please sign in to comment.