LCOV - code coverage report
Current view: top level - tests/harness - cputimer.cc (source / functions) Hit Total Coverage
Test: Test Coverage for xapian-core 954b5873a738 Lines: 8 10 80.0 %
Date: 2019-06-30 05:20:33 Functions: 1 1 100.0 %
Branches: 5 28 17.9 %

           Branch data     Line data    Source code
       1                 :            : /** @file cputimer.cc
       2                 :            :  * @brief Measure CPU time.
       3                 :            :  */
       4                 :            : /* Copyright (C) 2009,2015,2018 Olly Betts
       5                 :            :  *
       6                 :            :  * This program is free software; you can redistribute it and/or
       7                 :            :  * modify it under the terms of the GNU General Public License as
       8                 :            :  * published by the Free Software Foundation; either version 2 of the
       9                 :            :  * License, or (at your option) any later version.
      10                 :            :  *
      11                 :            :  * This program is distributed in the hope that it will be useful,
      12                 :            :  * but WITHOUT ANY WARRANTY; without even the implied warranty of
      13                 :            :  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
      14                 :            :  * GNU General Public License for more details.
      15                 :            :  *
      16                 :            :  * You should have received a copy of the GNU General Public License
      17                 :            :  * along with this program; if not, write to the Free Software
      18                 :            :  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA
      19                 :            :  */
      20                 :            : 
      21                 :            : #include <config.h>
      22                 :            : 
      23                 :            : #include "cputimer.h"
      24                 :            : 
      25                 :            : #include "errno_to_string.h"
      26                 :            : #include "testsuite.h"
      27                 :            : 
      28                 :            : #ifdef HAVE_GETRUSAGE
      29                 :            : # include <sys/time.h>
      30                 :            : # include <sys/resource.h>
      31                 :            : #elif defined HAVE_TIMES
      32                 :            : # include <sys/times.h>
      33                 :            : # ifdef HAVE_SYSCONF
      34                 :            : #  include "safeunistd.h"
      35                 :            : # endif
      36                 :            : #elif defined HAVE_FTIME
      37                 :            : # include <sys/timeb.h>
      38                 :            : #else
      39                 :            : # include <ctime>
      40                 :            : #endif
      41                 :            : 
      42                 :            : #include <cerrno>
      43                 :            : #include <cstdlib>
      44                 :            : #include <cstring>
      45                 :            : #include <string>
      46                 :            : 
      47                 :            : using namespace std;
      48                 :            : 
      49                 :            : double
      50                 :         56 : CPUTimer::get_current_cputime() const
      51                 :            : {
      52                 :            : #ifdef XAPIAN_DEBUG_LOG
      53                 :            :     SKIP_TEST("Skipping timed test because configured with --enable-log");
      54                 :            : #else
      55 [ +  + ][ +  - ]:         56 :     static bool skip = (getenv("AUTOMATED_TESTING") != NULL);
      56         [ -  + ]:         56 :     if (skip) {
      57 [ #  # ][ #  # ]:          0 :         SKIP_TEST("Skipping timed test because $AUTOMATED_TESTING is set");
         [ #  # ][ #  # ]
      58                 :            :     }
      59                 :            : 
      60                 :         56 :     double t = 0;
      61                 :            : #ifdef HAVE_GETRUSAGE
      62                 :            :     struct rusage r;
      63         [ -  + ]:         56 :     if (getrusage(RUSAGE_SELF, &r) == -1) {
      64 [ #  # ][ #  # ]:          0 :         FAIL_TEST("Couldn't measure CPU for self: " << errno_to_string(errno));
         [ #  # ][ #  # ]
         [ #  # ][ #  # ]
      65                 :            :     }
      66                 :            : 
      67                 :         56 :     t = r.ru_utime.tv_sec + r.ru_stime.tv_sec;
      68                 :         56 :     t += (r.ru_utime.tv_usec + r.ru_stime.tv_usec) * 0.000001;
      69                 :            : #elif defined HAVE_TIMES
      70                 :            :     struct tms b;
      71                 :            :     if (times(&b) == clock_t(-1)) {
      72                 :            :         FAIL_TEST("Couldn't measure CPU: " << errno_to_string(errno));
      73                 :            :     }
      74                 :            :     t = (double)(b.tms_utime + b.tms_stime);
      75                 :            : # ifdef HAVE_SYSCONF
      76                 :            :     t /= sysconf(_SC_CLK_TCK);
      77                 :            : # else
      78                 :            :     t /= CLK_TCK;
      79                 :            : # endif
      80                 :            : #else
      81                 :            :     // FIXME: Fallback to just using wallclock time, which is probably only
      82                 :            :     // going to be used on Microsoft Windows, where nobody has implemented
      83                 :            :     // the code required to get the CPU time used by a process.
      84                 :            : # ifdef HAVE_FTIME
      85                 :            :     struct timeb tb;
      86                 :            : #  ifdef FTIME_RETURNS_VOID
      87                 :            :     ftime(&tb);
      88                 :            :     t = tb.time + (tb.millitm * 0.001);
      89                 :            : #  else
      90                 :            :     if (ftime(&tb) == -1) {
      91                 :            :         t = time(NULL);
      92                 :            :     } else {
      93                 :            :         t = tb.time + (tb.millitm * 0.001);
      94                 :            :     }
      95                 :            : #  endif
      96                 :            : # else
      97                 :            :     t = time(NULL);
      98                 :            : # endif
      99                 :            : #endif
     100                 :            : 
     101                 :         56 :     return t;
     102                 :            : #endif
     103                 :            : }

Generated by: LCOV version 1.11