Docker Foundation - Union FS
Union FS Docker 的创新在文件系统:通过镜像解决容器分发的问题 将不同目录挂载到同一个虚拟文件系统下的文件系统 支持为每一个成员目录(类似于 Git Branch)设定权限:readonly、readwrite、whiteout-able 文件系统分层,对 readonly 权限的 Branch 可以逻辑上进行修改(增量、不影响 readonly 部分) 用途 将多个 Disk 挂在同一个目录下 将一个 readonly 的 Branch 和一个 writeable 的 Branch 联合在一起 Docker 镜像 层可以复用,增量分发 12# docker image inspect --format '{{.RootFS}}' centos:latest{layers [sha256:74ddd0ec08fa43d09f32636ba91a0a3053b02cb4627c35051aff89f853606b59] } 镜像文件系统 bootfs Bootloader 引导加载 kerne...
Docker Foundation - Namespace + Cgroup
概要 基于 Linux Kernel 的 Cgroup、Namespace、Union FS 等技术,对进程进行封装隔离,属于 OS 层面的虚拟化技术 由于被隔离的进程独立于宿主机和其它被隔离的进程,因此称为容器 演变历史 最早基于 LXC – a linux container runtime 0.7:去除 LXC,转而使用自研的 Libcontainer 1.11:使用 runC 和 containerd Docker 在容器的基础上,进行了进一步的封装,极大地简化了容器的创建和维护 Docker vs VM Docker Engine 是一个 Daemon 进程,启动一个容器的时候,相当于执行了一次进程的 Fork 操作 Container VM 启动时间 秒级 分钟级 硬盘使用 MB 级别 GB 级别 性能 接近原生 弱于 单机支持量 上千 几十 容器标准 OCI – Open Container Initiative K8S 的主要贡献:标准化 后期 Docker 公司不得不让步,兼容 OCI 标准 核心规范:Image + Runti...
ASM - Introduction
Glance ASM is an all purpose Java bytecode manipulation and analysis framework. It can be used to modify existing classes or to dynamically generate classes, directly in binary form. ASM provides some common bytecode transformations and analysis algorithms from which custom complex transformations and code analysis tools can be built. ASM offers similar functionality as other Java bytecode frameworks, but is focused on performance. Because it was designed and implemented to be as small and as fast as p...
Cloud Native Foundation - Go Engineering
IO 模型 阻塞:等待数据报文就绪;同步:等待内核拷贝数据 阻塞 + 同步 非阻塞 + 同步轮询 轮询不是一个好的检查状态变更的方式,很难支撑高并发的场景 IO 多路复用 Go select channel 也是借鉴于此 select / poll 在用户态将关心的 fd 列表(限制为 1024)发送给 kernel epoll 不需要传递 fd 列表:通过 mmap,将用户态和内核态的内存共享 事件驱动 epoll_create:初始化 wq(wait queue)、rdlist(ready list)、rbr(red-black root) epoll_ctl:一个 socket 创建后会加入到红黑树进行管理 epoll_wait:当 socket 尚未就绪时,用户线程会阻塞 当收到数据报文后,触发中断处理程序 然后将对应的 epitem(对应一个 socket)移动到 rdlist,并且唤醒阻塞在该 socket 上的用户进程 支持高并发 非阻塞 + 异步 HTTP G 与 socket fd 绑定 当 socket fd 尚未就绪时,将对应的 G 设置为 ...
Cloud Native Foundation - Go Memory Management
堆内存管理Linux 进程 管理过程 Allocator 的职责:通过系统调用向 OS 申请内存(MB/GB);响应 Mutator(即应用) 的内存申请(KB) Collector:交还给 Allocator,再由 Allocator 决定是否释放并归还给 OS 初始化连续内存块为堆 在内存申请的时候,Allocator 从堆内存的未分配区域分割小内存块 用链表将已分配内存连接起来 需要信息描述每个内存块的元数据(对象头):大小、是否使用、下一个内存块的地址 Example: Ruby allocates memory from the memory allocator, which in turn allocates from the kernel 面临挑战 内存分配需要系统调用,在频繁分配内存的时候,系统性能较低 C 的每次 malloc 是需要系统调用的 Java 的 Allocator 可以直接一次性申请大内存,然后在用户态再慢慢分配 多线程共享相同的内存空间,同时申请内存,需要加锁,或者采用类似 JVM 中的 TLAB 来缓解竞争 经过不断地内存分配和回收,...
Bytecode Manipulation - Javassist
概要 Unlike other similar bytecode editors, Javassist provides two levels of API: source level and bytecode level. 生成字节码1234567891011121314151617package me.zhongmingmao.javassist;public class Point { private int x; private int y; public Point(int x, int y) { this.x = x; this.y = y; } public void move(int x, int y) { this.x = x; this.y = y; }} 12$ javac me/zhongmingmao/javassist/Point.java$ javap -v me.zhongmingmao.javassist.Point 123456789101112131415161...
Bytecode Manipulation - Java Agent Practice
概念Instrument Instrument 是 JVM 提供的一个可以修改已加载类的类库,依赖于 JVMTI 的 Attach API 机制 要使用 Instrument 的类修改功能,需要实现 java.lang.instrument.ClassFileTransformer 接口 可以在 ClassFileTransformer#transform 中利用 ASM 或者 Byte Buddy 等工具对字节码进行操作 Instrument 通过与 Java Agent 结合来注入到 JVM 中 JVMTI & Agent JPDA(Java Platform Debugger Architecture)是 JDK 标准,必须实现 如果 JVM 启动时开启了 JPDA,那么类是允许被重新加载的 已加载的旧版类信息被卸载,然后重新加载新版本的类 JVMTI 是 JVM 提供的一套对 JVM 进行操作的工具接口,Agent 是 JVMTI 的一种实现 Attach API 的作用:提供 JVM 进程间通信的能力 Attach 后,目标 JVM 在运行时走到 Agent 中定义的 age...
Cloud Native Foundation - Go Thread Scheduling
进程 & 线程 进程:资源分配的基本单位 线程:调度的基本单位 在内核视角,线程与进程无本质差别,在 Linux 中都是以**task_struct**进行描述 glibc 中的 pthread 库提供了 Native POSIX Thread Library 支持 Native POSIX Thread Library (NPTL) is an implementation of the POSIX Threads specification for the Linux operating system. 子进程通过 fork 的方式产生,基于父子关系形成进程树,Linux 中的初始进程一般为systemd 12$ pmap 11: /lib/systemd/systemd --system --deserialize 37 Linux 进程内存 虚拟地址 https://ggirjau.com/text-data-bss-heap-stack-and-where-in-memory-are-stored-variables-of-c-program/ Segme...
Cloud Native Foundation - Go Sync
线程安全 锁 Go 不仅提供了基于 CSP 的通讯模型,也支持基于共享内存的多线程数据访问 sync 包提供了锁的基本原语 同步工具 用途 sync.Mutex 互斥锁:Lock加锁,Unlock解锁 sync.RWMutex 读写分离锁:不限制并发读,只限制并发写和并发读写 sync.WaitGroup 等待一组 goroutine 返回,类似于 Java 的 CountDownLatch sync.Once 保证某段代码只执行1次,典型场景:单例模式 sync.Cond 让一组 goroutine 在满足特定条件时被唤醒,典型场景:生产者消费者模型 Mutex12345678910111213141516171819202122232425262728293031323334353637func unsafeWrite() { // fatal error: concurrent map writes conflictMap := map[int]int{} for i := 0; i < 100; i++ ...
Cloud Native Foundation - Go Features
特点 可以高效编译、支持高并发、面向垃圾回收 秒级完成大型程序的单节点编译 依赖管理清晰 不支持继承 支持垃圾回收、支持并发执行、支持多线程通讯 对多核计算机支持友好 特性来源 环境变量 Env Desc Value GOARCH The architecture, or processor, for which to compile code. amd64 GOBIN The directory where ‘go install’ will install a command. GOCACHE The directory where the go command will store cached information for reuse in future builds. ~/Library/Caches/go-build GOMODCACHE The directory where the go command will store downloaded modules. ~/go/pkg/mod ...












