473,383 Members | 1,748 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,383 software developers and data experts.

O_DIRECT on stdin?


Is there a way of setting O_DIRECT on a preexisting file like sys.stdin?

Does C allow this sort of thing?

Thanks!

Nov 7 '05 #1
8 3947
>Is there a way of setting O_DIRECT on a preexisting file like sys.stdin?

Does C allow this sort of thing?


There is no O_DIRECT or fcntl() in Standard C.

fcntl() operates on an open file descriptor, and the file descriptor
for stdin is 0. Two calls to fcntl(), one with F_GETFL and one
with F_SETFL, would do what you want.

I'm not sure why you want to do that, though. It's not going to
get you character-at-a-time I/O, if that's what you want.

Gordon L. Burditt
Nov 7 '05 #2
I think this is fcntl(..., F_SETFL, ...), so something like
import os, fcntl, sys
flags = fcntl.fcntl(sys.stdin.fileno(), fcntl.F_GETFL)
flags |= os.O_DIRECT
fcntl.fcntl(sys.stdin.fileno(), fcntl.F_SETFL, flags)

Jeff

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.1 (GNU/Linux)

iD8DBQFDb7CcJd01MZaTXX0RAi5+AJ0eeXXROgvPtbcg6br8Zf/KsGPAxgCeLFb0
xNIG26j8JMJIecGiNWmcU0E=
=2LQx
-----END PGP SIGNATURE-----

Nov 7 '05 #3
On Mon, 07 Nov 2005 19:48:47 +0000, Gordon Burditt wrote:
[quoted text muted]


There is no O_DIRECT or fcntl() in Standard C.

fcntl() operates on an open file descriptor, and the file descriptor
for stdin is 0. Two calls to fcntl(), one with F_GETFL and one
with F_SETFL, would do what you want.

I'm not sure why you want to do that, though. It's not going to
get you character-at-a-time I/O, if that's what you want.

Gordon L. Burditt


I want to be able to read a HUGE file without having such a negative
impact on the system's buffer cache.

I'm trying:

if hasattr(os, 'O_DIRECT'):
try:
flags = fcntl.fcntl(sys.stdin.fileno(), fcntl.F_GETFL)
flags |= os.O_DIRECT
fcntl.fcntl(sys.stdin.fileno(), fcntl.F_SETFL, flags)
except:
sys.stderr.write('Setting O_DIRECT on stdin attempted but failed\n')
else:
sys.stderr.write('Setting O_DIRECT on stdin succeeded :)\n')

....but while this code doesn't error out, I get:

seki-root> reblock -e $[1024*1024*80] $[1024*1024] 300 < /dev/sda1 > /dev/null
stdin seems seekable, but file length is 0 - no exact percentages
Estimated filetransfer size is 85899345920 bytes
Estimated percentages will only be as accurate as your size estimate
Setting O_DIRECT on stdin succeeded :)
Traceback (most recent call last):
File "/Dcs/seki/strombrg/bin/reblock", line 276, in ?
main()
File "/Dcs/seki/strombrg/bin/reblock", line 222, in main
block = os.read(0,blocksize)
OSError: [Errno 22] Invalid argument
Mon Nov 07 12:25:53

....but if I comment out the fcntl/O_DIRECT code, then the same thing works
well.

Any other ideas folks?

Thanks!

Nov 7 '05 #4
>I want to be able to read a HUGE file without having such a negative
impact on the system's buffer cache.

I'm trying:

if hasattr(os, 'O_DIRECT'):
try:
flags = fcntl.fcntl(sys.stdin.fileno(), fcntl.F_GETFL)
flags |= os.O_DIRECT
fcntl.fcntl(sys.stdin.fileno(), fcntl.F_SETFL, flags)
except:
sys.stderr.write('Setting O_DIRECT on stdin attempted but failed\n')
else:
sys.stderr.write('Setting O_DIRECT on stdin succeeded :)\n')

...but while this code doesn't error out, I get:

seki-root> reblock -e $[1024*1024*80] $[1024*1024] 300 < /dev/sda1 > /dev/null
stdin seems seekable, but file length is 0 - no exact percentages
Estimated filetransfer size is 85899345920 bytes
Estimated percentages will only be as accurate as your size estimate
Setting O_DIRECT on stdin succeeded :)
Traceback (most recent call last):
File "/Dcs/seki/strombrg/bin/reblock", line 276, in ?
main()
File "/Dcs/seki/strombrg/bin/reblock", line 222, in main
block = os.read(0,blocksize)
OSError: [Errno 22] Invalid argument
Mon Nov 07 12:25:53

...but if I comment out the fcntl/O_DIRECT code, then the same thing works
well.

Any other ideas folks?


Does O_DIRECT perhaps invoke some of the restrictions of "raw"
device files, where the current offset and transfer size must be a
multiple of some block size? (I don't see any mention of that in
FreeBSD's documentation.) What is the value of blocksize at the
time of the traceback above? I suggest keeping it well under 2G.

Gordon L. Burditt
Nov 7 '05 #5
On Mon, 07 Nov 2005 20:42:50 +0000, Gordon Burditt wrote:
[quoted text muted]


Does O_DIRECT perhaps invoke some of the restrictions of "raw"
device files, where the current offset and transfer size must be a
multiple of some block size? (I don't see any mention of that in
FreeBSD's documentation.) What is the value of blocksize at the
time of the traceback above? I suggest keeping it well under 2G.

Gordon L. Burditt


I'm not aware of such a restriction on O_DIRECT, but then I just learned
of O_DIRECT's existence earlier today. :)

The value of blocksize at the time of the error should be 1 megabyte.

Nov 7 '05 #6
Here's some text from my open(2) manpage:
Transfer sizes, and the alignment of user buffer and file offset mustall
be multiples of the logical block size of the file system.
It's unlikely that in practice you can get Python's sys.stdin.read() or
os.read() to reliably use a buffer that fits the alignment restriction.

However, a small "C" extension could provide something like os.read() which
*does* meet the restriction. The meat of it would look something like
#define PAGESIZE 4096 // a guess which may be right for x86 linux
#define REQUESTSIZE 1048576

ssize_t result;
char *buf, *base;
PyObject *pyresult;

buf = malloc(bufsize, REQUESTSIZE + PAGESIZE - 1);
base = round_up(buf, PAGESIZE);

result = read(0, base, REQUESTSIZE);

if(result == -1) {
set python error from errno
pyresult = NULL;
goto DONE;
}

pyresult = PyString_FromStringAndSize(base, result);

DONE:
free(buf);
return pyresult;

Here's a clever but untested "C" macro that claims to implement round_up:
#define round_up(amount, align) ((((amount) - 1) | ((align) - 1)) +1)

Jeff

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.1 (GNU/Linux)

iD8DBQFDb8iGJd01MZaTXX0RAm/AAKCjJmBlQ/Lzu5kEnnA/GzqLeeE03QCffJWP
0T/vsxfB6dLlr4kvwaOtJjE=
=3Pyy
-----END PGP SIGNATURE-----

Nov 7 '05 #7
In article <ma**************************************@python.o rg>,
je****@unpythonic.net wrote:
Here's some text from my open(2) manpage:
Transfer sizes, and the alignment of user buffer and file offset must
all
be multiples of the logical block size of the file system.
Does that apply in the example he gave, < /dev/sda1 ?

It seems to me this would not go through any filesystem anyway.
That might account for the "invalid argument" error, but at any
rate it would be irrelevant.

Plus it doesn't seem to score very high on portability, according
to the Linux man page I'm looking at -- apparently not a POSIX
or any such standard, just borrowed from Irix in recent Linux
versions, and FreeBSD with slightly different behavior. Don't
see any trace of it in NetBSD, MacOS X.
It's unlikely that in practice you can get Python's sys.stdin.read() or
os.read() to reliably use a buffer that fits the alignment restriction.


Though of course os.read() would eliminate one layer of buffering
altogether. Might be worth a try.

Donn Cave, do**@u.washington.edu
Nov 7 '05 #8
"Gordon Burditt" <go****@hammy.burditt.org> wrote in message
news:11*************@corp.supernews.com...
I want to be able to read a HUGE file without having such a negative
impact on the system's buffer cache.
[snip] Does O_DIRECT perhaps invoke some of the restrictions of "raw"
device files, where the current offset and transfer size must be a
multiple of some block size?


Very likely. It is also likely that the same applies to the destination (ie
memory) address.

Alex
Nov 8 '05 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: lickspittle | last post by:
Hi, I have Python embedded with my other code, and when my other code opens a console and redirects stdout, stdin and stderr to it, then calls PyRun_InteractiveLoop, it immediately returns with...
3
by: Harayasu | last post by:
Hi, Using fgets() I can read from stdin and with fputs() I can write to stdout. Now I have two programs, one writing to stdin and the other one reading from stdin. And I would like the second...
23
by: herrcho | last post by:
What's the difference between STDIN and Keyboard buffer ? when i get char through scanf, i type in some characters and press enter, then, where do the characters go ? to STDIN or Keyboard...
6
by: Charlie Zender | last post by:
Hi, I have a program which takes the output filename argument from stdin. Once the program knows the output filename, it tries to open it. If the output file exists, the program asks the user to...
6
by: ccdrbrg | last post by:
What is the best way to protect stdin within a library? I am writing a terminal based program that provides plugin capability using the dlopen() API. Sequencing program commands (typed) and...
1
by: asdsd sir | last post by:
Hi!I'm new in Python and i'd like to ask some general questions about stdin,stdout... Firstly... if we type like something like : cat "file.txt"|python somefile.py #somefile.py import sys
8
by: aine_canby | last post by:
The following line in my code is failing because sys.stdin.encoding is Null. This has only started happening since I started working with Pydef in Eclipse SDK. Any ideas? ...
31
by: Nikos Chantziaras | last post by:
Hello. Is there a way to check if the current process has an stdin handle? In the win32 API, one can do: _eof(_fileno(stdin)) Crucial here is that the above doesn't block. Is there a...
0
by: Gabriel Genellina | last post by:
En Thu, 25 Sep 2008 09:49:31 -0300, Almar Klein <almar.klein@gmail.com> escribió: Use subprocess.PIPE Usually the tricky part is to figure out exactly whether there is more input or not. With...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.