wlschmidt / lcdmiscellany

Automatically exported from code.google.com/p/lcdmiscellany
1 stars 1 forks source link

Performance counters guidance #2

Closed GoogleCodeExporter closed 8 years ago

GoogleCodeExporter commented 8 years ago
What steps will reproduce the problem?
Not a problem I am trying to figure out how to add performance counters and
can't seem to get it.  This is what I have so far code wise but it isn't
working.  Do you have any guidance on the structure I have read the docs
and it seems a little vague on the structure for my mind lol.

Here is what I have:

struct MyCounters extends CounterManager {
    var %UPtime, %UP;
    function MyCounters() {
        %UPtime = PerformanceCounter("Processor", "*", "% Processor Time");

    }
    function UPtimeUpdate() {
        %UP = %UPtime.GetValueList();
        %UPtime.Update(%UP);
    }
    function CounterUpdate() {
        %UPtimeUpdate();

    }
}

function GetMyCounters() {
    if (IsNull(_counterManager)) {
        _counterManager = MyCounters();
    }
    return _counterManager;
}

I call it with %counters = GetMyCounters(); and it seems to work in that
your counters still work fine but mine is blank.

Thanks for any help:)

Original issue reported on code.google.com by drwag...@gmail.com on 26 Oct 2009 at 3:08

GoogleCodeExporter commented 8 years ago
A couple issues here, mostly because of limitations to my inheritance schema.

First, constructors of parent classes are not automatically called, so you need 
to
call them yourself.  As a result, "eventHandler.Insert(-1, $this);" is not being
called on your object, so it's not being added to the list of objects that 
respond to
certain events, and thus, CounterUpdate() is never being called.  Adding a call 
to
"%CounterManager();" in your constructor would fix this.  I'm not sure why the
original counters seem to be getting updated for you without this call.

There's a much bigger (And more fundamental) problem, however.  Once you do 
this,
your mycounter's object's CounterUpdate() function will be called whenever the
counters are automatically updated.  However, the parent class's CounterUpdate()
function won't be called.  As a result, your new uptime counter will work fine, 
but
the CounterManager it extends won't be updated.  It's actually currently 
impossible
to call a function of a parent class that shares the name of a function in a 
child
class, because of how my inheritance stuff works (It's a complete hack).  The 
only
(hackish) workaround in this case, without duplicating code or modifying my 
original
class, is to create an EventHandler() function (See below).

Also, you're using the same global variable name for your global object as I am 
for
mine, so if mine gets created before yours does, you only end up with one of 
mine, so
yours won't work (This might be why my values are updating properly - you're 
actually
getting one of my objects).

Here's a version that works (Both inherited and its own counter should be 
updated),
that takes advantage of the fact I look for two functions in my event handler 
code. 
I also fixed the counter to actually get up time (in seconds), and removed the
pointless "%UPtime.Update(%UP)".  The *.Update() calls are for updating the 
graph
objects, and UPtime isn't a graph:

struct MyCounters extends CounterManager {
    var %UPtime, %UP;
    function MyCounters() {
        %UPtime = PerformanceCounter("System", "", "System Up Time");
        %CounterManager();
    }
    function UPtimeUpdate() {
        %UP = %UPtime.GetValue();
    }

    function HandleEvent($event) {
        if ($event ==s "CounterUpdate") {
            %UPtimeUpdate();
        }
    }
}

function GetMyCounters() {
    if (IsNull(_myCounterManager)) {
        _myCounterManager = MyCounters();
        // You could overwrite _counterManager here as well, if you want,
        // but if it already exists, it'll still be registered with the
        // eventHandler, anyways, so you won't gain anything from it.
    }
    return _myCounterManager;
}

Original comment by mattme...@gmail.com on 26 Oct 2009 at 4:10

GoogleCodeExporter commented 8 years ago
Ohh...  The PerformanceCounter's Update() function is to update at times other 
than
when updating all the counters active in the background (Updating them all at 
once is
a lot more efficient, because of how performance counters work).  As 
UPtimeUpdate()
is being called in response to a CountersUpdate event (Which actually means the
counters were just updated), your call to the PerformanceCounter's Update() 
function
is unnecessary.

Original comment by mattme...@gmail.com on 26 Oct 2009 at 4:18

GoogleCodeExporter commented 8 years ago
Wow thanks for the fast reply!  Works well question for you though.  The 
FormatTime
function does not work with the uptime number right?  When I feed it to it the
min/sec seem right but the hours is wrong.  ie (FormatTime("Hh:NN:SS",
%mycounters.UP)) So I would have to calculate it manually I assume which leads 
to my
other question lol.

I was looking through the forums and saw GetTickCount().  Is it more efficient 
to do
something like:

$ticks = GetTickCount();
$hours = FormatValue($ticks/3600000, 2, 0);
$ticks -= $hours * 3600000;
$min = FormatValue($ticks/60000, 2, 0);
$ticks -= $min * 60000;
$secs = FormatValue($ticks/1000, 2, 0);
DisplayText("UP: " +s $hours +s ":" +s $min +s ":" +s $secs, $xoffset, 
$yoffset);

And thanks for the great program:)

Original comment by drwag...@gmail.com on 26 Oct 2009 at 9:44

GoogleCodeExporter commented 8 years ago
Edit sorry that might not have been clear what I am seeing is my uptime 
calculated
with GetTickCount() is say 10:15:40 (which is right) and with the performance 
counter
using FormatTime() is 2:15:40

Original comment by drwag...@gmail.com on 26 Oct 2009 at 9:59

GoogleCodeExporter commented 8 years ago
FormatTime() expects seconds since January 1st, 1970.  Doesn't format 
durations. 
Suppose you might be able to hack it to do so, but hours will reset at 12 or 
24, so
you'll need to add days as well.

You might want to convert it to an int ("$val | 0" will do this more 
efficiently than
FormatValue) and then pass it to FormatInterval().  Source for FormatInterval() 
is in
MediaView.c.  It's basically like your function, but has leading 0's (Which you
currently can't add with FormatValue().  It adds leading spaces).

Original comment by mattme...@gmail.com on 26 Oct 2009 at 5:42

GoogleCodeExporter commented 8 years ago
Wow that is awesome! Much easier than all that math I did lol.  It is
FormatDuration() though:)

Thanks again

Original comment by drwag...@gmail.com on 26 Oct 2009 at 9:09

GoogleCodeExporter commented 8 years ago
Sorry about that.  :)

Happy to help.

Original comment by mattme...@gmail.com on 26 Oct 2009 at 9:13