Skip to content

Instantly share code, notes, and snippets.

@redliu312
Forked from netj/memusg
Created August 28, 2016 14:47
Show Gist options
  • Select an option

  • Save redliu312/413c5b5ff6f772a7a5ee71e5f45af528 to your computer and use it in GitHub Desktop.

Select an option

Save redliu312/413c5b5ff6f772a7a5ee71e5f45af528 to your computer and use it in GitHub Desktop.

Revisions

  1. @netj netj revised this gist Nov 30, 2015. 1 changed file with 6 additions and 6 deletions.
    12 changes: 6 additions & 6 deletions memusg
    Original file line number Diff line number Diff line change
    @@ -22,30 +22,30 @@
    set -um

    # check input
    [ $# -gt 0 ] || { sed -n '2,/^#$/ s/^# //p' <"$0"; exit 1; }
    [[ $# -gt 0 ]] || { sed -n '2,/^#$/ s/^# //p' <"$0"; exit 1; }

    # TODO support more options: peak, footprint, sampling rate, etc.

    pgid=`ps -o pgid= $$`
    pgid=$(ps -o pgid= $$)
    # make sure we're in a separate process group
    if [ $pgid = $(ps -o pgid= $(ps -o ppid= $$)) ]; then
    if [[ "$pgid" == "$(ps -o pgid= $(ps -o ppid= $$))" ]]; then
    cmd=
    set -- "$0" "$@"
    for a; do cmd+="'${a//"'"/"'\\''"}' "; done
    exec bash -i -c "$cmd"
    fi

    # detect operating system and prepare measurement
    case `uname` in
    case $(uname) in
    Darwin|*BSD) sizes() { /bin/ps -o rss= -g $1; } ;;
    Linux) sizes() { /bin/ps -o rss= -$1; } ;;
    *) echo "`uname`: unsupported operating system" >&2; exit 2 ;;
    *) echo "$(uname): unsupported operating system" >&2; exit 2 ;;
    esac

    # monitor the memory usage in the background.
    (
    peak=0
    while sizes=`sizes $pgid`
    while sizes=$(sizes $pgid)
    do
    set -- $sizes
    sample=$((${@/#/+}))
  2. @netj netj revised this gist Nov 10, 2015. 1 changed file with 16 additions and 1 deletion.
    17 changes: 16 additions & 1 deletion memusg
    Original file line number Diff line number Diff line change
    @@ -4,6 +4,21 @@
    #
    # Author: Jaeho Shin <[email protected]>
    # Created: 2010-08-16
    ############################################################################
    # Copyright 2010 Jaeho Shin. #
    # #
    # Licensed under the Apache License, Version 2.0 (the "License"); #
    # you may not use this file except in compliance with the License. #
    # You may obtain a copy of the License at #
    # #
    # http://www.apache.org/licenses/LICENSE-2.0 #
    # #
    # Unless required by applicable law or agreed to in writing, software #
    # distributed under the License is distributed on an "AS IS" BASIS, #
    # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. #
    # See the License for the specific language governing permissions and #
    # limitations under the License. #
    ############################################################################
    set -um

    # check input
    @@ -43,4 +58,4 @@ monpid=$!


    # run the given command
    exec "$@"
    exec "$@"
  3. @netj netj revised this gist Oct 31, 2011. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion memusg
    Original file line number Diff line number Diff line change
    @@ -13,7 +13,7 @@ set -um

    pgid=`ps -o pgid= $$`
    # make sure we're in a separate process group
    if [ x$pgid = x$(ps -o pgid= $(ps -o ppid= $$)) ]; then
    if [ $pgid = $(ps -o pgid= $(ps -o ppid= $$)) ]; then
    cmd=
    set -- "$0" "$@"
    for a; do cmd+="'${a//"'"/"'\\''"}' "; done
  4. @netj netj revised this gist Oct 31, 2011. 1 changed file with 0 additions and 0 deletions.
    Empty file modified memusg
    100644 → 100755
    Empty file.
  5. @pcmdx pcmdx revised this gist Oct 19, 2011. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion memusg
    Original file line number Diff line number Diff line change
    @@ -13,7 +13,7 @@ set -um

    pgid=`ps -o pgid= $$`
    # make sure we're in a separate process group
    if [ x$pgid = x$(ps -o pgid= $(ps -o ppid= $$)) ]; then
    if [ $pgid = $(ps -o pgid= $(ps -o ppid= $$)) ]; then
    cmd=
    set -- "$0" "$@"
    for a; do cmd+="'${a//"'"/"'\\''"}' "; done
  6. @netj netj revised this gist Oct 15, 2010. 1 changed file with 9 additions and 1 deletion.
    10 changes: 9 additions & 1 deletion memusg
    Original file line number Diff line number Diff line change
    @@ -11,6 +11,15 @@ set -um

    # TODO support more options: peak, footprint, sampling rate, etc.

    pgid=`ps -o pgid= $$`
    # make sure we're in a separate process group
    if [ x$pgid = x$(ps -o pgid= $(ps -o ppid= $$)) ]; then
    cmd=
    set -- "$0" "$@"
    for a; do cmd+="'${a//"'"/"'\\''"}' "; done
    exec bash -i -c "$cmd"
    fi

    # detect operating system and prepare measurement
    case `uname` in
    Darwin|*BSD) sizes() { /bin/ps -o rss= -g $1; } ;;
    @@ -19,7 +28,6 @@ case `uname` in
    esac

    # monitor the memory usage in the background.
    pgid=`ps -o pgid= $$`
    (
    peak=0
    while sizes=`sizes $pgid`
  7. @netj netj created this gist Aug 16, 2010.
    38 changes: 38 additions & 0 deletions memusg
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,38 @@
    #!/usr/bin/env bash
    # memusg -- Measure memory usage of processes
    # Usage: memusg COMMAND [ARGS]...
    #
    # Author: Jaeho Shin <[email protected]>
    # Created: 2010-08-16
    set -um

    # check input
    [ $# -gt 0 ] || { sed -n '2,/^#$/ s/^# //p' <"$0"; exit 1; }

    # TODO support more options: peak, footprint, sampling rate, etc.

    # detect operating system and prepare measurement
    case `uname` in
    Darwin|*BSD) sizes() { /bin/ps -o rss= -g $1; } ;;
    Linux) sizes() { /bin/ps -o rss= -$1; } ;;
    *) echo "`uname`: unsupported operating system" >&2; exit 2 ;;
    esac

    # monitor the memory usage in the background.
    pgid=`ps -o pgid= $$`
    (
    peak=0
    while sizes=`sizes $pgid`
    do
    set -- $sizes
    sample=$((${@/#/+}))
    let peak="sample > peak ? sample : peak"
    sleep 0.1
    done
    echo "memusg: peak=$peak" >&2
    ) &
    monpid=$!


    # run the given command
    exec "$@"