gpt4 book ai didi

java - 如果删除 printf,应用程序将无法正常工作

转载 作者:太空宇宙 更新时间:2023-11-04 02:49:43 25 4
gpt4 key购买 nike

我有一个让我发疯的 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 变量值,或者 COOJA 模拟停止并显示 Java Illegal read - out of bounds 消息。

为什么会这样???可能是什么原因?这是一件很奇怪的事情。

提前致谢。


这个文件的完整代码是这样的:

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;
}

该函数被另一个函数周期性地调用,每分钟一次。

最佳答案

您可以在 Java 或 C 中看到这一点,如果您有多个线程使用非线程安全的库。在这种情况下,有时您会得到正确的值,有时您只对了一半,有时您会胡说八道。

如果您添加一个 printf,例如System.out.printf() 或任何 IO 或 sleep() 或使用调试逐步执行代码,这确实会减慢您的应用程序,因此每个线程在库中花费的时间只有很小一部分,从而间接避免了冲突。

您是否使用多线程?您调用的库/方法是线程安全的吗?

关于java - 如果删除 printf,应用程序将无法正常工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23385052/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com