LCOV - code coverage report
Current view: top level - harness - cputimer.cc (source / functions) Hit Total Coverage
Test: Test Coverage for xapian-core r Lines: 6 7 85.7 %
Date: 2011-08-21 Functions: 1 1 100.0 %
Branches: 1 4 25.0 %

           Branch data     Line data    Source code
       1                 :            : /** @file cputimer.cc
       2                 :            :  * @brief Measure CPU time.
       3                 :            :  */
       4                 :            : /* Copyright (C) 2009 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 "testsuite.h"
      26                 :            : 
      27                 :            : #include "safeerrno.h"
      28                 :            : 
      29                 :            : #ifdef HAVE_GETRUSAGE
      30                 :            : # include <sys/time.h>
      31                 :            : # include <sys/resource.h>
      32                 :            : #elif defined HAVE_TIMES
      33                 :            : # include <sys/times.h>
      34                 :            : # ifdef HAVE_SYSCONF
      35                 :            : #  include "safeunistd.h"
      36                 :            : # endif
      37                 :            : #elif defined HAVE_FTIME
      38                 :            : # include <sys/timeb.h>
      39                 :            : #else
      40                 :            : # include <ctime>
      41                 :            : #endif
      42                 :            : 
      43                 :            : #include <cstring>
      44                 :            : #include <string>
      45                 :            : 
      46                 :            : using namespace std;
      47                 :            : 
      48                 :            : double
      49                 :         56 : CPUTimer::get_current_cputime() const
      50                 :            : {
      51                 :         56 :     double t = 0;
      52                 :            : #ifdef HAVE_GETRUSAGE
      53                 :            :     struct rusage r;
      54         [ -  + ]:         56 :     if (getrusage(RUSAGE_SELF, &r) == -1) {
      55         [ #  # ]:          0 :         FAIL_TEST(string("Couldn't measure CPU for self: ") + strerror(errno));
      56                 :            :     }
      57                 :            : 
      58                 :         56 :     t = r.ru_utime.tv_sec + r.ru_stime.tv_sec;
      59                 :         56 :     t += (r.ru_utime.tv_usec + r.ru_stime.tv_usec) * 0.000001;
      60                 :            : #elif defined HAVE_TIMES
      61                 :            :     struct tms b;
      62                 :            :     if (times(&b) == (clock_t)-1) {
      63                 :            :         FAIL_TEST(string("Couldn't measure CPU: ") + strerror(errno));
      64                 :            :     }
      65                 :            :     t = (double)(b.tms_utime + b.tms_stime);
      66                 :            : # ifdef HAVE_SYSCONF
      67                 :            :     t /= sysconf(_SC_CLK_TCK);
      68                 :            : # else
      69                 :            :     t /= CLK_TCK;
      70                 :            : # endif
      71                 :            : #else
      72                 :            :     // FIXME: Fallback to just using wallclock time, which is probably only
      73                 :            :     // going to be used on Microsoft Windows, where nobody has implemented
      74                 :            :     // the code required to get the CPU time used by a process.
      75                 :            : # ifdef HAVE_FTIME 
      76                 :            :     struct timeb tb;
      77                 :            : #  ifdef FTIME_RETURNS_VOID
      78                 :            :     ftime(&tb);
      79                 :            :     t = tb.time + (tb.millitm * 0.001);
      80                 :            : #  else
      81                 :            :     if (ftime(&tb) == -1) {
      82                 :            :         t = time(NULL);
      83                 :            :     } else {
      84                 :            :         t = tb.time + (tb.millitm * 0.001);
      85                 :            :     }
      86                 :            : #  endif
      87                 :            : # else
      88                 :            :     t = time(NULL);
      89                 :            : # endif
      90                 :            : #endif
      91                 :            : 
      92                 :         56 :     return t;
      93                 :            : }

Generated by: LCOV version 1.8