LinuxQuestions.org
Visit Jeremy's Blog.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Software > Linux - Kernel
User Name
Password
Linux - Kernel This forum is for all discussion relating to the Linux kernel.

Notices


Reply
  Search this Thread
Old 02-27-2025, 08:18 AM   #1
elvise
LQ Newbie
 
Registered: Feb 2025
Posts: 3

Rep: Reputation: 0
c++ queues and kernel buffers


Hi all,

I don't know if this is the right forum to ask this question but here it is!

The context is a c++ application implementing a classic producer/consumer pattern using two threads and the std::queue. The implementation comes from one of many thread-safe examples available on the web. The queue has no control on maximum size, that is the producer can push as many elements as it wants.

The producer receives real-time sample buffers from an optical network interface (something like 50/100M samples per second) and pushes them in the queue. Buffers are 40k bytes size.

The consumer extracts buffers from the queue and writes them on SSD disk.

Running the application, "top" shows that the kernel buffers start to eat the available memory (16GB) until there's about 0.5GB available. The situation then remains stable, with the producer pushing millions of samples in the queue and the consumer slowly writing on the disk.

After all samples (for 5 minutes of streaming something like 120GB at 100M samples per second) are received, the consumer has yet to write to disk much more GB than the ones available in memory.

"top" also shows that no disk swapping is performed.

When the consumer finishes its job several minutes after, the file contaning all samples has correct size.

The question is: how can this happen? I thought that after allocating all memory for kernel buffers, the OS began to swap or the kernel killed the application because of out of memory. Where is this huge queue memorized while the consumer slowly writes its elements to file? I can't explain this...maybe some kernel expert can help me understand.

Thank you very much!

Regards,
Elvio
 
Old 02-28-2025, 07:49 AM   #2
plasticassius
LQ Newbie
 
Registered: Feb 2025
Posts: 3

Rep: Reputation: 0
Hi Elvio, I assume that you C++ program is running in user space, so it wouldn't consume kernel memory other than what is used to buffer i/o. The kernel buffers writes to disk when the disk can't keep up. Do you have a swap file or partition? You won't see swapping without that, but if the swap device is the same SSD as you're writing the file to, swapping won't be any faster than writing to the output file itself. Hope this helps.
 
Old 02-28-2025, 11:20 AM   #3
plasticassius
LQ Newbie
 
Registered: Feb 2025
Posts: 3

Rep: Reputation: 0
I think the most likely explanation is that the ssd mostly keeps up with the data, but needs the small writes to be grouped together into bigger ones.
 
Old 02-28-2025, 07:42 PM   #4
syg00
LQ Veteran
 
Registered: Aug 2003
Location: Australia
Distribution: Lots ...
Posts: 21,385

Rep: Reputation: 4191Reputation: 4191Reputation: 4191Reputation: 4191Reputation: 4191Reputation: 4191Reputation: 4191Reputation: 4191Reputation: 4191Reputation: 4191Reputation: 4191
While we wait for a kernel expert ...

What you describe is classic page cache usage. That is how it is designed to work. The usage is "elastic" in that it will occupy otherwise unused RAM, and will give it up if some other task requires it.
This also explains the delayed writes, but was designed for spinning disk. For SSD, depending on the model, and what bus it is connected to, can maintain multiple concurrent writes with the correct I/O scheduler.
Note all the "it depends".

If it's working ok, I'd say leave it alone unless you can tolerate making things worse as a learning exercise. Maybe have a read of this for some background.
 
Old 03-05-2025, 05:16 AM   #5
elvise
LQ Newbie
 
Registered: Feb 2025
Posts: 3

Original Poster
Rep: Reputation: 0
Thank you all for your answers and explanations, it is clear that I need more background on the subject.
In the mean time the kernel is just a black box doing magic!

PS the swap file is on another SSD.

Regards,
Elvio
 
Old 03-12-2025, 09:27 PM   #6
sundialsvcs
LQ Guru
 
Registered: Feb 2004
Location: SE Tennessee, USA
Distribution: Gentoo, LFS
Posts: 11,206
Blog Entries: 4

Rep: Reputation: 4126Reputation: 4126Reputation: 4126Reputation: 4126Reputation: 4126Reputation: 4126Reputation: 4126Reputation: 4126Reputation: 4126Reputation: 4126Reputation: 4126
Basically, I think you need to dream up some kind of effective throttle. For example, some variant of std::queue] that does have "flow controls."

(Of course, when I say, "dream up," I really mean that you should methodically search through (e.g.) "GitHub" and "SourceForge" to find existing work that already does this.)

The writer may find that the queue sometimes refuses to accept another entry. This may mean that a sample might be dropped.

Another possibility is to design the system so that there are multiple consumers who are grabbing-off samples and writing them to different devices, to be sorted-out later.
 
Old 03-13-2025, 12:51 AM   #7
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 24,276

Rep: Reputation: 7966Reputation: 7966Reputation: 7966Reputation: 7966Reputation: 7966Reputation: 7966Reputation: 7966Reputation: 7966Reputation: 7966Reputation: 7966Reputation: 7966
You are stating you have 16 GB RAM and 120 GB data. That won't fit, for*sure.
I don't think top is the right tool to analyze it. You may completely switch off swap to see what will happen.
 
Old 03-24-2025, 12:19 PM   #8
elvise
LQ Newbie
 
Registered: Feb 2025
Posts: 3

Original Poster
Rep: Reputation: 0
Hi all,

actually the RAM is 32Gb, not 16Gb, maybe this could partially explain the kernel doing magic.
The files do contain the right stuff (even the 120Gb ones) therefore the system is indeed doing right.

As said before a mix of page caching, fast SSD and maybe other factors do the trick.
Thanks again for your explanations.

Regards,
Elvio

PS Didn't try to disable the swap, if I have the chance I will try.
 
  


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
Threads checking for space two times in queues VS keeping the queues in sorted order Aquarius_Girl Programming 1 12-23-2015 08:05 PM
POSIX Queues with message type facility of System V queues ehsan_haq98@yahoo.com Programming 2 12-27-2009 11:45 AM
POSIX message queues(Solaris) to SYS V message queues(Linux) devershetty Programming 1 01-22-2007 10:15 AM
Message queues with custom buffers estratos Programming 2 12-17-2006 03:13 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Software > Linux - Kernel

All times are GMT -5. The time now is 05:23 PM.

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