LCOV - code coverage report
Current view: top level - src/backend/utils/misc - pg_rusage.c (source / functions) Hit Total Coverage
Test: PostgreSQL 17beta1 Lines: 20 24 83.3 %
Date: 2024-05-29 12:11:05 Functions: 2 2 100.0 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : /*-------------------------------------------------------------------------
       2             :  *
       3             :  * pg_rusage.c
       4             :  *    Resource usage measurement support routines.
       5             :  *
       6             :  *
       7             :  * Portions Copyright (c) 1996-2024, PostgreSQL Global Development Group
       8             :  * Portions Copyright (c) 1994, Regents of the University of California
       9             :  *
      10             :  *
      11             :  * IDENTIFICATION
      12             :  *    src/backend/utils/misc/pg_rusage.c
      13             :  *
      14             :  *-------------------------------------------------------------------------
      15             :  */
      16             : #include "postgres.h"
      17             : 
      18             : #include <unistd.h>
      19             : 
      20             : #include "utils/pg_rusage.h"
      21             : 
      22             : 
      23             : /*
      24             :  * Initialize usage snapshot.
      25             :  */
      26             : void
      27        8478 : pg_rusage_init(PGRUsage *ru0)
      28             : {
      29        8478 :     getrusage(RUSAGE_SELF, &ru0->ru);
      30        8478 :     gettimeofday(&ru0->tv, NULL);
      31        8478 : }
      32             : 
      33             : /*
      34             :  * Compute elapsed time since ru0 usage snapshot, and format into
      35             :  * a displayable string.  Result is in a static string, which is
      36             :  * tacky, but no one ever claimed that the Postgres backend is
      37             :  * threadable...
      38             :  */
      39             : const char *
      40         366 : pg_rusage_show(const PGRUsage *ru0)
      41             : {
      42             :     static char result[100];
      43             :     PGRUsage    ru1;
      44             : 
      45         366 :     pg_rusage_init(&ru1);
      46             : 
      47         366 :     if (ru1.tv.tv_usec < ru0->tv.tv_usec)
      48             :     {
      49          26 :         ru1.tv.tv_sec--;
      50          26 :         ru1.tv.tv_usec += 1000000;
      51             :     }
      52         366 :     if (ru1.ru.ru_stime.tv_usec < ru0->ru.ru_stime.tv_usec)
      53             :     {
      54           0 :         ru1.ru.ru_stime.tv_sec--;
      55           0 :         ru1.ru.ru_stime.tv_usec += 1000000;
      56             :     }
      57         366 :     if (ru1.ru.ru_utime.tv_usec < ru0->ru.ru_utime.tv_usec)
      58             :     {
      59           0 :         ru1.ru.ru_utime.tv_sec--;
      60           0 :         ru1.ru.ru_utime.tv_usec += 1000000;
      61             :     }
      62             : 
      63         366 :     snprintf(result, sizeof(result),
      64         366 :              _("CPU: user: %d.%02d s, system: %d.%02d s, elapsed: %d.%02d s"),
      65         366 :              (int) (ru1.ru.ru_utime.tv_sec - ru0->ru.ru_utime.tv_sec),
      66         366 :              (int) (ru1.ru.ru_utime.tv_usec - ru0->ru.ru_utime.tv_usec) / 10000,
      67         366 :              (int) (ru1.ru.ru_stime.tv_sec - ru0->ru.ru_stime.tv_sec),
      68         366 :              (int) (ru1.ru.ru_stime.tv_usec - ru0->ru.ru_stime.tv_usec) / 10000,
      69         366 :              (int) (ru1.tv.tv_sec - ru0->tv.tv_sec),
      70         366 :              (int) (ru1.tv.tv_usec - ru0->tv.tv_usec) / 10000);
      71             : 
      72         366 :     return result;
      73             : }

Generated by: LCOV version 1.14