Contiki-NG
simple-energest.c
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2018, RISE SICS.
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  * notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  * notice, this list of conditions and the following disclaimer in the
12  * documentation and/or other materials provided with the distribution.
13  * 3. Neither the name of the Institute nor the names of its contributors
14  * may be used to endorse or promote products derived from this software
15  * without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
18  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20  * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
21  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27  * SUCH DAMAGE.
28  *
29  */
30 
31 /**
32 * \addtogroup simple-energest
33 * @{
34 */
35 
36 /**
37  * \file
38  * A process that periodically prints out the time spent in
39  * radio tx, radio rx, total time and duty cycle.
40  *
41  * \author Simon Duquennoy <simon.duquennoy@ri.se>
42  */
43 
44 #include "contiki.h"
45 #include "sys/energest.h"
46 #include "simple-energest.h"
47 #include <stdio.h>
48 #include <limits.h>
49 
50 /* Log configuration */
51 #include "sys/log.h"
52 #define LOG_MODULE "Energest"
53 #define LOG_LEVEL LOG_LEVEL_INFO
54 
55 static unsigned long last_tx, last_rx, last_time, last_cpu, last_lpm, last_deep_lpm;
56 static unsigned long delta_tx, delta_rx, delta_time, delta_cpu, delta_lpm, delta_deep_lpm;
57 static unsigned long curr_tx, curr_rx, curr_time, curr_cpu, curr_lpm, curr_deep_lpm;
58 
59 PROCESS(simple_energest_process, "Simple Energest");
60 /*---------------------------------------------------------------------------*/
61 static unsigned long
62 to_permil(unsigned long delta_metric, unsigned long delta_time)
63 {
64  return (1000ul * (delta_metric)) / delta_time;
65 }
66 /*---------------------------------------------------------------------------*/
67 static void
68 simple_energest_step(void)
69 {
70  static unsigned count = 0;
71 
72  energest_flush();
73 
74  curr_time = ENERGEST_GET_TOTAL_TIME();
75  curr_cpu = energest_type_time(ENERGEST_TYPE_CPU);
76  curr_lpm = energest_type_time(ENERGEST_TYPE_LPM);
77  curr_deep_lpm = energest_type_time(ENERGEST_TYPE_DEEP_LPM);
78  curr_tx = energest_type_time(ENERGEST_TYPE_TRANSMIT);
79  curr_rx = energest_type_time(ENERGEST_TYPE_LISTEN);
80 
81  delta_time = curr_time - last_time;
82  delta_cpu = curr_cpu - last_cpu;
83  delta_lpm = curr_lpm - last_lpm;
84  delta_deep_lpm = curr_deep_lpm - last_deep_lpm;
85  delta_tx = curr_tx - last_tx;
86  delta_rx = curr_rx - last_rx;
87 
88  last_time = curr_time;
89  last_cpu = curr_cpu;
90  last_lpm = curr_lpm;
91  last_deep_lpm = curr_deep_lpm;
92  last_tx = curr_tx;
93  last_rx = curr_rx;
94 
95  LOG_INFO("--- Period summary #%u (%lu seconds)\n", count++, delta_time/ENERGEST_SECOND);
96  LOG_INFO("Total time : %10lu\n", delta_time);
97  LOG_INFO("CPU : %10lu/%10lu (%lu permil)\n", delta_cpu, delta_time, to_permil(delta_cpu, delta_time));
98  LOG_INFO("LPM : %10lu/%10lu (%lu permil)\n", delta_lpm, delta_time, to_permil(delta_lpm, delta_time));
99  LOG_INFO("Deep LPM : %10lu/%10lu (%lu permil)\n", delta_deep_lpm, delta_time, to_permil(delta_deep_lpm, delta_time));
100  LOG_INFO("Radio Tx : %10lu/%10lu (%lu permil)\n", delta_tx, delta_time, to_permil(delta_tx, delta_time));
101  LOG_INFO("Radio Rx : %10lu/%10lu (%lu permil)\n", delta_rx, delta_time, to_permil(delta_rx, delta_time));
102  LOG_INFO("Radio total : %10lu/%10lu (%lu permil)\n", delta_tx+delta_rx, delta_time, to_permil(delta_tx+delta_rx, delta_time));
103 }
104 /*---------------------------------------------------------------------------*/
105 PROCESS_THREAD(simple_energest_process, ev, data)
106 {
107  static struct etimer periodic_timer;
108  PROCESS_BEGIN();
109 
110  etimer_set(&periodic_timer, SIMPLE_ENERGEST_PERIOD);
111  while(1) {
112  PROCESS_WAIT_EVENT_UNTIL(etimer_expired(&periodic_timer));
113  etimer_reset(&periodic_timer);
114  simple_energest_step();
115  }
116  PROCESS_END();
117 }
118 /*---------------------------------------------------------------------------*/
119 void
121 {
122  energest_flush();
123  last_time = ENERGEST_GET_TOTAL_TIME();
124  last_cpu = energest_type_time(ENERGEST_TYPE_CPU);
125  last_lpm = energest_type_time(ENERGEST_TYPE_LPM);
126  curr_tx = energest_type_time(ENERGEST_TYPE_TRANSMIT);
127  last_deep_lpm = energest_type_time(ENERGEST_TYPE_DEEP_LPM);
128  last_rx = energest_type_time(ENERGEST_TYPE_LISTEN);
129  process_start(&simple_energest_process, NULL);
130 }
131 
132 /** @} */
#define PROCESS(name, strname)
Declare a process.
Definition: process.h:307
static volatile uint64_t count
Num.
Definition: clock.c:50
Header file for the energy estimation mechanism
#define PROCESS_BEGIN()
Define the beginning of a process.
Definition: process.h:120
#define PROCESS_END()
Define the end of a process.
Definition: process.h:131
#define PROCESS_WAIT_EVENT_UNTIL(c)
Wait for an event to be posted to the process, with an extra condition.
Definition: process.h:157
A process that periodically prints out the time spent in radio tx, radio rx, total time and duty cycle.
void simple_energest_init(void)
Initialize the deployment module.
int etimer_expired(struct etimer *et)
Check if an event timer has expired.
Definition: etimer.c:213
#define SIMPLE_ENERGEST_PERIOD
The period at which Energest statistics will be logged.
A timer.
Definition: etimer.h:76
PROCESS_THREAD(cc2538_rf_process, ev, data)
Implementation of the cc2538 RF driver process.
Definition: cc2538-rf.c:1110
void etimer_reset(struct etimer *et)
Reset an event timer with the same interval as was previously set.
Definition: etimer.c:192
Header file for the logging system
void etimer_set(struct etimer *et, clock_time_t interval)
Set an event timer.
Definition: etimer.c:177
void process_start(struct process *p, process_data_t data)
Start a process.
Definition: process.c:99