freertos-addons icon indicating copy to clipboard operation
freertos-addons copied to clipboard

Simulation under MacOS?

Open spachner opened this issue 6 years ago • 1 comments
trafficstars

Hi,

many thanks for the great work!

Any chance to get the simulation work under MacOS?

regards

spachner

spachner avatar Jul 11 '19 18:07 spachner

Hi,

I did it myself. Thanks to https://yyshen.github.io/2015/01/18/binding_threads_to_cores_osx.html

Just added some missing includes and change two types. FreeRTOS simulation runs now fine under MacOS 10.14.4.

#if defined  __APPLE__
// taken from https://yyshen.github.io/2015/01/18/binding_threads_to_cores_osx.html

#include <sys/sysctl.h>

#include <mach/thread_policy.h>
#include <mach/thread_act.h>

#define SYSCTL_CORE_COUNT   "machdep.cpu.core_count"
#define UNUSED(v)          (void)(v)

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)); }

int sched_getaffinity(pid_t pid, size_t cpu_size, cpu_set_t *cpu_set)
{
    UNUSED(pid);
    UNUSED(cpu_size);
    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;
}

int pthread_setaffinity_np(pthread_t thread, size_t cpu_size, cpu_set_t *cpu_set)
{
    mach_port_t mach_thread;
    size_t core = 0;

    for (core = 0; core < 8 * cpu_size; core++) {
        if (CPU_ISSET(core, cpu_set)) break;
    }
    printf("binding to core %d\n", core);
    uint32_t periodMs = 1;
    thread_time_constraint_policy_data_t policy = {
        .period      = (uint32_t) (periodMs * 1000000),
        .computation = 50000,
        .constraint  = policy.period,
        .preemptible = true,
    };
    mach_thread = pthread_mach_thread_np(thread);
    thread_policy_set(mach_thread, THREAD_AFFINITY_POLICY, (thread_policy_t)&policy, 1);
    return 0;
}
#endif

spachner avatar Jul 11 '19 18:07 spachner

Have you tried the newer supported POSIX port contained within the FreeRTOS sources?

I'm going to close this because adding a MacOS port should be an enhancement under FreeRTOS itself, not the Add-ons.

michaelbecker avatar Jul 07 '23 14:07 michaelbecker