concurrency-hierarchy-bench icon indicating copy to clipboard operation
concurrency-hierarchy-bench copied to clipboard

macOS arm64 support

Open chadbrewbaker opened this issue 4 years ago • 0 comments

As far as I got today. I used http://www.hybridkernel.com/2015/01/18/binding_threads_to_cores_osx.html as a reference.

Still getting some C++ errors, probably have something configured wrong with the standards setting:

error: no matching constructor for initialization of 'std::vector<test_func>'
std::vector<test_func> ALL_FUNCS = {
#ifndef __APPLE__
#include <error.h>
#include <sys/sysinfo.h>
#endif

#ifdef __APPLE__
#include <sys/types.h>
#include <sys/sysctl.h>
#include <mach/error.h>
#include <pthread.h>
#include <mach/thread_policy.h>
#include <mach/thread_act.h>
#include <mach/mach_types.h>
#define SYSCTL_CORE_COUNT   "machdep.cpu.core_count"

typedef struct cpu_set {
  uint32_t    count;
} cpu_set_t;

static inline void
CPU_ZERO(cpu_set_t *cs) { cs->count = 0; }

static inline void
CPU_SET(int num, cpu_set_t *cs) { cs->count |= (1 << num); }

static inline int
CPU_ISSET(int num, cpu_set_t *cs) { return (cs->count & (1 << num)); }

#define pid_t size_t
int sched_getaffinity(pid_t pid, size_t cpu_size, cpu_set_t *cpu_set)
{
  int32_t core_count = 0;
  size_t  len = sizeof(core_count);
  int ret = sysctlbyname(SYSCTL_CORE_COUNT, &core_count, &len, 0, 0);
  if (ret) {
    printf("error while get core count %d\n", ret);
    return -1;
  }
  cpu_set->count = 0;
  for (int i = 0; i < core_count; i++) {
    cpu_set->count |= (1 << i);
  }

  return 0;
}

//#sched_setaffinity(0, sizeof(cpuset), &cpuset)
//#define pid_t pthread_t
int sched_setaffinity(pthread_t thread, size_t cpu_size,
                           cpu_set_t *cpu_set)
{
    mach_port_t mach_thread;
  int core = 0;

  for (core = 0; core < 8 * cpu_size; core++) {
    if (CPU_ISSET(core, cpu_set)) break;
  }
  printf("binding to core %d\n", core);
  thread_affinity_policy_data_t policy = { core };
  mach_thread = pthread_mach_thread_np(thread);
  thread_policy_set(mach_thread, THREAD_AFFINITY_POLICY,
                    (thread_policy_t)&policy, 1);

  return 0;
}

#endif

chadbrewbaker avatar Nov 24 '20 19:11 chadbrewbaker