forked from wolfwood/postmark
-
Notifications
You must be signed in to change notification settings - Fork 0
/
postmark-1.51.c
1531 lines (1267 loc) · 46.9 KB
/
postmark-1.51.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
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
Written by Jeffrey Katcher under contract to Network Appliance.
Copyright (C) 1997-2001
Network Appliance, Inc.
This code has been successfully compiled and run by Network
Appliance on various platforms, including Solaris 2 on an Ultra-170,
and Windows NT on a Compaq ProLiant. However, this PostMark source
code is distributed under the Artistic License appended to the end
of this file. As such, no support is provided. However, please report
any errors to the author, Jeffrey Katcher <katcher@netapp.com>, or to
Andy Watson <watson@netapp.com>.
Versions:
1.00 - Original release - 8/17/97
1.01 - Fixed endless loop on EOF,
Divide by zero when file_size_high=file_size_low - 10/29/97
(Thanks to Chuck Murnane)
1.1 - Added new commands to distribute work across multiple directories
and/or file systems and multiple work subdirectories.
Changed set location command (+,-) to allow file systems & weights
Added set subdirectories command and code to distribute work across
multiple subdirectories
Added file redirect to show and run commands
Improved help system - 4/8/98
1.11 - Fixed unfortunate problem where read_file opens in append mode thus
avoiding actual reads. (Thanks to Kent Peacock)
1.12 - Changed bytes read and written to float. Hopefully this will avoid
overflow when very large file sizes are used.
1.13 - Added terse report option allowing results to be easily included in
other things. (Thanks to Walter Wong)
Also tweaked help code to allow partial matches
1.14 - Automatically stop run if work files are depleted
1.5 - Many people (most recently Michael Flaster) have emphasized that the
pseudo-random number generator was more pseudo than random. After
a review of the literature and extensive benchmarking, I've replaced
the previous PRNG with the Mersenne Twister. While an excellent PRNG,
it retains much of the performance of the previous implementation.
URL: http://www.math.keio.ac.jp/~matumoto/emt.html
Also changed MB definition to 1024KB, tweaked show command
1.51 - Added command to load config file from CLI
*/
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <time.h>
#include <fcntl.h>
#define PM_VERSION "v1.51 : 8/14/01"
#ifdef _WIN32
#include <io.h>
#include <direct.h>
#define GETWD(x) getcwd(x,MAX_LINE)
#define MKDIR(x) mkdir(x)
#define SEPARATOR "\\"
#else
extern char *getwd();
#define GETWD(x) getwd(x)
#define MKDIR(x) mkdir(x,0700)
#define SEPARATOR "/"
#include <sys/stat.h>
#include <sys/types.h>
#include <fcntl.h>
#include <unistd.h>
#endif
#define MAX_LINE 255
#define MAX_FILENAME 80
#define KILOBYTE 1024
#define MEGABYTE (KILOBYTE*KILOBYTE)
#define PROMPT "pm>"
typedef struct { /* ADT for table of CLI commands */
char *name; /* name of command */
int (*func)(); /* pointer to callback function */
char *help; /* descriptive help string */
} cmd;
extern int cli_set_size();
extern int cli_set_number();
extern int cli_set_seed();
extern int cli_set_transactions();
extern int cli_set_location();
extern int cli_set_subdirs();
extern int cli_set_read();
extern int cli_set_write();
extern int cli_set_buffering();
extern int cli_set_bias_read();
extern int cli_set_bias_create();
extern int cli_set_report();
extern int cli_set_createonly();
extern int cli_run();
extern int cli_load();
extern int cli_show();
extern int cli_help();
extern int cli_quit();
cmd command_list[]={ /* table of CLI commands */
{"set size",cli_set_size,"Sets low and high bounds of files"},
{"set number",cli_set_number,"Sets number of simultaneous files"},
{"set seed",cli_set_seed,"Sets seed for random number generator"},
{"set transactions",cli_set_transactions,"Sets number of transactions"},
{"set location",cli_set_location,"Sets location of working files"},
{"set subdirectories",cli_set_subdirs,"Sets number of subdirectories"},
{"set read",cli_set_read,"Sets read block size"},
{"set write",cli_set_write,"Sets write block size"},
{"set buffering",cli_set_buffering,"Sets usage of buffered I/O"},
{"set bias read",cli_set_bias_read,
"Sets the chance of choosing read over append"},
{"set bias create",cli_set_bias_create,
"Sets the chance of choosing create over delete"},
{"set report",cli_set_report,"Choose verbose or terse report format"},
{"set createonly",cli_set_createonly, "Choose to create files only or not (0/1); default(0) run transactions and delete all files"},
{"run",cli_run,"Runs one iteration of benchmark"},
{"load",cli_load,"Read configuration file"},
{"show",cli_show,"Displays current configuration"},
{"help",cli_help,"Prints out available commands"},
{"quit",cli_quit,"Exit program"},
NULL
};
extern void verbose_report();
extern void terse_report();
void (*reports[])()={verbose_report,terse_report};
/* Counters */
int files_created; /* number of files created */
int files_deleted; /* number of files deleted */
int files_read; /* number of files read */
int files_appended; /* number of files appended */
float bytes_written; /* number of bytes written to files */
float bytes_read; /* number of bytes read from files */
/* Configurable Parameters */
int file_size_low=500;
int file_size_high=10000; /* file size: fixed or random within range */
int simultaneous=500; /* simultaneous files */
int seed=42; /* random number generator seed */
int transactions=500; /* number of transactions */
int subdirectories=0; /* Number of subdirectories */
int read_block_size=512; /* I/O block sizes */
int write_block_size=512;
int bias_read=5; /* chance of picking read over append */
int bias_create=5; /* chance of picking create over delete */
int buffered_io=1; /* use C library buffered I/O */
int report=0; /* 0=verbose, 1=terse report format */
int createonly=0; /* 0=create files, run transactions and delete files; 1=only create files */
/* Working Storage */
char *file_source; /* pointer to buffer of random text */
typedef struct {
char name[MAX_FILENAME+1]; /* name of individual file */
int size; /* current size of file, 0 = unused file slot */
} file_entry;
file_entry *file_table; /* table of files in use */
int file_allocated; /* pointer to last allocated slot in file_table */
typedef struct file_system_struct {
file_entry system;
struct file_system_struct *next,*prev;
} file_system;
file_system *file_systems; /* table of file systems/directories to use */
int file_system_weight; /* sum of weights for all file systems */
int file_system_count; /* number of configured file systems */
char **location_index; /* weighted index of file systems */
char *read_buffer; /* temporary space for reading file data into */
#define RND(x) ((x>0)?(genrand() % (x)):0)
extern unsigned long genrand();
extern void sgenrand();
/* converts integer values to byte/kilobyte/megabyte strings */
char *scale(i)
int i;
{
static char buffer[MAX_LINE]; /* storage for current conversion */
if (i/MEGABYTE)
sprintf(buffer,"%.2f megabytes",(float)i/MEGABYTE);
else
if (i/KILOBYTE)
sprintf(buffer,"%.2f kilobytes",(float)i/KILOBYTE);
else
sprintf(buffer,"%d bytes",i);
return(buffer);
}
/* converts float values to byte/kilobyte/megabyte strings */
char *scalef(i)
float i;
{
static char buffer[MAX_LINE]; /* storage for current conversion */
if (i/(float)MEGABYTE>1)
sprintf(buffer,"%.2f megabytes",i/(float)MEGABYTE);
else
if (i/(float)KILOBYTE)
sprintf(buffer,"%.2f kilobytes",i/(float)KILOBYTE);
else
sprintf(buffer,"%f bytes",i);
return(buffer);
}
/* UI callback for 'set size' command - sets range of file sizes */
int cli_set_size(param)
char *param; /* remainder of command line */
{
char *token;
int size;
if (param && (size=atoi(param))>0)
{
file_size_low=size;
if ((token=strchr(param,' ')) && (size=atoi(token))>0 &&
size>=file_size_low)
file_size_high=size;
else
file_size_high=file_size_low;
}
else
fprintf(stderr,"Error: no file size low or high bounds specified\n");
return(1);
}
int cli_generic_int(var,param,error)
int *var; /* pointer to variable to set */
char *param; /* remainder of command line */
char *error; /* error message */
{
int value;
if (param && (value=atoi(param))>0)
*var=value;
else
fprintf(stderr,"Error: %s\n",error);
return(1);
}
/* UI callback for 'set number' command - sets number of files to create */
int cli_set_number(param)
char *param; /* remainder of command line */
{
return(cli_generic_int(&simultaneous,param,"invalid number of files"));
}
/* UI callback for 'set seed' command - initial value for random number gen */
int cli_set_seed(param)
char *param; /* remainder of command line */
{
return(cli_generic_int(&seed,param,"invalid seed for random numbers"));
}
/* UI callback for 'set transactions' - configure number of transactions */
int cli_set_transactions(param)
char *param; /* remainder of command line */
{
return(cli_generic_int(&transactions,param,"no transactions specified"));
}
int parse_weight(params)
char *params;
{
int weight=1;
char *split;
if( (split=strrchr(params,' ')) )
{
*split='\0';
if ((weight=atoi(split+1))<=0)
{
fprintf(stderr,"Error: ignoring invalid weight '%s'\n",split+1);
weight=1;
}
}
return(weight);
}
void add_location(params,weight)
char *params;
int weight;
{
file_system *new_file_system;
if( (new_file_system=(file_system *)calloc(1,sizeof(file_system))) )
{
strcpy(new_file_system->system.name,params);
new_file_system->system.size=weight;
if (file_systems)
{
new_file_system->prev=file_systems->prev;
file_systems->prev->next=new_file_system;
file_systems->prev=new_file_system;
}
else
{
new_file_system->prev=new_file_system;
file_systems=new_file_system;
}
file_system_weight+=weight;
file_system_count++;
}
}
void delete_location(loc_name)
char *loc_name;
{
file_system *traverse;
for (traverse=file_systems; traverse; traverse=traverse->next)
if (!strcmp(traverse->system.name,loc_name))
{
file_system_weight-=traverse->system.size;
file_system_count--;
if (file_systems->prev==file_systems)
{
free(file_systems);
file_systems=NULL;
}
else
{
if (file_systems->prev==traverse)
file_systems->prev=traverse->prev;
if (traverse==file_systems)
file_systems=file_systems->next;
else
traverse->prev->next=traverse->next;
if (traverse->next)
traverse->next->prev=traverse->prev;
free(traverse);
}
break;
}
if (!traverse)
fprintf(stderr,"Error: cannot find location '%s'\n",loc_name);
}
void delete_locations()
{
file_system *next;
while (file_systems)
{
next=file_systems->next;
free(file_systems);
file_systems=next;
}
file_system_weight=0;
file_system_count=0;
}
/* UI callback for 'set location' - configure current working directory */
int cli_set_location(param)
char *param; /* remainder of command line */
{
if (param)
{
switch (*param)
{
case '+': /* add location to list */
add_location(param+1,parse_weight(param+1));
break;
case '-': /* remove location from list */
delete_location(param+1);
break;
default:
delete_locations();
add_location(param,parse_weight(param));
}
}
else
fprintf(stderr,"Error: no directory name specified\n");
return(1);
}
/* UI callback for 'set subdirectories' - configure number of subdirectories */
int cli_set_subdirs(param)
char *param; /* remainder of command line */
{
return(cli_generic_int(&subdirectories,param,
"invalid number of subdirectories"));
}
/* UI callback for 'set read' - configure read block size (integer) */
int cli_set_read(param)
char *param; /* remainder of command line */
{
return(cli_generic_int(&read_block_size,param,"invalid read block size"));
}
/* UI callback for 'set write' - configure write block size (integer) */
int cli_set_write(param)
char *param; /* remainder of command line */
{
return(cli_generic_int(&write_block_size,param,"invalid write block size"));
}
/* UI callback for 'set buffering' - sets buffering mode on or off
- true = buffered I/O (default), false = raw I/O */
int cli_set_buffering(param)
char *param; /* remainder of command line */
{
if (param && (!strcmp(param,"true") || !strcmp(param,"false")))
buffered_io=(!strcmp(param,"true"))?1:0;
else
fprintf(stderr,"Error: no buffering mode (true/false) specified\n");
return(1);
}
/* UI callback for 'set bias read' - sets probability of read vs. append */
int cli_set_bias_read(param)
char *param; /* remainder of command line */
{
int value;
if (param && (value=atoi(param))>=-1 && value<=10)
bias_read=value;
else
fprintf(stderr,
"Error: no bias specified (0-10 for greater chance,-1 to disable)\n");
return(1);
}
/* UI callback for 'set bias create' - sets probability of create vs. delete */
int cli_set_bias_create(param)
char *param; /* remainder of command line */
{
int value;
if (param && (value=atoi(param))>=-1 && value<=10)
bias_create=value;
else
fprintf(stderr,
"Error: no bias specified (0-10 for greater chance,-1 to disable)\n");
return(1);
}
/* UI callback for 'set report' - chooses verbose or terse report formats */
int cli_set_report(param)
char *param; /* remainder of command line */
{
int match=0;
if (param)
{
if (!strcmp(param,"verbose"))
report=0;
else
if (!strcmp(param,"terse"))
report=1;
else
match=-1;
}
if (!param || match==-1)
fprintf(stderr,"Error: either 'verbose' or 'terse' required\n");
return(1);
}
int cli_set_createonly(param)
char *param;
{
int match = 0;
if (param)
{
if (!strcmp(param,"0"))
createonly = 0;
else
if(!strcmp(param,"1"))
createonly=1;
else
match=-1;
}
if (!param || match==-1)
fprintf(stderr,"Error: either '0' or '1' required for createonly\n");
return(1);
}
/* populate file source buffer with 'size' bytes of readable randomness */
char *initialize_file_source(size)
int size; /* number of bytes of junk to create */
{
char *new_source;
int i;
if ((new_source=(char *)malloc(size))==NULL) /* allocate buffer */
fprintf(stderr,"Error: failed to allocate source file of size %d\n",size);
else
for (i=0; i<size; i++) /* file buffer with junk */
new_source[i]=32+RND(95);
return(new_source);
}
/* returns differences in times -
1 second is the minimum to avoid divide by zero errors */
time_t diff_time(t1,t0)
time_t t1;
time_t t0;
{
return((t1-=t0)?t1:1);
}
/* prints out results from running transactions */
void verbose_report(fp,end_time,start_time,t_end_time,t_start_time,deleted)
FILE *fp;
time_t end_time,start_time,t_end_time,t_start_time; /* timers from run */
int deleted; /* files deleted back-to-back */
{
time_t elapsed,t_elapsed;
int interval;
elapsed=diff_time(end_time,start_time);
t_elapsed=diff_time(t_end_time,t_start_time);
fprintf(fp,"Time:\n");
fprintf(fp,"\t%d seconds total\n", (int)elapsed);
fprintf(fp,"\t%d seconds of transactions (%d per second)\n", (int)t_elapsed,
(int)(transactions/t_elapsed));
fprintf(fp,"\nFiles:\n");
fprintf(fp,"\t%d created (%d per second)\n",files_created,
(int)(files_created/elapsed));
interval=diff_time(t_start_time,start_time);
fprintf(fp,"\t\tCreation alone: %d files (%d per second)\n",simultaneous,
simultaneous/interval);
fprintf(fp,"\t\tMixed with transactions: %d files (%d per second)\n",
files_created-simultaneous, (int)((files_created-simultaneous)/t_elapsed));
fprintf(fp,"\t%d read (%d per second)\n",files_read, (int)(files_read/t_elapsed));
fprintf(fp,"\t%d appended (%d per second)\n",files_appended,
(int)(files_appended/t_elapsed));
fprintf(fp,"\t%d deleted (%d per second)\n",files_created,
(int)(files_created/elapsed));
interval=diff_time(end_time,t_end_time);
if (createonly==0)
{
fprintf(fp,"\t\tDeletion alone: %d files (%d per second)\n",deleted,
deleted/interval);
fprintf(fp,"\t\tMixed with transactions: %d files (%d per second)\n",
files_deleted-deleted,(int)((files_deleted-deleted)/t_elapsed));
}
fprintf(fp,"\nData:\n");
fprintf(fp,"\t%s read ",scalef(bytes_read));
fprintf(fp,"(%s per second)\n",scalef(bytes_read/(float)elapsed));
fprintf(fp,"\t%s written ",scalef(bytes_written));
fprintf(fp,"(%s per second)\n",scalef(bytes_written/(float)elapsed));
}
void terse_report(fp,end_time,start_time,t_end_time,t_start_time,deleted)
FILE *fp;
time_t end_time,start_time,t_end_time,t_start_time; /* timers from run */
int deleted; /* files deleted back-to-back */
{
time_t elapsed,t_elapsed;
int interval;
elapsed=diff_time(end_time,start_time);
t_elapsed=diff_time(t_end_time,t_start_time);
interval=diff_time(t_start_time,start_time);
fprintf(fp,"%d %d %.2f ", (int)elapsed, (int)t_elapsed,
(float)transactions/t_elapsed);
fprintf(fp, "%.2f %.2f %.2f ", (float)files_created/elapsed,
(float)simultaneous/interval,
(float)(files_created-simultaneous)/t_elapsed);
fprintf(fp, "%.2f %.2f ", (float)files_read/t_elapsed,
(float)files_appended/t_elapsed);
fprintf(fp, "%.2f %.2f %.2f ", (float)files_created/elapsed,
(float)deleted/interval,
(float)(files_deleted-deleted)/t_elapsed);
fprintf(fp, "%.2f %.2f\n", (float)bytes_read/elapsed,
(float)bytes_written/elapsed);
}
/* returns file_table entry of unallocated file
- if not at end of table, then return next entry
- else search table for gaps */
int find_free_file()
{
int i;
if (file_allocated<simultaneous<<1 && file_table[file_allocated].size==0)
return(file_allocated++);
else /* search entire table for holes */
for (i=0; i<simultaneous<<1; i++)
if (file_table[i].size==0)
{
file_allocated=i;
return(file_allocated++);
}
return(-1); /* return -1 only if no free files found */
}
/* write 'size' bytes to file 'fd' using unbuffered I/O and close file */
void write_blocks(fd,size)
int fd;
int size; /* bytes to write to file */
{
int offset=0; /* offset into file */
int i;
/* write even blocks */
for (i=size; i>=write_block_size;
i-=write_block_size,offset+=write_block_size)
write(fd,file_source+offset,write_block_size);
write(fd,file_source+offset,i); /* write remainder */
bytes_written+=size; /* update counter */
close(fd);
}
/* write 'size' bytes to file 'fp' using buffered I/O and close file */
void fwrite_blocks(fp,size)
FILE *fp;
int size; /* bytes to write to file */
{
int offset=0; /* offset into file */
int i;
/* write even blocks */
for (i=size; i>=write_block_size;
i-=write_block_size,offset+=write_block_size)
fwrite(file_source+offset,write_block_size,1,fp);
fwrite(file_source+offset,i,1,fp); /* write remainder */
bytes_written+=size; /* update counter */
fclose(fp);
}
void create_file_name(dest)
char *dest;
{
char conversion[MAX_LINE+1];
*dest='\0';
if (file_system_count)
{
strcat(dest,
location_index[(file_system_count==1)?0:RND(file_system_weight)]);
strcat(dest,SEPARATOR);
}
if (subdirectories>1)
{
sprintf(conversion,"s%d%s",(int)(RND(subdirectories)),SEPARATOR);
strcat(dest,conversion);
}
sprintf(conversion,"%d",++files_created);
strcat(dest,conversion);
}
/* creates new file of specified length and fills it with data */
void create_file(buffered)
int buffered; /* 1=buffered I/O (default), 0=unbuffered I/O */
{
FILE *fp=NULL;
int fd=-1;
int free_file; /* file_table slot for new file */
if ((free_file=find_free_file())!=-1) /* if file space is available */
{ /* decide on name and initial length */
create_file_name(file_table[free_file].name);
file_table[free_file].size=
file_size_low+RND(file_size_high-file_size_low);
if (buffered)
fp=fopen(file_table[free_file].name,"w");
else
fd=open(file_table[free_file].name,O_RDWR|O_CREAT,0644);
if (fp || fd!=-1)
{
if (buffered)
fwrite_blocks(fp,file_table[free_file].size);
else
write_blocks(fd,file_table[free_file].size);
}
else
fprintf(stderr,"Error: cannot open '%s' for writing\n",
file_table[free_file].name);
}
}
/* deletes specified file from disk and file_table */
void delete_file(number)
int number;
{
if (file_table[number].size)
{
if (remove(file_table[number].name))
fprintf(stderr,"Error: Cannot delete '%s'\n",file_table[number].name);
else
{ /* reset entry in file_table and update counter */
file_table[number].size=0;
files_deleted++;
}
}
}
/* reads entire specified file into temporary buffer */
void read_file(number,buffered)
int number; /* number of file to read (from file_table) */
int buffered; /* 1=buffered I/O (default), 0=unbuffered I/O */
{
FILE *fp=NULL;
int fd=-1;
int i;
if (buffered)
fp=fopen(file_table[number].name,"r");
else
fd=open(file_table[number].name,O_RDONLY,0644);
if (fp || fd!=-1)
{ /* read as many blocks as possible then read the remainder */
if (buffered)
{
for (i=file_table[number].size; i>=read_block_size; i-=read_block_size)
fread(read_buffer,read_block_size,1,fp);
fread(read_buffer,i,1,fp);
fclose(fp);
}
else
{
for (i=file_table[number].size; i>=read_block_size; i-=read_block_size)
read(fd,read_buffer,read_block_size);
read(fd,read_buffer,i);
close(fd);
}
/* increment counters to record transaction */
bytes_read+=file_table[number].size;
files_read++;
}
else
fprintf(stderr,"Error: cannot open '%s' for reading\n",
file_table[number].name);
}
/* appends random data to a chosen file up to the maximum configured length */
void append_file(number,buffered)
int number; /* number of file (from file_table) to append date to */
int buffered; /* 1=buffered I/O (default), 0=unbuffered I/O */
{
FILE *fp=NULL;
int fd=-1;
int block; /* size of data to append */
if (file_table[number].size<file_size_high)
{
if (buffered)
fp=fopen(file_table[number].name,"a");
else
fd=open(file_table[number].name,O_RDWR|O_APPEND,0644);
if ((fp || fd!=-1) && file_table[number].size<file_size_high)
{
block=RND(file_size_high-file_table[number].size)+1;
if (buffered)
fwrite_blocks(fp,block);
else
write_blocks(fd,block);
file_table[number].size+=block;
files_appended++;
}
else
fprintf(stderr,"Error: cannot open '%s' for append\n",
file_table[number].name);
}
}
/* finds and returns the offset of a file that is in use from the file_table */
int find_used_file() /* only called after files are created */
{
int used_file;
while (file_table[used_file=RND(simultaneous<<1)].size==0)
;
return(used_file);
}
/* reset global counters - done before each test run */
void reset_counters()
{
files_created=0;
files_deleted=0;
files_read=0;
files_appended=0;
bytes_written=0;
bytes_read=0;
}
/* perform the configured number of file transactions
- a transaction consisted of either a read or append and either a
create or delete all chosen at random */
int run_transactions(buffered)
int buffered; /* 1=buffered I/O (default), 0=unbuffered I/O */
{
int percent; /* one tenth of the specified transactions */
int i;
percent=transactions/10;
for (i=0; i<transactions; i++)
{
if (files_created==files_deleted)
{
printf("out of files!\n");
printf("For this workload, either increase the number of files or\n");
printf("decrease the number of transactions.\n");
break;
}
if (bias_read!=-1) /* if read/append not locked out... */
{
if (RND(10)<bias_read) /* read file */
read_file(find_used_file(),buffered);
else /* append file */
append_file(find_used_file(),buffered);
}
if (bias_create!=-1) /* if create/delete not locked out... */
{
if (RND(10)<bias_create) /* create file */
create_file(buffered);
else /* delete file */
delete_file(find_used_file());
}
if ((i % percent)==0) /* if another tenth of the work is done...*/
{
putchar('.'); /* print progress indicator */
fflush(stdout);
}
}
return(transactions-i);
}
char **build_location_index(list,weight)
file_system *list;
int weight;
{
char **index;
int count;
int i=0;
if ((index=(char **)calloc(1,weight*sizeof(char *)))==NULL)
fprintf(stderr,"Error: cannot build weighted index of locations\n");
else
for (; list; list=list->next)
for (count=0; count<list->system.size; count++)
index[i++]=list->system.name;
return(index);
}
void create_subdirectories(dir_list,base_dir,subdirs)
file_system *dir_list;
char *base_dir;
int subdirs;
{
char dir_name[MAX_LINE+1]; /* buffer holding subdirectory names */
char save_dir[MAX_LINE+1];
int i;
if (dir_list)
{
for (; dir_list; dir_list=dir_list->next)
create_subdirectories(NULL,dir_list->system.name,subdirs);
}
else
{
if (base_dir)
sprintf(save_dir,"%s%s",base_dir,SEPARATOR);
else
*save_dir='\0';
for (i=0; i<subdirs; i++)
{
sprintf(dir_name,"%ss%d",save_dir,i);
MKDIR(dir_name);
}
}
}
void delete_subdirectories(dir_list,base_dir,subdirs)
file_system *dir_list;
char *base_dir;
int subdirs;
{
char dir_name[MAX_LINE+1]; /* buffer holding subdirectory names */
char save_dir[MAX_LINE+1];
int i;
if (dir_list)
{
for (; dir_list; dir_list=dir_list->next)
delete_subdirectories(NULL,dir_list->system.name,subdirs);
}
else
{
if (base_dir)
sprintf(save_dir,"%s%s",base_dir,SEPARATOR);
else
*save_dir='\0';
for (i=0; i<subdirs; i++)
{
sprintf(dir_name,"%ss%d",save_dir,i);
rmdir(dir_name);
}
}
}
/* CLI callback for 'run' - benchmark execution loop */
int cli_run(param) /* none */
char *param; /* unused */
{
time_t start_time,t_start_time,t_end_time,end_time; /* elapsed timers */
int delete_base; /* snapshot of deleted files counter */
FILE *fp=NULL; /* file descriptor for directing output */
int incomplete;
int i; /* generic iterator */
reset_counters(); /* reset counters before each run */
sgenrand(seed); /* initialize random number generator */
/* allocate file space and fill with junk */
file_source=initialize_file_source(file_size_high<<1);
/* allocate read buffer */
read_buffer=(char *)malloc(read_block_size);
/* allocate table of files at 2 x simultaneous files */
file_allocated=0;
if ((file_table=(file_entry *)calloc(simultaneous<<1,sizeof(file_entry)))==
NULL)
fprintf(stderr,"Error: Failed to allocate table for %d files\n",