Skip to content

Commit abffe0f

Browse files
committed
- Add support to get Darwin (MacOS X) ticks per second.
- Fix unit test: - Comment out sysctl test until there is a working sysctl module - Coerce a value to long so the diff calculation will work correctly
1 parent f67e9cb commit abffe0f

File tree

2 files changed

+25
-17
lines changed

2 files changed

+25
-17
lines changed

aplib/aplib.tsc_time.pyx

+14-7
Original file line numberDiff line numberDiff line change
@@ -236,18 +236,25 @@ cdef uint64_t get_ticks_per_sec() except -1:
236236
cdef char buffer[128]
237237
cdef size_t buffer_size
238238

239+
# XXX - Need to find a way to get ticks per sec on Linux, fake it for now
239240
IF UNAME_SYSNAME == "Linux":
240241
return 2793008320
241242

242243
buffer_size = sizeof(buffer)
243244

244-
if sysctlbyname("machdep.tsc_freq_new", <void *>&buffer[0], &buffer_size, NULL, 0) == -1:
245-
if sysctlbyname("machdep.tsc_freq", <void *>&buffer[0], &buffer_size, NULL, 0) == -1:
246-
# Not all systems have this sysctl that we can build on
247-
if libc.getenv("BUILDING") == NULL:
248-
raise SystemError
249-
else:
250-
return 2793008320
245+
IF UNAME_SYSNAME == "Darwin":
246+
if sysctlbyname("machdep.tsc.frequency", <void *>&buffer[0], &buffer_size, NULL, 0) == -1:
247+
raise SystemError
248+
ELSE:
249+
# Leave this for backwards compatibility with 32/64-bit legacy systems
250+
if sysctlbyname("machdep.tsc_freq_new", <void *>&buffer[0], &buffer_size, NULL, 0) == -1:
251+
if sysctlbyname("machdep.tsc_freq", <void *>&buffer[0], &buffer_size, NULL, 0) == -1:
252+
# Not all systems have this sysctl that we can build on
253+
if libc.getenv("BUILDING") == NULL:
254+
raise SystemError
255+
else:
256+
return 2793008320
257+
251258
if buffer_size == 4:
252259
value = (<uint32_t *>buffer)[0];
253260
else:

test/test_tsc_time.py

+11-10
Original file line numberDiff line numberDiff line change
@@ -23,21 +23,22 @@
2323
__version__ = '$Revision: #2 $'
2424

2525
import struct
26-
import sysctl
26+
#import sysctl
2727
import time
2828
import unittest
2929
from aplib import tsc_time
3030

3131
class Test(unittest.TestCase):
3232

33-
def test_ticks_per_sec(self):
34-
freq = struct.unpack('I', sysctl.sysctl('machdep.tsc_freq'))[0]
35-
self.assertEqual(freq,
36-
tsc_time.ticks_per_sec
37-
)
38-
self.assertEqual(freq / 1000000,
39-
tsc_time.ticks_per_usec
40-
)
33+
# Stub test until we have a working sysctl
34+
#def test_ticks_per_sec(self):
35+
# freq = struct.unpack('I', sysctl.sysctl('machdep.tsc_freq'))[0]
36+
# self.assertEqual(freq,
37+
# tsc_time.ticks_per_sec
38+
# )
39+
# self.assertEqual(freq / 1000000,
40+
# tsc_time.ticks_per_usec
41+
# )
4142

4243
def _assert_close(self, a, b, diff):
4344
low = a - diff
@@ -303,7 +304,7 @@ def test_negative_time(self):
303304
)
304305

305306
# Microseconds in 1 year.
306-
diff = 365 * 24 * 60 * 60 * 1000000
307+
diff = long(365 * 24 * 60 * 60 * 1000000)
307308
now_usec = tsc_time.now_posix_usec()
308309
ago = now_usec - diff
309310

0 commit comments

Comments
 (0)