-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsump.c
5635 lines (5146 loc) · 195 KB
/
sump.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
/* sump.c - SUMP Pump(TM) SMP/CMP parallel data pump library.
* SUMP Pump is a trademark of Ordinal Technology Corp
*
* $Revision: 124 $
*
* Copyright (C) 2010 - 2011, Ordinal Technology Corp, http://www.ordinal.com
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of Version 2 of the GNU General Public
* License as published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software Foundation,
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*
* Linking SUMP Pump statically or dynamically with other modules is
* making a combined work based on SUMP Pump. Thus, the terms and
* conditions of the GNU General Public License v.2 cover the whole
* combination.
*
* In addition, as a special exception, the copyright holders of SUMP Pump
* give you permission to combine SUMP Pump program with free software
* programs or libraries that are released under the GNU LGPL and with
* independent modules that communicate with SUMP Pump solely through
* Ordinal Technology Corp's Nsort Subroutine Library interface as defined
* in the Nsort User Guide, http://www.ordinal.com/NsortUserGuide.pdf.
* You may copy and distribute such a system following the terms of the
* GNU GPL for SUMP Pump and the licenses of the other code concerned,
* provided that you include the source code of that other code when and
* as the GNU GPL requires distribution of source code.
*
* Note that people who make modified versions of SUMP Pump are not
* obligated to grant this special exception for their modified
* versions; it is their choice whether to do so. The GNU General
* Public License gives permission to release a modified version without
* this exception; this exception also makes it possible to release a
* modified version which carries forward this exception.
*
* For more information on SUMP Pump, see:
* http://www.ordinal.com/sump.html
* http://code.google.com/p/sump-pump/
*/
# define AIO_CAPABLE
#if !defined(_WIN32)
# define _GNU_SOURCE
# include <pthread.h>
# include <unistd.h>
# include <dlfcn.h>
# include <sched.h>
# include <sys/mman.h>
# include <sys/types.h>
# include <sys/wait.h>
# include <sys/time.h>
# include <stdint.h>
# include <ctype.h>
# include <signal.h>
# if !defined(__CYGWIN32__)
# include <aio.h>
# include <sys/types.h>
# include <sys/stat.h>
# endif
# define PTFlld "lld"
# define PTFllu "llu"
# define PTFllx "llx"
# define ERRNO errno
#else /* now defined(_WIN32) */
# define PTFlld "I64d"
# define PTFllu "I64u"
# define PTFllx "I64x"
# define ERRNO GetLastError()
#endif /* !defined(_WIN32) */
#include "sump.h"
#include "sumpversion.h"
#include <stdio.h>
#include <string.h>
#include <fcntl.h>
#include <stdarg.h>
#include <stdlib.h>
#include <string.h>
#include <string.h>
#include <errno.h>
#if defined(SUMP_PUMP_NO_SORT)
/* define some nsort typedefs to minimize the number of #if's in this file */
typedef unsigned nsort_t; /* nsort context identifier */
typedef int nsort_msg_t; /* return status & error message numbers */
#else
/* use Nsort include files */
# include "nsort.h"
# include "nsorterrno.h"
#endif
/* values for the flags sump pump structure member
*/
#define SP_UNICODE 0x0001 /* not yet supported */
#define SP_UTF_8 0x0002 /* input records are utf-8
* characters */
#define SP_ASCII SP_UTF_8
#define SP_FIXED 0x0003 /* input records are a fixed
* number of bytes */
#define SP_WHOLE_BUF 0x0004 /* there are no input records,
* instead there is only an
* input buffer */
#define SP_REC_TYPE_MASK 0x0007
#define SP_SORT 0x0008
#define SP_GROUP_BY 0x0010
#define SP_EXEC 0x0020
#define REC_TYPE(sp) ((sp)->flags & SP_REC_TYPE_MASK)
#define TRUE 1
#define FALSE 0
/* sort_state values */
#define SORT_INPUT 1
#define SORT_OUTPUT 2
#define SORT_DONE 3
#define ERROR_BUF_SIZE 500 /* size of error buffer */
#define DEFAULT_BUFFERED_TRANSFER_SIZE (1024 * 1024)
#define DEFAULT_PIPE_TRANSFER_SIZE 8192
/* state structure for a sump pump instance */
struct sump
{
unsigned flags; /* caller-defined bit flags,
* see sump.h */
sp_pump_t pump_func; /* caller-defined pump function that
* is executed in parallel by the
* sump pump */
void *pump_arg; /* caller-defined arg to pump func */
unsigned num_tasks; /* number of pump tasks */
unsigned num_threads; /* number of threads executing the
* pump func */
unsigned num_in_bufs; /* number of input buffers */
unsigned num_outputs; /* number of sump pump output channels*/
ssize_t in_buf_size; /* input buffer size in bytes */
struct sump_out *out; /* array of output structures, one for
* each output */
void *delimiter; /* record delimiter, for text input */
size_t rec_size; /* record size */
pthread_mutex_t sump_mtx; /* mutex for sump pump infrastructure */
pthread_mutex_t sp_mtx; /* mutex for pump funcs to use
* via sp_mutex_lock() and
* sp_mutex_unlock() calls */
pthread_cond_t in_buf_readable_cond; /* input buffer available for
* reading by pump funcs */
pthread_cond_t in_buf_done_cond; /* an input buffer has been
* completely read by all potential
* pump threads and can be reused */
pthread_cond_t task_avail_cond; /* a task is available for the
* taking by a sump pump thread */
pthread_cond_t task_drained_cond; /* a task has been completely
* executed and its output has
* been drained (read) for its
* output buffer(s) */
pthread_cond_t task_output_ready_cond; /* a task's output ready to
* be read */
pthread_cond_t task_output_empty_cond; /* a task's output buffer has
* been read and is empty */
size_t in_buf_current_bytes; /* bytes in current input buf */
/* number of bytes at end of prev input buffer containing a partial rec */
size_t prev_in_buf_ending_rec_partial_bytes;
pthread_t *thread; /* array of sump pump threads */
uint64_t cnt_in_buf_readable; /* number of input buffers that
* have been filled with input
* data and are available for
* reading by sump pump threads
* executing pump functions */
uint64_t cnt_in_buf_done; /* number of input buffers
* that have been read by all
* their readers */
uint64_t cnt_task_init; /* number of tasks initialized and
* available for the taking by any
* sump pump thread */
uint64_t cnt_task_begun; /* number of tasks allocated/taken
* and begun by sump pump threads */
uint64_t cnt_task_drained; /* number of tasks that have
* been completed and had all
* their output buffer(s)
* completely read/drained. */
uint64_t cnt_task_done; /* number of done tasks whose
* actual ending position has been
* verified to be the same as their
* expected ending */
struct sp_task *task; /* array of sump pump tasks */
struct in_buf *in_buf; /* array of sump pump input buffers */
nsort_t nsort_ctx; /* used only if this is a sort */
char *error_buf; /* buf to hold error msg */
size_t error_buf_size; /* buf to hold error msg */
int error_code; /* pump func generated error code */
unsigned sort_error; /* sort error code */
char *sort_temp_buf; /* sort temporary buf */
size_t sort_temp_buf_size; /* size of sort temporary buf */
size_t sort_temp_buf_bytes; /* bytes of data in temp buf */
char input_eof; /* sp_write_input() called with
* size <= 0 */
char broken_input; /* sp_write_input() called with
* a negative size */
char sort_state; /* only used for sorting */
char match_keys; /* only used for sorting - indicates
* -match has been specified */
char wait_done; /* sp_wait already called for this sp*/
char in_file_alloc; /* in_file string was malloc()'d and
* should be free()'d */
char *in_file; /* input file str or NULL if none */
struct sp_file *in_file_sp; /* input file of sump pump */
struct exec_state *ex_state; /* used when internal pump func invokes
* an external executable program.
* one state per sump pump thread */
char **exec_argv; /* exec process command line */
};
/* struct for an output of a task */
struct task_out
{
char *buf; /* output buffer where map task should
* write its results */
size_t size; /* capacity of output buffer */
size_t bytes_copied; /* number of bytes written so far into
* output buffer */
char stalled; /* the map thread handling this task is
* stalled waiting for the writer thread to
* empty its full buf */
};
#define INVALID_FD (-1)
#define PIPE_BUF_SIZE 4096
/* macro to allow the stderr of external programs performing pump functions
* to be the second output of the sump pump. The initial implementation of
* external programs for pump functions did gather stderr as the second
* output; but it seemed problematic because when an eternal program failed
* and wrote an error message to its standard error, the sump pump thread
* that reads the pump func input and writes it to the standard input of
* external program would notice that external program had terminated
* abnormally and would set the error code for the sump pump; then the
* thread reading the stderr of the external program would notice the sump
* pump error code and exit before it would read the error message in the
* stderr; thus the error message never made it through sump pump.
*
#define SUMP_PIPE_STDERR
*/
/* per-pipe struct used when pump funcs call a separate program */
struct std_pipe
{
struct exec_state *ex;
int perrno;
#if defined(win_nt)
HANDLE rd_h; /* read handle */
HANDLE wr_h; /* write handle */
#else
int rd_fd; /* read file descriptor */
int wr_fd; /* write file descriptor */
#endif
};
/* per-thread struct used when pump funcs call a separate program */
struct exec_state
{
sp_task_t t;
struct std_pipe in;
char in_buf[PIPE_BUF_SIZE];
struct std_pipe out;
char out_buf[PIPE_BUF_SIZE];
#if defined(SUMP_PIPE_STDERR)
struct std_pipe err;
char err_buf[PIPE_BUF_SIZE];
#endif
};
/* struct for a sump pump task */
struct sp_task
{
struct sump *sp; /* the "host" sp_t of this task */
uint64_t task_number; /* task number */
int thread_index; /* id of thread performing this task */
char *in_buf; /* input buffer */
size_t in_buf_bytes; /* number of bytes written into in_buf
* by the reader thread. these bytes
* will be read out by map task */
char *rec_buf; /* buf to hold rec returned by pf_get_rec() */
size_t rec_buf_size; /* size of rec_buf */
char *curr_rec; /* pointer to the current record */
char *temp_buf; /* temp buf to help with printf */
size_t temp_buf_size; /* size of the temp buf */
char *error_buf; /* error message posted by sp_error() call */
size_t error_buf_size; /* size of the error message buf */
int error_code; /* pump func generated error code */
int sort_error; /* nsort error code */
uint64_t curr_in_buf_index; /* current input buffer index */
char *begin_rec; /* pointer to the beginning record */
uint64_t begin_in_buf_index;/* beginning input buffer index */
/* the following 2 members are set by the thread calling sp_write_input()
*/
uint64_t expected_end_index; /* expected end in buf index */
int expected_end_offset; /* expected end in buf offset */
char first_group_rec; /* next record read will be the first
* record for its record group */
char first_in_buf; /* this task is still reading its
* first input buffer */
char input_eof; /* boolean: this task is done reading
* its input */
char output_eof; /* boolean: thread performing task is
* done writing its output to its output
* buffer, but we may still need to wait
* until the output buffer has been read
* before the task is done */
int outs_drained; /* number of outputs for this task that
* have been completely drained (read) */
struct task_out *out; /* array of task outputs */
};
/* struct for a sump pump input buffer */
typedef struct in_buf
{
char *in_buf; /* input buffer */
size_t in_buf_bytes; /* number of bytes written into in_buf
* by the reader thread. these bytes
* will be read out by map task */
size_t in_buf_size; /* size of the in_buf */
size_t alloc_size; /* allocation size of the in_buf */
unsigned num_readers; /* number of threads performing
* tasks that read this buf */
unsigned num_readers_done;/* number of reader threads that are
* done with this buffer */
} in_buf_t;
/* struct for a link (copy thread) between an output of one sump pump and
* the input of another.
*/
struct sp_link
{
struct sump *out_sp; /* sp_t we are reading from */
unsigned out_index; /* output index of read sp_t */
struct sump *in_sp; /* sp_t we are writing to */
size_t buf_size; /* buf size */
char *buf; /* temp buf for transfering data */
pthread_t thread; /* thread executing link_main() */
int error_code; /* error code */
};
/* struct for a file reader or writer thread */
struct sp_file
{
char *fname; /* file name */
pthread_t thread; /* thread executing either file_reader() or
* file_writer() */
sp_t sp; /* sump pump this file */
int mode; /* file access mode */
#if defined(win_nt)
HANDLE fd; /* file handle */
#else
int fd; /* file descriptor */
#endif
char wait_done; /* sp_wait already called for this sp_file */
int out_index; /* sump pump output index (if relevant) */
int aio_count; /* the max and target number of async i/o's */
size_t transfer_size; /* read or write request size */
int error_code; /* error code */
int can_seek; /* if true, then direct/async-capable file */
int is_std; /* file is either stdin, stdout or stderr */
};
/* file access modes */
#define MODE_UNSPECIFIED 0 /* no access mode has been specified */
#define MODE_BUFFERED 1 /* use standard read() or write() calls */
#define MODE_DIRECT 2 /* direct and asynchronous r/w requests */
/* struct for a sump pump output */
struct sump_out
{
size_t buf_size; /* size of each task's corresponding
* output buffer for this output */
int size_specified; /* boolean: non-zero if out buf size
* has been set */
double buf_size_mult;/* factor increase over input buf size */
size_t partial_bytes_copied; /* the number of bytes copied
* from the task output buffer
* currently being read from */
char file_alloc; /* file name was malloc()'d and
* should be free()'d */
char *file; /* output file str or NULL if none */
struct sp_file *file_sp; /* output file for this sump pump out */
uint64_t cnt_task_drained; /* number of tasks that have
* been completed and their
* output buffer for this
* particular output has been
* completely read */
};
#if defined(AIO_CAPABLE)
/* sump aio struct */
struct sump_aio
{
uint64_t buf_index; /* sump pump buffer index */
size_t buf_offset; /* beginning io offset within buffer */
char last_buf_io; /* boolean indicating last io for buf*/
int64_t file_offset; /* file offset */
size_t nbytes; /* request size */
struct aiocb aio;
};
#endif
/* global sump pump mutex */
static pthread_mutex_t Global_lock = PTHREAD_MUTEX_INITIALIZER;
static sp_t Global_external_sp;
static int Global_external_count;
/* file descriptor for /dev/zero */
static int Zero_fd;
/* default size for sp_write_input() and sp_read_output() transfers for
* regression testing of those interfaces.
*/
static size_t Default_rw_test_size;
/* default file access mode */
static int Default_file_mode = MODE_DIRECT;
/* die - quit program due to a fatal sump pump infrastructure error.
*/
static void die(char *fmt, ...)
{
va_list ap;
fprintf(stderr, "sump pump fatal error: ");
va_start(ap, fmt);
vfprintf(stderr, fmt, ap);
va_end(ap);
exit(1);
}
#define PAGE_SIZE page_size()
/* page_size - internal routine to get the system page size.
*/
static int page_size()
{
static int size;
if (size == 0)
{
#if defined(win_nt)
SYSTEM_INFO si;
GetSystemInfo(&si);
size = si.dwPageSize;
#else
size = getpagesize();
#endif
}
return (size);
}
/* sp_get_time_us - return elapsed time in microseconds.
*
* The caller can discard the upper 32 bits *only* when timing intervals
* less than ~4000 seconds = 1 hour 11 minutes.
*/
static uint64_t sp_get_time_us(void)
{
struct timeval time;
static uint64_t begin;
if (begin == 0) /* if first time */
{
if (gettimeofday(&time, NULL) < 0)
die("gettimeofday() failure\n");
begin = (time.tv_sec * (uint64_t)1000 * 1000) + time.tv_usec;
}
if (gettimeofday(&time, NULL) < 0)
die("gettimeofday() failure\n");
return (time.tv_sec * (uint64_t)1000 * 1000) + time.tv_usec - begin;
}
static FILE *TraceFp;
#define TRACE if (TraceFp != NULL) trace
/* trace - print a trace message
*/
static void trace(const char *fmt, ...)
{
va_list ap;
uint64_t diff = sp_get_time_us();
int seconds = (int)(diff / 1000000);
int fractions = (int)(diff % 1000000);
fprintf(TraceFp, "%2d.%06d: ", seconds, fractions);
va_start(ap, fmt);
vfprintf(TraceFp, fmt, ap);
va_end(ap);
fflush(TraceFp);
}
/* sp_get_version - get the subversion version for sump pump
*
* Returns: a string containing the subversion version. The string should
* NOT be free()'d.
*/
const char *sp_get_version(void)
{
return (sp_version);
}
/* sp_get_id - get the subversion id keyword substitution for sump pump
*
* Returns: a string containing the subversion id keyword. The string should
* NOT be free()'d.
*/
const char *sp_get_id(void)
{
return ("$Id$");
}
/* get_error_msg - place a system error message in the provided buffer
*/
static char *get_error_msg(int error, char *err_buf, size_t err_buf_size)
{
#if defined(win_nt)
char *lpMsgBuf;
char *eol;
if (error == 0) /* if no error specified, get last error */
error = GetLastError();
FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER |
FORMAT_MESSAGE_FROM_SYSTEM |
FORMAT_MESSAGE_IGNORE_INSERTS,
GetModuleHandle(NULL),
error,
MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
(LPTSTR) &lpMsgBuf,
0,
NULL);
eol = lpMsgBuf + strlen(lpMsgBuf);
/* remove trailing \r\n so (%d) appears on the same line */
if (eol > lpMsgBuf && eol[-1] == '\n' && eol[-2] == '\r')
eol[-2] = '\0';
_snprintf(err_buf, err_buf_size - 1, "%s (%d)", lpMsgBuf, error);
LocalFree(lpMsgBuf);
return (err_buf);
#else
if (error == 0) /* if no error specified, get last error */
error = errno;
return (strerror_r(error, err_buf, err_buf_size));
#endif
}
/* start_error - raise an error and set error message during sp_start()
* but before the sump pump threads are created.
*/
static int start_error(sp_t sp, const char *fmt, ...)
{
va_list ap;
int ret;
if (sp->error_code != 0) /* if prior error */
return sp->error_code; /* ignore this one */
sp->error_code = SP_START_ERROR;
va_start(ap, fmt);
ret = vsnprintf(sp->error_buf, sp->error_buf_size, fmt, ap);
va_end(ap);
#if defined(win_nt)
if (ret == -1) /* non-standard vsnprintf overflow indicator on Windows */
{
va_start(ap, fmt);
ret = _vscprintf(fmt, ap);
va_end(ap);
}
#endif
if ((size_t)ret >= sp->error_buf_size)
{
if (sp->error_buf_size != 0)
free(sp->error_buf);
sp->error_buf_size = (size_t)ret + 1;
sp->error_buf = (char *)malloc(sp->error_buf_size);
va_start(ap, fmt);
vsnprintf(sp->error_buf, sp->error_buf_size, fmt, ap);
va_end(ap);
}
return SP_START_ERROR;
}
/* broadcast_all_conds - internal routine to broadcast all sump pump conditions
* The sump_mtx should already be locked.
*/
static void broadcast_all_conds(sp_t sp)
{
pthread_cond_broadcast(&sp->in_buf_readable_cond); /*multiple sp threads*/
pthread_cond_broadcast(&sp->in_buf_done_cond);/* sp_write_input() caller */
pthread_cond_broadcast(&sp->task_avail_cond); /* multiple sp threads */
pthread_cond_broadcast(&sp->task_drained_cond);/* sp_write_input() caller*/
pthread_cond_broadcast(&sp->task_output_ready_cond); /* mult sp threads */
pthread_cond_broadcast(&sp->task_output_empty_cond); /* mult sp threads */
}
/* sp_raise_error - raise an error for a sump pump
*/
void sp_raise_error(sp_t sp, int error_code, const char *fmt, ...)
{
va_list ap;
int ret;
pthread_mutex_lock(&sp->sump_mtx);
if (sp->error_code != 0) /* if prior error */
{
pthread_mutex_unlock(&sp->sump_mtx);
return; /* ignore this one. let prior error stand */
}
sp->error_code = error_code;
va_start(ap, fmt);
ret = vsnprintf(sp->error_buf, sp->error_buf_size, fmt, ap);
va_end(ap);
#if defined(win_nt)
if (ret == -1) /* non-standard vsnprintf overflow indicator on Windows */
{
va_start(ap, fmt);
ret = _vscprintf(fmt, ap);
va_end(ap);
}
#endif
if ((size_t)ret >= sp->error_buf_size)
{
if (sp->error_buf_size != 0)
free(sp->error_buf);
sp->error_buf_size = (size_t)ret + 1;
sp->error_buf = (char *)malloc(sp->error_buf_size);
va_start(ap, fmt);
vsnprintf(sp->error_buf, sp->error_buf_size, fmt, ap);
va_end(ap);
}
/* Wake up all possible waiting threads for the sump pump.
* Some of the below pthread_cond_broadcasts could just be signals.
* But since error handling isn't a performance critical operation,
* signals are used instead.
*/
broadcast_all_conds(sp);
pthread_mutex_unlock(&sp->sump_mtx);
}
#if defined(win_nt)
# include "sump_win.c"
#else
/* init_zero_fd - initialize Zero_fd, the file descriptor for /dev/zero.
*/
static void init_zero_fd()
{
pthread_mutex_lock(&Global_lock);
if (Zero_fd <= 0)
Zero_fd = open("/dev/zero", O_RDWR);
pthread_mutex_unlock(&Global_lock);
return;
}
#endif
#if !defined(SUMP_PUMP_NO_SORT)
/* function pointers to nsort library entry points. These are
* non-NULL if the nsort library is linked in.
*/
nsort_msg_t (*Nsort_define)(const char *def,
unsigned options,
nsort_error_callback_t *callbacks,
nsort_t *ctxp);
nsort_msg_t (*Nsort_release_recs)(void *buf,
size_t size,
nsort_t *ctxp);
nsort_msg_t (*Nsort_release_end)(nsort_t *ctxp);
nsort_msg_t (*Nsort_return_recs)(void *buf,
size_t *size,
nsort_t *ctxp);
nsort_msg_t (*Nsort_end)(nsort_t *ctxp);
const char *(*Nsort_get_stats)(nsort_t *ctxp);
char *(*Nsort_message)(nsort_t *ctxp);
char *(*Nsort_version)(void);
typedef nsort_msg_t (*declare_function_t)(char *name, nsort_compare_t func, void *arg);
typedef nsort_msg_t (*define_t)(const char *, unsigned, nsort_callback_t *, nsort_t *ctxp);
typedef nsort_msg_t (*merge_define_t)(const char *def, unsigned options, nsort_error_callback_t *callbacks, int merge_width, nsort_merge_callback_t *merge_input, nsort_t *ctxp);
typedef nsort_msg_t (*release_recs_t)(void *buf, size_t size, nsort_t *ctxp);
typedef nsort_msg_t (*release_end_t)(nsort_t *ctxp);
typedef nsort_msg_t (*return_recs_t)(void *buf, size_t *size, nsort_t *ctxp);
typedef nsort_msg_t (*print_stats_t)(nsort_t *ctxp, FILE *fp);
typedef const char *(*get_stats_t)(nsort_t *ctxp);
typedef char *(*message_t)(nsort_t *ctxp);
typedef nsort_msg_t (*end_t)(nsort_t *ctxp);
typedef char *(*version_t)(void);
/* get_nsort_syms - internal routine to dynamically link to nsort library
*/
static int get_nsort_syms()
{
# if defined(win_nt)
# define dlsym(handle, name) GetProcAddress((handle), (name))
HANDLE syms;
if ((syms = LoadLibrary("libnsort.dll")) == NULL)
return (-1);
# else
void *syms;
if ((syms = dlopen("libnsort.so", RTLD_GLOBAL | RTLD_LAZY)) == NULL)
return (-1);
# endif
if ((Nsort_define = (define_t)dlsym(syms, "nsort_define")) == NULL)
return (-2);
if ((Nsort_release_recs = (release_recs_t)dlsym(syms, "nsort_release_recs")) == NULL)
return (-2);
if ((Nsort_release_end = (release_end_t)dlsym(syms, "nsort_release_end")) == NULL)
return (-2);
if ((Nsort_return_recs = (return_recs_t)dlsym(syms, "nsort_return_recs")) == NULL)
return (-2);
if ((Nsort_end = (end_t)dlsym(syms, "nsort_end")) == NULL)
return (-2);
if ((Nsort_get_stats = (get_stats_t)dlsym(syms, "nsort_get_stats")) == NULL)
return (-2);
if ((Nsort_message = (message_t)dlsym(syms, "nsort_message")) == NULL)
return (-2);
if ((Nsort_version = (version_t)dlsym(syms, "nsort_version")) == NULL)
return (-2);
return (0);
}
/* link_in_nsort - internal routine to, if not already done, dynamically
* link in the nsort library.
*/
static int link_in_nsort()
{
int ret;
# if !defined(win_nt)
pthread_mutex_lock(&Global_lock);
# endif
ret = Nsort_define == NULL ? get_nsort_syms() : 0;
# if !defined(win_nt)
pthread_mutex_unlock(&Global_lock);
# endif
return (ret);
}
/* post_nsort_error - internal routine to post an error received from nsort.
*/
static void post_nsort_error(sp_t sp, unsigned ret)
{
pthread_mutex_lock(&sp->sump_mtx);
if (sp->error_code == SP_OK) /* if no other error yet, this is the one */
{
char *msg = (*Nsort_message)(&sp->nsort_ctx);
sp->error_code = SP_SORT_EXEC_ERROR;
sp->sort_error = ret;
if (msg == NULL)
msg = "No Nsort error message";
strncpy(sp->error_buf, msg, sp->error_buf_size);
sp->error_buf[sp->error_buf_size - 1] = '\0'; /* handle overflow */
}
sp->sort_error = ret;
sp->sort_state = SORT_DONE;
pthread_cond_broadcast(&sp->task_output_ready_cond);
pthread_mutex_unlock(&sp->sump_mtx);
}
#define STAT_DRCTV " -stat"
/* sp_start_sort - start an nsort instance with a sump pump wrapper so
* that its input or output can be assigned to a file or
* linked to another sump pump. For instance, the sort
* input or output can be linked to sump pump performing
* record pumping such as "map" on input and "reduce"
* for output.
* Parameters:
* sp - Pointer to where to return newly allocated sp_t
* identifier that will be used in as the first
* argument to all subsequent sp_*() calls.
* def - Nsort sort definition string. Besides the Nsort
* commands listed in the Nsort User Guide, the following
* directives are also recognized:
* -match[=%d] Each output record will be preceded by a
* single byte that indicates the
* specified number of keys in this record
* are the same as in the previous record.
* If no key number is specified, all keys
* are examined for a match condition.
*
* Returns: SP_OK or a sump pump error code
*/
int sp_start_sort(sp_t *caller_sp,
char *def_fmt,
...)
{
int ret;
char *def_plus = NULL;
sp_t sp;
size_t def_len;
char *def;
char thread_drctv[30];
unsigned char *p;
# if defined(win_nt)
SYSTEM_INFO si;
# endif
*caller_sp = NULL; /* assume the worst for now */
sp = (sp_t)calloc(1, sizeof(struct sump));
if (sp == NULL)
return (SP_MEM_ALLOC_ERROR);
sp->error_buf_size = ERROR_BUF_SIZE;
sp->error_buf = (char *)calloc(1, sp->error_buf_size);
if (sp->error_buf == NULL)
return (SP_MEM_ALLOC_ERROR);
*caller_sp = sp; /* allow access to error_buf even if failure */
/* fill in default parameters */
# if defined(win_nt)
GetSystemInfo(&si);
sp->num_threads = si.dwNumberOfProcessors;
# else
sp->num_threads = sysconf(_SC_NPROCESSORS_ONLN);
# endif
sprintf(thread_drctv, "-threads=%d ", sp->num_threads);
if (sp->num_outputs > 32)
sp->num_outputs = 32;
sp->num_outputs = 1;
sp->out = (struct sump_out *)calloc(1, sizeof(struct sump_out));
sp->delimiter = (void *)"\n";
sp->rec_size = 0;
if (link_in_nsort() != 0) /* if error */
{
sp->error_code = SP_NSORT_LINK_FAILURE;
sp->sort_state = SORT_DONE;
return (sp->error_code);
}
sp->flags |= SP_SORT;
if (def_fmt != NULL)
{
va_list ap;
size_t def_len;
va_start(ap, def_fmt);
#if defined(win_nt)
def_len = _vscprintf(def_fmt, ap);
#else
def_len = vsnprintf(NULL, 0, def_fmt, ap);
#endif
va_end(ap);
def = (char *)calloc(def_len + 1, 1);
va_start(ap, def_fmt);
if (vsnprintf(def, def_len + 1, def_fmt, ap) != def_len)
die("sp_start_sort: vnsprintf failed to return %d\n", def_len);
va_end(ap);
}
else
def = "";
def_len = strlen(def) + 1; /* plus '\0' */
def_len += strlen(STAT_DRCTV);
def_len += strlen(thread_drctv);
def_plus = (char *)malloc(def_len);
if (def_plus == NULL)
return (SP_MEM_ALLOC_ERROR);
strcpy(def_plus, thread_drctv); /* goes first so caller can override */
strcat(def_plus, def);
strcat(def_plus, STAT_DRCTV);
/* check for input file declaration */
for (p = (unsigned char *)def_plus; *p != '\0'; p++)
{
if (p[0] == '-' || p[0] == '/')
{
p++;
/* detect -IN[_]F[ILE] */
if (toupper(p[0]) == 'I' && toupper(p[1]) == 'N' &&
(toupper(p[2]) == 'F' || p[2] == '_'))
{
sp->in_file = "<defined to nsort>";
p += 3;
}
/* detect -OUT[_]F[ILE] */
else if (toupper(p[0]) == 'O' && toupper(p[1]) == 'U' &&
toupper(p[2]) == 'T' &&
(toupper(p[3]) == 'F' || p[3] == '_'))
{
sp->out[0].file = "<defined to nsort>";
p += 4;
}
/* detect -MATCH */
else if (toupper(p[0]) == 'M' && toupper(p[1]) == 'A' &&
toupper(p[2]) == 'T' && toupper(p[3]) == 'C' &&
toupper(p[4]) == 'H')
{
sp->match_keys = TRUE;
p += 5;
}
}
}
/* check for output file declaration */
ret = (*Nsort_define)(def_plus, 0, NULL, &sp->nsort_ctx);
free(def_plus);
if (def_fmt != NULL)
free(def);
if (ret < 0)
{
char *msg = (*Nsort_message)(&sp->nsort_ctx);
sp->sort_error = ret;
sp->error_code = SP_SORT_DEF_ERROR;
if (msg == NULL)
msg = "No Nsort error message";
strncpy(sp->error_buf, msg, sp->error_buf_size - 1);
sp->error_buf[sp->error_buf_size - 1] = '\0'; /* handle overflow */
sp->sort_state = SORT_DONE;
return (SP_SORT_DEF_ERROR);
}
sp->sort_state = sp->in_file != NULL ? SORT_OUTPUT : SORT_INPUT;
pthread_mutex_init(&sp->sump_mtx, NULL);
/* use output ready cond for state changes */
pthread_cond_init(&sp->task_output_ready_cond, NULL);
sp->sort_temp_buf_size = sp->out[0].buf_size ? sp->out[0].buf_size : 4096;
if ((sp->sort_temp_buf = (char *)malloc(sp->sort_temp_buf_size)) == NULL)
return (SP_MEM_ALLOC_ERROR);
return (SP_OK);
}
/* sp_get_sort_stats - get a string containing the nsort statistics report
* for an nsort sump pump that has completed.
*
* Returns: a string containing the Nsort statistics report. The string should
* NOT be free()'d and is valid until the passed sp_t is sp_free()'d.
*/
const char *sp_get_sort_stats(sp_t sp)
{
const char *ret;
if (sp->error_code)
return ("no stats because of nsort error");
if (!(sp->flags & SP_SORT))
return (NULL);
if ((ret = (*Nsort_get_stats)(&sp->nsort_ctx)) == NULL)
ret = "Nsort_get_stats() failure\n";
return (ret);
}