raindrops.git  about / heads / tags
real-time stats for preforking Rack servers
blob 83001a5831bb8c6ff64ff5ac4cab43ea1dca90ad 5006 bytes (raw)
$ git show aggregate-pmq:ext/raindrops/linux_tcp_info.c	# shows this blob on the CLI

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
 
#if defined(__linux__) && defined(HAVE_LINUX_TCP_H)
#include <ruby.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <linux/tcp.h>
#ifdef TCP_INFO
#include "my_fileno.h"

#define TCPI_ATTR_READER(x) \
static VALUE tcp_info_##x(VALUE self) \
{ \
	struct tcp_info *info = DATA_PTR(self); \
	return UINT2NUM((uint32_t)info->tcpi_##x); \
}

TCPI_ATTR_READER(state)
TCPI_ATTR_READER(ca_state)
TCPI_ATTR_READER(retransmits)
TCPI_ATTR_READER(probes)
TCPI_ATTR_READER(backoff)
TCPI_ATTR_READER(options)
TCPI_ATTR_READER(snd_wscale)
TCPI_ATTR_READER(rcv_wscale)
TCPI_ATTR_READER(rto)
TCPI_ATTR_READER(ato)
TCPI_ATTR_READER(snd_mss)
TCPI_ATTR_READER(rcv_mss)
TCPI_ATTR_READER(unacked)
TCPI_ATTR_READER(sacked)
TCPI_ATTR_READER(lost)
TCPI_ATTR_READER(retrans)
TCPI_ATTR_READER(fackets)
TCPI_ATTR_READER(last_data_sent)
TCPI_ATTR_READER(last_ack_sent)
TCPI_ATTR_READER(last_data_recv)
TCPI_ATTR_READER(last_ack_recv)
TCPI_ATTR_READER(pmtu)
TCPI_ATTR_READER(rcv_ssthresh)
TCPI_ATTR_READER(rtt)
TCPI_ATTR_READER(rttvar)
TCPI_ATTR_READER(snd_ssthresh)
TCPI_ATTR_READER(snd_cwnd)
TCPI_ATTR_READER(advmss)
TCPI_ATTR_READER(reordering)
TCPI_ATTR_READER(rcv_rtt)
TCPI_ATTR_READER(rcv_space)
TCPI_ATTR_READER(total_retrans)

static size_t tcpi_memsize(const void *ptr)
{
	return sizeof(struct tcp_info);
}

static const rb_data_type_t tcpi_type = {
	"tcp_info",
	{ NULL, RUBY_TYPED_DEFAULT_FREE, tcpi_memsize, /* reserved */ },
	/* parent, data, [ flags ] */
};

static VALUE alloc(VALUE klass)
{
	struct tcp_info *info;

	return TypedData_Make_Struct(klass, struct tcp_info, &tcpi_type, info);
}

/*
 * call-seq:
 *
 *	Raindrops::TCP_Info.new(tcp_socket)	-> TCP_Info object
 *
 * Reads a TCP_Info object from any given +tcp_socket+.  See the tcp(7)
 * manpage and /usr/include/linux/tcp.h for more details.
 */
static VALUE init(VALUE self, VALUE io)
{
	int fd = my_fileno(io);
	struct tcp_info *info = DATA_PTR(self);
	socklen_t len = (socklen_t)sizeof(struct tcp_info);
	int rc = getsockopt(fd, IPPROTO_TCP, TCP_INFO, info, &len);

	if (rc != 0)
		rb_sys_fail("getsockopt");

	return self;
}

void Init_raindrops_linux_tcp_info(void)
{
	VALUE cRaindrops = rb_define_class("Raindrops", rb_cObject);
	VALUE cTCP_Info;

	/*
	 * Document-class: Raindrops::TCP_Info
	 *
	 * This is used to wrap "struct tcp_info" as described in tcp(7)
	 * and /usr/include/linux/tcp.h.  The following readers methods
	 * are defined corresponding to the "tcpi_" fields in the
	 * tcp_info struct.
	 *
	 * In particular, the +last_data_recv+ field is useful for measuring
	 * the amount of time a client spent in the listen queue before
	 * +accept()+, but only if +TCP_DEFER_ACCEPT+ is used with the
	 * listen socket (it is on by default in Unicorn).
	 *
	 * - state
	 * - ca_state
	 * - retransmits
	 * - probes
	 * - backoff
	 * - options
	 * - snd_wscale
	 * - rcv_wscale
	 * - rto
	 * - ato
	 * - snd_mss
	 * - rcv_mss
	 * - unacked
	 * - sacked
	 * - lost
	 * - retrans
	 * - fackets
	 * - last_data_sent
	 * - last_ack_sent
	 * - last_data_recv
	 * - last_ack_recv
	 * - pmtu
	 * - rcv_ssthresh
	 * - rtt
	 * - rttvar
	 * - snd_ssthresh
	 * - snd_cwnd
	 * - advmss
	 * - reordering
	 * - rcv_rtt
	 * - rcv_space
	 * - total_retrans
	 *
	 * https://kernel.org/doc/man-pages/online/pages/man7/tcp.7.html
	 */
	cTCP_Info = rb_define_class_under(cRaindrops, "TCP_Info", rb_cObject);
	rb_define_alloc_func(cTCP_Info, alloc);
	rb_define_private_method(cTCP_Info, "initialize", init, 1);

	/*
	 * Document-method: Raindrops::TCP_Info#get!
	 *
	 * call-seq:
	 *
	 *	info = Raindrops::TCP_Info.new(tcp_socket)
	 *	info.get!(tcp_socket)
	 *
	 * Update an existing TCP_Info objects with the latest stats
	 * from the given socket.  This even allows sharing TCP_Info
	 * objects between different sockets to avoid garbage.
	 */
	rb_define_method(cTCP_Info, "get!", init, 1);

#define TCPI_DEFINE_METHOD(x) \
	rb_define_method(cTCP_Info, #x, tcp_info_##x, 0)

	TCPI_DEFINE_METHOD(state);
	TCPI_DEFINE_METHOD(ca_state);
	TCPI_DEFINE_METHOD(retransmits);
	TCPI_DEFINE_METHOD(probes);
	TCPI_DEFINE_METHOD(backoff);
	TCPI_DEFINE_METHOD(options);
	TCPI_DEFINE_METHOD(snd_wscale);
	TCPI_DEFINE_METHOD(rcv_wscale);
	TCPI_DEFINE_METHOD(rto);
	TCPI_DEFINE_METHOD(ato);
	TCPI_DEFINE_METHOD(snd_mss);
	TCPI_DEFINE_METHOD(rcv_mss);
	TCPI_DEFINE_METHOD(unacked);
	TCPI_DEFINE_METHOD(sacked);
	TCPI_DEFINE_METHOD(lost);
	TCPI_DEFINE_METHOD(retrans);
	TCPI_DEFINE_METHOD(fackets);
	TCPI_DEFINE_METHOD(last_data_sent);
	TCPI_DEFINE_METHOD(last_ack_sent);
	TCPI_DEFINE_METHOD(last_data_recv);
	TCPI_DEFINE_METHOD(last_ack_recv);
	TCPI_DEFINE_METHOD(pmtu);
	TCPI_DEFINE_METHOD(rcv_ssthresh);
	TCPI_DEFINE_METHOD(rtt);
	TCPI_DEFINE_METHOD(rttvar);
	TCPI_DEFINE_METHOD(snd_ssthresh);
	TCPI_DEFINE_METHOD(snd_cwnd);
	TCPI_DEFINE_METHOD(advmss);
	TCPI_DEFINE_METHOD(reordering);
	TCPI_DEFINE_METHOD(rcv_rtt);
	TCPI_DEFINE_METHOD(rcv_space);
	TCPI_DEFINE_METHOD(total_retrans);
}
#endif /* TCP_INFO */
#endif /* defined(__linux__) && defined(HAVE_LINUX_TCP_H) */

git clone https://yhbt.net/raindrops.git