statistics.hh (7829:9069448c4fbc) statistics.hh (7831:c1e158414648)
1/*
2 * Copyright (c) 2003-2005 The Regents of The University of Michigan
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are
7 * met: redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer;

--- 1421 unchanged lines hidden (view full) ---

1430
1431 sum = Counter();
1432 squares = Counter();
1433 samples = Counter();
1434 }
1435};
1436
1437/**
1/*
2 * Copyright (c) 2003-2005 The Regents of The University of Michigan
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are
7 * met: redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer;

--- 1421 unchanged lines hidden (view full) ---

1430
1431 sum = Counter();
1432 squares = Counter();
1433 samples = Counter();
1434 }
1435};
1436
1437/**
1438 * Templatized storage and interface for a histogram stat.
1439 */
1440class HistStor
1441{
1442 public:
1443 /** The parameters for a distribution stat. */
1444 struct Params : public DistParams
1445 {
1446 /** The number of buckets.. */
1447 size_type buckets;
1448
1449 Params() : DistParams(Hist) {}
1450 };
1451
1452 private:
1453 /** The minimum value to track. */
1454 Counter min_bucket;
1455 /** The maximum value to track. */
1456 Counter max_bucket;
1457 /** The number of entries in each bucket. */
1458 Counter bucket_size;
1459
1460 /** The current sum. */
1461 Counter sum;
1462 /** The sum of squares. */
1463 Counter squares;
1464 /** The number of samples. */
1465 Counter samples;
1466 /** Counter for each bucket. */
1467 VCounter cvec;
1468
1469 public:
1470 HistStor(Info *info)
1471 : cvec(safe_cast<const Params *>(info->storageParams)->buckets)
1472 {
1473 reset(info);
1474 }
1475
1476 void grow_up();
1477 void grow_out();
1478 void grow_convert();
1479
1480 /**
1481 * Add a value to the distribution for the given number of times.
1482 * @param val The value to add.
1483 * @param number The number of times to add the value.
1484 */
1485 void
1486 sample(Counter val, int number)
1487 {
1488 assert(min_bucket < max_bucket);
1489 if (val < min_bucket) {
1490 if (min_bucket == 0)
1491 grow_convert();
1492
1493 while (val < min_bucket)
1494 grow_out();
1495 } else if (val >= max_bucket + bucket_size) {
1496 if (min_bucket == 0) {
1497 while (val >= max_bucket + bucket_size)
1498 grow_up();
1499 } else {
1500 while (val >= max_bucket + bucket_size)
1501 grow_out();
1502 }
1503 }
1504
1505 size_type index =
1506 (int64_t)std::floor((val - min_bucket) / bucket_size);
1507
1508 assert(index >= 0 && index < size());
1509 cvec[index] += number;
1510
1511 sum += val * number;
1512 squares += val * val * number;
1513 samples += number;
1514 }
1515
1516 /**
1517 * Return the number of buckets in this distribution.
1518 * @return the number of buckets.
1519 */
1520 size_type size() const { return cvec.size(); }
1521
1522 /**
1523 * Returns true if any calls to sample have been made.
1524 * @return True if any values have been sampled.
1525 */
1526 bool
1527 zero() const
1528 {
1529 return samples == Counter();
1530 }
1531
1532 void
1533 prepare(Info *info, DistData &data)
1534 {
1535 const Params *params = safe_cast<const Params *>(info->storageParams);
1536
1537 assert(params->type == Hist);
1538 data.type = params->type;
1539 data.min = min_bucket;
1540 data.max = max_bucket + bucket_size - 1;
1541 data.bucket_size = bucket_size;
1542
1543 data.min_val = min_bucket;
1544 data.max_val = max_bucket;
1545
1546 int buckets = params->buckets;
1547 data.cvec.resize(buckets);
1548 for (off_type i = 0; i < buckets; ++i)
1549 data.cvec[i] = cvec[i];
1550
1551 data.sum = sum;
1552 data.squares = squares;
1553 data.samples = samples;
1554 }
1555
1556 /**
1557 * Reset stat value to default
1558 */
1559 void
1560 reset(Info *info)
1561 {
1562 const Params *params = safe_cast<const Params *>(info->storageParams);
1563 min_bucket = 0;
1564 max_bucket = params->buckets - 1;
1565 bucket_size = 1;
1566
1567 size_type size = cvec.size();
1568 for (off_type i = 0; i < size; ++i)
1569 cvec[i] = Counter();
1570
1571 sum = Counter();
1572 squares = Counter();
1573 samples = Counter();
1574 }
1575};
1576
1577/**
1438 * Templatized storage and interface for a distribution that calculates mean
1439 * and variance.
1440 */
1441class SampleStor
1442{
1443 public:
1444 struct Params : public DistParams
1445 {

--- 843 unchanged lines hidden (view full) ---

2289 params->buckets = (size_type)ceil((max - min + 1.0) / bkt);
2290 this->setParams(params);
2291 this->doInit();
2292 return this->self();
2293 }
2294};
2295
2296/**
1578 * Templatized storage and interface for a distribution that calculates mean
1579 * and variance.
1580 */
1581class SampleStor
1582{
1583 public:
1584 struct Params : public DistParams
1585 {

--- 843 unchanged lines hidden (view full) ---

2429 params->buckets = (size_type)ceil((max - min + 1.0) / bkt);
2430 this->setParams(params);
2431 this->doInit();
2432 return this->self();
2433 }
2434};
2435
2436/**
2437 * A simple histogram stat.
2438 * @sa Stat, DistBase, HistStor
2439 */
2440class Histogram : public DistBase<Histogram, HistStor>
2441{
2442 public:
2443 /**
2444 * Set the parameters of this histogram. @sa HistStor::Params
2445 * @param size The number of buckets in the histogram
2446 * @return A reference to this histogram.
2447 */
2448 Histogram &
2449 init(size_type size)
2450 {
2451 HistStor::Params *params = new HistStor::Params;
2452 params->buckets = size;
2453 this->setParams(params);
2454 this->doInit();
2455 return this->self();
2456 }
2457};
2458
2459/**
2297 * Calculates the mean and variance of all the samples.
2298 * @sa DistBase, SampleStor
2299 */
2300class StandardDeviation : public DistBase<StandardDeviation, SampleStor>
2301{
2302 public:
2303 /**
2304 * Construct and initialize this distribution.

--- 484 unchanged lines hidden ---
2460 * Calculates the mean and variance of all the samples.
2461 * @sa DistBase, SampleStor
2462 */
2463class StandardDeviation : public DistBase<StandardDeviation, SampleStor>
2464{
2465 public:
2466 /**
2467 * Construct and initialize this distribution.

--- 484 unchanged lines hidden ---