Take the 2-minute tour ×
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free, no registration required.

I am trying to see how much memory and CPU the whole Unity desktop is using. For this I tried looking at the Unity process but since it is divided in many parts it became difficult. I want to see how much Unity as a whole, is consuming in memory (and/or CPU). Is there a parameter to see this, a script to read all Unity components and sum them up. Optionally including all dependent packages of Unity like Compiz.

share|improve this question
    
Just curious, what does htop give you? –  drN Nov 27 '12 at 20:12
    
htop, top and ps show the Unity processes, but I want a way to just see the overall memory/cpu usage using all components that form Unity. –  Luis Nov 27 '12 at 20:27
    
Is it possible for you to use grep, awk, and stuff? It'll be complicated by the fact that some things that use a lot of CPU are dependencies of Unity but also are dependencies of other things. –  hexafraction Nov 27 '12 at 20:32
    
I was thinking, if they do not depend completely on Unity to function, then they are not counted for since they are also depended on other things. So am trying to focus on only elements that are entirely Unity and are not needed by others. –  Luis Nov 27 '12 at 20:38
    
Unless identifying the Unity components is of interest, just boot up in text mode (edit the grub linux line to replace "quiet splash" with "text", and check the free memory. Then start adding back pieces like the X server, and your favorite light weight desktop, taking memory usage snapshots along the way. –  ubfan1 Nov 27 '12 at 21:22

1 Answer 1

up vote 1 down vote accepted

Just wrote that python script that does the job:

#!/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()

Usage: Just run it to tell you what unity uses (memory usage + cpu %)

  • -c -> convert Kb to Mb

  • -d -> debugging is on

  • -a -> also do the work for secondary processes

E.g.

./script.py -c -d

The secondary and primary processes are global variables on the top of the script, so feel free to edit the script and include the processes that you are interested in.

PS: I find the memory usage of a process by using pmap -x, which I find to be a little... excessive (it says that my X server eats up to 70 MB), but I guess the developers of it know more than I do.

share|improve this answer
    
running the script gets me this: 3611.36 MB 0.0% CPU I am pretty sure Unity is not using 3.6 GB of RAM. Maybe it is accumulating all child processes. Apart from this, am impressed with the code. –  Luis May 6 '13 at 20:50
    
@LuisAlvarado I will take a look at it again, memory measuring is quite a hard process (stackoverflow.com/questions/131303/…) –  hakermania May 7 '13 at 9:49
    
I can't seem to find something simple enough. smem seems to be a good option, but for some reason it shows too few processes... –  hakermania May 7 '13 at 11:02

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.