2011-03-20 19:18:19 +01:00
|
|
|
/********************************************************************************
|
2021-06-10 07:51:27 +02:00
|
|
|
* apps/testing/ostest/roundrobin.c
|
2011-03-20 19:18:19 +01:00
|
|
|
*
|
2021-06-10 07:51:27 +02:00
|
|
|
* Licensed to the Apache Software Foundation (ASF) under one or more
|
|
|
|
* contributor license agreements. See the NOTICE file distributed with
|
|
|
|
* this work for additional information regarding copyright ownership. The
|
|
|
|
* ASF licenses this file to you under the Apache License, Version 2.0 (the
|
|
|
|
* "License"); you may not use this file except in compliance with the
|
|
|
|
* License. You may obtain a copy of the License at
|
2011-03-20 19:18:19 +01:00
|
|
|
*
|
2021-06-10 07:51:27 +02:00
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
2011-03-20 19:18:19 +01:00
|
|
|
*
|
2021-06-10 07:51:27 +02:00
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
|
|
|
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
|
|
|
|
* License for the specific language governing permissions and limitations
|
|
|
|
* under the License.
|
2011-03-20 19:18:19 +01:00
|
|
|
*
|
|
|
|
********************************************************************************/
|
|
|
|
|
|
|
|
/********************************************************************************
|
|
|
|
* Included Files
|
|
|
|
********************************************************************************/
|
|
|
|
|
|
|
|
#include <nuttx/config.h>
|
2015-07-24 21:04:40 +02:00
|
|
|
|
2011-03-20 19:18:19 +01:00
|
|
|
#include <stdio.h>
|
2012-11-05 21:02:56 +01:00
|
|
|
#include <stdbool.h>
|
2015-07-24 21:04:40 +02:00
|
|
|
#include <semaphore.h>
|
|
|
|
#include <sched.h>
|
|
|
|
|
2011-03-20 19:18:19 +01:00
|
|
|
#include "ostest.h"
|
|
|
|
|
|
|
|
#if CONFIG_RR_INTERVAL > 0
|
|
|
|
|
|
|
|
/********************************************************************************
|
2015-07-24 21:04:40 +02:00
|
|
|
* Pre-processor Definitions
|
2011-03-20 19:18:19 +01:00
|
|
|
********************************************************************************/
|
|
|
|
|
2012-11-05 21:02:56 +01:00
|
|
|
/* This numbers should be tuned for different processor speeds via .config file.
|
|
|
|
* With default values the test takes about 30s on Cortex-M3 @ 24MHz. With 32767
|
2021-06-10 11:56:16 +02:00
|
|
|
* range and 10 runs it takes ~320s.
|
|
|
|
*/
|
2011-03-20 19:18:19 +01:00
|
|
|
|
2018-12-12 23:37:38 +01:00
|
|
|
#ifndef CONFIG_TESTING_OSTEST_RR_RANGE
|
|
|
|
# define CONFIG_TESTING_OSTEST_RR_RANGE 10000
|
|
|
|
# warning "CONFIG_TESTING_OSTEST_RR_RANGE undefined, using default value = 10000"
|
|
|
|
#elif (CONFIG_TESTING_OSTEST_RR_RANGE < 1) || (CONFIG_TESTING_OSTEST_RR_RANGE > 32767)
|
|
|
|
# define CONFIG_TESTING_OSTEST_RR_RANGE 10000
|
|
|
|
# warning "Invalid value of CONFIG_TESTING_OSTEST_RR_RANGE, using default value = 10000"
|
2012-11-05 21:02:56 +01:00
|
|
|
#endif
|
2011-03-20 19:18:19 +01:00
|
|
|
|
2018-12-12 23:37:38 +01:00
|
|
|
#ifndef CONFIG_TESTING_OSTEST_RR_RUNS
|
|
|
|
# define CONFIG_TESTING_OSTEST_RR_RUNS 10
|
|
|
|
# warning "CONFIG_TESTING_OSTEST_RR_RUNS undefined, using default value = 10"
|
|
|
|
#elif (CONFIG_TESTING_OSTEST_RR_RUNS < 1) || (CONFIG_TESTING_OSTEST_RR_RUNS > 32767)
|
|
|
|
# define CONFIG_TESTING_OSTEST_RR_RUNS 10
|
|
|
|
# warning "Invalid value of CONFIG_TESTING_OSTEST_RR_RUNS, using default value = 10"
|
2012-11-05 21:02:56 +01:00
|
|
|
#endif
|
2011-03-20 19:18:19 +01:00
|
|
|
|
2015-07-24 21:04:40 +02:00
|
|
|
/********************************************************************************
|
|
|
|
* Private Data
|
|
|
|
********************************************************************************/
|
|
|
|
|
|
|
|
static sem_t g_rrsem;
|
|
|
|
|
2011-03-20 19:18:19 +01:00
|
|
|
/********************************************************************************
|
|
|
|
* Private Functions
|
|
|
|
********************************************************************************/
|
|
|
|
|
|
|
|
/********************************************************************************
|
2012-11-05 21:02:56 +01:00
|
|
|
* Name: get_primes
|
2011-03-20 19:18:19 +01:00
|
|
|
*
|
|
|
|
* Description
|
2012-11-05 21:02:56 +01:00
|
|
|
* This function searches for prime numbers in the most primitive way possible.
|
2011-03-20 19:18:19 +01:00
|
|
|
********************************************************************************/
|
|
|
|
|
2012-11-05 21:02:56 +01:00
|
|
|
static void get_primes(int *count, int *last)
|
2011-03-20 19:18:19 +01:00
|
|
|
{
|
2012-11-05 21:02:56 +01:00
|
|
|
int number;
|
|
|
|
int local_count = 0;
|
2013-02-26 15:09:43 +01:00
|
|
|
|
|
|
|
*last = 0; /* To make the compiler happy */
|
2012-11-05 21:02:56 +01:00
|
|
|
|
2018-12-12 23:37:38 +01:00
|
|
|
for (number = 1; number < CONFIG_TESTING_OSTEST_RR_RANGE; number++)
|
2021-06-10 11:56:16 +02:00
|
|
|
{
|
|
|
|
int div;
|
|
|
|
bool is_prime = true;
|
2012-11-05 21:02:56 +01:00
|
|
|
|
2021-06-10 11:56:16 +02:00
|
|
|
for (div = 2; div <= number / 2; div++)
|
2012-11-05 21:02:56 +01:00
|
|
|
if (number % div == 0)
|
2021-06-10 11:56:16 +02:00
|
|
|
{
|
|
|
|
is_prime = false;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (is_prime)
|
|
|
|
{
|
|
|
|
local_count++;
|
|
|
|
*last = number;
|
2011-03-20 19:18:19 +01:00
|
|
|
#if 0 /* We don't really care what the numbers are */
|
2021-06-10 11:56:16 +02:00
|
|
|
printf(" Prime %d: %d\n", local_count, number);
|
2011-03-20 19:18:19 +01:00
|
|
|
#endif
|
2021-06-10 11:56:16 +02:00
|
|
|
}
|
2011-03-20 19:18:19 +01:00
|
|
|
}
|
|
|
|
|
2012-11-05 21:02:56 +01:00
|
|
|
*count = local_count;
|
2011-03-20 19:18:19 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/********************************************************************************
|
2012-11-05 21:02:56 +01:00
|
|
|
* Name: get_primes_thread
|
2011-03-20 19:18:19 +01:00
|
|
|
********************************************************************************/
|
|
|
|
|
2013-02-26 15:09:43 +01:00
|
|
|
static FAR void *get_primes_thread(FAR void *parameter)
|
2011-03-20 19:18:19 +01:00
|
|
|
{
|
2017-04-07 23:47:16 +02:00
|
|
|
int id = (int)((intptr_t)parameter);
|
2013-02-26 15:09:43 +01:00
|
|
|
int count;
|
|
|
|
int last;
|
|
|
|
int i;
|
2011-03-20 19:18:19 +01:00
|
|
|
|
2015-07-24 21:04:40 +02:00
|
|
|
while (sem_wait(&g_rrsem) < 0);
|
|
|
|
|
2021-06-10 11:56:16 +02:00
|
|
|
printf("get_primes_thread id=%d started, "
|
|
|
|
"looking for primes < %d, doing %d run(s)\n",
|
2018-12-12 23:37:38 +01:00
|
|
|
id, CONFIG_TESTING_OSTEST_RR_RANGE, CONFIG_TESTING_OSTEST_RR_RUNS);
|
2011-03-20 19:18:19 +01:00
|
|
|
|
2018-12-12 23:37:38 +01:00
|
|
|
for (i = 0; i < CONFIG_TESTING_OSTEST_RR_RUNS; i++)
|
2011-03-20 19:18:19 +01:00
|
|
|
{
|
2012-11-05 21:02:56 +01:00
|
|
|
get_primes(&count, &last);
|
2011-03-20 19:18:19 +01:00
|
|
|
}
|
|
|
|
|
2012-11-05 21:02:56 +01:00
|
|
|
printf("get_primes_thread id=%d finished, found %d primes, last one was %d\n",
|
|
|
|
id, count, last);
|
2011-03-20 19:18:19 +01:00
|
|
|
|
|
|
|
pthread_exit(NULL);
|
|
|
|
return NULL; /* To keep some compilers happy */
|
|
|
|
}
|
|
|
|
|
|
|
|
/********************************************************************************
|
|
|
|
* Public Functions
|
|
|
|
********************************************************************************/
|
|
|
|
|
|
|
|
/********************************************************************************
|
|
|
|
* Name: rr_test
|
|
|
|
********************************************************************************/
|
|
|
|
|
|
|
|
void rr_test(void)
|
|
|
|
{
|
2012-11-05 21:02:56 +01:00
|
|
|
pthread_t get_primes1_thread;
|
|
|
|
pthread_t get_primes2_thread;
|
2011-03-20 19:18:19 +01:00
|
|
|
struct sched_param sparam;
|
|
|
|
pthread_attr_t attr;
|
|
|
|
pthread_addr_t result;
|
|
|
|
int status;
|
|
|
|
|
2015-07-24 21:04:40 +02:00
|
|
|
/* Setup common thread attrributes */
|
|
|
|
|
2011-03-20 19:18:19 +01:00
|
|
|
status = pthread_attr_init(&attr);
|
|
|
|
if (status != OK)
|
|
|
|
{
|
2013-02-26 15:09:43 +01:00
|
|
|
printf("rr_test: ERROR: pthread_attr_init failed, status=%d\n", status);
|
2011-03-20 19:18:19 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
sparam.sched_priority = sched_get_priority_min(SCHED_FIFO);
|
|
|
|
status = pthread_attr_setschedparam(&attr, &sparam);
|
|
|
|
if (status != OK)
|
|
|
|
{
|
2021-06-10 11:56:16 +02:00
|
|
|
printf("rr_test: ERROR: pthread_attr_setschedparam failed, status=%d\n",
|
|
|
|
status);
|
2011-03-20 19:18:19 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
printf("rr_test: Set thread priority to %d\n", sparam.sched_priority);
|
|
|
|
}
|
|
|
|
|
|
|
|
status = pthread_attr_setschedpolicy(&attr, SCHED_RR);
|
|
|
|
if (status != OK)
|
|
|
|
{
|
2021-06-10 11:56:16 +02:00
|
|
|
printf("rr_test: ERROR: pthread_attr_setschedpolicy failed, status=%d\n",
|
|
|
|
status);
|
2011-03-20 19:18:19 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2012-11-05 21:02:56 +01:00
|
|
|
printf("rr_test: Set thread policy to SCHED_RR\n");
|
2011-03-20 19:18:19 +01:00
|
|
|
}
|
|
|
|
|
2015-07-24 21:50:31 +02:00
|
|
|
/* This semaphore will prevent anything from running until we are ready */
|
2015-07-24 21:04:40 +02:00
|
|
|
|
|
|
|
sched_lock();
|
|
|
|
sem_init(&g_rrsem, 0, 0);
|
|
|
|
|
|
|
|
/* Start the threads */
|
|
|
|
|
2012-11-05 21:02:56 +01:00
|
|
|
printf("rr_test: Starting first get_primes_thread\n");
|
|
|
|
|
2021-06-10 11:56:16 +02:00
|
|
|
status = pthread_create(&get_primes1_thread,
|
|
|
|
&attr, get_primes_thread, (FAR void *)1);
|
2011-03-20 19:18:19 +01:00
|
|
|
if (status != 0)
|
|
|
|
{
|
2013-02-26 15:09:43 +01:00
|
|
|
printf(" ERROR: Thread 1 creation failed: %d\n", status);
|
2011-03-20 19:18:19 +01:00
|
|
|
}
|
|
|
|
|
2013-02-26 15:09:43 +01:00
|
|
|
printf(" First get_primes_thread: %d\n", (int)get_primes1_thread);
|
2012-11-05 21:02:56 +01:00
|
|
|
printf("rr_test: Starting second get_primes_thread\n");
|
2011-03-20 19:18:19 +01:00
|
|
|
|
2021-06-10 11:56:16 +02:00
|
|
|
status = pthread_create(&get_primes2_thread,
|
|
|
|
&attr, get_primes_thread, (FAR void *)2);
|
2011-03-20 19:18:19 +01:00
|
|
|
if (status != 0)
|
|
|
|
{
|
2013-02-26 15:09:43 +01:00
|
|
|
printf(" ERROR: Thread 2 creation failed: %d\n", status);
|
2011-03-20 19:18:19 +01:00
|
|
|
}
|
|
|
|
|
2013-02-26 15:09:43 +01:00
|
|
|
printf(" Second get_primes_thread: %d\n", (int)get_primes2_thread);
|
2021-06-10 11:56:16 +02:00
|
|
|
printf("rr_test: "
|
|
|
|
"Waiting for threads to complete -- this should take awhile\n");
|
|
|
|
printf(" "
|
|
|
|
"If RR scheduling is working, they should start and complete at\n");
|
2013-02-26 15:09:43 +01:00
|
|
|
printf(" about the same time\n");
|
2011-03-20 19:18:19 +01:00
|
|
|
|
2015-07-24 21:04:40 +02:00
|
|
|
sem_post(&g_rrsem);
|
|
|
|
sem_post(&g_rrsem);
|
|
|
|
sched_unlock();
|
|
|
|
|
2012-11-05 21:02:56 +01:00
|
|
|
pthread_join(get_primes2_thread, &result);
|
|
|
|
pthread_join(get_primes1_thread, &result);
|
2011-03-20 19:18:19 +01:00
|
|
|
printf("rr_test: Done\n");
|
2015-07-24 21:50:31 +02:00
|
|
|
sem_destroy(&g_rrsem);
|
2011-03-20 19:18:19 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
#endif /* CONFIG_RR_INTERVAL */
|