]> rtime.felk.cvut.cz Git - l4.git/blob - l4/pkg/ferret/examples/demo/scalar_demo/main.c
l4: ferret: Add "more work" into memory reading cycle to see a delay.
[l4.git] / l4 / pkg / ferret / examples / demo / scalar_demo / main.c
1 /**
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.
5  *
6  * \date   03/04/2009
7  * \author Martin Pohlack <mp26@os.inf.tu-dresden.de>
8  * \author Bjoern Doebel <doebel@tudos.org>
9  */
10 /*
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.
15  */
16 #include <stdlib.h>
17 #include <stdio.h>
18
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>
23
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>
29
30 #include <l4/ferret/clock.h>
31 #include <l4/ferret/types.h>
32
33 int main(void)
34 {
35         printf("Hello from the scalar demo.\n");
36
37         l4_cap_idx_t srv = lookup_sensordir();
38
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;
43
44     l4_sleep(1000);
45     // create sensor and configure it to a value range of 0 -- 100 and
46     // the unit '%'
47     ret = ferret_create(srv, 10, 1, 0, FERRET_SCALAR,
48                         0, "0:100:%", s1, &malloc);
49     if (ret)
50     {
51         printf("Error creating sensor: ret = %d\n", ret);
52         exit(1);
53     }
54
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);
59     if (ret)
60     {
61         printf("Error creating sensor: ret = %d\n", ret);
62         exit(1);
63     }
64
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);
69     if (ret)
70     {
71         printf("Error creating sensor: ret = %d\n", ret);
72         exit(1);
73     }
74
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);
79     if (ret)
80     {
81         printf("Error creating sensor: ret = %d\n", ret);
82         exit(1);
83     }
84     // -=# monitoring code end #=-
85
86     pm = 0;
87     j = 0;
88
89         printf("-= Done creating sensors. Starting to produce data.=-\n");
90
91     while (1)
92     {
93         pm = (pm + 1) % 1000;
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 #=-
98
99         // -=# monitoring code start #=-
100         FERRET_GET_TIME(FERRET_TIME_REL_US, start);
101         // -=# monitoring code end #=-
102
103         // do some work
104         loops = 1000000 + l4util_rand();
105         for (i = 0; i < loops; i++)
106             asm volatile ("": : : "memory");
107
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
111                                                            // with fiasco-ux
112         FERRET_GET_TIME(FERRET_TIME_REL_US, end);
113         ferret_scalar_put(s2, end - start);
114         // -=# monitoring code end #=-
115
116         // -=# monitoring code start #=-
117         // another simple value transported
118         ferret_scalar_put(s3, j);
119         // -=# monitoring code end #=-
120
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 #=-
126
127         l4_sleep(10);
128         j++;
129     }
130
131     // -=# monitoring code start #=-
132     /* will never be reached due to the infinite loop above, but
133      * demonstrates cleanup.
134      *
135      * The sensor will be freed after all parties released it, that
136      * is, the creator and all monitors.
137      */
138     ret = ferret_free_sensor(10, 1, 0, s1, &free);
139     if (ret)
140     {
141         printf("Error freeing sensor: ret = %d\n", ret);
142         exit(1);
143     }
144
145     ret = ferret_free_sensor(10, 2, 0, s2, &free);
146     if (ret)
147     {
148         printf("Error freeing sensor: ret = %d\n", ret);
149         exit(1);
150     }
151
152     ret = ferret_free_sensor(10, 3, 0, s3, &free);
153     if (ret)
154     {
155         printf("Error freeing sensor: ret = %d\n", ret);
156         exit(1);
157     }
158
159     ret = ferret_free_sensor(10, 4, 0, s4, &free);
160     if (ret)
161     {
162         printf("Error freeing sensor: ret = %d\n", ret);
163         exit(1);
164     }
165
166     // -=# monitoring code end #=-
167
168     return 0;
169 }