Skip to content

[HELP] Unexpected result of scheduling testing. #19370

Description

@wangtao13

Description

I want to compare the scheduler of Nuttx with other RTOS, so I wrote a simple test and run it in hello.c

static pthread_mutex_t g_mutex;
static volatile uint32_t g_count1 = 0;
static volatile uint32_t g_count2 = 0;
static volatile int isexit = 0;

static void *low_task1(char *argv)
{
    while (!isexit) {
        pthread_mutex_lock(&g_mutex);
        g_count1++;
        pthread_mutex_unlock(&g_mutex);
    }
    return 0;
}

static void * low_task2(char *arg)
{
    while (!isexit) {
        pthread_mutex_lock(&g_mutex);
        g_count2++;
        pthread_mutex_unlock(&g_mutex);
    }
    return 0;
}

static void *hi_task(char *argv)
{
    struct timespec start, end;
    uint32_t total_ops;
    double elapsed_ms;

    pthread_mutex_init(&g_mutex, NULL);

    clock_gettime(CLOCK_MONOTONIC, &start);

    sleep(5);

    clock_gettime(CLOCK_MONOTONIC, &end);

    total_ops = g_count1 + g_count2;
    elapsed_ms = (end.tv_sec - start.tv_sec) * 1000.0 +
                 (end.tv_nsec - start.tv_nsec) / 1000000.0;

    printf("Low task 1 count: %" PRIu32 "\n", g_count1);
    printf("Low task 2 count: %" PRIu32 "\n", g_count2);
    printf("Total operations: %" PRIu32 "\n", total_ops);
    printf("Elapsed time: %.2f ms\n", elapsed_ms);
    if (total_ops > 0) {
        printf("Average time per operation: %.3f us\n",
               (elapsed_ms * 1000.0) / total_ops);
    }

    pthread_mutex_destroy(&g_mutex);
    isexit = 1;

    return 0;
}

int cs_test_main(int argc, char *argv[])
{
    pthread_t hi, low1, low2;
    pthread_attr_t attr_hi, attr_low;
    struct sched_param param_hi, param_low;

    pthread_attr_init(&attr_hi);
    pthread_attr_init(&attr_low);

    param_hi.sched_priority = 200;
    pthread_attr_setschedpolicy(&attr_hi, SCHED_RR);
    pthread_attr_setschedparam(&attr_hi, &param_hi);
    pthread_attr_setinheritsched(&attr_low, PTHREAD_EXPLICIT_SCHED);

    if (pthread_create(&hi, &attr_hi, hi_task, NULL) != 0) {
        perror("pthread_create hi");
        return 1;
    }

    if (pthread_create(&low1, &attr_low, low_task1, NULL) != 0) {
        perror("pthread_create low1");
        return 1;
    }
    if (pthread_create(&low2, &attr_low, low_task2, NULL) != 0) {
        perror("pthread_create low2");
        return 1;
    }

    pthread_attr_destroy(&attr_hi);
    pthread_attr_destroy(&attr_low);

    pthread_join(hi, NULL);
    pthread_join(low1, NULL);
    pthread_join(low2, NULL);

    return 0;
}

A high prio task sleeps 5 seconds, then 2 lower tasks runs in ping-pong way be getting/putting mutex. After 5 seconds, high task shows the results.

Here is my testing results.

Low task 1 count: 1828625
Low task 2 count: 1716974
Total operations: 3545599
Elapsed time: *float* ms
Average time per operation: *float* us

In the same hardware, another RTOS got followings.

Low task 1 count: 1519473
Low task 2 count: 1519472
Total operations: 3038945

The Nuttx's configuration of scheduler is

CONFIG_RR_INTERVAL=200
# CONFIG_SCHED_SPORADIC is not set

I tested in qemu-arma7 with SCHED configurations,

cat .config | grep SCHED
# CONFIG_DEBUG_SCHED is not set
# CONFIG_SCHED_TICKLESS is not set
# CONFIG_SCHED_SPORADIC is not set
CONFIG_SCHED_HAVE_PARENT=y
# CONFIG_SCHED_CHILD_STATUS is not set
CONFIG_SCHED_WAITPID=y
# CONFIG_SCHED_DUMP_LEAK is not set
# CONFIG_SCHED_DUMP_ON_EXIT is not set
# CONFIG_SCHED_USER_IDENTITY is not set
# CONFIG_SCHED_THREAD_LOCAL is not set
# CONFIG_SCHED_IRQMONITOR is not set
# CONFIG_SCHED_CRITMONITOR is not set
CONFIG_SCHED_CPULOAD_NONE=y
# CONFIG_SCHED_CPULOAD_SYSCLK is not set
# CONFIG_SCHED_CPULOAD_EXTCLK is not set
CONFIG_SCHED_CPULOAD_TICKSPERSEC=100
CONFIG_SCHED_PROFILE_TICKSPERSEC=1000
# CONFIG_SCHED_INSTRUMENTATION is not set
CONFIG_SCHED_STACK_RECORD=0
CONFIG_SCHED_WORKQUEUE=y
CONFIG_SCHED_HPWORK=y
CONFIG_SCHED_HPNTHREADS=1
CONFIG_SCHED_HPWORKPRIORITY=192
CONFIG_SCHED_HPWORKSTACKSIZE=4096
CONFIG_SCHED_HPWORKSTACKSECTION=""
# CONFIG_SCHED_LPWORK is not set
# CONFIG_SCHED_BACKTRACE is not set
# CONFIG_SCHED_EVENTS is not set

and got this,

nsh> hello
Low task 1 count: 4312752
Low task 2 count: 3142026
Total operations: 7454778
Elapsed time: 5001.00 ms

My question is: Why counts of 2 two lower prio tasks are different too much, while in another RTOS, the two counts are very close?

Verification

  • I have verified before submitting the report.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Fields

    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions