~vcs-imports/mammoth-replicator/trunk

« back to all changes in this revision

Viewing changes to src/port/getrusage.c

  • Committer: alvherre
  • Date: 2005-12-16 21:24:52 UTC
  • Revision ID: svn-v4:db760fc0-0f08-0410-9d63-cc6633f64896:trunk:1
Initial import of the REL8_0_3 sources from the Pgsql CVS repository.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*-------------------------------------------------------------------------
 
2
 *
 
3
 * getusage.c
 
4
 *        64-bit versions of fseeko/ftello()
 
5
 *
 
6
 * Portions Copyright (c) 1996-2005, PostgreSQL Global Development Group
 
7
 * Portions Copyright (c) 1994, Regents of the University of California
 
8
 *
 
9
 *
 
10
 * IDENTIFICATION
 
11
 *        $PostgreSQL: pgsql/src/port/getrusage.c,v 1.8 2004-12-31 22:03:53 pgsql Exp $
 
12
 *
 
13
 *-------------------------------------------------------------------------
 
14
 */
 
15
 
 
16
#include <stdio.h>
 
17
#include <errno.h>
 
18
 
 
19
#include "c.h"
 
20
#include "rusagestub.h"
 
21
 
 
22
/* This code works on:
 
23
 *              univel
 
24
 *              solaris_i386
 
25
 *              sco
 
26
 *              solaris_sparc
 
27
 *              svr4
 
28
 *              hpux 9.*
 
29
 *              win32
 
30
 * which currently is all the supported platforms that don't have a
 
31
 * native version of getrusage().  So, if configure decides to compile
 
32
 * this file at all, we just use this version unconditionally.
 
33
 */
 
34
 
 
35
int
 
36
getrusage(int who, struct rusage * rusage)
 
37
{
 
38
#ifdef WIN32
 
39
 
 
40
        FILETIME starttime;
 
41
        FILETIME exittime;
 
42
        FILETIME kerneltime;
 
43
        FILETIME usertime;
 
44
        ULARGE_INTEGER li;
 
45
 
 
46
        if (rusage == (struct rusage *)NULL)
 
47
        {
 
48
                errno = EFAULT;
 
49
                return -1;
 
50
        }
 
51
        memset(rusage, 0, sizeof(struct rusage));
 
52
        if (GetProcessTimes(GetCurrentProcess(),
 
53
                                                &starttime, &exittime, &kerneltime, &usertime) == 0)
 
54
        {
 
55
                _dosmaperr(GetLastError());
 
56
                return -1;
 
57
        }
 
58
 
 
59
        /* Convert FILETIMEs (0.1 us) to struct timeval */
 
60
        memcpy(&li, &kerneltime, sizeof(FILETIME));
 
61
        li.QuadPart /= 10L; /* Convert to microseconds */
 
62
        rusage->ru_stime.tv_sec  = li.QuadPart / 1000000L;
 
63
        rusage->ru_stime.tv_usec = li.QuadPart % 1000000L;
 
64
 
 
65
        memcpy(&li, &usertime, sizeof(FILETIME));
 
66
        li.QuadPart /= 10L; /* Convert to microseconds */
 
67
        rusage->ru_utime.tv_sec  = li.QuadPart / 1000000L;
 
68
        rusage->ru_utime.tv_usec = li.QuadPart % 1000000L;
 
69
 
 
70
#else /* all but WIN32 */
 
71
 
 
72
        struct tms      tms;
 
73
        int                     tick_rate = CLK_TCK;    /* ticks per second */
 
74
        clock_t         u,
 
75
                                s;
 
76
 
 
77
        if (rusage == (struct rusage *) NULL)
 
78
        {
 
79
                errno = EFAULT;
 
80
                return -1;
 
81
        }
 
82
        if (times(&tms) < 0)
 
83
        {
 
84
                /* errno set by times */
 
85
                return -1;
 
86
        }
 
87
        switch (who)
 
88
        {
 
89
                case RUSAGE_SELF:
 
90
                        u = tms.tms_utime;
 
91
                        s = tms.tms_stime;
 
92
                        break;
 
93
                case RUSAGE_CHILDREN:
 
94
                        u = tms.tms_cutime;
 
95
                        s = tms.tms_cstime;
 
96
                        break;
 
97
                default:
 
98
                        errno = EINVAL;
 
99
                        return -1;
 
100
        }
 
101
#define TICK_TO_SEC(T, RATE)    ((T)/(RATE))
 
102
#define TICK_TO_USEC(T,RATE)    (((T)%(RATE)*1000000)/RATE)
 
103
        rusage->ru_utime.tv_sec = TICK_TO_SEC(u, tick_rate);
 
104
        rusage->ru_utime.tv_usec = TICK_TO_USEC(u, tick_rate);
 
105
        rusage->ru_stime.tv_sec = TICK_TO_SEC(s, tick_rate);
 
106
        rusage->ru_stime.tv_usec = TICK_TO_USEC(u, tick_rate);
 
107
 
 
108
#endif /* WIN32 */
 
109
 
 
110
        return 0;
 
111
}