Beispiel #1
0
static PyObject*
pyclock(_Py_clock_info_t *info)
{
#ifdef WIN32_PERF_COUNTER
    return win_perf_counter(info);
#else
    return floatclock(info);
#endif
}
Beispiel #2
0
static PyObject*
pyclock(_Py_clock_info_t *info)
{
#ifdef WIN32_PERF_COUNTER
    PyObject *res;
    if (win_perf_counter(info, &res) == 0)
        return res;
#endif
    return floatclock(info);
}
Beispiel #3
0
static PyObject*
py_process_time(_Py_clock_info_t *info)
{
#if defined(MS_WINDOWS)
    HANDLE process;
    FILETIME creation_time, exit_time, kernel_time, user_time;
    ULARGE_INTEGER large;
    double total;
    BOOL ok;

    process = GetCurrentProcess();
    ok = GetProcessTimes(process, &creation_time, &exit_time, &kernel_time, &user_time);
    if (!ok)
        return PyErr_SetFromWindowsErr(0);

    large.u.LowPart = kernel_time.dwLowDateTime;
    large.u.HighPart = kernel_time.dwHighDateTime;
    total = (double)large.QuadPart;
    large.u.LowPart = user_time.dwLowDateTime;
    large.u.HighPart = user_time.dwHighDateTime;
    total += (double)large.QuadPart;
    if (info) {
        info->implementation = "GetProcessTimes()";
        info->resolution = 1e-7;
        info->monotonic = 1;
        info->adjustable = 0;
    }
    return PyFloat_FromDouble(total * 1e-7);
#else

#if defined(HAVE_SYS_RESOURCE_H)
    struct rusage ru;
#endif
#ifdef HAVE_TIMES
    struct tms t;
    static long ticks_per_second = -1;
#endif

#if defined(HAVE_CLOCK_GETTIME) \
    && (defined(CLOCK_PROCESS_CPUTIME_ID) || defined(CLOCK_PROF))
    struct timespec tp;
#ifdef CLOCK_PROF
    const clockid_t clk_id = CLOCK_PROF;
    const char *function = "clock_gettime(CLOCK_PROF)";
#else
    const clockid_t clk_id = CLOCK_PROCESS_CPUTIME_ID;
    const char *function = "clock_gettime(CLOCK_PROCESS_CPUTIME_ID)";
#endif

    if (clock_gettime(clk_id, &tp) == 0) {
        if (info) {
            struct timespec res;
            info->implementation = function;
            info->monotonic = 1;
            info->adjustable = 0;
            if (clock_getres(clk_id, &res) == 0)
                info->resolution = res.tv_sec + res.tv_nsec * 1e-9;
            else
                info->resolution = 1e-9;
        }
        return PyFloat_FromDouble(tp.tv_sec + tp.tv_nsec * 1e-9);
    }
#endif

#if defined(HAVE_SYS_RESOURCE_H)
    if (getrusage(RUSAGE_SELF, &ru) == 0) {
        double total;
        total = ru.ru_utime.tv_sec + ru.ru_utime.tv_usec * 1e-6;
        total += ru.ru_stime.tv_sec + ru.ru_stime.tv_usec * 1e-6;
        if (info) {
            info->implementation = "getrusage(RUSAGE_SELF)";
            info->monotonic = 1;
            info->adjustable = 0;
            info->resolution = 1e-6;
        }
        return PyFloat_FromDouble(total);
    }
#endif

#ifdef HAVE_TIMES
    if (times(&t) != (clock_t)-1) {
        double total;

        if (ticks_per_second == -1) {
#if defined(HAVE_SYSCONF) && defined(_SC_CLK_TCK)
            ticks_per_second = sysconf(_SC_CLK_TCK);
            if (ticks_per_second < 1)
                ticks_per_second = -1;
#elif defined(HZ)
            ticks_per_second = HZ;
#else
            ticks_per_second = 60; /* magic fallback value; may be bogus */
#endif
        }

        if (ticks_per_second != -1) {
            total = (double)t.tms_utime / ticks_per_second;
            total += (double)t.tms_stime / ticks_per_second;
            if (info) {
                info->implementation = "times()";
                info->monotonic = 1;
                info->adjustable = 0;
                info->resolution = 1.0 / ticks_per_second;
            }
            return PyFloat_FromDouble(total);
        }
    }
#endif

    return floatclock(info);
#endif
}