TSP: The Transport Sample Protocol



Main Page | Modules | Alphabetical List | Data Structures | File List | Data Fields | Globals | Related Pages

histogram.c

Go to the documentation of this file.
00001 
00036 /*----------------------------------------------------------------------+
00037   |                                                                     |
00038   |     FILE :          histogram.c                                     |
00039   |                                                                     |
00040   |     DESCRIPTION :   contains functions to build histogram in which  |
00041   |                     values are automatically sorted and archived    |
00042   |                     according to a paramtizable interval lenght     |
00043   |                                                                     |
00044   |     AUTHORS :       Marc LEROY                                      |
00045   |                                                                     |
00046   |     CREATION :      16/06/94                                        |
00047   |                                                                     |
00048   |     UPDATES :       20/12/94 Bruno Moisan                           |
00049   |                     interval lenght is parametrizable               |
00050   |                     number of intervals is parametrizable           |
00051   |                                                                     |
00052   |                     1/04/2004 Yves Dufrenne                         |
00053   |                     more sample, some comments                      |
00054   |                                                                     |
00055   +----------------------------------------------------------------------*/
00056 
00057 /*----------------------------------------------------------------------+
00058   |                                                                     |
00059   |     STANDARD INCLUDES                                               |
00060   |                                                                     |
00061   +----------------------------------------------------------------------*/
00062 
00063 #include <stdio.h>
00064 
00065 /*----------------------------------------------------------------------+
00066   |                                                                     |
00067   |     PROJECT INCLUDES                                                |
00068   |                                                                     |
00069   +----------------------------------------------------------------------*/
00070 #include "histogram.h"
00071 
00072 /*----------------------------------------------------------------------+
00073   |                                                                     |
00074   |     CONSTANTS and MACRO-FUNCTIONS                                   |
00075   |                                                                     |
00076   +----------------------------------------------------------------------*/
00077 
00078 #define TSP_HISTO_MAX   1000
00079 #define TSP_HISTO_DELTA 100  /* Default step is 100 us */
00080 
00081 /*----------------------------------------------------------------------+
00082   |                                                                     |
00083   |     STATIC and GLOBAL VARIABLES                                     |
00084   |                                                                     |
00085   +----------------------------------------------------------------------*/
00086 
00087 static  unsigned long   histo[TSP_HISTO_MAX];
00088 
00089 typedef struct {
00090   double                date;  /* Store the date of the evenement in seconds */
00091   unsigned long         delta; /* Store the width of this step in us */
00092 } info_evt_t;
00093 
00094 /*Use to store when some overrange delta happend */
00095 info_evt_t      list_evt[TSP_HISTO_MAX];
00096 
00097 static  unsigned int    max     = TSP_HISTO_MAX;
00098 static  unsigned int    delta   = TSP_HISTO_DELTA;
00099 static  unsigned int    nb_sample;
00100 static  unsigned int    nb_evt;  /* remeber the number of overrange values */
00101 static  double          threshold;
00102 
00103 /*----------------------------------------------------------------------+
00104   |                                                                     |
00105   |     FUNCTION :      tsp_histo_nb_interval                           |
00106   |                                                                     |
00107   |     DESCRIPTION :   set the number of used intervals                |
00108   |                                                                     |
00109   |     PARAMETERS :    nb      number of requested interval            |
00110   |                                                                     |
00111   |     GLOBAL VAR :    histo, max                                      |
00112   |                                                                     |
00113   |     MSG_RETURN :    0 if ok, -1 if nb > TSP_HISTO_MAX                       |
00114   |                                                                     |
00115   +----------------------------------------------------------------------*/
00116 
00117 int tsp_histo_nb_interval( unsigned int nb )
00118 {
00119   if ( (nb >= 0) && (nb <= TSP_HISTO_MAX ) )
00120     {
00121       max = nb;
00122       return 0;
00123     }
00124   else
00125     {
00126       fprintf (stderr, "WARNING : tsp_histo_nb_interval(%d)     invalid interval number requested", nb);
00127       return -1;
00128     }
00129 }
00130 
00131 /*----------------------------------------------------------------------+
00132   |                                                                     |
00133   |     FUNCTION :      tsp_histo_set_delta                                     |
00134   |                                                                     |
00135   |     DESCRIPTION :   set the width of the intervals                  |
00136   |                                                                     |
00137   |     PARAMETERS :    width   requested width in us                   |
00138   |                                                                     |
00139   |     GLOBAL VAR :    histo, delta                                    |
00140   |                                                                     |
00141   |     MSG_RETURN :    0 if ok, -1 if width <= 0                       |
00142   |                                                                     |
00143   +----------------------------------------------------------------------*/
00144 
00145 int tsp_histo_set_delta( unsigned int width )
00146 {
00147  
00148   if ( width > 0 )
00149     {
00150       delta = width;
00151       threshold = delta*10;
00152       return 0;
00153     }
00154   else
00155     {
00156       fprintf (stderr, "WARNING : tsp_histo_set_delta(%d) invalid interval width", width);
00157       return -1;
00158     }
00159 }
00160 
00161 /*----------------------------------------------------------------------+
00162   |                                                                     |
00163   |     FUNCTION :      tsp_histo_init                                  |
00164   |                                                                     |
00165   |     DESCRIPTION :   clear the histogram                             |
00166   |                                                                     |
00167   |     PARAMETERS :    none                                            |
00168   |                                                                     |
00169   |     GLOBAL VAR :    histo, max                                      |
00170   |                                                                     |
00171   |     MSG_RETURN :    none                                            |
00172   |                                                                     |
00173   +----------------------------------------------------------------------*/
00174 
00175 void tsp_histo_init()
00176 {
00177   int i;
00178 
00179   for(i=0;i<max;i++)
00180     histo[i]=0;
00181 
00182   nb_evt  = nb_sample = 0;
00183   threshold = delta*10;
00184 }
00185 
00186 /*----------------------------------------------------------------------+
00187   |                                                                     |
00188   |     FUNCTION :      tsp_histo_enter                                 |
00189   |                                                                     |
00190   |     DESCRIPTION :   outputs the histogram                           |
00191   |                                                                     |
00192   |     PARAMETERS :    value   value to put in the histogram   in us   |
00193   |                                                                     |
00194   |     GLOBAL VAR :    histo, max, delta                               |
00195   |                                                                     |
00196   |     MSG_RETURN :    none                                            |
00197   |                                                                     |
00198   +----------------------------------------------------------------------*/
00199 
00200 void tsp_histo_enter( unsigned long value )
00201 {
00202   if( value < ( max -1 ) * delta )
00203 
00204     histo[value/delta]++;
00205   else
00206     histo[max -1]++;
00207 }
00208 
00209 void tsp_histo_enter_with_date( unsigned long value, double date_evt )
00210 {
00211   nb_sample ++;
00212   tsp_histo_enter (value);
00213 
00214   /* Only try to store value if more than the width step */
00215   if (value >= threshold && nb_evt<TSP_HISTO_MAX )
00216     {
00217       list_evt[nb_evt].date = date_evt;
00218       list_evt[nb_evt++].delta = value;
00219     }
00220 }
00221 
00222 
00223 
00224 /*----------------------------------------------------------------------+
00225   |                                                                     |
00226   |     FUNCTION :      tsp_histo_dump                                  |
00227   |                                                                     |
00228   |     DESCRIPTION :   outputs the histogram                           |
00229   |                                                                     |
00230   |     PARAMETERS :    f       stream on which histogram has to be     |
00231   |                             displayed                               |
00232   |                                                                     |
00233   |                     title   title of the histogram                  |
00234   |                                                                     |
00235   |     GLOBAL VAR :    histo, max, delta                               |
00236   |                                                                     |
00237   |     MSG_RETURN :    none                                            |
00238   |                                                                     |
00239   +----------------------------------------------------------------------*/
00240 
00241 void tsp_histo_dump( FILE *f, char *title )
00242 {
00243   int i;
00244   fprintf(f, "=============== %s ==================\n", title);
00245 
00246   fprintf(f, "DURATION HISTOGRAM (micro seconds) : nb_sample=%d\n", nb_sample);
00247   for(i=0;i<max -1;i++)
00248     {
00249       if (histo[i] !=0)
00250         fprintf(f,"%10u <= dt < %10u \t: %5ld\n", delta*i, delta*(i+1), histo[i]);
00251                 
00252     }
00253 
00254   fprintf(f,"             dt >= %4d \t: %5ld\n", delta*(max-1), histo[max-1]);
00255 
00256   /* Guess if witdh is twice bigger than mean , that this evt is interisting */
00257   if (nb_evt !=0)
00258     {
00259       fprintf(f, "DATE Overrange (date in seconde, delta in micro seconds) : nb_evt=%d\n", nb_evt);
00260       for(i=0;i<nb_evt;i++)
00261         {
00262           fprintf(f,"\t%10.6fs : \t %5lu\n", list_evt[i].date, list_evt[i].delta);
00263         }
00264     }
00265 
00266   fprintf(f, "===========================================\n");
00267 
00268 }
00269 
Framework Home Page.

Beware !! TSP wave is coming...