Rami
Rami

Reputation: 8314

Execute a command in shell and print out the summary (time + memory)?

Sorry if the question is basic, is there options in shell to show how much time and how much memory (maximum memory occupied) the execution of a command has took?

Example: I want to call a binary file as follow: ./binary --option1 option1 --option2 option2

After the execution of this command I would like to know how much time and memory this command took.

Thanks

Upvotes: 1

Views: 338

Answers (2)

erik258
erik258

Reputation: 16304

There are several files in /proc that might be simpler than using a profiler, assuming you know the PID of the process in which you're interested.

Of primary interest is /proc/$PID/status which lists ( among other things ) Virtual Memory Peak and Size ( VmPeak, VmSize respectively), and Resident "High Water Mark" and current sets ( VmHWM, VnRSS respectively )

I set up a simple C program to grab memory and then free it and then watched the file in proc corresponding to that program's PID, and it seemed to verify the manpage.

see man proc for a complete list of files that may interest you.

Here's the command line and program I used for the test:

Monitored with

PID="<program's pid>"
watch "cat /proc/$PID/status | grep ^Vm"

( compile with gcc -o grabmem grabmem.c -std=c99 )

#include <sys/types.h>
#include <stdlib.h>
#include <unistd.h>
#include <stdio.h>

#define INTNUM ( 1024 * 1024 )
#define PASSES 16
int main(){
    int* mems[16];
    int cur = 0;
    while( mems[cur] = (int*)calloc( sizeof( int ), INTNUM ) ){
        for( int i = 0; i < INTNUM; i++)
            mems[cur][i] = rand();
        printf("PID %i Pass %i: Grabbed another %lu of mem.\n",
            (int)getpid(),
            cur,
            sizeof(int) * INTNUM
        );
        sleep( 1 );
        cur++;
        if( cur >= 16 ){
            printf("Freeing memory...");
            for( cur = (PASSES - 1); cur >= 0; cur-- ){
                free(mems[cur] );
                mems[cur] = NULL;
            }
            cur = 0;
            printf("OK\n");
        }
    }
    fprintf(stderr, "Couldn't calloc() memory.\n");
    exit( -1 );
}

Upvotes: 0

thkala
thkala

Reputation: 86353

The time(1) command, either as a separate executable or as a shell built-in, can be used to measure de the time used by a program, both in terms of wallclock time and CPU time.

But measuring the memory usage of a program, or even agreeing how to define it, is a bit different. Do you want the sum of its allocations? The maximum amount of memory allocated at a single moment? Are you interested in what the code does, or in the program behavior as a whole, where the memory allocator makes a difference? And how do you consider the memory used by shared objects? Or memory-mapped areas?

Valgrind may help with some memory-related questions, but it is more of a development tool, rather than a day-to-day system administrator tool. More specifically the Massif heap profiler can be used to profile the memory usage of an application, but it does have a measurable performance impact, especially with stack profiling enabled.

Upvotes: 3

Related Questions