Skip to content

Commit

Permalink
Update
Browse files Browse the repository at this point in the history
  • Loading branch information
tkf2019 committed Aug 29, 2024
1 parent 42290e6 commit b86bb01
Show file tree
Hide file tree
Showing 106 changed files with 2,534 additions and 1,055 deletions.
2 changes: 1 addition & 1 deletion .clang-format
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ BasedOnStyle: LLVM
IndentWidth: 4
ColumnLimit: 100
AllowShortBlocksOnASingleLine: Empty
# AllowShortFunctionsOnASingleLine: Inline
AllowShortFunctionsOnASingleLine: Inline
AllowShortLoopsOnASingleLine: true
AllowShortIfStatementsOnASingleLine: Never
AlignConsecutiveMacros: true
Expand Down
16 changes: 10 additions & 6 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,6 @@ endif()

set(CMAKE_C_STANDARD 23)
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -g -O3 -Wall -Wextra -Wno-unused-parameter")
if (DPDK)
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS}")
elseif (SCHED_POLICY MATCHES "^(fifo|rr|cfs)$")
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -mno-sse")
endif()

set(CMAKE_CXX_STANDARD 20)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -W -Wextra -Wall")
Expand Down Expand Up @@ -52,6 +47,10 @@ if(UINTR)
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -muintr")
endif()

if(FXSAVE)
add_definitions(-DSKYLOFT_FXSAVE)
endif()

if(LOG_LEVEL)
if(NOT LOG_LEVEL MATCHES "^(debug|info|notice|warn|err|crit)$")
message(FATAL_ERROR "Invalid log level: ${LOG_LEVEL}")
Expand All @@ -60,7 +59,6 @@ if(LOG_LEVEL)
string(TOUPPER ${LOG_LEVEL} LOG_LEVEL_UPPER)
add_definitions(-DLOG_LEVEL_${LOG_LEVEL_UPPER})
endif()

message(STATUS "Log level: ${LOG_LEVEL}")

if(NOT CMAKE_BUILD_TYPE OR CMAKE_BUILD_TYPE STREQUAL "")
Expand Down Expand Up @@ -91,3 +89,9 @@ else()
add_subdirectory(apps)
endif()

add_custom_target(
microbench
WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}/microbench
COMMAND make clean && make SKYLOFT_DIR=${CMAKE_CURRENT_BINARY_DIR}/install
COMMAND ln -sf ${CMAKE_CURRENT_SOURCE_DIR}/microbench/thread ${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/thread
)
19 changes: 16 additions & 3 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,25 @@ UINTR ?= 0
SCHED ?= fifo
DAEMON ?=
DEBUG ?=
STAT ?=
STAT ?= 0
LOG ?= info
FXSAVE ?= 0

CC ?= gcc
CFLAGS := -Wall -O2 -D_GNU_SOURCE
CMAKE_ARGS ?=

CMAKE_ARGS += -DSCHED_POLICY=$(SCHED)
CMAKE_ARGS += -DSIGNAL=$(SIGNAL) -DDPDK=$(DPDK) -DTIMER=$(TIMER) -DUINTR=$(UINTR) -DDAEMON=$(DAEMON) -DDEBUG=$(DEBUG) -DSTAT=$(STAT) -DLOG_LEVEL=$(LOG)
CMAKE_ARGS += \
-DSIGNAL=$(SIGNAL) \
-DDPDK=$(DPDK) \
-DTIMER=$(TIMER) \
-DUINTR=$(UINTR) \
-DDAEMON=$(DAEMON) \
-DDEBUG=$(DEBUG) \
-DSTAT=$(STAT) \
-DLOG_LEVEL=$(LOG) \
-DFXSAVE=$(FXSAVE)
CMAKE_ARGS += -DCMAKE_INSTALL_PREFIX=install

all: build
Expand Down Expand Up @@ -46,8 +56,11 @@ schbench: install
rocksdb: install
cd build && make rocksdb_server VERBOSE=1

microbench: install
cd build && make microbench VERBOSE=1

fmt:
@clang-format --style=file -i $(shell find utils/ libos/ apps/ tests/ experiments/ -iname '*.c' -o -iname '*.cc' -o -iname '*.h')
@clang-format --style=file -i $(shell find utils/ libos/ apps/ synthetic/ -iname '*.c' -o -iname '*.cc' -o -iname '*.h')

clean:
make -C build clean
Expand Down
7 changes: 1 addition & 6 deletions apps/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -21,19 +21,14 @@ target_link_libraries(test_timer skyloft utils)
add_executable(test_rcu test_rcu.c)
target_link_libraries(test_rcu skyloft utils)

add_executable(test_net test_net.c)
target_link_libraries(test_net skyloft utils)

if(DPDK)
add_executable(fakework fakework.c)
target_link_libraries(fakework skyloft utils)

include(${CMAKE_SCRIPTS}/rocksdb.mk)
add_custom_target(
rocksdb_server
WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}/rocksdb_server
COMMAND make clean && make SKYLOFT_DIR=${CMAKE_CURRENT_BINARY_DIR}/../install ROCKSDB_SRC=${rocksdb_SOURCE_DIR}/
COMMAND ln -sf ${CMAKE_CURRENT_SOURCE_DIR}/rocksdb_server/rocksdb_server ${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/rocksdb_server
COMMAND ln -sf ${CMAKE_CURRENT_SOURCE_DIR}/rocksdb_server/create_db ${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/create_db
)
add_dependencies(rocksdb_server librocksdb)
endif()
Expand Down
85 changes: 0 additions & 85 deletions apps/fakework.c

This file was deleted.

2 changes: 2 additions & 0 deletions apps/rocksdb_server/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
rocksdb_server
create_db
53 changes: 53 additions & 0 deletions apps/rocksdb_server/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
SKYLOFT_DIR ?=
ROCKSDB_SRC ?=

CFLAGS = -I$(SKYLOFT_DIR)/include -DSKYLOFT
SKYLOFT_LIBS = $(SKYLOFT_DIR)/lib/libskyloft.a $(SKYLOFT_DIR)/lib/libutils.a
SKYLOFT_LDFLAGS = -T $(SKYLOFT_DIR)/lib/libos.ld -lnuma
SKYLOFT_LDFLAGS += $(shell pkg-config --libs libdpdk)

CFLAGS += -I$(ROCKSDB_SRC)/include/rocksdb/
ROCKSDB_LIBS = $(ROCKSDB_SRC)/librocksdb.a
ROCKSDB_LDFLAGS = -lpthread -ldl -lz -lbz2 -lsnappy -lzstd -lstdc++ -lm

ifneq ($(DEBUG),)
CFLAGS += -DDEBUG -rdynamic -O0 -ggdb
LDFLAGS += -rdynamic
else
CFLAGS += -DNDEBUG -O3
endif

CC = gcc
CXX = g++
LD = gcc

rocksdb_server_src = rocksdb_server.cc
rocksdb_server_obj = $(rocksdb_server_src:.cc=.o)

create_db_src = create_db.cc
create_db_obj = $(create_db_src:.cc=.o)

src = $(rocksdb_server_src) $(create_db_src)
obj = $(rocksdb_server_obj) $(create_db_obj)

# must be first
all: rocksdb_server create_db

$(ROCKSDB_LIBS):
make -j -C rocksdb static_lib

rocksdb_server: $(rocksdb_server_obj) $(SKYLOFT_LIBS) $(ROCKSDB_LIBS)
$(LD) -o $@ $^ $(SKYLOFT_LDFLAGS) $(ROCKSDB_LDFLAGS)

create_db: $(create_db_obj) $(ROCKSDB_LIBS)
$(LD) -o $@ $^ $(ROCKSDB_LDFLAGS)

%.o: %.c
$(CC) $(CFLAGS) -c $< -o $@

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

.PHONY: clean
clean:
rm -f $(obj) rocksdb_server
83 changes: 83 additions & 0 deletions apps/rocksdb_server/create_db.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
#include <assert.h>
#include <random>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include "c.h"

#include <unistd.h> // sysconf() - get CPU count

const char DBPath[] = "/tmp/my_db";

int main(int argc, char **argv)
{
rocksdb_t *db;
rocksdb_backup_engine_t *be;
rocksdb_options_t *options = rocksdb_options_create();

rocksdb_slicetransform_t *prefix_extractor = rocksdb_slicetransform_create_fixed_prefix(8);
rocksdb_options_set_prefix_extractor(options, prefix_extractor);
rocksdb_options_set_plain_table_factory(options, 0, 10, 0.75, 3);

// Optimize RocksDB. This is the easiest way to
// get RocksDB to perform well
long cpus = sysconf(_SC_NPROCESSORS_ONLN); // get # of online cores
rocksdb_options_increase_parallelism(options, (int)(cpus));
rocksdb_options_optimize_level_style_compaction(options, 512 * 1024 * 1024);
// create the DB if it's not already present
rocksdb_options_set_create_if_missing(options, 1);

// open DB
char *err = NULL;
db = rocksdb_open(options, DBPath, &err);
if (err) {
printf("Failed to open DB: %s\n", err);
exit(1);
}

// Setup RNG
std::random_device rd;
std::mt19937_64 e2(rd());
std::uniform_int_distribution<long long int> dist(std::llround(std::pow(2, 64)));

// Put key-value
rocksdb_writeoptions_t *writeoptions = rocksdb_writeoptions_create();
const char *value = "value";
for (int i = 0; i < 5000; i++) {
char key[10];
// char value[64];
snprintf(key, sizeof(key), "key%d", i);
// snprintf(value, sizeof(value), "%lld", dist(e2));
rocksdb_put(db, writeoptions, key, strlen(key), value, strlen(value) + 1, &err);
assert(!err);
}

// Get value
rocksdb_readoptions_t *readoptions = rocksdb_readoptions_create();
for (int i = 0; i < 10000; i++) {
size_t len;
char key[10];
snprintf(key, sizeof(key), "key%d", i);
char *returned_value = rocksdb_get(db, readoptions, key, strlen(key), &len, &err);
if (err) {
printf("GET failed: %s\n", err);
break;
}
printf("key: %s, Returned value: %s\n", key, returned_value);
if (i < 5000) {
assert(strcmp(returned_value, "value") == 0);
free(returned_value);
} else {
assert(!returned_value);
}
}

// cleanup
rocksdb_writeoptions_destroy(writeoptions);
// rocksdb_readoptions_destroy(readoptions);
rocksdb_options_destroy(options);
rocksdb_close(db);

return 0;
}
Loading

0 comments on commit b86bb01

Please sign in to comment.