Created
January 24, 2026 17:40
-
-
Save tiborvass/3b0eb65e84088bc94357b5e5ba2f4218 to your computer and use it in GitHub Desktop.
raw walltime using commpage
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #include <stdio.h> | |
| #include <mach/mach_time.h> | |
| #define _COMM_PAGE_START_ADDRESS 0x0000000fffffc000 | |
| #define _COMM_PAGE_TIMEBASE_OFFSET (_COMM_PAGE_START_ADDRESS+0x88) | |
| #define _COMM_PAGE_NEWTIMEOFDAY_DATA (_COMM_PAGE_START_ADDRESS+0x120) | |
| typedef volatile struct commpage_timeofday_data { | |
| uint64_t TimeStamp_tick; | |
| uint64_t TimeStamp_sec; | |
| uint64_t TimeStamp_frac; | |
| uint64_t Ticks_scale; | |
| uint64_t Ticks_per_sec; | |
| } new_commpage_timeofday_data_t; | |
| int main(void) { | |
| uint64_t timebase_offset = *(uint64_t*)_COMM_PAGE_TIMEBASE_OFFSET; | |
| new_commpage_timeofday_data_t tdata = *(new_commpage_timeofday_data_t*)_COMM_PAGE_NEWTIMEOFDAY_DATA; | |
| uint64_t hwticks; | |
| __asm__ volatile( | |
| "mrs %0, S3_4_C15_C10_6" | |
| : "=r"(hwticks) | |
| : | |
| : | |
| ); | |
| uint64_t abs_time = hwticks + timebase_offset; | |
| uint64_t delta = abs_time - tdata.TimeStamp_tick; | |
| __uint128_t p = (__uint128_t) delta * tdata.Ticks_scale; | |
| __uint128_t t = ((__uint128_t)tdata.TimeStamp_sec << 64) | tdata.TimeStamp_frac; | |
| uint64_t wall_sec = (uint64_t)(t>>64); | |
| printf("%llu\n", wall_sec); | |
| return 0; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment