Skip to content

Commit 624d1f3

Browse files
committed
FEATURE: Add stats_tcp_retrans()
1 parent a5815d0 commit 624d1f3

File tree

6 files changed

+185
-1
lines changed

6 files changed

+185
-1
lines changed

Makefile.am

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,8 @@ memcached_SOURCES = \
8181
sasl_defs.h \
8282
stats_prefix.c \
8383
stats_prefix.h \
84+
stats.c \
85+
stats.h \
8486
thread.c \
8587
thread.h \
8688
mc_util.c \

memcached.c

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8089,6 +8089,11 @@ static void server_stats(ADD_STAT add_stats, conn *c, bool aggregate)
80898089
APPEND_STAT("limit_maxconns", "%d", settings.maxconns);
80908090
APPEND_STAT("threads", "%d", settings.num_threads);
80918091
APPEND_STAT("conn_yields", "%"PRIu64, thread_stats.conn_yields);
8092+
#ifdef STATS_TCP_RETRANS
8093+
if (IS_TCP(c->transport)) {
8094+
APPEND_STAT("tcp_retrans", "%ld", stats_tcp_retrans());
8095+
}
8096+
#endif
80928097
UNLOCK_STATS();
80938098
}
80948099

memcached.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
#include "lqdetect.h"
3636
#include "engine_loader.h"
3737
#include "sasl_defs.h"
38+
#include "stats.h"
3839

3940
/* This is the address we use for admin purposes. For example, doing stats
4041
* and heart beats from arcus_zk.
@@ -190,6 +191,10 @@ enum network_transport {
190191
udp_transport
191192
};
192193

194+
#ifdef STATS_TCP_RETRANS
195+
#define IS_TCP(x) (x == tcp_transport)
196+
#endif
197+
193198
#define IS_UDP(x) (x == udp_transport)
194199

195200
/**

stats.c

Lines changed: 142 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,142 @@
1+
/* -*- Mode: C; tab-width: 4; c-basic-offset: 4; indent-tabs-mode: nil -*- */
2+
/*
3+
* arcus-memcached - Arcus memory cache server
4+
* Copyright 2010-2014 NAVER Corp.
5+
* Copyright 2015 JaM2in Co., Ltd.
6+
*
7+
* Licensed under the Apache License, Version 2.0 (the "License");
8+
* you may not use this file except in compliance with the License.
9+
* You may obtain a copy of the License at
10+
*
11+
* http://www.apache.org/licenses/LICENSE-2.0
12+
*
13+
* Unless required by applicable law or agreed to in writing, software
14+
* distributed under the License is distributed on an "AS IS" BASIS,
15+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16+
* See the License for the specific language governing permissions and
17+
* limitations under the License.
18+
*/
19+
/*
20+
* Detailed statistics management. For simple stats like total number of
21+
* "get" requests, we use inline code in memcached.c and friends, but when
22+
* stats detail mode is activated, the code here records more information.
23+
*
24+
* Author:
25+
* Steven Grimm <[email protected]>
26+
*/
27+
#include "stats.h"
28+
29+
#include <ctype.h>
30+
#include <errno.h>
31+
#include <stdio.h>
32+
#include <stdlib.h>
33+
#include <string.h>
34+
35+
#ifdef STATS_TCP_RETRANS
36+
37+
static void rtrim(char **src) {
38+
if (src == NULL || *src == NULL || strlen(*src) == 0) {
39+
return;
40+
}
41+
42+
char *str = *src;
43+
for (int i = strlen(str) - 1; i >= 0; i--) {
44+
if (!isspace(str[i])) {
45+
break;
46+
}
47+
48+
str[i] = '\0';
49+
}
50+
51+
*src = str;
52+
}
53+
54+
int64_t stats_tcp_retrans(void) {
55+
char *key_line = NULL;
56+
char *value_line = NULL;
57+
int64_t tcp_retrans = -1;
58+
59+
FILE *fp = fopen("/proc/net/snmp", "r");
60+
if (fp == NULL) {
61+
goto done;
62+
}
63+
64+
size_t key_len = 0;
65+
size_t value_len = 0;
66+
ssize_t key_read = -1;
67+
ssize_t value_read = -1;
68+
69+
char *prefix = "Tcp:";
70+
size_t prefix_len = strlen(prefix);
71+
72+
while ((key_read = getline(&key_line, &key_len, fp)) != -1) {
73+
if (strncmp(prefix, key_line, prefix_len) != 0) {
74+
continue;
75+
}
76+
77+
if ((value_read = getline(&value_line, &value_len, fp)) != -1) {
78+
break;
79+
}
80+
}
81+
82+
if (key_read < 0 || value_read < 0) {
83+
goto done;
84+
}
85+
86+
/*
87+
* below lines are examples of key_line and value_line.
88+
* for examples below, tcp_retrans = 9814784
89+
*
90+
* Tcp: RtoAlgorithm RtoMin RtoMax MaxConn ActiveOpens PassiveOpens AttemptFails EstabResets CurrEstab InSegs OutSegs RetransSegs InErrs OutRsts InCsumErrors
91+
* Tcp: 1 200 120000 -1 231273424 45758374 43975115 12045288 360 9853401239 10104984649 9814784 12765 17318799 883
92+
*/
93+
94+
char *delimiter = " ";
95+
char *retrans_segs = "RetransSegs";
96+
97+
char *key_next = NULL;
98+
char *value_next = NULL;
99+
100+
char *key_token = strtok_r(key_line, delimiter, &key_next);
101+
char *value_token = strtok_r(value_line, delimiter, &value_next);
102+
103+
// first token is always "Tcp:"
104+
key_token = strtok_r(NULL, delimiter, &key_next);
105+
value_token = strtok_r(NULL, delimiter, &value_next);
106+
107+
while (key_token && value_token) {
108+
// last token always ends with "\n"
109+
rtrim(&key_token);
110+
rtrim(&value_token);
111+
112+
if (strcmp(key_token, retrans_segs) == 0) {
113+
char *end = NULL;
114+
errno = 0;
115+
tcp_retrans = strtoll(value_token, &end, 10);
116+
117+
if (errno > 0 || *end != '\0') {
118+
tcp_retrans = -1;
119+
}
120+
goto done;
121+
}
122+
123+
key_token = strtok_r(NULL, delimiter, &key_next);
124+
value_token = strtok_r(NULL, delimiter, &value_next);
125+
}
126+
127+
done:
128+
if (fp != NULL) {
129+
fclose(fp);
130+
}
131+
132+
if (key_line != NULL) {
133+
free(key_line);
134+
}
135+
136+
if (value_line != NULL) {
137+
free(value_line);
138+
}
139+
140+
return tcp_retrans;
141+
}
142+
#endif

stats.h

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
/*
2+
* arcus-memcached - Arcus memory cache server
3+
* Copyright 2010-2014 NAVER Corp.
4+
* Copyright 2015 JaM2in Co., Ltd.
5+
*
6+
* Licensed under the Apache License, Version 2.0 (the "License");
7+
* you may not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing, software
13+
* distributed under the License is distributed on an "AS IS" BASIS,
14+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
* See the License for the specific language governing permissions and
16+
* limitations under the License.
17+
*/
18+
/* stats */
19+
#ifndef STATS_H
20+
#define STATS_H
21+
22+
#include <stdint.h>
23+
24+
#define STATS_TCP_RETRANS
25+
26+
#ifdef STATS_TCP_RETRANS
27+
int64_t stats_tcp_retrans(void);
28+
#endif
29+
30+
#endif

stats_prefix.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
* See the License for the specific language governing permissions and
1616
* limitations under the License.
1717
*/
18-
/* stats */
18+
/* stats_prefix */
1919
void stats_prefix_init(char delimiter, void (*cb_when_prefix_overflow)(void));
2020
void stats_prefix_clear(void);
2121
int stats_prefix_count(void);

0 commit comments

Comments
 (0)