LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices



Reply
 
Search this Thread
Old 02-20-2013, 12:57 AM   #1
manants
LQ Newbie
 
Registered: Feb 2013
Posts: 5

Rep: Reputation: Disabled
flag based Mem usage checking within MATLAB run on linux


I need to check memory usage only while running a particular code in MATLAB.

I need to do the below sequence of events in MATLAB

matlab commands (4 code initializing)
start checking and logging mem usage (Can be done using system command)
main matlab commands
stop checking and logging mem usage (Can be done using system command)

I tried the following:
Using watch I can keep doing it infinitely. However it requires a manual stop!
And I cant predict when it will stop.

I tried creating a shell script with a flag and then tried to update the flag on another terminal. That to didnt work!
flag=0
while [flag == 0]
do
run cmd
done

and in a terminal tried flag=1


Please help! Will there be a mail sent to my mail id? if any one posts a solution?

Regards,
Manants

Last edited by manants; 02-20-2013 at 11:00 AM. Reason: Title made more apt
 
Old 02-20-2013, 01:01 AM   #2
descendant_command
Member
 
Registered: Mar 2012
Posts: 908

Rep: Reputation: 203Reputation: 203Reputation: 203
Doesn't sound urgent.
You should use a more descriptive title.
 
Old 02-20-2013, 01:02 AM   #3
shivaa
Senior Member
 
Registered: Jul 2012
Location: Grenoble, Fr.
Distribution: Sun Solaris, RHEL, Ubuntu, Debian 6.0
Posts: 1,797
Blog Entries: 4

Rep: Reputation: 286Reputation: 286Reputation: 286
First thing is, it's not urgent for us at all, so don't demand any urgency, but use a specifc subject line so more volunteer can interact with your issue.

Second, if you're concerned about memory usage by MATLAB, then try commands like top, which will show you memory usage by it until it gets finished.
Code:
~$ top
 
Old 02-20-2013, 01:13 AM   #4
manants
LQ Newbie
 
Registered: Feb 2013
Posts: 5

Original Poster
Rep: Reputation: Disabled
Exclamation Need help (URGENT TO POSTER)

@descendant and @shiva:
I am new here! The post was urgent to me! I am not sure why it or any message would be urgent to you (experienced user/solution provider)?
Do let me know where and how to change the title.
I have tried top but it keeps executing infinitely I want a cmd triggered solution. I tried what made sense using Google and when it failed have posted on this forum!

top | grep MATLAB ( This runs forever, I want to make it cmd triggered)

Regards,
Manants
 
Old 02-20-2013, 01:46 AM   #5
spazticclown
Member
 
Registered: Sep 2010
Distribution: Fedora, Android, CentOS
Posts: 91
Blog Entries: 2

Rep: Reputation: 21
Best to run the top command in another terminal, then launch MATLAB (or anything else you need to run) and watch what happens in the top terminal. When the MATLAB terminal completes top will still be running, you should see a good deal of useful information from top (q to quit).

Secondly you could run in a second terminal:
Code:
while true; do wait 5; free; done
This will run the free command every five seconds until broken [ctl+c], while you run MATLAB in another terminal you can monitor the memory used via this terminal.

There are probably a hundred other options too, hope this helps.
 
Old 02-20-2013, 02:01 AM   #6
shivaa
Senior Member
 
Registered: Jul 2012
Location: Grenoble, Fr.
Distribution: Sun Solaris, RHEL, Ubuntu, Debian 6.0
Posts: 1,797
Blog Entries: 4

Rep: Reputation: 286Reputation: 286Reputation: 286
The top command should not be run continuously. Else it would start consuming system resources. But run it, check mem. usage by various processes, and use q key to quit. In this way, you can keep monitoring processees and their system resources consumptions.

Also let's know which disto you're using - RHEL? Or any other?

BTW, To change thread title, click on Edit at right bottom of your post, and on next page you can see a box for editing thread title.

Last edited by shivaa; 02-20-2013 at 02:02 AM.
 
Old 02-20-2013, 04:39 AM   #7
joe_2000
Member
 
Registered: Jul 2012
Location: Aachen, Germany
Distribution: Crunchbang, Debian, currently trying Slackware now that #! is dying
Posts: 479

Rep: Reputation: 134Reputation: 134
Quote:
Originally Posted by manants View Post
Do let me know where and how to change the title.
What they meant is you should put something like "need command to check memory usage" so that people can tell what's your question without having to open your post.
Quote:
Originally Posted by manants View Post
I want a cmd triggered solution
You may want to try the free command?
 
Old 02-20-2013, 11:13 AM   #8
manants
LQ Newbie
 
Registered: Feb 2013
Posts: 5

Original Poster
Rep: Reputation: Disabled
please dont post saying try top or free

Hi,

@joe_2000 and spazticclown: Had got that I've, using google, created a similar script to what spazticclown has given as possible solution!
My problem is I dont wont to use Ctrl+C. As this means it requires manual monitoring!

I want to make/use an automated process and not keep monitoring! I think its possible in Linux!

@shivaa: I am a Not sure of all the jargon - disto you're using - RHEL? Or any other?
I have changed the title, thanks!

I repeat am looking for a solution where say I run only one process (MATLAB) and in the background using top/watch start monitoring the mem usage only when a particular routine in it is run, by activating a flag before and dropping the flag immediately after!

Regards,
Manants
 
Old 02-20-2013, 11:33 AM   #9
shivaa
Senior Member
 
Registered: Jul 2012
Location: Grenoble, Fr.
Distribution: Sun Solaris, RHEL, Ubuntu, Debian 6.0
Posts: 1,797
Blog Entries: 4

Rep: Reputation: 286Reputation: 286Reputation: 286
Quote:
Originally Posted by manants View Post
@shivaa: I am a Not sure of all the jargon - disto you're using - RHEL? Or any other?
I have changed the title, thanks!

I repeat am looking for a solution where say I run only one process (MATLAB) and in the background using top/watch start monitoring the mem usage only when a particular routine in it is run, by activating a flag before and dropping the flag immediately after!
Thanks for modifying the subject line.

Distro is short for distribution, means which operating system you're using - Redhat Linux, Solaris, Debian, Suse, Mint, Slackware...? Let's know it.
Code:
~$ uname -a
~$ cat /etc/issue
~$ cat /etc/redhat-release
Well, as far as I am concerned, top is the only in-built utility with RH Linux. And as I already said above, you can time to time keep checking the resource consumption for your MATLAB application until it gets finished.

Launch MATLAB, and just side by side open a new terminal and invoke top command. That's it. Keep checking top output.

Last edited by shivaa; 02-22-2013 at 02:06 AM.
 
Old 02-20-2013, 12:29 PM   #10
manants
LQ Newbie
 
Registered: Feb 2013
Posts: 5

Original Poster
Rep: Reputation: Disabled
continuing with shiva

Hi,

Thanks for the prompt reply!
output for uname -a is a big line
its a redhat machine had x86_64.
For the other two it says permission denied!

Even I am using top! However the only point is can I stop it apart from manually typing Ctrl+C
Currently am doing that!

Regards,
Manants
 
Old 02-20-2013, 12:54 PM   #11
suicidaleggroll
Senior Member
 
Registered: Nov 2010
Location: Colorado
Distribution: OpenSUSE, CentOS
Posts: 3,422

Rep: Reputation: 1239Reputation: 1239Reputation: 1239Reputation: 1239Reputation: 1239Reputation: 1239Reputation: 1239Reputation: 1239Reputation: 1239
If you want it log memory usage and then exit when the matlab script exits, then don't use watch or top, but write your own script to check that the matlab code is still running and then call free (or similar) every X seconds and log the result. Both watch and top are intended to be used interactively, not within a script, but that doesn't mean you can't do something like:

Code:
keepgoing=1
while [[ $keepgoing -eq 1 ]]; do
   ps -p $PID > /dev/null
   if [[ $? -eq 1 ]]; then
      keepgoing=0
   else
      echo $(date +%H:%M:%S.%N) $(free | awk 'NR==3 {print $3}') >> memlog
      sleep 1
   fi
done
Assuming the PID for your process is in the variable "PID"

This will of course keep track of the total memory usage on the system, not just the memory used by that matlab process, but it could be adapted as necessary.

Last edited by suicidaleggroll; 02-20-2013 at 12:59 PM.
 
Old 02-20-2013, 12:55 PM   #12
shivaa
Senior Member
 
Registered: Jul 2012
Location: Grenoble, Fr.
Distribution: Sun Solaris, RHEL, Ubuntu, Debian 6.0
Posts: 1,797
Blog Entries: 4

Rep: Reputation: 286Reputation: 286Reputation: 286
Alright. Perhaps my guess was correct, you're using RedHat Linux. But /etc/redhat-release should give you result, not permission deinied error.

BTW, you can now use top command as:
Code:
~$ pgrep matlab           # To check pid of matlab
1234                      # It is pid of the matlab process, I just give example of 1234
~$ ps -Af | grep matlab   # Or use this cmd to check pid of matlab

~$ top -p 1234            # Run top only for that pid i.e. matlab
~$ top -u username        # To check processes by a particular user
For more info. and command line options on top, see here.

Last edited by shivaa; 02-20-2013 at 12:57 PM.
 
Old 02-22-2013, 01:37 AM   #13
manants
LQ Newbie
 
Registered: Feb 2013
Posts: 5

Original Poster
Rep: Reputation: Disabled
hopefully will close soon!

@suicidaleggroll: Hi am ,
please give comments for the script! As well as how $3 and other variables are being used but then not mentioned in the script!
This will percolate down to a group of students!
Also can the variable "keepgoing" be changed from another script! I am looking for such a scenario.
And any books/links on shell scripting please! Thin and simple

@shivaa: I am still getting "bash: /etc/redhat-release: Permission denied" message
I can use top as you said, pipe it for logging! But I want to stop logging after a function/routine has finished executing! Is that possible? If so please let me know.
 
Old 02-22-2013, 01:50 AM   #14
chrism01
Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.6, Centos 5.10
Posts: 16,401

Rep: Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081
Please post the actual output(!) of the following cmds
Code:
uname -a
cat /etc/*release*
Do not just say 'output for uname -a is a big line'; that's insufficient.

bash scripting
http://rute.2038bug.com/index.html.gz
http://tldp.org/LDP/Bash-Beginners-G...tml/index.html
http://www.tldp.org/LDP/abs/html/

The answer by suicidaleggroll is definitely adaptable to your requirements.
You need to read up on awk http://www.grymoire.com/Unix/Awk.html
 
Old 02-22-2013, 11:40 AM   #15
suicidaleggroll
Senior Member
 
Registered: Nov 2010
Location: Colorado
Distribution: OpenSUSE, CentOS
Posts: 3,422

Rep: Reputation: 1239Reputation: 1239Reputation: 1239Reputation: 1239Reputation: 1239Reputation: 1239Reputation: 1239Reputation: 1239Reputation: 1239
Quote:
Originally Posted by manants View Post
@suicidaleggroll: Hi am ,
please give comments for the script! As well as how $3 and other variables are being used but then not mentioned in the script!
This will percolate down to a group of students!
Also can the variable "keepgoing" be changed from another script! I am looking for such a scenario.
And any books/links on shell scripting please! Thin and simple
What do you need for a comment? It's all pretty straight forward except maybe for the awk command.

ps -p $PID runs ps on that PID. The output is being dumped to /dev/null because we don't care about it, we're only interested in the exit status. $? grabs the exit status of the last command (ps). If the exit status is 0, then the process was running. If the exit status is 1, then the process was not running, so we break out of the loop.

Echo just prints to stdout
date +%H:%M:%S.%N prints the current time in HH:MM:SS.SSSSSSSSS format for timestamping your log file

free prints the memory usage of the system (used, available, cached, etc.). We're piping the output of free into awk to trim it up. NR==3 tells awk to only perform the next command on the 3rd record (line). print $3 tells awk to print the 3rd column. So essentially awk is taking the output of free and printing only the 3rd line and 3rd column, which is the total memory usage on the system +/- buffers and cache.

All of the output from echo is then being piped into a file called memlog so you have an actual record of the memory usage as a function of time


The script isn't intended to be used verbatim, it's just an example of one way you could log memory usage on the system over the lifetime of a process. You'll still need to fill in the code to get the PID, possibly modify the timestamp format, maybe modify what you're actually logging (if you want memory usage by Matlab rather than total memory usage on the system), etc.

chrism01 posted some good links on bash scripting

Last edited by suicidaleggroll; 02-22-2013 at 11:43 AM.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
boot sector and lilo collapse !!!!! URGENT URGENT URGEN !!!!! frelihm Linux - Software 21 12-02-2009 11:21 AM


All times are GMT -5. The time now is 10:48 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration