LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   flag based Mem usage checking within MATLAB run on linux (https://www.linuxquestions.org/questions/linux-newbie-8/flag-based-mem-usage-checking-within-matlab-run-on-linux-4175450890/)

manants 02-19-2013 11:57 PM

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

descendant_command 02-20-2013 12:01 AM

Doesn't sound urgent.
You should use a more descriptive title.

shivaa 02-20-2013 12:02 AM

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

manants 02-20-2013 12:13 AM

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

spazticclown 02-20-2013 12:46 AM

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.

shivaa 02-20-2013 01:01 AM

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.

joe_2000 02-20-2013 03:39 AM

Quote:

Originally Posted by manants (Post 4895698)
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 (Post 4895698)
I want a cmd triggered solution

You may want to try the free command?

manants 02-20-2013 10:13 AM

please dont post saying try top or free
 
Hi,

@joe_2000 and spazticclown: Had got that :hattip: 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 :newbie: 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

shivaa 02-20-2013 10:33 AM

Quote:

Originally Posted by manants (Post 4896011)
@shivaa: I am a :newbie: 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.

manants 02-20-2013 11:29 AM

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

suicidaleggroll 02-20-2013 11:54 AM

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.

shivaa 02-20-2013 11:55 AM

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.

manants 02-22-2013 12:37 AM

hopefully will close soon!
 
@suicidaleggroll: Hi am :newbie:,
please give comments for the script!:confused: 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:study:

@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.

chrism01 02-22-2013 12:50 AM

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

suicidaleggroll 02-22-2013 10:40 AM

Quote:

Originally Posted by manants (Post 4897225)
@suicidaleggroll: Hi am :newbie:,
please give comments for the script!:confused: 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:study:

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


All times are GMT -5. The time now is 05:42 PM.