Contiki 3.x
shell-power.c
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2009, Swedish Institute of Computer Science.
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  * This file is part of the Contiki operating system.
30  *
31  */
32 
33 /**
34  * \file
35  * Power reporting functions of the Contiki shell
36  * \author
37  * Adam Dunkels <adam@sics.se>
38  */
39 
40 #include "shell.h"
41 #include "sys/compower.h"
42 #include "sys/energest.h"
43 
44 #include <stdio.h>
45 #include "sys/cc.h"
46 
47 struct power_msg {
48  uint16_t len;
49  uint32_t cpu;
50  uint32_t lpm;
51  uint32_t transmit;
52  uint32_t listen;
53  uint32_t idle_transmit;
54  uint32_t idle_listen;
55 };
56 
57 #define WITH_POWERGRAPH 0
58 
59 /*---------------------------------------------------------------------------*/
60 PROCESS(shell_power_process, "power");
61 SHELL_COMMAND(power_command,
62  "power",
63  "power: print power profile",
64  &shell_power_process);
65 PROCESS(shell_energy_process, "energy");
66 SHELL_COMMAND(energy_command,
67  "energy",
68  "energy: print energy profile",
69  &shell_energy_process);
70 PROCESS(shell_powerconv_process, "powerconv");
71 SHELL_COMMAND(powerconv_command,
72  "powerconv",
73  "powerconv: convert power profile to human readable output",
74  &shell_powerconv_process);
75 #if WITH_POWERGRAPH
76 PROCESS(shell_powergraph_process, "powergraph");
77 SHELL_COMMAND(powergraph_command,
78  "powergraph",
79  "powergraph: convert power profile to a 'graphical' repressentation",
80  &shell_powergraph_process);
81 #endif /* WITH_POWERGRAPH */
82 /*---------------------------------------------------------------------------*/
83 PROCESS_THREAD(shell_power_process, ev, data)
84 {
85  static uint32_t last_cpu, last_lpm, last_transmit, last_listen;
86  static uint32_t last_idle_transmit, last_idle_listen;
87  struct power_msg msg;
88 
89  PROCESS_BEGIN();
90 
91  energest_flush();
92 
93  msg.len = 12;
94  msg.cpu = energest_type_time(ENERGEST_TYPE_CPU) - last_cpu;
95  msg.lpm = energest_type_time(ENERGEST_TYPE_LPM) - last_lpm;
96  msg.transmit = energest_type_time(ENERGEST_TYPE_TRANSMIT) - last_transmit;
97  msg.listen = energest_type_time(ENERGEST_TYPE_LISTEN) - last_listen;
98  msg.idle_transmit = compower_idle_activity.transmit - last_idle_transmit;
99  msg.idle_listen = compower_idle_activity.listen - last_idle_listen;
100 
101 
102  last_cpu = energest_type_time(ENERGEST_TYPE_CPU);
103  last_lpm = energest_type_time(ENERGEST_TYPE_LPM);
104  last_transmit = energest_type_time(ENERGEST_TYPE_TRANSMIT);
105  last_listen = energest_type_time(ENERGEST_TYPE_LISTEN);
106  last_idle_listen = compower_idle_activity.listen;
107  last_idle_transmit = compower_idle_activity.transmit;
108 
109  shell_output(&power_command, &msg, sizeof(msg), "", 0);
110 
111  PROCESS_END();
112 }
113 /*---------------------------------------------------------------------------*/
114 PROCESS_THREAD(shell_energy_process, ev, data)
115 {
116  struct power_msg msg;
117 
118  PROCESS_BEGIN();
119 
120  energest_flush();
121 
122  msg.len = 12;
123  msg.cpu = energest_type_time(ENERGEST_TYPE_CPU);
124  msg.lpm = energest_type_time(ENERGEST_TYPE_LPM);
125  msg.transmit = energest_type_time(ENERGEST_TYPE_TRANSMIT);
126  msg.listen = energest_type_time(ENERGEST_TYPE_LISTEN);
127  msg.idle_transmit = compower_idle_activity.transmit;
128  msg.idle_listen = compower_idle_activity.listen;
129 
130  shell_output(&energy_command, &msg, sizeof(msg), "", 0);
131 
132  PROCESS_END();
133 }
134 /*---------------------------------------------------------------------------*/
135 #define DEC2FIX(h,d) ((h * 64L) + (unsigned long)((d * 64L) / 1000L))
136 static void
137 printpower(struct power_msg *msg)
138 {
139  char buf[100];
140  unsigned long avg_power;
141  unsigned long time;
142 
143  time = msg->cpu + msg->lpm;
144 
145  avg_power = (3L *
146  (msg->cpu * DEC2FIX(1L,800L) +
147  msg->lpm * DEC2FIX(0L,545L) +
148  msg->transmit * DEC2FIX(17L,700L) +
149  msg->listen * DEC2FIX(20L,0))) / ((64L * time) / 1000);
150  snprintf(buf, sizeof(buf), "CPU %d%% LPM %d%% tx %d%% rx %d%% idle tx %d%% idle rx %d%% tot %lu uW",
151  (int)((100L * (unsigned long)msg->cpu) / time),
152  (int)((100L * (unsigned long)msg->lpm) / time),
153  (int)((100L * (unsigned long)msg->transmit) / time),
154  (int)((100L * (unsigned long)msg->listen) / time),
155  (int)((100L * (unsigned long)msg->idle_transmit) / time),
156  (int)((100L * (unsigned long)msg->idle_listen) / time),
157  avg_power);
158  shell_output_str(&powerconv_command, buf, "");
159 }
160 /*---------------------------------------------------------------------------*/
161 PROCESS_THREAD(shell_powerconv_process, ev, data)
162 {
163  struct power_msg *msg;
164  struct shell_input *input;
165  int len;
166 
167  PROCESS_BEGIN();
168  while(1) {
170  input = data;
171 
172  if(input->len1 + input->len2 == 0) {
173  PROCESS_EXIT();
174  }
175  len = input->len1;
176  for(msg = (struct power_msg *)input->data1;
177  len > 0;
178  msg++, len -= sizeof(struct power_msg)) {
179  printpower(msg);
180  }
181  len = input->len2;
182  for(msg = (struct power_msg *)input->data2;
183  len > 0;
184  msg++, len -= sizeof(struct power_msg)) {
185  printpower(msg);
186  }
187 
188  }
189 
190  PROCESS_END();
191 }
192 /*---------------------------------------------------------------------------*/
193 #if WITH_POWERGRAPH
194 #define MAX_POWERGRAPH 34
195 static void
196 printpowergraph(struct power_msg *msg)
197 {
198  int i;
199  unsigned long avg_power;
200  unsigned long time;
201  char buf[MAX_POWERGRAPH];
202 
203  time = msg->cpu + msg->lpm;
204 
205  avg_power = (3L *
206  (msg->cpu * DEC2FIX(1L,800L) +
207  msg->lpm * DEC2FIX(0L,545L) +
208  msg->transmit * DEC2FIX(17L,700L) +
209  msg->listen * DEC2FIX(20L,0))) / ((64L * time) / 1000);
210  memset(buf, 0, MAX_POWERGRAPH);
211  for(i = 0; avg_power > 0 && i < MAX_POWERGRAPH; ++i) {
212  buf[i] = '*';
213  avg_power -= MIN(2000, avg_power);
214  }
215  shell_output_str(&powergraph_command, buf, "");
216 }
217 /*---------------------------------------------------------------------------*/
218 PROCESS_THREAD(shell_powergraph_process, ev, data)
219 {
220  struct power_msg *msg;
221  struct shell_input *input;
222  int len;
223 
224  PROCESS_BEGIN();
225  while(1) {
227  input = data;
228 
229  if(input->len1 + input->len2 == 0) {
230  PROCESS_EXIT();
231  }
232  len = input->len1;
233  for(msg = (struct power_msg *)input->data1;
234  len > 0;
235  msg++, len -= sizeof(struct power_msg)) {
236  printpowergraph(msg);
237  }
238  len = input->len2;
239  for(msg = (struct power_msg *)input->data2;
240  len > 0;
241  msg++, len -= sizeof(struct power_msg)) {
242  printpowergraph(msg);
243  }
244 
245  }
246 
247  PROCESS_END();
248 }
249 #endif /* WITH_POWERGRAPH */
250 /*---------------------------------------------------------------------------*/
251 void
252 shell_power_init(void)
253 {
254  shell_register_command(&power_command);
255  shell_register_command(&powerconv_command);
256  shell_register_command(&energy_command);
257 
258 #if WITH_POWERGRAPH
259  shell_register_command(&powergraph_command);
260 #endif /* WITH_POWERGRAPH */
261 }
262 /*---------------------------------------------------------------------------*/
Default definitions of C compiler quirk work-arounds.
Main header file for the Contiki shell
void shell_output_str(struct shell_command *c, char *text1, const char *text2)
Output strings from a shell command.
Definition: shell.c:383
#define PROCESS_END()
Define the end of a process.
Definition: process.h:131
#define PROCESS(name, strname)
Declare a process.
Definition: process.h:307
static void input(void)
Process a received 6lowpan packet.
Definition: sicslowpan.c:1503
#define PROCESS_THREAD(name, ev, data)
Define the body of a process.
Definition: process.h:273
#define PROCESS_EXIT()
Exit the currently running process.
Definition: process.h:200
Header file for the communication power accounting module
void shell_output(struct shell_command *c, void *data1, int len1, const void *data2, int len2)
Output data from a shell command.
Definition: shell.c:395
Header file for the energy estimation mechanism
#define SHELL_COMMAND(name, command, description, process)
Define a shell command.
Definition: shell.h:219
int shell_event_input
The event number for shell input data.
Definition: shell.c:70
#define PROCESS_WAIT_EVENT_UNTIL(c)
Wait for an event to be posted to the process, with an extra condition.
Definition: process.h:157
void shell_register_command(struct shell_command *c)
Register a command with the shell.
Definition: shell.c:413
struct compower_activity compower_idle_activity
The default idle communication activity.
Definition: compower.c:50
Structure for shell input data.
Definition: shell.h:365
#define PROCESS_BEGIN()
Define the beginning of a process.
Definition: process.h:120