iostats.cc

Go to the documentation of this file.
00001 // 
00002 // iostats.cc      : Collect various statistics for Diffusion
00003 // authors         : Chalermek Intanagonwiwat and Fabio Silva
00004 //
00005 // Copyright (C) 2000-2003 by the University of Southern California
00006 // $Id: iostats.cc,v 1.2 2005/09/13 04:53:47 tomh Exp $
00007 //
00008 // This program is free software; you can redistribute it and/or
00009 // modify it under the terms of the GNU General Public License,
00010 // version 2, as published by the Free Software Foundation.
00011 //
00012 // This program is distributed in the hope that it will be useful,
00013 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00014 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00015 // GNU General Public License for more details.
00016 //
00017 // You should have received a copy of the GNU General Public License along
00018 // with this program; if not, write to the Free Software Foundation, Inc.,
00019 // 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
00020 //
00021 // Linking this file statically or dynamically with other modules is making
00022 // a combined work based on this file.  Thus, the terms and conditions of
00023 // the GNU General Public License cover the whole combination.
00024 //
00025 // In addition, as a special exception, the copyright holders of this file
00026 // give you permission to combine this file with free software programs or
00027 // libraries that are released under the GNU LGPL and with code included in
00028 // the standard release of ns-2 under the Apache 2.0 license or under
00029 // otherwise-compatible licenses with advertising requirements (or modified
00030 // versions of such code, with unchanged license).  You may copy and
00031 // distribute such a system following the terms of the GNU GPL for this
00032 // file and the licenses of the other code concerned, provided that you
00033 // include the source code of that other code when and as the GNU GPL
00034 // requires distribution of source code.
00035 //
00036 // Note that people who make modified versions of this file are not
00037 // obligated to grant this special exception for their modified versions;
00038 // it is their choice whether to do so.  The GNU General Public License
00039 // gives permission to release a modified version without this exception;
00040 // this exception also makes it possible to release a modified version
00041 // which carries forward this exception.
00042 
00043 #include "iostats.hh"
00044 
00045 DiffusionStats::DiffusionStats(int id, int warm_up_time)
00046 {
00047   // Zero various counters
00048   num_bytes_recv_ = 0;
00049   num_bytes_sent_ = 0;
00050   num_packets_recv_ = 0;
00051   num_packets_sent_ = 0;
00052   num_bcast_bytes_recv_ = 0;
00053   num_bcast_bytes_sent_ = 0;
00054   num_bcast_packets_recv_ = 0;
00055   num_bcast_packets_sent_ = 0;
00056 
00057   num_new_messages_sent_ = 0;
00058   num_new_messages_recv_ = 0;
00059   num_old_messages_sent_ = 0;
00060   num_old_messages_recv_ = 0;
00061 
00062   num_interest_messages_sent_ = 0;
00063   num_interest_messages_recv_ = 0;
00064   num_data_messages_sent_ = 0;
00065   num_data_messages_recv_ = 0;
00066   num_exploratory_data_messages_sent_ = 0;
00067   num_exploratory_data_messages_recv_ = 0;
00068   num_pos_reinforcement_messages_sent_ = 0;
00069   num_pos_reinforcement_messages_recv_ = 0;
00070   num_neg_reinforcement_messages_sent_ = 0;
00071   num_neg_reinforcement_messages_recv_ = 0;
00072 
00073   // Initialize id and time
00074   node_id_ = id;
00075   warm_up_time_ = warm_up_time;
00076   GetTime(&start_);
00077 }
00078 
00079 void DiffusionStats::logIncomingMessage(Message *msg)
00080 {
00081   NeighborStatsEntry *neighbor;
00082 
00083   // Ignore event if still warming up
00084   if (ignoreEvent())
00085     return;
00086 
00087   // We don't consider messages from local apps/filters
00088   if (msg->last_hop_ == LOCALHOST_ADDR)
00089     return;
00090 
00091   num_bytes_recv_ += (msg->data_len_ + sizeof(struct hdr_diff));
00092   num_packets_recv_++;
00093 
00094   if (msg->next_hop_ == BROADCAST_ADDR){
00095     num_bcast_packets_recv_++;
00096     num_bcast_bytes_recv_ += (msg->data_len_ + sizeof(struct hdr_diff));
00097   }
00098 
00099   if (msg->new_message_)
00100     num_new_messages_recv_++;
00101   else
00102     num_old_messages_recv_++;
00103 
00104   neighbor = getNeighbor(msg->last_hop_);
00105   neighbor->recv_messages_++;
00106   if (msg->next_hop_ == BROADCAST_ADDR)
00107     neighbor->recv_bcast_messages_++;
00108 
00109   switch (msg->msg_type_){
00110 
00111   case INTEREST:
00112 
00113     num_interest_messages_recv_++;
00114     
00115     break;
00116 
00117   case DATA:
00118 
00119     num_data_messages_recv_++;
00120 
00121     break;
00122 
00123   case EXPLORATORY_DATA:
00124 
00125     num_exploratory_data_messages_recv_++;
00126 
00127     break;
00128 
00129   case POSITIVE_REINFORCEMENT:
00130 
00131     num_pos_reinforcement_messages_recv_++;
00132 
00133     break;
00134 
00135   case NEGATIVE_REINFORCEMENT:
00136 
00137     num_neg_reinforcement_messages_recv_++;
00138 
00139     break;
00140 
00141   default:
00142 
00143     break;
00144   }
00145 }
00146 
00147 void DiffusionStats::logOutgoingMessage(Message *msg)
00148 {
00149   NeighborStatsEntry *neighbor;
00150 
00151   // Ignore event if still warming up
00152   if (ignoreEvent())
00153     return;
00154 
00155   num_bytes_sent_ += (msg->data_len_ + sizeof(struct hdr_diff));
00156   num_packets_sent_++;
00157 
00158   if (msg->next_hop_ == BROADCAST_ADDR){
00159     num_bcast_packets_sent_++;
00160     num_bcast_bytes_sent_ += (msg->data_len_ + sizeof(struct hdr_diff));
00161   }
00162 
00163   if (msg->new_message_)
00164     num_new_messages_sent_++;
00165   else
00166     num_old_messages_sent_++;
00167 
00168   if (msg->next_hop_ != BROADCAST_ADDR){
00169     neighbor = getNeighbor(msg->next_hop_);
00170     neighbor->sent_messages_++;
00171   }
00172 
00173   switch (msg->msg_type_){
00174 
00175   case INTEREST:
00176 
00177     num_interest_messages_sent_++;
00178     
00179     break;
00180 
00181   case DATA:
00182 
00183     num_data_messages_sent_++;
00184 
00185     break;
00186 
00187   case EXPLORATORY_DATA:
00188 
00189     num_exploratory_data_messages_sent_++;
00190 
00191     break;
00192 
00193   case POSITIVE_REINFORCEMENT:
00194 
00195     num_pos_reinforcement_messages_sent_++;
00196 
00197     break;
00198 
00199   case NEGATIVE_REINFORCEMENT:
00200 
00201     num_neg_reinforcement_messages_sent_++;
00202 
00203     break;
00204 
00205   default:
00206 
00207     break;
00208   }
00209 }
00210 
00211 void DiffusionStats::printStats(FILE *output)
00212 {
00213   NeighborStatsList::iterator itr;
00214   NeighborStatsEntry *neighbor;
00215   long seconds;
00216   long useconds;
00217   float total_time;
00218 
00219   // Compute elapsed running time
00220   GetTime(&finish_);
00221 
00222   seconds = finish_.tv_sec - start_.tv_sec;
00223   if (finish_.tv_usec < start_.tv_usec){
00224     seconds--;
00225     finish_.tv_usec += 1000000;
00226   }
00227 
00228   useconds = finish_.tv_usec - start_.tv_usec;
00229 
00230   total_time = (float) (1.0 * seconds) + ((float) useconds / 1000000.0);
00231 
00232   fprintf(output, "Diffusion Stats\n");
00233   fprintf(output, "---------------\n\n");
00234   fprintf(output, "Node id : %d\n", node_id_);
00235   fprintf(output, "Running time : %f seconds\n\n", total_time);
00236 
00237   fprintf(output, "Total bytes/packets sent    : %d/%d\n",
00238       num_bytes_sent_, num_packets_sent_);
00239   fprintf(output, "Total bytes/packets recv    : %d/%d\n",
00240       num_bytes_recv_, num_packets_recv_);
00241   fprintf(output, "Total BC bytes/packets sent : %d/%d\n",
00242       num_bcast_bytes_sent_, num_bcast_packets_sent_);
00243   fprintf(output, "Total BC bytes/packets recv : %d/%d\n",
00244       num_bcast_bytes_recv_, num_bcast_packets_recv_);
00245 
00246   fprintf(output, "\n");
00247 
00248   fprintf(output, "Messages\n");
00249   fprintf(output, "--------\n");
00250   fprintf(output, "Old               - Sent : %d - Recv : %d\n",
00251       num_old_messages_sent_, num_old_messages_recv_);
00252   fprintf(output, "New               - Sent : %d - Recv : %d\n",
00253       num_new_messages_sent_, num_new_messages_recv_);
00254 
00255   fprintf(output, "\n");
00256 
00257   fprintf(output, "Interest          - Sent : %d - Recv : %d\n",
00258       num_interest_messages_sent_, num_interest_messages_recv_);
00259   fprintf(output, "Data              - Sent : %d - Recv : %d\n",
00260       num_data_messages_sent_, num_data_messages_recv_);
00261   fprintf(output, "Exploratory Data  - Sent : %d - Recv : %d\n",
00262       num_exploratory_data_messages_sent_, num_exploratory_data_messages_recv_);
00263   fprintf(output, "Pos Reinforcement - Sent : %d - Recv : %d\n",
00264       num_pos_reinforcement_messages_sent_,
00265       num_pos_reinforcement_messages_recv_);
00266   fprintf(output, "Neg Reinforcement - Sent : %d - Recv : %d\n",
00267       num_neg_reinforcement_messages_sent_,
00268       num_neg_reinforcement_messages_recv_);
00269 
00270   fprintf(output, "\n");
00271 
00272   fprintf(output, "Neighbors Stats (in packets)\n");
00273   fprintf(output, "----------------------------\n");
00274 
00275   for (itr = nodes_.begin(); itr != nodes_.end(); ++itr){
00276     neighbor = *itr;
00277     if (neighbor){
00278       fprintf(output, "Node : %d - Sent(U/B/T) : %d/%d/%d - Recv(U/B/T) : %d/%d/%d\n",
00279           neighbor->id_, neighbor->sent_messages_, num_bcast_packets_sent_,
00280           (neighbor->sent_messages_ + num_bcast_packets_sent_),
00281           (neighbor->recv_messages_ - neighbor->recv_bcast_messages_),
00282           neighbor->recv_bcast_messages_,
00283           neighbor->recv_messages_);
00284     }
00285   }
00286 
00287   fprintf(output, "\n");
00288   fprintf(output, "Key: U - Unicast\n");
00289   fprintf(output, "     B - Broadcast\n");
00290   fprintf(output, "     T - Total\n");
00291 
00292   fprintf(output, "\n");
00293 }
00294 
00295 NeighborStatsEntry * DiffusionStats::getNeighbor(int id)
00296 {
00297   NeighborStatsList::iterator itr;
00298   NeighborStatsEntry *new_neighbor;
00299 
00300   for (itr = nodes_.begin(); itr != nodes_.end(); ++itr){
00301     if ((*itr)->id_ == id)
00302       break;
00303   }
00304 
00305   if (itr == nodes_.end()){
00306     // New neighbor
00307     new_neighbor = new NeighborStatsEntry(id);
00308     nodes_.push_front(new_neighbor);
00309     return new_neighbor;
00310   }
00311 
00312   return (*itr);
00313 }
00314 
00315 bool DiffusionStats::ignoreEvent()
00316 {
00317   struct timeval tv;
00318 
00319   GetTime(&tv);
00320 
00321   if ((start_.tv_sec + warm_up_time_) > tv.tv_sec)
00322     return true;
00323 
00324   return false;
00325 }

Generated on Tue Mar 6 16:47:46 2007 for ns2 Network Simulator 2.29 by  doxygen 1.4.6