- r - 以节省内存的方式增长 data.frame
- ruby-on-rails - ruby/ruby on rails 内存泄漏检测
- android - 无法解析导入android.support.v7.app
- UNIX 域套接字与共享内存(映射文件)
从用户空间我们可以使用cpuset实际上隔离我们系统中的一个特定核心,并只对该核心执行一个特定进程。
我正在尝试对内核模块做同样的事情。所以我希望模块在一个独立的核心中执行。换句话说:如何在内核模块中使用 cpuset
? *
使用 linux/cpuset.h在我的内核模块中不起作用。所以,我有一个这样的模块:
#include <linux/module.h>
#include <linux/cpuset.h>
...
#ifdef CONFIG_CPUSETS
printk(KERN_INFO, "cpusets is enabled!");
#endif
cpuset_init(); // this function is declared in cpuset.h
...
尝试加载此模块时,我收到(在 dmesg
中)以下消息 cpusets is enabled!
。但我也收到消息 Unknown symbol cpu_init (err 0)
。
同样,我尝试使用 linux/sched.h
中的 sched_setaffinity
将所有正在运行的进程移动到特定核心,然后将我的模块运行到一个独立的核心。我收到相同的错误消息:Unknown symbol sched_setaffinity (err 0)
。我想我得到了“未知符号”,因为这些函数在内核中没有 EXPORT_SYMBOL
。所以我去尝试调用 sys_sched_setaffinity
系统调用(基于此 question )但再次收到此消息:Unknown symbol sys_sched_setaffinity (err 0)
!
此外,我不是在寻找使用 isolcpus
的解决方案,它是在启动时设置的。我只想加载模块,然后进行隔离。
最佳答案
So I want the module to get executed in an isolated core.
和
actually isolate a specific core in our system and execute just one specific process to that core
这是在使用内核 3.16 的 Debian 机器上编译和测试的有效源代码。我会先描述如何加载和卸载,以及传递的参数是什么意思。
所有资源都可以在 github 上找到...
https://github.com/harryjackson/doc/tree/master/linux/kernel/toy/toy
构建并加载模块...
make
insmod toy param_cpu_id=2
卸载模块使用
rmmod toy
我没有使用 modprobe,因为它需要一些配置等。我们传递给 toy
内核模块的参数是我们要隔离的 CPU。除非在该 CPU 上执行,否则被调用的设备操作都不会运行。
加载模块后,您可以在此处找到它
/dev/toy
简单的操作如
cat /dev/toy
创建内核模块捕获并产生一些输出的事件。您可以使用 dmesg
查看输出。
源代码...
#include <linux/module.h>
#include <linux/fs.h>
#include <linux/miscdevice.h>
MODULE_LICENSE("GPL");
MODULE_AUTHOR("Harry");
MODULE_DESCRIPTION("toy kernel module");
MODULE_VERSION("0.1");
#define DEVICE_NAME "toy"
#define CLASS_NAME "toy"
static int param_cpu_id;
module_param(param_cpu_id , int, (S_IRUSR | S_IRGRP | S_IROTH));
MODULE_PARM_DESC(param_cpu_id, "CPU ID that operations run on");
//static void bar(void *arg);
//static void foo(void *cpu);
static int toy_open( struct inode *inodep, struct file *fp);
static ssize_t toy_read( struct file *fp , char *buffer, size_t len, loff_t * offset);
static ssize_t toy_write( struct file *fp , const char *buffer, size_t len, loff_t *);
static int toy_release(struct inode *inodep, struct file *fp);
static struct file_operations toy_fops = {
.owner = THIS_MODULE,
.open = toy_open,
.read = toy_read,
.write = toy_write,
.release = toy_release,
};
static struct miscdevice toy_device = {
.minor = MISC_DYNAMIC_MINOR,
.name = "toy",
.fops = &toy_fops
};
//static int CPU_IDS[64] = {0};
static int toy_open(struct inode *inodep, struct file *filep) {
int this_cpu = get_cpu();
printk(KERN_INFO "open: called on CPU:%d\n", this_cpu);
if(this_cpu == param_cpu_id) {
printk(KERN_INFO "open: is on requested CPU: %d\n", smp_processor_id());
}
else {
printk(KERN_INFO "open: not on requested CPU:%d\n", smp_processor_id());
}
put_cpu();
return 0;
}
static ssize_t toy_read(struct file *filep, char *buffer, size_t len, loff_t *offset){
int this_cpu = get_cpu();
printk(KERN_INFO "read: called on CPU:%d\n", this_cpu);
if(this_cpu == param_cpu_id) {
printk(KERN_INFO "read: is on requested CPU: %d\n", smp_processor_id());
}
else {
printk(KERN_INFO "read: not on requested CPU:%d\n", smp_processor_id());
}
put_cpu();
return 0;
}
static ssize_t toy_write(struct file *filep, const char *buffer, size_t len, loff_t *offset){
int this_cpu = get_cpu();
printk(KERN_INFO "write called on CPU:%d\n", this_cpu);
if(this_cpu == param_cpu_id) {
printk(KERN_INFO "write: is on requested CPU: %d\n", smp_processor_id());
}
else {
printk(KERN_INFO "write: not on requested CPU:%d\n", smp_processor_id());
}
put_cpu();
return 0;
}
static int toy_release(struct inode *inodep, struct file *filep){
int this_cpu = get_cpu();
printk(KERN_INFO "release called on CPU:%d\n", this_cpu);
if(this_cpu == param_cpu_id) {
printk(KERN_INFO "release: is on requested CPU: %d\n", smp_processor_id());
}
else {
printk(KERN_INFO "release: not on requested CPU:%d\n", smp_processor_id());
}
put_cpu();
return 0;
}
static int __init toy_init(void) {
int cpu_id;
if(param_cpu_id < 0 || param_cpu_id > 4) {
printk(KERN_INFO "toy: unable to load module without cpu parameter\n");
return -1;
}
printk(KERN_INFO "toy: loading to device driver, param_cpu_id: %d\n", param_cpu_id);
//preempt_disable(); // See notes below
cpu_id = get_cpu();
printk(KERN_INFO "toy init called and running on CPU: %d\n", cpu_id);
misc_register(&toy_device);
//preempt_enable(); // See notes below
put_cpu();
//smp_call_function_single(1,foo,(void *)(uintptr_t) 1,1);
return 0;
}
static void __exit toy_exit(void) {
misc_deregister(&toy_device);
printk(KERN_INFO "toy exit called\n");
}
module_init(toy_init);
module_exit(toy_exit);
上面的代码包含您要求的两种方法,即隔离 CPU 和在隔离内核上运行 init
。
在 init get_cpu
上禁用抢占,即在它之后发生的任何事情都不会被内核抢占,而是在一个内核上运行。请注意,这是使用 3.16 内核完成的,您的里程可能会因内核版本而异,但我认为这些 API 已经存在很长时间了
这是生成文件...
obj-m += toy.o
all:
make -C /lib/modules/$(shell uname -r)/build M=$(PWD) modules
clean:
make -C /lib/modules/$(shell uname -r)/build M=$(PWD) clean
注释。 get_cpu
在 linux/smp.h
中声明为
#define get_cpu() ({ preempt_disable(); smp_processor_id(); })
#define put_cpu() preempt_enable()
因此您实际上不需要在调用 get_cpu
之前调用 preempt_disable
。get_cpu 调用是对以下调用序列的包装...
preempt_count_inc();
barrier();
而 put_cpu 确实在这样做......
barrier();
if (unlikely(preempt_count_dec_and_test())) {
__preempt_schedule();
}
使用上面的方法你可以随心所欲。几乎所有这些都取自以下来源..
谷歌... smp_call_function_single
Linux 内核开发,Robert Love 着。
http://derekmolloy.ie/writing-a-linux-kernel-module-part-2-a-character-device/
关于linux - 使用 Cpuset 将内核模块隔离到特定内核,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36288877/
我的公司有一个 Web 应用程序,其中包含纯 JavaScript,它以自己的方式使用 $ -“美元符号”,如下所示: function $(e) { return document.getE
doc说“这意味着对单个节点上单个分区内的行的写入仅对执行操作的客户端可见”。 如果有另一个 client2 在同一个分区和同一个节点上执行操作,那么文档中提到的“THE CLIENT”执行的写入是否
只是一个想法,但在 DIV 上使用 IFRAME 本质上会使该元素与窗口隔离,从而降低 IFRAME 中运行的脚本速度 不会影响其他框架/窗口吗? 最佳答案 是的,对于第一部分,iframe 会“某种
我有以下模型 Inventory [product_name, quantity, reserved_quantity] 有数据 [Shirt, 1, 0] [Shorts, 10, 0] 如果以下代
我面临的情况如下。因为ThreadPool是每个进程1个实例,所以我的问题是是否会在 3秒后取消方法2排队的任务? http request comes in *method 1 gets execu
我希望在 Dart 中创建一个 Isolate,我可以通过编程方式暂停和恢复。这是我使用的代码。 import 'dart:io'; import 'dart:isolate'; void main(
我想编写一个具有隔离作用域的指令,但也希望使该作用域可用于父作用域的 Controller 。我找到了这个解决方案: app.directive('popupbutton', [functi
我有一个像这样的 JSON 对象: [ {"Subject": "Physics", "Active": 48, "Date": "2020-01-22T00:00:00Z"}, {"Su
我正在使用 Elixir 自动执行用 Gherkin 编写的规范中定义的验收测试。一种方法是使用名为 Cabbage 的 ExUnit 插件。 . 现在,ExUnit 似乎提供了一个在任何单个测试之前
我被要求为多个用户配置一个带有 docker 的 ubuntu 18.04 服务器。 目的: 我们有多个编写测试用例的测试人员。但是我们的笔记本电脑速度不够快,无法在 docker 环境中构建项目和运
我一直在网上寻找完整的解决方案,但到目前为止,我只能找到不合适的部分。 我正在寻找一个可以查看图像文件、循环遍历文件并隔离 Sprite 然后保存它们的程序。之后,我需要一种方法来重新访问该 Spri
我想知道如何隔离 JavaScript 函数的执行以避免浏览器崩溃。 示例:如果我想在控制台中输出一个包含大约 10k 元素的关联数组,浏览器将停止响应。我怎样才能避免这种情况? 最佳答案 解决方案是
我必须向我的数据库添加大量信息。添加此信息大约需要 5-7 分钟。我需要添加交易。 我试过这个: try { db.Connection.Open(); db.Transaction
我有 6 个 iframe,它们来自同一个域,但具有不同的 url 和子目录。他们都使用 html header “set-cookie”设置了一个名称相同但值不同的 cookie。我需要将它们的 c
我正在编写一个代码,它基本上读取一个文本文件(表格格式)并检查该文件是否包含预期的数据类型。为此我写了下面的课。 示例文件应该是这样的。 name age abc 20 xyz
我有一个表,线程。我有一个表,thread_participants。我正在尝试隔离与特定 thread_id 和特定 thread_participants.user_id 标识的行。 例如,如果
我有一个非常实际的问题。我的数据库中有大约 400 篇文章,在这些文章中我有其他文章的链接。在转换过程中,链接被破坏。我们在 CMS 中手动插入新的菜单链接项。我想制作一个脚本来查找(文章)id 并将
关闭。这个问题是off-topic .它目前不接受答案。 想改进这个问题吗? Update the question所以它是on-topic用于堆栈溢出。 关闭 10 年前。 Improve thi
我在想是否可以在 postgres 数据库(高于 8.3 的版本)上创建一个只能访问特定指定模式的用户。问题是,在我的数据库中我有一些模式。如果我撤销某个用户对除一个模式之外的所有模式的所有特权,他仍
我有两组点,一组来自分析,另一组用于分析数据的后处理结果。 黑色的分析数据是散乱的。 用于结果的点是红色的。 这是同一地 block 上的两组: 我遇到的问题是:我将插值到红点上,但如您所见,有些红点
我是一名优秀的程序员,十分优秀!