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  * $FreeBSD$
21  */
22 
23 /*
24  * Copyright 2007 Sun Microsystems, Inc.  All rights reserved.
25  * Use is subject to license terms.
26  */
27 
28 #pragma ident	"@(#)threadsappend.c	1.3	07/05/25 SMI"
29 
30 #include <sys/types.h>
31 #include <sys/stat.h>
32 #include <fcntl.h>
33 #include <pthread.h>
34 #include <string.h>
35 #include <stdio.h>
36 #include <unistd.h>
37 #include <stdlib.h>
38 #include <errno.h>
39 
40 /*
41  * The size of the output file, "go.out", should be 80*8192*2 = 1310720
42  *
43  * $ cd /tmp; go; ls -l go.out
44  * done.
45  * -rwxr-xr-x	1 jdm	staff	1310720 Apr 13 19:45 go.out
46  * $ cd /zfs; go; ls -l go.out
47  * done.
48  * -rwxr-xr-x	1 jdm	staff	663552 Apr 13 19:45 go.out
49  *
50  * The file on zfs is short as it does not appear that zfs is making the
51  * implicit seek to EOF and the actual write atomic. From the SUSv3
52  * interface spec, behavior is undefined if concurrent writes are performed
53  * from multi-processes to a single file. So I don't know if this is a
54  * standards violation, but I cannot find any such disclaimers in our
55  * man pages. This issue came up at a customer site in another context, and
56  * the suggestion was to open the file with O_APPEND, but that wouldn't
57  * help with zfs(see 4977529). Also see bug# 5031301.
58  */
59 
60 static int outfd = 0;
61 
62 static void *
63 go(void *data)
64 {
65 	int i = 0, n = *(int *)data;
66 	ssize_t	ret = 0;
67 	char buf[8192] = {0};
68 	(void) memset(buf, n, sizeof (buf));
69 
70 	for (i = 0; i < 80; i++) {
71 		ret = write(outfd, buf, sizeof (buf));
72 	}
73 	return (NULL);
74 }
75 
76 static void
77 usage()
78 {
79 	(void) fprintf(stderr,
80 	    "usage: zfs_threadsappend <file name>\n");
81 	exit(1);
82 }
83 
84 int
85 main(int argc, char **argv)
86 {
87 	pthread_t threads[2];
88 	int	  ret = 0;
89 	long	  ncpus = 0;
90 	int	  i;
91 
92 	if (argc != 2) {
93 		usage();
94 	}
95 
96 	ncpus = sysconf(_SC_NPROCESSORS_ONLN);
97 	if (ncpus < 0) {
98 		(void) fprintf(stderr,
99 		    "Invalid return from sysconf(_SC_NPROCESSORS_ONLN)"
100 		    " : errno (decimal)=%d\n", errno);
101 		exit(1);
102 	}
103 	if (ncpus < 2) {
104 		(void) fprintf(stderr,
105 		    "Must execute this binary on a multi-processor system\n");
106 		exit(1);
107 	}
108 
109 	outfd = open(argv[optind++], O_RDWR|O_CREAT|O_APPEND|O_TRUNC, 0777);
110 	if (outfd == -1) {
111 		(void) fprintf(stderr,
112 		    "zfs_threadsappend: "
113 		    "open(%s, O_RDWR|O_CREAT|O_APPEND|O_TRUNC, 0777)"
114 		    " failed\n", argv[optind]);
115 		perror("open");
116 		exit(1);
117 	}
118 
119 	for (i = 0; i < 2; i++) {
120 		ret = pthread_create(&threads[i], NULL, go, (void *)&i);
121 		if (ret != 0) {
122 			(void) fprintf(stderr,
123 			    "zfs_threadsappend: thr_create(#%d) "
124 			    "failed error=%d\n", i+1, ret);
125 			exit(1);
126 		}
127 	}
128 
129 	for (i = 0; i < 2; i++) {
130 		if (pthread_join(threads[i], NULL) != 0)
131 			break;
132 	}
133 
134 	return (0);
135 }
136