我有一个让我疯狂的Contiki应用程序。
我必须计算Tmote Sky的精力消耗。我正在使用energest,这是我在能耗估算函数中使用的一段代码,它定期被称为:
lpm_time = energest_type_time(ENERGEST_TYPE_LPM);
cpu_time = energest_type_time(ENERGEST_TYPE_CPU);
rx_time = energest_type_time(ENERGEST_TYPE_LISTEN);
tx_time = energest_type_time(ENERGEST_TYPE_TRANSMIT);
lpm = lpm_time - prev_times.lpm_time;
cpu = cpu_time - prev_times.cpu_time;
rx = rx_time - prev_times.rx_time;
tx = tx_time - prev_times.tx_time;
consumed_energy = (I_LPM * lpm + I_CPU * cpu + I_RX * rx + I_TX * tx) * VOLTS / RTIMER_ARCH_SECOND; /* mJ = mA * seconds * volts */
/*printf("lpm %lu, cpu %lu, rx %lu, tx %lu, prl %lu, prc %lu, prr %lu, prt %lu\n",
lpm_time, cpu_time, rx_time, tx_time, prev_times.lpm_time, prev_times.cpu_time, prev_times.rx_time, prev_times.tx_time);*/
prev_times.lpm_time = lpm_time;
prev_times.cpu_time = cpu_time;
prev_times.rx_time = rx_time;
prev_times.tx_time = tx_time;
问题是只有在启用了现在注释的printf
时,应用程序才能正常工作。
如果它按原样保持注释,我可以为consumed_energy
变量接收无意义的值,或者使用Java Illegal read - out of bounds
消息停止COOJA模拟。
为什么会这样?可能是什么原因? 这是一件非常奇怪的事情。
提前致谢。
此文件的完整代码如下:
volatile static struct energest_times prev_times;
float update_consumption()
{
uint32_t lpm_time;
uint32_t cpu_time;
uint32_t rx_time;
uint32_t tx_time;
uint32_t lpm;
uint32_t cpu;
uint32_t rx;
uint32_t tx;
float consumed_energy;
lpm_time = energest_type_time(ENERGEST_TYPE_LPM);
cpu_time = energest_type_time(ENERGEST_TYPE_CPU);
rx_time = energest_type_time(ENERGEST_TYPE_LISTEN);
tx_time = energest_type_time(ENERGEST_TYPE_TRANSMIT);
lpm = lpm_time - prev_times.lpm_time;
cpu = cpu_time - prev_times.cpu_time;
rx = rx_time - prev_times.rx_time;
tx = tx_time - prev_times.tx_time;
consumed_energy = (I_LPM * lpm + I_CPU * cpu + I_RX * rx + I_TX * tx) * VOLTS / RTIMER_ARCH_SECOND; /* mJ = mA * seconds * volts */
printf("lpm %lu, cpu %lu, rx %lu, tx %lu, prl %lu, prc %lu, prr %lu, prt %lu\n",
lpm_time, cpu_time, rx_time, tx_time, prev_times.lpm_time, prev_times.cpu_time, prev_times.rx_time, prev_times.tx_time);
prev_times.lpm_time = lpm_time;
prev_times.cpu_time = cpu_time;
prev_times.rx_time = rx_time;
prev_times.tx_time = tx_time;
printf("Consumed energy: %ld\n", (int32_t) consumed_energy);
return consumed_energy;
}
该功能由另一个功能定期调用,每分钟一次。
答案 0 :(得分:2)
如果你有多个线程使用非线程安全的库,你可以在Java或C中看到这个。在这种情况下,你有时会得到正确的价值,有时你会得到一半的权利,有时候你会变得毫无意义。
如果您添加printf,例如System.out.printf()或任何IO或sleep()或使用调试逐步执行代码,这实际上减慢了您的应用程序速度,因此每个线程在库中花费的时间非常小,从而间接避免冲突。
您使用的是多线程吗?您调用的库/方法是否安全?