kcc: Add a TODO for is_bridgehead_failed
[Samba.git] / ctdb / utils / ping_pong / ping_pong.c
blobfdb575daae29c305c12d978024e01b32580f53bc
1 /*
2 A ping-pong fcntl byte range lock test
4 Copyright (C) Andrew Tridgell 2002
5 Copyright (C) Michael Adam 2012
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, see <http://www.gnu.org/licenses/>.
22 This measures the ping-pong byte range lock latency. It is
23 especially useful on a cluster of nodes sharing a common lock
24 manager as it will give some indication of the lock managers
25 performance under stress.
27 tridge@samba.org, February 2002
31 #define _XOPEN_SOURCE 500
33 #include <stdio.h>
34 #include <stdlib.h>
35 #include <sys/time.h>
36 #include <time.h>
37 #include <errno.h>
38 #include <string.h>
39 #include <unistd.h>
40 #include <fcntl.h>
41 #include <sys/mman.h>
43 static struct timeval tp1,tp2;
45 static int do_reads, do_writes, use_mmap, do_check;
47 static void start_timer(void)
49 gettimeofday(&tp1,NULL);
52 static double end_timer(void)
54 gettimeofday(&tp2,NULL);
55 return (tp2.tv_sec + (tp2.tv_usec*1.0e-6)) -
56 (tp1.tv_sec + (tp1.tv_usec*1.0e-6));
59 /* lock a byte range in a open file */
60 static int lock_range(int fd, int offset, int len)
62 struct flock lock;
64 lock.l_type = F_WRLCK;
65 lock.l_whence = SEEK_SET;
66 lock.l_start = offset;
67 lock.l_len = len;
68 lock.l_pid = 0;
70 return fcntl(fd,F_SETLKW,&lock);
73 /* check whether we could place a lock */
74 static int check_lock(int fd, int offset, int len)
76 struct flock lock;
77 int ret;
79 lock.l_type = F_WRLCK;
80 lock.l_whence = SEEK_SET;
81 lock.l_start = offset;
82 lock.l_len = len;
83 lock.l_pid = 0;
85 ret = fcntl(fd, F_GETLK, &lock);
86 if (ret != 0) {
87 printf("error calling fcntl F_GETLCK: %s\n", strerror(errno));
88 return -1;
91 if (lock.l_type == F_UNLCK) {
92 /* we would be able to place the lock */
93 return 0;
96 /* we would not be able to place lock */
97 printf("check_lock failed: lock held: "
98 "pid='%d', type='%d', start='%d', len='%d'\n",
99 (int)lock.l_pid, (int)lock.l_type, (int)lock.l_start, (int)lock.l_len);
100 return 1;
103 /* unlock a byte range in a open file */
104 static int unlock_range(int fd, int offset, int len)
106 struct flock lock;
108 lock.l_type = F_UNLCK;
109 lock.l_whence = SEEK_SET;
110 lock.l_start = offset;
111 lock.l_len = len;
112 lock.l_pid = 0;
114 return fcntl(fd,F_SETLKW,&lock);
117 /* run the ping pong test on fd */
118 static void ping_pong(int fd, int num_locks)
120 unsigned count = 0;
121 int i=0, loops=0;
122 unsigned char *val;
123 unsigned char incr=0, last_incr=0;
124 unsigned char *p = NULL;
125 int ret;
127 ret = ftruncate(fd, num_locks+1);
128 if (ret == -1) {
129 printf("ftruncate failed: %s\n", strerror(errno));
130 return;
133 if (use_mmap) {
134 p = mmap(NULL, num_locks+1, PROT_READ|PROT_WRITE, MAP_SHARED, fd, 0);
135 if (p == MAP_FAILED) {
136 printf("mmap failed: %s\n", strerror(errno));
137 return;
141 val = (unsigned char *)calloc(num_locks+1, sizeof(unsigned char));
142 if (val == NULL) {
143 printf("calloc failed\n");
144 munmap(p, num_locks+1);
145 return;
148 start_timer();
150 lock_range(fd, 0, 1);
151 i = 0;
153 while (1) {
154 if (lock_range(fd, (i+1) % num_locks, 1) != 0) {
155 printf("lock at %d failed! - %s\n",
156 (i+1) % num_locks, strerror(errno));
158 if (do_check) {
159 ret = check_lock(fd, i, 1);
161 if (do_reads) {
162 unsigned char c;
163 if (use_mmap) {
164 c = p[i];
165 } else if (pread(fd, &c, 1, i) != 1) {
166 printf("read failed at %d\n", i);
168 incr = c - val[i];
169 val[i] = c;
171 if (do_writes) {
172 char c = val[i] + 1;
173 if (use_mmap) {
174 p[i] = c;
175 } else if (pwrite(fd, &c, 1, i) != 1) {
176 printf("write failed at %d\n", i);
179 if (unlock_range(fd, i, 1) != 0) {
180 printf("unlock at %d failed! - %s\n",
181 i, strerror(errno));
183 i = (i+1) % num_locks;
184 count++;
185 if (loops > num_locks && incr != last_incr) {
186 last_incr = incr;
187 printf("data increment = %u\n", incr);
188 fflush(stdout);
190 if (end_timer() > 1.0) {
191 printf("%8u locks/sec\r",
192 (unsigned)(2*count/end_timer()));
193 fflush(stdout);
194 start_timer();
195 count=0;
197 loops++;
201 int main(int argc, char *argv[])
203 char *fname;
204 int fd, num_locks;
205 int c;
207 while ((c = getopt(argc, argv, "rwmc")) != -1) {
208 switch (c){
209 case 'w':
210 do_writes = 1;
211 break;
212 case 'r':
213 do_reads = 1;
214 break;
215 case 'm':
216 use_mmap = 1;
217 break;
218 case 'c':
219 do_check = 1;
220 break;
221 default:
222 fprintf(stderr, "Unknown option '%c'\n", c);
223 exit(1);
227 argv += optind;
228 argc -= optind;
230 if (argc < 2) {
231 printf("ping_pong [options] <file> <num_locks>\n");
232 printf(" -r do reads\n");
233 printf(" -w do writes\n");
234 printf(" -m use mmap\n");
235 printf(" -c check locks\n");
236 exit(1);
239 fname = argv[0];
240 num_locks = atoi(argv[1]);
241 if (num_locks <= 0) {
242 printf("num_locks should be > 0\n");
243 exit(1);
246 fd = open(fname, O_CREAT|O_RDWR, 0600);
247 if (fd == -1) exit(1);
249 ping_pong(fd, num_locks);
251 return 0;