Skip to content

Instantly share code, notes, and snippets.

@cathay4t
Created July 21, 2020 06:47
Show Gist options
  • Save cathay4t/80f315b172db799a426b528f48a10008 to your computer and use it in GitHub Desktop.
Save cathay4t/80f315b172db799a426b528f48a10008 to your computer and use it in GitHub Desktop.
#!/usr/bin/python3
NSEC_PER_SEC = 10 ** 9
MAX_U64 = 2 ** 64
def jiffies_to_clock_t(user_hz, hz, jiffies):
tick_nsec = (NSEC_PER_SEC + hz / 2) / hz
if (tick_nsec % (NSEC_PER_SEC / user_hz)) == 0:
if hz < user_hz:
return int(jiffies * (user_hz / hz))
else:
return int(jiffies / (hz / user_hz))
else:
return int(jiffies * tick_nsec / (NSEC_PER_SEC / user_hz))
def clock_t_to_jiffies(user_hz, hz, clock):
if hz % user_hz == 0:
if clock >= MAX_U64 / (hz / user_hz):
return MAX_U64
return int(clock * (hz / user_hz))
if clock >= MAX_U64 / hz * user_hz:
return MAX_U64
return int(clock * hz / user_hz)
max_deviation = 0
for original_num in range(0, 2 ** 32):
user_hz = 100
hz = 250
new_num = jiffies_to_clock_t(
user_hz, hz, clock_t_to_jiffies(user_hz, hz, original_num)
)
a = abs(original_num - new_num)
if a > max_deviation:
max_deviation = a
print(f"{original_num}: {max_deviation}")
print(max_deviation)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment