5.cgo 原理解析及优化实践
cgo 原理解析及优化实践 朱德江 蚂蚁集团 MOSN 核心成员 Golang contributor Envoy Golang extension maintainer 公众号 • 开源爱好者 • 十余年网关研发 • OpenResty 老司机(NGINX + LuaJIT) • MOSN 核心成员 • Envoy Golang extension maintainer • • 玩过 DSL 编译器 • 对 LuaJIT、Go 有一些研究 目 录 背景介绍 01 cgo 工作机制 02 cgo 调度机制 03 CPU 优化 04 GC 优化 05 背景介绍 第一部分 网关发展历史 网关的扩展机制 什么是 MoE 举个例子 为什么需要 MoE Envoy 研发效能 良好的生态,上手门槛低 Wasm?Lua? Golang Golang 云原生架构 良好的可扩展性 高性能 MoE 有什么挑战 业界少见 - 重度依赖 cgo 性能敏感 延时敏感 网关场景 cgo 是成熟 & 稳定的 唯一的 bug:trace 工具 性能挖掘空间大 cgo 工作机制 第二部分 Foreign Function Interface 函数调用 数据交互 抽象模型 1 2 3 对 PC0 码力 | 45 页 | 5.74 MB | 1 年前301. MOSN 高性能网络扩展实践 - 王发康
GoLang Runtime 还未完全支持; 不能复用已有的 SDK,需要做网络 IO 适配改造 External-Proc Extension 跨语言支持、隔离性 需要跨进程通信性能低(UDS vs CGO 1KB Latency 差 8 倍); 需要扩展具备 gRPC server 能力, 多进程管理复杂 MOSN(GoLang) Extension 可复用 MOSN 现有的 filter 能力, 解决方案无论在当前阶段还是未来都具备一定优势 NanoVisor Cilium NginxUnit Dragonboat Badger Go with OpenCV etc CGO 是 Go 官方出品 用到 CGO 的一些项目 社区 CGO 维护频度 Go 1.16 Release Notes Go 1.15 Release Notes Go 1.13 Release Notes Go 1.12 Release Release Notes Go 1.5 Release Notes Go 1.4 Release Notes …… CGO 一次调用开销在 0.08 ~ 1.626 us,另外 CGO 调用 开销呈线性增长;CGO 中 增加 Go 自身计算逻辑时,其 Go 的计算消耗也呈线性增 长 CGO 开销调研 MoE 方案介绍 MoE 整体架构 MoE 功能职责 MoE TraceID 事例分析 MoE0 码力 | 29 页 | 2.80 MB | 1 年前3Go on GPU
package mtl // import "changkun.de/x/gopherchina2023gogpu/gpu/mtl" /* #cgo CFLAGS: -Werror -fmodules -x objective-c #cgo LDFLAGS: -framework Metal -framework CoreGraphics #include "mtl.h" */ import with GPUs ● Accelerate Go programs using GPUs ● Challenges in Go when using GPUs ○ Costs of Cgo ○ Fundamental infrastructure ○ Common abstraction ○ Writing and debugging shaders ● Conclusion Toolchains" Challenge 1: The cost of Cgo Operations on GPU involves system calls, and easiest approach is to use Cgo: /* #cgo CFLAGS: -Werror -fmodules -x objective-c #cgo LDFLAGS: -framework Metal -framework0 码力 | 57 页 | 4.62 MB | 1 年前31.3 MOSN 在云原生的探索及实践
云原生网络平台建设 • 升级 Xprotocol 框架 • 支持 WASM • 区块链网络框架 • 代码热更新 • 高性能网络层扩展 • fastGRPC • 协程收敛 epoll 模型 • CGO 性能优化 • 支持 zipkin,Jaeger 等 • 支持 ZK,Nacos 等 • 支持 Dubbo 3.0 • 支持 thrift, kafka 等 协议 • 支持 Istio 1 不能复用已有的 SDK,需要做网 络 IO 适配改造 External-Proc Extension 适合治理能力已经是一个远程服 务,集成进 Envoy 需要跨进程通信性能低(UDS vs CGO 1KB Latency 差 8 倍); 需要扩展具备 gRPC server 能力, 多进程管理复杂 MOSN(GoLang) Extension 可复用 MOSN 现有的 filter 能 解决方案无论在当前阶段还是未来都具备一定优势 NanoVisor Cilium NginxUnit Dragonboat Badger Go with OpenCV etc CGO 是 Go 官方出品 用到 CGO 的一些项目 社区 CGO 维护频度 Go 1.16 Release Notes Go 1.15 Release Notes Go 1.13 Release Notes Go 1.12 Release0 码力 | 36 页 | 35.61 MB | 1 年前32.6 Go in Linux Desktop Environment
appliations Deepin XX ? 遇到的问题 与其他组件的融合 X11 解决方式 using CGO to get anything Pure golang library CGO Tips Go-C C-Go struct array CGO Tips: Go call C functions && types convert Easy to use. Just srandom(C.uint(s)) r := C.random() fmt.Println(r) } Run CGO Tips: Even more convenient than C at make time package main /* #cgo pkg-config: gtk+-3.0 #includevoid write_c_function GTK_WINDOW_TOPLEVEL) C.write_c_function_in_go_source(w) C.gtk_widget_show_all(w) C.gtk_main() } Run CGO Tips: Struct is also easy to use package main /* #include #include struct tm* 0 码力 | 36 页 | 414.50 KB | 1 年前3Golang Manual By AstaXie-20120522
add to archive // Cgo directives CgoCFLAGS []string // cgo: flags for C compiler CgoLDFLAGS []string // cgo: flags for linker CgoPkgConfig []string // cgo: pkg-config names // Gccgo is a GCC front-end that can, with care, be linked with GCC-compiled C or C++ programs. The cgo program provides the mechanism for a “foreign function interface” to allow safe calling of C libraries MySQL: https://github.com/ziutek/mymysql ODBC: https://bitbucket.org/miquella/mgodbc Postgres (uses cgo): https://github.com/jbarham/gopgsqldriver Postgres (pure Go): https://github.com/bmizerany/pq SQLite:0 码力 | 6205 页 | 12.83 MB | 1 年前3Golang 微服务在腾讯游戏用户运营领域的探索及实践
流程 敏捷 隔离 性能 安全 监控 中间件技术选型 团队背景 内部生态 业务发展 开发成本 运营成本 Golang CSP并发 多核友好 自动GC 语法简洁 开源库多 CGO集 成 快速交付 内核稳定 生态趋势 数据中间件——指标服务 Golang 高性能 高可用 扩展性 安全性 可运营性 业务发展驱动技术演进 服务网关 过载保护 流控降级 问题 手段 嵌入脚本选型 • Expression、Learning cost Custom • CGO overhead Javascript(V8) • Performance Javascript (otto) • CGO / Thread Safe Python/PHP • CGO / Thread overhead cLua/cLuaJIT • Performance / API0 码力 | 34 页 | 1.22 MB | 1 年前3Go 入门指南(The way to Go)
Go 语言代码或者包而言,在各个操作系统平台上的可移植性是非常强的,只需要将源 码拷贝到相应平台上进行编译即可,或者可以使用交叉编译来构建目标平台的应用程序(第 2.2 节)。但 如果你打算使用 cgo 或者类似文件监控系统的软件,就需要根据实际情况进行相应地修改了。 1. Go 原生编译器 gc: 主要基于 Ken Thompson 先前在 Plan 9 操作系统上使用的 C 工具链。 Go 18 - 本文档使用 看云 构建 如果发生错误 fatal error: can’t find import: fmt 则说明你的环境变量没有配置正确。 如果你想要在 Windows 下使用 cgo (调用 C 语言写的代码),则需要安装 MinGW,一般推荐安 装 TDM-GCC。如果你使用的是 64 位操作系统,请务必安装 64 位版本的 MinGW。安装完成进行环境 变量等相关配置即可使用。 LICENSE /bin :包含可执行文件,如:编译器,Go 工具 /doc :包含示例程序,代码工具,本地文档等 /lib :包含文档模版 /misc :包含与支持 Go 编辑器有关的配置文件以及 cgo 的示例 /os_arch :包含标准库的包的对象文件( .a ) /src :包含源代码构建脚本和标准库的包的完整源代码(Go 是一门开源语言) /src/cmd :包含 Go 和 C 的编译器和命令行脚本0 码力 | 380 页 | 2.97 MB | 1 年前3Gitea v1.21.1 中文文档
GOOS=linux 和 GOARCH=arm64 )进行交 叉编译,您需要 aarch64-unknown-linux-gnu-gcc 交叉编译器。这是因为 Gitea 构建标签使用了 cgo 的外部函数接 口(FFI)。 在没有任何标签的情况下,交叉编译的 Gitea 为 Linux ARM64 版本: 1. GOOS=linux GOARCH=arm64 make build Zig。 编译 (Linux ➝ Linux) 1. CC="zig cc -target x86_64-linux-gnu" \ 2. CGO_ENABLED=1 \ 3. CGO_CFLAGS="-O2 -g -pthread" \ 4. CGO_LDFLAGS="-linkmode=external -v" 5. GOOS=linux \ 6. GOARCH=amd64 \ 7 8. make build 交叉编译 (Linux ➝ Windows) 1. CC="zig cc -target x86_64-windows-gnu" \ 2. CGO_ENABLED=1 \ 3. CGO_CFLAGS="-O2 -g -pthread" \ 4. GOOS=windows \ 5. GOARCH=amd64 \ 6. TAGS="bindata sqlite0 码力 | 303 页 | 3.88 MB | 1 年前3The Way To Go - 2012
amd64, 386 The portability of Go-code between OS’s is excellent (assuming you use pure Go-code, no cgo, inotify or very low level packages): just copy the source code to the other OS and compile, but you templates for the documentation \misc configuration files for editors for working with Go, cgo examples, … \pkg\os_arch with os_arch e.g. linux_amd64, windows_386, … the object files (.a) with C The cgo program provides the mechanism for FFI-support (foreign function interface) to allow safe calling of C libraries from Go code: http://golang.org/cmd/cgo is the primary cgo documentation0 码力 | 629 页 | 4.85 MB | 1 年前3
共 52 条
- 1
- 2
- 3
- 4
- 5
- 6