Reputation: 157
I am writing a program to benchmark the memory using read+write functions in c.
I have written a program below which accepts the block size of memory to be copied to another memory location using either sequential OR random access.
Each time I give a value greater than 12 bytes , the programmes give below message :
Elapsed time for reading and writing seconds in random manner : 0.000006
Error in `./a.out': free(): invalid next size (fast): 0x00000000022cc030 Aborted (core dumped)
#include <stdio.h>
#include <time.h>
#include <string.h>
#include <stdlib.h>
#include <pthread.h>
int rand_lim(int blocksize) {
/* return a random number between 0 and limit inclusive.
*/
int divisor = RAND_MAX/(blocksize+1);
int random_byte;
do {
random_byte = rand() / divisor;
} while (random_byte > blocksize);
return random_byte;
}
int main(int argc , char *argv[])
{
char temp;
int num_threads=0;
char *buffer, *write_buffer, access;
int i=0, block_size;
printf("Enter the block size of data in number of bytes:");
scanf("%d",&block_size);
buffer = (char*)malloc(block_size);
write_buffer = (char*)malloc(block_size);
for (i=0; i<block_size; i++)
{
memset(&buffer[i],'a',block_size);
//printf("%s",&buffer[i]);
}
printf("\nEnter the acess method --> random(R) OR seq(S) : ");
scanf("%s", &access);
printf("\nEnter the number of threads 1 , 2 , 4 , 8 \n");
scanf("%d", &num_threads);
if (access == 'S')
{
clock_t tic1 = clock();
for (i=0; i<block_size; i++)
{
memcpy(&write_buffer[i],&buffer[i],block_size);
//printf("%s",&write_buffer[i]);
}
clock_t toc1 = clock();
printf("Elapsed time for reading and writing in sequential manner : %f seconds \n", (double)(toc1 - tic1)/CLOCKS_PER_SEC);
free(buffer);
free(write_buffer);
}
else if(access == 'R')
{
clock_t tic1 = clock();
for (i=0; i<block_size; i++)
{
int j = rand_lim(block_size);
memcpy(&write_buffer[j],&buffer[j],block_size);
int dummy = i;
}
clock_t toc1 = clock();
printf("Elapsed time for reading and writing seconds in random manner : %f \n", (double)(toc1 - tic1)/CLOCKS_PER_SEC);
free(buffer);
free(write_buffer);
}
else
{
printf ("\nPlease enter the correct method for bench marking. Exiting...");
exit(0);
}
return 0;
}
I am trying to benchmark with large data like 1 GB but the programme doesn't allow this. Any help is appreciated.
Upvotes: 0
Views: 755
Reputation: 41321
In addition to John Zwinck's answer:
Probably what you wanted was
int block_count = ...;
write_buffer = malloc(block_count * block_size);
...
for (i=0; i<block_count; i++)
memcpy(write_buffer + i * block_size, buffer, block_size);
...
for (i=0; i<block_count; i++)
{
int j = rand_lim(block_count - 1); // because rand_lim includes upper limit
memcpy(write_buffer + j * block_size, buffer, block_size);
}
That is, write_buffer
consists of N
chunks with size block_size
each, and you write them either sequentially or randomly.
BTW rand_lim()
can be written much simpler:
int rand_lim(int limit) {
return rand() % (limit + 1); // if you want to include the upper limit for some reason
}
Upvotes: 0
Reputation: 25885
for (i=0; i<block_size; i++)
{
memset(&buffer[i],'a',block_size); // !!
}
You not need to run loop and every time memset the buffer with specific value. You can achieve it with single line
memset(buffer, 'a', block_size);
Also same is true for memcpy
. one of the is
for (i=0; i<block_size; i++)
{
memcpy(&write_buffer[i],&buffer[i],block_size);
//printf("%s",&write_buffer[i]);
}
here also
memcpy(write_buffer,buffer,block_size);
is enough
Finally
Do not cast the result of malloc
Upvotes: 1
Reputation: 249303
This is undefined behavior:
buffer = (char*)malloc(block_size);
for (i=0; i<block_size; i++)
{
memset(&buffer[i],'a',block_size); // !!
}
You should replace the loop with:
memset(buffer, 'a', block_size);
Upvotes: 3