2 this measures the ping-pong byte range lock latency. It is
3 especially useful on a cluster of nodes sharing a common lock
4 manager as it will give some indication of the lock managers
5 performance under stress
7 tridge@samba.org, February 2002
22 static struct timeval tp1,tp2;
24 static int do_reads, do_writes, use_mmap;
26 static void start_timer()
28 gettimeofday(&tp1,NULL);
31 static double end_timer()
33 gettimeofday(&tp2,NULL);
34 return (tp2.tv_sec + (tp2.tv_usec*1.0e-6)) -
35 (tp1.tv_sec + (tp1.tv_usec*1.0e-6));
38 /* lock a byte range in a open file */
39 static int lock_range(int fd, int offset, int len)
43 lock.l_type = F_WRLCK;
44 lock.l_whence = SEEK_SET;
45 lock.l_start = offset;
49 return fcntl(fd,F_SETLKW,&lock);
52 /* unlock a byte range in a open file */
53 static int unlock_range(int fd, int offset, int len)
57 lock.l_type = F_UNLCK;
58 lock.l_whence = SEEK_SET;
59 lock.l_start = offset;
63 return fcntl(fd,F_SETLKW,&lock);
66 /* run the ping pong test on fd */
67 static void ping_pong(int fd, int num_locks)
72 unsigned char incr=0, last_incr=0;
73 unsigned char *p = NULL;
75 ftruncate(fd, num_locks+1);
78 p = mmap(NULL, num_locks+1, PROT_READ|PROT_WRITE, MAP_SHARED, fd, 0);
81 val = (unsigned char *)calloc(num_locks+1, sizeof(unsigned char));
89 if (lock_range(fd, (i+1) % num_locks, 1) != 0) {
90 printf("lock at %d failed! - %s\n",
91 (i+1) % num_locks, strerror(errno));
97 } else if (pread(fd, &c, 1, i) != 1) {
98 printf("read failed at %d\n", i);
107 } else if (pwrite(fd, &c, 1, i) != 1) {
108 printf("write failed at %d\n", i);
111 if (unlock_range(fd, i, 1) != 0) {
112 printf("unlock at %d failed! - %s\n",
115 i = (i+1) % num_locks;
117 if (loops > num_locks && incr != last_incr) {
119 printf("data increment = %u\n", incr);
122 if (end_timer() > 1.0) {
123 printf("%8u locks/sec\r",
124 (unsigned)(2*count/end_timer()));
133 int main(int argc, char *argv[])
139 while ((c = getopt(argc, argv, "rwm")) != -1) {
151 fprintf(stderr, "Unknown option '%c'\n", c);
160 printf("ping_pong [options] <file> <num_locks>\n");
161 printf(" -r do reads\n");
162 printf(" -w do writes\n");
163 printf(" -m use mmap\n");
168 num_locks = atoi(argv[1]);
170 fd = open(fname, O_CREAT|O_RDWR, 0600);
171 if (fd == -1) exit(1);
173 ping_pong(fd, num_locks);