本文是 Unix/Linux系统管理自动化系列中的一篇,主要讲述如何实现自动化监控 CPU 的使用状态。
在 Unix/Linux 系统日常管理和系统维护的过程中,随时可能需要查看 CPU 的使用状态,并根据相应信息分析系统状况,判断系统的资源使用情况和系统负载情况。本文在介绍 Unix/Linux 系统中 CPU 相关机制和工具的基础上,对各工具进行了分析比较,并给出了自动化监控系统环境和 CPU 利用率的例子代码供读者参考。在 AIX 系统的 CPU 使用状态监控部分,我们将介绍 lparstat,mpstat 等工具。而在 Linux 系统 CPU 使用状态监控部分,我们将重点介绍 /proc 文件系统和 ps 等命令。
了解 AIX 系统中的可用 CPU 资源
AIX 系统中每个处理器在低位内存中有着对应的全局数据结构 PPDA (Per-processor Data Area),其中存储了处理器的各种配置信息和运行状态,比如时钟频率和 CPU 运行时间。AIX 系统接口查询 PPDA 中的数据并提供给终端用户或者应用程序使用。但在进行 CPU 监控之前,系统用户首先需要了解当前的系统环境和可用的计算资源。对于不同的系统环境,用户需要不同的监控策略。首先,对于没有划分微分区 (Micro Partition) 的服务器,系统用户可以通过很多操作系统接口得到机器实际可用的资源种类和数量。
硬件和操作系统没有使用虚拟化技术的系统
清单1.运行prtconf获得处理器的相关信息
$ prtconf | grep Processor Processor Type: PowerPC_POWER6 Processor Implementation Mode: POWER 6 Processor Version: PV_6 Number Of Processors: 4 Processor Clock Speed: 4005 MHz Model Implementation: Multiple Processor, PCI bus + proc0 Processor + proc2 Processor + proc4 Processor + proc6 Processor |
Processor Type 表示了处理器的类型属于 Power 6 系列。这里同样可以知道本机的处理器个数为 4,并且时钟运行频率为 4G。运用类似的命令接口,比如 lsdev -Cc processor,也可以得到其他与 CPU 相关的配置信息。
使用微分区的系统
虚拟化作为一项成熟的技术,在 IBM eServer pSeries 服务器和 BladeCenter JS 刀片服务器中已经得到广泛的应用。所以我们有必要简单说明用户在常见的虚拟化环境,微分区 (Micro Partition) 中如何得到可用的资源状态。AIX 的命令接口在虚拟化环境下仍然可以使用,但是通常得到的结果是分配给当前虚拟机的虚拟处理器 (Virtual CPU) 的信息,而不是物理处理器 (Physical CPU) 的。
清单2.在微分区上运行prtconf获得虚拟处理器的信息
# prtconf | grep Processor Processor Type: PowerPC_POWER5 Processor Implementation Mode: POWER 5 Processor Version: PV_5 Number Of Processors: 1 Processor Clock Speed: 1654 MHz Model Implementation: Multiple Processor, PCI bus + proc0 |
以上输出表示本机当前被分配了一个虚拟处理器,及其硬件类型和运行频率。为了获得该虚拟机获得的物理处理器的数量,通常需要访问对应的 HMC (Hardware Management Console) 或者 AMM (Advanced Management Module) 来获得详细的信息和配置。下面的实例运行于 HMC。
清单 3. 在 HMC 上运行 lssyscfg 获得微分区的配置
hscroot@hmc:~> lssyscfg -r prof -m Server-9117-MMA-SN060DD72 --filter "lpar_ids=p6ml4n05" name=p6ml4n05,lpar_name=p6ml4n05,lpar_id=6,lpar_env=aixlinux, all_resources=0,min_mem=2048,desired_mem=3072,max_mem=4096, min_num_huge_pages=0,desired_num_huge_pages=0, max_num_huge_pages=0,mem_mode=ded,proc_mode=shared,min_proc_units=0.1, desired_proc_units=0.2,max_proc_units=0.3,min_procs=1,desired_procs=1, max_procs=1,sharing_mode=cap,uncap_weight=0,shared_proc_pool_id=0, shared_proc_pool_name=DefaultPool,io_slots=none,lpar_io_pool_ids=none, max_virtual_slots=10, "virtual_serial_adapters=0/server/1/any//any/1,1/server/1/any//any/1", virtual_scsi_adapters=2/client/1/vioserver/16/0,virtual_eth_adapters=none, hca_adapters=none,boot_mode=norm,conn_monitoring=0,auto_start=0, power_ctrl_lpar_ids=none,work_group_id=none,redundant_err_path_reporting=0, bsr_arrays=0,lhea_logical_ports=23000000/2/1/5/none, lhea_capabilities=23000000/0, lpar_proc_compat_mode=default,electronic_err_reporting=null, virtual_fc_adapters=none |
此处跟 CPU 相关的重要字段有:
- min_proc_units:为了启动该分区所需的最少的物理处理器个数
- desired_proc_units: 该分区运行时通常需要的物理处理器个数
- max_proc_units: 该分区在运行时允许获得的最多的物理处理器个数
使用负载分区的系统
从 AIX 6.1 开始,用户可以创建并使用负载分区 (Workload Partition,简称 WPAR)。负载分区可以模拟一个 AIX 6.1 用户环境,但在缺省情况下,CPU 资源不会被以 dedicated 方式划分给负载分区。所以 AIX 的系统命令,比如 lsdev 在负载分区下的输出结果会是当前系统没有处理器。负载分区中的用户,在系统响应不够及时的情况下,通常有如下几种可能:
- 负载分区本身被分配的资源不足
- 负载分区的其对应的宿主机的负载过大,或者出现了 IO 瓶颈
后面的段落会具体介绍如何确定和解决负载分区中跟 CPU 相关的系统瓶颈。
获得 AIX 系统中处理器资源的代码示例
本段的最后给出一段基于 Perl 的例子代码,在 AIX5.3 和 6.1 上测试通过,可以侦测当前的系统环境和 CPU 的类型和数量。这段代码首先运行系统命令 oslevel 得到当前系统的版本,如果是 AIX6.1 则运行命令 lparstat 判断当前系统是否为 WPAR。无论 AIX 系统的版本如何,都加参数 -i 运行命令 lparstat 来获得分区的个数,并以此判断是否使用了微分区技术。最后调用命令 prtconf 输出 CPU 的一些参数。
该段代码可以帮助用户快速了解当前环境,尤其适用于工作环境复杂或频繁变更的场景。需要指出的是对于使用了微分区技术的系统,代码的最后输出的是虚拟处理器的数量。需要了解系统中物理处理器和逻辑处理器状态的用户,可以参阅本文其他段落加以补充。
清单4.获得当前的系统环境和CPU的类型和数量的代码示例
# WPAR仅存在于AIX 6.1系统 my $oslevel = `oslevel`; if ($oslevel =~ m/6\.1/) { # 在WPAR中不带参数运行lparstat命令只返回警告信息 my $output = `lparstat`; if ($output =~ m/The output is applicable only to the Global Environment/) { print "This machine is one WPAR with following CPU assigned.\n"; system ("prtconf | grep Processor"); exit 0; } } # 使用–i参数列出详细的配置信息 my @outputs = `lparstat -i`; foreach my $line (@outputs) { # 解析命令输出并得到分区个数 if ($line !~ m/Partition Number/) {next;} my ($blank, $partition_num) = split /Partition Number\s+:\s/, $line; chomp $partition_num; if ($partition_num eq '-') # full system环境没有划分微分区 { print "This machine is one full system without LPARs. The system has following physical CPUs installed.\n"; }elsif ($partition_num > 1) # 如果存在多于一个的分区,该系统使用了微分区技术 { print "This machine is one LPAR with virtual following CPUs installed. To check the assignment of physical CPU, please log on HMC or AMM.\n"; }else { print "Can not decide whether current environment is one LPAR or not. Please check HMC or AMM to decide this.\n"; } } # 打印处理器本身的参数 system ("prtconf | grep Processor"); exit 0; |
![]() ![]() |
使用 AIX 系统工具分析处理器的工作状态
在了解当前环境的可用 CPU 资源以后,可以对系统监控的对象和方法进行规划。多数情况下,操作系统提供的命令输出提供逻辑处理器 (Logical CPU) 的信息。在 IBM eServer pSeries 服务器和 BladeCenter JS 刀片服务器中,逻辑处理器通常通过并行多线程技术实现。下文是对该技术在 AIX 系统中应用的介绍。
并行多线程的查询和设置
并行多线程 (Simultaneous Multi-threading,简称 SMT) 是一种基于超标量 (superscalar) 体系结构处理器的技术,允许多个独立的线程在一个时钟周期内对多个功能处理单元 (Functional Unit) 发出指令。通常情况下,线程和功能处理单元的绑定是完全动态的。使用这项技术可以较明显的提高处理器的的实际利用率,进而表现出更强的运算性能。在 AIX 操作系统上,我们可以通过 smtctl 命令来查询和设置并行多线程的运行状态。
清单5.运行smtctl查询处理器对SMT技术的支持和当前的设置
$ smtctl This system is SMT capable. SMT is currently enabled. SMT boot mode is not set. SMT threads are bound to the same physical processor. proc0 has 2 SMT threads. Bind processor 0 is bound with proc0 Bind processor 1 is bound with proc0 proc2 has 2 SMT threads. Bind processor 2 is bound with proc2 Bind processor 3 is bound with proc2 proc4 has 2 SMT threads. Bind processor 4 is bound with proc4 Bind processor 5 is bound with proc4 proc6 has 2 SMT threads. Bind processor 6 is bound with proc6 Bind processor 7 is bound with proc6 |