stats.h raw

   1  // Copyright (c) 2016 The Limenka developers
   2  // Distributed under the MIT software license, see the accompanying
   3  // file COPYING or http://www.opensource.org/licenses/mit-license.php.
   4  
   5  #ifndef LIMENKA_STATS_STATS_H
   6  #define LIMENKA_STATS_STATS_H
   7  
   8  #include <sync.h>
   9  
  10  #include <atomic>
  11  #include <stdlib.h>
  12  #include <vector>
  13  
  14  #include <boost/signals2/signal.hpp>
  15  
  16  struct CStatsMempoolSample {
  17      uint32_t m_time_delta;    //use 32bit time delta to save memory
  18      int64_t m_tx_count;       //transaction count
  19      int64_t m_dyn_mem_usage;  //dynamic mempool usage
  20      int64_t m_min_fee_per_k;  //min fee per Kb
  21  };
  22  
  23  typedef std::vector<struct CStatsMempoolSample> mempoolSamples_t;
  24  
  25  // simple mempool stats container
  26  class CStatsMempool
  27  {
  28  public:
  29      uint64_t m_start_time;      //start time of the container
  30      mempoolSamples_t m_samples;
  31      uint64_t m_cleanup_counter; //internal counter to trogger cleanups
  32  
  33      CStatsMempool()
  34      {
  35          m_start_time = 0;
  36          m_cleanup_counter = 0;
  37      }
  38  };
  39  
  40  // Class that manages various types of statistics and its memory consumption
  41  class CStats
  42  {
  43  private:
  44      static size_t maxStatsMemory;                  //maximum amount of memory to use for the stats
  45  
  46      static CStats* m_shared_instance;
  47      mutable RecursiveMutex cs_stats;
  48  
  49      CStatsMempool m_mempool_stats; //mempool stats container
  50  
  51  public:
  52      static const size_t DEFAULT_MAX_STATS_MEMORY; //default maximum of memory to use
  53      static const bool DEFAULT_STATISTICS_ENABLED; //default value for enabling statistics
  54  
  55      static std::atomic<bool> m_stats_enabled; //if enabled, stats will be collected
  56      static CStats* DefaultStats(); //shared instance
  57  
  58      /* signals */
  59      boost::signals2::signal<void(void)> MempoolStatsDidChange; //mempool stats update signal
  60  
  61      /* add a mempool stats sample */
  62      void addMempoolSample(int64_t txcount, int64_t dynUsage, int64_t currentMinRelayFee);
  63  
  64      /* get all mempool samples in range */
  65      mempoolSamples_t mempoolGetValuesInRange(uint64_t& fromTime, uint64_t& toTime);
  66  
  67      /* set the target for the maximum memory consumption (in bytes) */
  68      void setMaxMemoryUsageTarget(size_t maxMem);
  69  
  70      /* register the statistics module help strings */
  71      static void AddStatsOptions();
  72  
  73      /* access the parameters and map it to the internal model */
  74      static bool parameterInteraction();
  75  };
  76  
  77  #endif // LIMENKA_STATS_STATS_H
  78