apps/dtnperf/utils.c
changeset 0 2b3e5ec03512
equal deleted inserted replaced
-1:000000000000 0:2b3e5ec03512
       
     1 #include "utils.h"
       
     2 
       
     3 
       
     4 
       
     5 
       
     6 /* ------------------------------------------
       
     7  * mega2byte
       
     8  *
       
     9  * Converts MBytes into Bytes
       
    10  * ------------------------------------------ */
       
    11 long mega2byte(long n)
       
    12 {
       
    13     return (n * 1000000);
       
    14 } // end mega2byte
       
    15 
       
    16 
       
    17 /* ------------------------------------------
       
    18  * kilo2byte
       
    19  *
       
    20  * Converts KBytes into Bytes
       
    21  * ------------------------------------------ */
       
    22 long kilo2byte(long n)
       
    23 {
       
    24     return (n * 1000);
       
    25 } // end kilo2byte
       
    26 
       
    27 
       
    28 /* ------------------------------------------
       
    29  * findDataUnit
       
    30  *
       
    31  * Extracts the data unit from the given string.
       
    32  * If no unit is specified, returns 'Z'.
       
    33  * ------------------------------------------ */
       
    34 char find_data_unit(const char *inarg)
       
    35 {
       
    36     // units are B (Bytes), K (KBytes) and M (MBytes)
       
    37     const char unitArray[] =
       
    38         {'B', 'K', 'M'
       
    39         };
       
    40     char * unit = malloc(sizeof(char));
       
    41 
       
    42     if ((unit = strpbrk(inarg, unitArray)) == NULL)
       
    43     {
       
    44         unit = "Z";
       
    45     }
       
    46     return unit[0];
       
    47 } // end find_data_unit
       
    48 
       
    49 
       
    50 /* ------------------------------------------
       
    51  * add_time
       
    52  * ------------------------------------------ */
       
    53 void add_time(struct timeval *tot_time, struct timeval part_time)
       
    54 {
       
    55     tot_time->tv_sec += part_time.tv_sec;
       
    56     tot_time->tv_usec += part_time.tv_sec;
       
    57 
       
    58     if (tot_time->tv_usec >= 1000000)
       
    59     {
       
    60         tot_time->tv_sec++;
       
    61         tot_time->tv_usec -= 1000000;
       
    62     }
       
    63 
       
    64 } // end add_time
       
    65 
       
    66 
       
    67 /* --------------------------------------------------
       
    68  * csv_time_report
       
    69  * -------------------------------------------------- */
       
    70 void csv_time_report(int b_sent, int payload, struct timeval start, struct timeval end, FILE* csv_log)
       
    71 {
       
    72     const char* time_report_hdr = "BUNDLE_SENT,PAYLOAD (byte),TIME (s),DATA_SENT (Mbyte),GOODPUT (Mbit/s)";
       
    73 
       
    74 
       
    75     double g_put, data;
       
    76 
       
    77     data = (b_sent * payload)/1000000.0;
       
    78 
       
    79     fprintf(csv_log, "\n\n%s\n", time_report_hdr);
       
    80 
       
    81     g_put = (data * 8 * 1000) / ((double)(end.tv_sec - start.tv_sec) * 1000.0 +
       
    82                           (double)(end.tv_usec - start.tv_usec) / 1000.0);
       
    83     
       
    84     double time_s =  ((double)(end.tv_sec - start.tv_sec) * 1000.0 + (double)(end.tv_usec - start.tv_usec) / 1000.0) / 1000;
       
    85     
       
    86     fprintf(csv_log, "%d,%d,%.1f,%E,%.3f\n", b_sent, payload, time_s, data, g_put);
       
    87 
       
    88 } // end csv_time_report
       
    89 
       
    90 
       
    91 
       
    92 
       
    93 /* --------------------------------------------------
       
    94  * csv_data_report
       
    95  * -------------------------------------------------- */
       
    96 void csv_data_report(int b_id, int payload, struct timeval start, struct timeval end, FILE* csv_log)
       
    97 {
       
    98     const char* data_report_hdr = "BUNDLE_ID,PAYLOAD (byte),TIME (s),GOODPUT (Mbit/s)";
       
    99     // const char* time_hdr = "BUNDLES_SENT,PAYLOAD,TIME,GOODPUT";
       
   100     double g_put;
       
   101 
       
   102     fprintf(csv_log, "\n\n%s\n", data_report_hdr);
       
   103 
       
   104     g_put = (payload * 8) / ((double)(end.tv_sec - start.tv_sec) * 1000.0 +
       
   105                              (double)(end.tv_usec - start.tv_usec) / 1000.0) / 1000.0;
       
   106                              
       
   107     double time_s =  ((double)(end.tv_sec - start.tv_sec) * 1000.0 + (double)(end.tv_usec - start.tv_usec) / 1000.0) / 1000;
       
   108 
       
   109     fprintf(csv_log, "%d,%d,%.1f,%.3f\n", b_id, payload, time_s, g_put);
       
   110 
       
   111 } // end csv_data_report
       
   112 
       
   113 
       
   114 
       
   115 /* -------------------------------------------------------------------
       
   116  * pattern
       
   117  *
       
   118  * Initialize the buffer with a pattern of (index mod 10).
       
   119  * ------------------------------------------------------------------- */
       
   120 void pattern(char *outBuf, int inBytes)
       
   121 {
       
   122     assert (outBuf != NULL);
       
   123     while (inBytes-- > 0)
       
   124     {
       
   125         outBuf[inBytes] = (inBytes % 10) + '0';
       
   126     }
       
   127 } // end pattern
       
   128 
       
   129 
       
   130 
       
   131 /* -------------------------------------------------------------------
       
   132  * Set timestamp to the given seconds
       
   133  * ------------------------------------------------------------------- */
       
   134 struct timeval set( double sec )
       
   135 {
       
   136     struct timeval mTime;
       
   137 
       
   138     mTime.tv_sec = (long) sec;
       
   139     mTime.tv_usec = (long) ((sec - mTime.tv_sec) * 1000000);
       
   140 
       
   141     return mTime;
       
   142 } // end set
       
   143 
       
   144 
       
   145 /* -------------------------------------------------------------------
       
   146  * Add seconds to my timestamp.
       
   147  * ------------------------------------------------------------------- */
       
   148 struct timeval add( double sec )
       
   149 {
       
   150     struct timeval mTime;
       
   151 
       
   152     mTime.tv_sec = (long) sec;
       
   153     mTime.tv_usec = (long) ((sec - ((long) sec )) * 1000000);
       
   154 
       
   155     // watch for overflow
       
   156     if ( mTime.tv_usec >= 1000000 )
       
   157     {
       
   158         mTime.tv_usec -= 1000000;
       
   159         mTime.tv_sec++;
       
   160     }
       
   161     assert( mTime.tv_usec >= 0 && mTime.tv_usec < 1000000 );
       
   162 
       
   163     return mTime;
       
   164 } // end add
       
   165 
       
   166 
       
   167 /* --------------------------------------------------
       
   168  * show_report
       
   169  * -------------------------------------------------- */
       
   170 void show_report (u_int buf_len, char* eid, struct timeval start, struct timeval end, long data, FILE* output)
       
   171 {
       
   172     double g_put;
       
   173 
       
   174     double time_s = ((double)(end.tv_sec - start.tv_sec) * 1000.0 + (double)(end.tv_usec - start.tv_usec) / 1000.0) / 1000.0;
       
   175 
       
   176     double data_MB = data / 1000000.0;
       
   177     
       
   178     if (output == NULL)
       
   179         printf("got %d byte report from [%s]: time=%.1f s - %E Mbytes sent", buf_len, eid, time_s, data_MB);
       
   180     else
       
   181         fprintf(output, "\n total time=%.1f s - %E Mbytes sent", time_s, data_MB);
       
   182   
       
   183     // report goodput (bits transmitted / time)
       
   184     g_put = (data_MB * 8) / time_s;
       
   185     if (output == NULL)
       
   186         printf(" (goodput = %.3f Mbit/s)\n", g_put);
       
   187     else
       
   188         fprintf(output, " (goodput = %.3f Mbit/s)\n", g_put);
       
   189 
       
   190     // report start - end time
       
   191     if (output == NULL)
       
   192         printf(" started at %u sec - ended at %u sec\n", (u_int)start.tv_sec, (u_int)end.tv_sec);
       
   193     else
       
   194         fprintf(output, " started at %u sec - ended at %u sec\n", (u_int)start.tv_sec, (u_int)end.tv_sec);
       
   195 
       
   196 } // end show_report
       
   197 
       
   198 
       
   199 char* get_filename(char* s)
       
   200 {
       
   201     int i = 0, k;
       
   202     char* temp;
       
   203     char c = 'a';
       
   204     k = strlen(s) - 1;
       
   205     temp = malloc(strlen(s));
       
   206     strcpy(temp, s);
       
   207     while ((c != '/') && (k >= 0))
       
   208     {
       
   209         c = temp[k];
       
   210         k--;
       
   211     }
       
   212 
       
   213     if (c == '/')
       
   214         k += 2;
       
   215 
       
   216     else
       
   217         return temp;
       
   218 
       
   219     while (k != (int)strlen(temp))
       
   220     {
       
   221         temp[i] = temp[k];
       
   222         i++;
       
   223         k++;
       
   224     }
       
   225     temp[i] = '\0';
       
   226 
       
   227     return temp;
       
   228 } // end get_filename
       
   229 
       
   230 
       
   231