From 6042fa1c2b00059ae435b05e8ef266e7200ca888 Mon Sep 17 00:00:00 2001 From: yangshuang70 Date: Sun, 22 Aug 2021 23:20:39 +0800 Subject: [PATCH 1/3] en --- README.en.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.en.md b/README.en.md index 60274c5..b3aef80 100644 --- a/README.en.md +++ b/README.en.md @@ -21,7 +21,7 @@ Software architecture description #### Contribution 1. Fork the repository -2. Create Feat_xxx branch +2. Create Feat_xxx branch. 3. Commit your code 4. Create Pull Request -- Gitee From eab3ec64a28c291f3ab1099a3ea140fbbf920fec Mon Sep 17 00:00:00 2001 From: yangshuang70 Date: Sun, 22 Aug 2021 23:56:58 +0800 Subject: [PATCH 2/3] readme.en --- README.en.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.en.md b/README.en.md index b3aef80..3a56339 100644 --- a/README.en.md +++ b/README.en.md @@ -22,7 +22,7 @@ Software architecture description 1. Fork the repository 2. Create Feat_xxx branch. -3. Commit your code +3. Commit your code. 4. Create Pull Request -- Gitee From d886a3bb0f150a25e929026eb0ddc9fd3ff054d3 Mon Sep 17 00:00:00 2001 From: yangshuang70 Date: Mon, 23 Aug 2021 10:42:40 +0800 Subject: [PATCH 3/3] fix: agent copyright. --- .gitignore | 6 +- src/Makefile | 2 +- src/agent/common/da_array.c | 19 +- src/agent/common/da_array.h | 50 ++- src/agent/common/da_atomic.h | 21 +- src/agent/common/da_atomic_asm.h | 222 +++++----- src/agent/common/da_atomic_asm8.h | 295 ++++++------- src/agent/common/da_atomic_gcc.h | 117 ++--- src/agent/common/da_atomic_gcc8.h | 125 +++--- src/agent/common/da_queue.h | 44 +- src/agent/common/da_rbtree.c | 20 +- src/agent/common/da_rbtree.h | 45 +- src/agent/common/da_string.c | 19 +- src/agent/common/da_string.h | 138 +++--- src/agent/compiler.h | 30 +- src/agent/da.c | 22 +- src/agent/da_buf.c | 25 +- src/agent/da_buf.h | 57 +-- src/agent/da_client.c | 19 +- src/agent/da_client.h | 15 +- src/agent/da_conf.c | 23 +- src/agent/da_conf.h | 185 ++++---- src/agent/da_conn.c | 20 +- src/agent/da_conn.h | 156 +++---- src/agent/da_core.c | 21 +- src/agent/da_core.h | 92 ++-- src/agent/da_errno.h | 129 +++--- src/agent/da_listener.c | 27 +- src/agent/da_listener.h | 19 +- src/agent/da_log.c | 24 +- src/agent/da_log.h | 72 +++- src/agent/da_mem_pool.c | 26 +- src/agent/da_mem_pool.h | 79 ++-- src/agent/da_msg.c | 29 +- src/agent/da_msg.h | 239 ++++++----- src/agent/da_request.c | 21 +- src/agent/da_request.h | 40 +- src/agent/da_response.c | 21 +- src/agent/da_response.h | 19 +- src/agent/da_server.c | 33 +- src/agent/da_server.h | 159 +++---- src/agent/da_signal.c | 19 +- src/agent/da_signal.h | 38 +- src/agent/da_stats.c | 19 +- src/agent/da_stats.h | 413 +++++++++--------- src/agent/da_time.c | 19 +- src/agent/da_time.h | 615 ++++++++++++++------------- src/agent/da_top_percentile.c | 15 + src/agent/da_top_percentile.h | 46 +- src/agent/da_util.c | 19 +- src/agent/da_util.h | 121 +++--- src/agent/event/da_epoll.c | 19 +- src/agent/event/da_event.h | 35 +- src/agent/hashkit/da_chash.c | 19 +- src/agent/hashkit/da_crc16.c | 15 + src/agent/hashkit/da_crc32.c | 19 +- src/agent/hashkit/da_fnv.c | 15 + src/agent/hashkit/da_hsieh.c | 18 +- src/agent/hashkit/da_jenkins.c | 24 +- src/agent/hashkit/da_ketama.c | 29 +- src/agent/hashkit/da_md5.c | 25 +- src/agent/hashkit/da_murmur.c | 29 +- src/agent/hashkit/da_one_at_a_time.c | 27 +- src/agent/proto/da_dtcparse.c | 40 +- src/agent/proto/da_protocal.h | 46 +- src/libs/common/version.h | 2 +- 66 files changed, 2477 insertions(+), 1934 deletions(-) diff --git a/.gitignore b/.gitignore index d81b26c..04b4181 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,7 @@ # Prerequisites # -.history \ No newline at end of file +.history +.vscode +settings.json +*.o +.dep* diff --git a/src/Makefile b/src/Makefile index 198902f..6b25c6c 100644 --- a/src/Makefile +++ b/src/Makefile @@ -1,6 +1,6 @@ include Make.conf -SUBDIRS := configcenter libs devel daemons core connector +SUBDIRS := agent configcenter libs devel daemons core connector include Make.rules diff --git a/src/agent/common/da_array.c b/src/agent/common/da_array.c index bd52ad9..ca7bbf8 100644 --- a/src/agent/common/da_array.c +++ b/src/agent/common/da_array.c @@ -1,9 +1,18 @@ /* - * da_array.c - * - * Created on: 2014年12月3日 - * Author: Jiansong - */ +* Copyright [2021] JD.com, Inc. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ #include "da_array.h" #include "../da_log.h" diff --git a/src/agent/common/da_array.h b/src/agent/common/da_array.h index 9f13599..c2089ef 100644 --- a/src/agent/common/da_array.h +++ b/src/agent/common/da_array.h @@ -1,46 +1,54 @@ /* - * da_array.h + * Copyright [2021] JD.com, Inc. * - * Created on: 2014年12月3日 - * Author: Jiansong + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. */ #ifndef DA_ARRAY_H_ #define DA_ARRAY_H_ -#include "stdlib.h" #include "stdint.h" +#include "stdlib.h" typedef int (*array_compare_t)(const void *, const void *); typedef int (*array_each_t)(void *, void *); struct array { - uint32_t nelem; /* # element */ - void *elem; /* element */ - size_t size; /* element size */ - uint32_t nalloc; /* # allocated element */ + uint32_t nelem; /* # element */ + void *elem; /* element */ + size_t size; /* element size */ + uint32_t nalloc; /* # allocated element */ }; -#define null_array { 0, NULL, 0, 0 } +#define null_array \ + { 0, NULL, 0, 0 } static inline void array_null(struct array *a) { - a->nelem = 0; - a->elem = NULL; - a->size = 0; - a->nalloc = 0; + a->nelem = 0; + a->elem = NULL; + a->size = 0; + a->nalloc = 0; } static inline void array_set(struct array *a, void *elem, size_t size, - uint32_t nalloc) { - a->nelem = 0; - a->elem = elem; - a->size = size; - a->nalloc = nalloc; + uint32_t nalloc) { + a->nelem = 0; + a->elem = elem; + a->size = size; + a->nalloc = nalloc; } -static inline uint32_t array_n(const struct array *a) { - return a->nelem; -} +static inline uint32_t array_n(const struct array *a) { return a->nelem; } struct array *array_create(uint32_t n, size_t size); void array_destroy(struct array *a); diff --git a/src/agent/common/da_atomic.h b/src/agent/common/da_atomic.h index b8ce466..82550c9 100644 --- a/src/agent/common/da_atomic.h +++ b/src/agent/common/da_atomic.h @@ -1,3 +1,18 @@ +/* + * Copyright [2021] JD.com, Inc. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ #ifndef DA_ATOMIC__T #define DA_ATOMIC__T @@ -9,11 +24,11 @@ #include "da_atomic_gcc.h" #endif -#if __WORDSIZE==64 || __GNUC__ >= 5 || (__GNUC__==4 && __GNUC_MINOR__>=3) -#define HAS_ATOMIC8 1 +#if __WORDSIZE == 64 || __GNUC__ >= 5 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 3) +#define HAS_ATOMIC8 1 #include "da_atomic_gcc8.h" #else -#define HAS_ATOMIC8 1 +#define HAS_ATOMIC8 1 #include "da_atomic_asm8.h" #endif diff --git a/src/agent/common/da_atomic_asm.h b/src/agent/common/da_atomic_asm.h index 1ac5ab0..9b8f692 100644 --- a/src/agent/common/da_atomic_asm.h +++ b/src/agent/common/da_atomic_asm.h @@ -1,3 +1,18 @@ +/* + * Copyright [2021] JD.com, Inc. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ #ifndef __ARCH_I386_ATOMIC__ #define __ARCH_I386_ATOMIC__ @@ -11,171 +26,157 @@ __BEGIN_DECLS * on us. We need to use _exactly_ the address the user gave us, * not some alias that contains the same information. */ -typedef struct { volatile int counter; } atomic_t; +typedef struct { + volatile int counter; +} atomic_t; -#define ATOMIC_INIT(i) { (i) } +#define ATOMIC_INIT(i) \ + { (i) } /** * atomic_read - read atomic variable * @v: pointer of type atomic_t - * + * * Atomically reads the value of @v. - */ -#define atomic_read(v) ((v)->counter) + */ +#define atomic_read(v) ((v)->counter) /** * atomic_set - set atomic variable * @v: pointer of type atomic_t * @i: required value - * + * * Atomically sets the value of @v to @i. - */ -#define atomic_set(v,i) (((v)->counter) = (i)) - -static __inline__ int atomic_clear(atomic_t *v) -{ - int i; - __asm__ __volatile__( - " xorl %0, %0\n" - "lock; xchgl %0, %1;" - :"=r"(i) - :"m"(v->counter)); - return i; + */ +#define atomic_set(v, i) (((v)->counter) = (i)) + +static __inline__ int atomic_clear(atomic_t *v) { + int i; + __asm__ __volatile__(" xorl %0, %0\n" + "lock; xchgl %0, %1;" + : "=r"(i) + : "m"(v->counter)); + return i; } /** * atomic_add - add integer to atomic variable * @i: integer value to add * @v: pointer of type atomic_t - * + * * Atomically adds @i to @v. */ -static __inline__ int atomic_add(int i, atomic_t *v) -{ - int __i = i; - __asm__ __volatile__( - "lock; xaddl %0, %1;" - :"=r"(i) - :"m"(v->counter), "0"(i)); - return __i; +static __inline__ int atomic_add(int i, atomic_t *v) { + int __i = i; + __asm__ __volatile__("lock; xaddl %0, %1;" + : "=r"(i) + : "m"(v->counter), "0"(i)); + return __i; } /** * atomic_sub - subtract the atomic variable * @i: integer value to subtract * @v: pointer of type atomic_t - * + * * Atomically subtracts @i from @v. */ -static __inline__ int atomic_sub(int i, atomic_t *v) -{ - return atomic_add(-i, v); +static __inline__ int atomic_sub(int i, atomic_t *v) { + return atomic_add(-i, v); } /** * atomic_sub_and_test - subtract value from variable and test result * @i: integer value to subtract * @v: pointer of type atomic_t - * + * * Atomically subtracts @i from @v and returns * true if the result is zero, or false for all * other cases. */ -static __inline__ int atomic_sub_and_test(int i, atomic_t *v) -{ - unsigned char c; - - __asm__ __volatile__( - "lock; subl %2,%0; sete %1" - :"=m" (v->counter), "=qm" (c) - :"ir" (i), "m" (v->counter) : "memory"); - return c; +static __inline__ int atomic_sub_and_test(int i, atomic_t *v) { + unsigned char c; + + __asm__ __volatile__("lock; subl %2,%0; sete %1" + : "=m"(v->counter), "=qm"(c) + : "ir"(i), "m"(v->counter) + : "memory"); + return c; } /** * atomic_inc - increment atomic variable * @v: pointer of type atomic_t - * + * * Atomically increments @v by 1. - */ -static __inline__ void atomic_inc(atomic_t *v) -{ - __asm__ __volatile__( - "lock; incl %0" - :"=m" (v->counter) - :"m" (v->counter)); + */ +static __inline__ void atomic_inc(atomic_t *v) { + __asm__ __volatile__("lock; incl %0" : "=m"(v->counter) : "m"(v->counter)); } /** * atomic_dec - decrement atomic variable * @v: pointer of type atomic_t - * + * * Atomically decrements @v by 1. - */ -static __inline__ void atomic_dec(atomic_t *v) -{ - __asm__ __volatile__( - "lock; decl %0" - :"=m" (v->counter) - :"m" (v->counter)); + */ +static __inline__ void atomic_dec(atomic_t *v) { + __asm__ __volatile__("lock; decl %0" : "=m"(v->counter) : "m"(v->counter)); } /** * atomic_dec_and_test - decrement and test * @v: pointer of type atomic_t - * + * * Atomically decrements @v by 1 and * returns true if the result is 0, or false for all other * cases. - */ -static __inline__ int atomic_dec_and_test(atomic_t *v) -{ - unsigned char c; - - __asm__ __volatile__( - "lock; decl %0; sete %1" - :"=m" (v->counter), "=qm" (c) - :"m" (v->counter) : "memory"); - return c != 0; + */ +static __inline__ int atomic_dec_and_test(atomic_t *v) { + unsigned char c; + + __asm__ __volatile__("lock; decl %0; sete %1" + : "=m"(v->counter), "=qm"(c) + : "m"(v->counter) + : "memory"); + return c != 0; } /** - * atomic_inc_and_test - increment and test + * atomic_inc_and_test - increment and test * @v: pointer of type atomic_t - * + * * Atomically increments @v by 1 * and returns true if the result is zero, or false for all * other cases. - */ -static __inline__ int atomic_inc_and_test(atomic_t *v) -{ - unsigned char c; - - __asm__ __volatile__( - "lock; incl %0; sete %1" - :"=m" (v->counter), "=qm" (c) - :"m" (v->counter) : "memory"); - return c != 0; + */ +static __inline__ int atomic_inc_and_test(atomic_t *v) { + unsigned char c; + + __asm__ __volatile__("lock; incl %0; sete %1" + : "=m"(v->counter), "=qm"(c) + : "m"(v->counter) + : "memory"); + return c != 0; } /** * atomic_add_negative - add and test if negative * @v: pointer of type atomic_t * @i: integer value to add - * + * * Atomically adds @i to @v and returns true * if the result is negative, or false when * result is greater than or equal to zero. - */ -static __inline__ int atomic_add_negative(int i, atomic_t *v) -{ - unsigned char c; - - __asm__ __volatile__( - "lock; addl %2,%0; sets %1" - :"=m" (v->counter), "=qm" (c) - :"ir" (i), "m" (v->counter) : "memory"); - return c; + */ +static __inline__ int atomic_add_negative(int i, atomic_t *v) { + unsigned char c; + + __asm__ __volatile__("lock; addl %2,%0; sets %1" + : "=m"(v->counter), "=qm"(c) + : "ir"(i), "m"(v->counter) + : "memory"); + return c; } /** @@ -185,32 +186,31 @@ static __inline__ int atomic_add_negative(int i, atomic_t *v) * * Atomically adds @i to @v and returns @i + @v */ -static __inline__ int atomic_add_return(int i, atomic_t *v) -{ - int __i = i; - __asm__ __volatile__( - "lock; xaddl %0, %1;" - :"=r"(i) - :"m"(v->counter), "0"(i)); - return i + __i; +static __inline__ int atomic_add_return(int i, atomic_t *v) { + int __i = i; + __asm__ __volatile__("lock; xaddl %0, %1;" + : "=r"(i) + : "m"(v->counter), "0"(i)); + return i + __i; } -static __inline__ int atomic_sub_return(int i, atomic_t *v) -{ - return atomic_add_return(-i,v); +static __inline__ int atomic_sub_return(int i, atomic_t *v) { + return atomic_add_return(-i, v); } -#define atomic_inc_return(v) (atomic_add_return(1,v)) -#define atomic_dec_return(v) (atomic_sub_return(1,v)) +#define atomic_inc_return(v) (atomic_add_return(1, v)) +#define atomic_dec_return(v) (atomic_sub_return(1, v)) /* These are x86-specific, used by some header files */ -#define atomic_clear_mask(mask, addr) \ -__asm__ __volatile__("lock; andl %0,%1" \ -: : "r" (~(mask)),"m" (*addr) : "memory") - -#define atomic_set_mask(mask, addr) \ -__asm__ __volatile__("lock; orl %0,%1" \ -: : "r" (mask),"m" (*(addr)) : "memory") +#define atomic_clear_mask(mask, addr) \ + __asm__ __volatile__("lock; andl %0,%1" \ + : \ + : "r"(~(mask)), "m"(*addr) \ + : "memory") + +#define atomic_set_mask(mask, addr) \ + __asm__ __volatile__("lock; orl %0,%1" : : "r"(mask), "m"(*(addr)) : "memor" \ + "y") __END_DECLS #endif diff --git a/src/agent/common/da_atomic_asm8.h b/src/agent/common/da_atomic_asm8.h index e282a5d..2a39af7 100644 --- a/src/agent/common/da_atomic_asm8.h +++ b/src/agent/common/da_atomic_asm8.h @@ -1,3 +1,18 @@ +/* + * Copyright [2021] JD.com, Inc. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ #ifndef __ARCH_I386_ATOMIC8__ #define __ARCH_I386_ATOMIC8__ @@ -16,118 +31,114 @@ __BEGIN_DECLS * on us. We need to use _exactly_ the address the user gave us, * not some alias that contains the same information. */ -typedef struct { volatile long long counter; } atomic8_t; +typedef struct { + volatile long long counter; +} atomic8_t; -#define ATOMIC8_INIT(i) { (i) } +#define ATOMIC8_INIT(i) \ + { (i) } /** * atomic8_read - read atomic variable * @v: pointer of type atomic8_t - * + * * Atomically reads the value of @v. - */ -static __inline__ long long atomic8_read(atomic8_t *v) -{ - register long long r; - __asm__ __volatile__( - /* original value for ebx, ecx doesn't matter */ - " movl %%ecx, %%edx\n" - " movl %%ebx, %%eax\n" - "lock; cmpxchg8b %1\n" - : "=&A" (r), "+o" (v->counter) - : - : "memory", "cc"); - return r; + */ +static __inline__ long long atomic8_read(atomic8_t *v) { + register long long r; + __asm__ __volatile__( + /* original value for ebx, ecx doesn't matter */ + " movl %%ecx, %%edx\n" + " movl %%ebx, %%eax\n" + "lock; cmpxchg8b %1\n" + : "=&A"(r), "+o"(v->counter) + : + : "memory", "cc"); + return r; } /** * atomic8_set - set atomic variable * @v: pointer of type atomic8_t * @i: required value - * + * * Atomically sets the value of @v to @i. - */ -static __inline__ long long atomic8_return_set(atomic8_t *v, long long i) -{ - register long long r = v->counter; + */ +static __inline__ long long atomic8_return_set(atomic8_t *v, long long i) { + register long long r = v->counter; #if __pic__ || __PIC__ - __asm__ __volatile__( - " pushl %%ebx\n" - " movl %2, %%ebx\n" - "1: repz; nop\n" // aka pause - "lock; cmpxchg8b (%1)\n" - " jnz 1b\n" - " popl %%ebx\n" - : "+A"(r) - : "ir"(v), "ir" ((int)i), "c"((int)((unsigned long long)i>>32)) - : "memory", "cc"); + __asm__ __volatile__(" pushl %%ebx\n" + " movl %2, %%ebx\n" + "1: repz; nop\n" // aka pause + "lock; cmpxchg8b (%1)\n" + " jnz 1b\n" + " popl %%ebx\n" + : "+A"(r) + : "ir"(v), "ir"((int)i), + "c"((int)((unsigned long long)i >> 32)) + : "memory", "cc"); #else - __asm__ __volatile__( - "1: repz; nop\n" // aka pause - "lock; cmpxchg8b %1\n" - " jnz 1b\n" - : "+A"(r), "+o" (v->counter) - : "b" ((int)i), "c"((int)((unsigned long long)i>>32)) - : "memory", "cc"); + __asm__ __volatile__("1: repz; nop\n" // aka pause + "lock; cmpxchg8b %1\n" + " jnz 1b\n" + : "+A"(r), "+o"(v->counter) + : "b"((int)i), "c"((int)((unsigned long long)i >> 32)) + : "memory", "cc"); #endif - return r; + return r; } -static __inline__ void atomic8_set(atomic8_t *v, long long i) -{ - atomic8_return_set(v, i); +static __inline__ void atomic8_set(atomic8_t *v, long long i) { + atomic8_return_set(v, i); } /** * atomic8_set - set atomic variable * @v: pointer of type atomic8_t * @i: required value - * + * * Atomically sets the value of @v to @i. - */ -static __inline__ long long atomic8_clear(atomic8_t *v) -{ - return atomic8_return_set(v, 0); + */ +static __inline__ long long atomic8_clear(atomic8_t *v) { + return atomic8_return_set(v, 0); } /** * atomic8_add - add integer to atomic variable * @i: integer value to add * @v: pointer of type atomic8_t - * + * * Atomically adds @i to @v. */ -static __inline__ long long atomic8_add(long long i, atomic8_t *v) -{ - register long long r = v->counter; +static __inline__ long long atomic8_add(long long i, atomic8_t *v) { + register long long r = v->counter; #if __pic__ || __PIC__ - __asm__ __volatile__( - " pushl %%ebx\n" - "1: movl %2, %%ebx\n" - " movl %3, %%ecx\n" - " addl %%eax, %%ebx\n" - " adcl %%edx, %%ecx\n" - "repz; nop\n" - "lock; cmpxchg8b (%1)\n" - " jnz 1b\n" - " popl %%ebx\n" - : "+A" (r) - : "ir"(v), "ir" ((int)i), "ir"((int)((unsigned long long)i>>32)) - : "memory", "ecx", "cc"); + __asm__ __volatile__(" pushl %%ebx\n" + "1: movl %2, %%ebx\n" + " movl %3, %%ecx\n" + " addl %%eax, %%ebx\n" + " adcl %%edx, %%ecx\n" + "repz; nop\n" + "lock; cmpxchg8b (%1)\n" + " jnz 1b\n" + " popl %%ebx\n" + : "+A"(r) + : "ir"(v), "ir"((int)i), + "ir"((int)((unsigned long long)i >> 32)) + : "memory", "ecx", "cc"); #else - __asm__ __volatile__( - "1: movl %2, %%ebx\n" - " movl %3, %%ecx\n" - " addl %%eax, %%ebx\n" - " adcl %%edx, %%ecx\n" - "repz; nop\n" - "lock; cmpxchg8b %1\n" - " jnz 1b\n" - : "+A"(r), "+o" (v->counter) - : "g" ((int)i), "g"((int)((unsigned long long)i>>32)) - : "memory", "ebx", "ecx", "cc"); + __asm__ __volatile__("1: movl %2, %%ebx\n" + " movl %3, %%ecx\n" + " addl %%eax, %%ebx\n" + " adcl %%edx, %%ecx\n" + "repz; nop\n" + "lock; cmpxchg8b %1\n" + " jnz 1b\n" + : "+A"(r), "+o"(v->counter) + : "g"((int)i), "g"((int)((unsigned long long)i >> 32)) + : "memory", "ebx", "ecx", "cc"); #endif - return r; + return r; } /** @@ -137,138 +148,128 @@ static __inline__ long long atomic8_add(long long i, atomic8_t *v) * * Atomically adds @i to @v and returns @i + @v */ -static __inline__ long long atomic8_add_return(long long i, atomic8_t *v) -{ - register long long r = v->counter; +static __inline__ long long atomic8_add_return(long long i, atomic8_t *v) { + register long long r = v->counter; #if __pic__ || __PIC__ - __asm__ __volatile__( - " pushl %%ebx\n" - "1: movl %2, %%ebx\n" - " movl %3, %%ecx\n" - " addl %%eax, %%ebx\n" - " adcl %%edx, %%ecx\n" - "repz; nop\n" - "lock; cmpxchg8b (%1)\n" - " jnz 1b\n" - " movl %%ebx, %%eax\n" - " movl %%ecx, %%edx\n" - " popl %%ebx\n" - : "+A" (r) - : "ir"(v), "ir" ((int)i), "ir"((int)((unsigned long long)i>>32)) - : "memory", "ecx", "cc"); + __asm__ __volatile__(" pushl %%ebx\n" + "1: movl %2, %%ebx\n" + " movl %3, %%ecx\n" + " addl %%eax, %%ebx\n" + " adcl %%edx, %%ecx\n" + "repz; nop\n" + "lock; cmpxchg8b (%1)\n" + " jnz 1b\n" + " movl %%ebx, %%eax\n" + " movl %%ecx, %%edx\n" + " popl %%ebx\n" + : "+A"(r) + : "ir"(v), "ir"((int)i), + "ir"((int)((unsigned long long)i >> 32)) + : "memory", "ecx", "cc"); #else - __asm__ __volatile__( - "1: movl %2, %%ebx\n" - " movl %3, %%ecx\n" - " addl %%eax, %%ebx\n" - " adcl %%edx, %%ecx\n" - "repz; nop\n" - "lock; cmpxchg8b %1\n" - " jnz 1b\n" - " movl %%ebx, %%eax\n" - " movl %%ecx, %%edx\n" - : "+A" (r), "+o" (v->counter) - : "g" ((int)i), "g"((int)((unsigned long long)i>>32)) - : "memory", "ebx", "ecx", "cc"); + __asm__ __volatile__("1: movl %2, %%ebx\n" + " movl %3, %%ecx\n" + " addl %%eax, %%ebx\n" + " adcl %%edx, %%ecx\n" + "repz; nop\n" + "lock; cmpxchg8b %1\n" + " jnz 1b\n" + " movl %%ebx, %%eax\n" + " movl %%ecx, %%edx\n" + : "+A"(r), "+o"(v->counter) + : "g"((int)i), "g"((int)((unsigned long long)i >> 32)) + : "memory", "ebx", "ecx", "cc"); #endif - return r; + return r; } /** * atomic8_sub - subtract the atomic variable * @i: integer value to subtract * @v: pointer of type atomic8_t - * + * * Atomically subtracts @i from @v. */ -static __inline__ long long atomic8_sub(long long i, atomic8_t *v) -{ - return atomic8_add(-i, v); +static __inline__ long long atomic8_sub(long long i, atomic8_t *v) { + return atomic8_add(-i, v); } /** * atomic8_sub_and_test - subtract value from variable and test result * @i: integer value to subtract * @v: pointer of type atomic8_t - * + * * Atomically subtracts @i from @v and returns * true if the result is zero, or false for all * other cases. */ -static __inline__ int atomic8_sub_and_test(long long i, atomic8_t *v) -{ - return atomic8_add_return(-i, v)==0; +static __inline__ int atomic8_sub_and_test(long long i, atomic8_t *v) { + return atomic8_add_return(-i, v) == 0; } /** * atomic8_inc - increment atomic variable * @v: pointer of type atomic8_t - * + * * Atomically increments @v by 1. - */ -static __inline__ long long atomic8_inc(atomic8_t *v) -{ - return atomic8_add(1, v); + */ +static __inline__ long long atomic8_inc(atomic8_t *v) { + return atomic8_add(1, v); } /** * atomic8_dec - decrement atomic variable * @v: pointer of type atomic8_t - * + * * Atomically decrements @v by 1. - */ -static __inline__ long long atomic8_dec(atomic8_t *v) -{ - return atomic8_add(-1, v); + */ +static __inline__ long long atomic8_dec(atomic8_t *v) { + return atomic8_add(-1, v); } /** * atomic8_dec_and_test - decrement and test * @v: pointer of type atomic8_t - * + * * Atomically decrements @v by 1 and * returns true if the result is 0, or false for all other * cases. - */ -static __inline__ int atomic8_dec_and_test(atomic8_t *v) -{ - return atomic8_add_return(-1, v)==0; + */ +static __inline__ int atomic8_dec_and_test(atomic8_t *v) { + return atomic8_add_return(-1, v) == 0; } /** - * atomic8_inc_and_test - increment and test + * atomic8_inc_and_test - increment and test * @v: pointer of type atomic8_t - * + * * Atomically increments @v by 1 * and returns true if the result is zero, or false for all * other cases. - */ -static __inline__ int atomic8_inc_and_test(atomic8_t *v) -{ - return atomic8_add_return(1, v)==0; + */ +static __inline__ int atomic8_inc_and_test(atomic8_t *v) { + return atomic8_add_return(1, v) == 0; } /** * atomic8_add_negative - add and test if negative * @v: pointer of type atomic8_t * @i: integer value to add - * + * * Atomically adds @i to @v and returns true * if the result is negative, or false when * result is greater than or equal to zero. - */ -static __inline__ int atomic8_add_negative(long long i, atomic8_t *v) -{ - return atomic8_add_return(i, v) < 0; + */ +static __inline__ int atomic8_add_negative(long long i, atomic8_t *v) { + return atomic8_add_return(i, v) < 0; } -static __inline__ long long atomic8_sub_return(long long i, atomic8_t *v) -{ - return atomic8_add_return(-i,v); +static __inline__ long long atomic8_sub_return(long long i, atomic8_t *v) { + return atomic8_add_return(-i, v); } -#define atomic8_inc_return(v) (atomic8_add_return(1,v)) -#define atomic8_dec_return(v) (atomic8_sub_return(1,v)) +#define atomic8_inc_return(v) (atomic8_add_return(1, v)) +#define atomic8_dec_return(v) (atomic8_sub_return(1, v)) __END_DECLS #endif diff --git a/src/agent/common/da_atomic_gcc.h b/src/agent/common/da_atomic_gcc.h index 7acb5f9..a6d95bb 100644 --- a/src/agent/common/da_atomic_gcc.h +++ b/src/agent/common/da_atomic_gcc.h @@ -1,3 +1,18 @@ +/* + * Copyright [2021] JD.com, Inc. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ #ifndef __ARCH_I386_ATOMIC__ #define __ARCH_I386_ATOMIC__ @@ -11,128 +26,120 @@ __BEGIN_DECLS */ typedef volatile int atomic_t; -#define ATOMIC_INIT(i) { (i) } +#define ATOMIC_INIT(i) \ + { (i) } /** * atomic_read - read atomic variable * @v: pointer of type atomic_t - * + * * Atomically reads the value of @v. - */ -#define atomic_read(v) (*(v)) + */ +#define atomic_read(v) (*(v)) /** * atomic_set - set atomic variable * @v: pointer of type atomic_t * @i: required value - * + * * Atomically sets the value of @v to @i. - */ -#define atomic_set(v,i) (*(v) = (i)) + */ +#define atomic_set(v, i) (*(v) = (i)) -static __inline__ int atomic_clear(atomic_t *v) -{ - return __sync_fetch_and_and(v, 0); +static __inline__ int atomic_clear(atomic_t *v) { + return __sync_fetch_and_and(v, 0); } /** * atomic_add - add integer to atomic variable * @i: integer value to add * @v: pointer of type atomic_t - * + * * Atomically adds @i to @v. */ -static __inline__ int atomic_add(int i, atomic_t *v) -{ - return __sync_fetch_and_add(v, i); +static __inline__ int atomic_add(int i, atomic_t *v) { + return __sync_fetch_and_add(v, i); } /** * atomic_sub - subtract the atomic variable * @i: integer value to subtract * @v: pointer of type atomic_t - * + * * Atomically subtracts @i from @v. */ -static __inline__ int atomic_sub(int i, atomic_t *v) -{ - return __sync_fetch_and_sub(v, i); +static __inline__ int atomic_sub(int i, atomic_t *v) { + return __sync_fetch_and_sub(v, i); } /** * atomic_sub_and_test - subtract value from variable and test result * @i: integer value to subtract * @v: pointer of type atomic_t - * + * * Atomically subtracts @i from @v and returns * true if the result is zero, or false for all * other cases. */ -static __inline__ int atomic_sub_and_test(int i, atomic_t *v) -{ - return __sync_sub_and_fetch(v, i) == 0; +static __inline__ int atomic_sub_and_test(int i, atomic_t *v) { + return __sync_sub_and_fetch(v, i) == 0; } /** * atomic_inc - increment atomic variable * @v: pointer of type atomic_t - * + * * Atomically increments @v by 1. - */ -static __inline__ int atomic_inc(atomic_t *v) -{ - return __sync_fetch_and_add(v, 1); + */ +static __inline__ int atomic_inc(atomic_t *v) { + return __sync_fetch_and_add(v, 1); } /** * atomic_dec - decrement atomic variable * @v: pointer of type atomic_t - * + * * Atomically decrements @v by 1. - */ -static __inline__ int atomic_dec(atomic_t *v) -{ - return __sync_fetch_and_sub(v, 1); + */ +static __inline__ int atomic_dec(atomic_t *v) { + return __sync_fetch_and_sub(v, 1); } /** * atomic_dec_and_test - decrement and test * @v: pointer of type atomic_t - * + * * Atomically decrements @v by 1 and * returns true if the result is 0, or false for all other * cases. - */ -static __inline__ int atomic_dec_and_test(atomic_t *v) -{ - return __sync_sub_and_fetch(v, 1) == 0; + */ +static __inline__ int atomic_dec_and_test(atomic_t *v) { + return __sync_sub_and_fetch(v, 1) == 0; } /** - * atomic_inc_and_test - increment and test + * atomic_inc_and_test - increment and test * @v: pointer of type atomic_t - * + * * Atomically increments @v by 1 * and returns true if the result is zero, or false for all * other cases. - */ -static __inline__ int atomic_inc_and_test(atomic_t *v) -{ - return __sync_add_and_fetch(v, 1) == 0; + */ +static __inline__ int atomic_inc_and_test(atomic_t *v) { + return __sync_add_and_fetch(v, 1) == 0; } /** * atomic_add_negative - add and test if negative * @v: pointer of type atomic_t * @i: integer value to add - * + * * Atomically adds @i to @v and returns true * if the result is negative, or false when * result is greater than or equal to zero. - */ -static __inline__ int atomic_add_negative(int i, atomic_t *v) -{ - return __sync_add_and_fetch(v, i) < 0; + */ +static __inline__ int atomic_add_negative(int i, atomic_t *v) { + return __sync_add_and_fetch(v, i) < 0; } /** @@ -142,18 +149,16 @@ static __inline__ int atomic_add_negative(int i, atomic_t *v) * * Atomically adds @i to @v and returns @i + @v */ -static __inline__ int atomic_add_return(int i, atomic_t *v) -{ - return __sync_add_and_fetch(v, i); +static __inline__ int atomic_add_return(int i, atomic_t *v) { + return __sync_add_and_fetch(v, i); } -static __inline__ int atomic_sub_return(int i, atomic_t *v) -{ - return __sync_sub_and_fetch(v, i); +static __inline__ int atomic_sub_return(int i, atomic_t *v) { + return __sync_sub_and_fetch(v, i); } -#define atomic_inc_return(v) (atomic_add_return(1,v)) -#define atomic_dec_return(v) (atomic_sub_return(1,v)) +#define atomic_inc_return(v) (atomic_add_return(1, v)) +#define atomic_dec_return(v) (atomic_sub_return(1, v)) __END_DECLS #endif diff --git a/src/agent/common/da_atomic_gcc8.h b/src/agent/common/da_atomic_gcc8.h index 8bce999..50a7a2a 100644 --- a/src/agent/common/da_atomic_gcc8.h +++ b/src/agent/common/da_atomic_gcc8.h @@ -1,3 +1,18 @@ +/* + * Copyright [2021] JD.com, Inc. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ #ifndef __ARCH_I386_ATOMIC8__ #define __ARCH_I386_ATOMIC8__ @@ -11,135 +26,127 @@ __BEGIN_DECLS */ typedef volatile int64_t atomic8_t; -#define ATOMIC_INIT(i) { (i) } +#define ATOMIC_INIT(i) \ + { (i) } /** * atomic8_read - read atomic8 variable * @v: pointer of type atomic8_t - * + * * Atomically reads the value of @v. - */ -#if __WORDSIZE==64 -#define atomic8_read(v) (*(v)) + */ +#if __WORDSIZE == 64 +#define atomic8_read(v) (*(v)) #else -#define atomic8_read(v) atomic8_add_return(0,v) +#define atomic8_read(v) atomic8_add_return(0, v) #endif /** * atomic8_set - set atomic8 variable * @v: pointer of type atomic8_t * @i: required value - * + * * Atomically sets the value of @v to @i. - */ -#if __WORDSIZE==64 -#define atomic8_set(v,i) (*(v) = (i)) + */ +#if __WORDSIZE == 64 +#define atomic8_set(v, i) (*(v) = (i)) #else -#define atomic8_set(v,i) (*(v) = (i)) +#define atomic8_set(v, i) (*(v) = (i)) #endif -static __inline__ int64_t atomic8_clear(atomic8_t *v) -{ - return __sync_fetch_and_and(v, 0); +static __inline__ int64_t atomic8_clear(atomic8_t *v) { + return __sync_fetch_and_and(v, 0); } /** * atomic8_add - add integer to atomic8 variable * @i: integer value to add * @v: pointer of type atomic8_t - * + * * Atomically adds @i to @v. */ -static __inline__ int64_t atomic8_add(int64_t i, atomic8_t *v) -{ - return __sync_fetch_and_add(v, i); +static __inline__ int64_t atomic8_add(int64_t i, atomic8_t *v) { + return __sync_fetch_and_add(v, i); } /** * atomic8_sub - subtract the atomic8 variable * @i: integer value to subtract * @v: pointer of type atomic8_t - * + * * Atomically subtracts @i from @v. */ -static __inline__ int64_t atomic8_sub(int64_t i, atomic8_t *v) -{ - return __sync_fetch_and_sub(v, i); +static __inline__ int64_t atomic8_sub(int64_t i, atomic8_t *v) { + return __sync_fetch_and_sub(v, i); } /** * atomic8_sub_and_test - subtract value from variable and test result * @i: integer value to subtract * @v: pointer of type atomic8_t - * + * * Atomically subtracts @i from @v and returns * true if the result is zero, or false for all * other cases. */ -static __inline__ int atomic8_sub_and_test(int64_t i, atomic8_t *v) -{ - return __sync_sub_and_fetch(v, i) == 0; +static __inline__ int atomic8_sub_and_test(int64_t i, atomic8_t *v) { + return __sync_sub_and_fetch(v, i) == 0; } /** * atomic8_inc - increment atomic8 variable * @v: pointer of type atomic8_t - * + * * Atomically increments @v by 1. - */ -static __inline__ int64_t atomic8_inc(atomic8_t *v) -{ - return __sync_fetch_and_add(v, 1); + */ +static __inline__ int64_t atomic8_inc(atomic8_t *v) { + return __sync_fetch_and_add(v, 1); } /** * atomic8_dec - decrement atomic8 variable * @v: pointer of type atomic8_t - * + * * Atomically decrements @v by 1. - */ -static __inline__ int64_t atomic8_dec(atomic8_t *v) -{ - return __sync_fetch_and_sub(v, 1); + */ +static __inline__ int64_t atomic8_dec(atomic8_t *v) { + return __sync_fetch_and_sub(v, 1); } /** * atomic8_dec_and_test - decrement and test * @v: pointer of type atomic8_t - * + * * Atomically decrements @v by 1 and * returns true if the result is 0, or false for all other * cases. - */ -static __inline__ int atomic8_dec_and_test(atomic8_t *v) -{ - return __sync_sub_and_fetch(v, 1) == 0; + */ +static __inline__ int atomic8_dec_and_test(atomic8_t *v) { + return __sync_sub_and_fetch(v, 1) == 0; } /** - * atomic8_inc_and_test - increment and test + * atomic8_inc_and_test - increment and test * @v: pointer of type atomic8_t - * + * * Atomically increments @v by 1 * and returns true if the result is zero, or false for all * other cases. - */ -static __inline__ int atomic8_inc_and_test(atomic8_t *v) -{ - return __sync_add_and_fetch(v, 1) == 0; + */ +static __inline__ int atomic8_inc_and_test(atomic8_t *v) { + return __sync_add_and_fetch(v, 1) == 0; } /** * atomic8_add_negative - add and test if negative * @v: pointer of type atomic8_t * @i: integer value to add - * + * * Atomically adds @i to @v and returns true * if the result is negative, or false when * result is greater than or equal to zero. - */ -static __inline__ int atomic8_add_negative(int64_t i, atomic8_t *v) -{ - return __sync_add_and_fetch(v, i) < 0; + */ +static __inline__ int atomic8_add_negative(int64_t i, atomic8_t *v) { + return __sync_add_and_fetch(v, i) < 0; } /** @@ -149,18 +156,16 @@ static __inline__ int atomic8_add_negative(int64_t i, atomic8_t *v) * * Atomically adds @i to @v and returns @i + @v */ -static __inline__ int64_t atomic8_add_return(int64_t i, atomic8_t *v) -{ - return __sync_add_and_fetch(v, i); +static __inline__ int64_t atomic8_add_return(int64_t i, atomic8_t *v) { + return __sync_add_and_fetch(v, i); } -static __inline__ int64_t atomic8_sub_return(int64_t i, atomic8_t *v) -{ - return __sync_sub_and_fetch(v, i); +static __inline__ int64_t atomic8_sub_return(int64_t i, atomic8_t *v) { + return __sync_sub_and_fetch(v, i); } -#define atomic8_inc_return(v) (atomic8_add_return(1,v)) -#define atomic8_dec_return(v) (atomic8_sub_return(1,v)) +#define atomic8_inc_return(v) (atomic8_add_return(1, v)) +#define atomic8_dec_return(v) (atomic8_sub_return(1, v)) __END_DECLS #endif diff --git a/src/agent/common/da_queue.h b/src/agent/common/da_queue.h index 569d856..6b8a492 100644 --- a/src/agent/common/da_queue.h +++ b/src/agent/common/da_queue.h @@ -1,34 +1,18 @@ /* - * Copyright (c) 1991, 1993 - * The Regents of the University of California. All rights reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions - * are met: - * 1. Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * 2. 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. - * 4. Neither the name of the University 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 THE REGENTS AND CONTRIBUTORS ``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 THE REGENTS OR CONTRIBUTORS 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. - * - * @(#)queue.h 8.5 (Berkeley) 8/20/94 - * $FreeBSD: src/sys/sys/queue.h,v 1.73 2010/02/20 01:05:30 emaste Exp $ - */ +* Copyright [2021] JD.com, Inc. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ #ifndef _DA_QUEUE_H_ #define _DA_QUEUE_H_ diff --git a/src/agent/common/da_rbtree.c b/src/agent/common/da_rbtree.c index 2d5c7d8..be3966d 100644 --- a/src/agent/common/da_rbtree.c +++ b/src/agent/common/da_rbtree.c @@ -1,10 +1,18 @@ /* - * da_rbtree.c - * - * Created on: 2014年12月2日 - * Author: Jiansong - */ - +* Copyright [2021] JD.com, Inc. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ #include"da_rbtree.h" diff --git a/src/agent/common/da_rbtree.h b/src/agent/common/da_rbtree.h index d8ce8da..b5f0e20 100644 --- a/src/agent/common/da_rbtree.h +++ b/src/agent/common/da_rbtree.h @@ -1,40 +1,49 @@ /* - * da_rbtree.h + * Copyright [2021] JD.com, Inc. * - * Created on: 2014年12月2日 - * Author: Jiansong + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. */ #ifndef DA_RBTREE_H_ #define DA_RBTREE_H_ -#include -#include +#include +#include -#define rbtree_red(_node) ((_node)->color = 1) -#define rbtree_black(_node) ((_node)->color = 0) -#define rbtree_is_red(_node) ((_node)->color) -#define rbtree_is_black(_node) (!rbtree_is_red(_node)) +#define rbtree_red(_node) ((_node)->color = 1) +#define rbtree_black(_node) ((_node)->color = 0) +#define rbtree_is_red(_node) ((_node)->color) +#define rbtree_is_black(_node) (!rbtree_is_red(_node)) #define rbtree_copy_color(_n1, _n2) ((_n1)->color = (_n2)->color) struct rbnode { - struct rbnode *left; /* left link */ - struct rbnode *right; /* right link */ - struct rbnode *parent; /* parent link */ - uint64_t key; /* key for ordering */ - void *data; /* opaque data */ - uint8_t color; /* red | black */ + struct rbnode *left; /* left link */ + struct rbnode *right; /* right link */ + struct rbnode *parent; /* parent link */ + uint64_t key; /* key for ordering */ + void *data; /* opaque data */ + uint8_t color; /* red | black */ }; struct rbtree { - struct rbnode *root; /* root node */ - struct rbnode *sentinel; /* nil node */ + struct rbnode *root; /* root node */ + struct rbnode *sentinel; /* nil node */ }; void rbtree_node_init(struct rbnode *node); void rbtree_init(struct rbtree *tree, struct rbnode *node); struct rbnode *rbtree_min(struct rbtree *tree); -struct rbnode *rbtree_search(struct rbtree *tree,struct rbnode *tnode); +struct rbnode *rbtree_search(struct rbtree *tree, struct rbnode *tnode); void rbtree_insert(struct rbtree *tree, struct rbnode *node); void rbtree_delete(struct rbtree *tree, struct rbnode *node); diff --git a/src/agent/common/da_string.c b/src/agent/common/da_string.c index c851ac0..d2ee610 100644 --- a/src/agent/common/da_string.c +++ b/src/agent/common/da_string.c @@ -1,9 +1,18 @@ /* - * da_string.c - * - * Created on: 2014年12月4日 - * Author: Jiansong - */ +* Copyright [2021] JD.com, Inc. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ #include "da_string.h" #include diff --git a/src/agent/common/da_string.h b/src/agent/common/da_string.h index 98d51e9..19cccfc 100644 --- a/src/agent/common/da_string.h +++ b/src/agent/common/da_string.h @@ -1,40 +1,53 @@ /* - * da_string.h + * Copyright [2021] JD.com, Inc. * - * Created on: 2014年12月4日 - * Author: Jiansong + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. */ #ifndef DA_STRING_H_ #define DA_STRING_H_ -#include -#include +#include "../da_util.h" #include #include #include -#include "../da_util.h" +#include +#include /* * warning:you must manage the memory yourself */ struct string { - uint32_t len; /* string length */ - uint8_t *data; /* string data */ + uint32_t len; /* string length */ + uint8_t *data; /* string data */ }; -#define string(_str) { sizeof(_str) - 1, (uint8_t *)(_str) } -#define null_string { 0, NULL } +#define string(_str) \ + { sizeof(_str) - 1, (uint8_t *)(_str) } +#define null_string \ + { 0, NULL } -#define string_set_text(_str, _text) do { \ - (_str)->len = (uint32_t)(sizeof(_text) - 1);\ - (_str)->data = (uint8_t *)(_text); \ -} while (0); +#define string_set_text(_str, _text) \ + do { \ + (_str)->len = (uint32_t)(sizeof(_text) - 1); \ + (_str)->data = (uint8_t *)(_text); \ + } while (0); -#define string_set_raw(_str, _raw) do { \ - (_str)->len = (uint32_t)(da_strlen(_raw)); \ - (_str)->data = (uint8_t *)(_raw); \ -} while (0); +#define string_set_raw(_str, _raw) \ + do { \ + (_str)->len = (uint32_t)(da_strlen(_raw)); \ + (_str)->data = (uint8_t *)(_raw); \ + } while (0); void string_init(struct string *str); void string_deinit(struct string *str); @@ -44,61 +57,52 @@ int string_copy(struct string *dst, const uint8_t *src, uint32_t srclen); int string_compare(const struct string *s1, const struct string *s2); static inline uint8_t *_da_strchr(uint8_t *p, uint8_t *last, uint8_t c) { - while (p < last) { - if (*p == c) { - return p; - } - p++; - } - return NULL; + while (p < last) { + if (*p == c) { + return p; + } + p++; + } + return NULL; } static inline uint8_t *_da_strrchr(uint8_t *p, uint8_t *start, uint8_t c) { - while (p >= start) { - if (*p == c) { - return p; - } - p--; - } + while (p >= start) { + if (*p == c) { + return p; + } + p--; + } - return NULL; + return NULL; } -#define da_strcpy(_d,_s) \ - strcpy((char *)_d,(char *)_s) -#define da_strncpy(_d,_s,_n) \ - strncpy((char *)_d,(char *)_s,(size_t)(_n)) -#define da_strcat(_d,_s) \ - strcat((char *)_d,(char *)_s) -#define da_memcpy(_d, _c, _n) \ - memcpy(_d, _c, (size_t)(_n)) -#define da_memmove(_d, _c, _n) \ - memmove(_d, _c, (size_t)(_n)) -#define da_memchr(_d, _c, _n) \ - memchr(_d, _c, (size_t)(_n)) -#define da_strlen(_s) \ - strlen((char *)(_s)) -#define da_strncmp(_s1, _s2, _n) \ - strncmp((char *)(_s1), (char *)(_s2), (size_t)(_n)) -#define da_strchr(_p, _l, _c) \ - _da_strchr((uint8_t *)(_p), (uint8_t *)(_l), (uint8_t)(_c)) -#define da_strrchr(_p, _s, _c) \ - _da_strrchr((uint8_t *)(_p),(uint8_t *)(_s), (uint8_t)(_c)) -#define da_strndup(_s, _n) \ - (uint8_t *)strndup((char *)(_s), (size_t)(_n)); -#define da_snprintf(_s, _n, ...) \ - snprintf((char *)(_s), (size_t)(_n), __VA_ARGS__) -#define da_scnprintf(_s, _n, ...) \ - _scnprintf((char *)(_s), (size_t)(_n), __VA_ARGS__) -#define da_vscnprintf(_s, _n, _f, _a) \ - _vscnprintf((char *)(_s), (size_t)(_n), _f, _a) -#define da_strftime(_s, _n, fmt, tm) \ - (int)strftime((char *)(_s), (size_t)(_n), fmt, tm) -#define da_safe_snprintf(_s, _n, ...) \ - _safe_snprintf((char *)(_s), (size_t)(_n), __VA_ARGS__) -#define da_safe_vsnprintf(_s, _n, _f, _a) \ - _safe_vsnprintf((char *)(_s), (size_t)(_n), _f, _a) - +#define da_strcpy(_d, _s) strcpy((char *)_d, (char *)_s) +#define da_strncpy(_d, _s, _n) strncpy((char *)_d, (char *)_s, (size_t)(_n)) +#define da_strcat(_d, _s) strcat((char *)_d, (char *)_s) +#define da_memcpy(_d, _c, _n) memcpy(_d, _c, (size_t)(_n)) +#define da_memmove(_d, _c, _n) memmove(_d, _c, (size_t)(_n)) +#define da_memchr(_d, _c, _n) memchr(_d, _c, (size_t)(_n)) +#define da_strlen(_s) strlen((char *)(_s)) +#define da_strncmp(_s1, _s2, _n) \ + strncmp((char *)(_s1), (char *)(_s2), (size_t)(_n)) +#define da_strchr(_p, _l, _c) \ + _da_strchr((uint8_t *)(_p), (uint8_t *)(_l), (uint8_t)(_c)) +#define da_strrchr(_p, _s, _c) \ + _da_strrchr((uint8_t *)(_p), (uint8_t *)(_s), (uint8_t)(_c)) +#define da_strndup(_s, _n) (uint8_t *)strndup((char *)(_s), (size_t)(_n)); +#define da_snprintf(_s, _n, ...) \ + snprintf((char *)(_s), (size_t)(_n), __VA_ARGS__) +#define da_scnprintf(_s, _n, ...) \ + _scnprintf((char *)(_s), (size_t)(_n), __VA_ARGS__) +#define da_vscnprintf(_s, _n, _f, _a) \ + _vscnprintf((char *)(_s), (size_t)(_n), _f, _a) +#define da_strftime(_s, _n, fmt, tm) \ + (int)strftime((char *)(_s), (size_t)(_n), fmt, tm) +#define da_safe_snprintf(_s, _n, ...) \ + _safe_snprintf((char *)(_s), (size_t)(_n), __VA_ARGS__) +#define da_safe_vsnprintf(_s, _n, _f, _a) \ + _safe_vsnprintf((char *)(_s), (size_t)(_n), _f, _a) /* * A (very) limited version of snprintf diff --git a/src/agent/compiler.h b/src/agent/compiler.h index 883fe9f..629811e 100644 --- a/src/agent/compiler.h +++ b/src/agent/compiler.h @@ -1,8 +1,17 @@ /* - * compiler.h + * Copyright [2021] JD.com, Inc. * - * Created on: 2014年12月2日 - * Author: Jiansong + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. */ #ifndef COMPILER_H_ @@ -12,14 +21,13 @@ * Gcc before 3.0 needs [0] to declare a variable-size array */ #ifndef VAR_ARRAY -#if __GNUC__ < 3 -#define VAR_ARRAY 0 +#if __GNUC__ < 3 +#define VAR_ARRAY 0 #else #define VAR_ARRAY #endif #endif - /* Support passing function parameters in registers. For this, the * CONFIG_REGPARM macro has to be set to the maximal number of registers * allowed. Some functions have intentionally received a regparm lower than @@ -28,7 +36,7 @@ */ #ifndef REGPRM1 #if CONFIG_REGPARM >= 1 && __GNUC__ >= 3 -#define REGPRM1 __attribute__((regparm(1))) +#define REGPRM1 __attribute__((regparm(1))) #else #define REGPRM1 #endif @@ -36,7 +44,7 @@ #ifndef REGPRM2 #if CONFIG_REGPARM >= 2 && __GNUC__ >= 3 -#define REGPRM2 __attribute__((regparm(2))) +#define REGPRM2 __attribute__((regparm(2))) #else #define REGPRM2 REGPRM1 #endif @@ -44,13 +52,12 @@ #ifndef REGPRM3 #if CONFIG_REGPARM >= 3 && __GNUC__ >= 3 -#define REGPRM3 __attribute__((regparm(3))) +#define REGPRM3 __attribute__((regparm(3))) #else #define REGPRM3 REGPRM2 #endif #endif - /* By default, gcc does not inline large chunks of code, but we want it to * respect our choices. */ @@ -62,7 +69,6 @@ #endif #endif - /* * Gcc >= 3 provides the ability for the programme to give hints to the * compiler about what branch of an if is most likely to be taken. This @@ -71,7 +77,7 @@ */ #if !defined(likely) #if __GNUC__ < 3 -#define __builtin_expect(x,y) (x) +#define __builtin_expect(x, y) (x) #define likely(x) (x) #define unlikely(x) (x) #elif __GNUC__ < 4 diff --git a/src/agent/da.c b/src/agent/da.c index 2bf56fa..da4adc0 100644 --- a/src/agent/da.c +++ b/src/agent/da.c @@ -1,12 +1,18 @@ /* - ============================================================================ - Name : da.c - Author : jiansong - Version : - Copyright : Your copyright notice - Description : Hello World in C, Ansi-style - ============================================================================ - */ +* Copyright [2021] JD.com, Inc. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ #include #include diff --git a/src/agent/da_buf.c b/src/agent/da_buf.c index 9f73c7f..eb02106 100644 --- a/src/agent/da_buf.c +++ b/src/agent/da_buf.c @@ -1,9 +1,18 @@ /* - * da_buf.c - * - * Created on: 2014年12月3日 - * Author: Jiansong - */ +* Copyright [2021] JD.com, Inc. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ #include "da_buf.h" #include "stdlib.h" #include "da_mem_pool.h" @@ -78,7 +87,7 @@ struct mbuf *mbuf_get() { } /* - * 将mbuf归还内存池 + * 灏唌buf褰掕繕鍐呭瓨姹 */ void mbuf_put(struct mbuf *mbuf) { uint8_t *buf; @@ -90,7 +99,7 @@ void mbuf_put(struct mbuf *mbuf) { } /* - * 重置mbuf + * 閲嶇疆mbuf */ void mbuf_rewind(struct mbuf *mbuf) { mbuf->pos = mbuf->start; @@ -98,7 +107,7 @@ void mbuf_rewind(struct mbuf *mbuf) { } /* - * 返回mbuf中现有的数据 + * 杩斿洖mbuf涓幇鏈夌殑鏁版嵁 */ uint32_t mbuf_length(struct mbuf *mbuf) { ASSERT(mbuf->last >= mbuf->pos); diff --git a/src/agent/da_buf.h b/src/agent/da_buf.h index 784b99d..8e22f5f 100644 --- a/src/agent/da_buf.h +++ b/src/agent/da_buf.h @@ -1,8 +1,17 @@ /* - * da_buf.h + * Copyright [2021] JD.com, Inc. * - * Created on: 2014年12月3日 - * Author: Jiansong + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. */ #ifndef DA_BUF_H_ @@ -17,34 +26,30 @@ struct mbuf; typedef void (*mbuf_copy_t)(struct mbuf *, void *); struct mbuf { - uint32_t magic; /* mbuf magic (const) */ - STAILQ_ENTRY(mbuf) next; /* next mbuf */ - uint8_t *pos; /* read marker */ - uint8_t *last; /* write marker */ - uint8_t *start; /* start of buffer (const) */ - uint8_t *end; /* end of buffer (const) */ + uint32_t magic; /* mbuf magic (const) */ + STAILQ_ENTRY(mbuf) next; /* next mbuf */ + uint8_t *pos; /* read marker */ + uint8_t *last; /* write marker */ + uint8_t *start; /* start of buffer (const) */ + uint8_t *end; /* end of buffer (const) */ }; STAILQ_HEAD(buf_stqh, mbuf); -#define MBUF_MAGIC 0xdeadbeef +#define MBUF_MAGIC 0xdeadbeef /* - * 最小的长度应该把msg头接收下来 + * 鏈灏忕殑闀垮害搴旇鎶妋sg澶存帴鏀朵笅鏉 */ -#define MBUF_MIN_SIZE 256 -#define MBUF_MAX_SIZE 16777216 -#define DEF_MBUF_SIZE 16384 - -static inline bool -mbuf_empty(struct mbuf *mbuf) -{ - return mbuf->pos == mbuf->last ? true : false; +#define MBUF_MIN_SIZE 256 +#define MBUF_MAX_SIZE 16777216 +#define DEF_MBUF_SIZE 16384 + +static inline bool mbuf_empty(struct mbuf *mbuf) { + return mbuf->pos == mbuf->last ? true : false; } -static inline bool -mbuf_full(struct mbuf *mbuf) -{ - return mbuf->last == mbuf->end ? true : false; +static inline bool mbuf_full(struct mbuf *mbuf) { + return mbuf->last == mbuf->end ? true : false; } int mbuf_init(struct instance *ins); @@ -58,6 +63,8 @@ size_t mbuf_data_size(void); void mbuf_insert(struct buf_stqh *mhdr, struct mbuf *mbuf); void mbuf_remove(struct buf_stqh *mhdr, struct mbuf *mbuf); void mbuf_copy(struct mbuf *mbuf, uint8_t *pos, size_t n); -struct mbuf* mbuf_split(struct mbuf *mbuf,uint8_t *pos, mbuf_copy_t cb, void *cbarg); -struct mbuf* _mbuf_split(struct mbuf *mbuf, uint8_t *pos, mbuf_copy_t cb,void *cbarg); +struct mbuf *mbuf_split(struct mbuf *mbuf, uint8_t *pos, mbuf_copy_t cb, + void *cbarg); +struct mbuf *_mbuf_split(struct mbuf *mbuf, uint8_t *pos, mbuf_copy_t cb, + void *cbarg); #endif /* DA_BUF_H_ */ diff --git a/src/agent/da_client.c b/src/agent/da_client.c index 371c495..026a19b 100644 --- a/src/agent/da_client.c +++ b/src/agent/da_client.c @@ -1,9 +1,18 @@ /* - * da_client.c - * - * Created on: 2014年12月3日 - * Author: Jiansong - */ +* Copyright [2021] JD.com, Inc. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ #include #include "da_client.h" diff --git a/src/agent/da_client.h b/src/agent/da_client.h index b8e6457..7dea6ee 100644 --- a/src/agent/da_client.h +++ b/src/agent/da_client.h @@ -1,8 +1,17 @@ /* - * da_client.h + * Copyright [2021] JD.com, Inc. * - * Created on: 2014年12月3日 - * Author: Jiansong + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. */ #ifndef DA_CLIENT_H_ diff --git a/src/agent/da_conf.c b/src/agent/da_conf.c index e169de8..1e02c00 100644 --- a/src/agent/da_conf.c +++ b/src/agent/da_conf.c @@ -1,9 +1,18 @@ /* - * da_conf.c - * - * Created on: 2014年12月4日 - * Author: Jiansong - */ +* Copyright [2021] JD.com, Inc. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ #include #include @@ -522,7 +531,7 @@ static int conf_parse_core(struct conf *cf) { return -1; } - //转存一下localip到conf_pool中,后续server_pool需要使用 + //杞瓨涓涓媗ocalip鍒癱onf_pool涓紝鍚庣画server_pool闇瑕佷娇鐢 struct conf_pool *tmpCP = (struct conf_pool *)void_cp; strncpy(tmpCP->localip, cf->localip, sizeof(tmpCP->localip)); @@ -1010,7 +1019,7 @@ struct conf *conf_create(char *filename) { return NULL; } - //后续需要用到本地IP,所以放在前面获取 + //鍚庣画闇瑕佺敤鍒版湰鍦癐P锛屾墍浠ユ斁鍦ㄥ墠闈㈣幏鍙 get_local_ip(cf->localip, sizeof(cf->localip)); status = conf_parse(cf); diff --git a/src/agent/da_conf.h b/src/agent/da_conf.h index 0a17dfd..f804e02 100644 --- a/src/agent/da_conf.h +++ b/src/agent/da_conf.h @@ -1,127 +1,134 @@ /* - * da_conf.h + * Copyright [2021] JD.com, Inc. * - * Created on: 2014年12月4日 - * Author: Jiansong + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. */ #ifndef DA_CONF_H_ #define DA_CONF_H_ -#include "mxml.h" #include "da_hashkit.h" #include "da_string.h" +#include "mxml.h" -#define CONF_ERROR (void *) "has an invalid value" -#define CONF_OK (void *) NULL - -#define CONF_UNSET_HASH (hash_type_t) -1 -#define CONF_UNSET_PTR NULL -#define CONF_UNSET_NUM -1 -#define CONF_DEFAULT_SERVERS 8 - - -#define CONF_DEFAULT_POOL 8 -#define CONF_DEFAULT_INSTANCE 8 -#define CONF_DEFAULT_HASH HASH_CHASH -#define CONF_DEFAULT_TIMEOUT -1 -#define CONF_DEFAULT_LISTEN_BACKLOG 512 -#define CONF_DEFAULT_CLIENT_CONNECTIONS 0 -#define CONF_DEFAULT_REDIS false -#define CONF_DEFAULT_PRECONNECT false -#define CONF_DEFAULT_AUTO_EJECT_HOSTS false -#define CONF_DEFAULT_SERVER_RETRY_TIMEOUT 30 * 1000 /* in msec */ -#define CONF_DEFAULT_SERVER_FAILURE_LIMIT 2 -#define CONF_DEFAULT_SERVER_CONNECTIONS 1 -#define CONF_DEFAULT_TOP_PERCENTILE_ENABLE 1 -#define CONF_DEFAULT_TOP_PERCENTILE_DOMAIN "10.187.152.196" -#define CONF_DEFAULT_TOP_PERCENTILE_PORT 20020 -#define CONF_DEFAULT_LOG_SWITCH 0 /*1:on, 0:off*/ +#define CONF_ERROR (void *)"has an invalid value" +#define CONF_OK (void *)NULL + +#define CONF_UNSET_HASH (hash_type_t) - 1 +#define CONF_UNSET_PTR NULL +#define CONF_UNSET_NUM -1 +#define CONF_DEFAULT_SERVERS 8 + +#define CONF_DEFAULT_POOL 8 +#define CONF_DEFAULT_INSTANCE 8 +#define CONF_DEFAULT_HASH HASH_CHASH +#define CONF_DEFAULT_TIMEOUT -1 +#define CONF_DEFAULT_LISTEN_BACKLOG 512 +#define CONF_DEFAULT_CLIENT_CONNECTIONS 0 +#define CONF_DEFAULT_REDIS false +#define CONF_DEFAULT_PRECONNECT false +#define CONF_DEFAULT_AUTO_EJECT_HOSTS false +#define CONF_DEFAULT_SERVER_RETRY_TIMEOUT 30 * 1000 /* in msec */ +#define CONF_DEFAULT_SERVER_FAILURE_LIMIT 2 +#define CONF_DEFAULT_SERVER_CONNECTIONS 1 +#define CONF_DEFAULT_TOP_PERCENTILE_ENABLE 1 +#define CONF_DEFAULT_TOP_PERCENTILE_DOMAIN "10.187.152.196" +#define CONF_DEFAULT_TOP_PERCENTILE_PORT 20020 +#define CONF_DEFAULT_LOG_SWITCH 0 /*1:on, 0:off*/ #define CONF_DEFAULT_REMOTE_LOG_SWITCH 1 /*1:on, 0:off*/ #define CONF_DEFAULT_REMOTE_LOG_IP "10.187.152.196" #define CONF_DEFAULT_REMOTE_LOG_PORT 9997 - struct conf_listen { - struct string pname; /* listen: as "name:port" */ - struct string name; /* name */ - int port; /* port */ - struct sockinfo info; /* listen socket info */ - unsigned valid :1; /* valid? */ + struct string pname; /* listen: as "name:port" */ + struct string name; /* name */ + int port; /* port */ + struct sockinfo info; /* listen socket info */ + unsigned valid : 1; /* valid? */ }; struct conf_server { - struct string name; /* name */ - struct array instance; /* instance */ - int replica_enable; - struct string idc; - unsigned valid : 1; /* valid? */ + struct string name; /* name */ + struct array instance; /* instance */ + int replica_enable; + struct string idc; + unsigned valid : 1; /* valid? */ }; struct conf_instance { - struct string addr; /* server: as "name:port:weight" */ - int port; /* port */ - int weight; /* weight */ - struct sockinfo info; /* connect socket info */ - struct string idc; /* idc */ - struct string role; /* role */ - unsigned valid : 1; /* valid? */ - int enable; /* Enable */ + struct string addr; /* server: as "name:port:weight" */ + int port; /* port */ + int weight; /* weight */ + struct sockinfo info; /* connect socket info */ + struct string idc; /* idc */ + struct string role; /* role */ + unsigned valid : 1; /* valid? */ + int enable; /* Enable */ }; struct conf_pool { - int mid; - struct string name; /* pool name (root node) */ - struct string accesskey; - struct conf_listen listen; /* listen: */ - hash_type_t hash; /* hash: */ - int timeout; /* timeout: */ - int backlog; /* backlog: */ - int client_connections; /* client_connections: */ - int preconnect; /* preconnect: */ - int server_connections; /* server_connections: */ - struct array server; /* servers: conf_server[] */ - struct string idc; /* idc*/ - int replica_enable; /* ReplicaEnable*/ - int main_report; - int instance_report; - int auto_remove_replica; - - int top_percentile_enable; /*tp99 性能指标开启状态*/ - struct string top_percentile_domain; /*tp99 性能指标上报服务器地址*/ - int top_percentile_port; /*tp99 性能指标上报服务器端口*/ - - char localip[16]; /*本地IP,放在此位置*/ - unsigned valid:1; /* valid? */ - + int mid; + struct string name; /* pool name (root node) */ + struct string accesskey; + struct conf_listen listen; /* listen: */ + hash_type_t hash; /* hash: */ + int timeout; /* timeout: */ + int backlog; /* backlog: */ + int client_connections; /* client_connections: */ + int preconnect; /* preconnect: */ + int server_connections; /* server_connections: */ + struct array server; /* servers: conf_server[] */ + struct string idc; /* idc*/ + int replica_enable; /* ReplicaEnable*/ + int main_report; + int instance_report; + int auto_remove_replica; + + int top_percentile_enable; /*tp99 鎬ц兘鎸囨爣寮鍚姸鎬*/ + struct string top_percentile_domain; /*tp99 鎬ц兘鎸囨爣涓婃姤鏈嶅姟鍣ㄥ湴鍧*/ + int top_percentile_port; /*tp99 鎬ц兘鎸囨爣涓婃姤鏈嶅姟鍣ㄧ鍙*/ + + char localip[16]; /*鏈湴IP锛屾斁鍦ㄦ浣嶇疆*/ + unsigned valid : 1; /* valid? */ }; struct conf_log { - int log_switch; - int remote_log_switch; - struct string remote_log_ip; - int remote_log_port; + int log_switch; + int remote_log_switch; + struct string remote_log_ip; + int remote_log_port; }; struct conf { - char *fname; /* file name (ref in argv[]) */ - FILE *fh; /* file handle */ - struct string arg; /* string[] (parsed {key, value} pairs) */ - struct array pool; /* conf_pool[] (parsed pools) */ - mxml_node_t *tree; - char localip[16]; - struct conf_log stCL; - unsigned parsed:1; /* parsed? */ - unsigned valid:1; /* valid? */ + char *fname; /* file name (ref in argv[]) */ + FILE *fh; /* file handle */ + struct string arg; /* string[] (parsed {key, value} pairs) */ + struct array pool; /* conf_pool[] (parsed pools) */ + mxml_node_t *tree; + char localip[16]; + struct conf_log stCL; + unsigned parsed : 1; /* parsed? */ + unsigned valid : 1; /* valid? */ }; struct command { - struct string name; - char *(*set)(struct conf *cf, struct command *cmd, void *data); - int offset; + struct string name; + char *(*set)(struct conf *cf, struct command *cmd, void *data); + int offset; }; -#define null_command { null_string, NULL, 0 } +#define null_command \ + { null_string, NULL, 0 } int conf_server_each_transform(void *elem, void *data); int conf_pool_each_transform(void *elem, void *data); diff --git a/src/agent/da_conn.c b/src/agent/da_conn.c index f797ba6..bb3d941 100644 --- a/src/agent/da_conn.c +++ b/src/agent/da_conn.c @@ -1,10 +1,18 @@ /* - * da_conn.c - * - * Created on: 2014年11月30日 - * Author: Jiansong - */ - +* Copyright [2021] JD.com, Inc. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ #include #include "da_mem_pool.h" #include "da_conn.h" diff --git a/src/agent/da_conn.h b/src/agent/da_conn.h index d73edf9..340ee4a 100644 --- a/src/agent/da_conn.h +++ b/src/agent/da_conn.h @@ -1,45 +1,54 @@ /* - * da_conn.h + * Copyright [2021] JD.com, Inc. * - * Created on: 2014年11月30日 - * Author: Jiansong + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. */ #ifndef DA_CONN_H_ #define DA_CONN_H_ -#include -#include -#include +#include "compiler.h" +#include "da_errno.h" +#include "da_msg.h" #include "da_queue.h" #include "da_rbtree.h" -#include "compiler.h" #include "da_util.h" -#include "da_msg.h" -#include "da_errno.h" - +#include +#include +#include struct conn; struct context; struct msg; -//connection type +// connection type #define FRONTWORK 0x0001 -#define BACKWORK 0x0002 -#define LISTENER 0x0004 +#define BACKWORK 0x0002 +#define LISTENER 0x0004 -//connection epoll flag +// connection epoll flag #define RECV_ACTIVE 0x0001 #define SEND_ACTIVE 0x0002 -#define RECV_READY 0x0004 -#define SEND_READY 0x0008 +#define RECV_READY 0x0004 +#define SEND_READY 0x0008 -typedef int (*conn_recv_t)(struct context *, struct conn*); -typedef struct msg* (*conn_recv_next_t)(struct context *, struct conn *, bool); -typedef void (*conn_recv_done_t)(struct context *, struct conn *, struct msg *,struct msg *); +typedef int (*conn_recv_t)(struct context *, struct conn *); +typedef struct msg *(*conn_recv_next_t)(struct context *, struct conn *, bool); +typedef void (*conn_recv_done_t)(struct context *, struct conn *, struct msg *, + struct msg *); -typedef int (*conn_send_t)(struct context *, struct conn*); -typedef struct msg* (*conn_send_next_t)(struct context *, struct conn *); +typedef int (*conn_send_t)(struct context *, struct conn *); +typedef struct msg *(*conn_send_next_t)(struct context *, struct conn *); typedef void (*conn_send_done_t)(struct context *, struct conn *, struct msg *); typedef void (*conn_close_t)(struct context *, struct conn *); @@ -52,57 +61,56 @@ typedef void (*conn_msgq_t)(struct context *, struct conn *, struct msg *); typedef void (*conn_msgtree_t)(struct context *, struct conn *, struct msg *); struct conn { - TAILQ_ENTRY(conn) conn_tqe; /*list linked in server or server pool*/ - void *owner; /*owner server server_pool*/ - - int fd; /*socket fd*/ - int family; /*socket family*/ - socklen_t addrlen; /*socket addr len*/ - struct sockaddr *addr; /*socket address (ref in server)*/ - - uint32_t type; /*front conn,back conn,or listener*/ - uint32_t events; /*the event need process*/ - uint32_t flag; /*epool flag*/ - - struct rbtree msg_tree; /*tree for message search*/ - struct rbnode msg_rbs; /*sentinel for msg_tree */ - struct msg_tqh imsg_q; /*request msgq */ - struct msg_tqh omsg_q; /*outstanding request Q */ - - - struct msg *rmsg; /* current message being rcvd */ - struct msg *smsg; /* current message being sent */ - - conn_recv_t recv; /* recv (read) handler */ - conn_recv_next_t recv_next; /* recv next message handler */ - conn_recv_done_t recv_done; /* read done handler */ - conn_send_t send; /* send (write) handler */ - conn_send_next_t send_next; /* write next message handler */ - conn_send_done_t send_done; /* write done handler */ - conn_close_t close; /* close handler */ - conn_active_t active; /* active? handler */ - conn_ref_t ref; /* connection reference handler */ - conn_unref_t unref; /* connection unreference handler */ - - conn_msgq_t enqueue_outq; /* connection outq msg enqueue handler */ - conn_msgq_t dequeue_outq; /* connection outq msg dequeue handler */ - conn_msgq_t enqueue_inq; /* connection outq msg enqueue handler */ - conn_msgq_t dequeue_inq; /* connection outq msg dequeue handler */ - - conn_msgtree_t en_msgtree; /* connection msg tree entree handler*/ - conn_msgtree_t de_msgtree; /* connection msg tree detree handler*/ - - size_t recv_bytes; /* received (read) bytes */ - size_t send_bytes; /* sent (written) bytes */ - - int err ; /*connection error no*/ - unsigned error :1; /*connetion err sig*/ - unsigned connecting :1; /*connecting is on*/ - unsigned connected :1; /*connected*/ - unsigned eof:1; /*connection is end,half connection*/ - unsigned done :1; /*connection is done*/ - unsigned writecached:1; - unsigned isvalid:1; + TAILQ_ENTRY(conn) conn_tqe; /*list linked in server or server pool*/ + void *owner; /*owner server server_pool*/ + + int fd; /*socket fd*/ + int family; /*socket family*/ + socklen_t addrlen; /*socket addr len*/ + struct sockaddr *addr; /*socket address (ref in server)*/ + + uint32_t type; /*front conn,back conn,or listener*/ + uint32_t events; /*the event need process*/ + uint32_t flag; /*epool flag*/ + + struct rbtree msg_tree; /*tree for message search*/ + struct rbnode msg_rbs; /*sentinel for msg_tree */ + struct msg_tqh imsg_q; /*request msgq */ + struct msg_tqh omsg_q; /*outstanding request Q */ + + struct msg *rmsg; /* current message being rcvd */ + struct msg *smsg; /* current message being sent */ + + conn_recv_t recv; /* recv (read) handler */ + conn_recv_next_t recv_next; /* recv next message handler */ + conn_recv_done_t recv_done; /* read done handler */ + conn_send_t send; /* send (write) handler */ + conn_send_next_t send_next; /* write next message handler */ + conn_send_done_t send_done; /* write done handler */ + conn_close_t close; /* close handler */ + conn_active_t active; /* active? handler */ + conn_ref_t ref; /* connection reference handler */ + conn_unref_t unref; /* connection unreference handler */ + + conn_msgq_t enqueue_outq; /* connection outq msg enqueue handler */ + conn_msgq_t dequeue_outq; /* connection outq msg dequeue handler */ + conn_msgq_t enqueue_inq; /* connection outq msg enqueue handler */ + conn_msgq_t dequeue_inq; /* connection outq msg dequeue handler */ + + conn_msgtree_t en_msgtree; /* connection msg tree entree handler*/ + conn_msgtree_t de_msgtree; /* connection msg tree detree handler*/ + + size_t recv_bytes; /* received (read) bytes */ + size_t send_bytes; /* sent (written) bytes */ + + int err; /*connection error no*/ + unsigned error : 1; /*connetion err sig*/ + unsigned connecting : 1; /*connecting is on*/ + unsigned connected : 1; /*connected*/ + unsigned eof : 1; /*connection is end,half connection*/ + unsigned done : 1; /*connection is done*/ + unsigned writecached : 1; + unsigned isvalid : 1; }; TAILQ_HEAD(conn_tqh, conn); @@ -138,8 +146,8 @@ struct conn *get_client_conn(void *owner); struct conn *get_server_conn(void *owner); /* -*get instance conn -*/ + *get instance conn + */ struct conn *get_instance_conn(void *owner); /* diff --git a/src/agent/da_core.c b/src/agent/da_core.c index 58deaca..cbb4bad 100644 --- a/src/agent/da_core.c +++ b/src/agent/da_core.c @@ -1,9 +1,18 @@ /* - * da_core.c - * - * Created on: 2014年11月30日 - * Author: Jiansong - */ +* Copyright [2021] JD.com, Inc. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ #include #include @@ -35,7 +44,7 @@ void cache_send_event(struct conn *conn) { } /* - * 根据可用连接及后端连接数,计算前端连接可用的连接数 + * 鏍规嵁鍙敤杩炴帴鍙婂悗绔繛鎺ユ暟锛岃绠楀墠绔繛鎺ュ彲鐢ㄧ殑杩炴帴鏁 */ static int core_calc_connections(struct context *ctx) { int status; diff --git a/src/agent/da_core.h b/src/agent/da_core.h index a28a2d1..efbbd07 100644 --- a/src/agent/da_core.h +++ b/src/agent/da_core.h @@ -1,30 +1,39 @@ /* - * da_core.h + * Copyright [2021] JD.com, Inc. * - * Created on: 2014年11月30日 - * Author: Jiansong + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. */ #ifndef DA_CORE_H_ #define DA_CORE_H_ -#include "da_util.h" #include "da_array.h" +#include "da_util.h" #ifdef HAVE_ASSERT_PANIC -# define DA_ASSERT_PANIC 1 +#define DA_ASSERT_PANIC 1 #endif #ifdef HAVE_ASSERT_LOG -# define DA_ASSERT_LOG 1 +#define DA_ASSERT_LOG 1 #endif -# define DA_STATS 1 -# define USE_COMPATIBLE_MODE +#define DA_STATS 1 +#define USE_COMPATIBLE_MODE #ifdef USE_COMPATIBLE_MODE -# define DA_COMPATIBLE_MODE 1 +#define DA_COMPATIBLE_MODE 1 #else -# define DA_COMPATIBLE_MODE 0 +#define DA_COMPATIBLE_MODE 0 #endif #define RESERVED_FDS 32 @@ -38,48 +47,47 @@ struct conn; struct instance; struct context { - uint32_t id; /* unique context id */ - struct conf *cf; /* configuration */ - struct stats *stats; /* stats */ + uint32_t id; /* unique context id */ + struct conf *cf; /* configuration */ + struct stats *stats; /* stats */ - struct array pool; /* server_pool[] */ - struct event_base *evb; /* event base */ - int max_timeout; /* max timeout in msec */ - int timeout; /* timeout in msec */ + struct array pool; /* server_pool[] */ + struct event_base *evb; /* event base */ + int max_timeout; /* max timeout in msec */ + int timeout; /* timeout in msec */ - uint32_t max_nfd; /* max # files */ - uint32_t max_ncconn; /* max # client connections */ - uint32_t max_nsconn; /* max # server connections */ + uint32_t max_nfd; /* max # files */ + uint32_t max_ncconn; /* max # client connections */ + uint32_t max_nsconn; /* max # server connections */ - uint32_t sum_nconn; /* client connections and server connections sum*/ + uint32_t sum_nconn; /* client connections and server connections sum*/ }; struct instance { - struct context *ctx; /* active context */ - int log_level; /* log level */ - char *log_dir; /* log dir*/ - char *conf_filename; /* configuration filename */ - char hostname[DA_MAXHOSTNAMELEN]; /* hostname */ - size_t mbuf_chunk_size; /* mbuf chunk size */ - int event_max_timeout; /* epoll max time out*/ - int stats_interval; /* stats aggregation interval */ - pid_t pid; /* process id */ - char *pid_filename; /* pid filename */ - unsigned pidfile:1; /* pid file created? */ - int cpumask; /*cpu mask for run*/ - char **argv; /* argv of main() */ + struct context *ctx; /* active context */ + int log_level; /* log level */ + char *log_dir; /* log dir*/ + char *conf_filename; /* configuration filename */ + char hostname[DA_MAXHOSTNAMELEN]; /* hostname */ + size_t mbuf_chunk_size; /* mbuf chunk size */ + int event_max_timeout; /* epoll max time out*/ + int stats_interval; /* stats aggregation interval */ + pid_t pid; /* process id */ + char *pid_filename; /* pid filename */ + unsigned pidfile : 1; /* pid file created? */ + int cpumask; /*cpu mask for run*/ + char **argv; /* argv of main() */ }; -//status for server -enum core_status -{ - NORMAL, - RELOADING, - EXITING, - EXITED, +// status for server +enum core_status { + NORMAL, + RELOADING, + EXITING, + EXITED, }; -//extern struct conn *wait_send_queue[]; /*cached conn*/ +// extern struct conn *wait_send_queue[]; /*cached conn*/ void cache_send_event(struct conn *conn); int core_core(void *arg, uint32_t events); diff --git a/src/agent/da_errno.h b/src/agent/da_errno.h index 6575fec..e948511 100644 --- a/src/agent/da_errno.h +++ b/src/agent/da_errno.h @@ -1,72 +1,93 @@ /* - * da_errno.h + * Copyright [2021] JD.com, Inc. * - * Created on: 2015年1月4日 - * Author: Jiansong + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. */ #ifndef DA_ERRNO_H_ #define DA_ERRNO_H_ enum conn_errno { - CONN_EPOLLCTL_ERR = 1, - CONN_EPOLLADD_ERR = 2, - CONN_EPOLL_DEL_ERR = 3, - CONN_CONNECT_ERR = 4, - CONN_SETNOBLOCK_ERR = 5, - CONN_CREATSOCK_ERR = 6, - CONN_RECV_ERR = 7, - CONN_SEND_ERR = 8, - CONN_MSG_GET_ERR = 9, - CONN_BUF_GET_ERR = 10, - CONN_MSG_PARSE_ERR = 11, - CONN_NOT_VALID = 12, + CONN_EPOLLCTL_ERR = 1, + CONN_EPOLLADD_ERR = 2, + CONN_EPOLL_DEL_ERR = 3, + CONN_CONNECT_ERR = 4, + CONN_SETNOBLOCK_ERR = 5, + CONN_CREATSOCK_ERR = 6, + CONN_RECV_ERR = 7, + CONN_SEND_ERR = 8, + CONN_MSG_GET_ERR = 9, + CONN_BUF_GET_ERR = 10, + CONN_MSG_PARSE_ERR = 11, + CONN_NOT_VALID = 12, }; enum msg_errno { - MSG_FRAGMENT_ERR=2, - MSG_REQ_FORWARD_ERR=3, - MSG_BACKWORKER_ERR=4, - MSG_COALESCE_ERR =5, - MSG_NOKEY_ERR = 6, + MSG_FRAGMENT_ERR = 2, + MSG_REQ_FORWARD_ERR = 3, + MSG_BACKWORKER_ERR = 4, + MSG_COALESCE_ERR = 5, + MSG_NOKEY_ERR = 6, }; -static inline char* GetMsgErrorCodeStr(int errcode) -{ - enum msg_errno err; - err=(enum msg_errno)errcode; - switch(err) - { - case MSG_FRAGMENT_ERR : return "fragment request error!"; - case MSG_REQ_FORWARD_ERR: return "forward request to server error!"; - case MSG_BACKWORKER_ERR : return "server error!"; - case MSG_COALESCE_ERR : return "coalesce request error!"; - case MSG_NOKEY_ERR : return "request without key!"; - } - return "unknow"; +static inline char *GetMsgErrorCodeStr(int errcode) { + enum msg_errno err; + err = (enum msg_errno)errcode; + switch (err) { + case MSG_FRAGMENT_ERR: + return "fragment request error!"; + case MSG_REQ_FORWARD_ERR: + return "forward request to server error!"; + case MSG_BACKWORKER_ERR: + return "server error!"; + case MSG_COALESCE_ERR: + return "coalesce request error!"; + case MSG_NOKEY_ERR: + return "request without key!"; + } + return "unknow"; } -static inline char * GetConnErrorCodeStr(int errcode) -{ - enum conn_errno err; - err=(enum conn_errno)errcode; - switch(err) - { - case CONN_EPOLLCTL_ERR: return "conn epoll ctl error"; - case CONN_EPOLLADD_ERR: return "conn epoll add error"; - case CONN_EPOLL_DEL_ERR: return "conn epoll del error"; - case CONN_CONNECT_ERR: return "conn server error"; - case CONN_SETNOBLOCK_ERR: return "conn set noblock error"; - case CONN_CREATSOCK_ERR: return "conn create socket error"; - case CONN_RECV_ERR: return "conn recv error"; - case CONN_SEND_ERR: return "conn send error"; - case CONN_MSG_GET_ERR: return "conn get msg error"; - case CONN_BUF_GET_ERR: return "conn get buf error"; - case CONN_MSG_PARSE_ERR: return "conn parse msg error"; - case CONN_NOT_VALID: return "conn accesstoken invalid"; - } - return "unknow"; +static inline char *GetConnErrorCodeStr(int errcode) { + enum conn_errno err; + err = (enum conn_errno)errcode; + switch (err) { + case CONN_EPOLLCTL_ERR: + return "conn epoll ctl error"; + case CONN_EPOLLADD_ERR: + return "conn epoll add error"; + case CONN_EPOLL_DEL_ERR: + return "conn epoll del error"; + case CONN_CONNECT_ERR: + return "conn server error"; + case CONN_SETNOBLOCK_ERR: + return "conn set noblock error"; + case CONN_CREATSOCK_ERR: + return "conn create socket error"; + case CONN_RECV_ERR: + return "conn recv error"; + case CONN_SEND_ERR: + return "conn send error"; + case CONN_MSG_GET_ERR: + return "conn get msg error"; + case CONN_BUF_GET_ERR: + return "conn get buf error"; + case CONN_MSG_PARSE_ERR: + return "conn parse msg error"; + case CONN_NOT_VALID: + return "conn accesstoken invalid"; + } + return "unknow"; } - #endif /* DA_ERRNO_H_ */ diff --git a/src/agent/da_listener.c b/src/agent/da_listener.c index 851b2ab..0dda2f2 100644 --- a/src/agent/da_listener.c +++ b/src/agent/da_listener.c @@ -1,9 +1,18 @@ /* - * da_listener.c - * - * Created on: 2014年12月3日 - * Author: Jiansong - */ +* Copyright [2021] JD.com, Inc. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ #include "da_listener.h" #include "da_array.h" @@ -243,7 +252,7 @@ static int listener_accept(struct context *ctx, struct conn *l) { continue; } /* - * 多进程情况下,同时accept会出现错误,errno=11,程序吞掉这个错误 + * 澶氳繘绋嬫儏鍐典笅锛屽悓鏃禷ccept浼氬嚭鐜伴敊璇紝errno=11,绋嬪簭鍚炴帀杩欎釜閿欒 */ if (errno == EAGAIN || errno == EWOULDBLOCK || errno == ECONNABORTED) { log_debug("accept on l %d not ready - eagain", l->fd); @@ -265,7 +274,7 @@ static int listener_accept(struct context *ctx, struct conn *l) { } /* - * 对全局的FD资源进行限制,每个进程单独资源 + * 瀵瑰叏灞鐨凢D璧勬簮杩涜闄愬埗,姣忎釜杩涚▼鍗曠嫭璧勬簮 */ if (get_ncurr_cconn() >= ctx->max_ncconn || get_ncurr_cconn() @@ -302,7 +311,7 @@ static int listener_accept(struct context *ctx, struct conn *l) { return status; } - /*所有的客户端连接在exec之后全部关闭*/ + /*鎵鏈夌殑瀹㈡埛绔繛鎺ュ湪exec涔嬪悗鍏ㄩ儴鍏抽棴*/ status = fcntl(c->fd, F_SETFD, FD_CLOEXEC); if (status < 0) { log_error("fcntl FD_CLOEXEC on c %d from p %d failed: %s", @@ -312,7 +321,7 @@ static int listener_accept(struct context *ctx, struct conn *l) { } /* - * 对于ipv4与ipv6协议关闭negale算法 + * 瀵逛簬ipv4涓巌pv6鍗忚鍏抽棴negale绠楁硶 */ if (l->family == AF_INET || l->family == AF_INET6) { status = set_tcpnodelay(c->fd); diff --git a/src/agent/da_listener.h b/src/agent/da_listener.h index 44a672b..287a643 100644 --- a/src/agent/da_listener.h +++ b/src/agent/da_listener.h @@ -1,17 +1,26 @@ /* - * da_listener.h + * Copyright [2021] JD.com, Inc. * - * Created on: 2014年12月3日 - * Author: Jiansong + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. */ #ifndef DA_LISTENER_H_ #define DA_LISTENER_H_ -#include +#include "da_util.h" #include #include -#include "da_util.h" +#include struct conn; struct context; diff --git a/src/agent/da_log.c b/src/agent/da_log.c index d89c7f9..76936b3 100644 --- a/src/agent/da_log.c +++ b/src/agent/da_log.c @@ -1,9 +1,18 @@ /* - * da_log.c - * - * Created on: 2014年11月29日 - * Author: Jiansong - */ +* Copyright [2021] JD.com, Inc. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ #include #include @@ -31,6 +40,7 @@ static int noconsole = 0; //remote log area static struct sockaddr_in remote_log_addr; static int sockaddr_length = 0; +int da_gettid(void); void _set_remote_log_config_(const char *addr, int port, const char *own_addr, int own_port, int businessid, int iSwitch) { @@ -119,7 +129,7 @@ void _set_log_level_(int l) { void _set_log_switch_(int iSwitch) { - //0 不开启本地日志 + //0 涓嶅紑鍚湰鍦版棩蹇 __log_switch__ = iSwitch; } @@ -148,7 +158,7 @@ void _write_log_(int level, char *filename, const char *funcname, filename = basename(filename); off = snprintf(buf, LOGSIZE, "<%d>[%02d:%02d:%02d] pid[%d]: %s(%d)[%s]: ", level, tm.tm_hour, - tm.tm_min, tm.tm_sec, gettid(), filename, lineno, funcname); + tm.tm_min, tm.tm_sec, da_gettid(), filename, lineno, funcname); } if (off >= LOGSIZE) off = LOGSIZE - 1; diff --git a/src/agent/da_log.h b/src/agent/da_log.h index b837c68..69a2fcf 100644 --- a/src/agent/da_log.h +++ b/src/agent/da_log.h @@ -1,51 +1,77 @@ /* - * da_log.h + * Copyright [2021] JD.com, Inc. * - * Created on: 2014年11月29日 - * Author: Jiansong + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. */ #ifndef DA_LOG_H_ #define DA_LOG_H_ -#include -#include -#include #include +#include #include #include -#include +#include #include #include #include #include +#include +#include #include "da_util.h" - extern int __log_level__; extern int __business_id__; -#define log_bare(lvl, fmt, args...) _write_log_(lvl, NULL, NULL, 0 , fmt, ##args) -#define log_generic(lvl, fmt, args...) _write_log_(lvl, __FILE__, __FUNCTION__, __LINE__ , fmt, ##args) -#define log_emerg(fmt, args...) log_generic(0, fmt, ##args) -#define log_alert(fmt, args...) log_generic(1, fmt, ##args) -#define log_crit(fmt, args...) log_generic(2, fmt, ##args) -#define log_error(fmt, args...) log_generic(3, fmt, ##args) -#define log_warning(fmt, args...) do{ if(__log_level__>=4)log_generic(4, fmt, ##args); } while(0) -#define log_notice(fmt, args...) do{ if(__log_level__>=5)log_generic(5, fmt, ##args); } while(0) -#define log_info(fmt, args...) do{ if(__log_level__>=6)log_generic(6, fmt, ##args); } while(0) -#define log_debug(fmt, args...) do{ if(__log_level__>=7)log_generic(7, fmt, ##args); } while(0) - +#define log_bare(lvl, fmt, args...) _write_log_(lvl, NULL, NULL, 0, fmt, ##args) +#define log_generic(lvl, fmt, args...) \ + _write_log_(lvl, __FILE__, __FUNCTION__, __LINE__, fmt, ##args) +#define log_emerg(fmt, args...) log_generic(0, fmt, ##args) +#define log_alert(fmt, args...) log_generic(1, fmt, ##args) +#define log_crit(fmt, args...) log_generic(2, fmt, ##args) +#define log_error(fmt, args...) log_generic(3, fmt, ##args) +#define log_warning(fmt, args...) \ + do { \ + if (__log_level__ >= 4) \ + log_generic(4, fmt, ##args); \ + } while (0) +#define log_notice(fmt, args...) \ + do { \ + if (__log_level__ >= 5) \ + log_generic(5, fmt, ##args); \ + } while (0) +#define log_info(fmt, args...) \ + do { \ + if (__log_level__ >= 6) \ + log_generic(6, fmt, ##args); \ + } while (0) +#define log_debug(fmt, args...) \ + do { \ + if (__log_level__ >= 7) \ + log_generic(7, fmt, ##args); \ + } while (0) void _init_log_(const char *app, const char *dir); void _set_log_level_(int l); void _set_log_switch_(int iSwitch); -void _write_log_ (int, char*, const char *, int, const char *, ...) __attribute__((format(printf,5,6))); +void _write_log_(int, char *, const char *, int, const char *, ...) + __attribute__((format(printf, 5, 6))); int _set_remote_log_fd_(); -void remote_log(int type, const char *key, int op_type, int op_result, char *content, long op_time, int cmd, int magic, int contentlen); +void remote_log(int type, const char *key, int op_type, int op_result, + char *content, long op_time, int cmd, int magic, + int contentlen); void write_stderr(const char *fmt, ...); - -static inline int gettid(void) { return syscall(__NR_gettid); } +static inline int da_gettid(void) { return syscall(__NR_gettid); } #endif /* DA_LOG_H_ */ diff --git a/src/agent/da_mem_pool.c b/src/agent/da_mem_pool.c index cf731a7..1a5c213 100644 --- a/src/agent/da_mem_pool.c +++ b/src/agent/da_mem_pool.c @@ -1,12 +1,18 @@ /* - * da_mem.c - * - * Created on: 2014年11月30日 - * Author: Jiansong - * - * 利用双向尾队列链接内存,经过测试发现为队列的效率略高于双向循环队列 - */ - +* Copyright [2021] JD.com, Inc. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ #include "da_mem_pool.h" #include "da_log.h" #include "da_util.h" @@ -26,7 +32,7 @@ struct pool_head *create_pool(char *name, unsigned int size, unsigned int flags) struct pool_head *start; unsigned int align; - //16字节对齐 + //16瀛楄妭瀵归綈 align = 16; size = (size + align - 1) & -align; @@ -111,7 +117,7 @@ void pool_gc() { static int recurse; struct pool_head *entry; - //预防重复调用 + //棰勯槻閲嶅璋冪敤 if (recurse++) goto out; diff --git a/src/agent/da_mem_pool.h b/src/agent/da_mem_pool.h index 8f645e8..884d0d6 100644 --- a/src/agent/da_mem_pool.h +++ b/src/agent/da_mem_pool.h @@ -1,31 +1,39 @@ /* - * da_mem.h + * Copyright [2021] JD.com, Inc. * - * Created on: 2014年11月30日 - * Author: Jiansong + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. */ #ifndef DA_MEM_POOL_H_ #define DA_MEM_POOL_H_ +#include "da_queue.h" #include #include -#include "da_queue.h" - #define MEM_F_SHARED 0x01 struct pool_head { - void **free_list; - CIRCLEQ_ENTRY(pool_head) pool_circqe; - unsigned int used; - unsigned int allocated; - unsigned int limit; - unsigned int minavail; - unsigned int size; - unsigned int flags; - unsigned int users; - char name[12]; + void **free_list; + CIRCLEQ_ENTRY(pool_head) pool_circqe; + unsigned int used; + unsigned int allocated; + unsigned int limit; + unsigned int minavail; + unsigned int size; + unsigned int flags; + unsigned int users; + char name[12]; }; CIRCLEQ_HEAD(pool_circqh, pool_head); @@ -40,7 +48,8 @@ void init_pools(); * returns it, otherwise creates this one. NULL is returned if no memory * is available for a new creation. */ -struct pool_head *create_pool(char *name, unsigned int size, unsigned int flags); +struct pool_head *create_pool(char *name, unsigned int size, + unsigned int flags); /* Allocate a new entry for pool , and return it for immediate use. * NULL is returned if no memory is available for a new creation. @@ -78,17 +87,17 @@ void dump_pools(void); * first case, is updated to point to the * next element in the list. */ -#define pool_alloc(pool) \ -({ \ - void *__p; \ - if ((__p = (pool)->free_list) == NULL) \ - __p = pool_refill_alloc(pool); \ - else { \ - (pool)->free_list = *(void **)(pool)->free_list;\ - (pool)->used++; \ - } \ - __p; \ -}) +#define pool_alloc(pool) \ + ({ \ + void *__p; \ + if ((__p = (pool)->free_list) == NULL) \ + __p = pool_refill_alloc(pool); \ + else { \ + (pool)->free_list = *(void **)(pool)->free_list; \ + (pool)->used++; \ + } \ + __p; \ + }) /* * Puts a memory area back to the corresponding pool. @@ -99,13 +108,13 @@ void dump_pools(void); * pointer. Just like with the libc's free(), nothing * is done if is NULL. */ -#define pool_free(pool, ptr) \ -({ \ - if (likely((ptr) != NULL)) { \ - *(void **)(ptr) = (void *)(pool)->free_list; \ - (pool)->free_list = (void *)(ptr); \ - (pool)->used--; \ - } \ -}) +#define pool_free(pool, ptr) \ + ({ \ + if (likely((ptr) != NULL)) { \ + *(void **)(ptr) = (void *)(pool)->free_list; \ + (pool)->free_list = (void *)(ptr); \ + (pool)->used--; \ + } \ + }) #endif /* DA_MEM_H_ */ diff --git a/src/agent/da_msg.c b/src/agent/da_msg.c index 21a09a4..c86f789 100644 --- a/src/agent/da_msg.c +++ b/src/agent/da_msg.c @@ -1,9 +1,18 @@ /* - * da_msg.c - * - * Created on: 2014年12月2日 - * Author: Jiansong - */ +* Copyright [2021] JD.com, Inc. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ #include #include "da_msg.h" @@ -108,7 +117,7 @@ int msg_init() { } msg_id = 0; frag_id = 0; - //红黑树,超时设置 + //绾㈤粦鏍戯紝瓒呮椂璁剧疆 rbtree_init(&tmo_rbt, &tmo_rbs); return 0; } @@ -226,7 +235,7 @@ struct msg *msg_get(struct conn *conn, bool request) { } /* - *释放msg所对应的资源,回收msg对象 + *閲婃斁msg鎵瀵瑰簲鐨勮祫婧愶紝鍥炴敹msg瀵硅薄 */ void msg_put(struct msg *m) { @@ -428,8 +437,8 @@ int msg_recv(struct context *ctx, struct conn *conn) { do { /* - * 从msg池中获取一个msg,当由于内存原因获取msg失败时置 - * conn->err标志,由于连接客户端关闭原因返回空的不处理 + * 浠巑sg姹犱腑鑾峰彇涓涓猰sg锛屽綋鐢变簬鍐呭瓨鍘熷洜鑾峰彇msg澶辫触鏃剁疆 + * conn->err鏍囧織锛岀敱浜庤繛鎺ュ鎴风鍏抽棴鍘熷洜杩斿洖绌虹殑涓嶅鐞 */ msg = conn->recv_next(ctx, conn, true); @@ -599,7 +608,7 @@ static int msg_send_chain(struct context *ctx, struct conn *conn, msg->sending = 0; conn->send_done(ctx, conn, msg); } - //因为要执行TAILQ_REMOVE(&send_msgq, msg, m_tqe); + //鍥犱负瑕佹墽琛孴AILQ_REMOVE(&send_msgq, msg, m_tqe); continue; } diff --git a/src/agent/da_msg.h b/src/agent/da_msg.h index cf1b941..ff5f8f9 100644 --- a/src/agent/da_msg.h +++ b/src/agent/da_msg.h @@ -1,22 +1,31 @@ /* - * da_msg.h + * Copyright [2021] JD.com, Inc. * - * Created on: 2014年12月2日 - * Author: Jiansong + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. */ #ifndef DA_MSG_H_ #define DA_MSG_H_ -#include -#include -#include #include "compiler.h" +#include "da_array.h" +#include "da_buf.h" #include "da_queue.h" #include "da_rbtree.h" -#include "da_buf.h" -#include "da_array.h" #include "da_string.h" +#include +#include +#include struct msg; struct conn; @@ -28,130 +37,128 @@ typedef int (*msg_fragment_t)(struct msg *, uint32_t, struct msg_tqh *); typedef int (*msg_coalesce_t)(struct msg *r); typedef enum msg_parse_result { - MSG_PARSE_OK, /* parsing ok */ - MSG_PARSE_ERROR, /* parsing error */ - MSG_PARSE_REPAIR, /* more to parse -> repair parsed & unparsed data */ - MSG_PARSE_AGAIN, /* incomplete -> parse again */ + MSG_PARSE_OK, /* parsing ok */ + MSG_PARSE_ERROR, /* parsing error */ + MSG_PARSE_REPAIR, /* more to parse -> repair parsed & unparsed data */ + MSG_PARSE_AGAIN, /* incomplete -> parse again */ } msg_parse_result_t; -#define MSG_TYPE_CODEC(ACTION) \ - ACTION( NOP ) \ - ACTION( RSP_RESULTCODE ) \ - ACTION( RSP_RESULTSET ) \ - ACTION( REQ_SVRADMIN ) \ - ACTION( REQ_GET ) \ - ACTION( REQ_PURGE ) \ - ACTION( REQ_INSERT ) \ - ACTION( REQ_UPDATE ) \ - ACTION( REQ_DELETE ) \ - ACTION( REQ_REPLACE) \ - ACTION( REQ_FLUSH ) \ - ACTION( REQ_INVALIDATE ) \ - ACTION( REQ_MONITOR ) \ +#define MSG_TYPE_CODEC(ACTION) \ + ACTION(NOP) \ + ACTION(RSP_RESULTCODE) \ + ACTION(RSP_RESULTSET) \ + ACTION(REQ_SVRADMIN) \ + ACTION(REQ_GET) \ + ACTION(REQ_PURGE) \ + ACTION(REQ_INSERT) \ + ACTION(REQ_UPDATE) \ + ACTION(REQ_DELETE) \ + ACTION(REQ_REPLACE) \ + ACTION(REQ_FLUSH) \ + ACTION(REQ_INVALIDATE) \ + ACTION(REQ_MONITOR) #define DEFINE_ACTION(_name) MSG_##_name, -typedef enum msg_type { - MSG_TYPE_CODEC(DEFINE_ACTION) -} msg_type_t; +typedef enum msg_type { MSG_TYPE_CODEC(DEFINE_ACTION) } msg_type_t; #undef DEFINE_ACTION /* * start point and end point must within a mbuf */ struct keypos { - uint8_t *start; /* key start pos */ - uint8_t *end; /* key end pos */ + uint8_t *start; /* key start pos */ + uint8_t *end; /* key end pos */ }; struct msg { - TAILQ_ENTRY(msg) c_i_tqe; /*in client inmsg queue*/ - TAILQ_ENTRY(msg) c_o_tqe; /*in client omsg queue*/ - TAILQ_ENTRY(msg) s_i_tqe; /*in the server inmsg queue*/ - TAILQ_ENTRY(msg) o_tqe; /*in frag_msgq or send q*/ - - - uint64_t id; /* id for svr asyn operation*/ - int idx; /* index of server*/ - uint64_t serialnr; /* client serialnr*/ - msg_type_t cmd; /* msg type */ - struct string accesskey; /* accesskey for the msg*/ - uint64_t keytype; /* keytype of the primary key*/ - uint64_t keyCount; /* number of key count*/ - struct keypos keys[32]; /* array of keys */ - int64_t hitflag; - struct conn *owner; /* message owner - client | server */ - struct conn *peer_conn; /* message peer(client | server) connection*/ - uint64_t peerid; /* id of msg peer*/ - struct msg *peer; /* msg peer*/ - - int64_t affectrows; /* affect rows in result info*/ - uint64_t totalrows; /* total rows in result info*/ - uint64_t numrows; /* result rows*/ - uint32_t fieldnums; /* field nums*/ - int64_t resultcode; /* resultcode in result info*/ - - struct msg *frag_owner; /* owner of fragment message */ - uint32_t nfrag; /* # fragment */ - uint32_t nfrag_done; /* # fragment done */ - uint64_t frag_id; /* id of fragmented message */ - struct msg *frag_seq[32]; /* sequence of fragment message, map from keys to fragments*/ - - struct buf_stqh buf_q; /* buff list in msg*/ - uint32_t mlen; /* message length */ - uint64_t start_ts; /* start timestamp in usec */ - struct rbnode tmo_rbe; /* entry in time rbtree */ - struct rbnode msg_rbe; /* entry in backwork rbtree*/ - - msg_parse_t parser; /* msg parse */ - msg_parse_result_t parse_res; /* msg parse result*/ - - msg_fragment_t fragment; /* message fragment */ - msg_coalesce_t coalesce; /* message post-coalesce */ - - struct mbuf *keyendbuf; /* the buf that end key located*/ - uint8_t *keyendpos; - struct mbuf *keycountendbuf; /* the buf that keycount end*/ - uint8_t *keycountendpos; /* end position in buff*/ - struct mbuf *setsplitbuf; - uint8_t *setsplitpos; - uint64_t keycountstartlen; /* the length until the start of keycount*/ - uint64_t keycountlen; /* keycount len*/ - uint64_t keylen; /* all key len include length and key value*/ - - uint8_t flags; /* header flags*/ - uint32_t *seclen; /* header 8 section len in msg header*/ - uint8_t cur_parse_id; /* current parse id*/ - uint64_t cur_parse_type; /* current parse type*/ - int cur_parse_lenth; /* current parse lenth*/ - int state; /* current parse state*/ - int field; /* current parse field*/ - int sec_parsed_len; - int parsed_len; /* len has been parsed*/ - int subkeylen; /* parsing subkey length*/ - int subkeycount; /* parsing subkey count */ - uint8_t *pos; /* parser position marker */ - uint8_t *token; /* token marker */ - - int err; /* errno on error? */ - unsigned error :1; /* error? */ - unsigned ferror :1; /* error? */ - unsigned request :1; /* request? or response? */ - unsigned done :1; /* done? */ - unsigned fdone :1; /* all fragments are done? */ - unsigned swallow :1; /* swallow response? */ - - unsigned cli_inq:1; /*msg in client in msgq*/ - unsigned cli_outq:1; /*msg in client out msgq*/ - unsigned sev_inq:1; /*msg in server in msgq*/ - unsigned sev_msgtree:1; /*msg in server in msg tree*/ - unsigned sending:1; /*msg is sending*/ + TAILQ_ENTRY(msg) c_i_tqe; /*in client inmsg queue*/ + TAILQ_ENTRY(msg) c_o_tqe; /*in client omsg queue*/ + TAILQ_ENTRY(msg) s_i_tqe; /*in the server inmsg queue*/ + TAILQ_ENTRY(msg) o_tqe; /*in frag_msgq or send q*/ + + uint64_t id; /* id for svr asyn operation*/ + int idx; /* index of server*/ + uint64_t serialnr; /* client serialnr*/ + msg_type_t cmd; /* msg type */ + struct string accesskey; /* accesskey for the msg*/ + uint64_t keytype; /* keytype of the primary key*/ + uint64_t keyCount; /* number of key count*/ + struct keypos keys[32]; /* array of keys */ + int64_t hitflag; + struct conn *owner; /* message owner - client | server */ + struct conn *peer_conn; /* message peer(client | server) connection*/ + uint64_t peerid; /* id of msg peer*/ + struct msg *peer; /* msg peer*/ + + int64_t affectrows; /* affect rows in result info*/ + uint64_t totalrows; /* total rows in result info*/ + uint64_t numrows; /* result rows*/ + uint32_t fieldnums; /* field nums*/ + int64_t resultcode; /* resultcode in result info*/ + + struct msg *frag_owner; /* owner of fragment message */ + uint32_t nfrag; /* # fragment */ + uint32_t nfrag_done; /* # fragment done */ + uint64_t frag_id; /* id of fragmented message */ + struct msg *frag_seq[32]; /* sequence of fragment message, map from keys to + fragments*/ + + struct buf_stqh buf_q; /* buff list in msg*/ + uint32_t mlen; /* message length */ + uint64_t start_ts; /* start timestamp in usec */ + struct rbnode tmo_rbe; /* entry in time rbtree */ + struct rbnode msg_rbe; /* entry in backwork rbtree*/ + + msg_parse_t parser; /* msg parse */ + msg_parse_result_t parse_res; /* msg parse result*/ + + msg_fragment_t fragment; /* message fragment */ + msg_coalesce_t coalesce; /* message post-coalesce */ + + struct mbuf *keyendbuf; /* the buf that end key located*/ + uint8_t *keyendpos; + struct mbuf *keycountendbuf; /* the buf that keycount end*/ + uint8_t *keycountendpos; /* end position in buff*/ + struct mbuf *setsplitbuf; + uint8_t *setsplitpos; + uint64_t keycountstartlen; /* the length until the start of keycount*/ + uint64_t keycountlen; /* keycount len*/ + uint64_t keylen; /* all key len include length and key value*/ + + uint8_t flags; /* header flags*/ + uint32_t *seclen; /* header 8 section len in msg header*/ + uint8_t cur_parse_id; /* current parse id*/ + uint64_t cur_parse_type; /* current parse type*/ + int cur_parse_lenth; /* current parse lenth*/ + int state; /* current parse state*/ + int field; /* current parse field*/ + int sec_parsed_len; + int parsed_len; /* len has been parsed*/ + int subkeylen; /* parsing subkey length*/ + int subkeycount; /* parsing subkey count */ + uint8_t *pos; /* parser position marker */ + uint8_t *token; /* token marker */ + + int err; /* errno on error? */ + unsigned error : 1; /* error? */ + unsigned ferror : 1; /* error? */ + unsigned request : 1; /* request? or response? */ + unsigned done : 1; /* done? */ + unsigned fdone : 1; /* all fragments are done? */ + unsigned swallow : 1; /* swallow response? */ + + unsigned cli_inq : 1; /*msg in client in msgq*/ + unsigned cli_outq : 1; /*msg in client out msgq*/ + unsigned sev_inq : 1; /*msg in server in msgq*/ + unsigned sev_msgtree : 1; /*msg in server in msg tree*/ + unsigned sending : 1; /*msg is sending*/ }; TAILQ_HEAD(msg_tqh, msg); void msg_tmo_delete(struct msg *msg); void msg_tmo_insert(struct msg *msg, struct conn *conn); -struct msg * msg_tmo_min(void); +struct msg *msg_tmo_min(void); int msg_init(); struct msg *msg_get(struct conn *conn, bool request); int msg_deinit(); @@ -163,7 +170,9 @@ bool msg_empty(struct msg *msg); int msg_recv(struct context *ctx, struct conn *conn); int msg_send(struct context *ctx, struct conn *conn); uint64_t msg_gen_frag_id(void); -int msg_append_buf(struct msg *msg, struct mbuf *sbuf, uint8_t *pos, size_t len); -struct mbuf *msg_insert_mem_bulk(struct msg *msg,struct mbuf *mbuf,uint8_t *pos,size_t len); +int msg_append_buf(struct msg *msg, struct mbuf *sbuf, uint8_t *pos, + size_t len); +struct mbuf *msg_insert_mem_bulk(struct msg *msg, struct mbuf *mbuf, + uint8_t *pos, size_t len); uint32_t msg_backend_idx(struct msg *msg, uint8_t *key, uint32_t keylen); #endif /* DA_MSG_H_ */ diff --git a/src/agent/da_request.c b/src/agent/da_request.c index a57f778..a2b0800 100644 --- a/src/agent/da_request.c +++ b/src/agent/da_request.c @@ -1,9 +1,18 @@ /* - * da_request.c - * - * Created on: 2014年12月9日 - * Author: Jiansong - */ +* Copyright [2021] JD.com, Inc. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ #include #include "da_request.h" @@ -278,7 +287,7 @@ static void req_forward(struct context *ctx, struct conn *c_conn, struct server_pool *pool; struct cache_instance *ci; - /*insert msg to client imsgq,waiting for + /*insert msg to client imsgq锛寃aiting for *the response,first add to backworker's queue */ c_conn->enqueue_inq(ctx, c_conn, msg); diff --git a/src/agent/da_request.h b/src/agent/da_request.h index f8ad275..29d4d41 100644 --- a/src/agent/da_request.h +++ b/src/agent/da_request.h @@ -1,8 +1,17 @@ /* - * da_request.h + * Copyright [2021] JD.com, Inc. * - * Created on: 2014年12月9日 - * Author: Jiansong + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. */ #ifndef DA_REQUEST_H_ @@ -15,19 +24,26 @@ struct conn; void req_put(struct msg *msg); struct msg *req_recv_next(struct context *ctx, struct conn *conn, bool alloc); -void req_recv_done(struct context *ctx, struct conn *conn, struct msg *msg,struct msg *nmsg); -void req_client_enqueue_omsgq(struct context *ctx, struct conn *conn,struct msg *msg); -void req_client_dequeue_omsgq(struct context *ctx, struct conn *conn,struct msg *msg); -void req_client_enqueue_imsgq(struct context *ctx, struct conn *conn,struct msg *msg); -void req_client_dequeue_imsgq(struct context *ctx, struct conn *conn,struct msg *msg); +void req_recv_done(struct context *ctx, struct conn *conn, struct msg *msg, + struct msg *nmsg); +void req_client_enqueue_omsgq(struct context *ctx, struct conn *conn, + struct msg *msg); +void req_client_dequeue_omsgq(struct context *ctx, struct conn *conn, + struct msg *msg); +void req_client_enqueue_imsgq(struct context *ctx, struct conn *conn, + struct msg *msg); +void req_client_dequeue_imsgq(struct context *ctx, struct conn *conn, + struct msg *msg); bool req_done(struct conn *c, struct msg *msg); -void req_server_enqueue_imsgq(struct context *ctx, struct conn *conn,struct msg *msg); -void req_server_dequeue_imsgq(struct context *ctx, struct conn *conn,struct msg *msg); +void req_server_enqueue_imsgq(struct context *ctx, struct conn *conn, + struct msg *msg); +void req_server_dequeue_imsgq(struct context *ctx, struct conn *conn, + struct msg *msg); struct msg *req_send_next(struct context *ctx, struct conn *conn); void req_send_done(struct context *ctx, struct conn *conn, struct msg *msg); void req_server_de_msgtree(struct context *ctx, struct conn *conn, - struct msg *msg); + struct msg *msg); void req_server_en_msgtree(struct context *ctx, struct conn *conn, - struct msg *msg); + struct msg *msg); bool req_error(struct conn *conn, struct msg *msg); #endif /* DA_REQUEST_H_ */ diff --git a/src/agent/da_response.c b/src/agent/da_response.c index 013957f..49ee388 100644 --- a/src/agent/da_response.c +++ b/src/agent/da_response.c @@ -1,9 +1,18 @@ /* - * da_response.c - * - * Created on: 2014年12月9日 - * Author: Jiansong - */ +* Copyright [2021] JD.com, Inc. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ #include #include @@ -182,7 +191,7 @@ void rsp_recv_done(struct context *ctx, struct conn *conn, struct msg *msg, return; } - //Get请求信息放在此处进行统计 + //Get璇锋眰淇℃伅鏀惧湪姝ゅ杩涜缁熻 if(req->cmd == MSG_REQ_GET) { stats_pool_incr(ctx, c_conn->owner, pool_requests_get); diff --git a/src/agent/da_response.h b/src/agent/da_response.h index 3b23018..e9e7587 100644 --- a/src/agent/da_response.h +++ b/src/agent/da_response.h @@ -1,8 +1,17 @@ /* - * da_response.h + * Copyright [2021] JD.com, Inc. * - * Created on: 2014年12月9日 - * Author: Jiansong + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. */ #ifndef DA_RESPONSE_H_ @@ -12,11 +21,11 @@ struct conn; struct msg; struct context; - void rsp_put(struct msg *msg); struct msg *rsp_get(struct conn *conn); struct msg *rsp_recv_next(struct context *ctx, struct conn *conn, bool alloc); -void rsp_recv_done(struct context *ctx, struct conn *conn, struct msg *msg,struct msg *nmsg); +void rsp_recv_done(struct context *ctx, struct conn *conn, struct msg *msg, + struct msg *nmsg); void rsp_forward(struct context *ctx, struct conn *s_conn, struct msg *req); struct msg *rsp_send_next(struct context *ctx, struct conn *conn); void rsp_send_done(struct context *ctx, struct conn *conn, struct msg *msg); diff --git a/src/agent/da_server.c b/src/agent/da_server.c index abd54a5..ecbfb6d 100644 --- a/src/agent/da_server.c +++ b/src/agent/da_server.c @@ -1,9 +1,18 @@ /* - * da_server.c - * - * Created on: 2014年12月2日 - * Author: Jiansong - */ +* Copyright [2021] JD.com, Inc. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ #include #include "da_server.h" #include "da_hashkit.h" @@ -17,10 +26,10 @@ #include "da_stats.h" #include "da_time.h" -static int keep_alive = 1; // 开启keepalive属性. 缺省值: 0(关闭) -static int keep_idle = 5; // 如果在60秒内没有任何数据交互,则进行探测. 缺省值:7200(s) -static int keep_interval = 5; // 探测时发探测包的时间间隔为5秒. 缺省值:75(s) -static int keep_count = 1; // 探测重试的次数. 全部超时则认定连接失效..缺省值:9(次) +static int keep_alive = 1; // 寮鍚痥eepalive灞炴. 缂虹渷鍊: 0(鍏抽棴) +static int keep_idle = 5; // 濡傛灉鍦60绉掑唴娌℃湁浠讳綍鏁版嵁浜や簰,鍒欒繘琛屾帰娴. 缂虹渷鍊:7200(s) +static int keep_interval = 5; // 鎺㈡祴鏃跺彂鎺㈡祴鍖呯殑鏃堕棿闂撮殧涓5绉. 缂虹渷鍊:75(s) +static int keep_count = 1; // 鎺㈡祴閲嶈瘯鐨勬鏁. 鍏ㄩ儴瓒呮椂鍒欒瀹氳繛鎺ュけ鏁..缂虹渷鍊:9(娆) void instance_ref(struct conn *conn, void *owner) { @@ -876,7 +885,7 @@ int server_pool_run(struct server_pool *pool) { } /* - * 对于每个server pool 构建后台的服务器结构,构建hash环 + * 瀵逛簬姣忎釜server pool 鏋勫缓鍚庡彴鐨勬湇鍔″櫒缁撴瀯锛屾瀯寤篽ash鐜 */ static int server_pool_each_run(void *elem, void *data) { return server_pool_run(elem); @@ -885,7 +894,7 @@ static int server_pool_each_run(void *elem, void *data) { /* - * 初始化server pool + * 鍒濆鍖杝erver pool */ int server_pool_init(struct array *server_pool, struct array *conf_pool, struct context *ctx) { @@ -902,7 +911,7 @@ int server_pool_init(struct array *server_pool, struct array *conf_pool, } /* transform conf pool to server pool */ - // 对于conf_pool中的每个对象调用conf_pool_each_transform函数 + // 瀵逛簬conf_pool涓殑姣忎釜瀵硅薄璋冪敤conf_pool_each_transform鍑芥暟 status = array_each(conf_pool, conf_pool_each_transform, server_pool); if (status != 0) { server_pool_deinit(server_pool); diff --git a/src/agent/da_server.h b/src/agent/da_server.h index 4b167e8..788ce4e 100644 --- a/src/agent/da_server.h +++ b/src/agent/da_server.h @@ -1,111 +1,120 @@ /* - * da_server.h + * Copyright [2021] JD.com, Inc. * - * Created on: 2014年12月2日 - * Author: Jiansong + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. */ #ifndef DA_SERVER_H_ #define DA_SERVER_H_ +#include "da_array.h" #include "da_conn.h" #include "da_string.h" -#include "da_array.h" -#include #include +#include typedef uint32_t (*hash_t)(const char *, size_t); #define ERROR_UPPER_LIMIT 1 #define FAIL_TIME_LIMIT 6 struct continuum { - uint32_t index; /* server index */ - uint32_t value; /* hash value */ + uint32_t index; /* server index */ + uint32_t value; /* hash value */ }; struct cache_instance { - struct string pname; - void *owner; - int nerr; - int idx; - uint16_t port; /* port */ - uint32_t weight; /* weight */ - int family; /* socket family */ - socklen_t addrlen; /* socket length */ - struct sockaddr *addr; /* socket address (ref in conf_server) */ - uint32_t ns_conn_q; /* # server connection */ - struct conn_tqh s_conn_q; /*server connection*/ - uint64_t last_failure_ms; /*cahche the failure time*/ - uint16_t failure_num;/*cache failure time*/ - int num; + struct string pname; + void *owner; + int nerr; + int idx; + uint16_t port; /* port */ + uint32_t weight; /* weight */ + int family; /* socket family */ + socklen_t addrlen; /* socket length */ + struct sockaddr *addr; /* socket address (ref in conf_server) */ + uint32_t ns_conn_q; /* # server connection */ + struct conn_tqh s_conn_q; /*server connection*/ + uint64_t last_failure_ms; /*cahche the failure time*/ + uint16_t failure_num; /*cache failure time*/ + int num; }; - struct server { - uint32_t idx; /* server index */ - struct server_pool *owner; /* owner pool */ - uint16_t high_prty_idx; - uint16_t high_prty_cnt; - uint16_t low_prty_idx; - uint16_t low_prty_cnt; - struct string name; /* name (ref in conf_server) */ - int weight; - int replica_enable; - struct cache_instance *master; - struct array high_ptry_ins; - struct array low_prty_ins; + uint32_t idx; /* server index */ + struct server_pool *owner; /* owner pool */ + uint16_t high_prty_idx; + uint16_t high_prty_cnt; + uint16_t low_prty_idx; + uint16_t low_prty_cnt; + struct string name; /* name (ref in conf_server) */ + int weight; + int replica_enable; + struct cache_instance *master; + struct array high_ptry_ins; + struct array low_prty_ins; }; struct server_pool { - uint32_t idx; /* pool index */ - uint32_t mid; /* pool manager id*/ - struct context *ctx; /* owner context */ - struct conn *listener; - struct conn_tqh c_conn_q; /*client connection*/ - uint32_t c_conn_count; /* number of client connection */ - struct array server; /* server[] */ - uint32_t ncontinuum; /* # continuum points */ - uint32_t nserver_continuum; /* # servers - live and dead on continuum (const) */ - struct continuum *continuum; /* continuum */ + uint32_t idx; /* pool index */ + uint32_t mid; /* pool manager id*/ + struct context *ctx; /* owner context */ + struct conn *listener; + struct conn_tqh c_conn_q; /*client connection*/ + uint32_t c_conn_count; /* number of client connection */ + struct array server; /* server[] */ + uint32_t ncontinuum; /* # continuum points */ + uint32_t + nserver_continuum; /* # servers - live and dead on continuum (const) */ + struct continuum *continuum; /* continuum */ + + struct string name; /* pool name (ref in conf_pool) */ + struct string addrstr; /* pool address (ref in conf_pool) */ + struct string accesskey; /* access token for this pool */ + uint16_t port; /* port */ + int family; /* socket family */ + socklen_t addrlen; /* socket length */ + struct sockaddr *addr; /* socket address (ref in conf_pool) */ + int key_hash_type; /* key hash type (hash_type_t) */ + hash_t key_hash; /* key hasher */ + int backlog; /* listen backlog */ + int timeout; /* timeout in msec */ + uint32_t client_connections; /* maximum # client connection */ + uint32_t server_connections; /* maximum # server connection */ + int replica_enable; /*Replica enable for whole system*/ + struct string module_idc; /*sp IDC*/ + unsigned preconnect : 1; /* preconnect? */ + int main_report; + int instance_report; + int auto_remove_replica; - struct string name; /* pool name (ref in conf_pool) */ - struct string addrstr; /* pool address (ref in conf_pool) */ - struct string accesskey; /* access token for this pool */ - uint16_t port; /* port */ - int family; /* socket family */ - socklen_t addrlen; /* socket length */ - struct sockaddr *addr; /* socket address (ref in conf_pool) */ - int key_hash_type; /* key hash type (hash_type_t) */ - hash_t key_hash; /* key hasher */ - int backlog; /* listen backlog */ - int timeout; /* timeout in msec */ - uint32_t client_connections;/* maximum # client connection */ - uint32_t server_connections;/* maximum # server connection */ - int replica_enable; /*Replica enable for whole system*/ - struct string module_idc; /*sp IDC*/ - unsigned preconnect :1; /* preconnect? */ - int main_report; - int instance_report; - int auto_remove_replica; - - int top_percentile_enable; - int top_percentile_fd; - struct sockaddr_in top_percentile_addr; - int top_percentile_addr_len; - struct remote_param *top_percentile_param; + int top_percentile_enable; + int top_percentile_fd; + struct sockaddr_in top_percentile_addr; + int top_percentile_addr_len; + struct remote_param *top_percentile_param; }; uint32_t server_pool_idx(struct server_pool *pool, uint8_t *key, - uint32_t keylen); + uint32_t keylen); void server_close(struct context *ctx, struct conn *conn); void instance_ref(struct conn *conn, void *owner); void instance_unref(struct conn *conn); int server_active(struct conn *conn); int server_init(struct array *server, struct array *conf_server, - struct server_pool *sp); + struct server_pool *sp); void server_connected(struct context *ctx, struct conn *conn); int server_init(struct array *server, struct array *conf_server, - struct server_pool *sp); + struct server_pool *sp); void instance_deinit(struct array *server); void server_deinit(struct array *server); int server_pool_preconnect(struct context *ctx); @@ -113,10 +122,10 @@ void server_pool_disconnect(struct context *ctx); int server_pool_run(struct server_pool *pool); void server_pool_deinit(struct array *server_pool); int server_pool_init(struct array *server_pool, struct array *conf_pool, - struct context *ctx); + struct context *ctx); int server_timeout(struct conn *conn); -struct conn * server_pool_conn(struct context *ctx, struct server_pool *pool, - struct msg *msg); +struct conn *server_pool_conn(struct context *ctx, struct server_pool *pool, + struct msg *msg); int incr_instance_failure_time(struct cache_instance *ci); int decr_instance_failure_time(struct msg *msg); #endif /* DA_SERVER_H_ */ diff --git a/src/agent/da_signal.c b/src/agent/da_signal.c index 13bfe0a..3e8840e 100644 --- a/src/agent/da_signal.c +++ b/src/agent/da_signal.c @@ -1,9 +1,18 @@ /* - * da_signal.c - * - * Created on: 2014年12月2日 - * Author: Jiansong - */ +* Copyright [2021] JD.com, Inc. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ #include "da_signal.h" #include "da_log.h" diff --git a/src/agent/da_signal.h b/src/agent/da_signal.h index a549104..d947121 100644 --- a/src/agent/da_signal.h +++ b/src/agent/da_signal.h @@ -1,33 +1,42 @@ /* - * da_signal.h + * Copyright [2021] JD.com, Inc. * - * Created on: 2014年12月2日 - * Author: Jiansong + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. */ #ifndef DA_SIGNAL_H_ #define DA_SIGNAL_H_ +#include "compiler.h" +#include "da_queue.h" #include #include -#include "da_queue.h" -#include "compiler.h" #define MAX_SIGNAL 256 /* those are highly dynamic and stored in pools */ struct sig_handler { - TAILQ_ENTRY(sig_handler) sig_tqe; - void *handler; /* function to call or task to wake up */ - int arg; /*arg needed when process function or signals*/ + TAILQ_ENTRY(sig_handler) sig_tqe; + void *handler; /* function to call or task to wake up */ + int arg; /*arg needed when process function or signals*/ }; TAILQ_HEAD(sig_tqh, sig_handler); /* one per signal */ struct signal_descriptor { - int count; /* number of times raised */ - struct sig_tqh sig_tqh; /* sig_handler */ + int count; /* number of times raised */ + struct sig_tqh sig_tqh; /* sig_handler */ }; extern volatile int signal_queue_len; @@ -38,15 +47,14 @@ void signal_handler(int sig); void __signal_process_queue(); int signal_init(); void deinit_signals(); -struct sig_handler *signal_register_fct(int sig,void (*fct)(struct sig_handler *), int arg); +struct sig_handler * +signal_register_fct(int sig, void (*fct)(struct sig_handler *), int arg); void signal_unregister_handler(int sig, struct sig_handler *handler); void signal_unregister_target(int sig, void *target); - - static inline void signal_process_queue() { - if (unlikely(signal_queue_len > 0)) - __signal_process_queue(); + if (unlikely(signal_queue_len > 0)) + __signal_process_queue(); } #endif /* DA_SIGNAL_H_ */ diff --git a/src/agent/da_stats.c b/src/agent/da_stats.c index 553a294..ad90152 100644 --- a/src/agent/da_stats.c +++ b/src/agent/da_stats.c @@ -1,9 +1,18 @@ /* - * da_stats.c - * - * Created on: 2015骞3鏈9鏃 - * Author: Jiansong - */ +* Copyright [2021] JD.com, Inc. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ #include #include diff --git a/src/agent/da_stats.h b/src/agent/da_stats.h index 3a1414a..42f9e70 100644 --- a/src/agent/da_stats.h +++ b/src/agent/da_stats.h @@ -1,15 +1,24 @@ /* - * da_stats.h + * Copyright [2021] JD.com, Inc. * - * Created on: 2015年3月9日 - * Author: Jiansong + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. */ #ifndef DA_STATS_H_ #define DA_STATS_H_ -#include "da_string.h" #include "da_array.h" +#include "da_string.h" struct event_base; struct server_pool; @@ -17,236 +26,239 @@ struct context; struct server; struct cache_instance; -typedef void (*event_stats_cb_t)(void *,int); +typedef void (*event_stats_cb_t)(void *, int); #define STATS_FILE "da.stats" #define STATS_DIR "../stats/" -#define STATS_INTERVAL (10 * 1000) /* in msec */ -#define DEFAULT_HTTP_TIMEOUT 5 /*default heep connect and request timeout in second*/ - -#define STATS_POOL_CODEC(ACTION) \ - /* client behavior */ \ - ACTION( client_eof, STATS_COUNTER, "# eof on client connections") \ - ACTION( client_err, STATS_COUNTER, "# errors on client connections") \ - ACTION( client_connections, STATS_GAUGE, "# active client connections") \ - /* forwarder behavior */ \ - ACTION( fragment_error, STATS_COUNTER, "# times we encountered a fragment error") \ - ACTION( forward_error, STATS_COUNTER, "# times we encountered a forwarding error") \ - ACTION( coalesce_error, STATS_COUNTER, "# times we encountered a coalesce error") \ - /* data behavior */ \ - ACTION( pool_requests, STATS_COUNTER, "# pool requests") \ - ACTION( pool_requests_get, STATS_COUNTER, "# pool requests get") \ - ACTION( pool_request_bytes, STATS_COUNTER, " pool total request bytes") \ - ACTION( pool_responses, STATS_COUNTER, "# pool responses") \ - ACTION( pool_response_bytes, STATS_COUNTER, " pool total response bytes") \ - ACTION( pool_withoutkey_req, STATS_COUNTER, "# pool without key requests") \ - ACTION( pool_tech_hit, STATS_COUNTER, "# pool tech hit times") \ - ACTION( pool_logic_hit, STATS_COUNTER, "# pool logic hit times") \ - ACTION( pool_elaspe_time, STATS_COUNTER, "# pool elapse time(us)") \ - ACTION( pool_package_split, STATS_COUNTER, "# pool package split times") \ - ACTION( pool_request_get_keys, STATS_COUNTER, "# pool get request key count") \ - -#define STATS_SERVER_CODEC(ACTION) \ - /* server behavior */ \ - ACTION( server_eof, STATS_COUNTER, "# eof on server connections") \ - ACTION( server_err, STATS_COUNTER, "# errors on server connections") \ - ACTION( server_connections, STATS_GAUGE, "# active server connections") \ - /* data behavior */ \ - ACTION( server_requests, STATS_COUNTER, "# server requests") \ - ACTION( server_request_bytes, STATS_COUNTER, "server total request bytes") \ - ACTION( server_responses, STATS_COUNTER, "# server responses") \ - ACTION( server_response_bytes, STATS_COUNTER, "server total response bytes") \ - ACTION( server_request_error, STATS_COUNTER, "# server requests error") \ - ACTION( server_in_queue, STATS_GAUGE, "# requests in incoming queue") \ - ACTION( server_in_tree, STATS_GAUGE, "# requests in backwork search tree") \ +#define STATS_INTERVAL (10 * 1000) /* in msec */ +#define DEFAULT_HTTP_TIMEOUT \ + 5 /*default heep connect and request timeout in second*/ + +#define STATS_POOL_CODEC(ACTION) \ + /* client behavior */ \ + ACTION(client_eof, STATS_COUNTER, "# eof on client connections") \ + ACTION(client_err, STATS_COUNTER, "# errors on client connections") \ + ACTION(client_connections, STATS_GAUGE, "# active client connections") \ + /* forwarder behavior */ \ + ACTION(fragment_error, STATS_COUNTER, \ + "# times we encountered a fragment error") \ + ACTION(forward_error, STATS_COUNTER, \ + "# times we encountered a forwarding error") \ + ACTION(coalesce_error, STATS_COUNTER, \ + "# times we encountered a coalesce error") \ + /* data behavior */ \ + ACTION(pool_requests, STATS_COUNTER, "# pool requests") \ + ACTION(pool_requests_get, STATS_COUNTER, "# pool requests get") \ + ACTION(pool_request_bytes, STATS_COUNTER, " pool total request bytes") \ + ACTION(pool_responses, STATS_COUNTER, "# pool responses") \ + ACTION(pool_response_bytes, STATS_COUNTER, " pool total response bytes") \ + ACTION(pool_withoutkey_req, STATS_COUNTER, "# pool without key requests") \ + ACTION(pool_tech_hit, STATS_COUNTER, "# pool tech hit times") \ + ACTION(pool_logic_hit, STATS_COUNTER, "# pool logic hit times") \ + ACTION(pool_elaspe_time, STATS_COUNTER, "# pool elapse time(us)") \ + ACTION(pool_package_split, STATS_COUNTER, "# pool package split times") \ + ACTION(pool_request_get_keys, STATS_COUNTER, "# pool get request key count") + +#define STATS_SERVER_CODEC(ACTION) \ + /* server behavior */ \ + ACTION(server_eof, STATS_COUNTER, "# eof on server connections") \ + ACTION(server_err, STATS_COUNTER, "# errors on server connections") \ + ACTION(server_connections, STATS_GAUGE, "# active server connections") \ + /* data behavior */ \ + ACTION(server_requests, STATS_COUNTER, "# server requests") \ + ACTION(server_request_bytes, STATS_COUNTER, "server total request bytes") \ + ACTION(server_responses, STATS_COUNTER, "# server responses") \ + ACTION(server_response_bytes, STATS_COUNTER, "server total response bytes") \ + ACTION(server_request_error, STATS_COUNTER, "# server requests error") \ + ACTION(server_in_queue, STATS_GAUGE, "# requests in incoming queue") \ + ACTION(server_in_tree, STATS_GAUGE, "# requests in backwork search tree") typedef enum stats_type { - STATS_INVALID, - STATS_COUNTER, /* monotonic accumulator */ - STATS_GAUGE, /* non-monotonic accumulator */ - STATS_TIMESTAMP, /* monotonic timestamp (in nsec) */ - STATS_SENTINEL + STATS_INVALID, + STATS_COUNTER, /* monotonic accumulator */ + STATS_GAUGE, /* non-monotonic accumulator */ + STATS_TIMESTAMP, /* monotonic timestamp (in nsec) */ + STATS_SENTINEL } stats_type_t; /* struct for flush data to stats file*/ -struct _map_item -{ - char filename[256]; - void *_map_start; - int _map_size; +struct _map_item { + char filename[256]; + void *_map_start; + int _map_size; }; -struct stats_file_pool_head -{ - char poolname[256]; - uint32_t mid; - uint32_t poolfields; - uint32_t servernum; +struct stats_file_pool_head { + char poolname[256]; + uint32_t mid; + uint32_t poolfields; + uint32_t servernum; }; -struct stats_file_server_head -{ - char servername[256]; - uint32_t sid; - uint32_t serverfields; +struct stats_file_server_head { + char servername[256]; + uint32_t sid; + uint32_t serverfields; }; -struct stats_file_item -{ - stats_type_t type; - int64_t stat_once; - uint64_t stat_all; +struct stats_file_item { + stats_type_t type; + int64_t stat_once; + uint64_t stat_all; }; -struct stats_file_server -{ - struct stats_file_server_head *shead; - struct stats_file_item *server_item_list; +struct stats_file_server { + struct stats_file_server_head *shead; + struct stats_file_item *server_item_list; }; -struct stats_file_pool -{ - struct stats_file_pool_head *phead; - struct stats_file_item *pool_item_list; - struct array stats_file_servers; +struct stats_file_pool { + struct stats_file_pool_head *phead; + struct stats_file_item *pool_item_list; + struct array stats_file_servers; }; struct stats_metric { - stats_type_t type; /* type */ - struct string name; /* name (ref) */ - union { - int64_t counter; /* accumulating counter */ - int64_t timestamp; /* monotonic timestamp */ - } value; + stats_type_t type; /* type */ + struct string name; /* name (ref) */ + union { + int64_t counter; /* accumulating counter */ + int64_t timestamp; /* monotonic timestamp */ + } value; }; struct stats_server { - struct string name; /* server name (ref) */ - struct array metric; /* stats_metric[] for server codec */ + struct string name; /* server name (ref) */ + struct array metric; /* stats_metric[] for server codec */ }; struct stats_pool { - uint32_t mid; - uint16_t port; - struct string name; /* pool name (ref) */ - struct array metric; /* stats_metric[] for pool codec */ - struct array server; /* stats_server[] */ - int main_report; - int instance_report; + uint32_t mid; + uint16_t port; + struct string name; /* pool name (ref) */ + struct array metric; /* stats_metric[] for pool codec */ + struct array server; /* stats_server[] */ + int main_report; + int instance_report; }; struct stats { - int interval; /* stats aggregation interval */ - uint64_t start_ts; /* start timestamp of da */ - struct array _map_items; /* _map_item[] */ - struct array aggregator; /* stats_file_pool[](b,a+b) */ - struct array current; /* stats_pool[] (a)*/ - struct array shadow; /* stats_pool[] (b)*/ - pthread_t tid; /* stats aggregator thread */ - char localip[16]; /* ip address of this machine */ - volatile int aggregate; /* shadow (b) aggregate? */ - volatile int updated; /* current (a) updated? */ + int interval; /* stats aggregation interval */ + uint64_t start_ts; /* start timestamp of da */ + struct array _map_items; /* _map_item[] */ + struct array aggregator; /* stats_file_pool[](b,a+b) */ + struct array current; /* stats_pool[] (a)*/ + struct array shadow; /* stats_pool[] (b)*/ + pthread_t tid; /* stats aggregator thread */ + char localip[16]; /* ip address of this machine */ + volatile int aggregate; /* shadow (b) aggregate? */ + volatile int updated; /* current (a) updated? */ }; #define DEFINE_ACTION(_name, _type, _desc) STATS_POOL_##_name, typedef enum stats_pool_field { - STATS_POOL_CODEC(DEFINE_ACTION) - STATS_POOL_NFIELD + STATS_POOL_CODEC(DEFINE_ACTION) STATS_POOL_NFIELD } stats_pool_field_t; #undef DEFINE_ACTION #define DEFINE_ACTION(_name, _type, _desc) STATS_SERVER_##_name, typedef enum stats_server_field { - STATS_SERVER_CODEC(DEFINE_ACTION) - STATS_SERVER_NFIELD + STATS_SERVER_CODEC(DEFINE_ACTION) STATS_SERVER_NFIELD } stats_server_field_t; #undef DEFINE_ACTION /* struct for report data to monitor centor*/ -struct _ReportParam -{ - uint32_t uCType; - uint32_t uEType; - int64_t iData1; - int64_t iData2; - int64_t iExtra; - int64_t iDateTime; - uint32_t uCmd; +struct _ReportParam { + uint32_t uCType; + uint32_t uEType; + int64_t iData1; + int64_t iData2; + int64_t iExtra; + int64_t iDateTime; + uint32_t uCmd; }; -enum -{ - REQ_ELAPSE = 1, - GET_HIT_RATIO, - REQ_COUNT, - NET_IN_FLOW, - NET_OUT_FLOW = 6, - RESP_COUNT, - GET_TECH_HIT_RATIO = 9, - AGENT_TIMES = 15, //agent count. data1 req, data2 res - AGENT_TIME = 16, //agent time. data1 req count, data2 time - AGENT_ERROR = 17, //agent error count. data1 count, data2 default 0 - AGENT_HIT = 18, //agent logic ratio. data1 get count, data2 hit count - AGENT_TECH_HIT = 19, //agent tech ratio. data1 get count, data2 hit count - AGENT_INOUT_BYTES = 20, //agent request and response bytes. data1 request bytes, data2 response bytes - CONN_COUNT =100 +enum { + REQ_ELAPSE = 1, + GET_HIT_RATIO, + REQ_COUNT, + NET_IN_FLOW, + NET_OUT_FLOW = 6, + RESP_COUNT, + GET_TECH_HIT_RATIO = 9, + AGENT_TIMES = 15, // agent count. data1 req, data2 res + AGENT_TIME = 16, // agent time. data1 req count, data2 time + AGENT_ERROR = 17, // agent error count. data1 count, data2 default 0 + AGENT_HIT = 18, // agent logic ratio. data1 get count, data2 hit count + AGENT_TECH_HIT = 19, // agent tech ratio. data1 get count, data2 hit count + AGENT_INOUT_BYTES = 20, // agent request and response bytes. data1 request + // bytes, data2 response bytes + CONN_COUNT = 100 }; -enum -{ - FIVE_SEC = 1, - TEN_SEC, - THIRTY_SEC, - ONE_MIN, +enum { + FIVE_SEC = 1, + TEN_SEC, + THIRTY_SEC, + ONE_MIN, }; -enum -{ - CMD_INSERT = 0, - CMD_UPDATE, +enum { + CMD_INSERT = 0, + CMD_UPDATE, }; #if defined DA_STATS && DA_STATS == 1 -#define stats_pool_incr(_ctx, _pool, _name) do { \ - _stats_pool_incr(_ctx, _pool, STATS_POOL_##_name); \ -} while (0) - -#define stats_pool_decr(_ctx, _pool, _name) do { \ - _stats_pool_decr(_ctx, _pool, STATS_POOL_##_name); \ -} while (0) - -#define stats_pool_incr_by(_ctx, _pool, _name, _val) do { \ - _stats_pool_incr_by(_ctx, _pool, STATS_POOL_##_name, _val); \ -} while (0) - -#define stats_pool_decr_by(_ctx, _pool, _name, _val) do { \ - _stats_pool_decr_by(_ctx, _pool, STATS_POOL_##_name, _val); \ -} while (0) - -#define stats_pool_set_ts(_ctx, _pool, _name, _val) do { \ - _stats_pool_set_ts(_ctx, _pool, STATS_POOL_##_name, _val); \ -} while (0) - -#define stats_server_incr(_ctx, _server, _name) do { \ - _stats_server_incr(_ctx, _server, STATS_SERVER_##_name); \ -} while (0) - -#define stats_server_decr(_ctx, _server, _name) do { \ - _stats_server_decr(_ctx, _server, STATS_SERVER_##_name); \ -} while (0) - -#define stats_server_incr_by(_ctx, _server, _name, _val) do { \ - _stats_server_incr_by(_ctx, _server, STATS_SERVER_##_name, _val); \ -} while (0) - -#define stats_server_decr_by(_ctx, _server, _name, _val) do { \ - _stats_server_decr_by(_ctx, _server, STATS_SERVER_##_name, _val); \ -} while (0) - -#define stats_server_set_ts(_ctx, _server, _name, _val) do { \ - _stats_server_set_ts(_ctx, _server, STATS_SERVER_##_name, _val); \ -} while (0) +#define stats_pool_incr(_ctx, _pool, _name) \ + do { \ + _stats_pool_incr(_ctx, _pool, STATS_POOL_##_name); \ + } while (0) + +#define stats_pool_decr(_ctx, _pool, _name) \ + do { \ + _stats_pool_decr(_ctx, _pool, STATS_POOL_##_name); \ + } while (0) + +#define stats_pool_incr_by(_ctx, _pool, _name, _val) \ + do { \ + _stats_pool_incr_by(_ctx, _pool, STATS_POOL_##_name, _val); \ + } while (0) + +#define stats_pool_decr_by(_ctx, _pool, _name, _val) \ + do { \ + _stats_pool_decr_by(_ctx, _pool, STATS_POOL_##_name, _val); \ + } while (0) + +#define stats_pool_set_ts(_ctx, _pool, _name, _val) \ + do { \ + _stats_pool_set_ts(_ctx, _pool, STATS_POOL_##_name, _val); \ + } while (0) + +#define stats_server_incr(_ctx, _server, _name) \ + do { \ + _stats_server_incr(_ctx, _server, STATS_SERVER_##_name); \ + } while (0) + +#define stats_server_decr(_ctx, _server, _name) \ + do { \ + _stats_server_decr(_ctx, _server, STATS_SERVER_##_name); \ + } while (0) + +#define stats_server_incr_by(_ctx, _server, _name, _val) \ + do { \ + _stats_server_incr_by(_ctx, _server, STATS_SERVER_##_name, _val); \ + } while (0) + +#define stats_server_decr_by(_ctx, _server, _name, _val) \ + do { \ + _stats_server_decr_by(_ctx, _server, STATS_SERVER_##_name, _val); \ + } while (0) + +#define stats_server_set_ts(_ctx, _server, _name, _val) \ + do { \ + _stats_server_set_ts(_ctx, _server, STATS_SERVER_##_name, _val); \ + } while (0) #else @@ -268,23 +280,34 @@ enum #endif -#define stats_enabled DA_STATS +#define stats_enabled DA_STATS void stats_describe(void); -void _stats_pool_incr(struct context *ctx, struct server_pool *pool, stats_pool_field_t fidx); -void _stats_pool_decr(struct context *ctx, struct server_pool *pool, stats_pool_field_t fidx); -void _stats_pool_incr_by(struct context *ctx, struct server_pool *pool, stats_pool_field_t fidx, int64_t val); -void _stats_pool_decr_by(struct context *ctx, struct server_pool *pool, stats_pool_field_t fidx, int64_t val); -void _stats_pool_set_ts(struct context *ctx, struct server_pool *pool, stats_pool_field_t fidx, int64_t val); - -void _stats_server_incr(struct context *ctx, struct cache_instance *ins, stats_server_field_t fidx); -void _stats_server_decr(struct context *ctx, struct cache_instance *ins, stats_server_field_t fidx); -void _stats_server_incr_by(struct context *ctx, struct cache_instance *ins, stats_server_field_t fidx, int64_t val); -void _stats_server_decr_by(struct context *ctx, struct cache_instance *ins, stats_server_field_t fidx, int64_t val); -void _stats_server_set_ts(struct context *ctx, struct cache_instance *ins, stats_server_field_t fidx, int64_t val); - -struct stats *stats_create(int stats_interval, char *localip, struct array *server_pool); +void _stats_pool_incr(struct context *ctx, struct server_pool *pool, + stats_pool_field_t fidx); +void _stats_pool_decr(struct context *ctx, struct server_pool *pool, + stats_pool_field_t fidx); +void _stats_pool_incr_by(struct context *ctx, struct server_pool *pool, + stats_pool_field_t fidx, int64_t val); +void _stats_pool_decr_by(struct context *ctx, struct server_pool *pool, + stats_pool_field_t fidx, int64_t val); +void _stats_pool_set_ts(struct context *ctx, struct server_pool *pool, + stats_pool_field_t fidx, int64_t val); + +void _stats_server_incr(struct context *ctx, struct cache_instance *ins, + stats_server_field_t fidx); +void _stats_server_decr(struct context *ctx, struct cache_instance *ins, + stats_server_field_t fidx); +void _stats_server_incr_by(struct context *ctx, struct cache_instance *ins, + stats_server_field_t fidx, int64_t val); +void _stats_server_decr_by(struct context *ctx, struct cache_instance *ins, + stats_server_field_t fidx, int64_t val); +void _stats_server_set_ts(struct context *ctx, struct cache_instance *ins, + stats_server_field_t fidx, int64_t val); + +struct stats *stats_create(int stats_interval, char *localip, + struct array *server_pool); void stats_destroy(struct stats *stats); void stats_swap(struct stats *stats); diff --git a/src/agent/da_time.c b/src/agent/da_time.c index de9b327..daa8db3 100644 --- a/src/agent/da_time.c +++ b/src/agent/da_time.c @@ -1,9 +1,18 @@ /* - * da_time.c - * - * Created on: 2014年12月1日 - * Author: Jiansong - */ +* Copyright [2021] JD.com, Inc. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ #include "da_time.h" diff --git a/src/agent/da_time.h b/src/agent/da_time.h index fc5b671..6d208a5 100644 --- a/src/agent/da_time.h +++ b/src/agent/da_time.h @@ -1,82 +1,93 @@ /* - * da_time.h + * Copyright [2021] JD.com, Inc. * - * Created on: 2014年12月1日 - * Author: Jiansong + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. */ #ifndef DA_TIME_H_ #define DA_TIME_H_ - -#include +#include "compiler.h" #include +#include #include -#include "compiler.h" /* eternity when exprimed in timeval */ #ifndef TV_ETERNITY -#define TV_ETERNITY (~0UL) +#define TV_ETERNITY (~0UL) #endif /* eternity when exprimed in ms */ #ifndef TV_ETERNITY_MS -#define TV_ETERNITY_MS (-1) +#define TV_ETERNITY_MS (-1) #endif -#define TIME_ETERNITY (TV_ETERNITY_MS) +#define TIME_ETERNITY (TV_ETERNITY_MS) /* we want to be able to detect time jumps. Fix the maximum wait time to a low * value so that we know the time has changed if we wait longer. */ -#define MAX_DELAY_MS 1000 - - -/* returns the lowest delay amongst and , and respects TIME_ETERNITY */ -#define MINTIME(old, new) (((new)<0)?(old):(((old)<0||(new)<(old))?(new):(old))) -#define SETNOW(a) (*a=now) - -extern uint32_t curr_sec_ms; /* millisecond of current second (0..999) */ -extern uint32_t ms_left_scaled; /* milliseconds left for current second (0..2^32-1) */ -extern uint32_t curr_sec_ms_scaled; /* millisecond of current second (0..2^32-1) */ -extern uint64_t now_ms; /* internal date in milliseconds (may wrap) */ -extern uint64_t now_us; /* internal date in us (may wrap) */ -extern uint32_t samp_time; /* total elapsed time over current sample */ -extern uint32_t idle_time; /* total idle time over current sample */ -extern uint32_t idle_pct; /* idle to total ratio over last sample (percent) */ -extern struct timeval now; /* internal date is a monotonic function of real clock */ -extern struct timeval date; /* the real current date */ -extern struct timeval start_date; /* the process's start date */ -extern struct timeval before_poll; /* system date before calling poll() */ -extern struct timeval after_poll; /* system date after leaving poll() */ +#define MAX_DELAY_MS 1000 +/* returns the lowest delay amongst and , and respects TIME_ETERNITY + */ +#define MINTIME(old, new) \ + (((new) < 0) ? (old) : (((old) < 0 || (new) < (old)) ? (new) : (old))) +#define SETNOW(a) (*a = now) + +extern uint32_t curr_sec_ms; /* millisecond of current second (0..999) */ +extern uint32_t + ms_left_scaled; /* milliseconds left for current second (0..2^32-1) */ +extern uint32_t + curr_sec_ms_scaled; /* millisecond of current second (0..2^32-1) */ +extern uint64_t now_ms; /* internal date in milliseconds (may wrap) */ +extern uint64_t now_us; /* internal date in us (may wrap) */ +extern uint32_t samp_time; /* total elapsed time over current sample */ +extern uint32_t idle_time; /* total idle time over current sample */ +extern uint32_t idle_pct; /* idle to total ratio over last sample (percent) */ +extern struct timeval + now; /* internal date is a monotonic function of real clock */ +extern struct timeval date; /* the real current date */ +extern struct timeval start_date; /* the process's start date */ +extern struct timeval before_poll; /* system date before calling poll() */ +extern struct timeval after_poll; /* system date after leaving poll() */ /**** exported functions *************************************************/ /* * adds ms to , set the result to and returns a pointer */ -REGPRM3 struct timeval *tv_ms_add(struct timeval *tv, const struct timeval *from, int ms); +REGPRM3 struct timeval *tv_ms_add(struct timeval *tv, + const struct timeval *from, int ms); /* - * compares and modulo 1ms: returns 0 if equal, -1 if tv1 < tv2, 1 if tv1 > tv2 - * Must not be used when either argument is eternity. Use tv_ms_cmp2() for that. + * compares and modulo 1ms: returns 0 if equal, -1 if tv1 < tv2, 1 + * if tv1 > tv2 Must not be used when either argument is eternity. Use + * tv_ms_cmp2() for that. */ REGPRM2 int tv_ms_cmp(const struct timeval *tv1, const struct timeval *tv2); /* - * compares and modulo 1 ms: returns 0 if equal, -1 if tv1 < tv2, 1 if tv1 > tv2, - * assuming that TV_ETERNITY is greater than everything. + * compares and modulo 1 ms: returns 0 if equal, -1 if tv1 < tv2, 1 + * if tv1 > tv2, assuming that TV_ETERNITY is greater than everything. */ REGPRM2 int tv_ms_cmp2(const struct timeval *tv1, const struct timeval *tv2); /**** general purpose functions and macros *******************************/ - /* tv_now: sets to the current time */ -REGPRM1 static inline struct timeval *tv_now(struct timeval *tv) -{ - gettimeofday(tv, NULL); - return tv; +REGPRM1 static inline struct timeval *tv_now(struct timeval *tv) { + gettimeofday(tv, NULL); + return tv; } /* tv_udpate_date: sets to system time, and sets to something as @@ -94,11 +105,10 @@ REGPRM2 void tv_update_date(int max_wait, int interrupted); * note that only tv_usec is necessary to detect it since a tv_usec > 999999 * is normally not possible. */ -REGPRM1 static inline struct timeval *tv_eternity(struct timeval *tv) -{ - tv->tv_sec = (typeof(tv->tv_sec))TV_ETERNITY; - tv->tv_usec = (typeof(tv->tv_usec))TV_ETERNITY; - return tv; +REGPRM1 static inline struct timeval *tv_eternity(struct timeval *tv) { + tv->tv_sec = (typeof(tv->tv_sec))TV_ETERNITY; + tv->tv_usec = (typeof(tv->tv_usec))TV_ETERNITY; + return tv; } /* @@ -106,45 +116,44 @@ REGPRM1 static inline struct timeval *tv_eternity(struct timeval *tv) * */ REGPRM1 static inline struct timeval *tv_zero(struct timeval *tv) { - tv->tv_sec = tv->tv_usec = 0; - return tv; + tv->tv_sec = tv->tv_usec = 0; + return tv; } /* * returns non null if tv is [eternity], otherwise 0. */ -#define tv_iseternity(tv) ((tv)->tv_usec == (typeof((tv)->tv_usec))TV_ETERNITY) +#define tv_iseternity(tv) ((tv)->tv_usec == (typeof((tv)->tv_usec))TV_ETERNITY) /* * returns 0 if tv is [eternity], otherwise non-zero. */ -#define tv_isset(tv) ((tv)->tv_usec != (typeof((tv)->tv_usec))TV_ETERNITY) +#define tv_isset(tv) ((tv)->tv_usec != (typeof((tv)->tv_usec))TV_ETERNITY) /* * returns non null if tv is [0], otherwise 0. */ -#define tv_iszero(tv) (((tv)->tv_sec | (tv)->tv_usec) == 0) +#define tv_iszero(tv) (((tv)->tv_sec | (tv)->tv_usec) == 0) /* * Converts a struct timeval to a number of milliseconds. */ -REGPRM1 static inline unsigned long __tv_to_ms(const struct timeval *tv) -{ - unsigned long ret; +REGPRM1 static inline unsigned long __tv_to_ms(const struct timeval *tv) { + unsigned long ret; - ret = tv->tv_sec * 1000; - ret += tv->tv_usec / 1000; - return ret; + ret = tv->tv_sec * 1000; + ret += tv->tv_usec / 1000; + return ret; } /* * Converts a struct timeval to a number of milliseconds. */ -REGPRM2 static inline struct timeval * __tv_from_ms(struct timeval *tv, unsigned long ms) -{ - tv->tv_sec = ms / 1000; - tv->tv_usec = (ms % 1000) * 1000; - return tv; +REGPRM2 static inline struct timeval *__tv_from_ms(struct timeval *tv, + unsigned long ms) { + tv->tv_sec = ms / 1000; + tv->tv_usec = (ms % 1000) * 1000; + return tv; } /* Return a number of 1024Hz ticks between 0 and 1023 for input number of @@ -154,173 +163,169 @@ REGPRM2 static inline struct timeval * __tv_from_ms(struct timeval *tv, unsigned * which is almost twice as low as a direct usec to ms conversion. This version * also has the benefit of returning 1024 for 1000000. */ -REGPRM1 static inline unsigned int __usec_to_1024th(unsigned int usec) -{ - return (usec * 1073 + 742516) >> 20; +REGPRM1 static inline unsigned int __usec_to_1024th(unsigned int usec) { + return (usec * 1073 + 742516) >> 20; } - /**** comparison functions and macros ***********************************/ - -/* tv_cmp: compares and : returns 0 if equal, -1 if tv1 < tv2, 1 if tv1 > tv2. */ -REGPRM2 static inline int __tv_cmp(const struct timeval *tv1, const struct timeval *tv2) -{ - if ((unsigned)tv1->tv_sec < (unsigned)tv2->tv_sec) - return -1; - else if ((unsigned)tv1->tv_sec > (unsigned)tv2->tv_sec) - return 1; - else if ((unsigned)tv1->tv_usec < (unsigned)tv2->tv_usec) - return -1; - else if ((unsigned)tv1->tv_usec > (unsigned)tv2->tv_usec) - return 1; - else - return 0; +/* tv_cmp: compares and : returns 0 if equal, -1 if tv1 < tv2, 1 if + * tv1 > tv2. */ +REGPRM2 static inline int __tv_cmp(const struct timeval *tv1, + const struct timeval *tv2) { + if ((unsigned)tv1->tv_sec < (unsigned)tv2->tv_sec) + return -1; + else if ((unsigned)tv1->tv_sec > (unsigned)tv2->tv_sec) + return 1; + else if ((unsigned)tv1->tv_usec < (unsigned)tv2->tv_usec) + return -1; + else if ((unsigned)tv1->tv_usec > (unsigned)tv2->tv_usec) + return 1; + else + return 0; } /* tv_iseq: compares and : returns 1 if tv1 == tv2, otherwise 0 */ #define tv_iseq __tv_iseq -REGPRM2 static inline int __tv_iseq(const struct timeval *tv1, const struct timeval *tv2) -{ - return ((unsigned)tv1->tv_sec == (unsigned)tv2->tv_sec) && - ((unsigned)tv1->tv_usec == (unsigned)tv2->tv_usec); +REGPRM2 static inline int __tv_iseq(const struct timeval *tv1, + const struct timeval *tv2) { + return ((unsigned)tv1->tv_sec == (unsigned)tv2->tv_sec) && + ((unsigned)tv1->tv_usec == (unsigned)tv2->tv_usec); } /* tv_isgt: compares and : returns 1 if tv1 > tv2, otherwise 0 */ #define tv_isgt _tv_isgt REGPRM2 int _tv_isgt(const struct timeval *tv1, const struct timeval *tv2); -REGPRM2 static inline int __tv_isgt(const struct timeval *tv1, const struct timeval *tv2) -{ - return - ((unsigned)tv1->tv_sec == (unsigned)tv2->tv_sec) ? - ((unsigned)tv1->tv_usec > (unsigned)tv2->tv_usec) : - ((unsigned)tv1->tv_sec > (unsigned)tv2->tv_sec); +REGPRM2 static inline int __tv_isgt(const struct timeval *tv1, + const struct timeval *tv2) { + return ((unsigned)tv1->tv_sec == (unsigned)tv2->tv_sec) + ? ((unsigned)tv1->tv_usec > (unsigned)tv2->tv_usec) + : ((unsigned)tv1->tv_sec > (unsigned)tv2->tv_sec); } /* tv_isge: compares and : returns 1 if tv1 >= tv2, otherwise 0 */ #define tv_isge __tv_isge -REGPRM2 static inline int __tv_isge(const struct timeval *tv1, const struct timeval *tv2) -{ - return - ((unsigned)tv1->tv_sec == (unsigned)tv2->tv_sec) ? - ((unsigned)tv1->tv_usec >= (unsigned)tv2->tv_usec) : - ((unsigned)tv1->tv_sec > (unsigned)tv2->tv_sec); +REGPRM2 static inline int __tv_isge(const struct timeval *tv1, + const struct timeval *tv2) { + return ((unsigned)tv1->tv_sec == (unsigned)tv2->tv_sec) + ? ((unsigned)tv1->tv_usec >= (unsigned)tv2->tv_usec) + : ((unsigned)tv1->tv_sec > (unsigned)tv2->tv_sec); } /* tv_islt: compares and : returns 1 if tv1 < tv2, otherwise 0 */ #define tv_islt __tv_islt -REGPRM2 static inline int __tv_islt(const struct timeval *tv1, const struct timeval *tv2) -{ - return - ((unsigned)tv1->tv_sec == (unsigned)tv2->tv_sec) ? - ((unsigned)tv1->tv_usec < (unsigned)tv2->tv_usec) : - ((unsigned)tv1->tv_sec < (unsigned)tv2->tv_sec); +REGPRM2 static inline int __tv_islt(const struct timeval *tv1, + const struct timeval *tv2) { + return ((unsigned)tv1->tv_sec == (unsigned)tv2->tv_sec) + ? ((unsigned)tv1->tv_usec < (unsigned)tv2->tv_usec) + : ((unsigned)tv1->tv_sec < (unsigned)tv2->tv_sec); } /* tv_isle: compares and : returns 1 if tv1 <= tv2, otherwise 0 */ #define tv_isle _tv_isle REGPRM2 int _tv_isle(const struct timeval *tv1, const struct timeval *tv2); -REGPRM2 static inline int __tv_isle(const struct timeval *tv1, const struct timeval *tv2) -{ - return - ((unsigned)tv1->tv_sec == (unsigned)tv2->tv_sec) ? - ((unsigned)tv1->tv_usec <= (unsigned)tv2->tv_usec) : - ((unsigned)tv1->tv_sec < (unsigned)tv2->tv_sec); +REGPRM2 static inline int __tv_isle(const struct timeval *tv1, + const struct timeval *tv2) { + return ((unsigned)tv1->tv_sec == (unsigned)tv2->tv_sec) + ? ((unsigned)tv1->tv_usec <= (unsigned)tv2->tv_usec) + : ((unsigned)tv1->tv_sec < (unsigned)tv2->tv_sec); } /* - * compares and modulo 1ms: returns 0 if equal, -1 if tv1 < tv2, 1 if tv1 > tv2 - * Must not be used when either argument is eternity. Use tv_ms_cmp2() for that. + * compares and modulo 1ms: returns 0 if equal, -1 if tv1 < tv2, 1 + * if tv1 > tv2 Must not be used when either argument is eternity. Use + * tv_ms_cmp2() for that. */ #define tv_ms_cmp _tv_ms_cmp REGPRM2 int _tv_ms_cmp(const struct timeval *tv1, const struct timeval *tv2); -REGPRM2 static inline int __tv_ms_cmp(const struct timeval *tv1, const struct timeval *tv2) -{ - if ((unsigned)tv1->tv_sec == (unsigned)tv2->tv_sec) { - if ((unsigned)tv2->tv_usec >= (unsigned)tv1->tv_usec + 1000) - return -1; - else if ((unsigned)tv1->tv_usec >= (unsigned)tv2->tv_usec + 1000) - return 1; - else - return 0; - } - else if (((unsigned)tv2->tv_sec > (unsigned)tv1->tv_sec + 1) || - (((unsigned)tv2->tv_sec == (unsigned)tv1->tv_sec + 1) && - ((unsigned)tv2->tv_usec + 1000000 >= (unsigned)tv1->tv_usec + 1000))) - return -1; - else if (((unsigned)tv1->tv_sec > (unsigned)tv2->tv_sec + 1) || - (((unsigned)tv1->tv_sec == (unsigned)tv2->tv_sec + 1) && - ((unsigned)tv1->tv_usec + 1000000 >= (unsigned)tv2->tv_usec + 1000))) - return 1; - else - return 0; +REGPRM2 static inline int __tv_ms_cmp(const struct timeval *tv1, + const struct timeval *tv2) { + if ((unsigned)tv1->tv_sec == (unsigned)tv2->tv_sec) { + if ((unsigned)tv2->tv_usec >= (unsigned)tv1->tv_usec + 1000) + return -1; + else if ((unsigned)tv1->tv_usec >= (unsigned)tv2->tv_usec + 1000) + return 1; + else + return 0; + } else if (((unsigned)tv2->tv_sec > (unsigned)tv1->tv_sec + 1) || + (((unsigned)tv2->tv_sec == (unsigned)tv1->tv_sec + 1) && + ((unsigned)tv2->tv_usec + 1000000 >= + (unsigned)tv1->tv_usec + 1000))) + return -1; + else if (((unsigned)tv1->tv_sec > (unsigned)tv2->tv_sec + 1) || + (((unsigned)tv1->tv_sec == (unsigned)tv2->tv_sec + 1) && + ((unsigned)tv1->tv_usec + 1000000 >= + (unsigned)tv2->tv_usec + 1000))) + return 1; + else + return 0; } /* - * compares and modulo 1 ms: returns 0 if equal, -1 if tv1 < tv2, 1 if tv1 > tv2, - * assuming that TV_ETERNITY is greater than everything. + * compares and modulo 1 ms: returns 0 if equal, -1 if tv1 < tv2, 1 + * if tv1 > tv2, assuming that TV_ETERNITY is greater than everything. */ #define tv_ms_cmp2 _tv_ms_cmp2 REGPRM2 int _tv_ms_cmp2(const struct timeval *tv1, const struct timeval *tv2); -REGPRM2 static inline int __tv_ms_cmp2(const struct timeval *tv1, const struct timeval *tv2) -{ - if (tv_iseternity(tv1)) - if (tv_iseternity(tv2)) - return 0; /* same */ - else - return 1; /* tv1 later than tv2 */ - else if (tv_iseternity(tv2)) - return -1; /* tv2 later than tv1 */ - return tv_ms_cmp(tv1, tv2); +REGPRM2 static inline int __tv_ms_cmp2(const struct timeval *tv1, + const struct timeval *tv2) { + if (tv_iseternity(tv1)) + if (tv_iseternity(tv2)) + return 0; /* same */ + else + return 1; /* tv1 later than tv2 */ + else if (tv_iseternity(tv2)) + return -1; /* tv2 later than tv1 */ + return tv_ms_cmp(tv1, tv2); } /* - * compares and modulo 1 ms: returns 1 if tv1 <= tv2, 0 if tv1 > tv2, - * assuming that TV_ETERNITY is greater than everything. Returns 0 if tv1 is - * TV_ETERNITY, and always assumes that tv2 != TV_ETERNITY. Designed to replace - * occurrences of (tv_ms_cmp2(tv,now) <= 0). + * compares and modulo 1 ms: returns 1 if tv1 <= tv2, 0 if tv1 > + * tv2, assuming that TV_ETERNITY is greater than everything. Returns 0 if tv1 + * is TV_ETERNITY, and always assumes that tv2 != TV_ETERNITY. Designed to + * replace occurrences of (tv_ms_cmp2(tv,now) <= 0). */ #define tv_ms_le2 _tv_ms_le2 REGPRM2 int _tv_ms_le2(const struct timeval *tv1, const struct timeval *tv2); -REGPRM2 static inline int __tv_ms_le2(const struct timeval *tv1, const struct timeval *tv2) -{ - if (likely((unsigned)tv1->tv_sec > (unsigned)tv2->tv_sec + 1)) - return 0; - - if (likely((unsigned)tv1->tv_sec < (unsigned)tv2->tv_sec)) - return 1; - - if (likely((unsigned)tv1->tv_sec == (unsigned)tv2->tv_sec)) { - if ((unsigned)tv2->tv_usec >= (unsigned)tv1->tv_usec + 1000) - return 1; - else - return 0; - } - - if (unlikely(((unsigned)tv1->tv_sec == (unsigned)tv2->tv_sec + 1) && - ((unsigned)tv1->tv_usec + 1000000 >= (unsigned)tv2->tv_usec + 1000))) - return 0; - else - return 1; +REGPRM2 static inline int __tv_ms_le2(const struct timeval *tv1, + const struct timeval *tv2) { + if (likely((unsigned)tv1->tv_sec > (unsigned)tv2->tv_sec + 1)) + return 0; + + if (likely((unsigned)tv1->tv_sec < (unsigned)tv2->tv_sec)) + return 1; + + if (likely((unsigned)tv1->tv_sec == (unsigned)tv2->tv_sec)) { + if ((unsigned)tv2->tv_usec >= (unsigned)tv1->tv_usec + 1000) + return 1; + else + return 0; + } + + if (unlikely( + ((unsigned)tv1->tv_sec == (unsigned)tv2->tv_sec + 1) && + ((unsigned)tv1->tv_usec + 1000000 >= (unsigned)tv2->tv_usec + 1000))) + return 0; + else + return 1; } - /**** operators **********************************************************/ - /* * Returns the time in ms elapsed between tv1 and tv2, assuming that tv1<=tv2. * Must not be used when either argument is eternity. */ #define tv_ms_elapsed __tv_ms_elapsed -REGPRM2 unsigned long _tv_ms_elapsed(const struct timeval *tv1, const struct timeval *tv2); -REGPRM2 static inline unsigned long __tv_ms_elapsed(const struct timeval *tv1, const struct timeval *tv2) -{ - unsigned long ret; - - ret = ((signed long)(tv2->tv_sec - tv1->tv_sec)) * 1000; - ret += ((signed long)(tv2->tv_usec - tv1->tv_usec)) / 1000; - return ret; +REGPRM2 unsigned long _tv_ms_elapsed(const struct timeval *tv1, + const struct timeval *tv2); +REGPRM2 static inline unsigned long __tv_ms_elapsed(const struct timeval *tv1, + const struct timeval *tv2) { + unsigned long ret; + + ret = ((signed long)(tv2->tv_sec - tv1->tv_sec)) * 1000; + ret += ((signed long)(tv2->tv_usec - tv1->tv_usec)) / 1000; + return ret; } /* @@ -330,13 +335,14 @@ REGPRM2 static inline unsigned long __tv_ms_elapsed(const struct timeval *tv1, c */ #define tv_ms_remain __tv_ms_remain -REGPRM2 unsigned long _tv_ms_remain(const struct timeval *tv1, const struct timeval *tv2); -REGPRM2 static inline unsigned long __tv_ms_remain(const struct timeval *tv1, const struct timeval *tv2) -{ - if (tv_ms_cmp(tv1, tv2) >= 0) - return 0; /* event elapsed */ - - return __tv_ms_elapsed(tv1, tv2); +REGPRM2 unsigned long _tv_ms_remain(const struct timeval *tv1, + const struct timeval *tv2); +REGPRM2 static inline unsigned long __tv_ms_remain(const struct timeval *tv1, + const struct timeval *tv2) { + if (tv_ms_cmp(tv1, tv2) >= 0) + return 0; /* event elapsed */ + + return __tv_ms_elapsed(tv1, tv2); } /* @@ -345,124 +351,131 @@ REGPRM2 static inline unsigned long __tv_ms_remain(const struct timeval *tv1, co * Returns TIME_ETERNITY if tv2 is eternity. */ #define tv_ms_remain2 _tv_ms_remain2 -REGPRM2 unsigned long _tv_ms_remain2(const struct timeval *tv1, const struct timeval *tv2); -REGPRM2 static inline unsigned long __tv_ms_remain2(const struct timeval *tv1, const struct timeval *tv2) -{ - if (tv_iseternity(tv2)) - return TIME_ETERNITY; - - return tv_ms_remain(tv1, tv2); +REGPRM2 unsigned long _tv_ms_remain2(const struct timeval *tv1, + const struct timeval *tv2); +REGPRM2 static inline unsigned long __tv_ms_remain2(const struct timeval *tv1, + const struct timeval *tv2) { + if (tv_iseternity(tv2)) + return TIME_ETERNITY; + + return tv_ms_remain(tv1, tv2); } /* * adds to , set the result to and returns a pointer */ #define tv_add _tv_add -REGPRM3 struct timeval *_tv_add(struct timeval *tv, const struct timeval *from, const struct timeval *inc); -REGPRM3 static inline struct timeval *__tv_add(struct timeval *tv, const struct timeval *from, const struct timeval *inc) -{ - tv->tv_usec = from->tv_usec + inc->tv_usec; - tv->tv_sec = from->tv_sec + inc->tv_sec; - if (tv->tv_usec >= 1000000) { - tv->tv_usec -= 1000000; - tv->tv_sec++; - } - return tv; +REGPRM3 struct timeval *_tv_add(struct timeval *tv, const struct timeval *from, + const struct timeval *inc); +REGPRM3 static inline struct timeval *__tv_add(struct timeval *tv, + const struct timeval *from, + const struct timeval *inc) { + tv->tv_usec = from->tv_usec + inc->tv_usec; + tv->tv_sec = from->tv_sec + inc->tv_sec; + if (tv->tv_usec >= 1000000) { + tv->tv_usec -= 1000000; + tv->tv_sec++; + } + return tv; } - /* * If is set, then add it to and set the result to , then * return 1, otherwise return 0. It is meant to be used in if conditions. */ #define tv_add_ifset _tv_add_ifset -REGPRM3 int _tv_add_ifset(struct timeval *tv, const struct timeval *from, const struct timeval *inc); -REGPRM3 static inline int __tv_add_ifset(struct timeval *tv, const struct timeval *from, const struct timeval *inc) -{ - if (tv_iseternity(inc)) - return 0; - tv->tv_usec = from->tv_usec + inc->tv_usec; - tv->tv_sec = from->tv_sec + inc->tv_sec; - if (tv->tv_usec >= 1000000) { - tv->tv_usec -= 1000000; - tv->tv_sec++; - } - return 1; +REGPRM3 int _tv_add_ifset(struct timeval *tv, const struct timeval *from, + const struct timeval *inc); +REGPRM3 static inline int __tv_add_ifset(struct timeval *tv, + const struct timeval *from, + const struct timeval *inc) { + if (tv_iseternity(inc)) + return 0; + tv->tv_usec = from->tv_usec + inc->tv_usec; + tv->tv_sec = from->tv_sec + inc->tv_sec; + if (tv->tv_usec >= 1000000) { + tv->tv_usec -= 1000000; + tv->tv_sec++; + } + return 1; } /* * adds to and returns a pointer */ -REGPRM2 static inline struct timeval *__tv_add2(struct timeval *tv, const struct timeval *inc) -{ - tv->tv_usec += inc->tv_usec; - tv->tv_sec += inc->tv_sec; - if (tv->tv_usec >= 1000000) { - tv->tv_usec -= 1000000; - tv->tv_sec++; - } - return tv; +REGPRM2 static inline struct timeval *__tv_add2(struct timeval *tv, + const struct timeval *inc) { + tv->tv_usec += inc->tv_usec; + tv->tv_sec += inc->tv_sec; + if (tv->tv_usec >= 1000000) { + tv->tv_usec -= 1000000; + tv->tv_sec++; + } + return tv; } - /* * Computes the remaining time between tv1=now and event=tv2. if tv2 is passed, * 0 is returned. The result is stored into tv. */ #define tv_remain _tv_remain -REGPRM3 struct timeval *_tv_remain(const struct timeval *tv1, const struct timeval *tv2, struct timeval *tv); -REGPRM3 static inline struct timeval *__tv_remain(const struct timeval *tv1, const struct timeval *tv2, struct timeval *tv) -{ - tv->tv_usec = tv2->tv_usec - tv1->tv_usec; - tv->tv_sec = tv2->tv_sec - tv1->tv_sec; - if ((signed)tv->tv_sec > 0) { - if ((signed)tv->tv_usec < 0) { - tv->tv_usec += 1000000; - tv->tv_sec--; - } - } else if (tv->tv_sec == 0) { - if ((signed)tv->tv_usec < 0) - tv->tv_usec = 0; - } else { - tv->tv_sec = 0; - tv->tv_usec = 0; - } - return tv; +REGPRM3 struct timeval *_tv_remain(const struct timeval *tv1, + const struct timeval *tv2, + struct timeval *tv); +REGPRM3 static inline struct timeval *__tv_remain(const struct timeval *tv1, + const struct timeval *tv2, + struct timeval *tv) { + tv->tv_usec = tv2->tv_usec - tv1->tv_usec; + tv->tv_sec = tv2->tv_sec - tv1->tv_sec; + if ((signed)tv->tv_sec > 0) { + if ((signed)tv->tv_usec < 0) { + tv->tv_usec += 1000000; + tv->tv_sec--; + } + } else if (tv->tv_sec == 0) { + if ((signed)tv->tv_usec < 0) + tv->tv_usec = 0; + } else { + tv->tv_sec = 0; + tv->tv_usec = 0; + } + return tv; } - /* * Computes the remaining time between tv1=now and event=tv2. if tv2 is passed, * 0 is returned. The result is stored into tv. Returns ETERNITY if tv2 is * eternity. */ #define tv_remain2 _tv_remain2 -REGPRM3 struct timeval *_tv_remain2(const struct timeval *tv1, const struct timeval *tv2, struct timeval *tv); -REGPRM3 static inline struct timeval *__tv_remain2(const struct timeval *tv1, const struct timeval *tv2, struct timeval *tv) -{ - if (tv_iseternity(tv2)) - return tv_eternity(tv); - return __tv_remain(tv1, tv2, tv); +REGPRM3 struct timeval *_tv_remain2(const struct timeval *tv1, + const struct timeval *tv2, + struct timeval *tv); +REGPRM3 static inline struct timeval *__tv_remain2(const struct timeval *tv1, + const struct timeval *tv2, + struct timeval *tv) { + if (tv_iseternity(tv2)) + return tv_eternity(tv); + return __tv_remain(tv1, tv2, tv); } - /* * adds ms to , set the result to and returns a pointer */ #define tv_ms_add _tv_ms_add -REGPRM3 struct timeval *_tv_ms_add(struct timeval *tv, const struct timeval *from, int ms); -REGPRM3 static inline struct timeval *__tv_ms_add(struct timeval *tv, const struct timeval *from, int ms) -{ - tv->tv_usec = from->tv_usec + (ms % 1000) * 1000; - tv->tv_sec = from->tv_sec + (ms / 1000); - while (tv->tv_usec >= 1000000) { - tv->tv_usec -= 1000000; - tv->tv_sec++; - } - return tv; +REGPRM3 struct timeval *_tv_ms_add(struct timeval *tv, + const struct timeval *from, int ms); +REGPRM3 static inline struct timeval * +__tv_ms_add(struct timeval *tv, const struct timeval *from, int ms) { + tv->tv_usec = from->tv_usec + (ms % 1000) * 1000; + tv->tv_sec = from->tv_sec + (ms / 1000); + while (tv->tv_usec >= 1000000) { + tv->tv_usec -= 1000000; + tv->tv_sec++; + } + return tv; } - /* * compares and : returns 1 if is before , otherwise 0. * This should be very fast because it's used in schedulers. @@ -470,10 +483,13 @@ REGPRM3 static inline struct timeval *__tv_ms_add(struct timeval *tv, const stru * as long as tv1<=tv2) */ -#define tv_isbefore(tv1, tv2) \ - (unlikely((unsigned)(tv1)->tv_sec < (unsigned)(tv2)->tv_sec) ? 1 : \ - (unlikely((unsigned)(tv1)->tv_sec > (unsigned)(tv2)->tv_sec) ? 0 : \ - unlikely((unsigned)(tv1)->tv_usec < (unsigned)(tv2)->tv_usec))) +#define tv_isbefore(tv1, tv2) \ + (unlikely((unsigned)(tv1)->tv_sec < (unsigned)(tv2)->tv_sec) \ + ? 1 \ + : (unlikely((unsigned)(tv1)->tv_sec > (unsigned)(tv2)->tv_sec) \ + ? 0 \ + : unlikely((unsigned)(tv1)->tv_usec < \ + (unsigned)(tv2)->tv_usec))) /* * returns the first event between and into . @@ -481,54 +497,55 @@ REGPRM3 static inline struct timeval *__tv_ms_add(struct timeval *tv, const stru * to be the same as or , it is recommended to use * tv_bound instead. */ -#define tv_min(tvmin, tv1, tv2) ({ \ - if (tv_isbefore(tv1, tv2)) { \ - *tvmin = *tv1; \ - } \ - else { \ - *tvmin = *tv2; \ - } \ - tvmin; \ -}) +#define tv_min(tvmin, tv1, tv2) \ + ({ \ + if (tv_isbefore(tv1, tv2)) { \ + *tvmin = *tv1; \ + } else { \ + *tvmin = *tv2; \ + } \ + tvmin; \ + }) /* * returns the first event between and into . * a zero tv is ignored. is returned. This function has been * optimized to be called as tv_min(a,a,b) or tv_min(b,a,b). */ -#define tv_bound(tv1, tv2) ({ \ - if (tv_isbefore(tv2, tv1)) \ - *tv1 = *tv2; \ - tv1; \ -}) +#define tv_bound(tv1, tv2) \ + ({ \ + if (tv_isbefore(tv2, tv1)) \ + *tv1 = *tv2; \ + tv1; \ + }) /* Update the idle time value twice a second, to be called after * tv_update_date() when called after poll(). It relies on to be * updated to the system time before calling poll(). */ -static inline void measure_idle() -{ - /* Let's compute the idle to work ratio. We worked between after_poll - * and before_poll, and slept between before_poll and date. The idle_pct - * is updated at most twice every second. Note that the current second - * rarely changes so we avoid a multiply when not needed. - */ - int delta; - - if ((delta = date.tv_sec - before_poll.tv_sec)) - delta *= 1000000; - idle_time += delta + (date.tv_usec - before_poll.tv_usec); - - if ((delta = date.tv_sec - after_poll.tv_sec)) - delta *= 1000000; - samp_time += delta + (date.tv_usec - after_poll.tv_usec); - - after_poll.tv_sec = date.tv_sec; after_poll.tv_usec = date.tv_usec; - if (samp_time < 500000) - return; - - idle_pct = (100 * idle_time + samp_time / 2) / samp_time; - idle_time = samp_time = 0; +static inline void measure_idle() { + /* Let's compute the idle to work ratio. We worked between after_poll + * and before_poll, and slept between before_poll and date. The idle_pct + * is updated at most twice every second. Note that the current second + * rarely changes so we avoid a multiply when not needed. + */ + int delta; + + if ((delta = date.tv_sec - before_poll.tv_sec)) + delta *= 1000000; + idle_time += delta + (date.tv_usec - before_poll.tv_usec); + + if ((delta = date.tv_sec - after_poll.tv_sec)) + delta *= 1000000; + samp_time += delta + (date.tv_usec - after_poll.tv_usec); + + after_poll.tv_sec = date.tv_sec; + after_poll.tv_usec = date.tv_usec; + if (samp_time < 500000) + return; + + idle_pct = (100 * idle_time + samp_time / 2) / samp_time; + idle_time = samp_time = 0; } #endif /* DA_TIME_H_ */ diff --git a/src/agent/da_top_percentile.c b/src/agent/da_top_percentile.c index abb5fd9..cefbd35 100644 --- a/src/agent/da_top_percentile.c +++ b/src/agent/da_top_percentile.c @@ -1,3 +1,18 @@ +/* +* Copyright [2021] JD.com, Inc. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ #include #include #include diff --git a/src/agent/da_top_percentile.h b/src/agent/da_top_percentile.h index 0d40dbf..4969a07 100644 --- a/src/agent/da_top_percentile.h +++ b/src/agent/da_top_percentile.h @@ -1,35 +1,41 @@ +/* + * Copyright [2021] JD.com, Inc. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ #ifndef DA_TOP_PERCENTILE_H_ #define DA_TOP_PERCENTILE_H_ - #include - struct context; struct server_pool; +enum E_REPORT_TYPE { RT_MIN, RT_SHARDING, RT_ALL, RT_MAX }; -enum E_REPORT_TYPE -{ - RT_MIN, - RT_SHARDING, - RT_ALL, - RT_MAX +struct remote_param { + uint64_t app_id; + uint64_t interface_id; }; - -struct remote_param -{ - uint64_t app_id; - uint64_t interface_id; -}; - - int8_t get_host_name_info(const char *addr, char *result); -int8_t set_remote_config(const char *addr, uint16_t port, struct sockaddr_in *remote_addr); -int8_t set_remote_param(uint64_t app_id, uint64_t interface_id, enum E_REPORT_TYPE type, struct remote_param *pParam); +int8_t set_remote_config(const char *addr, uint16_t port, + struct sockaddr_in *remote_addr); +int8_t set_remote_param(uint64_t app_id, uint64_t interface_id, + enum E_REPORT_TYPE type, struct remote_param *pParam); int set_remote_fd(); -void top_percentile_report(struct context* ctx, struct server_pool *pool, int64_t elaspe, int32_t status, enum E_REPORT_TYPE type); - +void top_percentile_report(struct context *ctx, struct server_pool *pool, + int64_t elaspe, int32_t status, + enum E_REPORT_TYPE type); #endif diff --git a/src/agent/da_util.c b/src/agent/da_util.c index 4f8a984..a1f3857 100644 --- a/src/agent/da_util.c +++ b/src/agent/da_util.c @@ -1,9 +1,18 @@ /* - * da_util.c - * - * Created on: 2014年11月29日 - * Author: Jiansong - */ +* Copyright [2021] JD.com, Inc. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ #include "da_util.h" #include "da_string.h" diff --git a/src/agent/da_util.h b/src/agent/da_util.h index 17d4498..e10359e 100644 --- a/src/agent/da_util.h +++ b/src/agent/da_util.h @@ -1,42 +1,49 @@ /* - * da_util.h + * Copyright [2021] JD.com, Inc. * - * Created on: 2014年11月29日 - * Author: Jiansong + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. */ #ifndef DA_UTIL_H_ #define DA_UTIL_H_ +#include +#include +#include +#include +#include #include #include +#include +#include #include #include -#include -#include -#include -#include -#include -#include #include -#include struct string; -#define LF (uint8_t) 10 -#define CR (uint8_t) 13 -#define CRLF "\x0d\x0a" - -#define NELEMS(a) ((sizeof(a)) / sizeof((a)[0])) +#define LF (uint8_t)10 +#define CR (uint8_t)13 +#define CRLF "\x0d\x0a" -#define MIN(a, b) ((a) < (b) ? (a) : (b)) -#define MAX(a, b) ((a) > (b) ? (a) : (b)) +#define NELEMS(a) ((sizeof(a)) / sizeof((a)[0])) +#define MIN(a, b) ((a) < (b) ? (a) : (b)) +#define MAX(a, b) ((a) > (b) ? (a) : (b)) #define DA_MAXHOSTNAMELEN 256 -#define DA_UNIX_ADDRSTRLEN \ - (sizeof(struct sockaddr_un) - offsetof(struct sockaddr_un, sun_path)) - +#define DA_UNIX_ADDRSTRLEN \ + (sizeof(struct sockaddr_un) - offsetof(struct sockaddr_un, sun_path)) /* * Length of 1 byte, 2 bytes, 4 bytes, 8 bytes and largest integral @@ -48,60 +55,52 @@ struct string; * # define UINT32_MAX (4294967295U) * # define UINT64_MAX (__UINT64_C(18446744073709551615)) */ -#define DA_UINT8_MAXLEN (3 + 1) -#define DA_UINT16_MAXLEN (5 + 1) -#define DA_UINT32_MAXLEN (10 + 1) -#define DA_UINT64_MAXLEN (20 + 1) -#define DA_UINTMAX_MAXLEN DA_UINT64_MAXLEN - - -#define da_sendn(_s, _b, _n) \ - _da_sendn(_s, _b, (size_t)(_n)) -#define da_recvn(_s, _b, _n) \ - _da_recvn(_s, _b, (size_t)(_n)) +#define DA_UINT8_MAXLEN (3 + 1) +#define DA_UINT16_MAXLEN (5 + 1) +#define DA_UINT32_MAXLEN (10 + 1) +#define DA_UINT64_MAXLEN (20 + 1) +#define DA_UINTMAX_MAXLEN DA_UINT64_MAXLEN +#define da_sendn(_s, _b, _n) _da_sendn(_s, _b, (size_t)(_n)) +#define da_recvn(_s, _b, _n) _da_recvn(_s, _b, (size_t)(_n)) -#define da_read(_d, _b, _n) \ - read(_d, _b, (size_t)(_n)) -#define da_readv(_d, _b, _n) \ - readv(_d, _b, (int)(_n)) -#define da_write(_d, _b, _n) \ - write(_d, _b, (size_t)(_n)) -#define da_writev(_d, _b, _n) \ - writev(_d, _b, (int)(_n)) +#define da_read(_d, _b, _n) read(_d, _b, (size_t)(_n)) +#define da_readv(_d, _b, _n) readv(_d, _b, (int)(_n)) +#define da_write(_d, _b, _n) write(_d, _b, (size_t)(_n)) +#define da_writev(_d, _b, _n) writev(_d, _b, (int)(_n)) ssize_t _da_sendn(int sd, const void *vptr, size_t n); ssize_t _da_recvn(int sd, void *vptr, size_t n); -#define da_gethostname(_name, _len) \ - gethostname((char *)_name, (size_t)(_len)) +#define da_gethostname(_name, _len) gethostname((char *)_name, (size_t)(_len)) -#define da_atoi(_line, _n) \ - _da_atoi((uint8_t *)_line, (size_t)(_n)) +#define da_atoi(_line, _n) _da_atoi((uint8_t *)_line, (size_t)(_n)) int _da_atoi(uint8_t *line, size_t n); #ifdef DA_ASSERT_PANIC -#define ASSERT(_x) do { \ - if (!(_x)) { \ - da_assert(#_x, __FILE__, __LINE__,1); \ - } \ -} while (0) +#define ASSERT(_x) \ + do { \ + if (!(_x)) { \ + da_assert(#_x, __FILE__, __LINE__, 1); \ + } \ + } while (0) #elif DA_ASSERT_LOG -#define ASSERT(_x) do { \ - if (!(_x)) { \ - da_assert(#_x, __FILE__, __LINE__,0); \ - } \ -} while (0) +#define ASSERT(_x) \ + do { \ + if (!(_x)) { \ + da_assert(#_x, __FILE__, __LINE__, 0); \ + } \ + } while (0) #else #define ASSERT(_x) #endif -void da_assert(const char * cond, const char *file, int line, int panic); +void da_assert(const char *cond, const char *file, int line, int panic); void da_stacktrace(int skip_count); void da_stacktrace_fd(int fd); @@ -126,13 +125,13 @@ bool da_valid_port(int n); */ struct sockinfo { - int family; /* socket address family */ - socklen_t addrlen; /* socket address length */ - union { - struct sockaddr_in in; /* ipv4 socket address */ - struct sockaddr_in6 in6; /* ipv6 socket address */ - struct sockaddr_un un; /* unix domain address */ - } addr; + int family; /* socket address family */ + socklen_t addrlen; /* socket address length */ + union { + struct sockaddr_in in; /* ipv4 socket address */ + struct sockaddr_in6 in6; /* ipv6 socket address */ + struct sockaddr_un un; /* unix domain address */ + } addr; }; int da_resolve(struct string *name, int port, struct sockinfo *si); diff --git a/src/agent/event/da_epoll.c b/src/agent/event/da_epoll.c index 7b323a3..702b7e0 100644 --- a/src/agent/event/da_epoll.c +++ b/src/agent/event/da_epoll.c @@ -1,9 +1,18 @@ /* - * da_epoll.c - * - * Created on: 2014年11月28日 - * Author: Jiansong - */ +* Copyright [2021] JD.com, Inc. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ #include #include diff --git a/src/agent/event/da_event.h b/src/agent/event/da_event.h index 3bda816..0edf27f 100644 --- a/src/agent/event/da_event.h +++ b/src/agent/event/da_event.h @@ -1,35 +1,44 @@ /* - * da_event.h + * Copyright [2021] JD.com, Inc. * - * Created on: 2014年11月30日 - * Author: Jiansong + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. */ #ifndef DA_EVENT_H_ #define DA_EVENT_H_ -#include -#include -#include "errno.h" #include "../da_conn.h" #include "../da_log.h" #include "../da_util.h" +#include "errno.h" +#include +#include -#define EVENT_SIZE 1024 +#define EVENT_SIZE 1024 -#define EVENT_READ 0x0000ff +#define EVENT_READ 0x0000ff #define EVENT_WRITE 0x00ff00 -#define EVENT_ERR 0xff0000 +#define EVENT_ERR 0xff0000 struct conn; typedef int (*event_cb_t)(void *, uint32_t); struct event_base { - int ep; - struct epoll_event *event; - int nevent; - event_cb_t cb; + int ep; + struct epoll_event *event; + int nevent; + event_cb_t cb; }; struct event_base *event_base_create(int nevent, event_cb_t cb); diff --git a/src/agent/hashkit/da_chash.c b/src/agent/hashkit/da_chash.c index a5fb82a..1179c2f 100644 --- a/src/agent/hashkit/da_chash.c +++ b/src/agent/hashkit/da_chash.c @@ -1,9 +1,18 @@ /* - * da_chash.c - * - * Created on: 2015年3月4日 - * Author: Jiansong - */ +* Copyright [2021] JD.com, Inc. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ #include "da_hashkit.h" diff --git a/src/agent/hashkit/da_crc16.c b/src/agent/hashkit/da_crc16.c index e896df3..3ea12cd 100644 --- a/src/agent/hashkit/da_crc16.c +++ b/src/agent/hashkit/da_crc16.c @@ -1,3 +1,18 @@ +/* +* Copyright [2021] JD.com, Inc. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ #include static const uint16_t crc16tab[256] = { diff --git a/src/agent/hashkit/da_crc32.c b/src/agent/hashkit/da_crc32.c index 0decc6e..bfe5ca1 100644 --- a/src/agent/hashkit/da_crc32.c +++ b/src/agent/hashkit/da_crc32.c @@ -1,9 +1,18 @@ /* - * The crc32 functions and data was originally written by Spencer - * Garrett and was gleaned from the PostgreSQL source - * tree via the files contrib/ltree/crc32.[ch] and from FreeBSD at - * src/usr.bin/cksum/crc32.c. - */ +* Copyright [2021] JD.com, Inc. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ #include diff --git a/src/agent/hashkit/da_fnv.c b/src/agent/hashkit/da_fnv.c index 490c1ac..de4bead 100644 --- a/src/agent/hashkit/da_fnv.c +++ b/src/agent/hashkit/da_fnv.c @@ -1,3 +1,18 @@ +/* +* Copyright [2021] JD.com, Inc. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ #include "da_hashkit.h" diff --git a/src/agent/hashkit/da_hsieh.c b/src/agent/hashkit/da_hsieh.c index a2e3c9d..86968c1 100644 --- a/src/agent/hashkit/da_hsieh.c +++ b/src/agent/hashkit/da_hsieh.c @@ -1,9 +1,17 @@ /* - * By Paul Hsieh (C) 2004, 2005. Covered under the Paul Hsieh - * derivative license. - * See: http://www.azillionmonkeys.com/qed/weblicense.html for license - * details. - * http://www.azillionmonkeys.com/qed/hash.html +* Copyright [2021] JD.com, Inc. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. */ #include"da_hashkit.h" diff --git a/src/agent/hashkit/da_jenkins.c b/src/agent/hashkit/da_jenkins.c index e630663..b9c770e 100644 --- a/src/agent/hashkit/da_jenkins.c +++ b/src/agent/hashkit/da_jenkins.c @@ -1,14 +1,18 @@ /* - * By Bob Jenkins, 2006. bob_jenkins@burtleburtle.net. You may use this - * code any way you wish, private, educational, or commercial. It's free. - * Use for hash table lookup, or anything where one collision in 2^^32 is - * acceptable. Do NOT use for cryptographic purposes. - * http://burtleburtle.net/bob/hash/index.html - * - * Modified by Brian Pontz for libmemcached - * TODO: - * Add big endian support - */ +* Copyright [2021] JD.com, Inc. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ #include"da_hashkit.h" diff --git a/src/agent/hashkit/da_ketama.c b/src/agent/hashkit/da_ketama.c index a67968c..55926ad 100644 --- a/src/agent/hashkit/da_ketama.c +++ b/src/agent/hashkit/da_ketama.c @@ -1,3 +1,18 @@ +/* +* Copyright [2021] JD.com, Inc. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ #include #include #include @@ -9,7 +24,7 @@ #include "../da_log.h" /* - * 一致性hash节点数 + * 涓鑷存ash鑺傜偣鏁 */ #define KETAMA_CONTINUUM_ADDITION 10 /* # extra slots to build into continuum */ #define KETAMA_POINTS_PER_SERVER 160 /* 40 points per hash */ @@ -68,7 +83,7 @@ int ketama_update(struct server_pool *pool) { if (continuum == NULL) { return -1; } - //设置pool的一致性hash环 + //璁剧疆pool鐨勪竴鑷存ash鐜 pool->continuum = continuum; pool->nserver_continuum = nserver_continuum; /* pool->ncontinuum is initialized later as it could be <= ncontinuum */ @@ -102,7 +117,7 @@ int ketama_update(struct server_pool *pool) { pointer_counter += points_per_server; } pool->ncontinuum = pointer_counter; - //对continum进行排序 + //瀵筩ontinum杩涜鎺掑簭 qsort(pool->continuum, pool->ncontinuum, sizeof(*pool->continuum), ketama_item_cmp); for (pointer_index = 0; @@ -142,7 +157,7 @@ int ketama_update(struct server_pool *pool) { for (server_index = 0; server_index < nserver; server_index++) { struct server *server = array_get(&pool->server, server_index); - //‘host:port:weight’ or ‘host:port:weight name’ + //鈥榟ost:port:weight鈥 or 鈥榟ost:port:weight name鈥 ASSERT(server->weight > 0); total_weight += server->weight; @@ -163,7 +178,7 @@ int ketama_update(struct server_pool *pool) { if (continuum == NULL) { return -1; } - //设置pool的一致性hash环 + //璁剧疆pool鐨勪竴鑷存ash鐜 pool->continuum = continuum; pool->nserver_continuum = nserver_continuum; /* pool->ncontinuum is initialized later as it could be <= ncontinuum */ @@ -181,7 +196,7 @@ int ketama_update(struct server_pool *pool) { server = array_get(&pool->server, server_index); - pct = (float) server->weight / (float) total_weight; //权重计算 + pct = (float) server->weight / (float) total_weight; //鏉冮噸璁$畻 pointer_per_server = (uint32_t) ((floorf( (float) (pct * KETAMA_POINTS_PER_SERVER / 4 * (float) nserver + 0.0000000001))) * 4); @@ -208,7 +223,7 @@ int ketama_update(struct server_pool *pool) { } pool->ncontinuum = pointer_counter; - //对continum进行排序 + //瀵筩ontinum杩涜鎺掑簭 qsort(pool->continuum, pool->ncontinuum, sizeof(*pool->continuum), ketama_item_cmp); diff --git a/src/agent/hashkit/da_md5.c b/src/agent/hashkit/da_md5.c index 0035bcb..d75a372 100644 --- a/src/agent/hashkit/da_md5.c +++ b/src/agent/hashkit/da_md5.c @@ -1,14 +1,19 @@ -#include "da_hashkit.h" - /* - * This is an OpenSSL-compatible implementation of the RSA Data Security, Inc. - * MD5 Message-Digest Algorithm (RFC 1321). - * - * Homepage: http://openwall.info/wiki/people/solar/software/public-domain-source-code/md5 - * - * Author: Alexander Peslyak, better known as Solar Designer - */ - +* Copyright [2021] JD.com, Inc. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ +#include "da_hashkit.h" #include typedef unsigned int MD5_u32plus; diff --git a/src/agent/hashkit/da_murmur.c b/src/agent/hashkit/da_murmur.c index b57d0ce..9c1b241 100644 --- a/src/agent/hashkit/da_murmur.c +++ b/src/agent/hashkit/da_murmur.c @@ -1,19 +1,18 @@ /* - * "Murmur" hash provided by Austin, tanjent@gmail.com - * http://murmurhash.googlepages.com/ - * - * Note - This code makes a few assumptions about how your machine behaves - - * - * 1. We can read a 4-byte value from any address without crashing - * 2. sizeof(int) == 4 - * - * And it has a few limitations - - * 1. It will not work incrementally. - * 2. It will not produce the same results on little-endian and big-endian - * machines. - * - * Updated to murmur2 hash - BP - */ +* Copyright [2021] JD.com, Inc. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ #include "da_hashkit.h" diff --git a/src/agent/hashkit/da_one_at_a_time.c b/src/agent/hashkit/da_one_at_a_time.c index 46b4d5c..7ff52ea 100644 --- a/src/agent/hashkit/da_one_at_a_time.c +++ b/src/agent/hashkit/da_one_at_a_time.c @@ -1,17 +1,18 @@ /* - * HashKit - * Copyright (C) 2009 Brian Aker - * All rights reserved. - * - * Use and distribution licensed under the BSD license. See - * the COPYING file in the parent directory for full text. - */ - -/* - * This has is Jenkin's "One at A time Hash". - * http://en.wikipedia.org/wiki/Jenkins_hash_function - */ - +* Copyright [2021] JD.com, Inc. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ #include "da_hashkit.h" uint32_t diff --git a/src/agent/proto/da_dtcparse.c b/src/agent/proto/da_dtcparse.c index 6ef3399..b6da7da 100644 --- a/src/agent/proto/da_dtcparse.c +++ b/src/agent/proto/da_dtcparse.c @@ -1,10 +1,18 @@ /* - * da_dtcparse.c - * - * Created on: 2014年12月24日 - * Author: Jiansong - */ - +* Copyright [2021] JD.com, Inc. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ #include #include #include @@ -408,7 +416,7 @@ void dtc_parse_req(struct msg *r) { state = ST_ID; break; } - //级联灰度版本特别的地方 + //绾ц仈鐏板害鐗堟湰鐗瑰埆鐨勫湴鏂 // case 17: { // if (b->last - p < r->cur_parse_lenth) { // log_debug( @@ -563,7 +571,7 @@ void dtc_parse_req(struct msg *r) { r->keys[0].end = p + r->cur_parse_lenth; if (r->keytype == String || r->keytype == Binary) { - r->keys[0].end -= 1; //去除字符串末尾的\0 + r->keys[0].end -= 1; //鍘婚櫎瀛楃涓叉湯灏剧殑\0 } log_debug( @@ -1720,7 +1728,7 @@ static int dtc_encode_agentid(struct msg *r) { } /* - * get分包函数,暂时不考虑联合组件的情况 + * get鍒嗗寘鍑芥暟,鏆傛椂涓嶈冭檻鑱斿悎缁勪欢鐨勬儏鍐 */ static int dtc_fragment_get(struct msg *r, uint32_t ncontinuum, struct msg_tqh *frag_msgq) { @@ -1733,7 +1741,7 @@ static int dtc_fragment_get(struct msg *r, uint32_t ncontinuum, CValue val; uint32_t idx = 0; int version_len = 0, requestinfo_len = 0; - //用于放置分组所有的key + //鐢ㄤ簬鏀剧疆鍒嗙粍鎵鏈夌殑key struct keypos keys[ncontinuum][r->keyCount]; int keynum[ncontinuum]; @@ -1746,7 +1754,7 @@ static int dtc_fragment_get(struct msg *r, uint32_t ncontinuum, struct keypos *kpos = &r->keys[i]; if(r->keytype == Unsigned || r->keytype == Signed) { if(sizeof(uint64_t) == kpos->end - kpos->start) { - //此处没有考虑大端小端问题源于SDK编码 + //姝ゅ娌℃湁鑰冭檻澶х灏忕闂婧愪簬SDK缂栫爜 val.u64 = *(uint64_t*)kpos->start; idx = msg_backend_idx(r, (uint8_t *)&val.u64, sizeof(uint64_t)); log_debug("key is %lu, idx is %u, len is %ld", @@ -1756,7 +1764,7 @@ static int dtc_fragment_get(struct msg *r, uint32_t ncontinuum, return -1; } else { - //多Key时每个key被附加4Byte Key长度 + //澶欿ey鏃舵瘡涓猭ey琚檮鍔4Byte Key闀垮害 int len = kpos->end - kpos->start - sizeof(uint32_t); if(len > 0) { char temp[len + 2]; @@ -1936,7 +1944,7 @@ int dtc_fragment(struct msg *r, uint32_t ncontinuum, struct msg_tqh *frag_msgq) { uint64_t randomkey=randomHashSeed++; r->idx = msg_backend_idx(r, (uint8_t *)&randomkey,sizeof(uint64_t)); - //级联灰度版本特别的地方 + //绾ц仈鐏板害鐗堟湰鐗瑰埆鐨勫湴鏂 status = dtc_encode_agentid(r); return status; } @@ -1989,7 +1997,7 @@ int dtc_fragment(struct msg *r, uint32_t ncontinuum, struct msg_tqh *frag_msgq) break; } } - //级联灰度版本特别的地方 + //绾ц仈鐏板害鐗堟湰鐗瑰埆鐨勫湴鏂 status = dtc_encode_agentid(r); return status; } @@ -2108,7 +2116,7 @@ static int dtc_coalesce_get(struct msg *r) { } } } - if (errortag == 1) { //有分包查找出错,one of the search result is in error + if (errortag == 1) { //鏈夊垎鍖呮煡鎵惧嚭閿,one of the search result is in error cmsg->peer = NULL; cmsg->peerid = 0; peermsg->peer = NULL; @@ -2118,7 +2126,7 @@ static int dtc_coalesce_get(struct msg *r) { peermsg->peer = r; peermsg->id = r->id; goto coalesce_succ_err; - } else if (validmsgcount == 0 || validmsgcount == 1) { //结果集统统处于一个包中,only a package contain search resule + } else if (validmsgcount == 0 || validmsgcount == 1) { //缁撴灉闆嗙粺缁熷浜庝竴涓寘涓紝only a package contain search resule peermsg = singleresmsg->peer; singleresmsg->peer = NULL; singleresmsg->peerid = 0; diff --git a/src/agent/proto/da_protocal.h b/src/agent/proto/da_protocal.h index b993f78..e94d3dd 100644 --- a/src/agent/proto/da_protocal.h +++ b/src/agent/proto/da_protocal.h @@ -1,41 +1,49 @@ /* - * da_protocal.h + * Copyright [2021] JD.com, Inc. * - * Created on: 2014年12月23日 - * Author: Jiansong + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. */ #ifndef DA_PROTOCAL_H_ #define DA_PROTOCAL_H_ -#include -#include #include "da_string.h" +#include +#include struct msg; struct msg_tqh; struct CPacketHeader { - uint8_t version; - uint8_t scts; - uint8_t flags; - uint8_t cmd; - uint32_t len[8]; + uint8_t version; + uint8_t scts; + uint8_t flags; + uint8_t cmd; + uint32_t len[8]; }; typedef union CValue { -// member - int64_t s64; - uint64_t u64; - double flt; - struct string str; + // member + int64_t s64; + uint64_t u64; + double flt; + struct string str; -}CValue; +} CValue; void dtc_parse_req(struct msg *r); void dtc_parse_rsp(struct msg *r); int dtc_coalesce(struct msg *r); -int dtc_fragment(struct msg *r,uint32_t ncontinuum,struct msg_tqh *frag_msgq); -int dtc_error_reply(struct msg *smsg,struct msg *dmsg); - +int dtc_fragment(struct msg *r, uint32_t ncontinuum, struct msg_tqh *frag_msgq); +int dtc_error_reply(struct msg *smsg, struct msg *dmsg); #endif /* DA_PROTOCAL_H_ */ diff --git a/src/libs/common/version.h b/src/libs/common/version.h index eb36e5c..c2bb91e 100644 --- a/src/libs/common/version.h +++ b/src/libs/common/version.h @@ -8,7 +8,7 @@ * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software -* distributed under the License is distributed on an "08568c8" BASIS, +* distributed under the License is distributed on an "eab3ec6" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. -- Gitee