def clock_gettime(space, clk_id):
     with lltype.scoped_alloc(TIMESPEC) as timespec:
         ret = c_clock_gettime(clk_id, timespec)
         if ret != 0:
             raise exception_from_saved_errno(space, space.w_OSError)
         secs = _timespec_to_seconds(timespec)
     return space.newfloat(secs)
示例#2
0
 def clock_gettime(space, clk_id):
     with lltype.scoped_alloc(rtime.TIMESPEC) as tp:
         ret = rtime.c_clock_gettime(clk_id, tp)
         if ret != 0:
             raise exception_from_saved_errno(space, space.w_IOError)
         t = float(rffi.getintfield(tp, "c_tv_sec")) + float(rffi.getintfield(tp, "c_tv_nsec")) * 0.000000001
     return space.wrap(t)
 def clock_gettime(space, clk_id):
     with lltype.scoped_alloc(rtime.TIMESPEC) as tp:
         ret = rtime.c_clock_gettime(clk_id, tp)
         if ret != 0:
             raise exception_from_saved_errno(space, space.w_IOError)
         t = (float(rffi.getintfield(tp, 'c_tv_sec')) +
              float(rffi.getintfield(tp, 'c_tv_nsec')) * 0.000000001)
     return space.newfloat(t)
示例#4
0
 def test_clock_gettime(self):
     if not rtime.HAS_CLOCK_GETTIME:
         py.test.skip("no clock_gettime()")
     lst = []
     for i in range(50):
         with lltype.scoped_alloc(rtime.TIMESPEC) as a1:
             res = rtime.c_clock_gettime(rtime.CLOCK_MONOTONIC, a1)
             assert res == 0
             t = (float(rffi.getintfield(a1, 'c_tv_sec')) +
                  float(rffi.getintfield(a1, 'c_tv_nsec')) * 0.000000001)
             lst.append(t)
     assert lst == sorted(lst)
示例#5
0
 def test_clock_gettime(self):
     if not rtime.HAS_CLOCK_GETTIME:
         py.test.skip("no clock_gettime()")
     lst = []
     for i in range(50):
         with lltype.scoped_alloc(rtime.TIMESPEC) as a1:
             res = rtime.c_clock_gettime(rtime.CLOCK_MONOTONIC, a1)
             assert res == 0
             t = (float(rffi.getintfield(a1, 'c_tv_sec')) +
                  float(rffi.getintfield(a1, 'c_tv_nsec')) * 0.000000001)
             lst.append(t)
     assert lst == sorted(lst)
    def process_time(space, w_info=None):
        if HAS_CLOCK_GETTIME and (rtime.CLOCK_PROF is not None or
                                  rtime.CLOCK_PROCESS_CPUTIME_ID is not None):
            if rtime.CLOCK_PROF is not None:
                clk_id = rtime.CLOCK_PROF
                implementation = "clock_gettime(CLOCK_PROF)"
            else:
                clk_id = rtime.CLOCK_PROCESS_CPUTIME_ID
                implementation = "clock_gettime(CLOCK_PROCESS_CPUTIME_ID)"
            with lltype.scoped_alloc(TIMESPEC) as timespec:
                ret = c_clock_gettime(clk_id, timespec)
                if ret == 0:
                    if w_info is not None:
                        with lltype.scoped_alloc(TIMESPEC) as tsres:
                            ret = c_clock_getres(clk_id, tsres)
                            if ret == 0:
                                res = _timespec_to_seconds(tsres)
                            else:
                                res = 1e-9
                        _setinfo(space, w_info, implementation, res, True,
                                 False)
                    return space.newfloat(_timespec_to_seconds(timespec))

        if True:  # XXX available except if it isn't?
            from rpython.rlib.rtime import (c_getrusage, RUSAGE, RUSAGE_SELF,
                                            decode_timeval)
            with lltype.scoped_alloc(RUSAGE) as rusage:
                ret = c_getrusage(RUSAGE_SELF, rusage)
                if ret == 0:
                    if w_info is not None:
                        _setinfo(space, w_info, "getrusage(RUSAGE_SELF)", 1e-6,
                                 True, False)
                    return space.newfloat(
                        decode_timeval(rusage.c_ru_utime) +
                        decode_timeval(rusage.c_ru_stime))
        if have_times:
            with lltype.scoped_alloc(rposix.TMS) as tms:
                ret = rposix.c_times(tms)
                if rffi.cast(lltype.Signed, ret) != -1:
                    cpu_time = float(
                        rffi.cast(lltype.Signed, tms.c_tms_utime) +
                        rffi.cast(lltype.Signed, tms.c_tms_stime))
                    if w_info is not None:
                        _setinfo(space, w_info, "times()",
                                 1.0 / rposix.CLOCK_TICKS_PER_SECOND, True,
                                 False)
                    return space.newfloat(cpu_time /
                                          rposix.CLOCK_TICKS_PER_SECOND)
        return clock(space)
示例#7
0
 def method_clock_gettime(self, space, clockid, args_w):
     if len(args_w) > 1:
         raise space.error(space.w_ArgumentError,
             "wrong number of arguments (given %d, expected 1..2)"
         )
     if len(args_w) == 1:
         unit = Coerce.symbol(space, args_w[0])
     else:
         unit = "float_second"
     if rtime.HAS_CLOCK_GETTIME:
         with lltype.scoped_alloc(rtime.TIMESPEC) as a:
             if rtime.c_clock_gettime(clockid, a) == 0:
                 sec = rffi.getintfield(a, 'c_tv_sec')
                 nsec = rffi.getintfield(a, 'c_tv_nsec')
             else:
                 raise error_for_oserror(space, OSError(
                     errno.EINVAL, "clock_gettime")
                 )
     elif clockid == CLOCK_PROCESS_CPUTIME_ID:
         r = rtime.clock()
         sec = int(r)
         nsec = r * 1000000000
     else:
         raise error_for_oserror(space, OSError(
             errno.EINVAL, "clock_gettime")
         )
     if unit == "float_second":
         return space.newfloat(sec + nsec * 0.000000001)
     elif unit == "float_millisecond":
         return space.newfloat(sec * 1000 + nsec * 0.000001)
     elif unit == "float_microsecond":
         return space.newfloat(sec * 1000000 + nsec * 0.001)
     elif unit == "second":
         return space.newint(int(sec))
     elif unit == "millisecond":
         return space.newint(int(sec) * 1000)
     elif unit == "microsecond":
         return space.newint(sec * 1000000)
     elif unit == "nanosecond":
         return space.newint(sec * 1000000000 + int(nsec))
     else:
         raise space.error(space.w_ArgumentError,
             "unexpected unit: %s" % unit
         )
示例#8
0
 def method_clock_gettime(self, space, clockid, args_w):
     if len(args_w) > 1:
         raise space.error(
             space.w_ArgumentError,
             "wrong number of arguments (given %d, expected 1..2)")
     if len(args_w) == 1:
         unit = Coerce.symbol(space, args_w[0])
     else:
         unit = "float_second"
     if rtime.HAS_CLOCK_GETTIME:
         with lltype.scoped_alloc(rtime.TIMESPEC) as a:
             if rtime.c_clock_gettime(clockid, a) == 0:
                 sec = rffi.getintfield(a, 'c_tv_sec')
                 nsec = rffi.getintfield(a, 'c_tv_nsec')
             else:
                 raise error_for_oserror(
                     space, OSError(errno.EINVAL, "clock_gettime"))
     elif clockid == CLOCK_PROCESS_CPUTIME_ID:
         r = rtime.clock()
         sec = int(r)
         nsec = r * 1000000000
     else:
         raise error_for_oserror(space,
                                 OSError(errno.EINVAL, "clock_gettime"))
     if unit == "float_second":
         return space.newfloat(sec + nsec * 0.000000001)
     elif unit == "float_millisecond":
         return space.newfloat(sec * 1000 + nsec * 0.000001)
     elif unit == "float_microsecond":
         return space.newfloat(sec * 1000000 + nsec * 0.001)
     elif unit == "second":
         return space.newint(int(sec))
     elif unit == "millisecond":
         return space.newint(int(sec) * 1000)
     elif unit == "microsecond":
         return space.newint(sec * 1000000)
     elif unit == "nanosecond":
         return space.newint(sec * 1000000000 + nsec)
     else:
         raise space.error(space.w_ArgumentError,
                           "unexpected unit: %s" % unit)
def time(space, w_info=None):
    """time() -> floating point number

    Return the current time in seconds since the Epoch.
    Fractions of a second may be present if the system clock provides them."""
    if HAS_CLOCK_GETTIME:
        with lltype.scoped_alloc(TIMESPEC) as timespec:
            ret = c_clock_gettime(rtime.CLOCK_REALTIME, timespec)
            if ret == 0:
                if w_info is not None:
                    with lltype.scoped_alloc(TIMESPEC) as tsres:
                        ret = c_clock_getres(rtime.CLOCK_REALTIME, tsres)
                        if ret == 0:
                            res = _timespec_to_seconds(tsres)
                        else:
                            res = 1e-9
                        _setinfo(space, w_info,
                                 "clock_gettime(CLOCK_REALTIME)", res, False,
                                 True)
                return space.newfloat(_timespec_to_seconds(timespec))
    else:
        return gettimeofday(space, w_info)