"Fossies" - the Fresh Open Source Software Archive

Member "tcpflow-1.6.1/src/netviz/time_histogram.h" (19 Feb 2021, 4934 Bytes) of package /linux/misc/tcpflow-1.6.1.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) C and C++ source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file. For more information about "time_histogram.h" see the Fossies "Dox" file reference documentation.

    1 /**
    2  * Interface for the timehistogram class
    3  * Currently this is a histogram that's specialized to create a stacked bar graph
    4  * with up to 2^16 different values on each bar.
    5  *
    6  * Times are stored as 64-bit microseconds since January 1, 1970
    7  *
    8  * This source file is public domain, as it is not based on the original tcpflow.
    9  *
   10  * Author: Michael Shick <mike@shick.in>
   11  * 
   12  * History:
   13  * 2013-01-01 - Initial version by Mike Shick
   14  */
   15 
   16 
   17 #ifndef TIME_HISTOGRAM_H
   18 #define TIME_HISTOGRAM_H
   19 
   20 #include "tcpflow.h"
   21 #include <map>
   22 
   23 class time_histogram {
   24 public:
   25     time_histogram();
   26 
   27     //typedef uint64_t count_t;           // counts in a slot
   28     //typedef uint16_t port_t;            // port number
   29     //typedef int32_t timescale_off_t;   // ordinal offset within the histogram
   30 
   31     // parameter for...?
   32     class span_params {
   33     public:
   34         span_params(uint64_t usec_, uint64_t bucket_count_) :
   35             usec(usec_), bucket_count(bucket_count_) {}
   36         uint64_t usec;
   37         uint64_t bucket_count;
   38     };
   39     typedef std::vector<span_params> span_params_vector_t;
   40 
   41     // a bucket counts packets received in a given timeframe, organized by TCP port
   42     class bucket {
   43     public:
   44         typedef std::map<in_port_t, uint64_t> counts_t;
   45         bucket() : counts(), portless_count(){};
   46         uint64_t sum() const {
   47             /* this could be done with std::accumulate */
   48             uint64_t count = 0;
   49             for(counts_t::const_iterator it=counts.begin();it!=counts.end();it++){
   50                 count += it->second;
   51             }
   52             count += portless_count;
   53             return count;
   54         };
   55         counts_t counts;
   56         uint64_t portless_count;
   57         void increment(in_port_t port, uint64_t delta, unsigned int flags = 0x00) {
   58             if(flags & F_NON_TCP) {
   59                 portless_count += delta;
   60             }
   61             else {
   62                 counts[port] += delta;
   63             }
   64         }
   65     };
   66 
   67     class histogram_map {
   68     public:
   69         typedef std::map<uint32_t, bucket *> buckets_t;
   70         buckets_t buckets;
   71         histogram_map(span_params span_) :
   72             buckets(), span(span_), bucket_width(span.usec / span.bucket_count),
   73             base_time(0), insert_count(0){}
   74 
   75         span_params span;
   76         uint64_t bucket_width;          // in microseconds
   77         uint64_t base_time;             // microseconds since Jan 1, 1970; set on first call to scale_timeval
   78         uint64_t insert_count;                   // of entire histogram
   79 
   80         uint64_t greatest_bucket_sum() const {
   81             uint64_t greatest = 0;
   82             for(buckets_t::const_iterator it = buckets.begin();it!=buckets.end();it++){
   83                 if(it->second->sum() > greatest) greatest = it->second->sum();
   84             }
   85             return greatest;
   86         }
   87 
   88         /** convert timeval to a scaled time.  */
   89         uint32_t scale_timeval(const struct timeval &ts) {
   90             uint64_t raw_time = ts.tv_sec * (1000LL * 1000LL) + ts.tv_usec;
   91             if(base_time == 0) {
   92                 base_time = raw_time - (bucket_width * ((uint64_t)(span.bucket_count * underflow_pad_factor)));
   93                 // snap base time to nearest bucket_width to simplify bar labelling later
   94                 uint64_t unit = span.usec / span.bucket_count;
   95                 base_time = (base_time / unit) * unit;
   96             }
   97             if (raw_time < base_time) return -1; // underflow
   98             return (raw_time - base_time) / bucket_width;
   99         }
  100 
  101         // returns true if the insertion resulted in over/underflow
  102         bool insert(const struct timeval &ts, const in_port_t port, const uint64_t count = 1,
  103                 const unsigned int flags = 0x00);
  104     };
  105 
  106     void insert(const struct timeval &ts, const in_port_t port, const uint64_t count = 1,
  107             const unsigned int flags = 0x00);
  108     void condense(double factor);
  109     uint64_t usec_per_bucket() const;
  110     uint64_t packet_count() const;
  111     time_t start_date() const;
  112     time_t end_date() const;
  113     uint64_t tallest_bar() const;
  114     const bucket &at(uint32_t index) const;
  115     size_t size() const;
  116     size_t non_sparse_size() const;
  117 
  118     /* iterators for the buckets */
  119     histogram_map::buckets_t::const_iterator begin() const;
  120     histogram_map::buckets_t::const_iterator end() const;
  121     histogram_map::buckets_t::const_reverse_iterator rbegin() const;
  122     histogram_map::buckets_t::const_reverse_iterator rend() const;
  123     static span_params_vector_t build_spans();
  124 
  125 private:
  126     std::vector<histogram_map> histograms;
  127     uint32_t best_fit_index;
  128     struct timeval earliest_ts, latest_ts;
  129     uint64_t insert_count;
  130 
  131     /** configuration:
  132      */
  133     static const uint32_t bucket_count;
  134     static const float underflow_pad_factor;
  135     static const std::vector<span_params> spans; // in microseconds
  136     static const bucket empty_bucket;
  137 public:
  138     static const unsigned int F_NON_TCP;
  139 };
  140 
  141 #endif