17c478bdstevel@tonic-gate/*
27c478bdstevel@tonic-gate * CDDL HEADER START
37c478bdstevel@tonic-gate *
47c478bdstevel@tonic-gate * The contents of this file are subject to the terms of the
57c478bdstevel@tonic-gate * Common Development and Distribution License, Version 1.0 only
67c478bdstevel@tonic-gate * (the "License").  You may not use this file except in compliance
77c478bdstevel@tonic-gate * with the License.
87c478bdstevel@tonic-gate *
97c478bdstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
107c478bdstevel@tonic-gate * or http://www.opensolaris.org/os/licensing.
117c478bdstevel@tonic-gate * See the License for the specific language governing permissions
127c478bdstevel@tonic-gate * and limitations under the License.
137c478bdstevel@tonic-gate *
147c478bdstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each
157c478bdstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
167c478bdstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the
177c478bdstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying
187c478bdstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner]
197c478bdstevel@tonic-gate *
207c478bdstevel@tonic-gate * CDDL HEADER END
217c478bdstevel@tonic-gate */
227c478bdstevel@tonic-gate/*
237c478bdstevel@tonic-gate * Copyright 2004 Sun Microsystems, Inc.  All rights reserved.
247c478bdstevel@tonic-gate * Use is subject to license terms.
257c478bdstevel@tonic-gate */
267c478bdstevel@tonic-gate
277c478bdstevel@tonic-gate#pragma ident	"%Z%%M%	%I%	%E% SMI"
287c478bdstevel@tonic-gate
297c478bdstevel@tonic-gate#include "streams_mmap.h"
307c478bdstevel@tonic-gate#include "streams_common.h"
317c478bdstevel@tonic-gate
327c478bdstevel@tonic-gate/*
337c478bdstevel@tonic-gate * Single-byte character memory map-based streams implementation
347c478bdstevel@tonic-gate */
357c478bdstevel@tonic-gate
367c478bdstevel@tonic-gatestatic int
377c478bdstevel@tonic-gatestream_mmap_prime(stream_t *str)
387c478bdstevel@tonic-gate{
397c478bdstevel@tonic-gate	char *nl;
407c478bdstevel@tonic-gate
417c478bdstevel@tonic-gate	if (stream_is_primed(str))
427c478bdstevel@tonic-gate		return (PRIME_SUCCEEDED);
437c478bdstevel@tonic-gate
447c478bdstevel@tonic-gate	stream_set(str, STREAM_PRIMED);
457c478bdstevel@tonic-gate
467c478bdstevel@tonic-gate	if (str->s_buffer_size == 0) {
477c478bdstevel@tonic-gate		stream_set(str, STREAM_EOS_REACHED);
487c478bdstevel@tonic-gate		return (PRIME_FAILED_EMPTY_FILE);
497c478bdstevel@tonic-gate	}
507c478bdstevel@tonic-gate
517c478bdstevel@tonic-gate	str->s_current.l_data.sp = str->s_buffer;
527c478bdstevel@tonic-gate	str->s_type.SF.s_release_origin = str->s_buffer;
537c478bdstevel@tonic-gate	if ((nl = (char *)memchr(str->s_buffer, '\n', str->s_buffer_size)) ==
547c478bdstevel@tonic-gate	    NULL) {
557c478bdstevel@tonic-gate		warn(WMSG_NEWLINE_ADDED, str->s_filename);
567c478bdstevel@tonic-gate		str->s_current.l_data_length = str->s_buffer_size;
577c478bdstevel@tonic-gate	} else {
587c478bdstevel@tonic-gate		str->s_current.l_data_length = nl - (char *)str->s_buffer;
597c478bdstevel@tonic-gate	}
607c478bdstevel@tonic-gate
617c478bdstevel@tonic-gate	str->s_current.l_collate.sp = NULL;
627c478bdstevel@tonic-gate	str->s_current.l_collate_length = 0;
637c478bdstevel@tonic-gate
647c478bdstevel@tonic-gate	__S(stats_incr_fetches());
657c478bdstevel@tonic-gate	return (PRIME_SUCCEEDED);
667c478bdstevel@tonic-gate}
677c478bdstevel@tonic-gate
687c478bdstevel@tonic-gate/*
697c478bdstevel@tonic-gate * stream_mmap_fetch() sets the fields of str->s_current to delimit the next
707c478bdstevel@tonic-gate * line of the field.
717c478bdstevel@tonic-gate */
727c478bdstevel@tonic-gatestatic ssize_t
737c478bdstevel@tonic-gatestream_mmap_fetch(stream_t *str)
747c478bdstevel@tonic-gate{
757c478bdstevel@tonic-gate	ssize_t dist_to_buf_end;
767c478bdstevel@tonic-gate	char *next_nl;
777c478bdstevel@tonic-gate
787c478bdstevel@tonic-gate	ASSERT(stream_is_primed(str));
797c478bdstevel@tonic-gate	ASSERT((str->s_status & STREAM_EOS_REACHED) == 0);
807c478bdstevel@tonic-gate
817c478bdstevel@tonic-gate	/*
827c478bdstevel@tonic-gate	 * adding one for newline
837c478bdstevel@tonic-gate	 */
847c478bdstevel@tonic-gate	str->s_current.l_data.sp = str->s_current.l_data.sp +
857c478bdstevel@tonic-gate	    str->s_current.l_data_length + 1;
867c478bdstevel@tonic-gate
877c478bdstevel@tonic-gate	dist_to_buf_end = str->s_buffer_size - (str->s_current.l_data.sp
887c478bdstevel@tonic-gate	    - (char *)str->s_buffer);
897c478bdstevel@tonic-gate	ASSERT(dist_to_buf_end >= 0 && dist_to_buf_end <= str->s_buffer_size);
907c478bdstevel@tonic-gate
917c478bdstevel@tonic-gate	next_nl = memchr(str->s_current.l_data.sp, '\n', dist_to_buf_end);
927c478bdstevel@tonic-gate
937c478bdstevel@tonic-gate	if (next_nl)
947c478bdstevel@tonic-gate		str->s_current.l_data_length = next_nl
957c478bdstevel@tonic-gate		    - str->s_current.l_data.sp;
967c478bdstevel@tonic-gate	else {
977c478bdstevel@tonic-gate		warn(WMSG_NEWLINE_ADDED, str->s_filename);
987c478bdstevel@tonic-gate		str->s_current.l_data_length = dist_to_buf_end;
997c478bdstevel@tonic-gate	}
1007c478bdstevel@tonic-gate
1017c478bdstevel@tonic-gate	/*
1027c478bdstevel@tonic-gate	 * adding one for newline
1037c478bdstevel@tonic-gate	 */
1047c478bdstevel@tonic-gate	if (str->s_current.l_data.sp + str->s_current.l_data_length + 1 >=
1057c478bdstevel@tonic-gate	    (char *)str->s_buffer + str->s_buffer_size)
1067c478bdstevel@tonic-gate		stream_set(str, STREAM_EOS_REACHED);
1077c478bdstevel@tonic-gate
1087c478bdstevel@tonic-gate	str->s_current.l_collate_length = 0;
1097c478bdstevel@tonic-gate
1107c478bdstevel@tonic-gate	__S(stats_incr_fetches());
1117c478bdstevel@tonic-gate	return (NEXT_LINE_COMPLETE);
1127c478bdstevel@tonic-gate}
1137c478bdstevel@tonic-gate
1147c478bdstevel@tonic-gatestatic int
1157c478bdstevel@tonic-gatestream_mmap_is_closable(stream_t *str)
1167c478bdstevel@tonic-gate{
1177c478bdstevel@tonic-gate	if (str->s_status & STREAM_OPEN)
1187c478bdstevel@tonic-gate		return (1);
1197c478bdstevel@tonic-gate	return (0);
1207c478bdstevel@tonic-gate}
1217c478bdstevel@tonic-gate
1227c478bdstevel@tonic-gatestatic int
1237c478bdstevel@tonic-gatestream_mmap_close(stream_t *str)
1247c478bdstevel@tonic-gate{
1257c478bdstevel@tonic-gate	if (str->s_type.SF.s_fd > -1) {
1267c478bdstevel@tonic-gate		(void) close(str->s_type.SF.s_fd);
1277c478bdstevel@tonic-gate		stream_unset(str, STREAM_OPEN);
1287c478bdstevel@tonic-gate		return (1);
1297c478bdstevel@tonic-gate	}
1307c478bdstevel@tonic-gate
1317c478bdstevel@tonic-gate	return (0);
1327c478bdstevel@tonic-gate}
1337c478bdstevel@tonic-gate
1347c478bdstevel@tonic-gatestatic int
1357c478bdstevel@tonic-gatestream_mmap_free(stream_t *str)
1367c478bdstevel@tonic-gate{
1377c478bdstevel@tonic-gate	if (!(str->s_status & STREAM_OPEN) ||
1387c478bdstevel@tonic-gate	    (str->s_consumer != NULL &&
1397c478bdstevel@tonic-gate	    str->s_consumer->s_status & STREAM_NOT_FREEABLE))
1407c478bdstevel@tonic-gate		return (0);
1417c478bdstevel@tonic-gate
1427c478bdstevel@tonic-gate	if (str->s_buffer == NULL)
1437c478bdstevel@tonic-gate		return (1);
1447c478bdstevel@tonic-gate
1457c478bdstevel@tonic-gate	if (munmap(str->s_buffer, str->s_buffer_size) < 0)
1467c478bdstevel@tonic-gate		die(EMSG_MUNMAP, str->s_filename);
1477c478bdstevel@tonic-gate
1487c478bdstevel@tonic-gate	str->s_buffer = NULL;
1497c478bdstevel@tonic-gate	str->s_buffer_size = 0;
1507c478bdstevel@tonic-gate
1517c478bdstevel@tonic-gate	stream_unset(str, STREAM_PRIMED);
1527c478bdstevel@tonic-gate
1537c478bdstevel@tonic-gate	return (1);
1547c478bdstevel@tonic-gate}
1557c478bdstevel@tonic-gate
1567c478bdstevel@tonic-gatestatic int
1577c478bdstevel@tonic-gatestream_mmap_eos(stream_t *str)
1587c478bdstevel@tonic-gate{
1597c478bdstevel@tonic-gate	int retval = 0;
1607c478bdstevel@tonic-gate
1617c478bdstevel@tonic-gate	if (str == NULL || str->s_status & STREAM_EOS_REACHED)
1627c478bdstevel@tonic-gate		return (1);
1637c478bdstevel@tonic-gate
1647c478bdstevel@tonic-gate	/*
1657c478bdstevel@tonic-gate	 * If the file's size is known to be zero, then we are at EOS; the
1667c478bdstevel@tonic-gate	 * remaining checks are only sensible if we successfully primed this
1677c478bdstevel@tonic-gate	 * stream.  The additional character is for the optional newline.
1687c478bdstevel@tonic-gate	 */
1697c478bdstevel@tonic-gate	if (str->s_filesize == 0 ||
1707c478bdstevel@tonic-gate	    (stream_is_primed(str) && str->s_current.l_data.sp -
1717c478bdstevel@tonic-gate	    (char *)str->s_buffer + str->s_current.l_data_length + 1 >=
1727c478bdstevel@tonic-gate	    str->s_buffer_size)) {
1737c478bdstevel@tonic-gate		retval = 1;
1747c478bdstevel@tonic-gate		stream_set(str, STREAM_EOS_REACHED);
1757c478bdstevel@tonic-gate	}
1767c478bdstevel@tonic-gate
1777c478bdstevel@tonic-gate	return (retval);
1787c478bdstevel@tonic-gate}
1797c478bdstevel@tonic-gate
1807c478bdstevel@tonic-gate#define	ALIGNED		(~(ulong_t)(PAGESIZE - 1))
1817c478bdstevel@tonic-gate
1827c478bdstevel@tonic-gate/*
1837c478bdstevel@tonic-gate * In certain cases, we know that we will never need the data on a page again
1847c478bdstevel@tonic-gate * for the duration of the sort.  (These cases are associated with merges
1857c478bdstevel@tonic-gate * involving temporary files.)  We can thus release all pages previous to the
1867c478bdstevel@tonic-gate * page containing the current line, using the MADV_DONTNEED flag to
1877c478bdstevel@tonic-gate * madvise(3C).  This additional memory management improves our chances of
1887c478bdstevel@tonic-gate * avoiding a paging situation, by evicting pages we know are of no use.
1897c478bdstevel@tonic-gate */
1907c478bdstevel@tonic-gatestatic void
1917c478bdstevel@tonic-gatestream_mmap_release_line(stream_t *str)
1927c478bdstevel@tonic-gate{
1937c478bdstevel@tonic-gate	caddr_t origin = str->s_type.SF.s_release_origin;
1947c478bdstevel@tonic-gate	size_t release = 0;
1957c478bdstevel@tonic-gate
1967c478bdstevel@tonic-gate	while ((caddr_t)((ulong_t)str->s_current.l_data.sp & ALIGNED) -
1977c478bdstevel@tonic-gate	    (origin + release) >= DEFAULT_RELEASE_SIZE)
1987c478bdstevel@tonic-gate		release += DEFAULT_RELEASE_SIZE;
1997c478bdstevel@tonic-gate
2007c478bdstevel@tonic-gate	if (release == 0)
2017c478bdstevel@tonic-gate		return;
2027c478bdstevel@tonic-gate
2037c478bdstevel@tonic-gate	if (madvise(origin, release, MADV_DONTNEED) == -1)
2047c478bdstevel@tonic-gate		warn(gettext("madvise failed"));
2057c478bdstevel@tonic-gate
2067c478bdstevel@tonic-gate	str->s_type.SF.s_release_origin += release;
2077c478bdstevel@tonic-gate}
2087c478bdstevel@tonic-gate
2097c478bdstevel@tonic-gateconst stream_ops_t stream_mmap_ops = {
2107c478bdstevel@tonic-gate	stream_mmap_is_closable,
2117c478bdstevel@tonic-gate	stream_mmap_close,
2127c478bdstevel@tonic-gate	stream_mmap_eos,
2137c478bdstevel@tonic-gate	stream_mmap_fetch,
2147c478bdstevel@tonic-gate	NULL,
2157c478bdstevel@tonic-gate	stream_mmap_free,
2167c478bdstevel@tonic-gate	NULL,
2177c478bdstevel@tonic-gate	stream_mmap_prime,
2187c478bdstevel@tonic-gate	NULL,
2197c478bdstevel@tonic-gate	stream_mmap_release_line,
2207c478bdstevel@tonic-gate	NULL,
2217c478bdstevel@tonic-gate	stream_stdio_unlink
2227c478bdstevel@tonic-gate};
223