#if BLD_WIN_LIKE || VXWORKS
int gettimeofday(struct timeval *tv, struct timezone *tz)
{
    #if BLD_WIN_LIKE
        FILETIME        fileTime;
        MprTime         now;
        static int      tzOnce;


        if (NULL != tv) {
            /* Convert from 100-nanosec units to microsectonds */
            GetSystemTimeAsFileTime(&fileTime);
            now = ((((MprTime) fileTime.dwHighDateTime) << BITS(uint)) + ((MprTime) fileTime.dwLowDateTime));
            now /= 10;
            now -= TIME_GENESIS;
            tv->tv_sec = (long) (now / 1000000);
            tv->tv_usec = (long) (now % 1000000);
        }
        if (NULL != tz) {
            TIME_ZONE_INFORMATION   zone;
            int                     rc, bias;
            rc = GetTimeZoneInformation(&zone);
            bias = (int) zone.Bias;
            if (rc == TIME_ZONE_ID_DAYLIGHT) {
                tz->tz_dsttime = 1;
            } else {
                tz->tz_dsttime = 0;
            }
            tz->tz_minuteswest = bias;
        }
        return 0;


    #elif VXWORKS
        struct tm       tm;
        struct timespec now;
        time_t          t;
        char            *tze, *p;
        int             rc;


        if ((rc = clock_gettime(CLOCK_REALTIME, &now)) == 0) {
            tv->tv_sec  = now.tv_sec;
            tv->tv_usec = (now.tv_nsec + 500) / MS_PER_SEC;
            if ((tze = getenv("TIMEZONE")) != 0) {
                if ((p = strchr(tze, ':')) != 0) {
                    if ((p = strchr(tze, ':')) != 0) {
                        tz->tz_minuteswest = mprAtoi(++p, 10);
                    }
                }
                t = tickGet();
                tz->tz_dsttime = (localtime_r(&t, &tm) == 0) ? tm.tm_isdst : 0;
            }
        }
        return rc;
    #endif
}
#endif /* BLD_WIN_LIKE || VXWORKS */

/*
    Returns time in milliseconds since the epoch: 0:0:0 UTC Jan 1 1970.
 */
MprTime mprGetTime(MprCtx ctx)
{
#if VXWORKS
    struct timespec  tv;
    clock_gettime(CLOCK_REALTIME, &tv);
    return (MprTime) (((MprTime) tv.tv_sec) * 1000) + (tv.tv_nsec / (1000 * 1000));
#else
    struct timeval  tv;
    gettimeofday(&tv, NULL);
    return (MprTime) (((MprTime) tv.tv_sec) * 1000) + (tv.tv_usec / 1000);
#endif
}

/*
    High resolution timer
 */
#if MPR_HIGH_RES_TIMER
    #if BLD_UNIX_LIKE
        uint64 mprGetTicks() {
            uint64  now;
            __asm__ __volatile__ ("rdtsc" : "=A" (now));
            return now;
        }
    #elif BLD_WIN_LIKE
        uint64 mprGetTicks() {
            LARGE_INTEGER  now;
            QueryPerformanceCounter(&now);
            return (((uint64) now.HighPart) << 32) + now.HighPart;
        }
    #else
        uint64 mprGetTicks() {
            return (uint64) mprGetTime();
        }
    #endif
#else 
    uint64 mprGetTicks() {
        return 0;
    }
#endif

#if BLD_DEBUG
    #if MPR_HIGH_RES_TIMER
        #define MEASURE(ctx, tag1, tag2, op) \
            if (1) { \
                char tags[64]; \
                MprTime elapsed, start = mprGetTime(ctx); \
                uint64  ticks = mprGetTicks(); \
                mprSprintf(tags, sizeof(tags) - 1, "%s.%s", tag1, tag2); \
                op; \
                elapsed = mprGetTime(ctx) - start; \
                if (elapsed < 1000) { \
                    mprLog(ctx, 4, "TIME: %s elapsed %,d msec, %,d ticks", tags, elapsed, mprGetTicks() - ticks); \
                } else { \
                    mprLog(ctx, 4, "TIME: %s elapsed %,d msec", tags, elapsed); \
                } \
            } else 
    #else
        #define MEASURE(ctx, tag1, tag2, op) \
            if (1) { \
                char tags[64]; \
                MprTime start = mprGetTime(ctx); \
                mprSprintf(tags, sizeof(tags) - 1, "%s.%s", tag1, tag2); \
                op; \
                mprLog(ctx, 4, "TIME: %s elapsed %,d msec", tags, mprGetTime(ctx) - start); \
            } else 
    #endif
#else
    #define MEASURE(ctx, tag1, tag2, op) op
#endif

 

arrow
arrow
    全站熱搜

    Person 發表在 痞客邦 留言(0) 人氣()