summaryrefslogtreecommitdiffstats
path: root/src/test/messenger/xio_client.cc
blob: c916d4915fb84dbb387b9b2060d893b5e4598fb9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
// -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*-
// vim: ts=8 sw=2 smarttab
/*
 * Ceph - scalable distributed file system
 *
 * Copyright (C) 2013 CohortFS, LLC
 *
 * This is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License version 2.1, as published by the Free Software
 * Foundation.  See file COPYING.
 *
 */

#include <sys/types.h>

#include <iostream>
#include <string>

using namespace std;

#include "common/config.h"
#include "msg/msg_types.h"
#include "msg/xio/XioMessenger.h"
#include "msg/xio/FastStrategy.h"
#include "msg/xio/QueueStrategy.h"
#include "msg/xio/XioMsg.h"
#include "messages/MPing.h"
#include "common/Timer.h"
#include "common/ceph_argparse.h"
#include "global/global_init.h"
#include "perfglue/heap_profiler.h"
#include "common/address_helper.h"
#include "message_helper.h"
#include "xio_dispatcher.h"
#include "msg/xio/XioConnection.h"

#define dout_subsys ceph_subsys_xio_client

void usage(ostream& out)
{
  out << "usage: xio_client [options]\n"
"options:\n"
"  --addr X\n"
"  --port X\n"
"  --msgs X\n"
"  --dsize X\n"
"  --nfrags X\n"
"  --dfast\n"
    ;
}

int main(int argc, const char **argv)
{
	vector<const char*> args;
	Messenger* messenger;
	XioDispatcher *dispatcher;
	std::vector<const char*>::iterator arg_iter;
	std::string val;
	entity_addr_t dest_addr;
	ConnectionRef conn;
	int r = 0;

	std::string addr = "localhost";
	std::string port = "1234";
	int n_msgs = 50;
	int n_dsize = 0;
	int n_nfrags = 1;
	bool dfast = false;

	struct timespec ts;
	ts.tv_sec = 5;
	ts.tv_nsec = 0;

	argv_to_vec(argc, argv, args);

	auto cct = global_init(NULL, args,
			       CEPH_ENTITY_TYPE_ANY,
			       CODE_ENVIRONMENT_UTILITY,
			       CINIT_FLAG_NO_DEFAULT_CONFIG_FILE);

	for (arg_iter = args.begin(); arg_iter != args.end();) {
	  if (ceph_argparse_witharg(args, arg_iter, &val, "--addr",
				    (char*) NULL)) {
	    addr = val;
	  } else if (ceph_argparse_witharg(args, arg_iter, &val, "--port",
				    (char*) NULL)) {
	    port = val;
	  } else if (ceph_argparse_witharg(args, arg_iter, &val, "--msgs",
				    (char*) NULL)) {
	    n_msgs = atoi(val.c_str());
	  } else if (ceph_argparse_witharg(args, arg_iter, &val, "--dsize",
				    (char*) NULL)) {
	    n_dsize = atoi(val.c_str());
	  } else if (ceph_argparse_witharg(args, arg_iter, &val, "--nfrags",
				    (char*) NULL)) {
	    n_nfrags = atoi(val.c_str());
	  } else if (ceph_argparse_flag(args, arg_iter, "--dfast",
					   (char*) NULL)) {
	    dfast = true;
	  } else {
	    ++arg_iter;
	  }
	};

	if (!args.empty()) {
	  cerr << "What is this? -- " << args[0] << std::endl;
	  usage(cerr);
	  exit(1);
	}

	DispatchStrategy* dstrategy;
	if (dfast)
	  dstrategy = new FastStrategy();
	else
	  dstrategy = new QueueStrategy(2);

	messenger = new XioMessenger(g_ceph_context,
				     entity_name_t::MON(-1),
				     "xio_client",
				     0 /* nonce */,
				     0 /* cflags */,
				     dstrategy);

	// enable timing prints
	static_cast<XioMessenger*>(messenger)->set_magic(
	  MSG_MAGIC_REDUPE /* resubmit messages on delivery (REQUIRED) */ |
	  MSG_MAGIC_TRACE_CTR /* timing prints */);

	// ensure we have a pool of sizeof(payload data)
	if (n_dsize)
	  (void) static_cast<XioMessenger*>(messenger)->pool_hint(n_dsize);

	messenger->set_default_policy(Messenger::Policy::lossy_client(0));

	string dest_str = "tcp://";
	dest_str += addr;
	dest_str += ":";
	dest_str += port;
	entity_addr_from_url(&dest_addr, dest_str.c_str());
	entity_inst_t dest_server(entity_name_t::MON(-1), dest_addr);

	dispatcher = new XioDispatcher(messenger);
	messenger->add_dispatcher_head(dispatcher);

	dispatcher->set_active(); // this side is the pinger

	r = messenger->start();
	if (r < 0)
		goto out;

	conn = messenger->get_connection(dest_server);

	// do stuff
	time_t t1, t2;
	t1 = time(NULL);

	int msg_ix;
	for (msg_ix = 0; msg_ix < n_msgs; ++msg_ix) {
	  /* add a data payload if asked */
	  if (! n_dsize) {
	    conn->send_message(new MPing());
	  } else {
	    conn->send_message(new_simple_ping_with_data("xio_client", n_dsize, n_nfrags));
	  }
	}

	// do stuff
	while (conn->is_connected()) {
	  nanosleep(&ts, NULL);
	}

	t2 = time(NULL);
	cout << "Processed "
	     << static_cast<XioConnection*>(conn->get())->get_scount()
	     << " one-way messages in " << t2-t1 << "s"
	     << std::endl;

	conn->put();

	// wait a bit for cleanup to finalize
	ts.tv_sec = 5;
	nanosleep(&ts, NULL);

	messenger->shutdown();

out:
	return r;
}