From ad90fce12bbdd5d6d32a34cee22cbbb93785ab99 Mon Sep 17 00:00:00 2001 From: kite707 <12201680@inha.edu> Date: Wed, 7 Feb 2024 15:12:26 +0900 Subject: [PATCH 1/6] =?UTF-8?q?setting:=20ignore=ED=8C=8C=EC=9D=BC?= =?UTF-8?q?=EC=97=90=20=EC=98=A4=EB=B8=8C=EC=A0=9D=ED=8A=B8=ED=8C=8C?= =?UTF-8?q?=EC=9D=BC,=20=EB=9D=BC=EC=9D=B4=EB=B8=8C=EB=9F=AC=EB=A6=AC=20?= =?UTF-8?q?=ED=8C=8C=EC=9D=BC=20=EC=B6=94=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .gitignore | 2 ++ 1 file changed, 2 insertions(+) diff --git a/.gitignore b/.gitignore index 485dee64..6e01b2f9 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,3 @@ .idea +*.a +*.o From cc0425853983fd250e061ddd9e3ba768e874e191 Mon Sep 17 00:00:00 2001 From: kite707 <12201680@inha.edu> Date: Wed, 7 Feb 2024 15:13:30 +0900 Subject: [PATCH 2/6] =?UTF-8?q?feat:=20quorum=EC=9D=98=20string=ED=95=A8?= =?UTF-8?q?=EC=88=98=20c=EB=A1=9C=20=ED=8F=AC=ED=8C=85?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- quorum/quorum.c | 10 ++++++++++ quorum/quorum.h | 12 ++++++++++++ 2 files changed, 22 insertions(+) create mode 100644 quorum/quorum.c create mode 100644 quorum/quorum.h diff --git a/quorum/quorum.c b/quorum/quorum.c new file mode 100644 index 00000000..7544b995 --- /dev/null +++ b/quorum/quorum.c @@ -0,0 +1,10 @@ +#include "quorum.h" + +const char *index_to_string(uint64_t i) { + static char buffer[21]; // buffer size for uint64_t + if (i == UINT64_MAX) { + return "∞"; + } + snprintf(buffer, sizeof(buffer), "%" PRIu64, (uint64_t)i); + return buffer; +} \ No newline at end of file diff --git a/quorum/quorum.h b/quorum/quorum.h new file mode 100644 index 00000000..e5fdc610 --- /dev/null +++ b/quorum/quorum.h @@ -0,0 +1,12 @@ +#ifndef _QUORUM_H +#define _QUORUM_H + +#include +#include +#include + +typedef uint64_t Index; + +const char *index_to_string(uint64_t i); + +#endif \ No newline at end of file From 7ce885911c26801df93536d0f1723dd6b781083b Mon Sep 17 00:00:00 2001 From: kite707 <12201680@inha.edu> Date: Wed, 7 Feb 2024 15:13:50 +0900 Subject: [PATCH 3/6] =?UTF-8?q?feat:=20c=EB=A1=9C=20=ED=8F=AC=ED=8C=85?= =?UTF-8?q?=ED=95=9C=20=ED=95=A8=EC=88=98=20go=EC=97=90=20=EC=A0=81?= =?UTF-8?q?=EC=9A=A9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- quorum/quorum.go | 15 +++++++-------- 1 file changed, 7 insertions(+), 8 deletions(-) diff --git a/quorum/quorum.go b/quorum/quorum.go index 2899e46c..b5d16837 100644 --- a/quorum/quorum.go +++ b/quorum/quorum.go @@ -14,19 +14,18 @@ package quorum -import ( - "math" - "strconv" -) +/* +#cgo LDFLAGS: -L. -lquorum +#include "quorum.h" +*/ +import "C" + // Index is a Raft log position. type Index uint64 func (i Index) String() string { - if i == math.MaxUint64 { - return "∞" - } - return strconv.FormatUint(uint64(i), 10) + return C.GoString(C.index_to_string(C.uint64_t(i))) } // AckedIndexer allows looking up a commit index for a given ID of a voter From b44a3684296fa97bb7236b9e36b965bd65dc5794 Mon Sep 17 00:00:00 2001 From: kite707 <12201680@inha.edu> Date: Wed, 7 Feb 2024 15:33:13 +0900 Subject: [PATCH 4/6] =?UTF-8?q?feat:=20quorumC=ED=8F=B4=EB=8D=94=20?= =?UTF-8?q?=EC=83=9D=EC=84=B1=20=EB=B0=8F=20=ED=8C=8C=EC=9D=BC=20=EC=9D=B4?= =?UTF-8?q?=EB=8F=99?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- quorum/quorum.go | 4 ++-- quorum/{ => quorumC}/quorum.c | 0 quorum/quorumC/quorum.h | 12 ++++++++++++ 3 files changed, 14 insertions(+), 2 deletions(-) rename quorum/{ => quorumC}/quorum.c (100%) create mode 100644 quorum/quorumC/quorum.h diff --git a/quorum/quorum.go b/quorum/quorum.go index b5d16837..ef1812a3 100644 --- a/quorum/quorum.go +++ b/quorum/quorum.go @@ -15,8 +15,8 @@ package quorum /* -#cgo LDFLAGS: -L. -lquorum -#include "quorum.h" +#cgo LDFLAGS: -L./quorumC -lquorum +#include "quorumC/quorum.h" */ import "C" diff --git a/quorum/quorum.c b/quorum/quorumC/quorum.c similarity index 100% rename from quorum/quorum.c rename to quorum/quorumC/quorum.c diff --git a/quorum/quorumC/quorum.h b/quorum/quorumC/quorum.h new file mode 100644 index 00000000..e5fdc610 --- /dev/null +++ b/quorum/quorumC/quorum.h @@ -0,0 +1,12 @@ +#ifndef _QUORUM_H +#define _QUORUM_H + +#include +#include +#include + +typedef uint64_t Index; + +const char *index_to_string(uint64_t i); + +#endif \ No newline at end of file From a81c2a80d737dddd810dd397a41f330372871769 Mon Sep 17 00:00:00 2001 From: Lunawood Date: Sat, 10 Feb 2024 23:20:03 +0900 Subject: [PATCH 5/6] =?UTF-8?q?MajorityConfig,=20insertionSort=20=EC=99=84?= =?UTF-8?q?=EB=A3=8C=20but=20=EA=BB=8D=EB=8D=B0=EA=B8=B0=20=EC=A0=9C?= =?UTF-8?q?=EA=B1=B0=20=ED=95=84=EC=9A=94?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- quorum/majority.go | 107 ++++++++++++++++++++++++++++++++++++++------- quorum/quorum.go | 1 - 2 files changed, 91 insertions(+), 17 deletions(-) diff --git a/quorum/majority.go b/quorum/majority.go index 12766137..97adc59e 100644 --- a/quorum/majority.go +++ b/quorum/majority.go @@ -19,27 +19,85 @@ import ( "math" "sort" "strings" + "unsafe" ) +/* +#include +#include +#include + +int compare(const void *a, const void *b) { + return (*(int*)a - *(int*)b); +} + +// sort slice by ascending & slice -> string +const char* cMajorityConfig(int* sl, int size) { + qsort(sl, size, sizeof(int), compare); + + char* buf = (char*)malloc((2 * size + 3) * sizeof(char)); + + // TEST Q&A : 100 200 300 -> 100 200 300 0 0 0 33 0 + //for(int i = 0; i < sizeof(sl); i++){ + // printf("%d ", sl[i]); + //} + //printf("\n"); + + int index = 0; + buf[index++] = '('; + for (int i = 0; i < size; i++) { + if (i > 0) { + buf[index++] = ' '; + } + index += sprintf(buf + index, "%d", sl[i]); + } + buf[index++] = ')'; + buf[index] = '\0'; + + return buf; +} + +void insertSort(uint64_t *sl, int len) { + int a = 0, b = len; + for (int i = a + 1; i < b; i++) { + for (int j = i; j > a && sl[j] < sl[j-1]; j--) { + uint64_t tmp = sl[j]; + sl[j] = sl[j-1]; + sl[j-1] = tmp; + } + } +} +*/ +import "C" + // MajorityConfig is a set of IDs that uses majority quorums to make decisions. type MajorityConfig map[uint64]struct{} +// by chanjun func (c MajorityConfig) String() string { + // make slice sl := make([]uint64, 0, len(c)) + + // push key only for id := range c { sl = append(sl, id) } - sort.Slice(sl, func(i, j int) bool { return sl[i] < sl[j] }) - var buf strings.Builder - buf.WriteByte('(') - for i := range sl { - if i > 0 { - buf.WriteByte(' ') - } - fmt.Fprint(&buf, sl[i]) + + // Q&A : If pass an array directly, the array size changes + // and 0s enter. + // Assignment C memory + size := len(sl) * int(unsafe.Sizeof(C.int(0))) + cArray := C.malloc(C.size_t(size)) + defer C.free(cArray) + + // Copy values ​​from Go slice to C array + for i, v := range sl { + offset := i * int(unsafe.Sizeof(C.int(0))) + ptr := unsafe.Pointer(uintptr(cArray) + uintptr(offset)) + *(*C.int)(ptr) = C.int(v) } - buf.WriteByte(')') - return buf.String() + + return C.GoString(C.cMajorityConfig((*C.int)(cArray), C.int(len(sl)))) } // Describe returns a (multi-line) representation of the commit indexes for the @@ -102,6 +160,7 @@ func (c MajorityConfig) Describe(l AckedIndexer) string { return buf.String() } +// by chanjun // Slice returns the MajorityConfig as a sorted slice. func (c MajorityConfig) Slice() []uint64 { var sl []uint64 @@ -112,12 +171,28 @@ func (c MajorityConfig) Slice() []uint64 { return sl } -func insertionSort(sl []uint64) { - a, b := 0, len(sl) - for i := a + 1; i < b; i++ { - for j := i; j > a && sl[j] < sl[j-1]; j-- { - sl[j], sl[j-1] = sl[j-1], sl[j] - } +// by chanjun +func insertionSort(arr []uint64) { + // C로 전달할 배열 + size := len(arr) * int(unsafe.Sizeof(C.uint64_t(0))) + cArray := C.malloc(C.size_t(size)) + defer C.free(cArray) + + // Copy values ​​from Go slice to C array + for i, v := range arr { + offset := i * int(unsafe.Sizeof(C.uint64_t(0))) + ptr := unsafe.Pointer(uintptr(cArray) + uintptr(offset)) + *(*C.uint64_t)(ptr) = C.uint64_t(v) + } + + // C 함수 호출 + C.insertSort((*C.uint64_t)(cArray), C.int(len(arr))) + + // 정렬된 결과를 다시 Go 슬라이스로 변환하고 testSlice에 할당 + for i := range arr { + offset := i * int(unsafe.Sizeof(C.uint64_t(0))) + ptr := unsafe.Pointer(uintptr(cArray) + uintptr(offset)) + arr[i] = uint64(*(*C.uint64_t)(ptr)) } } diff --git a/quorum/quorum.go b/quorum/quorum.go index ef1812a3..dd51fd64 100644 --- a/quorum/quorum.go +++ b/quorum/quorum.go @@ -20,7 +20,6 @@ package quorum */ import "C" - // Index is a Raft log position. type Index uint64 From a5b3dcd37e746d932409b3a7ccf859b8dba9274c Mon Sep 17 00:00:00 2001 From: Lunawood Date: Thu, 15 Feb 2024 21:26:20 +0900 Subject: [PATCH 6/6] modify MajorityConfig, InsertionSort & add Slice --- quorum/majority.go | 96 +++++++------------------------------ quorum/majorityC/majority.c | 48 +++++++++++++++++++ quorum/majorityC/majority.h | 19 ++++++++ 3 files changed, 84 insertions(+), 79 deletions(-) create mode 100644 quorum/majorityC/majority.c create mode 100644 quorum/majorityC/majority.h diff --git a/quorum/majority.go b/quorum/majority.go index 97adc59e..8915ee26 100644 --- a/quorum/majority.go +++ b/quorum/majority.go @@ -23,50 +23,8 @@ import ( ) /* -#include -#include -#include - -int compare(const void *a, const void *b) { - return (*(int*)a - *(int*)b); -} - -// sort slice by ascending & slice -> string -const char* cMajorityConfig(int* sl, int size) { - qsort(sl, size, sizeof(int), compare); - - char* buf = (char*)malloc((2 * size + 3) * sizeof(char)); - - // TEST Q&A : 100 200 300 -> 100 200 300 0 0 0 33 0 - //for(int i = 0; i < sizeof(sl); i++){ - // printf("%d ", sl[i]); - //} - //printf("\n"); - - int index = 0; - buf[index++] = '('; - for (int i = 0; i < size; i++) { - if (i > 0) { - buf[index++] = ' '; - } - index += sprintf(buf + index, "%d", sl[i]); - } - buf[index++] = ')'; - buf[index] = '\0'; - - return buf; -} - -void insertSort(uint64_t *sl, int len) { - int a = 0, b = len; - for (int i = a + 1; i < b; i++) { - for (int j = i; j > a && sl[j] < sl[j-1]; j--) { - uint64_t tmp = sl[j]; - sl[j] = sl[j-1]; - sl[j-1] = tmp; - } - } -} +#cgo LDFLAGS: -L./majorityC -lmajority +#include "majorityC/majority.h" */ import "C" @@ -83,21 +41,11 @@ func (c MajorityConfig) String() string { sl = append(sl, id) } - // Q&A : If pass an array directly, the array size changes - // and 0s enter. - // Assignment C memory - size := len(sl) * int(unsafe.Sizeof(C.int(0))) - cArray := C.malloc(C.size_t(size)) - defer C.free(cArray) - - // Copy values ​​from Go slice to C array - for i, v := range sl { - offset := i * int(unsafe.Sizeof(C.int(0))) - ptr := unsafe.Pointer(uintptr(cArray) + uintptr(offset)) - *(*C.int)(ptr) = C.int(v) + if len(sl) != 0 { + return C.GoString(C.cMajorityConfig((unsafe.Pointer(&sl[0])), C.int(len(sl)))) + } else { + return "()" } - - return C.GoString(C.cMajorityConfig((*C.int)(cArray), C.int(len(sl)))) } // Describe returns a (multi-line) representation of the commit indexes for the @@ -167,32 +115,22 @@ func (c MajorityConfig) Slice() []uint64 { for id := range c { sl = append(sl, id) } - sort.Slice(sl, func(i, j int) bool { return sl[i] < sl[j] }) + + // 조건문이 없으면 "index out of range [0] with length 0"라는 오류가 생김 + // sort.Slice(sl, func(i, j int) bool { return sl[i] < sl[j] }) + if len(sl) != 0 { + C.cSlice(unsafe.Pointer(&sl[0]), C.int(len(sl))) + } + return sl } // by chanjun +// insertionSort : just quick sort func insertionSort(arr []uint64) { - // C로 전달할 배열 - size := len(arr) * int(unsafe.Sizeof(C.uint64_t(0))) - cArray := C.malloc(C.size_t(size)) - defer C.free(cArray) - - // Copy values ​​from Go slice to C array - for i, v := range arr { - offset := i * int(unsafe.Sizeof(C.uint64_t(0))) - ptr := unsafe.Pointer(uintptr(cArray) + uintptr(offset)) - *(*C.uint64_t)(ptr) = C.uint64_t(v) - } - - // C 함수 호출 - C.insertSort((*C.uint64_t)(cArray), C.int(len(arr))) - - // 정렬된 결과를 다시 Go 슬라이스로 변환하고 testSlice에 할당 - for i := range arr { - offset := i * int(unsafe.Sizeof(C.uint64_t(0))) - ptr := unsafe.Pointer(uintptr(cArray) + uintptr(offset)) - arr[i] = uint64(*(*C.uint64_t)(ptr)) + if len(arr) != 0 { + // C 함수 호출 + C.cinsertionSort(unsafe.Pointer(&arr[0]), C.int(len(arr))) } } diff --git a/quorum/majorityC/majority.c b/quorum/majorityC/majority.c new file mode 100644 index 00000000..113d8d94 --- /dev/null +++ b/quorum/majorityC/majority.c @@ -0,0 +1,48 @@ +#include +#include +#include + +int compare(const void *a, const void *b){ + return(*(long long unsigned int*)a - *(long long unsigned int*)b); +} + +// sort slice by ascending & slice -> string +const char* cMajorityConfig(void* p, int size) { + long long unsigned int* sl = (long long unsigned int*) p; + + qsort(sl, size, sizeof(long long unsigned int), compare); + + char* buf = (char*)malloc((2 * size + 3) * sizeof(char)); + + int index = 0; + buf[index++] = '('; + for (int i = 0; i < size; i++) { + if (i > 0) { + buf[index++] = ' '; + } + index += sprintf(buf + index, "%llu", sl[i]); + } + buf[index++] = ')'; + buf[index] = '\0'; + + return buf; +} + +// sort.Slice(sl, func(i, j int) bool { return sl[i] < sl[j] }) 의 역활 +void cSlice(void* p, int size) { + // 형변환 + long long unsigned int* arr = (long long unsigned int*)p; + qsort(arr, size, sizeof(long long unsigned int), compare); +} + +void cinsertionSort(void* p, int size) { + long long unsigned int* sl = (long long unsigned int*) p; + int a = 0, b = size; + for (int i = a + 1; i < b; i++) { + for (int j = i; j > a && sl[j] < sl[j-1]; j--) { + long long unsigned int tmp = sl[j]; + sl[j] = sl[j-1]; + sl[j-1] = tmp; + } + } +} \ No newline at end of file diff --git a/quorum/majorityC/majority.h b/quorum/majorityC/majority.h new file mode 100644 index 00000000..9abb6cbd --- /dev/null +++ b/quorum/majorityC/majority.h @@ -0,0 +1,19 @@ +//quorum.h +#ifndef _MAJORITY_H +#define _MAJORITY_H + +#include +#include +#include + +int compare(const void *a, const void *b); + +// sort slice by ascending & slice -> string +const char* cMajorityConfig(void* p, int size); + +// sort.Slice(sl, func(i, j int) bool { return sl[i] < sl[j] }) 의 역활 +void cSlice(void* p, int size); + +void cinsertionSort(void* p, int size); + +#endif \ No newline at end of file