diff options
| author | Nick Barnes | 2001-10-31 14:40:56 +0000 |
|---|---|---|
| committer | Nick Barnes | 2001-10-31 14:40:56 +0000 |
| commit | 7acfca905d76140f4cc0b09c9a12de237de364cd (patch) | |
| tree | 3ed8babfa3a73d30f29e08ca5d5adcda4ca4e826 /mps/code/meter.c | |
| parent | b7ce4893f9902d57cd67ac9a92fa6c3d5a8fc833 (diff) | |
| download | emacs-7acfca905d76140f4cc0b09c9a12de237de364cd.tar.gz emacs-7acfca905d76140f4cc0b09c9a12de237de364cd.zip | |
Branch imports for masters.
Copied from Perforce
Change: 23678
ServerID: perforce.ravenbrook.com
Diffstat (limited to 'mps/code/meter.c')
| -rw-r--r-- | mps/code/meter.c | 104 |
1 files changed, 104 insertions, 0 deletions
diff --git a/mps/code/meter.c b/mps/code/meter.c new file mode 100644 index 00000000000..fc3b36d7bbd --- /dev/null +++ b/mps/code/meter.c | |||
| @@ -0,0 +1,104 @@ | |||
| 1 | /* impl.c.meter: METERS | ||
| 2 | * | ||
| 3 | * $HopeName: MMsrc!meter.c(trunk.9) $ | ||
| 4 | * Copyright (C) 1999 Harlequin Limited. All rights reserved. | ||
| 5 | * | ||
| 6 | * TRANSGRESSIONS | ||
| 7 | * | ||
| 8 | * .trans.label: We label meters with EventLabelAddr, but of course that's | ||
| 9 | * meant for labelling Addr's. We get away with it as long as the type | ||
| 10 | * Meter is compatible with Addr. | ||
| 11 | */ | ||
| 12 | |||
| 13 | #include "meter.h" | ||
| 14 | #include "mpm.h" | ||
| 15 | |||
| 16 | SRCID(meter, "$HopeName$"); | ||
| 17 | |||
| 18 | |||
| 19 | /* MeterInit -- initialize a meter */ | ||
| 20 | |||
| 21 | void MeterInit(Meter meter, char *name, void *owner) | ||
| 22 | { | ||
| 23 | Word sym; | ||
| 24 | |||
| 25 | meter->name = name; | ||
| 26 | meter->count = 0; | ||
| 27 | meter->total = 0.0; | ||
| 28 | meter->meanSquared = 0.0; | ||
| 29 | meter->max = 0; | ||
| 30 | meter->min = (Size)-1; | ||
| 31 | |||
| 32 | sym = EventInternString(name); | ||
| 33 | EventLabelAddr((Addr)meter, sym); /* see .trans.label */ | ||
| 34 | EVENT_PP(MeterInit, meter, owner); | ||
| 35 | UNUSED(owner); /* @@@@ hack */ | ||
| 36 | } | ||
| 37 | |||
| 38 | |||
| 39 | /* MeterAccumulate -- accumulate another data point in the meter */ | ||
| 40 | |||
| 41 | void MeterAccumulate(Meter meter, Size amount) | ||
| 42 | { | ||
| 43 | Count count = meter->count + 1; | ||
| 44 | double total = meter->total; | ||
| 45 | double meanSquared = meter->meanSquared; | ||
| 46 | double dcount = (double)count; | ||
| 47 | |||
| 48 | /* .limitation.variance: This computation accumulates a running | ||
| 49 | * mean^2, minimizing overflow, but sacrificing numerical stablity | ||
| 50 | * for small variances. For more accuracy, the data set should be | ||
| 51 | * emitted using a telemetry stream and analyzed off-line. | ||
| 52 | .stddev: stddev = sqrt(meanSquared - mean^2). | ||
| 53 | */ | ||
| 54 | meter->count = count; | ||
| 55 | meter->total = total + amount; | ||
| 56 | meter->meanSquared = | ||
| 57 | meanSquared / dcount * (dcount - 1.0) | ||
| 58 | + amount / dcount * amount; | ||
| 59 | if (amount > meter->max) | ||
| 60 | meter->max = amount; | ||
| 61 | if (amount < meter->min) | ||
| 62 | meter->min = amount; | ||
| 63 | } | ||
| 64 | |||
| 65 | |||
| 66 | /* MeterWrite -- describe method for meters */ | ||
| 67 | |||
| 68 | Res MeterWrite(Meter meter, mps_lib_FILE *stream) | ||
| 69 | { | ||
| 70 | Res res = ResOK; | ||
| 71 | |||
| 72 | res = WriteF(stream, | ||
| 73 | "meter $S {", meter->name, | ||
| 74 | "count: $U", meter->count, | ||
| 75 | NULL); | ||
| 76 | if (res != ResOK) | ||
| 77 | return res; | ||
| 78 | if (meter->count > 0) { | ||
| 79 | double mean = meter->total / (double)meter->count; | ||
| 80 | |||
| 81 | res = WriteF(stream, | ||
| 82 | ", total: $D", meter->total, | ||
| 83 | ", max: $U", meter->max, | ||
| 84 | ", min: $U", meter->min, | ||
| 85 | ", mean: $D", mean, | ||
| 86 | ", mean^2: $D", meter->meanSquared, | ||
| 87 | NULL); | ||
| 88 | if (res != ResOK) | ||
| 89 | return res; | ||
| 90 | } | ||
| 91 | res = WriteF(stream, "}\n", NULL); | ||
| 92 | |||
| 93 | return res; | ||
| 94 | } | ||
| 95 | |||
| 96 | |||
| 97 | /* MeterEmit -- emit an evnet with the current data from the meter */ | ||
| 98 | |||
| 99 | void MeterEmit(Meter meter) | ||
| 100 | { | ||
| 101 | EVENT_PDDWWW(MeterValues, meter, meter->total, meter->meanSquared, | ||
| 102 | meter->count, meter->max, meter->min); | ||
| 103 | UNUSED(meter); /* @@@@ hack */ | ||
| 104 | } | ||