diff options
author | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-05-04 12:15:05 +0000 |
---|---|---|
committer | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-05-04 12:15:05 +0000 |
commit | 46651ce6fe013220ed397add242004d764fc0153 (patch) | |
tree | 6e5299f990f88e60174a1d3ae6e48eedd2688b2b /src/backend/utils/misc/pg_rusage.c | |
parent | Initial commit. (diff) | |
download | postgresql-14-upstream.tar.xz postgresql-14-upstream.zip |
Adding upstream version 14.5.upstream/14.5upstream
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'src/backend/utils/misc/pg_rusage.c')
-rw-r--r-- | src/backend/utils/misc/pg_rusage.c | 73 |
1 files changed, 73 insertions, 0 deletions
diff --git a/src/backend/utils/misc/pg_rusage.c b/src/backend/utils/misc/pg_rusage.c new file mode 100644 index 0000000..bb5d9e7 --- /dev/null +++ b/src/backend/utils/misc/pg_rusage.c @@ -0,0 +1,73 @@ +/*------------------------------------------------------------------------- + * + * pg_rusage.c + * Resource usage measurement support routines. + * + * + * Portions Copyright (c) 1996-2021, PostgreSQL Global Development Group + * Portions Copyright (c) 1994, Regents of the University of California + * + * + * IDENTIFICATION + * src/backend/utils/misc/pg_rusage.c + * + *------------------------------------------------------------------------- + */ +#include "postgres.h" + +#include <unistd.h> + +#include "utils/pg_rusage.h" + + +/* + * Initialize usage snapshot. + */ +void +pg_rusage_init(PGRUsage *ru0) +{ + getrusage(RUSAGE_SELF, &ru0->ru); + gettimeofday(&ru0->tv, NULL); +} + +/* + * 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: user: %d.%02d s, system: %d.%02d s, elapsed: %d.%02d s"), + (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.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.tv.tv_sec - ru0->tv.tv_sec), + (int) (ru1.tv.tv_usec - ru0->tv.tv_usec) / 10000); + + return result; +} |