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 08-13-2019, 05:06 AM   #1
aKumara
LQ Newbie
 
Registered: Aug 2019
Posts: 4

Rep: Reputation: Disabled
how to determine page(cache) boundaries when writing to a file


In linux when writing to a file, kernel maintains multiple in memory pages (4KB in size). Data is first written to the pages and background process bdflush sends these data to disk drive.

Is there a way to determine page boundaries when writing sequentially to a file ?

Can I assume it is always 1-4096 : page 1 and 4097-8192 : page 2 ?

or can it vary ?

say if I start writing from 10 (i.e. first 10 bytes already written to the file previously and I set file position to 10 before start writing) will the page boundary still be

1-4096 : page 1

OR

10-5096 : page 1 ?

Reason for asking,

I can use sync_file_range http://man7.org/linux/man-pages/man2...e_range.2.html to flush data from kernel pages to disk drive in a orderly manner. If I can determine page boundaries I can call sync_file_range only when a page boundary is reached, so that unnecessary sync_file_range calls are avoided.
 
Old 08-13-2019, 05:54 AM   #2
berndbausch
Senior Member
 
Registered: Nov 2013
Location: Tokyo
Distribution: Redhat/Centos, Ubuntu, Raspbian, Fedora, Alpine, Cirros, OpenSuse/SLES
Posts: 3,179

Rep: Reputation: 832Reputation: 832Reputation: 832Reputation: 832Reputation: 832Reputation: 832Reputation: 832
Quote:
Originally Posted by aKumara View Post
In linux when writing to a file, kernel maintains multiple in memory pages (4KB in size). Data is first written to the pages and background process bdflush sends these data to disk drive.

Is there a way to determine page boundaries when writing sequentially to a file ?
The page size is usually 4K, but to be certain, run
Code:
$ getconf PAGESIZE
4096
As you assume, pages go from 0 to 4095, 4096 to 8191, 8192 to 12xxx etc. Boundaries are multiples of the page size.

EDIT: Boundaries are multiple of page sizes, but I am not entirely certain whether there is a guarantee that a file always starts at the beginning of a page. By default, this seems to be the case in an ext4 filesystem, but perhaps there are options that allow a file to start somewhere in the middle of a block, if it is small.

A quick test:
Code:
$ echo x >testfile
$ ls -ls testfile
4 -rw-rw-r-- 1 ubuntu ubuntu 2 Aug 13 19:57 testfile
The 4 at the beginning of the last line is the actual space occupied by this file. It's 4K. I conclude that the file starts at a block boundary, therefore at a page boundary in memory.

Last edited by berndbausch; 08-13-2019 at 06:01 AM. Reason: rewording, reformatting
 
  


Reply

Tags
file


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
Why does fs/buffer.c exist? Didn't the page cache and buffer cache merge in 2.4? bagelboy62 Linux - Kernel 2 12-14-2017 08:12 AM
what is left inside the cache even after clearing the page cache data? ankitlinux Linux - Newbie 4 10-09-2015 06:06 AM
Difference between page cache and buffer cache Nayaka Linux - Kernel 5 09-28-2011 08:23 AM
fdisk gives me warnign about cylinder boundaries.. zeppelin Linux - Hardware 2 10-19-2003 08:44 AM
Apache logging stops at 4k boundaries shilbert Linux - Software 2 06-26-2003 12:14 PM

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

All times are GMT -5. The time now is 08:31 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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration