limiting the hard drive read speed for a program c++

2

Is there a way to limit how fast a program can read from files without slowing the program itself down?

blood

Posted 2012-10-30T23:27:50.973

Reputation: 125

From inside or outside the program? Can you tell it to read from a new path, possibly mounted using a loopback or FUSE filesystem? – Ben Voigt – 2012-10-30T23:28:53.747

Is this specific to compiled C++ programs, or is the system you're writing this in implemented in C++? The tag is unclear (and possibly inapplicable). – None – 2012-10-30T23:30:35.963

Why? What are you trying to accomplish? – Adrian Cornish – 2012-10-30T23:32:27.747

From outside I do not want to edit the program in anyway. The program would be using the standard Windows file loading functions not interfacing to the program it's self. – blood – 2012-10-30T23:32:27.803

@Adrian Cornish I am trying to keep select programs from hogging all the hard drive's read speed and slowing down everything else that needs files. – blood – 2012-10-30T23:34:10.130

You could use OS facilities to reduce the processes' priorities. There are probably existing tools for the job. – Cogwheel – 2012-10-30T23:41:04.113

Here's an article about IO scheduling: https://en.wikipedia.org/wiki/I/O_scheduling. Unfortunately, it doesn't explain how to influence the IO scheduling of windows. But maybe you can use it as a starting point to search for that.

– None – 2012-10-30T23:47:41.730

Answers

2

On Windows Vista and above, you can set the I/O priority of a process. This won't limit the rate, but will give other processes priority over your process and allow them to take precedence. There are hidden APIs to do so, but it seems what you're looking for is a tool. When you set the process priority, using task manager, to Below Normal or Idle, the I/O priority drops too.

This question on suepruser has more information:

How to change I/O priority of a process or thread in Win7?

It also links to this project that allows you to set I/O priority.

http://sourceforge.net/projects/iopriority/

kichik

Posted 2012-10-30T23:27:50.973

Reputation: 236

0

Not sure about hardware or OS-specific solutions... But the obvious leaps out (forgive the crude implementation):

const long long maxBytesPerSec = 1048576;
static long long bytesRead = 0;
static long long secondsElapsed = 1;

istream & ReadBytesThrottled( istream & s, char * buffer, long long bytesToRead )
{
    while( (bytesRead+bytesToRead) / secondsElapsed > maxBytesPerSec ) {
        // Wait and update secondsElapsed
    }
    s.read(buffer, bytesToRead);
    if( s.good() ) bytesRead += bytesToRead;
    return s;
}

void ResetThrottle()
{
    bytesRead = 0;
    secondsElapsed = 1;
}

This is only relevant if you are reading chunks. I suppose you could derive from the stream and overload the relevant methods. Starts getting a bit yuck. But it's a simple and quick workaround if you can't find a lower-level solution.

paddy

Posted 2012-10-30T23:27:50.973

Reputation: 1 119