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