Examples

mallocdelta

#!/usr/bin/env python3

"""
Script to collect and monitor the memory usage of a zone.

Example:
    mallocdelta.py hostname username password zonename interval(in sec)
"""

###############################################################################
#
#                          NO WARRANTY
#
#  BECAUSE THE PROGRAM IS PROVIDED FREE OF CHARGE, THERE IS NO WARRANTY
#  FOR THE PROGRAM, TO THE EXTENT PERMITTED BY APPLICABLE LAW.  EXCEPT WHEN
#  OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR OTHER PARTIES
#  PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED
#  OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
#  MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.  THE ENTIRE RISK AS
#  TO THE QUALITY AND PERFORMANCE OF THE PROGRAM IS WITH YOU.  SHOULD THE
#  PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF ALL NECESSARY SERVICING,
#  REPAIR OR CORRECTION.
#
#  IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING
#  WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY AND/OR
#  REDISTRIBUTE THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES,
#  INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING
#  OUT OF THE USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED
#  TO LOSS OF DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY
#  YOU OR THIRD PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER
#  PROGRAMS), EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE
#  POSSIBILITY OF SUCH DAMAGES.
#
###############################################################################

import activelogic
from activelogic import SysDiagMallocUsageZone
import sys
import time


class MallocDelta:
    def __init__(self, sd, name):
        self.sd = sd

        zones = self.sd.list(SysDiagMallocUsageZone)
        self.zone = next(iter([z for z in zones if z.zonename == name]), None)

        if not self.zone:
            print("Available zones: %s" % [z.zonename for z in zones])
            sys.exit(1)

        self.meminfo = sd.malloc_usage_get(self.zone)
        self.starttime = 0

    def dump_time(self):
        print(time.ctime())

    def dump(self):
        self.dump_time()
        self.starttime = time.time()
        for x in sorted(self.meminfo, key=lambda a: a.name):
            print("TOTAL[%.0f]: %s %d bytes (%d entries). Total count: %d" % (
                self.starttime, x.name, x.cur_bytes, x.tot_allocs, x.cur_allocs))

    def dump_delta(self):
        self.dump_time()
        timedelta = time.time() - self.starttime
        prev = {}
        for x in sorted(self.meminfo, key=lambda a: a.name):
            prev[x.name] = x.cur_bytes, x.cur_allocs

        self.meminfo = self.sd.malloc_usage_get(self.zone)

        for x in sorted(self.meminfo, key=lambda a: a.name):
            newval = x.cur_bytes, x.cur_allocs
            prevval = prev.get(x.name, (0, 0))
            if newval != prevval:
                print("DELTA[+%.0f]: %s %+d bytes (%+d entries). Now total: %d" % (
                    timedelta, x.name, newval[0] - prevval[0], newval[1] - prevval[1], newval[0]))


if len(sys.argv) != 6:
    print("Usage: mallocdelta.py host user password zone interval(in sec)")
    print("  e.g: mallocdelta.py 127.0.0.1 admin password LiveView 10")
    sys.exit(1)

al = activelogic.sync_connect(*sys.argv[1:4])
sd = al.sysdiag()
md = MallocDelta(sd, sys.argv[4])

md.dump()

while True:
    time.sleep(int(sys.argv[5]))
    md.dump_delta()