Go GC:
Latency Problem Solvedwrites are just memory writes: \*slot = ptr| |---|---| |Stack scan|Collect pointers from global and goroutine stacksStacks scanned at preemption points| |Mark|Mark objects and follow pointers until pointer0 码力 | 20 页 | 897.11 KB | 2 年前3
Go 101 (Golang 101) v1.21.0$ §12 $ ) for details. • return lines in function declarations. • deferred function calls and goroutine creations. The two will be introduced in the article after next ( $ §13 $ ). ## Examples of Expressions Function Calls and Panic/Recover This article will introduce goroutines and deferred function calls. Goroutine and deferred function call are two unique features in Go. This article also explains panic and recover program, a computer, or a network. In Go 101, we only talk about program-scope concurrent computing. Goroutine is the Go way to create concurrent computations in Go programming. Goroutines are also often called0 码力 | 880 页 | 833.34 KB | 2 年前3
Go Context原理及其典型适用场景<-data: fmt.Println(ch) } } ## Context ● 一个任务,需要多个goroutine完成 ● 一个请求,会触发好多个goroutine做不同的事情 ● 如果我们的任务终止了,请求取消了,这些正在运行的goroutine怎么办? ● 使用Context来简化这些操作 ## Context能做什么 ## ☑ 手动、定时、超时发出取消信号 取消控制 Ctx2 Ctx4 Ctx1 Ctx5 Ctx3 Ctx6 ## Thread Local Storage 存放线程独享的数据 ThreadLocal(Java) Goroutine Local Storage(X) Context (V) 传递状态 VS 全局状态 ## Context Value func WithValue(parent Context, key0 码力 | 32 页 | 4.62 MB | 2 年前3
1.6 Go并发编程实践 - 晁岳攀alt=‘OCR图片’/> 基本同步原语 Mutex 互斥锁 Mutual exclusion, 任何时间只允许一个goroutine在临界区域运行 避免死锁 公平 零值是未锁状态 Unlock未加锁的Mutex会panic 加锁的Mutex不和这个特定的goroutine关联 非重入锁 ’ alt=‘OCR图片’/> 基本同步原语 Mutex - 初版(2008) export dd2074c8做了一次大的改动,它将waiter count(等待者的数量)和锁标识分开来(内部实现还是合用使用state字段)。新来的goroutine占优势,会有更大的机会获取锁。 2015年,commit edcad863,Go1.5中mutex实现为全协作式的,增加了spin机制,一旦有竞争,当前goroutine就会进入调度器。在临界区执行很短的情况下可能不是最好的解决方案。 2016年commit0556e262,Go1 2016年commit0556e262,Go1.9中增加了饥饿模式,让锁变得更公平,不公平的等待时间限制在1毫秒,并且修复了一个大bug,唤醒的goroutine总是放在等待队列的尾部会导致更加不公平的等待时间。 2019年commit 41cb0ae inline优化,将slow path抽取出来,保留 fast path以便内联 ’ alt=‘OCR图片’/> 基本同步原语 Mutex - 当前实现 type Mutex0 码力 | 82 页 | 16.62 MB | 1 月前3
2.2.3 Go语言的抢占式调度$ g $ _{0} $ 初始化 获取CPU核心数 argv 处理 m0: Go 程序启动后创建的第一个线程; ## 第二部分 ## GMP 的本质 ## G、M、P 的本质 G:goroutine,一个计算任务。由需要执行的代码和其上下文组成,上下文包括:当前代码位置,栈顶、栈底地址,状态等。 M: machine,系统线程,执行实体,想要在 CPU 上执行代码,必须有线程,与 C 语言中的线程相同,通过系统调用 curg gp.preempt = true gp.stackguard0 = stackPreempt return true } 这之后的流程需要正在运行的 goroutine 来配合 ## 协作式抢占的“协作”过程 ## 检查当前栈空间是否足够,不够的话,需要申请新的栈空间 ☐ ☐ ☐ ☐ ☐ ☐ package main //go:noinline func go:6) JMP 0 只要当 framesize > 0 时才会有栈空间检查 framesize 一般是由 locals 决定的 ## 协作式抢占的“协作”过程 ### 保存当前 goroutine 的运行现场,切换到 m.g0,执行 newstack morestack_noctxt ## ↓ morestack ↓ newstack ## ☀️ ☀️ ☁️ TEXT r0 码力 | 44 页 | 7.43 MB | 2 年前3
可视化学习 Go 并发编程Go 语言中的并发 • goroutine - 并发执行 • channel - 同步和消息传输 • select - 多路并发控制 ## Goroutine - 类似于 UNIX 中的 & • 很像线程,但更轻量 - 一个 goroutine 就是一个独立运行的函数 - 当一个 goroutine 阻塞时,所在的线程会阻塞,但其它 goroutine 不受影响 f("hello" <- time.Now() // 将时间发送给timerChan }() // 做一些其它事情;准备接收 // 接收会阻塞,直到 timerChan 传送值 // 值的发送是另上个 goroutine 结束的时间 completedAt := <-timerChan ## Select • 类似于 switch - 但它的判断条件是基于通信,而不是基于值的等量匹配 select package main func main() { // 创建一个 int 类型的 channel ch := make(chan int) // 启动一个新的匿名 goroutine go func() { // 发送 42 给 channel ch <- 42 }() // 从 channel 读取 <-ch0 码力 | 29 页 | 1.48 MB | 2 年前3
用Go语言实现推送服务器## Go语言特性 • 静态的、编译的 • 自动内存回收 • 命令式编程 • 函数可以作为值 • 面向并发 • 内置RPC支持 ## 推送服务器要求的应对 - 高并发 - goroutine - 可靠性 - 使用Redis暂存消息 - 高性能 - 静态编译语言 - 支持水平扩展 - 使用RPC组成集群 - 无单点故障 - 使用Redis实现数据共享 ## Go语言的并发模型 • 事件驱动,共享线程池 runtime.GOMAXPROCS(runtime.NumCPU()) - 使用 “go” 命令创建goroutine go sockstore.Start() - goroutine使用channel交换消息 - 异步场景,直接往指定channel发送数据 - 同步场景,往channel发送的数据中,包含一个获取返回值的channel • 推送服务器介绍 • 推送服务架构 • 部分代码 • 上线效果 ## 串行场景-Socket Server • 在主程序启动时启动 • 所有请求都用一个goroutine响应 - 对外提供API,实质是往goroutine发送消息 { select { case value := <-socketMapChans.chGet: 14g0 码力 | 25 页 | 260.04 KB | 2 年前3
2.4 Go 1.4 runtime### Go 1.4 runtime Gopher China 2015 1. Memory Allocator 2. Garbage Collector 3. Goroutine Scheduler  ### 1. Memory Allocator 在类 UNIX 系统,通过建议操作系统内核解除内存映射的方式释放物理内存,但不回收虚拟内存。 再次使用时,因缺页异常,由内核重新分配物理内存。 ### 3. Goroutine Scheduler 幷发调度器 ## goroutine 0 码力 | 29 页 | 608.57 KB | 2 年前3
Go性能优化概览-曹春晖## 第二部分 生产环境的优化 ## 首先,是发现问题 API 压测 全链路压测 进 pprof 寻找(可能的) 故障原因 CPU 用爆了?90%? 内存用爆了?OOM? Goroutine 用爆了?80w? 线程数爆了? 延迟太高? 按照不同的情况 选择不同的方案 生产环境被 高峰流量打爆了 线上一定要开 pprof 如果有安全考虑 那也一定要有能通过配置开启的能力 average: see Section 2.8, Statistics) ## 压测时关注哪些服务指标 因为我们是 Go 的服务,还可以额外看看: • Goroutine 数,线程数 - 如果 Goroutine 数很多,那这些 Goroutine 在干什么? • GC 频率,gctrace 的内容(线上保存 gctrace 的话,注意硬盘类型),GC 的 • Memstats 中的其它指标: 看 prometheus,内存 RSS 是多少,goroutine 数量多少,goroutine 栈占用多少 -> 如果 goroutine 不多,那么重点关注 heap profile 中的 inuse -> 定时任务类需要看 alloc 4. goroutine 数量过多 -> 从 profile 网页进去看看 goroutine 都在干什么 -> 查死锁、阻塞等问题 ->0 码力 | 40 页 | 8.69 MB | 2 年前3
Go 101 (Golang 101) v1.21.0$ §12 $ ) for details. • return lines in function declarations. • deferred function calls and goroutine creations. The two will be introduced in the article after next ( $ §13 $ ). ## Examples of Expressions Function Calls and Panic/Recover This article will introduce goroutines and deferred function calls. Goroutine and deferred function call are two unique features in Go. This article also explains panic and recover program, a computer, or a network. In Go 101, we only talk about program-scope concurrent computing. Goroutine is the Go way to create concurrent computations in Go programming. Goroutines are also often called0 码力 | 610 页 | 945.17 KB | 2 年前3
共 117 条
- 1
- 2
- 3
- 4
- 5
- 6
- 12













