亚马逊AWS首席云计算技术顾问 费良宏——AWS云计算之上Linux实例的优化

2020-02-27 303浏览

  • 1.AWS 云计算之上 Linux 实例的优化 费良宏,lianghon@amazon.com AWS 首席云计算技术顾问 August 19, 2016
  • 2.AWS 全球基础设施 13 个区域、35 个可用区、56 个边缘站点
  • 3.AWS 广泛的云计算服务 TECHNI CA L & BUSINESS SUPPORT Support HYBRID ARCHITECTURE I ntegrated Networking Professional S erv ic es Partner Ec osy stem Training & Certific ation Solutions Arc hitec ts Ac c ount Management Security & Pric ing Reports D irec t Connec t I dentity Federation I ntegrated App Deployments Data Backups I ntegrated Resourc e Management Business Apps A NA L YTI CS Data Warehousing Business I ntelligenc e Hadoop/ Spark Streaming Data Analysis Streaming Data Collec tion Mac hine L earning Elastic S earc h I dentity Management Compute VMs, Auto-scaling, & Lo ad B alan cin g Regions Business I ntelligenc e DevOps Tools MA RKETPL A CE S ec urity Networking Databases APP SERVICES Queuing & Notific ations Workflow S earc h Email Transc oding MOBILE SERVICES API Gateway I dentity Sync Mobile Analy tic s Single Integrated Console Push Notific ations DEVELOPMENT & OPERATIONS One-click App Deployment DevOps Resource Management Application Lifecycle Management Containers Triggers Resourc e Templates IoT Rules Engine D ev ic e Shadows Device SDKs D ev ic e Gateway Registry Ac c ess Control Key Management & Storage Storage Object, Blocks, Archival, Imp o r t /Exp o r t SECURITY & COMPLIANCE Monitoring & Logs Configuration Complianc e CORE SERVICES CDN INFRASTRUCTURE Availability Zones Web application firewall Assessment and reporting Databases Relational, NoSQL, C ach in g, Migr at io n Storage ENTERPRISE APPS Virtual Desktops Sharing & Collaboration Corporate Email Bac k up Resource & Usage Auditing Networking VP C , DX , DNS Points of Presenc e
  • 4.2016 Gartner 云计算 IaaS 魔力象限 *Gartner, Magic Quadrant for Cloud Infrastructure as a Service, Worldwide, Leong, Lydia, Petri, Gregor, Gill, Bob, Dorosh, Mike, August 32016 This graphic was published by Gartner, Inc. as part of a larger research document and should be evaluated in the context of the entire document. The Gartner document is available upon request from AWS : h ttp :/ / www.g a rtn er.co m / d o c/ rep ri n ts ?i d =1 -2G2 O 5FC &ct=1 5 05 19 &s t=s b Gartner does not endorse any vendor, product or service depicted in its research publications, and does not advise technology users to select only those vendors with the highest ratings or other designation. Gartner research publications consist of the opinions of Gartner's research organization and should not be construed as statements of fact. Gartner disclaims all warranties, expressed or implied, with respect to this research, including any warranties of merchantability or fitness for a particular purpose.
  • 5.Amazon Elastic Compute Cloud (EC2) 在云端的弹性 虚拟服务器 虚拟机1 虚拟机2 虚拟机n 虚拟层 宿主机 AWS全球区域中的 实体服务器
  • 6.Amazon EC2 诞生于十年前… • 第一代, 仅有一种类型与大小 – m1.small (1 vCPU, 1.7 GiB 内存, 160 GB 存储) • 仅支持 Linux 操作系统 • 仅支持按需 (On-Demand) 付费的方式
  • 7.Amazon EC2 实例的历史 m1.small c1.xlarge c1.medium t1.micro m2.xlarge m3.large m3.medium cr1.8xlarge g2.2xlarge m3.2xlarge m3.xlarge m1.medium cc2.8xlarge c3.8xlarge c3.4xlarge c3.2xlarge c3.xlarge c3.large r3.8xlarge r3.4xlarge c4.8xlarge c4.4xlarge r3.2xlarge c4.2xlarge r3.xlarge c4.xlarge r3.large c4.large 2006 2008 m1.large m1.xlarge 2010 m2.2xlarge m2.4xlarge cc1.4xlarge cg1.4xlarge 2012 hi1.4xlarge hs1.8xlarge 2014 i2.xlarge i2.2xlarge i2.4xlarge i2.4xlarge t2.micro d2.xlarge t2.small d2.2xlarge t2.med d2.4xlarge d2.8xlarge g2.8xlarge 2016 t2.large m4.large m4.xlarge m4.2xlarge m4.4xlarge m4.10xlarge
  • 8.今天,丰富的操作系统选择 • 免费的 OS AMI – Amazon Linux、CentOS、 FreeBSD、Ubuntu、Debian 等 • 付费 OS AMI (按小时付费) – Windows Server – Red Hat Enterprise Linux – SUSE Linux Enterprise • Marketplace
  • 9.EC2 实例: 家族与类型 通用类型: 计算优化: 内存优化:GPU:存储优化: 高 I/O:密集存储: T2、M4、M3 C4、C3 X1、R3 G2 I2 D2
  • 10.Amazon 弹性计算云服务 购买选项 API 网络 实例 EC2 EC2
  • 11.Amazon EC2 实例 Guest 1 Guest 2 Guest n Hypervisor Host Server
  • 12.关于Amazon EC2 的性能 • 定义 系统的性能 和它在不同的工作负载下 的特征 • Amazon EC2 实例的 性能表现 如何,以及 如何提供灵活性和敏捷性 • 如何 最大限度 的利用好Amazon EC2 实例
  • 13.关于 性能 的定义
  • 14.获得一台服务器 • 获取服务器用以完成所需的任务 • 基于不同的任务,性能的度量有很大的不同 ?
  • 15.性能的定义: 从问题的视角出发 性能的表现,取决于不同的视角: 响应时间 吞吐量 一致性 Workload Application System libraries System calls Kernel Devices
  • 16.影响性能的因素 资源 CPU 内存 影响性能的因素 关键指标 CPU 插槽、CPU 核的数量、时 CPU 利用率、运行队列长度 钟频率, CPU bursting 能力 内存容量 空闲内存、匿名分页、线程交换 网络 磁盘 最大带宽、包速率 每秒钟完成的输入/输出操 作,吞吐量 接收吞吐量、最大带宽下传输吞 吐量 等待队列长度、设备利用率、设 备错误
  • 17.资源的利用率 • 对于给定的性能,有效的资源被使用的如何? • 当处于在100%的利用率的情况下,则不能接受任 何更多的工作 • 低利用率可以显示出购买的资源多于所需
  • 18.实例选择 = 性能优化 • 挑选一个适合的实例等同于资源的性能优化 • 获得新的实例前尽早的释放不需要的实例 • 找到一个理想的实例类型和工作负载的组合
  • 19.实例选择流程图 Start Find best balance i2 Select memory to cache working set 来源:Netflix
  • 20.例子: Amazon EC2 C4 实例 • 最新一代的计算优化实例,最高性能的处理器和最 高的性价比 • 定制的 Intel E5-2666 v3 at 2.9 GHz • 可控制 P-state 和 C-state 配置 型号 c4.large c4.xlarge c4.2xlarge c4.4xlarge c4.8xlarge vCPU 内存(GiB) 专用EBS带宽 (Mbps) 2 3.75 500 4 7.5 750 8 15 1,000 16 30 2,000 36 60 4,000
  • 21.Amazon EC2 的 技术 特性
  • 22.CPU 指令核保护等级 • CPU 有至少两个保护等级 • 不能在用户模式下执行特权指令来保护系统。应用程 序利用系统调用访问内核 Kernel Application
  • 23.例子: Web 应用的系统调用
  • 24.X86 CPU 虚拟化: 在 Intel VT-x 之前 • 二进制翻译特权指令 • 半虚拟化 (PV) – PV 需要穿透VMM, 增加了延迟 – 系统调用绑定的应用程序受到严重的影响 PV Kernel VMM Application
  • 25.X86 CPU 虚拟化: 在 Intel VT-x 之后 • 硬件辅助的虚拟化 (HVM) • PV-HVM 使用PV 驱动,巧妙的处理了那些较慢 的模拟操作: • 例如:网络和块I/O PV-HVM VMM Kernel Application
  • 26.Xen 虚拟化模式 • 性能最好的是最新的 Xen 所支持的 PV/HVM 混合模式 • 在 Amazon EC2上: – “HVM” == “PVHVM”,如果Linux kernel 支持 – “PV” == “PV” • 当前在Amazon EC2 上最快的模式就是: “HVM” (PVHVM) • 未来最快的应该是 PVH
  • 27.Xen 虚拟化模式
  • 28.提示:使用PV-HVM AMIs 和 EBS
  • 29.关于 Timekeeping • 在一个实例中的 Timekeeping 很容易让人感到困惑 • gettimeofday(), clock_gettime(), QueryPerformanceCounter() • TSC – CPU counter, accessible from userspace – Requires calibration, vDSO – Invariant on Sandy Bridge+ processors • Xen 的 pvclock; 不支持 vDSO • 在当前这一代的实例中, 使用 TSC 作为时钟源(clocksource)
  • 30.SR-IOV • 单根 I/O 虚拟化 (SR-IOV) – PCIe 设备提供的虚拟化实例 • AWS “增强联网” – 适用于部分实例类型:C3、C4、R3、I2、M4、D2、X1 – 仅仅用于 VPC 环境下 • 性能提升: – 提高网络吞吐量 – 降低网络平均往返时延 (RTT) 和减少抖动 – 提高取决于实例类型和网络
  • 31.Linux 内核 调优
  • 32.调优的目标 1、 CPU Scheduler 2、 Virtual Memory 3、 Huge Pages 4、 File System 5、 Storage I/O 6、 Networking 7、 Hypervisor (Xen)
  • 33.1. CPU Scheduler 调整项: – Scheduler class、priorities、migration latency、tasksets… 用途: – 一些应用得益于通过使用 taskset(1)、numactl(8)、 cgroups以及 优化 sched_migration_cost_ns 而减少进程 迁移 – 一些 Java 应用得益于 SCHED_BATCH 减少上下文切换 # schedtool –B PID # schedtool -R -p 20
  • 34.2. Virtual Memory 调整项: – swappiness、overcommit、OOM … 用途: – swappiness 设置为0用以禁用交换,停止文件页面 缓存以释放更多的内存 vm.swappiness = 0 # Set the swappiness value as root echo 10 > /proc/sys/vm/swappiness # Alternatively, run this sysctl -w vm.swappiness=10 # from 60
  • 35.3. Huge Pages 内存页面尺寸为2M 或者 4M,而不是常见的 4K。降低各种CPU开销并且提高MMU 页面到缓存 的转换 调整项: – 显性的使用huge page、transparent huge pages (THPs) 用途: – THPs (内核中启用),依赖于工作负载和CPU,有时候可以提高性能 (~5% CPU负荷), 但是有时候会影响性能 (~25% CPU 负荷 , 当 %usr, 以及 %sys refrag)。禁用的方法: # grep Huge /proc/meminfo …Hugepagesize:2048 kB … # echo never > /sys/kernel/mm/transparent_hugepage/enabled # from madvise # java -XX:+UseLargePages
  • 36.4. File System 调整项: – 页面缓存的刷新,文件系统的类型和可调整的参数 (例如: ZFS) 用途: – 页面缓存刷新的行为可以调整为: background flush earlier、 aggressive flush later – 禁止访问时间戳和其它的选项,依赖于具体的文件系统 vm.dirty_ratio = 80 # from 40 vm.dirty_background_ratio = 5 # from 10 vm.dirty_expire_centisecs = 12000 # from 3000 mount -o defaults,noatime,discard,nobarrier …
  • 37.5. Storage I/O 调整项: – Read ahead size、number of in-flight requests、I/O scheduler、 volume stripe width… 用途: – 一些应用(例如 Cassandra)对read ahead size 非常敏感 – SSDs 可以使用更好的 “noop” scheduler (非缺省的设置) – 调整chunk size 和 stripe width 以匹配任务负载 /sys/block/*/queue/rq_affinity 2 /sys/block/*/queue/scheduler noop /sys/block/*/queue/nr_requests 256 /sys/block/*/queue/read_ahead_kb 256 mdadm –chunk=64 ...
  • 38.6. Networking 调整项: – TCP buffer sizes、TCP backlog、device backlog、TCP reuse … 用途: net.core.somaxconn = 1000 net.core.netdev_max_backlog = 5000 net.core.rmem_max = 16777216 net.core.wmem_max = 16777216 net.ipv4.tcp_wmem = 4096 12582912 16777216 net.ipv4.tcp_rmem = 4096 12582912 16777216 net.ipv4.tcp_max_syn_backlog = 8096 net.ipv4.tcp_slow_start_after_idle = 0 net.ipv4.tcp_tw_reuse = 1 net.ipv4.ip_local_port_range = 10240 65535 net.ipv4.tcp_abort_on_overflow = 1 # maybe
  • 39.7. Hypervisor (Xen) 调整项: – PV/HVM (AMI 提供) – 内核 clocksource,从慢到快: hpet、xen、tsc 用途: – 调优clocksource ,设置为TSC (小心时钟漂移)。较好的情 况下 CPU 用量减少 30%, 并且应用程序的平均延迟降低了 43% # cat /sys/devices/system/clocksource/clocksource0/current_clocksource Xen #echo tsc > /sys/devices/system/clocksource/clocksource0/current_clocksource
  • 40.提示: 使用TSC 作为时钟源
  • 41.性能 监测
  • 42.例子: Web 应用 • 在Apache 上安装 MediaWik,并带有140页的内容 • 负载随时间间隔而增加
  • 43.例子: Web 应用 • 内存使用统计
  • 44.例子: Web 应用 • 磁盘使用统计
  • 45.例子: Web 应用 • 网络使用统计
  • 46.例子: Web 应用 • CPU 使用统计
  • 47.无效的调优 “打地鼠”式的优化: – 随机的调整参数直到问题消失 “街灯”式的优化: – 1. 选择监控工具 • 只使用自己熟悉的 • 从互联网找工具 • 随机选择工具 – 2. 运行工具 – 3. 观察问题
  • 48.有效调优 基于观察,数据驱动: 1. 对系统负荷以及资源使用情况进行观察 2. 分析结果: • 确定使用的硬件/软件的组件 • 研究组件的可调整参数 • 量化预期的改进 3. 调优,实验以及确定目标
  • 49.USE 方法 USE 方法 - 对每一项硬件和软件资源,检查它们的: 1. 利用率(Utilization) 2. 饱和度(Saturation) 3. 错误数(Errors) 如果,资源约束显示为高饱和度或者高利用率 – 调整或者改变实例类型 – 了解资源的可调参数 USE 方法帮我们发现问题,然后使用工具解决问题
  • 50.实例之外的考虑 实例免责 - 复杂的性能问题没有明显的原因,也许是实例的问题? - 有时候分析之后确认与实例有关。但是大多数与应用有关 (80/20 原则) 80/20 原则: - 80%:通过提高应用程序的重构和优化获得改进 - 20%:OS 调优、实例或者基础架构的改善 20/80 延迟异常原则: - 20%::延迟异常是由程序代码引起 - 80%:是由实例、 crontab、网络以及 JVM GC 等引起
  • 51.分析的视角 工作负载分析 – “自顶向下”分析 – 从应用的附载着手 然后,分解请求时间 资源分析: – “自底向上”分析 – 从资源的性能开始着手,然后是工作负载 例如:USE 方法应用于负载的特征上 Application System Libraries System Calls Kernel Devices
  • 52.Linux 性能工具 1. Statistical 工具 2. Profiling 工具 3. Tracing 工具 4. Hardware 计数器
  • 53.1. Statistical 工具 最常用的有 vmstat、pidstat,、sar 等 $ sar -n TCP,ETCP,DEV 1 Linux 3.2.55 (test-e4f1a80b) 08/18/2014 _x86_64_ (8 CPU) 09:10:43 PM IFACE rxpck/s txpck/s rxkB/s txkB/s rxcmp/s txcmp/s rxmcst/s 09:10:44 PM lo 14.00 14.00 1.34 1.34 0.00 0.00 0.00 09:10:44 PM eth0 4114.00 4186.00 4537.46 28513.24 0.00 0.00 0.00 09:10:43 PM active/s passive/s iseg/s oseg/s 09:10:44 PM 21.00 4.00 4107.00 22511.00 09:10:43 PM atmptf/s estres/s retrans/s isegerr/s 09:10:44 PM 0.00 0.00 36.00 0.00 […] orsts/s 1.00
  • 54.2. Profiling 工具Profiling:典型的应用场景 – 对 CPU 堆栈跟踪的采样,可以解释 CPU 的使用 – 内存对象的频度计数,可以解释内存的使用 Profiling 产生的调优 – 热代码 (Hot code) 的路径 -> 相关的可调参数/配置? – 频繁的对象分配 -> 避免这种情况的方法?
  • 55.Profiling 类型 程序的profiling – 取决于应用和开发语言 – 例如: Java Flight Recorder, Yourkit, Lightweight Java Profiler – 许多工具缺乏准确以及存在问题;测试、验证、交叉检查 系统的 profiling – Linux perf_events ( “perf” 命令) – ftrace 可以用作内核函数的计数 – SystemTap 拥有各种分析功能
  • 56.面向应用的Profiling:LJP 轻量级的 Java Profiler (LJP) – 开源的、免费的、异步的 CPU profiler – 使用代理 (agent) 转储 hprof 同样格式的输出 •https://code.google.com/archive/p/lightweight-java-profiler/profiling 的输出结果可以输出为 - 火焰图 (flame graphs)
  • 57.
  • 58.系统Profiling:perf_events perf_events 对CPU 堆栈跟踪进行采样,可以显示为: – 应用的逻辑 • 依赖于应用和 VM,可以展示出高级的逻辑关系 – JVM 内部信息和库 – Linux 内核 perf CPU 火焰图 (flame graphs): # git clonehttps://github.com/brendangregg/FlameGraph# cd FlameGraph # perf record -F 99 -ag -- sleep 60 # perf script ./stackcollapse-perf.pl ./flamegraph.pl > perf.svg
  • 59.
  • 60.Broken Java stacks (missing frame pointer) Kernel TCP/IP Locks Time GC Idle thread epoll
  • 61.3. Tracing 工具 Linux 中现有的系统 tracers工具 - 1. ftrace 2. perf_events 3. eBPF 4. SystemTap 5. ktap 6. LTTng 7. dtrace4linux 8. Oracle Linux DTrace 9. Sysdig 接下来,归纳一下 ftrace & perf_events …
  • 62.ftrace Linux 内核的一部分 – 第一次出现是在 2.6.27 (2008), 在后续的版本中 得到增强 – 直接使用 /sys/kernel/debug/tracing 前端工具: perf-tools 集合 –https://github.com/brendangregg/perf-tools
  • 63.ftracetool:iosnoop # ./iosnoop –ts Tracing block I/O. Ctrl-C to end. STARTs ENDs COMM 5982800.302061 5982800.302679 supervise 5982800.302423 5982800.302842 supervise 5982800.304962 5982800.305446 supervise 5982800.305250 5982800.305676 supervise […] PID 1809 1809 1801 1801 TYPE DEV W 202,1 W 202,1 W 202,1 W 202,1 BLOCK 17039600 17039608 17039616 17039624 BYTES LATms 4096 0.62 4096 0.42 4096 0.48 4096 0.43 # ./iosnoop –hUSAGE:iosnoop [-hQst] [-d device] [-i iotype] [-p PID] [-n name] [duration] -d device # device string (eg, "202,1) -i iotype # match type (eg, '*R*' for all reads) -n name # process name to match on I/O issue -p PID # PID to match on I/O issue -Q # include queueing time in LATms -s # include start time of I/O (s) -t # include completion time of I/O (s) […]
  • 64.perf_events Linux 代码的一部分 – 位于tools/perf 之下 强大的工具集合分析器 – interval sampling, CPU performance counter events – user and kernel dynamic tracing – kernel line tracing and local variables (debuginfo) – kernel filtering,in-kernel 计数器 (perf stat) 高级用途需要内核 debuginfo – 通常要大于100M 字节
  • 65.perf_events 示例 # perf record –eskb:consume_skb–ag -- sleep 10 # perf report [...] 74.42% swapper [kernel.kallsyms] [k] consume_skb --- consume_skb arp_process arp_rcv __netif_receive_skb_core __netif_receive_skb Summarizing stack traces for a tracepoint netif_receive_skb virtnet_poll net_rx_action __do_softirq perf_events can do many things, it is hard to pick just one example irq_exit do_IRQ ret_from_intr […]
  • 66.4. Hardware 计数器 Model Specific Registers (MSRs) – 基本信息: timestamp clock, temperature, power – 部分已经存在于 Amazon EC2 性能监控计数器 (PMCs) – 高级信息:cycles, stall cycles, cache misses… – 缺省情况下Amazon EC2 不包含 周期性CPU 用量的根本原因 – 例如:更高的内存用量导致更多的延迟周期 (stall cycles)
  • 67.MSRs ( Model Specific Registers ) 可以用来验证CPU 的真实频率 – 能够适应 turboboost,很重要的实现性能对比 – 项目地址https://github.com/brendangregg/msr-cloud-toolsReal CPU MHz ec2-guest# ./showboost CPU MHz : 2500 Turbo MHz : 2900 (10 active) Turbo Ratio : 116% (10 active) CPU 0 summary every 5 seconds... TIME 06:11:35 06:11:40 06:11:45 [...] C0_MCYC 6428553166 6349881107 6240610655 C0_ACYC 7457384521 7365764152 7239046277 UTIL 51% 50% 49% RATIO 116% 115% 115% MHz 2900 2899 2899
  • 68.总结 l 实例的 选择 = 性能 优化 l 针对Linux 内核的调优 l 性能 监测
  • 69.参考链接 Amazon EC2 - –http://aws.amazon.com/ec2/instance-types/–https://docs.aws.amazon.com/zh_cn/AWSEC2/latest/U'>https://docs.aws.amazon.com/zh_cn/AWSEC2/latest/U