]> git.proxmox.com Git - ceph.git/blob - ceph/src/test/msgr/perf_msgr_server.cc
update sources to 12.2.7
[ceph.git] / ceph / src / test / msgr / perf_msgr_server.cc
1 // -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*-
2 // vim: ts=8 sw=2 smarttab
3 /*
4 * Ceph - scalable distributed file system
5 *
6 * Copyright (C) 2015 Haomai Wang
7 *
8 * Author: Haomai Wang <haomaiwang@gmail.com>
9 *
10 * This is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU Lesser General Public
12 * License version 2.1, as published by the Free Software
13 * Foundation. See file COPYING.
14 *
15 */
16
17 #include <stdlib.h>
18 #include <stdint.h>
19 #include <string>
20 #include <unistd.h>
21 #include <iostream>
22
23 using namespace std;
24
25 #include "common/ceph_argparse.h"
26 #include "common/debug.h"
27 #include "global/global_init.h"
28 #include "msg/Messenger.h"
29 #include "messages/MOSDOp.h"
30 #include "messages/MOSDOpReply.h"
31
32 class ServerDispatcher : public Dispatcher {
33 uint64_t think_time;
34 ThreadPool op_tp;
35 class OpWQ : public ThreadPool::WorkQueue<Message> {
36 list<Message*> messages;
37
38 public:
39 OpWQ(time_t timeout, time_t suicide_timeout, ThreadPool *tp)
40 : ThreadPool::WorkQueue<Message>("ServerDispatcher::OpWQ", timeout, suicide_timeout, tp) {}
41
42 bool _enqueue(Message *m) override {
43 messages.push_back(m);
44 return true;
45 }
46 void _dequeue(Message *m) override {
47 ceph_abort();
48 }
49 bool _empty() override {
50 return messages.empty();
51 }
52 Message *_dequeue() override {
53 if (messages.empty())
54 return NULL;
55 Message *m = messages.front();
56 messages.pop_front();
57 return m;
58 }
59 void _process(Message *m, ThreadPool::TPHandle &handle) override {
60 MOSDOp *osd_op = static_cast<MOSDOp*>(m);
61 MOSDOpReply *reply = new MOSDOpReply(osd_op, 0, 0, 0, false);
62 m->get_connection()->send_message(reply);
63 m->put();
64 }
65 void _process_finish(Message *m) override { }
66 void _clear() override {
67 assert(messages.empty());
68 }
69 } op_wq;
70
71 public:
72 ServerDispatcher(int threads, uint64_t delay): Dispatcher(g_ceph_context), think_time(delay),
73 op_tp(g_ceph_context, "ServerDispatcher::op_tp", "tp_serv_disp", threads, "serverdispatcher_op_threads"),
74 op_wq(30, 30, &op_tp) {
75 op_tp.start();
76 }
77 ~ServerDispatcher() override {
78 op_tp.stop();
79 }
80 bool ms_can_fast_dispatch_any() const override { return true; }
81 bool ms_can_fast_dispatch(const Message *m) const override {
82 switch (m->get_type()) {
83 case CEPH_MSG_OSD_OP:
84 return true;
85 default:
86 return false;
87 }
88 }
89
90 void ms_handle_fast_connect(Connection *con) override {}
91 void ms_handle_fast_accept(Connection *con) override {}
92 bool ms_dispatch(Message *m) override { return true; }
93 bool ms_handle_reset(Connection *con) override { return true; }
94 void ms_handle_remote_reset(Connection *con) override {}
95 bool ms_handle_refused(Connection *con) override { return false; }
96 void ms_fast_dispatch(Message *m) override {
97 usleep(think_time);
98 //cerr << __func__ << " reply message=" << m << std::endl;
99 op_wq.queue(m);
100 }
101 bool ms_verify_authorizer(Connection *con, int peer_type, int protocol,
102 bufferlist& authorizer, bufferlist& authorizer_reply,
103 bool& isvalid, CryptoKey& session_key,
104 std::unique_ptr<AuthAuthorizerChallenge> *challenge) override {
105 isvalid = true;
106 return true;
107 }
108 };
109
110 class MessengerServer {
111 Messenger *msgr;
112 string type;
113 string bindaddr;
114 ServerDispatcher dispatcher;
115
116 public:
117 MessengerServer(string t, string addr, int threads, int delay):
118 msgr(NULL), type(t), bindaddr(addr), dispatcher(threads, delay) {
119 msgr = Messenger::create(g_ceph_context, type, entity_name_t::OSD(0), "server", 0, 0);
120 msgr->set_default_policy(Messenger::Policy::stateless_server(0));
121 }
122 ~MessengerServer() {
123 msgr->shutdown();
124 msgr->wait();
125 }
126 void start() {
127 entity_addr_t addr;
128 addr.parse(bindaddr.c_str());
129 msgr->bind(addr);
130 msgr->add_dispatcher_head(&dispatcher);
131 msgr->start();
132 msgr->wait();
133 }
134 };
135
136 void usage(const string &name) {
137 cerr << "Usage: " << name << " [bind ip:port] [server worker threads] [thinktime us]" << std::endl;
138 cerr << " [bind ip:port]: The ip:port pair to bind, client need to specify this pair to connect" << std::endl;
139 cerr << " [server worker threads]: threads will process incoming messages and reply(matching pg threads)" << std::endl;
140 cerr << " [thinktime]: sleep time when do dispatching(match fast dispatch logic in OSD.cc)" << std::endl;
141 }
142
143 int main(int argc, char **argv)
144 {
145 vector<const char*> args;
146 argv_to_vec(argc, (const char **)argv, args);
147
148 auto cct = global_init(NULL, args, CEPH_ENTITY_TYPE_CLIENT,
149 CODE_ENVIRONMENT_UTILITY, 0);
150 common_init_finish(g_ceph_context);
151 g_ceph_context->_conf->apply_changes(NULL);
152
153 if (args.size() < 3) {
154 usage(argv[0]);
155 return 1;
156 }
157
158 int worker_threads = atoi(args[1]);
159 int think_time = atoi(args[2]);
160 std::string public_msgr_type = g_ceph_context->_conf->ms_public_type.empty() ? g_ceph_context->_conf->get_val<std::string>("ms_type") : g_ceph_context->_conf->ms_public_type;
161
162 cerr << " This tool won't handle connection error alike things, " << std::endl;
163 cerr << "please ensure the proper network environment to test." << std::endl;
164 cerr << " Or ctrl+c when meeting error and restart tests" << std::endl;
165 cerr << " using ms-public-type " << public_msgr_type << std::endl;
166 cerr << " bind ip:port " << args[0] << std::endl;
167 cerr << " worker threads " << worker_threads << std::endl;
168 cerr << " thinktime(us) " << think_time << std::endl;
169
170 MessengerServer server(public_msgr_type, args[0], worker_threads, think_time);
171 server.start();
172
173 return 0;
174 }