Skip to content

Commit

Permalink
FEATURE: Add stats_tcp_retrans()
Browse files Browse the repository at this point in the history
  • Loading branch information
uhm0311 committed May 3, 2024
1 parent a5815d0 commit 624d1f3
Show file tree
Hide file tree
Showing 6 changed files with 185 additions and 1 deletion.
2 changes: 2 additions & 0 deletions Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,8 @@ memcached_SOURCES = \
sasl_defs.h \
stats_prefix.c \
stats_prefix.h \
stats.c \
stats.h \
thread.c \
thread.h \
mc_util.c \
Expand Down
5 changes: 5 additions & 0 deletions memcached.c
Original file line number Diff line number Diff line change
Expand Up @@ -8089,6 +8089,11 @@ static void server_stats(ADD_STAT add_stats, conn *c, bool aggregate)
APPEND_STAT("limit_maxconns", "%d", settings.maxconns);
APPEND_STAT("threads", "%d", settings.num_threads);
APPEND_STAT("conn_yields", "%"PRIu64, thread_stats.conn_yields);
#ifdef STATS_TCP_RETRANS
if (IS_TCP(c->transport)) {
APPEND_STAT("tcp_retrans", "%ld", stats_tcp_retrans());
}
#endif
UNLOCK_STATS();
}

Expand Down
5 changes: 5 additions & 0 deletions memcached.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
#include "lqdetect.h"
#include "engine_loader.h"
#include "sasl_defs.h"
#include "stats.h"

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

#ifdef STATS_TCP_RETRANS
#define IS_TCP(x) (x == tcp_transport)
#endif

#define IS_UDP(x) (x == udp_transport)

/**
Expand Down
142 changes: 142 additions & 0 deletions stats.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,142 @@
/* -*- Mode: C; tab-width: 4; c-basic-offset: 4; indent-tabs-mode: nil -*- */
/*
* arcus-memcached - Arcus memory cache server
* Copyright 2010-2014 NAVER Corp.
* Copyright 2015 JaM2in Co., Ltd.
*
* 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.
*/
/*
* Detailed statistics management. For simple stats like total number of
* "get" requests, we use inline code in memcached.c and friends, but when
* stats detail mode is activated, the code here records more information.
*
* Author:
* Steven Grimm <[email protected]>
*/
#include "stats.h"

#include <ctype.h>
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#ifdef STATS_TCP_RETRANS

static void rtrim(char **src) {
if (src == NULL || *src == NULL || strlen(*src) == 0) {
return;
}

char *str = *src;
for (int i = strlen(str) - 1; i >= 0; i--) {
if (!isspace(str[i])) {
break;
}

str[i] = '\0';
}

*src = str;
}

int64_t stats_tcp_retrans(void) {
char *key_line = NULL;
char *value_line = NULL;
int64_t tcp_retrans = -1;

FILE *fp = fopen("/proc/net/snmp", "r");
if (fp == NULL) {
goto done;
}

size_t key_len = 0;
size_t value_len = 0;
ssize_t key_read = -1;
ssize_t value_read = -1;

char *prefix = "Tcp:";
size_t prefix_len = strlen(prefix);

while ((key_read = getline(&key_line, &key_len, fp)) != -1) {
if (strncmp(prefix, key_line, prefix_len) != 0) {
continue;
}

if ((value_read = getline(&value_line, &value_len, fp)) != -1) {
break;
}
}

if (key_read < 0 || value_read < 0) {
goto done;
}

/*
* below lines are examples of key_line and value_line.
* for examples below, tcp_retrans = 9814784
*
* Tcp: RtoAlgorithm RtoMin RtoMax MaxConn ActiveOpens PassiveOpens AttemptFails EstabResets CurrEstab InSegs OutSegs RetransSegs InErrs OutRsts InCsumErrors
* Tcp: 1 200 120000 -1 231273424 45758374 43975115 12045288 360 9853401239 10104984649 9814784 12765 17318799 883
*/

char *delimiter = " ";
char *retrans_segs = "RetransSegs";

char *key_next = NULL;
char *value_next = NULL;

char *key_token = strtok_r(key_line, delimiter, &key_next);
char *value_token = strtok_r(value_line, delimiter, &value_next);

// first token is always "Tcp:"
key_token = strtok_r(NULL, delimiter, &key_next);
value_token = strtok_r(NULL, delimiter, &value_next);

while (key_token && value_token) {
// last token always ends with "\n"
rtrim(&key_token);
rtrim(&value_token);

if (strcmp(key_token, retrans_segs) == 0) {
char *end = NULL;
errno = 0;
tcp_retrans = strtoll(value_token, &end, 10);

if (errno > 0 || *end != '\0') {
tcp_retrans = -1;
}
goto done;
}

key_token = strtok_r(NULL, delimiter, &key_next);
value_token = strtok_r(NULL, delimiter, &value_next);
}

done:
if (fp != NULL) {
fclose(fp);
}

if (key_line != NULL) {
free(key_line);
}

if (value_line != NULL) {
free(value_line);
}

return tcp_retrans;
}
#endif
30 changes: 30 additions & 0 deletions stats.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/*
* arcus-memcached - Arcus memory cache server
* Copyright 2010-2014 NAVER Corp.
* Copyright 2015 JaM2in Co., Ltd.
*
* 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.
*/
/* stats */
#ifndef STATS_H
#define STATS_H

#include <stdint.h>

#define STATS_TCP_RETRANS

#ifdef STATS_TCP_RETRANS
int64_t stats_tcp_retrans(void);
#endif

#endif
2 changes: 1 addition & 1 deletion stats_prefix.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/* stats */
/* stats_prefix */
void stats_prefix_init(char delimiter, void (*cb_when_prefix_overflow)(void));
void stats_prefix_clear(void);
int stats_prefix_count(void);
Expand Down

0 comments on commit 624d1f3

Please sign in to comment.