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