简体   繁体   English

Python mmap访问共享的内核内存

[英]Python mmap to access shared kernel memory

I have a kernel module in C which reads continuously data from a photiodiode and writes the values together with current time into a memory mapped file. 我在C中有一个内核模块,该模块从光电二极管连续读取数据,并将值和当前时间一起写入内存映射文件。 From the C program in ther user space I can access the data from the kernel. 从用户空间中的C程序,我可以从内核访问数据。 I tried to do the same in Python via the mmap functionality. 我试图通过mmap功能在Python中执行相同的操作。 However, when I try to mmap the file I get errors like "mmap length is greater than file size" or "mmap file is empty". 但是,当我尝试映射文件时,出现诸如“ mmap长度大于文件大小”或“ mmap文件为空”之类的错误。 It seems that Python cannot access the mmaped file from C, is that correct? 看来Python无法从C访问mmaped文件,对吗? In the end, I need a numpy array of the photodiode data for further processing. 最后,我需要一个光电二极管数据的numpy数组进行进一步处理。

Details about kernel data structure: The mmap contains a struct with index to latest voltage values and a struct array with voltage and time. 有关内核数据结构的详细信息:mmap包含一个具有最新电压值索引的结构以及一个具有电压和时间的结构数组。 The kernel has one big struct array and writes the photodiode data in chunks of page size into the struct array. 内核具有一个大的struct数组,并将光电二极管数据以页面大小的块写入struct数组。 The C user space program reads then each chunk for futher processing. 然后,C用户空间程序读取每个块以进行进一步处理。

Python code to read mmaped C file: 读取mmaped C文件的Python代码:

num_pages = 103
page_size = 10000
max_buffer_size = num_pages * page_size

class buf_element(ctypes.Structure):

    _fields_ = [("voltage", ctypes.c_int),
        ("time", ctypes.c_uint)]

class data(ctypes.Structure):

    _fields_ = [("latest_page_offset", ctypes.c_int),
                ("buffer", ctypes.POINTER(buf_element))]

length_data = ctypes.sizeof(ctypes.c_int) + max_buffer_size * ctypes.sizeof(buf_element);
fd = os.open(data_file, os.O_RDWR)
buf = mmap.mmap(fd, length_data, mmap.MAP_SHARED, mmap.PROT_READ)
test_data = data.from_buffer(buf)
print test_data.latest_page_offset
os.close(fd)

My idea was to use the already existing and working C code from python via C extensions. 我的想法是通过C扩展使用来自python的已经存在并且可以运行的C代码。 So, python calls C and hand over a numpy array and C writes the data into that. 因此,python调用C并移交一个numpy数组,然后C将数据写入该数组。 Is that the fastest way? 那是最快的方法吗? Other recommendations? 还有其他建议吗?

为了使其正常工作,我通过Python的Cython使用C代码。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM