Azinix

stats.c

Go to the documentation of this file.
00001 #include "evl.h"
00002 
00003 /* Data structure for storing statistics */
00004 
00005 typedef struct Uc_Stats_t {
00006   int totalPkts;
00007   int totalBytes;
00008   Hash_t *allIps;
00009   util_timing_t lastPrintTime;
00010   util_timing_t printInterval;
00011 } Uc_Stats_t;
00012 
00013 /**AutomaticStart */
00014 
00015 // prototypes 
00016 
00017 static void Uc_PrintStats( Uc_Stats_t *stats );
00018 static Uc_Stats_t * Uc_InitStats( char *argument );
00019 static void Uc_UpdateStats( Evl_Manager_t *mgr, Pkt_ProcessPkt_t *pp, Uc_Stats_t *stats );
00020 
00021 /**AutomaticEnd */
00022 
00023 
00024 /* \brief Code for gathering traffic statistics.  */
00025 
00026 int
00027 stats (Evl_Manager_t * mgr, Pkt_ProcessPkt_t * pp, void **mystate, void *argument )
00028 {
00029   static Uc_Stats_t *stats;
00030 
00031   if (*mystate == NULL) {
00032     printf( "First call to Uc_Stats\n");
00033     stats = Uc_InitStats( (char *) argument );
00034     *mystate = (void *) stats;
00035   }
00036 
00037   Uc_UpdateStats( mgr, pp, stats );
00038   Uc_PrintStats( stats );
00039 
00040   return 0;
00041 }
00042 
00043 
00044 /** \brief Allocate and initialize the stats structure */
00045 
00046 static Uc_Stats_t *
00047 Uc_InitStats( 
00048   char *argument
00049 )
00050 {
00051   Uc_Stats_t *result = ( Uc_Stats_t * ) malloc( sizeof( Uc_Stats_t ) );
00052 
00053   result->totalPkts = 0;
00054   result->totalBytes = 0;
00055 
00056   result->allIps = Hash_InitTable( Hash_NumCmp, Hash_NumHash ); 
00057 
00058   util_timing_set( & result->lastPrintTime );
00059 
00060   double printInterval;
00061   sscanf( argument, "%lf", & printInterval );
00062   result->printInterval = util_timing_pair( printInterval );
00063 
00064   return result;
00065 }
00066 
00067 
00068 /** \brief Update stats field */
00069 
00070 static void 
00071 Uc_UpdateStats(
00072   Evl_Manager_t *mgr,
00073   Pkt_ProcessPkt_t *pp,
00074   Uc_Stats_t *stats 
00075 )
00076 {
00077   stats->totalPkts++;
00078   stats->totalBytes += pp->length;
00079 
00080   if ( Pkt_EthernetReadL3Type( pp->pkt ) == Pkt_L3ProtIp_c ) {
00081     Pkt_IpHdr_t *ipPkt = Pkt_EthernetExtractIp( pp->pkt );
00082     Hash_Insert( stats->allIps, ipPkt->sourceIp, 0 );
00083     Hash_Insert( stats->allIps, ipPkt->destIp, 0 );
00084   }
00085 }
00086 
00087 
00088 /** \brief Print current state of stats structure 
00089 
00090    Ensures that at least printInterval time has elapsed
00091    between printings.
00092 
00093 */
00094 
00095 static void
00096 Uc_PrintStats(
00097   Uc_Stats_t *stats
00098 )
00099 {
00100   util_timing_t currentTime;
00101   util_timing_set( & currentTime );
00102   util_timing_t diffTime = util_timing_diff( currentTime, stats->lastPrintTime );
00103 
00104   if ( util_timing_compare( diffTime, stats->printInterval ) ) {
00105     return;
00106   }
00107   else {
00108     stats->lastPrintTime = currentTime;
00109   }
00110 
00111   printf("Packets:\t%d\n", stats->totalPkts );
00112   printf("Bytes:\t%d\n", stats->totalBytes );
00113 
00114   printf("Num IPs:\t%d\n", Hash_Count( stats->allIps ) );
00115   printf("---\n\n");
00116 }