Back to index

nordugrid-arc-nox  1.1.0~rc6
DataSpeed.cpp
Go to the documentation of this file.
00001 // -*- indent-tabs-mode: nil -*-
00002 
00003 #ifdef HAVE_CONFIG_H
00004 #include <config.h>
00005 #endif
00006 
00007 #include <ctime>
00008 #include <cstdio>
00009 #include <arc/data/DataSpeed.h>
00010 
00011 namespace Arc {
00012   
00013   Logger DataSpeed::logger(Logger::getRootLogger(), "DataSpeed"); 
00014 
00015   bool DataSpeed::verbose(void) {
00016     return be_verbose;
00017   }
00018 
00019   void DataSpeed::verbose(bool val) {
00020     be_verbose = val;
00021   }
00022 
00023   void DataSpeed::verbose(const std::string& prefix) {
00024     be_verbose = true;
00025     verbose_prefix = prefix;
00026   }
00027 
00028   void DataSpeed::hold(bool disable) {
00029     disabled = disable;
00030   }
00031 
00032   bool DataSpeed::transfer(unsigned long long int n) {
00033     if (disabled) {
00034       last_time = time(NULL);
00035       return true;
00036     }
00037     time_t t = time(NULL);
00038     time_t dt = t - last_time;
00039     Nall += n;
00040     if (dt > T)
00041       N = (n * dt) / T;
00042     else
00043       N = (N * (T - dt)) / T + n;
00044     if ((t - first_time) >= T * 3) {
00045       /* make decision only after statistics settles */
00046       /* check for speed */
00047       if (N < (T * min_speed))
00048         if (first_speed_failure != 0) {
00049           if (t > (first_speed_failure + min_speed_time))
00050             min_speed_failed = true;
00051         }
00052         else
00053           first_speed_failure = t;
00054       else
00055         first_speed_failure = 0;
00056       /* check for avearge speed */
00057       if ((min_average_speed * (t - first_time)) > Nall)
00058         min_average_speed_failed = true;
00059       /* check for inactivity time */
00060       if (t > (last_activity_time + max_inactivity_time))
00061         max_inactivity_time_failed = true;
00062     }
00063     if (n > 0)
00064       last_activity_time = t;
00065     last_time = t;
00066     if (be_verbose)
00067       /* statistics to screen */
00068       if ((t - last_printed) >= 1) {
00069         print_statistics(stderr, t);
00070         last_printed = t;
00071       }
00072     return !(min_speed_failed || min_average_speed_failed ||
00073              max_inactivity_time_failed);
00074   }
00075 
00076 
00077   void DataSpeed::print_statistics(FILE *o, time_t t) {
00078     if (show_progress != NULL) {
00079       (*show_progress)(o, verbose_prefix.c_str(),
00080                        (unsigned int)(t - first_time), Nall, Nmax,
00081                        (t > first_time ?
00082                         (((double)N) / (((t - first_time) > T) ? T :
00083                                         (t - first_time))) : ((double)0)),
00084                        (t > first_time ?
00085                         (((double)Nall) / (t - first_time)) : ((double)0)));
00086       return;
00087     }
00088     std::string::size_type outlen = verbose_prefix.length()+100;
00089     char out[outlen];
00090     snprintf(out,outlen,
00091             "%s%5u s: %10.1f kB  %8.1f kB/s  %8.1f kB/s    %c %c %c       ",
00092             verbose_prefix.c_str(),
00093             (unsigned int)(t - first_time),
00094             ((double)Nall) / 1024,
00095             (t > first_time ?
00096              (((double)N) / (((t - first_time) > T) ? T :
00097                              (t - first_time)) / 1024) : ((double)0)),
00098             (t > first_time ?
00099              (((double)Nall) / (t - first_time) / 1024) : ((double)0)),
00100             (min_speed_failed ? '!' : '.'),
00101             (min_average_speed_failed ? '!' : '.'),
00102             (max_inactivity_time_failed ? '!' : '.'));
00103     logger.msg(VERBOSE, out);
00104   }
00105 
00106   void DataSpeed::set_min_speed(unsigned long long int min_speed_,
00107                                 time_t min_speed_time_) {
00108     min_speed = min_speed_;
00109     min_speed_time = min_speed_time_;
00110   }
00111 
00112   void DataSpeed::set_min_average_speed(unsigned long long int
00113                                         min_average_speed_) {
00114     min_average_speed = min_average_speed_;
00115   }
00116 
00117   void DataSpeed::set_max_inactivity_time(time_t max_inactivity_time_) {
00118     max_inactivity_time = max_inactivity_time_;
00119   }
00120 
00121   void DataSpeed::set_base(time_t base_) {
00122     N = (N * base_) / T; /* allows ro change T on the fly */
00123     T = base_;
00124   }
00125 
00126   void DataSpeed::set_progress_indicator(show_progress_t func) {
00127     show_progress = func;
00128   }
00129 
00130   void DataSpeed::set_max_data(unsigned long long int max) {
00131     Nmax = max;
00132   }
00133 
00134   DataSpeed::DataSpeed(unsigned long long int min_speed_,
00135                        time_t min_speed_time_,
00136                        unsigned long long int min_average_speed_,
00137                        time_t max_inactivity_time_, time_t base_) {
00138     min_speed = min_speed_;
00139     min_speed_time = min_speed_time_;
00140     min_average_speed = min_average_speed_;
00141     max_inactivity_time = max_inactivity_time_;
00142     T = base_;
00143     be_verbose = false;
00144     disabled = false;
00145     show_progress = NULL;
00146     Nmax = 0;
00147     reset();
00148   }
00149 
00150   DataSpeed::~DataSpeed(void) {
00151     /* statistics to screen */
00152     if (be_verbose)
00153       print_statistics(stderr, time(NULL));
00154   }
00155 
00156   DataSpeed::DataSpeed(time_t base_) {
00157     min_speed = 0;
00158     min_speed_time = 0;
00159     min_average_speed = 0;
00160     max_inactivity_time = 600; /* MUST have at least pure timeout */
00161     T = base_;
00162     be_verbose = false;
00163     disabled = false;
00164     show_progress = NULL;
00165     Nmax = 0;
00166     reset();
00167   }
00168 
00169   void DataSpeed::reset(void) {
00170     first_time = time(NULL);
00171     last_time = first_time;
00172     last_activity_time = first_time;
00173     last_printed = first_time;
00174     N = 0;
00175     Nall = 0;
00176     first_speed_failure = 0;
00177     min_speed_failed = false;
00178     min_average_speed_failed = false;
00179     max_inactivity_time_failed = false;
00180     return;
00181   }
00182 
00183 } // namespace Arc