-
Notifications
You must be signed in to change notification settings - Fork 0
/
pa1_disk.c
304 lines (244 loc) · 8.9 KB
/
pa1_disk.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
//Assignment program to benchmark hard disk performance
//@author : Tejus Prasad
#include <stdio.h>
#include <pthread.h>
#include <time.h>
#include <sys/time.h>
#include <fcntl.h>
#include <sys/stat.h>
int fileDesc ;
pthread_mutex_t lock;
struct timeval start_time,end_time;
float write_thread_seq(long int size)
{
// open the file , write blocks of data using sequential access and return the time taken for write
pthread_mutex_lock(&lock);
long int blockSize=(long int)((1000000*20)/size);
char *fileName="trial.txt";
fileDesc = open(fileName,O_RDWR,S_IRWXU);
int itr;
char *writeData;
writeData=(char *)malloc(size);
gettimeofday(&start_time,NULL);
for(itr=0;itr<blockSize;itr++)
{
int currentPageSize =write(fileDesc,writeData,size);//write sequentially from starting position of file
}
gettimeofday(&end_time,NULL);
double data1=(double)start_time.tv_sec+((double)start_time.tv_usec/1000000);
double data2=(double)end_time.tv_sec+((double)end_time.tv_usec/1000000);
float dataTime=(double)data2-data1;//calculate the time taken for write in sec
pthread_mutex_unlock(&lock);
free(writeData);
return dataTime;
}
float read_thread_seq(long int size)
{
// open the file , read blocks of data using sequential access and return the time taken for read
long int blockSize=(long int)(1000000*20)/size;
pthread_mutex_lock(&lock);
char *fileName="trial.txt";
fileDesc = open(fileName,O_RDWR,S_IRWXU);
int itr;
char *readData;
readData=(char *)malloc(size);
gettimeofday(&start_time,NULL);
for(itr=0;itr<blockSize;itr++)
{
int currentPageSize =read(fileDesc,readData,size);//read sequentially from starting position of file
}
gettimeofday(&end_time,NULL);
double data1=(double)start_time.tv_sec+((double)start_time.tv_usec/1000000);
double data2=(double)end_time.tv_sec+((double)end_time.tv_usec/1000000);
float dataTime=data2-data1;//calculate the time taken for read in sec
pthread_mutex_unlock(&lock);
free(readData);
return dataTime;
}
float write_thread_random(long int size)
{
// open the file , write blocks of data using random access and return the time taken for write
int itr;
off_t random_pos;
long int blockSize=(long int)(1000000*20)/size;
char *fileName="trial.txt";
char *writeData;
fileDesc = open(fileName,O_RDWR,S_IRWXU);
pthread_mutex_lock(&lock);
writeData=(char *)malloc(size);
gettimeofday(&start_time,NULL);
for(itr=0;itr<blockSize;itr++)
{
random_pos = rand()%(int)blockSize;//assign random block to write
int currentPageSize =pwrite(fileDesc,writeData,size,random_pos);// writes from random block position
}
gettimeofday(&end_time,NULL);
double data1=(double)start_time.tv_sec+((double)start_time.tv_usec/1000000);
double data2=(double)end_time.tv_sec+((double)end_time.tv_usec/1000000);
float dataTime=data2-data1;//calculate the time taken for write in sec
free(writeData);
pthread_mutex_unlock(&lock);
return dataTime;
}
float read_thread_random(long int size)
{
// open the file , read blocks of data using random access and return the time taken for read
int itr;
off_t random_pos;
long int blockSize=(long int)(1000000*20)/size;
char *fileName="trial.txt";
char *readData;
fileDesc = open(fileName,O_RDWR,S_IRWXU);
pthread_mutex_lock(&lock);
readData=(char *)malloc(size);
gettimeofday(&start_time,NULL);
for(itr=0;itr<blockSize;itr++)
{
random_pos = rand()%(int)blockSize;//assign random block to read
int currentPageSize =pread(fileDesc,readData,size,random_pos);// read from random block position
}
gettimeofday(&end_time,NULL);
double data1=(double)start_time.tv_sec+((double)start_time.tv_usec/1000000);
double data2=(double)end_time.tv_sec+((double)end_time.tv_usec/1000000);
float dataTime=data2-data1; //calculate the time taken for read in sec
free(readData);
pthread_mutex_unlock(&lock);
return dataTime;
}
void main()
{
float throughput,latency;
int access;
int nothreads;
int operation;
long int size;
int i;
float timeTaken=0,dataTime;
printf("Enter Operation to perform 1-Read 2-Write");
fflush(stdout);
scanf("%d",&operation);
printf("\n Block size:(Select 1 for 1B=1,2 for 1KB=1024,3 for 1MB=1048576):");
fflush(stdout);
scanf("%d",&size);
printf("Access Method : 1-Sequential 2-Random");
fflush(stdout);
scanf("%d",&access);
printf("Enter number of threads 1,2,4 :");
fflush(stdout);
scanf("%d",¬hreads);
pthread_t pth[nothreads];
if(size==2)
{
// if 2 is selected assign 1024 bytes to read or write from disk
size=(long int)(1024);
}
if(size==3)
{ // if 3 is selected assign 1024*2014 bytes to read or write from disk
size=(long int)(1024*1024);
}
if (pthread_mutex_init(&lock, NULL) != 0)
{
printf("\n mutex init failed\n");
return 1;
}
char *fileName="trial.txt";
char *f="hs";
fileDesc = open(fileName,O_CREAT|O_RDWR,S_IRWXU);
for(i=0;i<=(1024*1024*20);i++)
{
int currentPageSize =write(fileDesc,f,2);
}
// create given no of threads and perform operations (read/write)
for(i=0;i<nothreads;i++)
{
if(access==1 && operation==1)
{
//this block is for read operation sequential access
pthread_create(&pth[i],NULL,read_thread_seq,(long int)size);
//creates a thread and calls read_thread function
}
if(access==2 && operation==1 )
{ //this block is for read operation random access
pthread_create(&pth[i],NULL,read_thread_random,(long int)size);
//creates a thread and calls read_thread_random function
}
if(access==1 && operation==2)
{ //this block is for write operation using sequential access
pthread_create(&pth[i],NULL,write_thread_seq,(long int)size);
//creates a thread and calls write_thread_seq function
}
if(access==2 && operation==2)
{ //this block is for write operation using random access
pthread_create(&pth[i],NULL,write_thread_random,(long int)size);
//creates a thread and calls write_thread_random function
}
}
// joining the thread with other thread
for(i=0;i<nothreads;i++)
{
pthread_join(pth[i],&dataTime);
timeTaken=(float)timeTaken+dataTime; // add the time taken for all threads
}
timeTaken=(timeTaken)/nothreads;
throughput=(float)(1000000*20)/(timeTaken); //(no of loops*size*2/time taken)
latency=(float)(timeTaken*1000*size)/(1000000*20);
if(access==1 && operation==1)
{
printf("Throughput for sequential access read is %f ",(float)(throughput)/(1024*1024));
printf("Latency for sequential access read is %f",latency);
}
if(access==2 && operation==1)
{
printf("Throughput for random access read is %f",(float)(throughput)/(1024*1024));
printf("Latency for random access read is %f",latency);
} if(access==1 && operation==2)
{
printf("Throughput for sequential access write is %f",throughput/(1024*1024));
printf("Latency for sequential access write is %f",latency);
}
if(access==2 && operation==2)
{
printf("Throughput for random access write is %f",throughput/(1024*1024));
printf("Latency for random access write is %f",latency);
}
//store result in file
int filesc;
char *fs="values.txt";
filesc = open(fs,O_CREAT|O_RDWR,S_IRWXU);
char *c = (char *)malloc(sizeof(char)*10000);
char *s = (char *)malloc(sizeof(char)*100);
c[0] = '\0';
strcat(c,"\r\n");
strcat(c,"size");
strcat(c," ");
strcat(c,"operation");
strcat(c," ");
strcat(c,"threads");
strcat(c," ");
strcat(c,"access");
strcat(c," ");
strcat(c,"throughput");
strcat(c," ");
strcat(c,"latency");
strcat(c,"\r\n");
sprintf (s,"%d",(int)size);
strcat(c,s);
strcat(c," ");
sprintf (s,"%d",(int)operation);
strcat(c,s);
strcat(c," ");
sprintf (s,"%d",(int)nothreads);
strcat(c,s);
strcat(c," ");
sprintf (s,"%d",(int)access);
strcat(c,s);
strcat(c," ");
sprintf (s,"%f",(float)throughput/(1024*1024));
strcat(c,s);
strcat(c," ");
sprintf (s,"%f",(float)latency);
strcat(c,s);
strcat(c,"\r\n");
printf("\n c is \n %s",c);
write(filesc,c,strlen(c));
}