-
Notifications
You must be signed in to change notification settings - Fork 15
/
dtprocs.c
938 lines (862 loc) · 29.1 KB
/
dtprocs.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
/****************************************************************************
* *
* COPYRIGHT (c) 1988 - 2021 *
* This Software Provided *
* By *
* Robin's Nest Software Inc. *
* *
* Permission to use, copy, modify, distribute and sell this software and *
* its documentation for any purpose and without fee is hereby granted, *
* provided that the above copyright notice appear in all copies and that *
* both that copyright notice and this permission notice appear in the *
* supporting documentation, and that the name of the author not be used *
* in advertising or publicity pertaining to distribution of the software *
* without specific, written prior permission. *
* *
* THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, *
* INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN *
* NO EVENT SHALL HE BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL *
* DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR *
* PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS *
* ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF *
* THIS SOFTWARE. *
* *
****************************************************************************/
/*
* Module: dtprocs.c
* Author: Robin T. Miller
* Date: August 7, 1993
*
* Description:
* Functions to handle multiple processes for 'dt' program.
*
* Modification History:
*
* September 5th, 2020 by Robin T. Miller
* When initializing a slice, ensure the min/max limit sizes do NOT
* exceed the data/slice limits. Otherwise, overwrites occur and thus cause
* false corruptions!
*
* March 7th, 2015 by Robin T. Miller
* When using slices, always set the random data limit, since variable
* options such as iodir=vary and/or iotype=vary may be specified, plus it
* does no harm to set this if random I/O options are not specified.
*
* July 16th, 2014 by Robin T. Miller
* In setup_slice(), when the step option is specified, setup the end
* position, which I/O functions use to avoid overwriting the slice length.
* Since code has been rearranged, the value setup in do_common_device_setup()
* is incorrect, since its' called before init_slice. Sadly, this means folks
* using the step option with slices could encounter a false data corruption,
* since we were writing past the end of the slice! ;( (oh my, dt v19 bug!)
*
* June 20th, 2013 by Robin T Miller
* Mostly a rewrite for multithreaded IO, so starting with new history!
*/
#include "dt.h"
#if !defined(WIN32)
# include <signal.h>
# include <strings.h>
# include <sys/stat.h>
# include <sys/wait.h>
#endif /* !defined(WIN32) */
#define PROC_ALLOC (sizeof(pid_t) * 3) /* Extra allocation for PID. */
/*
* Forward References:
*/
static int init_slice_info(struct dinfo *dip, slice_info_t *sip, large_t *data_resid);
static void setup_slice(struct dinfo *dip, slice_info_t *sip);
static void setup_iotuning(dinfo_t *dip, char *cmd);
static void setup_multiprocs(dinfo_t *dip, char *cmd);
static void update_cmd_line(dinfo_t *dip);
#if defined(WIN32)
#define HANDLE_MASK (MAXIMUM_WAIT_OBJECTS - 1)
PROCESS_INFORMATION *start_process(dinfo_t *dip, char *cmd);
/*
* abort_procs - Abort processes started by the parent.
*/
void
abort_procs(dinfo_t *dip)
{
int proc_num;
struct dt_procs *dtp;
PROCESS_INFORMATION *pip;
if ((dip->di_ptable == NULL) || dip->di_aborted_processes) return;
/*
* Terminate all active processes.
*/
for (dtp = dip->di_ptable, proc_num = 0; proc_num < dip->di_max_procs; proc_num++, dtp++) {
pip = dtp->dt_pip;
if ( !dtp->dt_active ) continue; /* Already reaped status! */
if (dip->di_debug_flag || dip->di_pDebugFlag) {
Printf(dip, "Terminating child process %d...\n", pip->dwProcessId);
}
/*
* I'd rather send a signal like Unix, to have child report
* statistics, but Windows does not support a kill() API!
*/
if (TerminateProcess(pip->hProcess, ERROR_PROCESS_ABORTED) == 0) {
DWORD error = GetLastError();
/*
* Child thread may be terminating due to signal.
*/
if (error != ERROR_ACCESS_DENIED) {
Fprintf(dip, "Failed to terminate child process %d...\n", pip->dwProcessId);
ReportErrorInfo(dip, NULL, error, "TerminateProcess() failed", OTHER_OP, True);
}
}
}
dip->di_aborted_processes = True;
return;
}
void
await_procs(dinfo_t *dip)
{
DWORD status;
struct dt_procs *dtp;
int proc_num = 0, wait_index;
PROCESS_INFORMATION *pip;
bool aborted_procs = False;
DWORD timeoutMs = INFINITE;
if (dip->di_debug_flag || dip->di_pDebugFlag) {
timeoutMs = (60 * 1000); /* For debug! */
Printf(dip, "Waiting for %d child processes to complete...\n", dip->di_procs_active);
}
/*
* For dt's oncerror option, we must wait on one process at a time,
* so we can abort procs if instructed to on child errors.
*/
while (dip->di_procs_active) {
/*
* Wait for all processes to complete.
*/
status = WaitForMultipleObjects((DWORD)dip->di_procs_active, // Number of handles.
dip->di_proc_handles,// Array of handles.
False, // Wait for any object.
timeoutMs); // Timeout in ms (disable).
if (status == WAIT_FAILED) {
ReportErrorInfo(dip, NULL, os_get_error(), "WaitForMultipleObjects() failed", OTHER_OP, False);
abort_procs(dip);
break;
}
if (status == WAIT_TIMEOUT) {
Printf(dip, "Still waiting on %d processes...\n", dip->di_procs_active);
Printf(dip, "Active PIDs:");
for (dtp = dip->di_ptable, proc_num = 0; proc_num < dip->di_max_procs; proc_num++, dtp++) {
pip = dtp->dt_pip;
if (dtp->dt_active) {
Print(dip, " %d", pip->dwProcessId);
}
}
Print(dip, "\n");
fflush(ofp);
continue;
}
if ((status & ~HANDLE_MASK) != WAIT_OBJECT_0) {
Fprintf(dip, "WaitForMultipleObjects returned status %#x\n", status);
dip->di_child_status = GetLastError();
abort_procs(dip);
return;
}
wait_index = (status & HANDLE_MASK); /* Handle index - 1 */
/*
* Find the process by searching for its' handle.
*/
for (dtp = dip->di_ptable, proc_num = 0; proc_num < dip->di_max_procs; proc_num++, dtp++) {
pip = dtp->dt_pip;
if (pip->hProcess == dip->di_proc_handles[wait_index]) {
break;
}
}
if ( !GetExitCodeProcess( pip->hProcess, &dip->di_child_status ) ) {
Fprintf(dip, "GetExitCodeProcess failed (%d)\n", GetLastError() );
dip->di_child_status = GetLastError();
}
if (dip->di_debug_flag || dip->di_pDebugFlag) {
Printf(dip, "Child process %d, exited with status %d\n",
pip->dwProcessId, dip->di_child_status);
}
dtp->dt_active = False;
dtp->dt_status = dip->di_child_status;
/*
* Close process and thread handles.
*/
CloseHandle( pip->hProcess );
CloseHandle( pip->hThread );
/*
* Remove the completed handle from the array to wait upon.
*/
for (; (wait_index < dip->di_procs_active); wait_index++) {
dip->di_proc_handles[wait_index] = dip->di_proc_handles[wait_index + 1];
}
if ( (exit_status == SUCCESS) && (dip->di_child_status != SUCCESS) ) {
if ( (dip->di_oncerr_action == ONERR_ABORT) &&
(dip->di_child_status != WARNING) && (dip->di_child_status != END_OF_FILE) ) {
if (!aborted_procs) {
abort_procs(dip); /* Abort procs on error. */
aborted_procs = True;
}
}
/*
* Save the most sever error for parent exit status.
*
* Severity Priorities:
* WARNING (lowest)
* END_OF_FILE
* Signal Number
* FATAL_ERROR (highest)
*/
if ( ((exit_status == SUCCESS) || (dip->di_child_status == FATAL_ERROR)) ||
((exit_status == WARNING) && (dip->di_child_status > WARNING)) ||
((exit_status == END_OF_FILE) && (dip->di_child_status > WARNING)) ) {
exit_status = dip->di_child_status; /* Set error code for exit. */
}
}
dip->di_procs_active--;
}
return;
}
/*
* start_process() - Start a Process.
*
* Inputs:
* dip = The device information pointer.
* cmd = The command line to execute.
*
* Return Value:
* Returns pointer to process information or NULL on failure.
*/
PROCESS_INFORMATION *
start_process(dinfo_t *dip, char *cmd)
{
STARTUPINFO si;
PROCESS_INFORMATION *pip;
ZeroMemory( &si, sizeof(si) );
si.cb = sizeof(si);
pip = (PROCESS_INFORMATION *)Malloc(dip, sizeof(*pip));
if (pip == NULL) return(pip);
ZeroMemory( pip, sizeof(*pip) );
if (dip->di_debug_flag || dip->di_pDebugFlag) {
Printf(dip, "Command: %s\n", cmd);
}
/* Start the child process. */
if ( !CreateProcess( NULL, // No module name (use command line)
cmd, // Command line
NULL, // Process handle not inheritable
NULL, // Thread handle not inheritable
False, // Handle inheritance flag
0, // No creation flags
NULL, // Use parent's environment block
NULL, // Use parent's starting directory
&si, // Pointer to STARTUPINFO structure
pip ) ) // Pointer to PROCESS_INFORMATION structure
{
ReportErrorInfo(dip, NULL, os_get_error(), "CreateProcess failed", OTHER_OP, True);
Free(dip, pip);
return(NULL);
}
return(pip);
}
static void
setup_iotuning(dinfo_t *dip, char *cmd)
{
if (dip->di_iotuning_flag && dip->di_iotune_file ) {
//(void)strcat(cmd, " disable=iotuning");
;
}
return;
}
static void
setup_multiprocs(dinfo_t *dip, char *cmd)
{
return;
}
pid_t
start_devs(dinfo_t *dip)
{
struct dt_procs *dtp;
size_t psize;
PROCESS_INFORMATION *pip;
char *devs, *token, *p;
int proc_num = 0;
char *dt_path, *dt_cmd;
char *our_cmd, *bp, *saveptr;
devs = p = (dip->di_input_file) ? dip->di_input_file : dip->di_output_file;
dip->di_num_devs = 1;
/* Count the devices specified. */
while (p = strchr(p, ',')) {
dip->di_num_devs++; p++;
}
dip->di_max_procs = dip->di_num_devs;
psize = (dip->di_max_procs * sizeof(*dtp));
if ((dip->di_ptable = (struct dt_procs *)Malloc(dip, psize)) == NULL) {
ReportErrorInfo(dip, NULL, os_get_error(), "No memory for proc table", OTHER_OP, False);
exit (FATAL_ERROR);
}
ZeroMemory(dip->di_ptable, psize);
our_cmd = (char *)Malloc(dip, LOG_BUFSIZE);
/* Setup pointers to dt path and dt command line. */
dt_path = dip->di_dtcmd;
p = strchr(dt_path, ' ');
if (!p) {
Fprintf(dip, "We're broken setting up dt's path!\n");
abort();
}
*p = '\0';
dt_cmd = ++p;
dip->di_cur_proc = 1;
dip->di_procs_active = 0;
/* Remember: strtok() replaces "," with NULL! */
token = strtok_r(devs, ",", &saveptr);
/*
* Start a process for each target device specified.
*/
for (dtp = dip->di_ptable, proc_num = 0; proc_num < dip->di_max_procs; proc_num++, dtp++) {
dtp->dt_device = token;
bp = our_cmd;
/* Note: Main stripped the device options already. */
bp += Sprintf(bp, "%s ", dt_path);
/* More work, but I like the device first on the command line! */
if (dip->di_input_file) {
bp += Sprintf(bp, "if=%s", token);
} else {
bp += Sprintf(bp, "of=%s", token);
}
/* Now, add original options. */
bp += Sprintf(bp, " %s enable=child", dt_cmd);
setup_iotuning(dip, bp);
pip = start_process(dip, our_cmd);
if (pip == NULL) return (-1);
if (dip->di_debug_flag || dip->di_pDebugFlag) {
Printf(dip, "Started process %d for %s...\n", pip->dwProcessId, dtp->dt_device);
}
dip->di_proc_handles[proc_num] = pip->hProcess;
dip->di_cur_proc++;
dtp->dt_pid = dip->di_child_pid = pip->dwProcessId;
dtp->dt_active = True;
dtp->dt_pip = pip;
dip->di_procs_active++;
token = strtok_r(NULL, ",", &saveptr); /* Next device please! */
}
Free(dip, our_cmd);
return (dip->di_child_pid);
}
pid_t
start_procs(dinfo_t *dip)
{
struct dt_procs *dtp;
size_t psize;
PROCESS_INFORMATION *pip;
int proc_num = 0;
dip->di_max_procs = dip->di_num_procs;
psize = (dip->di_max_procs * sizeof(*dtp));
if ((dip->di_ptable = (struct dt_procs *)Malloc(dip, psize)) == NULL) {
ReportErrorInfo (dip, NULL, os_get_error(), "No memory for proc table", OTHER_OP, False);
exit (FATAL_ERROR);
}
ZeroMemory(dip->di_ptable, psize);
dip->di_cur_proc = 1;
dip->di_procs_active = 0;
(void)strcat(dip->di_dtcmd, " enable=child");
setup_iotuning(dip, dip->di_dtcmd);
setup_multiprocs(dip, dip->di_dtcmd);
/*
* Spawn specified number of processes.
*/
for (dtp = dip->di_ptable, proc_num = 0; proc_num < dip->di_max_procs; proc_num++, dtp++) {
pip = start_process(dip, dip->di_dtcmd);
if (pip == NULL) return (-1);
if (dip->di_debug_flag || dip->di_pDebugFlag) {
Printf(dip, "Started process %d...\n", pip->dwProcessId);
}
dip->di_proc_handles[proc_num] = pip->hProcess;
dip->di_cur_proc++;
dtp->dt_pid = dip->di_child_pid = pip->dwProcessId;
dtp->dt_active = True;
dtp->dt_pip = pip;
dip->di_procs_active++;
}
return (dip->di_child_pid);
}
pid_t
start_slices(dinfo_t *dip)
{
struct slice_info slice_info;
slice_info_t *sip = &slice_info;
struct dt_procs *dtp;
size_t psize;
PROCESS_INFORMATION *pip;
int proc_num = 0;
large_t data_resid;
char *cmd = Malloc(dip, LOG_BUFSIZE);
dip->di_max_procs = dip->di_slices;
psize = (dip->di_max_procs * sizeof(*dtp));
if ((dip->di_ptable = (struct dt_procs *)Malloc(dip, psize)) == NULL) {
ReportErrorInfo (dip, NULL, os_get_error(), "No memory for proc table", OTHER_OP, False);
exit (FATAL_ERROR);
}
memset(dip->di_ptable, '\0', psize);
init_slice_info(dip, sip, &data_resid);
dip->di_cur_proc = 1;
dip->di_procs_active = 0;
/*
* Spawn specified number of processes.
*/
for (dtp = dip->di_ptable, proc_num = 0; proc_num < dip->di_max_procs; proc_num++, dtp++) {
sip->slice++;
/* Last slice gets any residual bytes. */
if ((proc_num + 1) == dip->di_max_procs) {
sip->slice_length += data_resid;
}
(void)sprintf(cmd, "%s enable=logpid slice=%d", dip->di_dtcmd, sip->slice);
setup_iotuning(dip, cmd);
setup_multiprocs(dip, cmd);
pip = start_process(dip, cmd);
if (pip == NULL) return (-1);
dip->di_proc_handles[proc_num] = pip->hProcess;
dip->di_cur_proc++;
dtp->dt_pid = dip->di_child_pid = pip->dwProcessId;
dtp->dt_active = True;
dtp->dt_pip = pip;
dip->di_procs_active++;
if (dip->di_debug_flag || dip->di_pDebugFlag) {
Printf (dip, "Started Slice %d, PID %d...\n", sip->slice, dip->di_child_pid);
}
if (proc_num < dip->di_max_procs) {
sip->slice_position += sip->slice_length;
}
}
return (dip->di_child_pid);
}
#else /* !defined(WIN32) */
/*
* abort_procs - Abort processes started by the parent.
*/
void
abort_procs(dinfo_t *dip)
{
struct dt_procs *dtp;
int procs;
pid_t pid;
if ((dip->di_ptable == NULL) || dip->di_aborted_processes) return;
/*
* Force all processes to terminate.
*/
for (dtp = dip->di_ptable, procs=0; procs < dip->di_max_procs; procs++, dtp++) {
if ((pid = dtp->dt_pid) == (pid_t) 0) continue;
if (dip->di_debug_flag || dip->di_pDebugFlag) {
Printf(dip, "Aborting child process %d via a SIGINT (signal %d)...\n",
pid, SIGINT);
}
if (dtp->dt_active) {
int status = kill (pid, SIGINT);
if ( (dip->di_debug_flag || dip->di_pDebugFlag) && (status == FAILURE) ) {
Perror(dip, "DEBUG: Failed to kill PID %d", pid);
}
}
}
dip->di_aborted_processes = True;
return;
}
void
await_procs(dinfo_t *dip)
{
pid_t wpid;
struct dt_procs *dtp;
int procs, status;
if (dip->di_debug_flag || dip->di_pDebugFlag) {
Printf (dip, "Waiting for %d child processes to complete...\n", dip->di_procs_active);
}
while (1) {
if ((wpid = waitpid ((pid_t) -1, &dip->di_child_status, 0)) == FAILURE) {
if (errno == ECHILD) {
if (dip->di_procs_active && dip->di_pDebugFlag) {
Printf(dip, "Processes still active (%d) and ECHILD received!\n",
dip->di_procs_active);
}
break; /* No more children... */
} else if (errno == EINTR) {
/* Note: Expect signal handler to abort children! */
//abort_procs(dip);
continue;
} else {
ReportErrorInfo (dip, NULL, os_get_error(), "waitpid", OTHER_OP, False);
exit (FATAL_ERROR);
}
}
/*
* Examine the child process status.
*/
if ( WIFSTOPPED(dip->di_child_status) ) {
Printf (dip, "Child process %d, stopped by signal %d.\n",
wpid, WSTOPSIG(dip->di_child_status));
continue; /* Maybe attached from debugger... */
} else if ( WIFSIGNALED(dip->di_child_status) ) {
status = WTERMSIG(dip->di_child_status);
Fprintf (dip, "Child process %d, exiting because of signal %d\n",
wpid, status);
} else { /* Process must be exiting... */
status = WEXITSTATUS (dip->di_child_status);
if (dip->di_debug_flag || dip->di_pDebugFlag) {
Printf (dip, "Child process %d, exited with status %d\n",
wpid, status);
}
}
/*
* Update this process' status.
*/
for (dtp = dip->di_ptable, procs = 0; procs < dip->di_max_procs; procs++, dtp++) {
if (dtp->dt_pid == wpid) {
dtp->dt_active = False;
dtp->dt_status = status;
dip->di_procs_active--;
break;
}
}
if ( (exit_status == SUCCESS) && (status != SUCCESS) ) {
if ( (dip->di_oncerr_action == ONERR_ABORT) &&
(status != WARNING) && (status != END_OF_FILE) ) {
abort_procs(dip); /* Abort procs on error. */
}
/*
* Save the most sever error for parent exit status.
*
* Severity Priorities: WARNING (lowest)
* END_OF_FILE
* Signal Number
* FATAL_ERROR (highest)
*/
if ( ((exit_status == SUCCESS) || (status == FATAL_ERROR)) ||
((exit_status == WARNING) && (status > WARNING)) ||
((exit_status == END_OF_FILE) && (status > WARNING)) ) {
exit_status = status; /* Set error code for exit. */
}
}
} /* End of while(1)... */
}
pid_t
fork_process(dinfo_t *dip)
{
pid_t pid;
if ((pid = fork()) == (pid_t) -1) {
if (errno == EAGAIN) {
if (dip->di_procs_active == 0) {
LogMsg (dip, dip->di_efp, logLevelCrit, 0,
"could NOT start any processes, please check your system...\n");
exit (FATAL_ERROR);
} else {
Printf (dip,
"Warning: System imposed process limit reached, only %d procs started...\n",
dip->di_procs_active);
}
} else {
ReportErrorInfo (dip, NULL, os_get_error(), "fork", OTHER_OP, False);
abort_procs(dip);
}
}
return (pid);
}
static void
update_cmd_line(dinfo_t *dip)
{
char *device, *bp, *p;
char *dt_path, *options, *buffer;
bp = buffer = (char *)Malloc(dip, LOG_BUFSIZE);
device = (dip->di_input_file) ? dip->di_input_file : dip->di_output_file;
/* Setup pointers to dt path and dt command line. */
dt_path = dip->di_dtcmd;
p = strchr(dt_path, ' ');
*p = '\0';
options = ++p;
/* Note: Main stripped the device options already. */
bp += Sprintf(bp, "%s ", dt_path);
if (dip->di_input_file) {
bp += Sprintf(bp, "if=%s", device);
} else {
bp += Sprintf(bp, "of=%s", device);
}
/* Now, add original options. */
bp += Sprintf(bp, " %s", options);
if (dip->di_debug_flag || dip->di_pDebugFlag) {
Printf(dip, "Command: %s\n", buffer);
}
if (dip->di_cmd_line) Free(dip, dip->di_cmd_line);
dip->di_cmd_line = Malloc(dip, (strlen(buffer) + 1) );
strcpy(dip->di_cmd_line, buffer);
Free(dip, buffer);
return;
}
static void
setup_iotuning(dinfo_t *dip, char *cmd)
{
return;
}
static void
setup_multiprocs(dinfo_t *dip, char *cmd)
{
return;
}
pid_t
start_devs(dinfo_t *dip)
{
struct dt_procs *dtp;
size_t psize;
char *devs, *token, *p, *saveptr;
int procs;
devs = p = (dip->di_input_file) ? dip->di_input_file : dip->di_output_file;
dip->di_num_devs = 1;
/* Count the devices specified. */
while (p = strchr(p, ',')) {
dip->di_num_devs++; p++;
}
dip->di_max_procs = dip->di_num_devs;
psize = (dip->di_max_procs * sizeof(*dtp));
if ((dip->di_ptable = (struct dt_procs *)Malloc(dip, psize)) == NULL) {
ReportErrorInfo(dip, NULL, os_get_error(), "No memory for proc table", OTHER_OP, False);
exit (FATAL_ERROR);
}
memset(dip->di_ptable, 0, psize);
dip->di_cur_proc = 1;
dip->di_procs_active = 0;
/* Remember: strtok() replaces "," with NULL! */
token = strtok_r(devs, ",", &saveptr);
/*
* Start a process for each target device specified.
*/
for (dtp = dip->di_ptable, procs = 0; procs < dip->di_max_procs; procs++, dtp++) {
dtp->dt_device = token;
if ((dip->di_child_pid = fork_process(dip)) == (pid_t) -1) {
break;
} else if (dip->di_child_pid) { /* Parent process gets the PID. */
dip->di_cur_proc++;
dtp->dt_pid = dip->di_child_pid;
dtp->dt_active = True;
dip->di_procs_active++;
if (dip->di_debug_flag || dip->di_pDebugFlag) {
Printf (dip, "Started Process %d for %s...\n", dip->di_child_pid, dtp->dt_device);
}
token = strtok_r(NULL, ",", &saveptr); /* Next device please! */
} else { /* Child process... */
dip->di_logpid_flag = True;
dip->di_multiple_devs = False;
if (dip->di_input_file) {
dip->di_input_file = dtp->dt_device;
} else {
dip->di_output_file = dtp->dt_device;
}
dip->di_process_id = getpid();
if (dip->di_log_format) {
dip->di_log_file = dip->di_log_format; /* Original log file w/format strings. */
}
setup_iotuning(dip, NULL); /* Setup IO tuning options. */
update_cmd_line(dip); /* Update the command line (for logging). */
make_unique_log(dip); /* Check & make a unique log. */
break; /* Child process, continue... */
}
}
return (dip->di_child_pid);
}
pid_t
start_procs(dinfo_t *dip)
{
struct dt_procs *dtp;
size_t psize;
int procs;
dip->di_max_procs = dip->di_num_procs;
psize = (dip->di_max_procs * sizeof(*dtp));
if ((dip->di_ptable = (struct dt_procs *)Malloc(dip, psize)) == NULL) {
ReportErrorInfo (dip, NULL, os_get_error(), "No memory for proc table", OTHER_OP, False);
exit (FATAL_ERROR);
}
memset(dip->di_ptable, '\0', psize);
dip->di_cur_proc = 1;
dip->di_procs_active = 0;
for (dtp = dip->di_ptable, procs = 0; procs < dip->di_max_procs; procs++, dtp++) {
if ((dip->di_child_pid = fork_process(dip)) == (pid_t) -1) {
break;
} else if (dip->di_child_pid) { /* Parent process gets the PID. */
dip->di_cur_proc++;
dtp->dt_pid = dip->di_child_pid;
dtp->dt_active = True;
dip->di_procs_active++;
if (dip->di_debug_flag || dip->di_pDebugFlag) {
Printf (dip, "Started Process %d...\n", dip->di_child_pid);
}
} else { /* Child process... */
dip->di_process_id = getpid();
if (dip->di_output_file) {
dip->di_unique_file = True; /* Create unique file name. */
}
if (dip->di_log_format) {
dip->di_log_file = dip->di_log_format; /* Original log file w/format strings. */
}
setup_iotuning(dip, NULL); /* Setup IO tuning options. */
setup_multiprocs(dip, NULL);/* Setup multiple process options. */
make_unique_log(dip); /* Check & make a unique log. */
break; /* Child process, continue... */
}
}
return (dip->di_child_pid);
}
pid_t
start_slices(dinfo_t *dip)
{
struct dt_procs *dtp;
size_t psize;
struct slice_info slice_info;
slice_info_t *sip = &slice_info;
large_t data_resid;
int procs;
dip->di_max_procs = dip->di_slices;
psize = (dip->di_max_procs * sizeof(*dtp));
if ((dip->di_ptable = (struct dt_procs *)Malloc(dip, psize)) == NULL) {
ReportErrorInfo(dip, NULL, os_get_error(), "No memory for proc table", OTHER_OP, False);
exit (FATAL_ERROR);
}
memset(dip->di_ptable, '\0', psize);
init_slice_info(dip, sip, &data_resid);
dip->di_cur_proc = 1;
dip->di_procs_active = 0;
for (dtp = dip->di_ptable, procs = 0; procs < dip->di_max_procs; procs++, dtp++) {
sip->slice++;
/* Last slice gets any residual bytes. */
if ((procs + 1) == dip->di_max_procs) {
sip->slice_length += data_resid;
}
if ((dip->di_child_pid = fork_process(dip)) == (pid_t) -1) {
break;
} else if (dip->di_child_pid) { /* Parent process gets the PID. */
dip->di_cur_proc++;
dtp->dt_pid = dip->di_child_pid;
dtp->dt_active = True;
dip->di_procs_active++;
if (dip->di_debug_flag || dip->di_pDebugFlag) {
Printf (dip, "Started Slice %d, PID %d...\n", sip->slice, dip->di_child_pid);
}
if (procs < dip->di_max_procs) {
sip->slice_position += sip->slice_length;
}
} else { /* Child process... */
/*
* Initialize the starting data pattern for each slice.
*/
dip->di_process_id = getpid();
if (dip->di_unique_pattern) {
dip->di_pattern = data_patterns[(dip->di_cur_proc - 1) % npatterns];
}
if (dip->di_log_format) {
dip->di_log_file = dip->di_log_format; /* Original log file w/format strings. */
}
setup_iotuning(dip, NULL); /* Setup IO tuning options. */
setup_multiprocs(dip, NULL);/* Setup multiple process options. */
make_unique_log(dip); /* Check & make a unique log. */
setup_slice(dip, sip);
break; /* Child process, continue... */
}
}
return (dip->di_child_pid);
}
#endif /* !defined(WIN32) */
int
init_slice(struct dinfo *dip, int slice)
{
struct slice_info slice_info;
slice_info_t *sip = &slice_info;
large_t data_resid;
int status;
status = init_slice_info(dip, sip, &data_resid);
if (status == FAILURE) return(status);
sip->slice_position += (sip->slice_length * (slice - 1));
/*
* Any residual goes to the last slice.
*/
if (slice == dip->di_slices) {
sip->slice_length += data_resid;
}
sip->slice = slice;
setup_slice(dip, sip);
/*
* Initialize the starting data pattern for each slice.
*
* TODO: Verify, but this is done in main I/O loop (I think).
*/
if (dip->di_unique_pattern) {
dip->di_pattern = data_patterns[(slice - 1) % npatterns];
}
return(status);
}
static int
init_slice_info(struct dinfo *dip, slice_info_t *sip, large_t *data_resid)
{
/* Note: The data limit was adjusted by file position by FindCapacity(). */
large_t data_limit = dip->di_data_limit;
//large_t data_limit = (dip->di_data_limit - dip->di_file_position);
large_t slice_length;
int status = SUCCESS;
sip->slice = 0;
sip->slice_position = dip->di_file_position;
slice_length = (data_limit / dip->di_slices);
sip->slice_length = rounddown(slice_length, dip->di_dsize);
if (sip->slice_length < (large_t)dip->di_dsize) {
LogMsg (dip, dip->di_efp, logLevelCrit, 0,
"Slice length of " LUF " bytes is smaller than device size of %u bytes!\n",
sip->slice_length, dip->di_dsize);
status = FAILURE;
} else {
*data_resid = (data_limit - (sip->slice_length * dip->di_slices));
*data_resid = rounddown(*data_resid, dip->di_dsize);
}
return(status);
}
static void
setup_slice(struct dinfo *dip, slice_info_t *sip)
{
dip->di_file_position = sip->slice_position;
/* Variable options may be set, so always set the random data limit! */
/* Note: This limit is really the max random data offset, see usage! */
/* This random data value is used for random I/O and reverse I/O. */
dip->di_rdata_limit = (dip->di_file_position + sip->slice_length);
/*
* Restrict data limit to slice length or user set limit.
*/
dip->di_data_limit = MIN(dip->di_data_limit, sip->slice_length);
/* BEWARE: These override the data limit, when specified! */
if (dip->di_min_limit > dip->di_data_limit) {
dip->di_min_limit = dip->di_data_limit;
}
if (dip->di_max_limit > dip->di_data_limit) {
dip->di_max_limit = dip->di_data_limit;
}
if (dip->di_step_offset) {
dip->di_end_position = (dip->di_file_position + dip->di_data_limit);
}
if (dip->di_debug_flag || dip->di_Debug_flag || dip->di_pDebugFlag) {
large_t blocks, lba;
Lprintf(dip, "\nSlice %d Information:\n", sip->slice);
lba = (large_t)(dip->di_file_position / dip->di_dsize);
Lprintf(dip, DT_FIELD_WIDTH FUF " (lba " LUF ")\n", "Starting offset",
dip->di_file_position, lba);
lba = (large_t)((dip->di_file_position + sip->slice_length) / dip->di_dsize);
Lprintf(dip, DT_FIELD_WIDTH FUF " (lba " LUF ")\n", "Ending offset",
(dip->di_file_position + sip->slice_length), (lba - 1));
blocks = (large_t)(sip->slice_length / dip->di_dsize);
Lprintf(dip, DT_FIELD_WIDTH FUF " bytes (" LUF " block%s)\n", "Slice length",
sip->slice_length, blocks, (blocks > 1) ? "s" : "");
blocks = (large_t)(dip->di_data_limit / dip->di_dsize);
Lprintf(dip, DT_FIELD_WIDTH FUF " bytes (" LUF " block%s)\n", "Data limit",
dip->di_data_limit, blocks, (blocks > 1) ? "s" : "");
Lprintf(dip, DT_FIELD_WIDTH FUF " (lba "LUF") - "FUF" (lba "LUF")\n", "Random range",
dip->di_file_position, (large_t)(dip->di_file_position / dip->di_dsize),
dip->di_rdata_limit, (large_t)(dip->di_rdata_limit / dip->di_dsize));
#if defined(DEBUG)
if (dip->di_step_offset) {
lba = (large_t)(dip->di_end_position / dip->di_dsize);
Lprintf(dip, DT_FIELD_WIDTH FUF " (lba " LUF ")\n", "End Position",
dip->di_end_position, lba);
}
#endif /* defined(DEBUG) */
Lflush(dip);
}
dip->di_slice_number = sip->slice;
return;
}