如何查看 Unity 使用了多少内存/CPU

如何查看 Unity 使用了多少内存/CPU

我正在尝试查看整个 Unity 桌面使用了多少内存和 CPU。为此,我尝试查看 Unity 进程,但由于它分为许多部分,因此变得很困难。我想查看整个 Unity 消耗了多少内存(和/或 CPU)。是否有一个参数可以查看这一点,是否有一个脚本可以读取所有 Unity 组件并将它们相加。可选择包括 Unity 的所有依赖包,如 Compiz。

答案1

刚刚写了完成这个工作的python脚本:

#!/usr/bin/env python

import os, sys

primary_processes = { "unity-panel-service", "unity-applications-daemon", "unity-files-daemon", "unity-gwibber-daemon", "unity-music-daemon", "unity-shopping-daemon", "unity-lens-video", "hud-service", "zeitgeist-fts", "zeitgeist-datahub", "zeitgeist-daemon", "zeitgeist-fts" }

secondary_processes = { "lightdm", "X", "gnome-session", "gnome-settings-daemon", "gnome-keyring-daemon", "compiz", "pulseaudio", "gnome-fallback-mount-helper", "nautilus", "gconf-helper", "gconfd-2", "gtk-window-decorator", "indicator-printers-service", "indicator-sound-service", "indicator-session-service", "indicator-messages-service", "indicator-application-service", "ubuntu-geoip-provider", "gnome-screensaver", "gnome-pty-helper", "update-notifier", "deja-dup-monitor" }

debugOn=False

def getPID(curProc):
    try:
        return os.popen("pidof "+curProc).read().strip()
    except:
        return "0"

def getProcMemUsage(curProcID):
    """
    Returns curProc's mem usage of curProcID in Kbytes
    """
    if(curProcID=="0" or curProcID==""):
        return 0

    try:
        if debugOn:
            print curProcID+": "+os.popen("pmap -x "+curProcID+" | tail -1 | awk '{print $3, $2}'").read().strip()

        return int(os.popen("pmap -x "+curProcID+" | tail -1 | awk '{print $3}'").read().strip())
    except:
        return 0

def getProcCpuUsage(curProcID):
    """
    Returns curProc's % cpu usage
    """

    if(curProcID=="0" or curProcID==""):
        return 0

    try:
        percentage=os.popen("ps aux | grep "+curProcID+" | grep -v grep | awk '{print $3}'").read().strip()
        if debugOn:
            print curProcID+": "+percentage+"%"
        return float(percentage)
    except:
        return 0

def KbToMb(curKB):
    return "{0:.2f}".format(curKB/1024.0)


def main():
    sumKB=0
    sumCPU=0

    if "-d" in sys.argv:
        global debugOn
        debugOn=True

    if "-a" in sys.argv:
        for proc in primary_processes :
            procpid=getPID(proc)
            sumKB += getProcMemUsage(procpid)
            sumCPU += getProcCpuUsage(procpid)
        for proc in secondary_processes :
            procpid=getPID(proc)
            sumKB += getProcMemUsage(procpid)
            sumCPU += getProcCpuUsage(procpid)
    else:
        for proc in primary_processes :
            procpid=getPID(proc)
            sumKB += getProcMemUsage(procpid)
            sumCPU += getProcCpuUsage(procpid)

    if "-c" in sys.argv:
        print str(KbToMb(sumKB))+" MB"
    else:
        print str(sumKB)+" KB"
    print str(sumCPU)+"% CPU"

main()

使用情况:只需运行它即可告诉您 Unity 的使用情况(内存使用情况 + CPU 百分比)

  • -c -> 将 Kb 转换为 Mb

  • -d -> 调试已开启

  • -a -> 也为辅助进程做工作

例如

./script.py -c -d

次要和主要进程是脚本顶部的全局变量,因此请随意编辑脚本并包含您感兴趣的进程。

附言:我发现使用 的进程的内存使用量pmap -x有点……过多(它说我的 X 服务器占用了 70 MB),但我想它的开发人员比我知道的更多。

相关内容