8000 add socket_bypass_tcpip bpf demo · lx1036/code@4d0c5de · GitHub
[go: up one dir, main page]

Skip to content

Commit 4d0c5de

Browse files
author
shenming
committed
add socket_bypass_tcpip bpf demo
1 parent 70f5caa commit 4d0c5de

File tree

19 files changed

+1202
-312
lines changed

19 files changed

+1202
-312
lines changed

go/k8s/algo/go/GMP.md

Lines changed: 0 additions & 8 deletions
This file was deleted.

go/k8s/algo/go/goroutine/main.go

Lines changed: 0 additions & 17 deletions
This file was deleted.

go/k8s/algo/leetcode/GMP.md

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
2+
3+
# GMP
4+
https://iswbm.com/537.html
5+
6+
* G(goroutine): 一个 goroutine 最小 4k 内存,4G内存可以创建 1024*1024 goroutine,大约 1Million 个 goroutine。
7+
goroutine 存在的意义:因为进程或线程切换时,需要从用户态到内核态的切换。
8+
* M(Thread): 操作系统线程,go runtime 设置最多可以创建 10K 个 thread。
9+
* P(Processor): 机器的 cpu 核数,可是设置 GOMAXPROCS 调小。
10+
11+
比如,一个 4c4g pod,可以最大创建 1M 个 goroutine,10K 个 thread, 4 个 P。thread 线程数量和 cpu processor 核数数量保持一致,
12+
这样防止线程切换非常浪费 cpu 资源,这样每一个 thread 都绑定在一个 cpu processor 上,无需线程切换。但是,也为了防止 g1 系统调用导致 thread1
13+
处于阻塞中,p1 就需要重建一个 thread2 来继续运行其他 goroutine。所以,不是那么死板。
14+
15+
所以,现在有 1M 个 goroutine 等着被调度到 4 个 thread/processor 上。
16+
17+
## Goroutine 调度
18+
19+
goroutine 所在的队列:
20+
* local queue: 每一个 thread 都有一个自己的 local queue,当某个 goroutine 找到可以绑定的 thread,就会存放在该 thread 所属的 local queue。
21+
* global queue: 全局 queue,当某个 goroutine 没有找到对应的 cpu 可以调度(说明 4 个 cpu 都在处理各自 local queue 的 goroutine,很忙),
22+
那这个 goroutine 就会放到 global queue。
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
package leetcode
2+
3+
import (
4+
"github.com/sirupsen/logrus"
5+
"sync"
6+
"testing"
7+
)
8+
9+
// 协程池示例代码 https://blog.51cto.com/zhangxueliang/8368196
10+
11+
func TestGoroutinePool(test *testing.T) {
12+
type Job struct {
13+
id int
14+
}
15+
type Result struct {
16+
id int
17+
done bool
18+
}
19+
20+
numOfJobs := 10
21+
numOfWorkers := 3
22+
23+
jobs := make(chan Job, numOfJobs)
24+
results := make(chan Result, numOfJobs)
25+
26+
worker := func(jobs chan Job, results chan Result) {
27+
for job := range jobs {
28+
// do job
29+
r := Result{
30+
id: job.id,
31+
done: true,
32+
}
33+
results <- r
34+
}
35+
}
36+
37+
// 创建协程池
38+
wg := &sync.WaitGroup{}
39+
for i := 0; i < numOfWorkers; i++ {
40+
wg.Add(1)
41+
go func() {
42+
defer wg.Done()
43+
worker(jobs, results)
44+
}()
45+
}
46+
47+
// 同步提交 job
48+
for i := 0; i < numOfJobs; i++ {
49+
jobs <- Job{
50+
id: i + 1,
51+
}
52+
}
53+
close(jobs)
54+
55+
go func() {
56+
wg.Wait()
57+
close(results)
58+
}()
59+
60+
for result := range results {
61+
logrus.Infof("result: %+v", result)
62+
}
63+
}

0 commit comments

Comments
 (0)
0