#!/usr/bin/python

### This program is free software; you can redistribute it and/or
### modify it under the terms of the GNU General Public License
### as published by the Free Software Foundation; either version 2
### of the License, or (at your option) any later version.
###
### This program is distributed in the hope that it will be useful,
### but WITHOUT ANY WARRANTY; without even the implied warranty of
### MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
### GNU General Public License for more details.
###
### You should have received a copy of the GNU General Public License
### along with this program; if not, write to the Free Software
### Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.

### Copyright 2013-2014 Dag Wieers <dag@wieers.com>

import vmguestlib
import optparse
import time

__version__ = vmguestlib.__version__

delay = 2
count = -1

parser = optparse.OptionParser(usage='usage: %prog [-a] [-c] [-m] delay count', version='%prog ' + __version__)
parser.add_option( '-a', '--all', action='store_true',
    dest='all', help='show all statistics (default)' )
parser.add_option( '-c', '--cpu', action='store_true',
    dest='cpu', help='show cpu statistics' )
parser.add_option( '-m', '--mem', action='store_true',
    dest='memory', help='show memory statistics' )
(options, args) = parser.parse_args()

try:
    if len(args) > 0:
        delay = int(args[0])
    if len(args) > 1:
        count = int(args[1])
except:
    parser.error('incorrect argument, try -h for the correct syntax')

if options.all or (not options.cpu and not options.memory):
    options.cpu = True
    options.memory = True

gl = vmguestlib.VMGuestLib()
gl.UpdateInfo()

if options.cpu:
    OldElapsedMs = gl.GetElapsedMs()
    OldStolenMs = gl.GetCpuStolenMs()
    OldUsedMs = gl.GetCpuUsedMs()

while count == -1 or count > 0:

    time.sleep(delay)

    gl.UpdateInfo()

    # TODO: Add clearscreen and bold/underline escape sequences
    if options.cpu:
        NewElapsedMs = gl.GetElapsedMs()
        NewStolenMs = gl.GetCpuStolenMs()
        NewUsedMs = gl.GetCpuUsedMs()

        # Make sure that if time stands still we don't end up in infinity
        if NewElapsedMs == OldElapsedMs:
            UsedCpu = 0
            StolenCpu = 0
            EffectiveMhz = 0
        else:
            UsedCpu = (NewUsedMs - OldUsedMs) * 100.0 / (NewElapsedMs - OldElapsedMs)
            StolenCpu = (NewStolenMs - OldStolenMs) * 100.0 / (NewElapsedMs - OldElapsedMs)
            EffectiveMhz = gl.GetHostProcessorSpeed() * (NewUsedMs - OldUsedMs) / (NewElapsedMs - OldElapsedMs)

        CpuLimit = gl.GetCpuLimitMHz()

        print 'VM Processor'
        print '    Processor Time: %.2f %%' % UsedCpu
        print '    CPU stolen time: %.2f %%' % StolenCpu
        print '    Effective VM Speed: %d MHz' % EffectiveMhz
        print '    Host processor speed: %d MHz' % gl.GetHostProcessorSpeed()
        print
        if CpuLimit == -1 & 0xFFFFFFFF:
            print '    Limit: unlimited'
        else:
            print '    Limit: %d MHz' % gl.GetCpuLimitMHz()
        print '    Reservation: %d MHz' % gl.GetCpuReservationMHz()
        print '    Shares: %d' % gl.GetCpuShares()

        OldElapsedMs = NewElapsedMs
        OldStolenMs = NewStolenMs
        OldUsedMs = NewUsedMs

    if options.cpu and options.memory:
        print

    if options.memory:
        MemLimit = gl.GetMemLimitMB()

        print 'VM Memory'
        print '    Active: %d MB' % gl.GetMemActiveMB()
        print '    Ballooned: %d MB' % gl.GetMemBalloonedMB()
        print '    Mapped: %d MB' % gl.GetMemMappedMB()
        print '    Overhead: %d MB' % gl.GetMemOverheadMB()
        print '    Shared: %d MB' % gl.GetMemSharedMB()
        print '    Shared Saved: %d MB' % gl.GetMemSharedSavedMB()
        print '    Swapped: %d MB' % gl.GetMemSwappedMB()
        print '    Target Size: %d MB' % gl.GetMemTargetSizeMB()
        print '    Used: %d MB' % gl.GetMemUsedMB()
        print
        if MemLimit == -1 & 0xFFFFFFFF:
            print '    Limit: unlimited'
        else:
            print '    Limit: %d MB' % MemLimit
        print '    Reservation: %d MB' % gl.GetMemReservationMB()
        print '    Shares: %d' % gl.GetMemShares()
        print

    if count > 0:
        count = count - 1

gl.CloseHandle()

# vim:ts=4:sw=4:et