1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147
|
/*
* Copyright (c) 1995 Regents of the University of California.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. All advertising materials mentioning features or use of this software
* must display the following acknowledgement:
* This product includes software developed by the Computer Systems
* Engineering Group at Lawrence Berkeley Laboratory.
* 4. Neither the name of the University nor of the Laboratory may be used
* to endorse or promote products derived from this software without
* specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/
static const char rcsid[] =
"@(#) $Header: /cs/research/mice/starship/src/local/CVS_repository/vic/rate-variable.cc,v 1.1.1.1 1998/02/18 18:03:41 ucacsva Exp $ (LBL)";
#include <stdlib.h>
#include "sys-time.h"
#include "Tcl.h"
/*
* The following command & the associate service procedure
* creates a `rate variable'. Reading a rate variable returns
* the time rate of change of its value. Writing a rate variable
* updates the rate of change estimate. A rate variable can be
* either simple or an array element. It must be global. This
* routine is called as:
* rate_variable varname filter_const
* where 'varname' is the variable name (e.g., "foo" or "foo(bar)")
* and 'filter_const' is a smoothing constant for the rate estimate.
* It should be >0 and <1 (a value of 1 will result in no smoothing).
*/
struct rv_data {
double rate;
double filter;
timeval lastupdate;
int lastval;
char format[16];
};
class RateVariable : public TclObject {
public:
RateVariable(const char* name) : TclObject(name) { }
protected:
static char* update_rate_var(ClientData, Tcl_Interp*, char* name1,
char* name2, int flags);
int command(int argc, const char*const* argv);
} rate_variable_cmd("rate_variable");
char* RateVariable::update_rate_var(ClientData clientData, Tcl_Interp* tcl,
char* name1, char* name2, int flags)
{
rv_data* rv = (rv_data*)clientData;
if (rv == NULL)
return ("no clientdata for rate var");
if (flags & TCL_TRACE_WRITES) {
/*
* the variable has been written with a new value.
* compute the rate of change & make that its vale
* for subsequent reads.
*/
char res[128];
flags &= TCL_GLOBAL_ONLY;
char* cv = Tcl_GetVar2(tcl, name1, name2, flags);
if (cv == NULL)
return (tcl->result);
int curval = atoi(cv);
double rate = 0.;
timeval tv;
gettimeofday(&tv, 0);
if (rv->lastupdate.tv_sec != 0) {
/* not first time through */
double dt = double(tv.tv_sec - rv->lastupdate.tv_sec) +
double(tv.tv_usec -
rv->lastupdate.tv_usec) * 1e-6;
if (dt <= 0.)
/* have to wait until next clock tick */
return NULL;
double dv = double(curval - rv->lastval);
if (dv >= 0.) {
rate = rv->rate;
rate += (dv/dt - rate) * rv->filter;
/* work around tcl f.p. precision bug */
if (rate < 1e-12)
rate = 0.;
}
}
rv->rate = rate;
rv->lastupdate = tv;
rv->lastval = curval;
sprintf(res, rv->format, rate);
Tcl_SetVar2(tcl, name1, name2, res, flags);
} else if (flags & (TCL_TRACE_DESTROYED|TCL_INTERP_DESTROYED)) {
delete rv;
}
return NULL;
}
int RateVariable::command(int argc, const char *const*argv)
{
Tcl& tcl = Tcl::instance();
const char* fmt;
if (argc == 4)
fmt = argv[3];
else if (argc == 3)
fmt = "%g";
else {
tcl.result("usage: rate_variable varname filter_const");
return (TCL_ERROR);
}
double fconst = atof(argv[2]);
if (fconst <= 0. || fconst > 1.) {
tcl.result("rate_variable: invalid filter constant");
return (TCL_ERROR);
}
rv_data* rv = new rv_data;
rv->filter = fconst;
rv->rate = 0.;
rv->lastupdate.tv_sec = 0;
rv->lastval = 0;
strcpy(rv->format, fmt);
int sts = Tcl_TraceVar(tcl.interp(), (char*)argv[1],
TCL_TRACE_WRITES|TCL_TRACE_UNSETS|TCL_GLOBAL_ONLY,
update_rate_var, (ClientData)rv);
if (sts != TCL_OK)
delete rv;
return (sts);
}
|