Skip to content

Commit 9dcf231

Browse files
devnexenkevinAlbs
andauthored
CDRIVER-4241 SHM counter on mac m1 arm64 implementation. (#894)
Co-authored-by: Kevin Albertson <[email protected]>
1 parent 67d5d44 commit 9dcf231

File tree

3 files changed

+19
-3
lines changed

3 files changed

+19
-3
lines changed

src/libmongoc/CMakeLists.txt

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -380,13 +380,16 @@ if (ENABLE_SHM_COUNTERS STREQUAL "AUTO")
380380
if (UNIX AND NOT APPLE)
381381
set (ENABLE_SHM_COUNTERS ON)
382382
endif ()
383+
if (APPLE AND ${CMAKE_SYSTEM_PROCESSOR} MATCHES "arm64")
384+
set (ENABLE_SHM_COUNTERS ON)
385+
endif ()
383386
endif ()
384387

385388
if (ENABLE_SHM_COUNTERS STREQUAL "ON")
386-
if (APPLE OR NOT UNIX)
389+
if (( NOT ${CMAKE_SYSTEM_PROCESSOR} MATCHES "arm64" AND APPLE ) OR NOT UNIX)
387390
message (
388391
FATAL_ERROR
389-
"Shared memory performance counters not supported on Mac or Windows")
392+
"Shared memory performance counters only supported on arm64 Mac and Linux")
390393
endif ()
391394
set (MONGOC_ENABLE_SHM_COUNTERS 1)
392395
find_library(RT_LIBRARY rt HINTS /usr/lib32)

src/libmongoc/doc/basic-troubleshooting.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ If available, the counters can be accessed outside of the application process vi
2323
This means that you can graph statistics about your application process easily from tools like Munin or Nagios.
2424
Your author often uses ``watch --interval=0.5 -d mongoc-stat $PID`` to monitor an application.
2525

26-
Performance counters are only available on Linux platforms supporting shared memory segments.
26+
Performance counters are only available on Linux platforms and macOS arm64 platforms supporting shared memory segments.
2727
On supported platforms they are enabled by default.
2828
Applications can be built without the counters by specifying the cmake option ``-DENABLE_SHM_COUNTERS=OFF``. Additionally, if
2929
performance counters are already compiled, they can be disabled at runtime by specifying the environment variable ``MONGOC_DISABLE_SHM``.

src/libmongoc/src/mongoc/mongoc-counters-private.h

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,19 @@ _mongoc_sched_getcpu (void)
101101
}
102102
#elif defined(MONGOC_HAVE_SCHED_GETCPU)
103103
#define _mongoc_sched_getcpu sched_getcpu
104+
#elif defined(__APPLE__) && defined(__aarch64__)
105+
static BSON_INLINE unsigned
106+
_mongoc_sched_getcpu (void)
107+
{
108+
uintptr_t tls;
109+
unsigned core_id;
110+
/* Get the current thread ID, not the core ID.
111+
* Getting the core ID requires privileged execution. */
112+
__asm__ volatile("mrs %x0, tpidrro_el0" : "=r"(tls));
113+
/* In ARM, only 8 cores are manageable. */
114+
core_id = tls & 0x07u;
115+
return core_id;
116+
}
104117
#else
105118
#define _mongoc_sched_getcpu() (0)
106119
#endif

0 commit comments

Comments
 (0)