Results 1 to 7 of 7

Thread: Getting CPU load, memory info etc.

  1. #1
    QCar Creator Jirka Jirout's Avatar
    Join Date
    Jul 2005
    Location
    Netherlands
    Posts
    590

    Getting CPU load, memory info etc.

    Is there a way of getting the system information using ObjC/Cocoa or AppleScript? I need to get the info that is normally provided by top in logging mode, but there seems to be a bug in the OS X that prevents me from re-directing grepped output to a file.

    top -l 0 > test.txt - works
    top -l 0 | grep Load - works
    top -l 0 | grep Load > test.txt - DOES NOT WORK :-(

  2. #2
    CarFrontEnd Creator iamgnat's Avatar
    Join Date
    Jul 2004
    Location
    NoVA
    Posts
    867
    Sort of (it C code), I don't have it with me though and I can't remember the detail that the example goes into.

    If you don't already have it, Amit Singh's Mac OSX Internals is great for this type of info.

    -dave

    EDIT: The other option is to go grab the source code to top and see how they do it

    EDIT 2: Your -l argument is the problem. Change it from 0 to 1. The issue is that with 0, it keeps outputting and redirections don't like that. If that is what you really want (e.g. so you aren't constantly running the top command), then execute the command through a file handle and run it through a select loop to process the data.
    My pathetic worklog.
    CarFrontEnd (now it's own sub-forum!!!!)

  3. #3
    QCar Creator Jirka Jirout's Avatar
    Join Date
    Jul 2005
    Location
    Netherlands
    Posts
    590
    I am using an NSTask to run this, getting it's standard output. Unfortunately, there is nothing written to standard output. I have a similar script to monitor the disk space (also constantly outputting something every few seconds) and that one works fine:

    Code:
    #!/bin/sh
    
    while [ 1 ]
    	do
    		df -l | grep \/
    		sleep 3
    	done
    But I will try the 1 parameter for -l and using a loop in my own script.

  4. #4
    CarFrontEnd Creator iamgnat's Avatar
    Join Date
    Jul 2004
    Location
    NoVA
    Posts
    867
    Quote Originally Posted by Jirka Jirout View Post
    I am using an NSTask to run this, getting it's standard output. Unfortunately, there is nothing written to standard output.
    If you are running the NSTask with the -l 0, it is buffering the output until the task completes (which it never will with the 0 argument).

    EDIT: Actually it looks like i'm wrong on that. You should be seeing output to the standardOutput handle from a NSTask object. If you post the snippet in question (launching, looping, etc..), i'll be happy to take a look and see if anything jumps out.

    I can't work up an example now (I can tell you the Perl code off the top of my head, but I don't do it that often in C ), but I will try to remember tonight when I get home.

    -dave
    My pathetic worklog.
    CarFrontEnd (now it's own sub-forum!!!!)

  5. #5
    atl
    atl is offline
    Newbie atl's Avatar
    Join Date
    Nov 2005
    Location
    Germany
    Posts
    39
    HI,

    to get CPU usage and other system related stuff from command line tools, normally you have to put the output of these tools into a pipe and read the pipe from your application. You can't run these tools with parameters for 1 sample (like "top -l 1"), because the start of the tool creates higher cpu usage and the output is forged.

    If you have Dashcode you can create an sample Widget from a template for a meassurement widget (in german called "Messgeraet"). This creates sample code for watching cpu usage in Javascript. But it should be not so difficult to do the same in Cocoa/ObjectC.

    I've done nearly the same via AppleScript. But because of the lack of pipes I had to use a temporary file.

    ByE...

  6. #6
    QCar Creator Jirka Jirout's Avatar
    Join Date
    Jul 2005
    Location
    Netherlands
    Posts
    590
    Well, what I ended up with for now is this:

    Code:
    #!/bin/sh
    
    while [ 1 ]
    	do
    		top -l 1 | grep -e Load -e PhysMem -e QCar
    		sleep 1
    	done
    But the issue with load caused by start of top is certainly a valid one and I have forgotten about it. This could also be the reason why this script always returns 0% as the CPU load for QCar process.

    So I will have to use the top sources from Darwin to build this completely in Cocoa after all...

  7. #7
    atl
    atl is offline
    Newbie atl's Avatar
    Join Date
    Nov 2005
    Location
    Germany
    Posts
    39
    The disadvantage of coding a tool what reads the current CPU usage once and quit is, that it has to wait some time before getting a acceptable value. Because the tool by itself rise the CPU and System usage on start (allocate resources, ...).

    I have used iostat to get the CPU usage. From my AppleScript application I run the command
    Code:
    /usr/sbin/iostat -Iw1 -n0
    and redirect the output into a file on a ramdisk. From this file I read regulary the last line and get the CPU usage from it. The RAM disk will be created on start of the application and destroyed on quit.

    I hope this helps you. The best would be, you redirect the output into a pipe. This should possible in Cocoa/ObjectC, because it's also possible in JavaScript.


    ByE...

Similar Threads

  1. How to Speed up Road Runner
    By JohnWPB in forum RR FAQ
    Replies: 4
    Last Post: 09-15-2007, 02:58 PM
  2. Replies: 19
    Last Post: 12-15-2006, 11:34 PM
  3. Can you load WinXP onto a memory stick?
    By Udonboy in forum General Hardware Discussion
    Replies: 14
    Last Post: 09-06-2004, 09:11 AM
  4. load soft ware to usb hard disk then load to comp
    By tracker92 in forum General Hardware Discussion
    Replies: 1
    Last Post: 08-29-2004, 07:57 PM
  5. for those looking to decrease memory load from xp
    By saikano in forum Software & Software Development
    Replies: 16
    Last Post: 07-18-2004, 07:05 PM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •