Author: Philip Jenvey <pjen...@underboss.org> Branch: py3k-get_clock_info Changeset: r84772:8f5877a05ea5 Date: 2016-05-27 21:26 -0700 http://bitbucket.org/pypy/pypy/changeset/8f5877a05ea5/
Log: add get_clock_info('process_time') diff --git a/pypy/module/time/interp_time.py b/pypy/module/time/interp_time.py --- a/pypy/module/time/interp_time.py +++ b/pypy/module/time/interp_time.py @@ -934,7 +934,7 @@ if _WIN: # untested so far - def process_time(space): + def process_time(space, w_info=None): from rpython.rlib.rposix import GetCurrentProcess, GetProcessTimes current_process = GetCurrentProcess() with lltype.scoped_alloc(rwin32.FILETIME) as creation_time, \ @@ -947,29 +947,48 @@ kernel_time.c_dwHighDateTime << 32) user_time2 = (user_time.c_dwLowDateTime | user_time.c_dwHighDateTime << 32) + if w_info is not None: + fill_clock_info(space, w_info, + "GetProcessTimes()", 1e-7, True, False) return space.wrap((float(kernel_time2) + float(user_time2)) * 1e-7) else: have_times = hasattr(rposix, 'c_times') - def process_time(space): + def process_time(space, w_info=None): if HAS_CLOCK_GETTIME and ( cConfig.CLOCK_PROF is not None or cConfig.CLOCK_PROCESS_CPUTIME_ID is not None): if cConfig.CLOCK_PROF is not None: clk_id = cConfig.CLOCK_PROF + function = "clock_gettime(CLOCK_PROF)" else: clk_id = cConfig.CLOCK_PROCESS_CPUTIME_ID + function = "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_gettime(clk_id, tsres) + if ret == 0: + res = tsres.c_tv_sec + tsres.c_tv_nsec * 1e-9 + else: + res = 1e-9 + fill_clock_info(space, w_info, function, + res, True, False) return space.wrap(_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: + fill_clock_info(space, w_info, + "getrusage(RUSAGE_SELF)", + 1e-6, True, False) return space.wrap(decode_timeval(rusage.c_ru_utime) + decode_timeval(rusage.c_ru_stime)) if have_times: @@ -977,6 +996,10 @@ ret = rposix.c_times(tms) if rffi.cast(lltype.Signed, ret) != -1: cpu_time = float(tms.c_tms_utime + tms.c_tms_stime) + if w_info is not None: + fill_clock_info(space, w_info, "times()", + 1.0 / rposix.CLOCK_TICKS_PER_SECOND, + True, False) return space.wrap(cpu_time / rposix.CLOCK_TICKS_PER_SECOND) return clock(space) @@ -1016,6 +1039,13 @@ return space.wrap((1.0 * value) / CLOCKS_PER_SEC) +def fill_clock_info(space, w_info, impl, res, mono, adj): + space.setattr(w_info, space.wrap('implementation'), space.wrap(impl)) + space.setattr(w_info, space.wrap('resolution'), space.wrap(res)) + space.setattr(w_info, space.wrap('monotonic'), space.wrap(mono)) + space.setattr(w_info, space.wrap('adjustable'), space.wrap(adj)) + + def get_clock_info_dict(space, name): if name == "time": return 5#floattime(info) diff --git a/pypy/module/time/test/test_time.py b/pypy/module/time/test/test_time.py --- a/pypy/module/time/test/test_time.py +++ b/pypy/module/time/test/test_time.py @@ -397,3 +397,10 @@ # Not really sure what to test about this # At least this tests that the attr exists... assert clock_info.resolution > 0 + + def test_get_clock_info_process_time(self): + import time + clock_info = time.get_clock_info("process_time") + assert clock_info.monotonic + assert not clock_info.adjustable + assert clock_info.resolution > 0 _______________________________________________ pypy-commit mailing list pypy-commit@python.org https://mail.python.org/mailman/listinfo/pypy-commit