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#include <inttypes.h>
50
51/* Log configuration */
52#include "sys/log.h"
53#define LOG_MODULE "Energest"
54#define LOG_LEVEL LOG_LEVEL_INFO
55
56static uint64_t last_tx, last_rx, last_time, last_cpu, last_lpm, last_deep_lpm;
57
58PROCESS(simple_energest_process, "Simple Energest");
59/*---------------------------------------------------------------------------*/
60static uint64_t
61to_permil(uint64_t delta_metric, uint64_t delta_time)
62{
63 return (1000ul * delta_metric) / delta_time;
64}
65/*---------------------------------------------------------------------------*/
66static void
67log_energest(const char *name, uint64_t delta, uint64_t delta_time)
68{
69 LOG_INFO("%-12s: %10"PRIu64"/%10"PRIu64" (%"PRIu64" permil)\n",
70 name, delta, delta_time, to_permil(delta, delta_time));
71}
72/*---------------------------------------------------------------------------*/
73static void
74simple_energest_step(void)
75{
76 static unsigned count = 0;
77 uint64_t curr_tx, curr_rx, curr_time, curr_cpu, curr_lpm, curr_deep_lpm;
78 uint64_t delta_time;
79
80 energest_flush();
81
82 curr_time = ENERGEST_GET_TOTAL_TIME();
83 curr_cpu = energest_type_time(ENERGEST_TYPE_CPU);
84 curr_lpm = energest_type_time(ENERGEST_TYPE_LPM);
85 curr_deep_lpm = energest_type_time(ENERGEST_TYPE_DEEP_LPM);
86 curr_tx = energest_type_time(ENERGEST_TYPE_TRANSMIT);
87 curr_rx = energest_type_time(ENERGEST_TYPE_LISTEN);
88
89 delta_time = MAX(curr_time - last_time, 1);
90
91 LOG_INFO("--- Period summary #%u (%"PRIu64" seconds)\n",
92 count++, delta_time / ENERGEST_SECOND);
93 LOG_INFO("Total time : %10"PRIu64"\n", delta_time);
94 log_energest("CPU", curr_cpu - last_cpu, delta_time);
95 log_energest("LPM", curr_lpm - last_lpm, delta_time);
96 log_energest("Deep LPM", curr_deep_lpm - last_deep_lpm, delta_time);
97 log_energest("Radio Tx", curr_tx - last_tx, delta_time);
98 log_energest("Radio Rx", curr_rx - last_rx, delta_time);
99 log_energest("Radio total", curr_tx - last_tx + curr_rx - last_rx,
100 delta_time);
101
102 last_time = curr_time;
103 last_cpu = curr_cpu;
104 last_lpm = curr_lpm;
105 last_deep_lpm = curr_deep_lpm;
106 last_tx = curr_tx;
107 last_rx = curr_rx;
108}
109/*---------------------------------------------------------------------------*/
110PROCESS_THREAD(simple_energest_process, ev, data)
111{
112 static struct etimer periodic_timer;
114
115 etimer_set(&periodic_timer, SIMPLE_ENERGEST_PERIOD);
116 while(1) {
118 etimer_reset(&periodic_timer);
119 simple_energest_step();
120 }
121 PROCESS_END();
122}
123/*---------------------------------------------------------------------------*/
124void
126{
127 energest_flush();
128 last_time = ENERGEST_GET_TOTAL_TIME();
129 last_cpu = energest_type_time(ENERGEST_TYPE_CPU);
130 last_lpm = energest_type_time(ENERGEST_TYPE_LPM);
131 last_deep_lpm = energest_type_time(ENERGEST_TYPE_DEEP_LPM);
132 last_tx = energest_type_time(ENERGEST_TYPE_TRANSMIT);
133 last_rx = energest_type_time(ENERGEST_TYPE_LISTEN);
134 process_start(&simple_energest_process, NULL);
135}
136
137/** @} */
Header file for the energy estimation mechanism.
PROCESS_THREAD(cc2538_rf_process, ev, data)
Implementation of the cc2538 RF driver process.
Definition: cc2538-rf.c:1154
static volatile uint64_t count
Num.
Definition: clock.c:50
void etimer_reset(struct etimer *et)
Reset an event timer with the same interval as was previously set.
Definition: etimer.c:192
int etimer_expired(struct etimer *et)
Check if an event timer has expired.
Definition: etimer.c:213
void etimer_set(struct etimer *et, clock_time_t interval)
Set an event timer.
Definition: etimer.c:177
#define PROCESS(name, strname)
Declare a process.
Definition: process.h:307
#define PROCESS_BEGIN()
Define the beginning of a process.
Definition: process.h:120
#define PROCESS_WAIT_EVENT_UNTIL(c)
Wait for an event to be posted to the process, with an extra condition.
Definition: process.h:157
#define PROCESS_END()
Define the end of a process.
Definition: process.h:131
void process_start(struct process *p, process_data_t data)
Start a process.
Definition: process.c:99
#define SIMPLE_ENERGEST_PERIOD
The period at which Energest statistics will be logged.
void simple_energest_init(void)
Initialize the deployment module.
Header file for the logging system.
A process that periodically prints out the time spent in radio tx, radio rx, total time and duty cycl...
A timer.
Definition: etimer.h:76