LCOV - code coverage report
Current view: top level - tests - stopwatch.h (source / functions) Hit Total Coverage
Test: coverage.info Lines: 13 16 81.2 %
Date: 2016-11-29 14:56:30 Functions: 3 3 100.0 %

          Line data    Source code
       1             : /* stopwatch.h - Helper code for timing
       2             :  * Copyright (C) 2013 g10 Code GmbH
       3             :  *
       4             :  * This file is part of Libgcrypt.
       5             :  *
       6             :  * Libgcrypt is free software; you can redistribute it and/or modify
       7             :  * it under the terms of the GNU Lesser General Public License as
       8             :  * published by the Free Software Foundation; either version 2.1 of
       9             :  * the License, or (at your option) any later version.
      10             :  *
      11             :  * Libgcrypt 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 Lesser General Public License for more details.
      15             :  *
      16             :  * You should have received a copy of the GNU Lesser General Public
      17             :  * License along with this program; if not, see <http://www.gnu.org/licenses/>.
      18             :  */
      19             : 
      20             : 
      21             : #include <time.h>
      22             : #ifdef _WIN32
      23             : # include <winsock2.h>
      24             : # include <windows.h>
      25             : #else
      26             : # include <sys/times.h>
      27             : #endif
      28             : 
      29             : 
      30             : #ifdef _WIN32
      31             : struct
      32             : {
      33             :   FILETIME creation_time, exit_time, kernel_time, user_time;
      34             : } started_at, stopped_at;
      35             : #else
      36             : static clock_t started_at, stopped_at;
      37             : #endif
      38             : 
      39             : 
      40             : static void
      41         576 : start_timer (void)
      42             : {
      43             : #ifdef _WIN32
      44             : #ifdef __MINGW32CE__
      45             :   GetThreadTimes (GetCurrentThread (),
      46             :                    &started_at.creation_time, &started_at.exit_time,
      47             :                    &started_at.kernel_time, &started_at.user_time);
      48             : #else
      49             :   GetProcessTimes (GetCurrentProcess (),
      50             :                    &started_at.creation_time, &started_at.exit_time,
      51             :                    &started_at.kernel_time, &started_at.user_time);
      52             : #endif
      53             :   stopped_at = started_at;
      54             : #else
      55             :   struct tms tmp;
      56             : 
      57         576 :   times (&tmp);
      58         576 :   started_at = stopped_at = tmp.tms_utime;
      59             : #endif
      60         576 : }
      61             : 
      62             : static void
      63         576 : stop_timer (void)
      64             : {
      65             : #ifdef _WIN32
      66             : #ifdef __MINGW32CE__
      67             :   GetThreadTimes (GetCurrentThread (),
      68             :                    &stopped_at.creation_time, &stopped_at.exit_time,
      69             :                    &stopped_at.kernel_time, &stopped_at.user_time);
      70             : #else
      71             :   GetProcessTimes (GetCurrentProcess (),
      72             :                    &stopped_at.creation_time, &stopped_at.exit_time,
      73             :                    &stopped_at.kernel_time, &stopped_at.user_time);
      74             : #endif
      75             : #else
      76             :   struct tms tmp;
      77             : 
      78         576 :   times (&tmp);
      79         576 :   stopped_at = tmp.tms_utime;
      80             : #endif
      81         576 : }
      82             : 
      83             : static const char *
      84         575 : elapsed_time (unsigned int divisor)
      85             : {
      86             :   static char buf[50];
      87             : #if _WIN32
      88             :   unsigned long long t1, t2, t;
      89             : 
      90             :   t1 = (((unsigned long long)started_at.kernel_time.dwHighDateTime << 32)
      91             :         + started_at.kernel_time.dwLowDateTime);
      92             :   t1 += (((unsigned long long)started_at.user_time.dwHighDateTime << 32)
      93             :         + started_at.user_time.dwLowDateTime);
      94             :   t2 = (((unsigned long long)stopped_at.kernel_time.dwHighDateTime << 32)
      95             :         + stopped_at.kernel_time.dwLowDateTime);
      96             :   t2 += (((unsigned long long)stopped_at.user_time.dwHighDateTime << 32)
      97             :         + stopped_at.user_time.dwLowDateTime);
      98             :   t = ((t2 - t1)/divisor)/10000;
      99             :   if (divisor != 1)
     100             :     snprintf (buf, sizeof buf, "%5.1fms", (double)t );
     101             :   else
     102             :     snprintf (buf, sizeof buf, "%5.0fms", (double)t );
     103             : #else
     104         575 :   if (divisor != 1)
     105           0 :     snprintf (buf, sizeof buf, "%5.1fms",
     106           0 :               ((((double) (stopped_at - started_at)/(double)divisor)
     107           0 :                 /CLOCKS_PER_SEC)*10000000));
     108             :   else
     109         575 :     snprintf (buf, sizeof buf, "%5.0fms",
     110         575 :               (((double) (stopped_at - started_at)/CLOCKS_PER_SEC)*10000000));
     111             : #endif
     112         575 :   return buf;
     113             : }

Generated by: LCOV version 1.11