Giter Club home page Giter Club logo

memorygraph's Introduction

memorygraph

MATLAB/octave unix tool to record true MATLAB/octave memory and CPU usage vs time

Alex Barnett 1/30/18-3/14/18; version of 10/15/18. With improvements by Joakim Anden, Jeremy Magland.

One point of this tool is to be able to extract reliable true peak RAM usage by a MATLAB/ocatve code, without the need to continuously human-read (h)top. The example graphs above show RAM and CPU usage vs time, when executing the MATLAB commands

pause(1)
a = randn(1,2e8);              % fills 1.6GB RAM. randn is single-threaded
pause(1)
b = exp(a);                    % fills another 1.6GB. Uses 8 threads
clear a                        % frees up the first 1.6GB
pause(1)
clear b                        % frees up the remaining 1.6GB
pause(1)

The pause commands are to demonstrate that the graph has the correct timing, and to separate the two calculation phases for clarity. Here's how you modify this code, by inserting five lines of memorygraph calls, to produce the above graphs (two of these lines insert the red fiduciary labels):

opts.dt = 0.1; memorygraph('start',opts);
pause(1)
a = randn(1,2e8);              % fills 1.6GB RAM. randn is single-threaded
memorygraph('label','randn done');
pause(1)
b = exp(a);                    % fills another 1.6GB. Uses 8 threads
memorygraph('label','exp done');
clear a               	       % frees up the first 1.6GB
pause(1)
clear b			       % frees up the remaining 1.6GB
pause(1)
memorygraph('plot');
memorygraph('done');

Note that the first label is spot-on in terms of timing, but the second comes early; we don't know why. Labels are therefore only approximate tools, but are very useful to tag multiple calculation steps. (Note the above was done on a laptop with i7-7700HQ CPU.)

Installation

Place the codes memorygraph.m and vline.m somewhere in your MATLAB/octave path.

Usage

Firstly, see the simple example above. For more detail, from MATLAB/octave, to start a graph (starts recording to a temp file in current directory):

memorygraph('start');

This samples every 1 sec. If you want more often use, eg

opts.dt = 0.1; memorygraph('start',opts);

This, or smaller dt, may cause top to slow down the CPU.

To plot the graph recorded so far (this may be done multiple times):

memorygraph('plot');

To read off data recorded so far (this may be done multiple times):

[bytes est_times cpu_times cpu_usages labelstrings labeltimes] = memorygraph('get');

This gives outputs:

bytes : total RAM used by MATLAB/octave, in bytes
est_times : estimated clock time in secs since graph started
cpu_times : MATLAB/octave CPU time used (counting all threads) reported by top
cpu_usages : current percentage CPU usage by MATLAB/octave at each time
labelstrings : cell array of strings the user has added
labeltimes : array of times since starting, in sec, for added labels

To read off, as above, and also plot a graph:

[bytes est_times cpu_times cpu_usages labelstrings labeltimes] = memorygraph('plot');

To plot the data from the outputs (rather than read from the temp file):

memorygraph('plot',bytes,est_times,cpu_times,cpu_usages,labelstrings,labeltimes);

To add a test string 'abc' which will appear alongside a vertical red line:

memorygraph('label','abc');

To clean up (kills the spawned processes and removes the temp file):

memorygraph('done');

Without args: does a self-test, produces the graph shown above.

Advanced usage

Temp files simply have each row as a filtered output from the unix top command, called at successive instants. They are therefore somewhat human readable. To get or plot an old temp file (without labels), use, for example:

memorygraph('plot','/tmp/memorygraph_f51f2aff.tmp');

Notes:

  • Linux/unix only. Tested in MATLAB and octave.
  • Hard-coded temp-file location, a randomly generated filename in the current directory. There is a miniscule probability of overwriting the tempfile of another program running memorygraph on the same filesystem.
  • The top display config must be standard (no changes to /etc/toprc nor ~/.toprc).
  • James Jun has pointed out that there is a similar tool in python: https://github.com/parikls/mem_usage_ui

Issues:

  • How do we get actual timestamps without estimating? (spawn 2nd process which appends to same temp file at time intervals?)
  • Why are label time-stamps sometimes off?

Changes:

  • Joakim and Jeremy helped fix the correct PID for the MATLAB/octave instance and the correct PID to kill.

  • Added plot and label methods, including plot from previous output.

  • Made randomized temp filename, and option to get or plot any temp filename.

memorygraph's People

Contributors

ahbarnett avatar janden avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

memorygraph's Issues

memory graph issue

Dear Alex I am using your memory graph code under Mac Matlab and I am experiencing some problems. Warning: we waited, but no memorygraph data found in memorygraph_9878fdd5.tmp, or file has been erased!
/bin/bash: line 0: kill: (58741) - No such process
no data found! the self-test has failed. Did some alien intervention erase the tempfile?

memorygraph

Do you know exactly where the issue is arising from?
thanks in advance

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.