Header And Logo

PostgreSQL
| The world's most advanced open source database.

Defines | Functions

getrusage.c File Reference

#include "c.h"
#include "rusagestub.h"
Include dependency graph for getrusage.c:

Go to the source code of this file.

Defines

#define TICK_TO_SEC(T, RATE)   ((T)/(RATE))
#define TICK_TO_USEC(T, RATE)   (((T)%(RATE)*1000000)/RATE)

Functions

int getrusage (int who, struct rusage *rusage)

Define Documentation

#define TICK_TO_SEC (   T,
  RATE 
)    ((T)/(RATE))

Referenced by getrusage().

#define TICK_TO_USEC (   T,
  RATE 
)    (((T)%(RATE)*1000000)/RATE)

Referenced by getrusage().


Function Documentation

int getrusage ( int  who,
struct rusage rusage 
)

Definition at line 32 of file getrusage.c.

References _dosmaperr(), NULL, rusage::ru_stime, rusage::ru_utime, RUSAGE_CHILDREN, RUSAGE_SELF, TICK_TO_SEC, TICK_TO_USEC, and times.

Referenced by pg_rusage_init(), ResetUsage(), and ShowUsage().

{
#ifdef WIN32

    FILETIME    starttime;
    FILETIME    exittime;
    FILETIME    kerneltime;
    FILETIME    usertime;
    ULARGE_INTEGER li;

    if (who != RUSAGE_SELF)
    {
        /* Only RUSAGE_SELF is supported in this implementation for now */
        errno = EINVAL;
        return -1;
    }

    if (rusage == (struct rusage *) NULL)
    {
        errno = EFAULT;
        return -1;
    }
    memset(rusage, 0, sizeof(struct rusage));
    if (GetProcessTimes(GetCurrentProcess(),
                        &starttime, &exittime, &kerneltime, &usertime) == 0)
    {
        _dosmaperr(GetLastError());
        return -1;
    }

    /* Convert FILETIMEs (0.1 us) to struct timeval */
    memcpy(&li, &kerneltime, sizeof(FILETIME));
    li.QuadPart /= 10L;         /* Convert to microseconds */
    rusage->ru_stime.tv_sec = li.QuadPart / 1000000L;
    rusage->ru_stime.tv_usec = li.QuadPart % 1000000L;

    memcpy(&li, &usertime, sizeof(FILETIME));
    li.QuadPart /= 10L;         /* Convert to microseconds */
    rusage->ru_utime.tv_sec = li.QuadPart / 1000000L;
    rusage->ru_utime.tv_usec = li.QuadPart % 1000000L;
#else                           /* all but WIN32 */

    struct tms  tms;
    int         tick_rate = CLK_TCK;    /* ticks per second */
    clock_t     u,
                s;

    if (rusage == (struct rusage *) NULL)
    {
        errno = EFAULT;
        return -1;
    }
    if (times(&tms) < 0)
    {
        /* errno set by times */
        return -1;
    }
    switch (who)
    {
        case RUSAGE_SELF:
            u = tms.tms_utime;
            s = tms.tms_stime;
            break;
        case RUSAGE_CHILDREN:
            u = tms.tms_cutime;
            s = tms.tms_cstime;
            break;
        default:
            errno = EINVAL;
            return -1;
    }
#define TICK_TO_SEC(T, RATE)    ((T)/(RATE))
#define TICK_TO_USEC(T,RATE)    (((T)%(RATE)*1000000)/RATE)
    rusage->ru_utime.tv_sec = TICK_TO_SEC(u, tick_rate);
    rusage->ru_utime.tv_usec = TICK_TO_USEC(u, tick_rate);
    rusage->ru_stime.tv_sec = TICK_TO_SEC(s, tick_rate);
    rusage->ru_stime.tv_usec = TICK_TO_USEC(u, tick_rate);
#endif   /* WIN32 */

    return 0;
}