当前位置: 首页 > news >正文

[netty5: LifecycleTracer ResourceSupport]-源码分析

LifecycleTracer

@UnstableApi
public abstract class LifecycleTracer {// 默认关闭static final boolean lifecycleTracingEnabled = SystemPropertyUtil.getBoolean("io.netty5.buffer.lifecycleTracingEnabled", false);// 重点!public static LifecycleTracer get() {// 生命周期追踪未启用 且 泄漏检测关闭if (!lifecycleTracingEnabled && LeakDetection.leakDetectionEnabled == 0) {return NoOpTracer.INSTANCE;}return new StackTracer();}public abstract void allocate();public abstract void acquire(int acquires);public abstract void drop(int acquires);public abstract void close(int acquires);public abstract void touch(Object hint);public abstract <I extends Resource<I>, T extends ResourceSupport<I, T>> Owned<T> send(Owned<T> instance);public abstract void splitTo(LifecycleTracer splitTracer);public abstract <E extends Throwable> E attachTrace(E throwable);public abstract Collection<TracePoint> collectTraces();// NoOpTracer// StackTracer, Trace, Traceback// TraceType // AttachmentType 
}

TraceType

用于记录资源生命周期中发生的关键事件。

枚举值含义
ALLOCATE分配资源(如内存)时发生的事件。
ACQUIRE从池中获取(重用)资源的事件。
CLOSE显式关闭资源的事件。
DROP底层释放资源(drop 操作)。
SEND资源被发送的事件(如跨线程)。
RECEIVE资源被接收的事件。
TOUCH调用 touch(Object hint) 时记录的事件。用于定位资源流向。
SPLIT缓冲区被拆分为子缓冲区的事件。
private enum TraceType {ALLOCATE,ACQUIRE,CLOSE,DROP,SEND,RECEIVE,TOUCH,SPLIT,
}

AttachmentType

用于连接不同资源之间的因果或传递关系。

枚举值含义
SEND_FROM表示当前追踪器是被“发送自”哪个追踪器。
RECEIVED_AT表示当前追踪器“接收到”哪个资源。
SPLIT_FROM当前追踪器是从哪个对象拆分来的。
SPLIT_TO当前追踪器拆分出去形成了哪个对象。
HINT来源于 touch(hint) 中的 hint 参数。
private enum AttachmentType {SEND_FROM,RECEIVED_AT,SPLIT_FROM,SPLIT_TO,HINT,
}

ResourceSupport

提供对资源生命周期追踪的支持,主要通过 LifecycleTracer 进行资源的生命周期管理。它是一个基础类,可以为所有继承它的资源类型提供生命周期追踪功能。

@UnstableApi
public abstract class ResourceSupport<I extends Resource<I>, T extends ResourceSupport<I, T>> implements Resource<I> {private int acquires; // Closed if negative.private Drop<T> drop;private final LifecycleTracer tracer;protected ResourceSupport(Drop<T> drop) {this.drop = drop;tracer = LifecycleTracer.get();tracer.allocate();}@SuppressWarnings("unchecked")static <T> T acquire(ResourceSupport<?, ?> obj) {return (T) obj.acquire();}static LifecycleTracer getTracer(ResourceSupport<?, ?> obj) {return obj.tracer;}protected final T acquire() {if (acquires < 0) {throw attachTrace(createResourceClosedException());}if (acquires == Integer.MAX_VALUE) {throw new IllegalStateException("Reached maximum allowed acquires (" + Integer.MAX_VALUE + ").");}acquires++;tracer.acquire(acquires);return impl();}protected abstract RuntimeException createResourceClosedException();@Overridepublic final void close() {if (acquires == -1) {throw attachTrace(new IllegalStateException("Double-free: Resource already closed and dropped."));}int acq = acquires;acquires--;if (acq != 0) {// Only record a CLOSE if we're not going to record a DROP.tracer.close(acq);} else {// The 'acquires' was 0, now decremented to -1, which means we need to drop.tracer.drop(0);try {drop.drop(impl());Reference.reachabilityFence(this); // Avoid racing with any Cleaner.} finally {makeInaccessible();}}}@Overridepublic final Send<I> send() {if (acquires < 0) {throw attachTrace(createResourceClosedException());}if (!isOwned()) {throw notSendableException();}try {var owned = tracer.send(prepareSend());return new SendFromOwned<>(owned, drop, getClass());} finally {acquires = -2; // Close without dropping. This also ignore future double-free attempts.makeInaccessible();}}protected <E extends Throwable> E attachTrace(E throwable) {return tracer.attachTrace(throwable);}protected IllegalStateException notSendableException() {return new IllegalStateException("Cannot send() a reference counted object with " + countBorrows() + " borrows: " + this + '.');}static boolean isOwned(ResourceSupport<?, ?> obj) {return obj.isOwned();}protected boolean isOwned() {return acquires == 0;}static int countBorrows(ResourceSupport<?, ?> obj) {return obj.countBorrows();}protected int countBorrows() {return Math.max(acquires, 0);}@Overridepublic boolean isAccessible() {return acquires >= 0;}@Overridepublic I touch(Object hint) {if (isAccessible()) {tracer.touch(hint);}return self();}protected abstract Owned<T> prepareSend();protected void makeInaccessible() {}protected Drop<T> unsafeGetDrop() {return drop;}protected void unsafeSetDrop(Drop<T> replacement) {drop = Objects.requireNonNull(replacement, "Replacement drop cannot be null.");}@SuppressWarnings("unchecked")private I self() {return (I) this;}@SuppressWarnings("unchecked")private T impl() {return (T) this;}
}

AdaptableBuffer

AdaptableBuffer 继承自 ResourceSupport,因此它和它的子类(UnsafeBufferNioBufferMemSegBuffer)能够利用 ResourceSupport 提供的资源生命周期追踪功能。

public abstract class AdaptableBuffer<T extends ResourceSupport<Buffer, T>> extends ResourceSupport<Buffer, T> implements Buffer {protected final AllocatorControl control;protected AdaptableBuffer(Drop<T> drop, AllocatorControl control) {super(drop);this.control = control;}@Overridepublic AdaptableBuffer<T> touch(Object hint) {super.touch(hint);return this;}@Overridepublic boolean equals(Object o) {return o instanceof Buffer && InternalBufferUtils.equals(this, (Buffer) o);}@Overridepublic int hashCode() {return InternalBufferUtils.hashCode(this);}
}
http://www.dtcms.com/a/269176.html

相关文章:

  • idea启动后闪一下,自动转为后台运行
  • 全国产化行业自主无人机智能处理单元-AI飞控+通信一体化模块SkyCore-I
  • VmWare 安装 mac 虚拟机
  • 量子计算+AI芯片:光子计算如何重构神经网络硬件生态
  • C++ 定位 New 表达式深度解析与实战教程
  • 如果让计算机理解人类语言- Word2Vec(Word to Vector,2013)
  • 系统学习Python——并发模型和异步编程:基础知识
  • 无需公网IP的文件交互:FileCodeBox容器化部署技术解析
  • AI编程才刚起步,对成熟的软件工程师并未带来质变
  • Java 内存分析工具 Arthas
  • Cookie的HttpOnly属性:作用、配置与前后端分工
  • 用U盘启动制作centos系统最常见报错,系统卡住无法继续问题(手把手)
  • 用于构建多模态情绪识别与推理(MERR)数据集的自动化工具
  • 2025年全国青少年信息素养大赛图形化(Scratch)编程小学高年级组初赛样题答案+解析
  • 【Netty高级】Netty的技术内幕
  • 设计模式—专栏简介
  • Baumer工业相机堡盟工业相机如何通过DeepOCR模型识别判断数值和字符串的范围和相似度(C#)
  • Spring AOP 设计解密:代理对象生成、拦截器链调度与注解适配全流程源码解析
  • 學習網頁製作
  • 应用俄文OCR技术,为跨语言交流与数字化管理提供更强大的支持
  • 【前端UI】【ShadCN UI】一个干净、语义化、可拓展、完全可控的“代码级组件模板库”
  • 选择排序算法详解(含Python实现)
  • python中MongoDB操作实践:查询文档、批量插入文档、更新文档、删除文档
  • 指尖上的魔法:优雅高效的Linux命令手册
  • GitHub 趋势日报 (2025年07月06日)
  • PyTorch 详细安装教程及核心API使用指南
  • Chatbox➕知识库➕Mcp = 机器学习私人语音助手
  • 分层Agent
  • turborepo 如何解决git管理包过大的问题
  • 二、Docker安装部署教程