mirror of https://github.com/postgres/postgres
for using it for other things besides VACUUM.REL8_1_STABLE
parent
9c873828bd
commit
12992ab37a
@ -0,0 +1,75 @@ |
|||||||
|
/*-------------------------------------------------------------------------
|
||||||
|
* |
||||||
|
* pg_rusage.c |
||||||
|
* Resource usage measurement support routines. |
||||||
|
* |
||||||
|
* |
||||||
|
* Portions Copyright (c) 1996-2005, PostgreSQL Global Development Group |
||||||
|
* Portions Copyright (c) 1994, Regents of the University of California |
||||||
|
* |
||||||
|
* |
||||||
|
* IDENTIFICATION |
||||||
|
* $PostgreSQL: pgsql/src/backend/utils/misc/pg_rusage.c,v 1.1 2005/10/03 22:52:23 tgl Exp $ |
||||||
|
* |
||||||
|
*------------------------------------------------------------------------- |
||||||
|
*/ |
||||||
|
#include "postgres.h" |
||||||
|
|
||||||
|
#include <unistd.h> |
||||||
|
|
||||||
|
#include "utils/pg_rusage.h" |
||||||
|
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Initialize usage snapshot. |
||||||
|
*/ |
||||||
|
void |
||||||
|
pg_rusage_init(PGRUsage *ru0) |
||||||
|
{ |
||||||
|
struct timezone tz; |
||||||
|
|
||||||
|
getrusage(RUSAGE_SELF, &ru0->ru); |
||||||
|
gettimeofday(&ru0->tv, &tz); |
||||||
|
} |
||||||
|
|
||||||
|
/*
|
||||||
|
* Compute elapsed time since ru0 usage snapshot, and format into |
||||||
|
* a displayable string. Result is in a static string, which is |
||||||
|
* tacky, but no one ever claimed that the Postgres backend is |
||||||
|
* threadable... |
||||||
|
*/ |
||||||
|
const char * |
||||||
|
pg_rusage_show(const PGRUsage *ru0) |
||||||
|
{ |
||||||
|
static char result[100]; |
||||||
|
PGRUsage ru1; |
||||||
|
|
||||||
|
pg_rusage_init(&ru1); |
||||||
|
|
||||||
|
if (ru1.tv.tv_usec < ru0->tv.tv_usec) |
||||||
|
{ |
||||||
|
ru1.tv.tv_sec--; |
||||||
|
ru1.tv.tv_usec += 1000000; |
||||||
|
} |
||||||
|
if (ru1.ru.ru_stime.tv_usec < ru0->ru.ru_stime.tv_usec) |
||||||
|
{ |
||||||
|
ru1.ru.ru_stime.tv_sec--; |
||||||
|
ru1.ru.ru_stime.tv_usec += 1000000; |
||||||
|
} |
||||||
|
if (ru1.ru.ru_utime.tv_usec < ru0->ru.ru_utime.tv_usec) |
||||||
|
{ |
||||||
|
ru1.ru.ru_utime.tv_sec--; |
||||||
|
ru1.ru.ru_utime.tv_usec += 1000000; |
||||||
|
} |
||||||
|
|
||||||
|
snprintf(result, sizeof(result), |
||||||
|
"CPU %d.%02ds/%d.%02du sec elapsed %d.%02d sec", |
||||||
|
(int) (ru1.ru.ru_stime.tv_sec - ru0->ru.ru_stime.tv_sec), |
||||||
|
(int) (ru1.ru.ru_stime.tv_usec - ru0->ru.ru_stime.tv_usec) / 10000, |
||||||
|
(int) (ru1.ru.ru_utime.tv_sec - ru0->ru.ru_utime.tv_sec), |
||||||
|
(int) (ru1.ru.ru_utime.tv_usec - ru0->ru.ru_utime.tv_usec) / 10000, |
||||||
|
(int) (ru1.tv.tv_sec - ru0->tv.tv_sec), |
||||||
|
(int) (ru1.tv.tv_usec - ru0->tv.tv_usec) / 10000); |
||||||
|
|
||||||
|
return result; |
||||||
|
} |
@ -0,0 +1,37 @@ |
|||||||
|
/*-------------------------------------------------------------------------
|
||||||
|
* |
||||||
|
* pg_rusage.h |
||||||
|
* header file for resource usage measurement support routines |
||||||
|
* |
||||||
|
* |
||||||
|
* Portions Copyright (c) 1996-2005, PostgreSQL Global Development Group |
||||||
|
* Portions Copyright (c) 1994, Regents of the University of California |
||||||
|
* |
||||||
|
* $PostgreSQL: pgsql/src/include/utils/pg_rusage.h,v 1.1 2005/10/03 22:52:26 tgl Exp $ |
||||||
|
* |
||||||
|
*------------------------------------------------------------------------- |
||||||
|
*/ |
||||||
|
#ifndef PG_RUSAGE_H |
||||||
|
#define PG_RUSAGE_H |
||||||
|
|
||||||
|
#include <sys/time.h> |
||||||
|
|
||||||
|
#ifdef HAVE_GETRUSAGE |
||||||
|
#include <sys/resource.h> |
||||||
|
#else |
||||||
|
#include "rusagestub.h" |
||||||
|
#endif |
||||||
|
|
||||||
|
|
||||||
|
/* State structure for pg_rusage_init/pg_rusage_show */ |
||||||
|
typedef struct PGRUsage |
||||||
|
{ |
||||||
|
struct timeval tv; |
||||||
|
struct rusage ru; |
||||||
|
} PGRUsage; |
||||||
|
|
||||||
|
|
||||||
|
extern void pg_rusage_init(PGRUsage *ru0); |
||||||
|
extern const char *pg_rusage_show(const PGRUsage *ru0); |
||||||
|
|
||||||
|
#endif /* PG_RUSAGE_H */ |
Loading…
Reference in new issue