Skip to content

Commit

Permalink
Made it far more atomic; it will attemp to process as many input or o…
Browse files Browse the repository at this point in the history
…utput bytes

as will let it reach its threshhold before it sleeps. It also sleeps for shorter
periods.
  • Loading branch information
mikemol committed Mar 9, 2012
1 parent 4614ae9 commit 0652f66
Showing 1 changed file with 74 additions and 37 deletions.
111 changes: 74 additions & 37 deletions entbuff.c
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,15 @@ void close_fdRandom()
fclose(fdRandom);
}

int read_from_buffer()
size_t read_from_buffer_internal(size_t toRead, size_t readPos)
{
// We let the caller handle concerns about how many bytes we can read,
// and where we can read from.

return fwrite(entbuff + readPos, toRead, 1, fdRandom);
}

size_t read_from_buffer(size_t toRead)
{
// If we don't have any bytes left to read, nullop.
if(0 == buff_read_remaining)
Expand All @@ -41,35 +49,76 @@ int read_from_buffer()
// Where in the buffer do we read from?
// pos_write is the next byte which will be written.
// So pos_write - 1 will be the last valid written byte.
int buff_read_pos = (pos_write - 1) - buff_read_remaining;
size_t buff_read_pos = (pos_write - 1) - buff_read_remaining;

// Did our starting position wrap?
if(buff_read_pos < 0)
// Buffer wrapped.
buff_read_pos += buff_size;

// Now, we may need to do one or two operations, depending on if our
// read range goes over the buffer wrap.
size_t first_read = (toRead < (buff_size - buff_read_pos)) ? toRead : (buff_size - buff_read_pos);
size_t bytes_read = read_from_buffer_internal(first_read, buff_read_pos);
toRead -= bytes_read;
buff_read_remaining -= bytes_read;
read_out_count += bytes_read;

if(( toRead > 0) && (buff_read_remaining > 0))
{
size_t bytes_read_second = read_from_buffer_internal(toRead, buff_read_pos);
buff_read_remaining -= bytes_read_second;
read_out_count += bytes_read_second;
bytes_read += bytes_read_second;
}

fwrite(entbuff + buff_read_pos, 1, 1, fdRandom);
int brr_w = buff_read_remaining;
--buff_read_remaining;
++read_out_count;
return 1;
return bytes_read;
}

size_t write_to_buffer_internal(size_t toWrite, size_t writePos)
{
// Caller can worry about buffer bounds
return fread(entbuff + writePos, toWrite, 1, fdRandom);
}

int write_to_buffer()
int write_to_buffer(int toWrite)
{
// If our buffer is full, nullop.
if(buff_size == buff_read_remaining)
return 0;

// If we don't have enough space in the buffer to add as many bytes as
// we're asked, reduce how many bytes we'll add.
toWrite = toWrite < (buff_size - buff_read_remaining) ? toWrite : (buff_size - buff_read_remaining);


// If we don't have any bytes left to work with, drop out.
if(toWrite == 0)
return 0;

// Buffer is wrapping.
if(pos_write >= buff_size)
pos_write -= buff_size;

fread(entbuff + pos_write, 1, 1, fdRandom);
int brr_w = buff_read_remaining;
int pw_w = pos_write;
++buff_read_remaining;
++pos_write;
++read_in_count;
return 1;
// Now we need to do one or two operations, depending on if our
// write range goes over the buffer wrap
size_t first_write = (toWrite <= (buff_size - pos_write)) ? toWrite : (buff_size - pos_write);
size_t bytes_written = write_to_buffer_internal(toWrite, pos_write);
toWrite -= bytes_written;
buff_read_remaining += bytes_written;
pos_write += bytes_written;
read_in_count += bytes_written;

if((toWrite > 0 && buff_size) > (buff_read_remaining))
{
size_t bytes_written_second = write_to_buffer_internal(toWrite, pos_write);
buff_read_remaining += bytes_written_second;
pos_write += bytes_written_second;
read_in_count += bytes_written_second;
bytes_written += bytes_written_second;
}

return bytes_written;
}

int check_ent()
Expand All @@ -88,10 +137,10 @@ int check_ent()
int main(int argc, char* argv[])
{
int entcnt = 0;
int entthresh_high = 4096 * 1 / 2;
int entthresh_high = 4096 * 1 / 8;
int entthresh_low = 4096 * 1 / 16;
int waittime = 62500;
int printperiod = 16;
int waittime = 12500;
int printperiod = 1000000 / waittime;

entbuff = mmap(NULL, buff_size, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
if(MAP_FAILED == entbuff)
Expand Down Expand Up @@ -147,30 +196,18 @@ int main(int argc, char* argv[])
printperiod_count = 0;
}
entcnt = check_ent();
while (
(
((entcnt >= entthresh_high) && (buff_read_remaining != buff_size))
|| ((entcnt <= entthresh_low) && (buff_read_remaining != 0))
)
)
if ( (entcnt > entthresh_high) || (entcnt < entthresh_low) )
{
int res = 0;
if(entcnt >= entthresh_high)
{
res = write_to_buffer();
}
else if(entcnt <= entthresh_low)
if(entcnt > entthresh_high)
{
res = read_from_buffer();
// Let's pull some bytes in for later.
write_to_buffer(entcnt - entthresh_high);
}
else
else if(entcnt < entthresh_low)
{
fprintf(stderr, "BUG: entcnt(%d), etl(%d), eth(%d), brr(%d), bs(%d)\n", entcnt, entthresh_low, entthresh_high, buff_read_remaining, buff_size);
// Let's get that back up to the threshold.
read_from_buffer(entthresh_low - entcnt);
}
entcnt = check_ent();
if(!res)
// Ran out of space, don't bother calling read or write.
break;
}
}

Expand Down

0 comments on commit 0652f66

Please sign in to comment.