1/*
2 * CDDL HEADER START
3 *
4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License (the "License").
6 * You may not use this file except in compliance with the License.
7 *
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 * or http://www.opensolaris.org/os/licensing.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
12 *
13 * When distributing Covered Code, include this CDDL HEADER in each
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 * If applicable, add the following below this CDDL HEADER, with the
16 * fields enclosed by brackets "[]" replaced with your own identifying
17 * information: Portions Copyright [yyyy] [name of copyright owner]
18 *
19 * CDDL HEADER END
20 */
21
22/*
23 * Copyright 2007 Sun Microsystems, Inc.  All rights reserved.
24 * Use is subject to license terms.
25 */
26
27
28#include <sys/types.h>
29#include <sys/stat.h>
30#include <fcntl.h>
31#include <pthread.h>
32#include <string.h>
33#include <stdio.h>
34#include <unistd.h>
35#include <stdlib.h>
36#include <errno.h>
37
38/*
39 * The size of the output file, "go.out", should be 80*8192*2 = 1310720
40 *
41 * $ cd /tmp; go; ls -l go.out
42 * done.
43 * -rwxr-xr-x	1 jdm	staff	1310720 Apr 13 19:45 go.out
44 * $ cd /zfs; go; ls -l go.out
45 * done.
46 * -rwxr-xr-x	1 jdm	staff	663552 Apr 13 19:45 go.out
47 *
48 * The file on zfs is short as it does not appear that zfs is making the
49 * implicit seek to EOF and the actual write atomic. From the SUSv3
50 * interface spec, behavior is undefined if concurrent writes are performed
51 * from multi-processes to a single file. So I don't know if this is a
52 * standards violation, but I cannot find any such disclaimers in our
53 * man pages. This issue came up at a customer site in another context, and
54 * the suggestion was to open the file with O_APPEND, but that wouldn't
55 * help with zfs(see 4977529). Also see bug# 5031301.
56 */
57
58static int outfd = 0;
59
60static void *
61go(void *data)
62{
63	int i = 0, n = *(int *)data;
64	ssize_t	ret = 0;
65	char buf[8192] = {0};
66	(void) memset(buf, n, sizeof (buf));
67
68	for (i = 0; i < 80; i++) {
69		ret = write(outfd, buf, sizeof (buf));
70	}
71	return (NULL);
72}
73
74static void
75usage()
76{
77	(void) fprintf(stderr,
78	    "usage: zfs_threadsappend <file name>\n");
79	exit(1);
80}
81
82int
83main(int argc, char **argv)
84{
85	pthread_t threads[2];
86	int	  ret = 0;
87	long	  ncpus = 0;
88	int	  i;
89
90	if (argc != 2) {
91		usage();
92	}
93
94	ncpus = sysconf(_SC_NPROCESSORS_ONLN);
95	if (ncpus < 0) {
96		(void) fprintf(stderr,
97		    "Invalid return from sysconf(_SC_NPROCESSORS_ONLN)"
98		    " : errno (decimal)=%d\n", errno);
99		exit(1);
100	}
101	if (ncpus < 2) {
102		(void) fprintf(stderr,
103		    "Must execute this binary on a multi-processor system\n");
104		exit(1);
105	}
106
107	outfd = open(argv[optind++], O_RDWR|O_CREAT|O_APPEND|O_TRUNC, 0777);
108	if (outfd == -1) {
109		(void) fprintf(stderr,
110		    "zfs_threadsappend: "
111		    "open(%s, O_RDWR|O_CREAT|O_APPEND|O_TRUNC, 0777)"
112		    " failed\n", argv[optind]);
113		perror("open");
114		exit(1);
115	}
116
117	for (i = 0; i < 2; i++) {
118		ret = pthread_create(&threads[i], NULL, go, (void *)&i);
119		if (ret != 0) {
120			(void) fprintf(stderr,
121			    "zfs_threadsappend: thr_create(#%d) "
122			    "failed error=%d\n", i+1, ret);
123			exit(1);
124		}
125	}
126
127	for (i = 0; i < 2; i++) {
128		if (pthread_join(threads[i], NULL) != 0)
129			break;
130	}
131
132	return (0);
133}
134