Utility for collecting TCP metrics from Netty channels.
| 36 | * Utility for collecting TCP metrics from Netty channels. |
| 37 | */ |
| 38 | final class TcpMetrics { |
| 39 | private static final Logger log = Logger.getLogger(TcpMetrics.class.getName()); |
| 40 | |
| 41 | static EpollInfo epollInfo = loadEpollInfo(); |
| 42 | |
| 43 | static final class EpollInfo { |
| 44 | final Class<?> channelClass; |
| 45 | final java.lang.reflect.Constructor<?> infoConstructor; |
| 46 | final Method tcpInfo; |
| 47 | final Method totalRetrans; |
| 48 | final Method retransmits; |
| 49 | final Method rtt; |
| 50 | |
| 51 | EpollInfo( |
| 52 | Class<?> channelClass, |
| 53 | java.lang.reflect.Constructor<?> infoConstructor, |
| 54 | Method tcpInfo, |
| 55 | Method totalRetrans, |
| 56 | Method retransmits, |
| 57 | Method rtt) { |
| 58 | this.channelClass = channelClass; |
| 59 | this.infoConstructor = infoConstructor; |
| 60 | this.tcpInfo = tcpInfo; |
| 61 | this.totalRetrans = totalRetrans; |
| 62 | this.retransmits = retransmits; |
| 63 | this.rtt = rtt; |
| 64 | } |
| 65 | } |
| 66 | |
| 67 | static EpollInfo loadEpollInfo() { |
| 68 | boolean epollAvailable = false; |
| 69 | try { |
| 70 | Class<?> epollClass = Class.forName("io.netty.channel.epoll.Epoll"); |
| 71 | Method isAvailableMethod = epollClass.getDeclaredMethod("isAvailable"); |
| 72 | epollAvailable = (Boolean) isAvailableMethod.invoke(null); |
| 73 | if (epollAvailable) { |
| 74 | Class<?> channelClass = Class.forName("io.netty.channel.epoll.EpollSocketChannel"); |
| 75 | Class<?> infoClass = Class.forName("io.netty.channel.epoll.EpollTcpInfo"); |
| 76 | return new EpollInfo( |
| 77 | channelClass, |
| 78 | infoClass.getDeclaredConstructor(), |
| 79 | channelClass.getMethod("tcpInfo", infoClass), |
| 80 | infoClass.getMethod("totalRetrans"), |
| 81 | infoClass.getMethod("retrans"), |
| 82 | infoClass.getMethod("rtt")); |
| 83 | } |
| 84 | } catch (ReflectiveOperationException e) { |
| 85 | log.log(Level.FINE, "Failed to initialize Epoll tcp_info reflection", e); |
| 86 | } finally { |
| 87 | log.log(Level.INFO, "Epoll available during static init of TcpMetrics:" |
| 88 | + "{0}", epollAvailable); |
| 89 | } |
| 90 | return null; |
| 91 | } |
| 92 | |
| 93 | private static final long RECORD_INTERVAL_MILLIS = TimeUnit.MINUTES.toMillis(5); |
| 94 | private final MetricRecorder metricRecorder; |
| 95 | private final Object tcpInfo; |
nothing calls this directly
no test coverage detected