Skip to content

Commit

Permalink
refactor: mv submodule slash to pika/src
Browse files Browse the repository at this point in the history
  • Loading branch information
liuzhen committed Nov 28, 2022
1 parent 6d8a604 commit b2a9cce
Show file tree
Hide file tree
Showing 57 changed files with 8,118 additions and 2 deletions.
5 changes: 3 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
*.obj
*pb.cc
*pb.h
build

# Precompiled Headers
*.gch
Expand All @@ -21,8 +22,7 @@
*.mod

# Compiled Static libraries
*.lai
*.la
*.lai *.la
*.a
*.lib

Expand All @@ -49,6 +49,7 @@ tags
# IDE
.vscode

# generate
make_config.mk
src/*.d
src/build_version.cc
10 changes: 10 additions & 0 deletions src/slash/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
cmake_minimum_required (VERSION 3.0)

set (CMAKE_CXX_STANDARD 11)
project (slash)

aux_source_directory(./src DIR_SRCS)

add_library(slash STATIC ${DIR_SRCS} )

target_include_directories(slash PUBLIC ${PROJECT_SOURCE_DIR}/..)
185 changes: 185 additions & 0 deletions src/slash/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,185 @@
CLEAN_FILES = # deliberately empty, so we can append below.
CXX=g++
LDFLAGS= -lpthread -lrt
CXXFLAGS= -g -std=c++11 -fno-builtin-memcmp -msse -msse4.2 -pipe -fPIC
PROFILING_FLAGS= -pg
ARFLAGS = rs
OPT=

# Set the default DEBUG_LEVEL to 0
DEBUG_LEVEL?=0

ifeq ($(MAKECMDGOALS),dbg)
DEBUG_LEVEL=2 # compatible with rocksdb
endif

# compile with -O2 if for release
# if we're compiling for release, compile without debug code (-DNDEBUG) and
# don't treat warnings as errors
ifeq ($(DEBUG_LEVEL),0)
DISABLE_WARNING_AS_ERROR=1
OPT += -O2 -fno-omit-frame-pointer -DNDEBUG
else
$(warning Warning: Compiling in debug mode. Don't use the resulting binary in production)
OPT += -O0 -D__XDEBUG__ -D_GNU_SOURCE $(PROFILING_FLAGS)
endif

#-----------------------------------------------

SRC_DIR=src
VERSION_CC=$(SRC_DIR)/build_version.cc
LIB_SOURCES := $(VERSION_CC) \
$(filter-out $(VERSION_CC), $(wildcard $(SRC_DIR)/*.cc))

AM_DEFAULT_VERBOSITY = 0

AM_V_GEN = $(am__v_GEN_$(V))
am__v_GEN_ = $(am__v_GEN_$(AM_DEFAULT_VERBOSITY))
am__v_GEN_0 = @echo " GEN " $@;
am__v_GEN_1 =
AM_V_at = $(am__v_at_$(V))
am__v_at_ = $(am__v_at_$(AM_DEFAULT_VERBOSITY))
am__v_at_0 = @
am__v_at_1 =

AM_V_CXX = $(am__v_CXX_$(V))
am__v_CXX_ = $(am__v_CXX_$(AM_DEFAULT_VERBOSITY))
am__v_CXX_0 = @echo " CXX " $@;
am__v_CXX_1 =
LD = $(CXX)
AM_V_LD = $(am__v_LD_$(V))
am__v_LD_ = $(am__v_LD_$(AM_DEFAULT_VERBOSITY))
am__v_LD_0 = @echo " LD " $@;
am__v_LD_1 =
AM_V_AR = $(am__v_AR_$(V))
am__v_AR_ = $(am__v_AR_$(AM_DEFAULT_VERBOSITY))
am__v_AR_0 = @echo " AR " $@;
am__v_AR_1 =

AM_LINK = $(AM_V_LD)$(CXX) $^ $(EXEC_LDFLAGS) -o $@ $(LDFLAGS)

# This (the first rule) must depend on "all".
default: all

WARNING_FLAGS = -W -Wextra -Wall -Wsign-compare \
-Wno-unused-parameter -Wno-redundant-decls -Wwrite-strings \
-Wpointer-arith -Wreorder -Wswitch -Wsign-promo \
-Woverloaded-virtual -Wnon-virtual-dtor -Wno-missing-field-initializers

ifndef DISABLE_WARNING_AS_ERROR
WARNING_FLAGS += -Werror
endif

CXXFLAGS += $(WARNING_FLAGS) -I.. $(OPT)

date := $(shell date +%F)
git_sha := $(shell git rev-parse HEAD 2>/dev/null)
gen_build_version = sed -e s/@@GIT_SHA@@/$(git_sha)/ -e s/@@GIT_DATE_TIME@@/$(date)/ src/build_version.cc.in
# Record the version of the source that we are compiling.
# We keep a record of the git revision in this file. It is then built
# as a regular source file as part of the compilation process.
# One can run "strings executable_filename | grep _build_" to find
# the version of the source that we used to build the executable file.
CLEAN_FILES += src/build_version.cc

FORCE:
src/build_version.cc: FORCE
$(AM_V_GEN)rm -f $@-t
$(AM_V_at)$(gen_build_version) > $@-t
$(AM_V_at)if test -f $@; then \
cmp -s $@-t $@ && rm -f $@-t || mv -f $@-t $@; \
else mv -f $@-t $@; fi

LIBOBJECTS = $(LIB_SOURCES:.cc=.o)

# if user didn't config LIBNAME, set the default
ifeq ($(LIBNAME),)
# we should only run slash in production with DEBUG_LEVEL 0
ifeq ($(DEBUG_LEVEL),0)
LIBNAME=libslash
else
LIBNAME=libslash_debug
endif
endif
LIBOUTPUT = lib
dummy := $(shell mkdir -p $(LIBOUTPUT))
LIBRARY = $(LIBOUTPUT)/${LIBNAME}.a

TESTS = slash_string_test slash_binlog_test slash_coding_test base_conf_test \
slash_env_test

EXAMPLES = conf_example cond_lock_example binlog_example mutex_example hash_example

.PHONY: clean dbg static_lib all check example

all: $(LIBRARY)

static_lib: $(LIBRARY)

example: $(LIBRARY) $(EXAMPLES)

check: $(LIBRARY) $(TESTS)
for t in $(notdir $(TESTS)); do echo "***** Running $$t"; ./$$t || exit 1; done

dbg: $(LIBRARY) $(EXAMPLES)

$(LIBRARY): $(LIBOBJECTS)
$(AM_V_AR)rm -f $@
$(AM_V_at)$(AR) $(ARFLAGS) $@ $(LIBOBJECTS)

%.o : %.cc
$(AM_V_CXX)$(CXX) $(CXXFLAGS) -c $< -o $@

%.d: %.cc
@set -e; rm -f $@; $(CXX) -MM $(CXXFLAGS) $< > $@.$$$$; \
sed 's,\($(notdir $*)\)\.o[ :]*,$(SRC_DIR)/\1.o $@ : ,g' < $@.$$$$ > $@; \
rm -f $@.$$$$

ifneq ($(MAKECMDGOALS),clean)
-include $(LIBOBJECTS:.o=.d)
endif

clean:
make -C ./examples clean
rm -f $(TESTS) $(EXAMPLES)
rm -f $(LIBRARY)
rm -rf $(CLEAN_FILES)
rm -rf $(LIBOUTPUT)
find . -name "*.[oda]*" -exec rm -f {} \;
find . -type f -regex ".*\.\(\(gcda\)\|\(gcno\)\)" -exec rm {} \;

# tests

TEST_MAIN = tests/test_main.o

slash_string_test: tests/slash_string_test.o $(TEST_MAIN) $(LIBOBJECTS)
$(AM_LINK)

slash_binlog_test: tests/slash_binlog_test.o $(TEST_MAIN) $(LIBOBJECTS)
$(AM_LINK)

base_conf_test: tests/base_conf_test.o $(TEST_MAIN) $(LIBOBJECTS)
$(AM_LINK)

slash_coding_test: tests/slash_coding_test.o $(TEST_MAIN) $(LIBOBJECTS)
$(AM_LINK)

slash_env_test: tests/slash_env_test.o $(TEST_MAIN) $(LIBOBJECTS)
$(AM_LINK)

# examples

conf_example: examples/conf_example.o $(LIBOBJECTS)
$(AM_LINK)

cond_lock_example: examples/cond_lock_example.o $(LIBOBJECTS)
$(AM_LINK)

binlog_example: examples/binlog_example.o $(LIBOBJECTS)
$(AM_LINK)

mutex_example: examples/mutex_example.o $(LIBOBJECTS)
$(AM_LINK)

hash_example: examples/hash_example.o $(LIBOBJECTS)
$(AM_LINK)
31 changes: 31 additions & 0 deletions src/slash/examples/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
CXX=g++
LDFLAGS= -lpthread -lrt
CXXFLAGS= -O2 -std=c++11 -fno-builtin-memcmp -msse -msse4.2

.PHONY: clean libslash

all: conf_example cond_lock_example binlog_example mutex_example hash_example

CXXFLAGS+= -I../..

conf_example: libslash conf_example.cc
$(CXX) $(CXXFLAGS) $@.cc -o$@ ../lib/libslash.a $(LDFLAGS)

cond_lock_example: libslash cond_lock_example.cc
$(CXX) $(CXXFLAGS) $@.cc -o$@ ../lib/libslash.a $(LDFLAGS)

binlog_example: libslash binlog_example.cc
$(CXX) $(CXXFLAGS) $@.cc -o$@ ../lib/libslash.a $(LDFLAGS)

mutex_example: libslash mutex_example.cc
$(CXX) $(CXXFLAGS) $@.cc -o$@ ../lib/libslash.a $(LDFLAGS)

hash_example: libslash hash_example.cc
$(CXX) $(CXXFLAGS) $@.cc -o$@ ../lib/libslash.a $(LDFLAGS)

clean:
find . -name "*.[oda]*" -exec rm -f {} \;
rm -rf ./conf_example ./cond_lock_example ./binlog_example ./mutex_example ./hash_example

libslash:
cd .. && $(MAKE) static_lib
65 changes: 65 additions & 0 deletions src/slash/examples/binlog_example.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
#include <iostream>

#include "slash/include/slash_binlog.h"
#include "slash/include/xdebug.h"

using namespace slash;

Binlog *log;

#define UNUSED(arg) ((void)arg)

void* Append(void *arg) {
UNUSED(arg);
int i = 0;
while (true) {
std::string item = "item" + std::to_string(i++);
Status s = log->Append(item);
printf ("+ Append (%s) return %s\n", item.c_str(), s.ToString().c_str());
sleep(1);
}
return NULL;
}

void* Reader(void *arg) {
UNUSED(arg);
int *id = (int *)arg;
BinlogReader* reader = log->NewBinlogReader(0, 0);

while (true) {
std::string str;
Status s = reader->ReadRecord(str);
if (s.ok()) {
printf ("- Reader %u: get (%s)\n", *id, str.c_str());
} else {
printf ("- Reader %lu: %s\n", pthread_self(), s.ToString().c_str());
}
sleep(1);
}
return NULL;
}

int main() {

Status s = Binlog::Open("./binlog", &log);
if (!s.ok()) {
printf ("Open failed %s\n", s.ToString().c_str());
return -1;
}

pthread_t pid;
pthread_create(&pid, NULL, &Append, NULL);

pthread_t cid[3];
int id[3];

for (int i = 0; i < 3; i++) {
id[i] = i;
pthread_create(&cid[i], NULL, &Reader, &id[i]);
}

void* res;
pthread_join(pid, &res);

return 0;
}
17 changes: 17 additions & 0 deletions src/slash/examples/cond_lock_example.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#include <iostream>

#include "slash/include/cond_lock.h"

#include <unistd.h>

slash::CondLock cl;

int main()
{
cl.Lock();
cl.Unlock();
uint32_t a = 2000;
cl.Lock();
cl.TimedWait(a);
return 0;
}
23 changes: 23 additions & 0 deletions src/slash/examples/conf_example.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
#include "slash/include/base_conf.h"
#include "slash/include/xdebug.h"

using namespace slash;

int main()
{
BaseConf b("./conf/pika.conf");

if (b.LoadConf() == 0) {
log_info("LoadConf true");
} else {
log_info("LoodConf error");
}

b.SetConfInt("port", 99999);
b.SetConfStr("pidfile", "./anan.pid");
b.WriteBack();
b.DumpConf();
b.WriteSampleConf();

return 0;
}
16 changes: 16 additions & 0 deletions src/slash/examples/hash_example.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
#include <string>
#include <iostream>

#include "slash/include/slash_hash.h"

using namespace slash;
int main() {
std::string input = "grape";
std::string output1 = sha256(input);
std::string output2 = md5(input);

std::cout << "sha256('"<< input << "'): " << output1 << std::endl;
std::cout << "md5('"<< input << "'): " << output2<< std::endl;

return 0;
}
Loading

0 comments on commit b2a9cce

Please sign in to comment.