landmarks icon indicating copy to clipboard operation
landmarks copied to clipboard

Support for `arm64` / Apple M1

Open grantpassmore opened this issue 3 years ago • 7 comments

Thank you for the incredibly useful landmarks library! It works flawlessly for us on x86, but we seem to be out of luck for now on our Apple M1s. Is there a nontrivial technical obstruction for supporting arm64? If there is anything we could do to help, we would be happy to try.

grantpassmore avatar Dec 06 '21 16:12 grantpassmore

I am guessing this is due to the need for a specific implementation of the caml_highres_clock primitive in https://github.com/LexiFi/landmarks/blob/master/src/clock.c which uses the rdtsc instruction on x86. I'm not very familiar with arm64, but I imagine that there should be some equivalent instruction. cc @mlasson

nojb avatar Dec 06 '21 16:12 nojb

Thank you for investigating! In case it is useful, my (very much non-expert) understanding is that the equivalent to rdtsc can be obtained on arm64 via the cntvct_el0 register, e.g., something like

asm volatile ("mrs %0, cntvct_el0" : "=r" (_var_))

cf. https://community.arm.com/support-forums/f/architectures-and-processors-forum/7019/arm-v8-pmu-cycle-counter .

grantpassmore avatar Dec 06 '21 16:12 grantpassmore

We don't have any arm64 hardware to test this on, but you can give it a try by adding this to clock.c:

#elif defined(__aarch64__)
    uint64_t v;
    asm volatile ("mrs %0, cntvct_el0" : "=r"(v));
    return caml_copy_int64(v);
#else
    ...

nojb avatar Dec 06 '21 16:12 nojb

Fantastic, thank you. We will test today and report our findings!

grantpassmore avatar Dec 06 '21 16:12 grantpassmore

Is that the same primitive that mtime exposes?

c-cube avatar Dec 06 '21 17:12 c-cube

@grantpassmore

Fantastic, thank you. We will test today and report our findings!

Do not hesitate to file a PR if you want afterward !

@c-cube

Is that the same primitive that mtime exposes? No, but actually any monotonic wall-clock time. Maybe we probably should add a way to support other clocks.

mlasson avatar Dec 06 '21 17:12 mlasson

maybe a dune variant to provide the clock implementation could help? One could provide a mtime-based variant, for exotic architectures where it's better supported (which might be no better than landmarks' current coverage).

c-cube avatar Dec 06 '21 17:12 c-cube

Is there anyone still looking at this issue? cc: @grantpassmore

k4rtik avatar Nov 17 '22 15:11 k4rtik

We've experimented with this fix on: https://github.com/johnyob/landmarks/tree/arm-support.

It has been tested with Apple M1 macs.

johnyob avatar Feb 08 '23 21:02 johnyob

We've experimented with this fix on: https://github.com/johnyob/landmarks/tree/arm-support.

It has been tested with Apple M1 macs.

The patch looks fine, can you open a PR? Thanks!

nojb avatar Feb 09 '23 06:02 nojb

Fixed in #35

nojb avatar Feb 11 '23 16:02 nojb