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;
76 p = mmap(NULL, num_locks+1, PROT_READ|PROT_WRITE, MAP_SHARED, fd, 0);
79 val = (unsigned char *)calloc(num_locks+1, sizeof(unsigned char));
87 if (lock_range(fd, (i+1) % num_locks, 1) != 0) {
88 printf("lock at %d failed! - %s\n",
89 (i+1) % num_locks, strerror(errno));
95 } if (pread(fd, &c, 1, i) != 1) {
96 printf("read failed at %d\n", i);
105 } else if (pwrite(fd, &c, 1, i) != 1) {
106 printf("write failed at %d\n", i);
109 if (unlock_range(fd, i, 1) != 0) {
110 printf("unlock at %d failed! - %s\n",
113 i = (i+1) % num_locks;
115 if (incr != last_incr) {
117 printf("data increment = %u\n", incr);
120 if (end_timer() > 1.0) {
121 printf("%8u locks/sec\r",
122 (unsigned)(2*count/end_timer()));
130 int main(int argc, char *argv[])
136 while ((c = getopt(argc, argv, "rwm")) != -1) {
148 fprintf(stderr, "Unknown option '%c'\n", c);
157 printf("ping_pong [options] <file> <num_locks>\n");
158 printf(" -r do reads\n");
159 printf(" -w do writes\n");
160 printf(" -m use mmap\n");
165 num_locks = atoi(argv[1]);
167 fd = open(fname, O_CREAT|O_RDWR, 0600);
168 if (fd == -1) exit(1);
170 ping_pong(fd, num_locks);