2 * \file ferret/examples/scalar_demo/main.c
3 * \brief Example demonstrating the usage of scalar sensors with Ferret/RE.
4 * Based on Martin Pohlack's original Ferret package for L4Env.
7 * \author Martin Pohlack <mp26@os.inf.tu-dresden.de>
8 * \author Bjoern Doebel <doebel@tudos.org>
11 * (c) 2005-2009 Technische Universität Dresden
12 * This file is part of TUD:OS and distributed under the terms of the
13 * GNU General Public License 2.
14 * Please see the COPYING-GPL-2 file for details.
19 #include <l4/re/c/util/cap_alloc.h>
20 #include <l4/re/c/namespace.h>
21 #include <l4/util/util.h>
22 #include <l4/util/rand.h>
24 #include <l4/ferret/client.h>
25 #include <l4/ferret/comm.h>
26 #include <l4/ferret/sensors/scalar.h>
27 #include <l4/ferret/sensors/scalar_producer.h>
28 #include <l4/ferret/sensors/scalar_consumer.h>
30 #include <l4/ferret/clock.h>
31 #include <l4/ferret/types.h>
35 printf("Hello from the scalar demo.\n");
37 l4_cap_idx_t srv = lookup_sensordir();
39 // -=# monitoring code start #=-
40 ferret_scalar_t * s1 = NULL, * s2 = NULL, * s3 = NULL, * s4 = NULL;
41 int ret, i, j, loops, pm;
42 ferret_utime_t start, end;
45 // create sensor and configure it to a value range of 0 -- 100 and
47 ret = ferret_create(srv, 10, 1, 0, FERRET_SCALAR,
48 0, "0:100:%", s1, &malloc);
51 printf("Error creating sensor: ret = %d\n", ret);
55 // create sensor and configure it to a value range of 0 -- 1000 and
56 // the unit 'time [us]'
57 ret = ferret_create(srv, 10, 2, 0, FERRET_SCALAR,
58 0, "0:1000:time [us]", s2, &malloc);
61 printf("Error creating sensor: ret = %d\n", ret);
65 // create sensor and configure it to a value range of 0 -- 100000 and
66 // the unit 'progress'
67 ret = ferret_create(srv, 10, 3, 0, FERRET_SCALAR,
68 0, "0:1000:progress", s3, &malloc);
71 printf("Error creating sensor: ret = %d\n", ret);
75 // create sensor and configure it to a value range of 0 -- 100000 and
76 // the unit 'Bandwidth [MB/s]'
77 ret = ferret_create(srv, 10, 4, 0, FERRET_SCALAR,
78 0, "0:67:Bandwidth [MB/s]", s4, &malloc);
81 printf("Error creating sensor: ret = %d\n", ret);
84 // -=# monitoring code end #=-
89 printf("-= Done creating sensors. Starting to produce data.=-\n");
94 // -=# monitoring code start #=-
95 // simply insert a local value to transport it to a monitor
96 ferret_scalar_put(s1, pm / 10);
97 // -=# monitoring code end #=-
99 // -=# monitoring code start #=-
100 FERRET_GET_TIME(FERRET_TIME_REL_US, start);
101 // -=# monitoring code end #=-
104 loops = 10000 + l4util_rand();
105 for (i = 0; i < loops; i++)
106 asm volatile ("": : : "memory");
108 // -=# monitoring code start #=-
109 // measure some duration and transport it to a monitor
110 //FERRET_GET_TIME(FERRET_TIME_REL_US_FAST, end); // does not work
112 FERRET_GET_TIME(FERRET_TIME_REL_US, end);
113 ferret_scalar_put(s2, end - start);
114 // -=# monitoring code end #=-
116 // -=# monitoring code start #=-
117 // another simple value transported
118 ferret_scalar_put(s3, j);
119 // -=# monitoring code end #=-
121 // -=# monitoring code start #=-
122 // make some internal statistic available externally
123 // bandwidth will be between 30 and 65 MB/s
124 ferret_scalar_put(s4, 30 + l4util_rand() % 35);
125 // -=# monitoring code end #=-
131 // -=# monitoring code start #=-
132 /* will never be reached due to the infinite loop above, but
133 * demonstrates cleanup.
135 * The sensor will be freed after all parties released it, that
136 * is, the creator and all monitors.
138 ret = ferret_free_sensor(10, 1, 0, s1, &free);
141 printf("Error freeing sensor: ret = %d\n", ret);
145 ret = ferret_free_sensor(10, 2, 0, s2, &free);
148 printf("Error freeing sensor: ret = %d\n", ret);
152 ret = ferret_free_sensor(10, 3, 0, s3, &free);
155 printf("Error freeing sensor: ret = %d\n", ret);
159 ret = ferret_free_sensor(10, 4, 0, s4, &free);
162 printf("Error freeing sensor: ret = %d\n", ret);
166 // -=# monitoring code end #=-