从 CPU 利用率中查找使用的核心?

从 CPU 利用率中查找使用的核心?

我有一台 Linux 服务器的平均和峰值 CPU 利用率(以百分比表示)。这台机器上主要部署了几个 Web 应用程序。我需要根据这些来决定哪台 AWS 机器适合我。AWS 根据 CPU 核心提供机器成本。

现在我需要从 CPU 利用率中找出平均核心利用率和峰值核心利用率,我可以根据某些公式从 CPU 利用率中推导出核心利用率吗?如果不行,我如何才能在一段时间内找到 Linux 服务器上的平均核心利用率和峰值核心利用率?

答案1

您可以找到核心用法的地方:

  • 性能 命令可能有一些有用的计数器
  • top键入时的命令1
  • 顶部实用程序提供视觉反馈
  • 该命令mpstat -P ALL 1提供每秒更新一次的显示。您还可以将输出定向到文本文件并使用某些实用程序对其进行解析。

答案2

您可以监视/proc/stat并观察平均值和最大值。这个答案有一个 bash 脚本可以给你灵感,你的man proc应该类似于:

/proc/stat
  kernel/system statistics.  Varies with architecture.  Common entries
  include:

  cpu  3357 0 4313 1362393
        The amount of time, measured in units of USER_HZ (1/100ths of
        a second on most architectures, use  sysconf(_SC_CLK_TCK)  to
        obtain  the  right  value),  that the system spent in various
        states:

        user   (1) Time spent in user mode.

        nice   (2) Time spent in user mode with low priority (nice).

        system (3) Time spent in system mode.

        idle   (4) Time spent in the idle task.  This value should be
               USER_HZ  times  the  second  entry in the /proc/uptime
               pseudo-file.

        iowait (since Linux 2.5.41)
               (5) Time waiting for I/O to complete.

        irq (since Linux 2.6.0-test4)
               (6) Time servicing interrupts.

        softirq (since Linux 2.6.0-test4)
               (7) Time servicing softirqs.

        steal (since Linux 2.6.11)
               (8) Stolen time, which is  the  time  spent  in  other
               operating  systems when running in a virtualized envi‐
               ronment

        guest (since Linux 2.6.24)
               (9) Time spent running a virtual CPU for guest operat‐
               ing systems under the control of the Linux kernel.

        guest_nice (since Linux 2.6.33)
               (10) Time spent running a niced guest (virtual CPU for
               guest operating systems under the control of the Linux
               kernel).

[来源:man在 shell 脚本中获取每个核心的 CPU 负载]

相关内容