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 uint64_t last_tx, last_rx, last_time, last_cpu, last_lpm, last_deep_lpm;
56 
57 PROCESS(simple_energest_process, "Simple Energest");
58 /*---------------------------------------------------------------------------*/
59 static uint64_t
60 to_permil(uint64_t delta_metric, uint64_t delta_time)
61 {
62  return (1000ul * delta_metric) / delta_time;
63 }
64 /*---------------------------------------------------------------------------*/
65 static void
66 log_energest(const char *name, uint64_t delta, uint64_t delta_time)
67 {
68  LOG_INFO("%-12s: %10"PRIu64"/%10"PRIu64" (%"PRIu64" permil)\n",
69  name, delta, delta_time, to_permil(delta, delta_time));
70 }
71 /*---------------------------------------------------------------------------*/
72 static void
73 simple_energest_step(void)
74 {
75  static unsigned count = 0;
76  uint64_t curr_tx, curr_rx, curr_time, curr_cpu, curr_lpm, curr_deep_lpm;
77  uint64_t delta_time;
78 
79  energest_flush();
80 
81  curr_time = ENERGEST_GET_TOTAL_TIME();
82  curr_cpu = energest_type_time(ENERGEST_TYPE_CPU);
83  curr_lpm = energest_type_time(ENERGEST_TYPE_LPM);
84  curr_deep_lpm = energest_type_time(ENERGEST_TYPE_DEEP_LPM);
85  curr_tx = energest_type_time(ENERGEST_TYPE_TRANSMIT);
86  curr_rx = energest_type_time(ENERGEST_TYPE_LISTEN);
87 
88  delta_time = MAX(curr_time - last_time, 1);
89 
90  LOG_INFO("--- Period summary #%u (%"PRIu64" seconds)\n",
91  count++, delta_time / ENERGEST_SECOND);
92  LOG_INFO("Total time : %10"PRIu64"\n", delta_time);
93  log_energest("CPU", curr_cpu - last_cpu, delta_time);
94  log_energest("LPM", curr_lpm - last_lpm, delta_time);
95  log_energest("Deep LPM", curr_deep_lpm - last_deep_lpm, delta_time);
96  log_energest("Radio Tx", curr_tx - last_tx, delta_time);
97  log_energest("Radio Rx", curr_rx - last_rx, delta_time);
98  log_energest("Radio total", curr_tx - last_tx + curr_rx - last_rx,
99  delta_time);
100 
101  last_time = curr_time;
102  last_cpu = curr_cpu;
103  last_lpm = curr_lpm;
104  last_deep_lpm = curr_deep_lpm;
105  last_tx = curr_tx;
106  last_rx = curr_rx;
107 }
108 /*---------------------------------------------------------------------------*/
109 PROCESS_THREAD(simple_energest_process, ev, data)
110 {
111  static struct etimer periodic_timer;
112  PROCESS_BEGIN();
113 
114  etimer_set(&periodic_timer, SIMPLE_ENERGEST_PERIOD);
115  while(1) {
116  PROCESS_WAIT_EVENT_UNTIL(etimer_expired(&periodic_timer));
117  etimer_reset(&periodic_timer);
118  simple_energest_step();
119  }
120  PROCESS_END();
121 }
122 /*---------------------------------------------------------------------------*/
123 void
125 {
126  energest_flush();
127  last_time = ENERGEST_GET_TOTAL_TIME();
128  last_cpu = energest_type_time(ENERGEST_TYPE_CPU);
129  last_lpm = energest_type_time(ENERGEST_TYPE_LPM);
130  last_deep_lpm = energest_type_time(ENERGEST_TYPE_DEEP_LPM);
131  last_tx = energest_type_time(ENERGEST_TYPE_TRANSMIT);
132  last_rx = energest_type_time(ENERGEST_TYPE_LISTEN);
133  process_start(&simple_energest_process, NULL);
134 }
135 
136 /** @} */
#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