Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
4 changes: 4 additions & 0 deletions .clang-tidy-ignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
libCacheSim/dataStructure/robin_hood.h



47 changes: 24 additions & 23 deletions .github/workflows/code-quality.yml
Original file line number Diff line number Diff line change
Expand Up @@ -66,32 +66,33 @@ jobs:
-DCMAKE_CXX_FLAGS="-Wall -Wextra -Werror -Wno-unused-variable -Wno-unused-function -Wno-unused-parameter -Wno-unused-but-set-variable -Wno-pedantic -Wformat=2 -Wformat-security -Wshadow -Wwrite-strings -Wmissing-include-dirs" \
-DCMAKE_EXPORT_COMPILE_COMMANDS=ON

- name: Clang-tidy check
if: steps.changed-files.outputs.any_changed == 'true'
run: |
echo "Running clang-tidy checks..."
FILES_WITH_ISSUES=0
# TODO(jason): disable clang-tidy check for now, as it reports too many CXX header issues
# - name: Clang-tidy check
# if: steps.changed-files.outputs.any_changed == 'true'
# run: |
# echo "Running clang-tidy checks..."
# FILES_WITH_ISSUES=0

for file in ${{ steps.changed-files.outputs.all_changed_files }}; do
echo "Checking $file with clang-tidy"
LOG_FILE="logs/tidy_$(basename "$file").log"
# for file in ${{ steps.changed-files.outputs.all_changed_files }}; do
# echo "Checking $file with clang-tidy"
# LOG_FILE="logs/tidy_$(basename "$file").log"

# Run clang-tidy with selected checks (same as pre-commit hook)
if ! clang-tidy -p=build \
-checks='-*,bugprone-*,cert-*,clang-analyzer-*,cppcoreguidelines-*,performance-*,portability-*,readability-*,-readability-magic-numbers,-readability-braces-around-statements,-cppcoreguidelines-avoid-magic-numbers,-readability-identifier-length,-clang-diagnostic-unused-command-line-argument' \
"$file" > "$LOG_FILE" 2>&1; then
echo "::error file=$file::clang-tidy found issues in $file"
cat "$LOG_FILE"
FILES_WITH_ISSUES=$((FILES_WITH_ISSUES + 1))
fi
done
# # Run clang-tidy with selected checks (same as pre-commit hook)
# if ! clang-tidy -p=build \
# -checks='-*,bugprone-*,cert-*,clang-analyzer-*,cppcoreguidelines-*,performance-*,portability-*,readability-*,-readability-magic-numbers,-readability-braces-around-statements,-cppcoreguidelines-avoid-magic-numbers,-readability-identifier-length,-clang-diagnostic-unused-command-line-argument' \
# "$file" > "$LOG_FILE" 2>&1; then
# echo "::error file=$file::clang-tidy found issues in $file"
# cat "$LOG_FILE"
# FILES_WITH_ISSUES=$((FILES_WITH_ISSUES + 1))
# fi
# done

if [ $FILES_WITH_ISSUES -gt 0 ]; then
echo "clang-tidy found issues in $FILES_WITH_ISSUES files."
exit 1
else
echo "clang-tidy check passed successfully."
fi
# if [ $FILES_WITH_ISSUES -gt 0 ]; then
# echo "clang-tidy found issues in $FILES_WITH_ISSUES files."
# exit 1
# else
# echo "clang-tidy check passed successfully."
# fi

- name: Compilation check
if: steps.changed-files.outputs.any_changed == 'true'
Expand Down
28 changes: 14 additions & 14 deletions example/cacheCluster/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,29 +1,29 @@
cmake_minimum_required (VERSION 3.2)
project (cacheCluster)
# set(CMAKE_BUILD_TYPE Debug)
cmake_minimum_re# Collect source files
Comment thread
1a1a11a marked this conversation as resolved.
Outdated
project(cacheCluster)
set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON)


# Find required packages
set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} "${CMAKE_SOURCE_DIR}/../../cmake/Modules/")
find_package(GLib "2.40" REQUIRED)
include_directories(${GLib_INCLUDE_DIRS})
message(STATUS "glib found? " ${GLib_FOUND} ", GLIB = " ${GLib_LIBRARY} ", header = " ${GLib_INCLUDE_DIRS})

find_package(GLib "2.40" REQUIRED)
message(STATUS "GLib found: ${GLib_LIBRARY}")

find_package(ZSTD)
# https://stackoverflow.com/questions/61377055/cannot-find-gflags-gflags-h-while-building-library-osx/61379123#61379123
include_directories(${ZSTD_INCLUDE_DIR})
if ("${ZSTD_LIBRARIES}" STREQUAL "")
message(FATAL_ERROR "zstd not found")
find_package(ZSTD REQUIRED)
if("${ZSTD_LIBRARIES}" STREQUAL "")
message(FATAL_ERROR "ZSTD not found")
endif()


################ this mode compiles the external cache replacement algorithm together with the simulator ############
message(STATUS "project_source dir = " ${PROJECT_SOURCE_DIR})
file(GLOB ALL_CXX_SRC ${PROJECT_SOURCE_DIR}/*.cpp)
file(GLOB ALL_C_SRC ${PROJECT_SOURCE_DIR}/*.c)
target_include_directories(cacheCluster PRIVATE
${GLib_INCLUDE_DIRS}
${CMAKE_SOURCE_DIR}/include
${PROJECT_SOURCE_DIR}
${PROJECT_SOURCE_DIR}/include
)

add_executable(cacheCluster ${ALL_CXX_SRC} ${ALL_C_SRC})
target_link_libraries(cacheCluster libCacheSim m ${GLib_LIBRARY} pthread dl ${ZSTD_LIBRARIES})

3 changes: 1 addition & 2 deletions example/cacheCluster/cacheCluster.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,7 @@ namespace CDNSimulator {

bool CacheCluster::get(request_t *req) {
// find the server idx
uint64_t idx =
ch_ring_get_server_from_uint64(req->obj_id, this->_ring);
uint64_t idx = ch_ring_get_server_from_uint64(req->obj_id, this->_ring);

// find the server
CacheServer &server = this->_cache_servers_vec.at(idx);
Expand Down
64 changes: 35 additions & 29 deletions example/cacheCluster/consistentHash.c
Original file line number Diff line number Diff line change
Expand Up @@ -4,23 +4,27 @@
// modified from libketama
//

#include "include/consistentHash.h"
#include "consistentHash.h"

#include <inttypes.h>
#include <stdbool.h>

#include "include/md5.h"
#include "md5.h"

#ifdef __cplusplus
extern "C" {
#endif

int ch_ring_compare(const void *a, const void *b) {
vnode_t *node_a = (vnode_t *)a;
vnode_t *node_b = (vnode_t *)b;
return (node_a->point < node_b->point)
? -1
: ((node_a->point > node_b->point) ? 1 : 0);
int ch_ring_compare(const void *node_a_ptr, const void *node_b_ptr) {
vnode_t *node_a = (vnode_t *)node_a_ptr;
vnode_t *node_b = (vnode_t *)node_b_ptr;
if (node_a->point < node_b->point) {
return -1;
}
if (node_a->point > node_b->point) {
return 1;
}
return 0;
}

void md5_digest(const char *const inString, unsigned char md5pword[16]) {
Expand All @@ -38,7 +42,7 @@ unsigned int ketama_hash(const char *const inString) {
(digest[1] << 8) | digest[0]);
}

ring_t *ch_ring_create_ring(int n_server, double *weight) {
ring_t *ch_ring_create_ring(int n_server, const double *weight) {
vnode_t *vnodes =
(vnode_t *)malloc(sizeof(vnode_t) * n_server * N_VNODE_PER_SERVER);

Expand All @@ -47,35 +51,38 @@ ring_t *ch_ring_create_ring(int n_server, double *weight) {
ring->n_point = n_server * N_VNODE_PER_SERVER;
ring->vnodes = vnodes;

int i;
unsigned int k, cnt = 0;
int server_idx;
unsigned int key_idx;
unsigned int cnt = 0;

for (i = 0; i < n_server; i++) {
for (server_idx = 0; server_idx < n_server; server_idx++) {
// default all servers have the same weight
unsigned int ks = N_VNODE_PER_SERVER / 4;
unsigned int keys_per_server = N_VNODE_PER_SERVER / 4;
if (weight != NULL)
ks = (unsigned int)floorf(weight[i] * (float)n_server *
(N_VNODE_PER_SERVER / 4));
keys_per_server =
(unsigned int)floorf((float)(weight[server_idx] * (double)n_server *
(N_VNODE_PER_SERVER / 4)));

for (k = 0; k < ks; k++) {
for (key_idx = 0; key_idx < keys_per_server; key_idx++) {
/* 40 hashes, 4 numbers per hash = 160 points per server */
char ss[30];
char string_buf[30];
unsigned char digest[16];

sprintf(ss, "%u-%u", i, k);
md5_digest(ss, digest);
sprintf(string_buf, "%u-%u", server_idx, key_idx);
md5_digest(string_buf, digest);

/* Use successive 4-bytes from hash as numbers for the points on the
* circle: */
int h;
for (h = 0; h < 4; h++) {
int hash_idx;
for (hash_idx = 0; hash_idx < 4; hash_idx++) {
// printf("%d i %d k %d h %d %d %d\n", cnt, i, k, h, ring->n_server,
// ring->n_point);
vnodes[cnt].point = (digest[3 + h * 4] << 24) |
(digest[2 + h * 4] << 16) |
(digest[1 + h * 4] << 8) | digest[h * 4];
vnodes[cnt].point = (digest[3 + hash_idx * 4] << 24) |
(digest[2 + hash_idx * 4] << 16) |
(digest[1 + hash_idx * 4] << 8) |
digest[hash_idx * 4];

vnodes[cnt].server_id = i;
vnodes[cnt].server_id = server_idx;
cnt++;
}
}
Expand Down Expand Up @@ -136,10 +143,9 @@ int ch_ring_get_server(const char *const key, const ring_t *const ring) {
return (ring->vnodes + ch_ring_get_vnode_idx(key, ring))->server_id;
}

int ch_ring_get_server_from_uint64(uint64_t obj_id,
const ring_t *const ring) {
char key[8];
memcpy(key, (char *) &obj_id, 8);
int ch_ring_get_server_from_uint64(uint64_t obj_id, const ring_t *const ring) {
char key[8];
memcpy(key, (char *)&obj_id, 8);
key[7] = 0;
return (ring->vnodes + ch_ring_get_vnode_idx(key, ring))->server_id;
}
Expand Down
3 changes: 2 additions & 1 deletion example/cacheCluster/include/cache.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,10 @@
#ifndef CDNSIMULATOR_CACHE_HPP
#define CDNSIMULATOR_CACHE_HPP

#include <inttypes.h>

#include <algorithm>
#include <iostream>
#include <inttypes.h>

#include "libCacheSim/cache.h"
#include "libCacheSim/evictionAlgo.h"
Expand Down
1 change: 0 additions & 1 deletion example/cacheCluster/include/cacheCluster.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ namespace CDNSimulator {

class CacheCluster {
private:

// the consistent hash ring
ring_t *_ring = nullptr;

Expand Down
2 changes: 1 addition & 1 deletion example/cacheCluster/include/consistentHash.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,11 @@
extern "C" {
#endif

#include <inttypes.h>
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <inttypes.h>

#define N_VNODE_PER_SERVER 160

Expand Down
136 changes: 136 additions & 0 deletions example/cacheCluster/include/ketama.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,136 @@
/*
* Copyright (c) 2017, Emory University, All rights reserved.
* Juncheng Yang <jason.yang.china@outlook.com>
*
* Modify for libCacheSim based on original version
* also add const modifier for compiler optimizations
*
*/

/*
* Copyright (c) 2007, Last.fm, All rights reserved.
* Richard Jones <rj@last.fm>
* Christian Muehlhaeuser <muesli@gmail.com>
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* * Neither the name of the Last.fm Limited nor the
* names of its contributors may be used to endorse or promote products
* derived from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY Last.fm ``AS IS'' AND ANY
* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL Last.fm BE LIABLE FOR ANY
* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/

#ifndef KETAMA_LIBKETAMA_KETAMA_H__
#define KETAMA_LIBKETAMA_KETAMA_H__

#include <sys/sem.h> /* semaphore functions and structs. */

#define MC_SHMSIZE 524288 // 512KB should be ample.

#ifdef __cplusplus /* If this is a C++ compiler, use C linkage */
extern "C" {
#endif

#ifndef __APPLE__
union semun {
int val; /* used for SETVAL only */
struct semid_ds *buf; /* for IPC_STAT and IPC_SET */
ushort *array; /* used for GETALL and SETALL */
};
#endif

typedef int (*compfn)(const void *, const void *);

typedef struct {
unsigned int point; // point on circle
char ip[22];
} mcs;

typedef struct {
char addr[22];
unsigned long memory; // in CDNSimulator, this is used as weight
} serverinfo;

typedef struct {
int numpoints;
void *modtime;
void *array; // array of mcs structs
} continuum;

typedef continuum *ketama_continuum;

/** build a consistent hashing ring
* given the number of servers and the weight of each server.
* if weight is NULL, then each server has equal weight.
* key_identifier is just a number for identifying shared memory */
int ketama_build_hashring(ketama_continuum *const contptr,
const unsigned int num_servers,
const unsigned long *const weight,
const int key_identifier);

int ketama_get_server_index(const ketama_continuum cont, const char *const key);

void ketama_get_server_indexes(const ketama_continuum cont,
const char *const key, unsigned int n,
int *indexes);

/** \brief Get a continuum struct that contains a reference to the server list.
* \param contptr The value of this pointer will contain the retrieved
* continuum. \param filename The server-definition file which defines our
* continuum. \return 0 on failure, 1 on success. */
int ketama_roll(ketama_continuum *contptr, char *filename);

/** \brief Frees any allocated memory.
* \param contptr The continuum that you want to be destroy. */
void ketama_smoke(ketama_continuum contptr);

/** \brief Maps a key onto a server in the continuum.
* \param key The key that you want to map to a specific server.
* \param cont Pointer to the continuum in which we will search.
* \return The mcs struct that the given key maps to. */
mcs *ketama_get_server(char *key, ketama_continuum cont);

/** \brief Print the server list of a continuum to stdout.
* \param c The continuum to print. */
void ketama_print_continuum(ketama_continuum c);

/** \brief Compare two server entries in the circle.
* \param a The first entry.
* \param b The second entry.
* \return -1 if b greater a, +1 if a greater b or 0 if both are equal. */
int ketama_compare(mcs *a, mcs *b);

/** \brief Hashing function, converting a string to an unsigned int by using
* MD5. \param inString The string that you want to hash. \return The resulting
* hash. */
unsigned int ketama_hashi(const char *const inString);

/** \brief Hashing function to 16 bytes char array using MD5.
* \param inString The string that you want to hash.
* \param md5pword The resulting hash. */
void ketama_md5_digest(const char *const inString, unsigned char md5pword[16]);

/** \brief Error method for error checking.
* \return The latest error that occurred. */
char *ketama_error();

#ifdef __cplusplus /* If this is a C++ compiler, end C linkage */
}
#endif

#endif // KETAMA_LIBKETAMA_KETAMA_H__
Loading
Loading