xref: /onnv-gate/usr/src/cmd/filebench/common/multi_client_sync.c (revision 7736:affefef63864)
1*7736SAndrew.W.Wilson@sun.com /*
2*7736SAndrew.W.Wilson@sun.com  * CDDL HEADER START
3*7736SAndrew.W.Wilson@sun.com  *
4*7736SAndrew.W.Wilson@sun.com  * The contents of this file are subject to the terms of the
5*7736SAndrew.W.Wilson@sun.com  * Common Development and Distribution License (the "License").
6*7736SAndrew.W.Wilson@sun.com  * You may not use this file except in compliance with the License.
7*7736SAndrew.W.Wilson@sun.com  *
8*7736SAndrew.W.Wilson@sun.com  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9*7736SAndrew.W.Wilson@sun.com  * or http://www.opensolaris.org/os/licensing.
10*7736SAndrew.W.Wilson@sun.com  * See the License for the specific language governing permissions
11*7736SAndrew.W.Wilson@sun.com  * and limitations under the License.
12*7736SAndrew.W.Wilson@sun.com  *
13*7736SAndrew.W.Wilson@sun.com  * When distributing Covered Code, include this CDDL HEADER in each
14*7736SAndrew.W.Wilson@sun.com  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15*7736SAndrew.W.Wilson@sun.com  * If applicable, add the following below this CDDL HEADER, with the
16*7736SAndrew.W.Wilson@sun.com  * fields enclosed by brackets "[]" replaced with your own identifying
17*7736SAndrew.W.Wilson@sun.com  * information: Portions Copyright [yyyy] [name of copyright owner]
18*7736SAndrew.W.Wilson@sun.com  *
19*7736SAndrew.W.Wilson@sun.com  * CDDL HEADER END
20*7736SAndrew.W.Wilson@sun.com  */
21*7736SAndrew.W.Wilson@sun.com /*
22*7736SAndrew.W.Wilson@sun.com  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
23*7736SAndrew.W.Wilson@sun.com  * Use is subject to license terms.
24*7736SAndrew.W.Wilson@sun.com  */
25*7736SAndrew.W.Wilson@sun.com 
26*7736SAndrew.W.Wilson@sun.com 
27*7736SAndrew.W.Wilson@sun.com #include "filebench.h"
28*7736SAndrew.W.Wilson@sun.com #include "multi_client_sync.h"
29*7736SAndrew.W.Wilson@sun.com #include <netdb.h>
30*7736SAndrew.W.Wilson@sun.com #include <netinet/in.h>
31*7736SAndrew.W.Wilson@sun.com #include <arpa/inet.h>
32*7736SAndrew.W.Wilson@sun.com #include <errno.h>
33*7736SAndrew.W.Wilson@sun.com 
34*7736SAndrew.W.Wilson@sun.com #define	MCS_NAMELENGTH	128
35*7736SAndrew.W.Wilson@sun.com #define	MCS_MSGLENGTH	(MCS_NAMELENGTH * 8)
36*7736SAndrew.W.Wilson@sun.com 
37*7736SAndrew.W.Wilson@sun.com static int mc_sync_sock_id;
38*7736SAndrew.W.Wilson@sun.com static char this_client_name[MCS_NAMELENGTH];
39*7736SAndrew.W.Wilson@sun.com 
40*7736SAndrew.W.Wilson@sun.com /*
41*7736SAndrew.W.Wilson@sun.com  * Open a socket to the master synchronization host
42*7736SAndrew.W.Wilson@sun.com  */
43*7736SAndrew.W.Wilson@sun.com int
mc_sync_open_sock(char * master_name,int master_port,char * my_name)44*7736SAndrew.W.Wilson@sun.com mc_sync_open_sock(char *master_name, int master_port, char *my_name)
45*7736SAndrew.W.Wilson@sun.com {
46*7736SAndrew.W.Wilson@sun.com 	struct sockaddr_in client_in;
47*7736SAndrew.W.Wilson@sun.com 	struct sockaddr_in master_in;
48*7736SAndrew.W.Wilson@sun.com 	struct hostent master_info;
49*7736SAndrew.W.Wilson@sun.com 	int error_num;
50*7736SAndrew.W.Wilson@sun.com 	char buffer[MCS_MSGLENGTH];
51*7736SAndrew.W.Wilson@sun.com 
52*7736SAndrew.W.Wilson@sun.com 	(void) strncpy(this_client_name, my_name, MCS_NAMELENGTH);
53*7736SAndrew.W.Wilson@sun.com 	if ((mc_sync_sock_id = socket(PF_INET, SOCK_STREAM, 0)) == -1) {
54*7736SAndrew.W.Wilson@sun.com 		filebench_log(LOG_ERROR, "could not create a client socket");
55*7736SAndrew.W.Wilson@sun.com 		return (FILEBENCH_ERROR);
56*7736SAndrew.W.Wilson@sun.com 	}
57*7736SAndrew.W.Wilson@sun.com 
58*7736SAndrew.W.Wilson@sun.com 	client_in.sin_family = AF_INET;
59*7736SAndrew.W.Wilson@sun.com 	client_in.sin_port = INADDR_ANY;
60*7736SAndrew.W.Wilson@sun.com 	client_in.sin_addr.s_addr = INADDR_ANY;
61*7736SAndrew.W.Wilson@sun.com 
62*7736SAndrew.W.Wilson@sun.com 	if (bind(mc_sync_sock_id, (struct sockaddr *)&client_in,
63*7736SAndrew.W.Wilson@sun.com 	    sizeof (client_in)) == -1) {
64*7736SAndrew.W.Wilson@sun.com 		filebench_log(LOG_ERROR, "could not bind to client socket");
65*7736SAndrew.W.Wilson@sun.com 		return (FILEBENCH_ERROR);
66*7736SAndrew.W.Wilson@sun.com 	}
67*7736SAndrew.W.Wilson@sun.com 
68*7736SAndrew.W.Wilson@sun.com 	if (gethostbyname_r(master_name, &master_info, buffer, MCS_MSGLENGTH,
69*7736SAndrew.W.Wilson@sun.com 	    &error_num) == NULL) {
70*7736SAndrew.W.Wilson@sun.com 		filebench_log(LOG_ERROR, "could not locate sync master");
71*7736SAndrew.W.Wilson@sun.com 		return (FILEBENCH_ERROR);
72*7736SAndrew.W.Wilson@sun.com 	}
73*7736SAndrew.W.Wilson@sun.com 
74*7736SAndrew.W.Wilson@sun.com 	master_in.sin_family = AF_INET;
75*7736SAndrew.W.Wilson@sun.com 	master_in.sin_port = htons((uint16_t)master_port);
76*7736SAndrew.W.Wilson@sun.com 	(void) memcpy(&master_in.sin_addr.s_addr, *master_info.h_addr_list,
77*7736SAndrew.W.Wilson@sun.com 	    sizeof (master_in.sin_addr.s_addr));
78*7736SAndrew.W.Wilson@sun.com 
79*7736SAndrew.W.Wilson@sun.com 	if (connect(mc_sync_sock_id, (struct sockaddr *)&master_in,
80*7736SAndrew.W.Wilson@sun.com 	    sizeof (master_in)) == -1) {
81*7736SAndrew.W.Wilson@sun.com 		filebench_log(LOG_ERROR,
82*7736SAndrew.W.Wilson@sun.com 		    "connection refused to sync master, error %d", errno);
83*7736SAndrew.W.Wilson@sun.com 		return (FILEBENCH_ERROR);
84*7736SAndrew.W.Wilson@sun.com 	}
85*7736SAndrew.W.Wilson@sun.com 
86*7736SAndrew.W.Wilson@sun.com 	return (FILEBENCH_OK);
87*7736SAndrew.W.Wilson@sun.com }
88*7736SAndrew.W.Wilson@sun.com 
89*7736SAndrew.W.Wilson@sun.com /*
90*7736SAndrew.W.Wilson@sun.com  * Send a synchronization message and wait for a reply
91*7736SAndrew.W.Wilson@sun.com  */
92*7736SAndrew.W.Wilson@sun.com int
mc_sync_synchronize(int sync_point)93*7736SAndrew.W.Wilson@sun.com mc_sync_synchronize(int sync_point)
94*7736SAndrew.W.Wilson@sun.com {
95*7736SAndrew.W.Wilson@sun.com 	char msg[MCS_MSGLENGTH];
96*7736SAndrew.W.Wilson@sun.com 	int amnt;
97*7736SAndrew.W.Wilson@sun.com 
98*7736SAndrew.W.Wilson@sun.com 	(void) snprintf(msg, MCS_MSGLENGTH,
99*7736SAndrew.W.Wilson@sun.com 	    "cmd=SYNC,id=xyzzy,name=%s,sample=%d\n",
100*7736SAndrew.W.Wilson@sun.com 	    this_client_name, sync_point);
101*7736SAndrew.W.Wilson@sun.com 	(void) send(mc_sync_sock_id, msg, strlen(msg), 0);
102*7736SAndrew.W.Wilson@sun.com 
103*7736SAndrew.W.Wilson@sun.com 	amnt = 0;
104*7736SAndrew.W.Wilson@sun.com 	msg[0] = 0;
105*7736SAndrew.W.Wilson@sun.com 
106*7736SAndrew.W.Wilson@sun.com 	while (strchr(msg, '\n') == NULL)
107*7736SAndrew.W.Wilson@sun.com 		amnt += recv(mc_sync_sock_id, msg, sizeof (msg), 0);
108*7736SAndrew.W.Wilson@sun.com 
109*7736SAndrew.W.Wilson@sun.com 	filebench_log(LOG_INFO, "sync point %d succeeded!\n", sync_point);
110*7736SAndrew.W.Wilson@sun.com 	return (FILEBENCH_OK);
111*7736SAndrew.W.Wilson@sun.com }
112