1*0Sstevel@tonic-gate /*
2*0Sstevel@tonic-gate * CDDL HEADER START
3*0Sstevel@tonic-gate *
4*0Sstevel@tonic-gate * The contents of this file are subject to the terms of the
5*0Sstevel@tonic-gate * Common Development and Distribution License, Version 1.0 only
6*0Sstevel@tonic-gate * (the "License"). You may not use this file except in compliance
7*0Sstevel@tonic-gate * with the License.
8*0Sstevel@tonic-gate *
9*0Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10*0Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing.
11*0Sstevel@tonic-gate * See the License for the specific language governing permissions
12*0Sstevel@tonic-gate * and limitations under the License.
13*0Sstevel@tonic-gate *
14*0Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each
15*0Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16*0Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the
17*0Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying
18*0Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner]
19*0Sstevel@tonic-gate *
20*0Sstevel@tonic-gate * CDDL HEADER END
21*0Sstevel@tonic-gate */
22*0Sstevel@tonic-gate /*
23*0Sstevel@tonic-gate * Copyright 2004 Sun Microsystems, Inc. All rights reserved.
24*0Sstevel@tonic-gate * Use is subject to license terms.
25*0Sstevel@tonic-gate */
26*0Sstevel@tonic-gate
27*0Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI"
28*0Sstevel@tonic-gate
29*0Sstevel@tonic-gate #include "streams_mmap.h"
30*0Sstevel@tonic-gate #include "streams_common.h"
31*0Sstevel@tonic-gate
32*0Sstevel@tonic-gate /*
33*0Sstevel@tonic-gate * Single-byte character memory map-based streams implementation
34*0Sstevel@tonic-gate */
35*0Sstevel@tonic-gate
36*0Sstevel@tonic-gate static int
stream_mmap_prime(stream_t * str)37*0Sstevel@tonic-gate stream_mmap_prime(stream_t *str)
38*0Sstevel@tonic-gate {
39*0Sstevel@tonic-gate char *nl;
40*0Sstevel@tonic-gate
41*0Sstevel@tonic-gate if (stream_is_primed(str))
42*0Sstevel@tonic-gate return (PRIME_SUCCEEDED);
43*0Sstevel@tonic-gate
44*0Sstevel@tonic-gate stream_set(str, STREAM_PRIMED);
45*0Sstevel@tonic-gate
46*0Sstevel@tonic-gate if (str->s_buffer_size == 0) {
47*0Sstevel@tonic-gate stream_set(str, STREAM_EOS_REACHED);
48*0Sstevel@tonic-gate return (PRIME_FAILED_EMPTY_FILE);
49*0Sstevel@tonic-gate }
50*0Sstevel@tonic-gate
51*0Sstevel@tonic-gate str->s_current.l_data.sp = str->s_buffer;
52*0Sstevel@tonic-gate str->s_type.SF.s_release_origin = str->s_buffer;
53*0Sstevel@tonic-gate if ((nl = (char *)memchr(str->s_buffer, '\n', str->s_buffer_size)) ==
54*0Sstevel@tonic-gate NULL) {
55*0Sstevel@tonic-gate warn(WMSG_NEWLINE_ADDED, str->s_filename);
56*0Sstevel@tonic-gate str->s_current.l_data_length = str->s_buffer_size;
57*0Sstevel@tonic-gate } else {
58*0Sstevel@tonic-gate str->s_current.l_data_length = nl - (char *)str->s_buffer;
59*0Sstevel@tonic-gate }
60*0Sstevel@tonic-gate
61*0Sstevel@tonic-gate str->s_current.l_collate.sp = NULL;
62*0Sstevel@tonic-gate str->s_current.l_collate_length = 0;
63*0Sstevel@tonic-gate
64*0Sstevel@tonic-gate __S(stats_incr_fetches());
65*0Sstevel@tonic-gate return (PRIME_SUCCEEDED);
66*0Sstevel@tonic-gate }
67*0Sstevel@tonic-gate
68*0Sstevel@tonic-gate /*
69*0Sstevel@tonic-gate * stream_mmap_fetch() sets the fields of str->s_current to delimit the next
70*0Sstevel@tonic-gate * line of the field.
71*0Sstevel@tonic-gate */
72*0Sstevel@tonic-gate static ssize_t
stream_mmap_fetch(stream_t * str)73*0Sstevel@tonic-gate stream_mmap_fetch(stream_t *str)
74*0Sstevel@tonic-gate {
75*0Sstevel@tonic-gate ssize_t dist_to_buf_end;
76*0Sstevel@tonic-gate char *next_nl;
77*0Sstevel@tonic-gate
78*0Sstevel@tonic-gate ASSERT(stream_is_primed(str));
79*0Sstevel@tonic-gate ASSERT((str->s_status & STREAM_EOS_REACHED) == 0);
80*0Sstevel@tonic-gate
81*0Sstevel@tonic-gate /*
82*0Sstevel@tonic-gate * adding one for newline
83*0Sstevel@tonic-gate */
84*0Sstevel@tonic-gate str->s_current.l_data.sp = str->s_current.l_data.sp +
85*0Sstevel@tonic-gate str->s_current.l_data_length + 1;
86*0Sstevel@tonic-gate
87*0Sstevel@tonic-gate dist_to_buf_end = str->s_buffer_size - (str->s_current.l_data.sp
88*0Sstevel@tonic-gate - (char *)str->s_buffer);
89*0Sstevel@tonic-gate ASSERT(dist_to_buf_end >= 0 && dist_to_buf_end <= str->s_buffer_size);
90*0Sstevel@tonic-gate
91*0Sstevel@tonic-gate next_nl = memchr(str->s_current.l_data.sp, '\n', dist_to_buf_end);
92*0Sstevel@tonic-gate
93*0Sstevel@tonic-gate if (next_nl)
94*0Sstevel@tonic-gate str->s_current.l_data_length = next_nl
95*0Sstevel@tonic-gate - str->s_current.l_data.sp;
96*0Sstevel@tonic-gate else {
97*0Sstevel@tonic-gate warn(WMSG_NEWLINE_ADDED, str->s_filename);
98*0Sstevel@tonic-gate str->s_current.l_data_length = dist_to_buf_end;
99*0Sstevel@tonic-gate }
100*0Sstevel@tonic-gate
101*0Sstevel@tonic-gate /*
102*0Sstevel@tonic-gate * adding one for newline
103*0Sstevel@tonic-gate */
104*0Sstevel@tonic-gate if (str->s_current.l_data.sp + str->s_current.l_data_length + 1 >=
105*0Sstevel@tonic-gate (char *)str->s_buffer + str->s_buffer_size)
106*0Sstevel@tonic-gate stream_set(str, STREAM_EOS_REACHED);
107*0Sstevel@tonic-gate
108*0Sstevel@tonic-gate str->s_current.l_collate_length = 0;
109*0Sstevel@tonic-gate
110*0Sstevel@tonic-gate __S(stats_incr_fetches());
111*0Sstevel@tonic-gate return (NEXT_LINE_COMPLETE);
112*0Sstevel@tonic-gate }
113*0Sstevel@tonic-gate
114*0Sstevel@tonic-gate static int
stream_mmap_is_closable(stream_t * str)115*0Sstevel@tonic-gate stream_mmap_is_closable(stream_t *str)
116*0Sstevel@tonic-gate {
117*0Sstevel@tonic-gate if (str->s_status & STREAM_OPEN)
118*0Sstevel@tonic-gate return (1);
119*0Sstevel@tonic-gate return (0);
120*0Sstevel@tonic-gate }
121*0Sstevel@tonic-gate
122*0Sstevel@tonic-gate static int
stream_mmap_close(stream_t * str)123*0Sstevel@tonic-gate stream_mmap_close(stream_t *str)
124*0Sstevel@tonic-gate {
125*0Sstevel@tonic-gate if (str->s_type.SF.s_fd > -1) {
126*0Sstevel@tonic-gate (void) close(str->s_type.SF.s_fd);
127*0Sstevel@tonic-gate stream_unset(str, STREAM_OPEN);
128*0Sstevel@tonic-gate return (1);
129*0Sstevel@tonic-gate }
130*0Sstevel@tonic-gate
131*0Sstevel@tonic-gate return (0);
132*0Sstevel@tonic-gate }
133*0Sstevel@tonic-gate
134*0Sstevel@tonic-gate static int
stream_mmap_free(stream_t * str)135*0Sstevel@tonic-gate stream_mmap_free(stream_t *str)
136*0Sstevel@tonic-gate {
137*0Sstevel@tonic-gate if (!(str->s_status & STREAM_OPEN) ||
138*0Sstevel@tonic-gate (str->s_consumer != NULL &&
139*0Sstevel@tonic-gate str->s_consumer->s_status & STREAM_NOT_FREEABLE))
140*0Sstevel@tonic-gate return (0);
141*0Sstevel@tonic-gate
142*0Sstevel@tonic-gate if (str->s_buffer == NULL)
143*0Sstevel@tonic-gate return (1);
144*0Sstevel@tonic-gate
145*0Sstevel@tonic-gate if (munmap(str->s_buffer, str->s_buffer_size) < 0)
146*0Sstevel@tonic-gate die(EMSG_MUNMAP, str->s_filename);
147*0Sstevel@tonic-gate
148*0Sstevel@tonic-gate str->s_buffer = NULL;
149*0Sstevel@tonic-gate str->s_buffer_size = 0;
150*0Sstevel@tonic-gate
151*0Sstevel@tonic-gate stream_unset(str, STREAM_PRIMED);
152*0Sstevel@tonic-gate
153*0Sstevel@tonic-gate return (1);
154*0Sstevel@tonic-gate }
155*0Sstevel@tonic-gate
156*0Sstevel@tonic-gate static int
stream_mmap_eos(stream_t * str)157*0Sstevel@tonic-gate stream_mmap_eos(stream_t *str)
158*0Sstevel@tonic-gate {
159*0Sstevel@tonic-gate int retval = 0;
160*0Sstevel@tonic-gate
161*0Sstevel@tonic-gate if (str == NULL || str->s_status & STREAM_EOS_REACHED)
162*0Sstevel@tonic-gate return (1);
163*0Sstevel@tonic-gate
164*0Sstevel@tonic-gate /*
165*0Sstevel@tonic-gate * If the file's size is known to be zero, then we are at EOS; the
166*0Sstevel@tonic-gate * remaining checks are only sensible if we successfully primed this
167*0Sstevel@tonic-gate * stream. The additional character is for the optional newline.
168*0Sstevel@tonic-gate */
169*0Sstevel@tonic-gate if (str->s_filesize == 0 ||
170*0Sstevel@tonic-gate (stream_is_primed(str) && str->s_current.l_data.sp -
171*0Sstevel@tonic-gate (char *)str->s_buffer + str->s_current.l_data_length + 1 >=
172*0Sstevel@tonic-gate str->s_buffer_size)) {
173*0Sstevel@tonic-gate retval = 1;
174*0Sstevel@tonic-gate stream_set(str, STREAM_EOS_REACHED);
175*0Sstevel@tonic-gate }
176*0Sstevel@tonic-gate
177*0Sstevel@tonic-gate return (retval);
178*0Sstevel@tonic-gate }
179*0Sstevel@tonic-gate
180*0Sstevel@tonic-gate #define ALIGNED (~(ulong_t)(PAGESIZE - 1))
181*0Sstevel@tonic-gate
182*0Sstevel@tonic-gate /*
183*0Sstevel@tonic-gate * In certain cases, we know that we will never need the data on a page again
184*0Sstevel@tonic-gate * for the duration of the sort. (These cases are associated with merges
185*0Sstevel@tonic-gate * involving temporary files.) We can thus release all pages previous to the
186*0Sstevel@tonic-gate * page containing the current line, using the MADV_DONTNEED flag to
187*0Sstevel@tonic-gate * madvise(3C). This additional memory management improves our chances of
188*0Sstevel@tonic-gate * avoiding a paging situation, by evicting pages we know are of no use.
189*0Sstevel@tonic-gate */
190*0Sstevel@tonic-gate static void
stream_mmap_release_line(stream_t * str)191*0Sstevel@tonic-gate stream_mmap_release_line(stream_t *str)
192*0Sstevel@tonic-gate {
193*0Sstevel@tonic-gate caddr_t origin = str->s_type.SF.s_release_origin;
194*0Sstevel@tonic-gate size_t release = 0;
195*0Sstevel@tonic-gate
196*0Sstevel@tonic-gate while ((caddr_t)((ulong_t)str->s_current.l_data.sp & ALIGNED) -
197*0Sstevel@tonic-gate (origin + release) >= DEFAULT_RELEASE_SIZE)
198*0Sstevel@tonic-gate release += DEFAULT_RELEASE_SIZE;
199*0Sstevel@tonic-gate
200*0Sstevel@tonic-gate if (release == 0)
201*0Sstevel@tonic-gate return;
202*0Sstevel@tonic-gate
203*0Sstevel@tonic-gate if (madvise(origin, release, MADV_DONTNEED) == -1)
204*0Sstevel@tonic-gate warn(gettext("madvise failed"));
205*0Sstevel@tonic-gate
206*0Sstevel@tonic-gate str->s_type.SF.s_release_origin += release;
207*0Sstevel@tonic-gate }
208*0Sstevel@tonic-gate
209*0Sstevel@tonic-gate const stream_ops_t stream_mmap_ops = {
210*0Sstevel@tonic-gate stream_mmap_is_closable,
211*0Sstevel@tonic-gate stream_mmap_close,
212*0Sstevel@tonic-gate stream_mmap_eos,
213*0Sstevel@tonic-gate stream_mmap_fetch,
214*0Sstevel@tonic-gate NULL,
215*0Sstevel@tonic-gate stream_mmap_free,
216*0Sstevel@tonic-gate NULL,
217*0Sstevel@tonic-gate stream_mmap_prime,
218*0Sstevel@tonic-gate NULL,
219*0Sstevel@tonic-gate stream_mmap_release_line,
220*0Sstevel@tonic-gate NULL,
221*0Sstevel@tonic-gate stream_stdio_unlink
222*0Sstevel@tonic-gate };
223