"Fossies" - the Fresh Open Source Software Archive

Member "bonnie++-2.00a/bon_time.cpp" (13 Sep 2018, 11259 Bytes) of package /linux/privat/bonnie++-2.00a.tgz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) C and C++ source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file. For more information about "bon_time.cpp" see the Fossies "Dox" file reference documentation.

    1 
    2 #include <stdlib.h>
    3 
    4 #include "bon_time.h"
    5 #include "duration.h"
    6 #include <time.h>
    7 #include <string.h>
    8 
    9 void BonTimer::start()
   10 {
   11   m_dur.start();
   12   m_cpu_dur.start();
   13 }
   14 void BonTimer::stop_and_record(tests_t test)
   15 {
   16   m_delta[test].Elapsed = m_dur.stop();
   17   m_delta[test].CPU = m_cpu_dur.stop();
   18 }
   19 
   20 void BonTimer::add_delta_report(report_s &rep, tests_t test)
   21 {
   22   if(m_delta[test].CPU == 0.0)
   23   {
   24     m_delta[test].FirstStart = rep.StartTime;
   25     m_delta[test].LastStop = rep.EndTime;
   26   }
   27   else
   28   {
   29     m_delta[test].FirstStart = min(m_delta[test].FirstStart, rep.StartTime);
   30     m_delta[test].LastStop = max(m_delta[test].LastStop, rep.EndTime);
   31   }
   32   m_delta[test].CPU += rep.CPU;
   33   m_delta[test].Elapsed = m_delta[test].LastStop - m_delta[test].FirstStart;
   34   m_delta[test].Latency = max(m_delta[test].Latency, rep.Latency);
   35 }
   36 
   37 BonTimer::BonTimer()
   38  : m_type(txt)
   39  , m_concurrency(1)
   40 {
   41   Initialize();
   42 }
   43 
   44 void
   45 BonTimer::Initialize()
   46 {
   47   for(int i = 0; i < TestCount; i++)
   48   {
   49     m_delta[i].CPU = 0.0;
   50     m_delta[i].Elapsed = 0.0;
   51     m_delta[i].Latency = 0.0;
   52   }
   53   random_source.reset();
   54 }
   55 
   56 void
   57 BonTimer::add_latency(tests_t test, double t)
   58 {
   59   m_delta[test].Latency = max(m_delta[test].Latency, t);
   60 }
   61 
   62 int BonTimer::print_cpu_stat(tests_t test)
   63 {
   64   if(m_delta[test].Elapsed == 0.0)
   65   {
   66     if(m_type == txt)
   67       fprintf(m_fp, "    ");
   68     else
   69       fprintf(m_fp, ",");
   70     return 0;
   71   }
   72   if(m_delta[test].Elapsed < MinTime)
   73   {
   74     if(m_type == txt)
   75       fprintf(m_fp, " +++");
   76     else
   77       fprintf(m_fp, ",+++");
   78     return 0;
   79   }
   80   int cpu = int(m_delta[test].CPU / m_delta[test].Elapsed * 100.0);
   81   if(m_type == txt)
   82     fprintf(m_fp, " %3d", cpu);
   83   else
   84     fprintf(m_fp, ",%d", cpu);
   85   return 0;
   86 }
   87 
   88 int BonTimer::print_stat(tests_t test, int test_size)
   89 {
   90   if(m_delta[test].Elapsed == 0.0)
   91   {
   92     if(m_type == txt)
   93       fprintf(m_fp, "      ");
   94     else
   95       fprintf(m_fp, ",");
   96   }
   97   else if(m_delta[test].Elapsed < MinTime)
   98   {
   99     if(m_type == txt)
  100       fprintf(m_fp, " +++++");
  101     else
  102       fprintf(m_fp, ",+++++");
  103   }
  104   else
  105   {
  106     double stat = double(test_size) / m_delta[test].Elapsed;
  107     if(test == Lseek)
  108     {
  109       if(m_type == txt)
  110       {
  111         if(stat >= 1000.0)
  112           fprintf(m_fp, " %5.0f", stat);
  113         else
  114           fprintf(m_fp, " %5.1f", stat);
  115       }
  116       else
  117       {
  118         if(stat >= 1000.0)
  119           fprintf(m_fp, ",%.0f", stat);
  120         else
  121           fprintf(m_fp, ",%.1f", stat);
  122       }
  123     }
  124     else
  125     {
  126       if(m_type == txt)
  127       {
  128         if(test < Lseek)
  129         {
  130           if(stat < 10000.0)
  131           {
  132             fprintf(m_fp, " %4dk", (int)stat);
  133           }
  134           else if(stat / 1024.0 < 1000.0)
  135           {
  136             stat = stat / 1024.0;
  137             if(stat < 100.0)
  138               fprintf(m_fp, " %4.1fm", stat);
  139             else
  140               fprintf(m_fp, " %4dm", (int)(stat + 0.5));
  141           }
  142           else
  143           {
  144             fprintf(m_fp, " %4.1fg", stat / 1024.0 / 1024.0);
  145           }
  146         }
  147         else
  148         {
  149           fprintf(m_fp, " %5d", stat);
  150         }
  151       }
  152       else
  153         fprintf(m_fp, ",%d", int(stat));
  154     }
  155   }
  156   print_cpu_stat(test);
  157   return 0;
  158 }
  159 
  160 int BonTimer::print_latency(tests_t test)
  161 {
  162   char buf[10];
  163   if(m_delta[test].Latency == 0.0)
  164   {
  165     buf[0] = '\0';
  166   }
  167   else
  168   {
  169     if(m_delta[test].Latency > 99.999999)
  170       _snprintf(buf
  171 #ifndef NO_SNPRINTF
  172 , sizeof(buf)
  173 #endif
  174               , "%ds", int(m_delta[test].Latency));
  175     else if(m_delta[test].Latency > 0.099999)
  176       _snprintf(buf
  177 #ifndef NO_SNPRINTF
  178 , sizeof(buf)
  179 #endif
  180               , "%dms", int(m_delta[test].Latency * 1000.0));
  181     else
  182       _snprintf(buf
  183 #ifndef NO_SNPRINTF
  184 , sizeof(buf)
  185 #endif
  186               , "%dus", int(m_delta[test].Latency * 1000000.0));
  187   }
  188   if(m_type == txt)
  189   {
  190     fprintf(m_fp, " %9s", buf);
  191   }
  192   else
  193   {
  194     fprintf(m_fp, ",%s", buf);
  195   }
  196   return 0;
  197 }
  198 
  199 void
  200 BonTimer::PrintHeader(FILE *fp)
  201 {
  202   fprintf(fp, "format_version,bonnie_version,name,concurrency,seed,file_size,io_chunk_size,putc,putc_cpu,put_block,put_block_cpu,rewrite,rewrite_cpu,getc,getc_cpu,get_block,get_block_cpu,seeks,seeks_cpu");
  203   fprintf(fp, ",num_files,max_size,min_size,num_dirs,file_chunk_size,seq_create,seq_create_cpu,seq_stat,seq_stat_cpu,seq_del,seq_del_cpu,ran_create,ran_create_cpu,ran_stat,ran_stat_cpu,ran_del,ran_del_cpu");
  204   fprintf(fp, ",putc_latency,put_block_latency,rewrite_latency,getc_latency,get_block_latency,seeks_latency,seq_create_latency,seq_stat_latency,seq_del_latency,ran_create_latency,ran_stat_latency,ran_del_latency");
  205   fprintf(fp, "\n");
  206   fflush(NULL);
  207 }
  208 
  209 void print_size(char *buf, unsigned int size, CPCCHAR units)
  210 {
  211   sprintf(buf, "%d", size);
  212   int ind = 0;
  213   if(units)
  214   {
  215     if(size == 0)
  216     {
  217       ind = strlen(buf);
  218       buf[ind] = units[0];
  219       buf[ind + 1] = '\0';
  220     }
  221     else
  222     {
  223       while(size % 1024 == 0 && units[ind + 1] != '\0')
  224       {
  225         size /= 1024;
  226         ind++;
  227       }
  228       sprintf(buf, "%d%c", size, units[ind]);
  229     }
  230   }
  231   ind = strlen(buf) - 1;
  232   if(buf[ind] == ' ')
  233     buf[ind] = '\0';
  234 }
  235 
  236 int
  237 BonTimer::DoReportIO(int file_size, int char_file_size
  238                    , int io_chunk_size, int Seeks, int SeekProcCount, FILE *fp)
  239 {
  240   int i;
  241   m_fp = fp;
  242   const int txt_machine_size = 20;
  243   PCCHAR separator = ":";
  244   if(m_type == csv)
  245     separator = ",";
  246   if(file_size)
  247   {
  248     if(m_type == txt)
  249     {
  250       fprintf(m_fp, "Version %5s       ", BON_VERSION);
  251       fprintf(m_fp,
  252         "------Sequential Output------ --Sequential Input- --Random-\n");
  253       fprintf(m_fp, "                    ");
  254       fprintf(m_fp,
  255         "-Per Chr- --Block-- -Rewrite- -Per Chr- --Block-- --Seeks--\n");
  256       fprintf(m_fp, "Name:Size etc       ");
  257       fprintf(m_fp, " /sec %%CP  /sec %%CP  /sec %%CP  /sec %%CP  /sec ");
  258       fprintf(m_fp, "%%CP  /sec %%CP\n");
  259     }
  260     char size_buf[1024];
  261     print_size(size_buf, file_size, "MG");
  262     char *tmp = size_buf + strlen(size_buf);
  263     if(m_type == txt)
  264     {
  265       strcat(tmp, separator);
  266       tmp++;
  267       if(io_chunk_size != DefaultChunkSize)
  268       {
  269         print_size(tmp, io_chunk_size, " km");
  270         tmp += strlen(tmp);
  271       }
  272       strcat(tmp, separator);
  273       tmp++;
  274       if(m_concurrency != 1)
  275       {
  276         sprintf(tmp, "%d", m_concurrency);
  277         tmp += strlen(tmp);
  278       }
  279       strcat(tmp, separator);
  280       tmp++;
  281       if(Seeks != DefaultSeeks)
  282       {
  283         sprintf(tmp, "%d", Seeks);
  284         tmp += strlen(tmp);
  285       }
  286       strcat(tmp, separator);
  287       tmp++;
  288       if(SeekProcCount != DefaultSeekProcCount)
  289       {
  290         sprintf(tmp, "%d", SeekProcCount);
  291         tmp += strlen(tmp) - 1;
  292       }
  293       tmp--;
  294       while(*tmp == separator[0] && tmp > size_buf)
  295       {
  296         *tmp = 0;
  297         tmp--;
  298       }
  299     }
  300     else
  301     {
  302       strcat(tmp, separator);
  303       tmp += strlen(tmp);
  304     }
  305     char buf[4096];
  306     if(m_type == txt)
  307     {
  308       // copy machine name to buf
  309       //
  310       _snprintf(buf
  311 #ifndef NO_SNPRINTF
  312 , txt_machine_size - 1
  313 #endif
  314               , "%s                  ", m_name);
  315       buf[txt_machine_size - 1] = '\0';
  316       // set cur to point to a byte past where we end the machine name
  317       // size of the buf - size of the new data - 1 for the space - 1 for the
  318       // terminating zero on the string
  319       char *cur;
  320       int offset = txt_machine_size - strlen(size_buf) - 2;
  321       if(offset < 0)
  322       {
  323         cur = buf;
  324       }
  325       else
  326       {
  327         cur = &buf[offset];
  328         *cur = ' '; // make cur a space
  329         cur++; // increment to where we store the size
  330       }
  331       strcpy(cur, size_buf);  // copy the size in
  332       fputs(buf, m_fp);
  333     }
  334     else
  335     {
  336       printf(CSV_VERSION "," BON_VERSION ",%s,%d,%s,%s,%d,%d", m_name
  337            , m_concurrency, random_source.getSeed().c_str(), size_buf, Seeks, SeekProcCount);
  338     }
  339     for(i = ByteWrite; i < Lseek; i++)
  340     {
  341       if(i == ByteWrite || i == ByteRead)
  342         print_stat(tests_t(i), char_file_size * 1024);
  343       else
  344         print_stat(tests_t(i), file_size * 1024);
  345     }
  346     print_stat(Lseek, Seeks);
  347     if(m_type == txt)
  348     {
  349       fprintf(m_fp, "\nLatency          ");
  350       for(i = ByteWrite; i <= Lseek; i++)
  351         print_latency(tests_t(i));
  352       fprintf(m_fp, "\n");
  353     }
  354   }
  355   else if(m_type == csv)
  356   {
  357     fprintf(m_fp, CSV_VERSION "," BON_VERSION ",%s,%d,%s,,,,,,,,,,,,,,,,", m_name
  358           , m_concurrency, random_source.getSeed().c_str());
  359   }
  360   return 0;
  361 }
  362 
  363 int
  364 BonTimer::DoReportFile(int directory_size
  365                      , int max_size, int min_size, int num_directories
  366                      , int file_chunk_size, FILE *fp)
  367 {
  368   PCCHAR separator = ":";
  369   m_fp = fp;
  370   int i;
  371   if(m_type == csv)
  372     separator = ",";
  373   if(directory_size)
  374   {
  375     char buf[128];
  376     char *tmp;
  377     sprintf(buf, "%d", directory_size);
  378     tmp = &buf[strlen(buf)];
  379     if(m_type == csv)
  380     {
  381       if(max_size == -1)
  382       {
  383         sprintf(tmp, ",link,");
  384       }
  385       else if(max_size == -2)
  386       {
  387         sprintf(tmp, ",symlink,");
  388       }
  389       else if(max_size)
  390       {
  391         if(min_size)
  392           sprintf(tmp, ",%d,%d", max_size, min_size);
  393         else
  394           sprintf(tmp, ",%d,", max_size);
  395       }
  396       else
  397       {
  398         sprintf(tmp, ",,");
  399       }
  400       strcat(tmp, separator);
  401       tmp += strlen(tmp);
  402       if(file_chunk_size != DefaultChunkSize)
  403       {
  404         tmp++;
  405         print_size(tmp, file_chunk_size, " km");
  406       }
  407     }
  408     else
  409     {
  410       if(max_size == -1)
  411       {
  412         sprintf(tmp, ":link");
  413       }
  414       else if(max_size == -2)
  415       {
  416         sprintf(tmp, ":symlink");
  417       }
  418       else if(max_size)
  419       {
  420         sprintf(tmp, ":%d:%d", max_size, min_size);
  421       }
  422     }
  423     tmp = &buf[strlen(buf)];
  424     if(num_directories > 1)
  425     {
  426       if(m_type == txt)
  427         sprintf(tmp, "/%d", num_directories);
  428       else
  429         sprintf(tmp, ",%d", num_directories);
  430     }
  431     else if(m_type == csv)
  432     {
  433        sprintf(tmp, ",");
  434     }
  435     if(m_type == txt)
  436     {
  437       fprintf(m_fp, "Version %5s       ", BON_VERSION);
  438       fprintf(m_fp,
  439         "------Sequential Create------ --------Random Create--------\n");
  440       fprintf(m_fp, "%-19.19s ", m_name);
  441       fprintf(m_fp,
  442            "-Create-- --Read--- -Delete-- -Create-- --Read--- -Delete--\n");
  443       if(min_size)
  444       {
  445         fprintf(m_fp, "files:max:min       ");
  446       }
  447       else
  448       {
  449         if(max_size)
  450           fprintf(m_fp, "files:max           ");
  451         else
  452           fprintf(m_fp, "              files ");
  453       }
  454       fprintf(m_fp, " /sec %%CP  /sec %%CP  /sec %%CP  /sec %%CP  /sec ");
  455       fprintf(m_fp, "%%CP  /sec %%CP\n");
  456       fprintf(m_fp, "%19s", buf);
  457     }
  458     else
  459     {
  460       fprintf(m_fp, ",%s", buf);
  461     }
  462     for(i = CreateSeq; i < TestCount; i++)
  463       print_stat(tests_t(i), directory_size * DirectoryUnit);
  464     if(m_type == txt)
  465     {
  466       fprintf(m_fp, "\nLatency          ");
  467       for(i = CreateSeq; i < TestCount; i++)
  468         print_latency(tests_t(i));
  469     }
  470   }
  471   else if(m_type == csv)
  472   {
  473     fprintf(m_fp, ",,,,,,,,,,,,,,,,,");
  474   }
  475   if(m_type == csv)
  476   {
  477     for(i = ByteWrite; i < TestCount; i++)
  478       print_latency(tests_t(i));
  479   }
  480   fprintf(m_fp, "\n");
  481   fflush(NULL);
  482   return 0;
  483 }
  484