LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 02-13-2012, 10:49 AM   #1
dennisdd
Member
 
Registered: Oct 2011
Posts: 37

Rep: Reputation: Disabled
for buffer streaming which is better solution for apps to access memory buffer?


I am developing driver that takes data buffers and stream to user space.
so far I have gathered there is two ways to able the user to access the memory buffers.
But, I am not sure what is the best.

Here is what I found out so far:
1.using mmap() func on apps layer to read, where using vmalloc to migrate it to userspace.
I saw this example from http://www.scs.ch/~frey/linux/memorymap.html. It comes with a apps test.
In this method, it seem like the user's "see" the whole memory banks allocated. (Where I can find how to understands mmap()works? )

2. is via the copy_to user() on the kernel side. But I have not see the example how this works. also how the apps layer works. (somehow when i cat nothing pop up)

So for buffers streaming which is the more recommend method? pls advise
 
Old 02-13-2012, 11:51 AM   #2
Nominal Animal
Senior Member
 
Registered: Dec 2010
Location: Finland
Distribution: Xubuntu, CentOS, LFS
Posts: 1,723
Blog Entries: 3

Rep: Reputation: 948Reputation: 948Reputation: 948Reputation: 948Reputation: 948Reputation: 948Reputation: 948Reputation: 948
Take a look at chapters 3 and 15 of Linux Device Drivers, Third Edition.

From the applications point of view, using mmap() yields a normal dynamically allocated memory region, except that it is shared with your driver. All changes one makes are immediately visible to the other.

If you have a device with physical memory of its own, using mmap() to map that memory directly to userspace is extremely efficient: you can get zero-copy operation that way. Each memory access is directly seen by the hardware device.

copy_to_user() and copy_from_user() do a copy between memory allocated and used by your driver, and any client application.

The main difference between these two approaches is that using a mmap() bypasses your kernel driver. The kernel driver will not know when nor how the userspace has modified the memory.

When the data is a continuous stream of bytes, mmap() is rarely used, because it is difficult for the kernel driver to know when/if it has further data to process. (It is not impossible, however: one could use an interface similar to raw memory-mapped sockets, and use read()/write() to indicate the amount of memory prepared in the buffer, completely ignoring the data pointer. This is very nonstandard, and will confuse most application programmers, though.)

On the whole, the best answer to your question depends highly on the manner in which your driver needs to handle the data, and whether you have a related device with its own physical memory or not. If you have byte streams, mmap() is typically not the best solution -- the extra copy (copy_to_user()/copy_from_user()) is really not that expensive.

Could you give us some more details on the nature of the data you intend to process? Are they byte streams, or perhaps datagrams of known size? Do you want to minimize latencies, or maximise throughput?

If the kernel driver is just a data processor, doing some transformation on the data, you'll get better performance by writing it as a library instead.
 
Old 02-13-2012, 12:37 PM   #3
dennisdd
Member
 
Registered: Oct 2011
Posts: 37

Original Poster
Rep: Reputation: Disabled
it is a some sort of live data stream from devices.
it is streaming is bytes.

on the apps layer it suppose to streams out the bytes.
i think the closest scenario is like those audio/video streams.
i foresee there will be the needs of couple more buffers pools and sampling frames..

can you recommend a good example using copy_to_user example?(thanks ahead)
I found this http://www.freesoftwaremagazine.com/.../drivers_linux
but when I cat it...nothing appears on my terminal screen.
Not sure what I did wrong.

I see your point. I too worry on the apps site how should the apps layer handle my data.
I might refreshed my page files without knowing when the apps hv grab the latest updates.

I am new to linux..still in learning process...thanks for the reply. it helps.
 
Old 02-13-2012, 11:07 PM   #4
Nominal Animal
Senior Member
 
Registered: Dec 2010
Location: Finland
Distribution: Xubuntu, CentOS, LFS
Posts: 1,723
Blog Entries: 3

Rep: Reputation: 948Reputation: 948Reputation: 948Reputation: 948Reputation: 948Reputation: 948Reputation: 948Reputation: 948
Quote:
Originally Posted by dennisdd View Post
i think the closest scenario is like those audio/video streams.
How high are your bandwidth requirements? If the data rate is just dozens of megabytes per second, the "extra" copy_to_user() or copy_from_user() overhead should not matter. After all, every time you use normal file I/O, you incur that overhead anyway. Besides, a non-mmap() driver is usually simpler to write logic-wise; the concurrency issues are pretty hairy. Solvable, but hairy.

Quote:
Originally Posted by dennisdd View Post
can you recommend a good example using copy_to_user example?
The HelloDriver at kernelnewbies.org is pretty good.

However, I do wholeheartedly recommend the LDD3, and believe it is overall the best source for information, explanations, and examples. That and Linux Kernel sources (including the Documentation/ tree) should be enough to write a working driver.

Quote:
Originally Posted by dennisdd View Post
but when I cat it...nothing appears on my terminal screen.
Good. It shouldn't. The driver writes to the kernel log. Check if the message appears in the kernel ring buffer (by running dmesg), or in your system logs (typically /var/log/kern.log).

Note that the LDD3 does explain all that, if you start at the first chapter.
 
1 members found this post helpful.
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Copy ALSA buffer to the buffer allocated in my driver. yethish Programming 1 08-04-2011 11:27 AM
Mythtv Video Buffer Streaming Problem comstocki Linux - Software 2 07-02-2007 06:21 AM
C: convert char buffer received from socket to host endianess buffer kpachopoulos Programming 3 06-07-2007 05:06 PM
What is the difference between the free buffer and buffer in the buffer hash queue? Swagata Linux - Enterprise 0 05-25-2006 11:57 PM
buffer problem with realplayer and streaming audio Accordion Linux - Newbie 0 02-27-2004 01:13 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 05:59 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration