Grab value for LEAF_tcpCurrEstab from net.inet.tcp.stats instead of

net.inet.tcp.pcblist, which is much cheaper.
This commit is contained in:
Gleb Smirnoff 2016-01-27 00:50:27 +00:00
parent 75dd79d937
commit a781766836

View File

@ -45,10 +45,10 @@ struct tcp_index {
};
static uint64_t tcp_tick;
static uint64_t tcp_stats_tick;
static struct tcpstat tcpstat;
static struct xinpgen *xinpgen;
static size_t xinpgen_len;
static u_int tcp_count;
static u_int tcp_total;
static u_int oidnum;
@ -64,13 +64,9 @@ tcp_compare(const void *p1, const void *p2)
}
static int
fetch_tcp(void)
fetch_tcp_stats(void)
{
size_t len;
struct xinpgen *ptr;
struct xtcpcb *tp;
struct tcp_index *oid;
in_addr_t inaddr;
len = sizeof(tcpstat);
if (sysctlbyname("net.inet.tcp.stats", &tcpstat, &len, NULL, 0) == -1) {
@ -82,6 +78,20 @@ fetch_tcp(void)
return (-1);
}
tcp_stats_tick = get_ticks();
return (0);
}
static int
fetch_tcp(void)
{
size_t len;
struct xinpgen *ptr;
struct xtcpcb *tp;
struct tcp_index *oid;
in_addr_t inaddr;
len = 0;
if (sysctlbyname("net.inet.tcp.pcblist", NULL, &len, NULL, 0) == -1) {
syslog(LOG_ERR, "net.inet.tcp.pcblist: %m");
@ -102,7 +112,6 @@ fetch_tcp(void)
tcp_tick = get_ticks();
tcp_count = 0;
tcp_total = 0;
for (ptr = (struct xinpgen *)(void *)((char *)xinpgen + xinpgen->xig_len);
ptr->xig_len > sizeof(struct xinpgen);
@ -114,10 +123,6 @@ fetch_tcp(void)
if (tp->xt_inp.inp_vflag & INP_IPV4)
tcp_total++;
if (tp->xt_tp.t_state == TCPS_ESTABLISHED ||
tp->xt_tp.t_state == TCPS_CLOSE_WAIT)
tcp_count++;
}
if (oidnum < tcp_total) {
@ -184,8 +189,8 @@ op_tcp(struct snmp_context *ctx __unused, struct snmp_value *value,
abort();
}
if (tcp_tick < this_tick)
if (fetch_tcp() == -1)
if (tcp_stats_tick < this_tick)
if (fetch_tcp_stats() == -1)
return (SNMP_ERR_GENERR);
switch (value->var.subs[sub - 1]) {
@ -226,7 +231,8 @@ op_tcp(struct snmp_context *ctx __unused, struct snmp_value *value,
break;
case LEAF_tcpCurrEstab:
value->v.uint32 = tcp_count;
value->v.uint32 = tcpstat.tcps_states[TCPS_ESTABLISHED] +
tcpstat.tcps_states[TCPS_CLOSE_WAIT];
break;
case LEAF_tcpInSegs: