AskOverflow.Dev

AskOverflow.Dev Logo AskOverflow.Dev Logo

AskOverflow.Dev Navigation

  • 主页
  • 系统&网络
  • Ubuntu
  • Unix
  • DBA
  • Computer
  • Coding
  • LangChain

Mobile menu

Close
  • 主页
  • 系统&网络
    • 最新
    • 热门
    • 标签
  • Ubuntu
    • 最新
    • 热门
    • 标签
  • Unix
    • 最新
    • 标签
  • DBA
    • 最新
    • 标签
  • Computer
    • 最新
    • 标签
  • Coding
    • 最新
    • 标签
主页 / computer / 问题 / 1848627
Accepted
JKHA
JKHA
Asked: 2024-07-10 18:07:38 +0800 CST2024-07-10 18:07:38 +0800 CST 2024-07-10 18:07:38 +0800 CST

如何计算命令行的平均 CPU 和内存使用情况?

  • 772

UNIX 中的以下命令:

time sh -c julia src/script_etudedtsi.jl 80 20 100 366 5 10 0 1 0 0 1 1

返回我的 Julia 脚本的已用时间。

我试过:

pidstat -u -p $(pgrep -f julia src/script_etudedtsi.jl 80 20 100 366 5 10 0 1 0 0 1 1) 1
pgrep: only one pattern can be provided

如何获取经过时间内执行同一条命令的平均 CPU 和内存使用情况?

command-line
  • 1 1 个回答
  • 423 Views

1 个回答

  • Voted
  1. Best Answer
    JayCravens
    2024-07-10T19:30:16+08:002024-07-10T19:30:16+08:00

    该time命令可以提供生成基本统计数据的方法。

    time -v your_command
    

    运行后,time将显示“用户”和“系统”时间,代表程序使用的 CPU 时间。重复此操作多次。通过平均计算“平均”CPU 使用率。将值相加,然后除以 CPU 核心数。

    对于内存使用情况,time输出“最大驻留集大小”,以千字节为单位给出“峰值使用情况”。取平均值。如果需要字节,请将总数乘以 1024。

    您可以使用psbash 脚本进行轮询:

    run_command.sh使用以下内容创建:

    #!/bin/bash
    
    "$@" &
    pid=$!
    echo $pid > /tmp/command.pid
    wait $pid
    rm /tmp/command.pid
    

    使可执行文件:chmod +x run_command.sh

    创建calc_mean_usage.sh方式:

    #!/bin/bash
    
    show_help() {
        echo "Usage: $0 [OPTION]... COMMAND"
        echo "Calculate mean CPU and memory usage."
        echo "    --help                       Shows this help"
        echo "    --log=[file]                 Log polling intervals"
        echo "    --show                       Output to console"
        exit 1
    }
    
    log_file=""
    show_output=false
    
    # Parse options
    while [[ $# -gt 0 ]]; do
        case "$1" in
            --help)
                show_help
                ;;
            --log=*)
                log_file="${1#*=}"
                shift
                ;;
            --show)
                show_output=true
                shift
                ;;
            --)
                shift
                break
                ;;
            *)
                break
                ;;
        esac
    done
    
    if [ $# -eq 0 ]; then
        show_help
    fi
    
    # Assign the command to an array
    your_command=("$@")
    
    # Run the wrapper and capture PID to a file
    ./run_command.sh "${your_command[@]}" &
    
    # Wait for file creation and read the PID
    while [ ! -f /tmp/command.pid ]; do
        sleep 0.1
    done
    pid=$(cat /tmp/command.pid)
    
    samples=0
    interval=1
    
    # Initialize data arrays
    cpu_usages=()
    mem_usages=()
    
    # Collect CPU and memory usage samples for duration
    while kill -0 $pid 2> /dev/null; do
        cpu_usage=$(ps -o %cpu= -p $pid)
        mem_usage=$(ps -o %mem= -p $pid)
    
        cpu_usages+=($cpu_usage)
        mem_usages+=($mem_usage)
    
        if $show_output; then
            echo "CPU Usage: $cpu_usage%, Memory Usage: $mem_usage%"
        fi
    
        if [ -n "$log_file" ]; then
            echo "CPU Usage: $cpu_usage%, Memory Usage: $mem_usage%" >> "$log_file"
        fi
    
        sleep $interval
        samples=$((samples + 1))
    done
    
    # Calculate mean CPU and memory usage
    total_cpu=0
    total_mem=0
    
    for cpu in "${cpu_usages[@]}"; do
        total_cpu=$(echo "$total_cpu + $cpu" | bc)
    done
    
    for mem in "${mem_usages[@]}"; do
        total_mem=$(echo "$total_mem + $mem" | bc)
    done
    
    mean_cpu=$(echo "scale=2; $total_cpu / $samples" | bc)
    mean_mem=$(echo "scale=2; $total_mem / $samples" | bc)
    
    echo "Mean CPU Usage: $mean_cpu%, Mean Memory Usage: $mean_mem%"
    
    exit 0
    

    用法:
    ./calc_mean_usage.sh bash -c "julia src/script_etudedtsi.jl 80 20 100 366 5 10 0 1 0 0 1 1"

    这将在后台运行your_command并在your_command的整个运行时间内每秒轮询一次,并将每秒输出 CPU 和内存使用情况,然后计算平均值。

    这样,您可以设置任何轮询间隔,而不必担心它会停止。

    • 6

相关问题

  • 如何使用键盘快捷键在 macOS 终端中选择一行?

  • 如何在 macOS 的终端中切换切换(连续性)?

  • Windows 上是否有可以通过 CMD 或 Powershell 提供加密安全随机字节的设备或可执行文件?

  • Python 的“pass”参数的批处理等价物是什么?

  • 禁用后无法启用 Microsoft Print to PDF

Sidebar

Stats

  • 问题 205573
  • 回答 270741
  • 最佳答案 135370
  • 用户 68524
  • 热门
  • 回答
  • Marko Smith

    如何减少“vmmem”进程的消耗?

    • 11 个回答
  • Marko Smith

    从 Microsoft Stream 下载视频

    • 4 个回答
  • Marko Smith

    Google Chrome DevTools 无法解析 SourceMap:chrome-extension

    • 6 个回答
  • Marko Smith

    Windows 照片查看器因为内存不足而无法运行?

    • 5 个回答
  • Marko Smith

    支持结束后如何激活 WindowsXP?

    • 6 个回答
  • Marko Smith

    远程桌面间歇性冻结

    • 7 个回答
  • Marko Smith

    子网掩码 /32 是什么意思?

    • 6 个回答
  • Marko Smith

    鼠标指针在 Windows 中按下的箭头键上移动?

    • 1 个回答
  • Marko Smith

    VirtualBox 无法以 VERR_NEM_VM_CREATE_FAILED 启动

    • 8 个回答
  • Marko Smith

    应用程序不会出现在 MacBook 的摄像头和麦克风隐私设置中

    • 5 个回答
  • Martin Hope
    Vickel Firefox 不再允许粘贴到 WhatsApp 网页中? 2023-08-18 05:04:35 +0800 CST
  • Martin Hope
    Saaru Lindestøkke 为什么使用 Python 的 tar 库时 tar.xz 文件比 macOS tar 小 15 倍? 2021-03-14 09:37:48 +0800 CST
  • Martin Hope
    CiaranWelsh 如何减少“vmmem”进程的消耗? 2020-06-10 02:06:58 +0800 CST
  • Martin Hope
    Jim Windows 10 搜索未加载,显示空白窗口 2020-02-06 03:28:26 +0800 CST
  • Martin Hope
    andre_ss6 远程桌面间歇性冻结 2019-09-11 12:56:40 +0800 CST
  • Martin Hope
    Riley Carney 为什么在 URL 后面加一个点会删除登录信息? 2019-08-06 10:59:24 +0800 CST
  • Martin Hope
    zdimension 鼠标指针在 Windows 中按下的箭头键上移动? 2019-08-04 06:39:57 +0800 CST
  • Martin Hope
    jonsca 我所有的 Firefox 附加组件突然被禁用了,我该如何重新启用它们? 2019-05-04 17:58:52 +0800 CST
  • Martin Hope
    MCK 是否可以使用文本创建二维码? 2019-04-02 06:32:14 +0800 CST
  • Martin Hope
    SoniEx2 更改 git init 默认分支名称 2019-04-01 06:16:56 +0800 CST

热门标签

windows-10 linux windows microsoft-excel networking ubuntu worksheet-function bash command-line hard-drive

Explore

  • 主页
  • 问题
    • 最新
    • 热门
  • 标签
  • 帮助

Footer

AskOverflow.Dev

关于我们

  • 关于我们
  • 联系我们

Legal Stuff

  • Privacy Policy

Language

  • Pt
  • Server
  • Unix

© 2023 AskOverflow.DEV All Rights Reserve