c - 运行两个不同的 LKM 每秒获取电池统计信息时系统挂起

标签 c linux-kernel batterymanager

我正在开发一个 Linux 内核模块 (LKM),它分析我笔记本电脑的电池参数并在每个给定时间间隔内写入内核日志文件 (/var/log/kern.log)。使用日志文件中的数据,我将绘制实时图表。

您可能会想我为什么要做这些事情。这是我的学术的一部分是对它的回答。另外,我觉得这很有趣。

我对这项任务的整个分析一直很简单,直到最近。我遵循的步骤是:

  1. 可以按照最佳答案here中给出的参数获取并找到了更多参数 here .使用这两个链接,我的任务就轻松开始了。
  2. 定期执行 LKM 功能的代码取自 here .这被修改为每秒运行一次。

有了这两个 LKM,我误以为我的任务很简单,因为唯一遗漏的任务是组合这两个 LKM 并将所需的参数数据写入日志文件。

我结合了两个 LKM 的代码并制作了一个 LKM,并将其插入我的 Ubuntu 14.04 64 位操作系统内核中。系统瞬间卡住了,吓了我一跳。我当时一头雾水。

在此之后,我减少了电池分析器 LKM 和定时器 LKM 的代码,这些代码对于我想要的输出来说是最少需要运行的。我发现简化的代码运行起来很舒服。

后来我将分析电池参数的代码迁移到一个函数并将其导出(使用 EXPORT_SYMBOL(<function_name>) ,从计时器方法调用。我非常确定这肯定会运行,因为两个 LKM 都运行良好.

我想通过以下 LKM 代码和视频向您展示我的机器中发生的真实情况。

bat_stat_analyzer.c - LKM 获取电池参数并写入内核日志文件。

#include <linux/module.h>
#include <linux/version.h>
#include <linux/kernel.h>
#include <linux/types.h>
#include <linux/kdev_t.h>
#include <linux/fs.h>
#include <linux/power_supply.h>

static int result = 0;
static struct power_supply *psy;
static void bat_stat(void);

EXPORT_SYMBOL(bat_stat);

static void bat_stat(void) {
  union power_supply_propval value;
  int charge_status, voltage_now, current_now, capacity;

  psy = power_supply_get_by_name("BAT1");
  result = psy->get_property(psy,POWER_SUPPLY_PROP_STATUS, &value);
  charge_status = (!result) ? value.intval : -1;
  result = psy->get_property(psy,POWER_SUPPLY_PROP_VOLTAGE_NOW, &value);
  voltage_now = (!result) ? value.intval : -1;
  result = psy->get_property(psy,POWER_SUPPLY_PROP_CURRENT_NOW, &value);
  current_now = (!result) ? value.intval : -1;
  result = psy->get_property(psy,POWER_SUPPLY_PROP_CAPACITY, &value);
  capacity = (!result) ? value.intval : -1;

  printk(KERN_INFO "%s:%d,%d,%d,%d\n",
    __func__, charge_status, voltage_now, current_now, capacity);
}

static int __init bat_stat_init(void) /* Constructor */
{
  bat_stat();
  return 0;
}

static void __exit bat_stat_exit(void) /* Destructor */
{
  printk(KERN_INFO "Good bye\n");
}

module_init(bat_stat_init);
module_exit(bat_stat_exit);

MODULE_LICENSE("GPL");
MODULE_AUTHOR("Sriram Kumar <sriramhearing_at_gmail_dot_com>");
MODULE_DESCRIPTION("First Battery Analyzer");

bat_stat_repeater.c - 重复调用函数的 LKM

#include <linux/module.h>   /* Needed by all modules */
#include <linux/kernel.h>   /* Needed for KERN_INFO */
#include <linux/init.h>     /* Needed for the macros */
#include <linux/jiffies.h>
#include <linux/time.h>
#include <linux/hrtimer.h>

static unsigned long period_ms;
static unsigned long period_ns;
static ktime_t ktime_period_ns;
static struct hrtimer my_hrtimer;
extern int bat_stat(void);


//~ static void bat_stat_repeat(unsigned long data)
static enum hrtimer_restart bat_stat_repeat(struct hrtimer *timer)
{
  unsigned long tjnow;
  ktime_t kt_now;
  bat_stat();
  printk(KERN_INFO "Repeating...\n");

  tjnow = jiffies;
  kt_now = hrtimer_cb_get_time(&my_hrtimer);
  hrtimer_forward(&my_hrtimer, kt_now, ktime_period_ns);
  return HRTIMER_RESTART;
}

static int __init bat_stat_init(void)
{
  struct timespec tp_hr_res;
  period_ms = 1000;
  hrtimer_get_res(CLOCK_MONOTONIC, &tp_hr_res);

  hrtimer_init(&my_hrtimer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
  my_hrtimer.function = &bat_stat_repeat;
  period_ns = period_ms*( (unsigned long)1E6L );
  ktime_period_ns = ktime_set(0,period_ns);
  hrtimer_start(&my_hrtimer, ktime_period_ns, HRTIMER_MODE_REL);

  return 0;
}

static void __exit bat_stat_exit(void)
{
  int ret_cancel = 0;
  while( hrtimer_callback_running(&my_hrtimer) ) {
    ret_cancel++;
  }
  if (ret_cancel != 0) {
    printk(KERN_INFO " testjiffy Waited for hrtimer callback to finish (%d)\n", ret_cancel);
  }
  if (hrtimer_active(&my_hrtimer) != 0) {
    ret_cancel = hrtimer_cancel(&my_hrtimer);
    printk(KERN_INFO " testjiffy active hrtimer cancelled: %d\n", ret_cancel);
  }
  if (hrtimer_is_queued(&my_hrtimer) != 0) {
    ret_cancel = hrtimer_cancel(&my_hrtimer);
    printk(KERN_INFO " testjiffy queued hrtimer cancelled: %d\n", ret_cancel);
  }
  printk(KERN_INFO "Exit testjiffy\n");
}

module_init(bat_stat_init);
module_exit(bat_stat_exit);

MODULE_LICENSE("GPL");

我使用的 Makefile 如下所示,因为它用于编译两个 LKM。

obj-m := <file_name>.o
KDIR := /lib/modules/$(shell uname -r)/build
PWD := $(shell pwd)
default:
    $(MAKE) -C $(KDIR) SUBDIRS=$(PWD) modules

可以在这个 youtube 上查看我使用这两个 LKM 获得的输出 link (只有 2 分钟的视频)。如果您希望将其作为图像查看,请附在下面:

Screenshot of battery analyzer output. System getting halted.

我想知道:

  • 是否有更好和/或更简单的方法来实现我正在尝试的目标?或
  • 我的 LKM 中有什么问题导致我的 CPU 阻塞?

提前致谢。

最佳答案

由于使用 hrtimers,您遇到了问题。此机制专为高精度而设计,回调在 hardirq 上下文中调用(禁用 IRQ),因此您的回调函数必须是原子的。然而,您从回调中调用的函数不是原子的,可能会休眠(因为互斥锁)。普通计时器有类似的问题,因此为了解决这个问题,您应该使用其他方式或重复任务,例如工作队列。

您的代码中的其他一些小问题:

  • 两个模块中的batt_stat 函数声明不同
  • 您不检查第一个模块中 power_supply_get_by_name() 的输出

此外,我真的看不出有任何理由将其拆分为两个内核模块,您应该只使用一个。

关于c - 运行两个不同的 LKM 每秒获取电池统计信息时系统挂起,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29830285/

相关文章:

linux-kernel - 启动期间Linux内核空间中的页表

java - 设备的总电池容量(以 mAh 为单位)以编程方式

c++ - 对原子操作的最低要求

linux - 持有自旋锁时内核抢占

C 编程 ... 为什么 "return"语句在这段代码中只给出一个值?

linux-kernel - 尝试将 Linux menuconfig 与 yocto/morty 一起使用

android - 为什么 BATTERY_PROPERTY_CHARGE_COUNTER 不像 BATTERY_PROPERTY_CURRENT_NOW 那样工作?

android - 估计android中的剩余电池时间

c - 如何让系统调用 write() 打印到屏幕上?

C 工作面试 - 类型转换和比较