]> rtime.felk.cvut.cz Git - frescor/fosa.git/blobdiff - src_marte_os/tests/test_cpu_clocks/test_cpu_clocks.c
Renaming fosa/src_marte to fosa/src_marte_os
[frescor/fosa.git] / src_marte_os / tests / test_cpu_clocks / test_cpu_clocks.c
diff --git a/src_marte_os/tests/test_cpu_clocks/test_cpu_clocks.c b/src_marte_os/tests/test_cpu_clocks/test_cpu_clocks.c
new file mode 100644 (file)
index 0000000..885bc96
--- /dev/null
@@ -0,0 +1,204 @@
+// -----------------------------------------------------------------------
+//  Copyright (C) 2006 - 2008 FRESCOR consortium partners:
+//
+//    Universidad de Cantabria,              SPAIN
+//    University of York,                    UK
+//    Scuola Superiore Sant'Anna,            ITALY
+//    Kaiserslautern University,             GERMANY
+//    Univ. Politécnica  Valencia,           SPAIN
+//    Czech Technical University in Prague,  CZECH REPUBLIC
+//    ENEA                                   SWEDEN
+//    Thales Communication S.A.              FRANCE
+//    Visual Tools S.A.                      SPAIN
+//    Rapita Systems Ltd                     UK
+//    Evidence                               ITALY
+//
+//    See http://www.frescor.org for a link to partners' websites
+//
+//           FRESCOR project (FP6/2005/IST/5-034026) is funded
+//        in part by the European Union Sixth Framework Programme
+//        The European Union is not liable of any use that may be
+//        made of this code.
+//
+//
+//  based on previous work (FSF) done in the FIRST project
+//
+//   Copyright (C) 2005  Mälardalen University, SWEDEN
+//                       Scuola Superiore S.Anna, ITALY
+//                       Universidad de Cantabria, SPAIN
+//                       University of York, UK
+//
+//   FSF API web pages: http://marte.unican.es/fsf/docs
+//                      http://shark.sssup.it/contrib/first/docs/
+//
+//   This file is part of FOSA (Frsh Operating System Adaption)
+//
+//  FOSA is free software; you can redistribute it and/or modify it
+//  under terms of the GNU General Public License as published by the
+//  Free Software Foundation; either version 2, or (at your option) any
+//  later version.  FOSA is distributed in the hope that it will be
+//  useful, but WITHOUT ANY WARRANTY; without even the implied warranty
+//  of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+//  General Public License for more details. You should have received a
+//  copy of the GNU General Public License along with FOSA; see file
+//  COPYING. If not, write to the Free Software Foundation, 675 Mass Ave,
+//  Cambridge, MA 02139, USA.
+//
+//  As a special exception, including FOSA header files in a file,
+//  instantiating FOSA generics or templates, or linking other files
+//  with FOSA objects to produce an executable application, does not
+//  by itself cause the resulting executable application to be covered
+//  by the GNU General Public License. This exception does not
+//  however invalidate any other reasons why the executable file might be
+//  covered by the GNU Public License.
+// -----------------------------------------------------------------------
+
+
+#include <unistd.h>
+#include <stdio.h>
+
+#include <assert.h>
+#include <stdlib.h> // for exit in assert
+#include <string.h> // for memset
+
+#include "fosa.h"
+
+/*****************************/
+/*   D E F I N I T I O N S   */
+/*****************************/
+#define RT_ERROR_SIGWAIT -2
+#define RT_ERROR_TIMER   -3
+
+#define SIGNAL_TIMER  FOSA_SIGNAL_MAX - 1
+
+/***************************/
+/*   P R O T O T Y P E S   */
+/***************************/
+static void * thread_body(void *thread_arg);
+
+
+static fosa_abs_time_t start_execution;
+static fosa_abs_time_t signal_reception;
+
+int main ()
+{
+    int err = -1;
+
+    fosa_thread_attr_t attr;
+    fosa_thread_id_t tid;
+
+    fosa_signal_t signal_received;
+    fosa_signal_info_t info_programmed, info_received;
+    fosa_rel_time_t work_interval = fosa_msec_to_rel_time(3200); // 3.2 seconds
+    fosa_clock_id_t clockid;
+    fosa_timer_id_t timerid;
+
+    fosa_rel_time_t budget;
+    fosa_rel_time_t elapsed_time;
+    fosa_signal_t signal_set[1];
+
+    /* Set the signal mask */
+    /***********************/
+    signal_set[0] = SIGNAL_TIMER;
+    if (fosa_set_accepted_signals(signal_set, 1) !=0)
+    {
+        printf ("Error while setting the signal mask\n");
+        exit (1);
+    }
+
+    /* Create the thread attributes and define its priority */
+    /********************************************************/
+    if (fosa_thread_attr_init (&attr) != 0) {
+        printf("Error while initializing the attributes\n");
+        exit(1);
+    }
+
+    if (fosa_thread_attr_set_prio (&attr,fosa_get_priority_min()+3) != 0) {
+        printf("Error while setting schedparam\n");
+        exit(1);
+    }
+
+    /* create the periodic thread.  It won't execute yet */
+    /* because it has lower priority than main().        */
+    /*****************************************************/
+    err = fosa_thread_create(&tid, &attr, thread_body, NULL);
+    if (err) {
+        printf("pthread_create failed thread\n");
+        exit(1);
+    }
+
+    /* Get the thread's cputime clock */
+    /**********************************/
+    if (fosa_thread_get_cputime_clock(tid, &clockid) !=0)
+    {
+        exit(RT_ERROR_TIMER);
+    }
+
+    /* Create a timer and arm it with a given budget */
+    /*************************************************/
+    info_programmed.sival_int = 42;
+    err = fosa_timer_create(clockid, SIGNAL_TIMER, info_programmed, &timerid);
+    printf("timer created, err=%d\n", err);
+    assert(err == 0);
+
+    budget = fosa_msec_to_rel_time(2500);
+    err = fosa_rel_timer_arm(timerid, &budget);
+    printf("timer armed for 2.5 secs, err=%d\n", err);
+    assert(err == 0);
+
+    /* We execute a little bit to ensure that execution time */
+    /* and real time differ                                  */
+    /*********************************************************/
+    printf("Main works for some time...\n");
+    fosa_eat(&work_interval);
+
+    /* Now we do the wait in order to allow the thread to run */
+    /**********************************************************/
+    printf("About to do the wait\n");
+    err = fosa_signal_wait(signal_set, 1 ,&signal_received, &info_received);
+
+    fosa_clock_get_time(FOSA_CLOCK_REALTIME, &signal_reception);
+    elapsed_time = fosa_abs_time_extract_interval(start_execution, signal_reception);
+
+    printf("signal received=%d value=%d (42?), err=%d\n",
+           signal_received, info_received.sival_int, err);
+
+    printf("Elapsed time between sigwait and timer expiration: %ld msecs\n",
+           fosa_rel_time_to_msec(elapsed_time) );
+
+    return 0;
+}
+
+
+// ----------------------------------------------------------------
+
+static void * thread_body(void *thread_arg)
+{
+    fosa_abs_time_t before_work_time;
+    fosa_abs_time_t after_work_time;
+    fosa_rel_time_t work_interval = fosa_msec_to_rel_time(1400);
+    fosa_rel_time_t elapsed_time;
+    int err;
+
+    fosa_clock_get_time(FOSA_CLOCK_REALTIME, &start_execution);
+
+    while(1)
+    {
+        err = fosa_clock_get_time(FOSA_CLOCK_REALTIME, &before_work_time);
+        assert(err == 0);
+        printf("Start periodic work  at %ld msecs\n", fosa_abs_time_to_msec(before_work_time) );
+
+        fosa_eat(&work_interval);
+
+        err = fosa_clock_get_time(FOSA_CLOCK_REALTIME, &after_work_time);
+        assert(err == 0);
+
+        printf("End periodic work  at %ld msecs\n", fosa_abs_time_to_msec(after_work_time) );
+        elapsed_time = fosa_abs_time_extract_interval(before_work_time, after_work_time);
+
+        printf("Elapsed time:  %ld msecs\n", fosa_rel_time_to_msec(elapsed_time) );
+
+    } // while
+    return NULL;
+}
+