]> git.proxmox.com Git - ceph.git/blame - ceph/src/test/msgr/perf_msgr_client.cc
add subtree-ish sources for 12.0.3
[ceph.git] / ceph / src / test / msgr / perf_msgr_client.cc
CommitLineData
7c673cae
FG
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
23using namespace std;
24
25#include "include/atomic.h"
26#include "common/ceph_argparse.h"
27#include "common/debug.h"
28#include "common/Cycles.h"
29#include "global/global_init.h"
30#include "msg/Messenger.h"
31#include "messages/MOSDOp.h"
32
33class MessengerClient {
34 class ClientThread;
35 class ClientDispatcher : public Dispatcher {
36 uint64_t think_time;
37 ClientThread *thread;
38
39 public:
40 ClientDispatcher(uint64_t delay, ClientThread *t): Dispatcher(g_ceph_context), think_time(delay), thread(t) {}
41 bool ms_can_fast_dispatch_any() const override { return true; }
42 bool ms_can_fast_dispatch(const Message *m) const override {
43 switch (m->get_type()) {
44 case CEPH_MSG_OSD_OPREPLY:
45 return true;
46 default:
47 return false;
48 }
49 }
50
51 void ms_handle_fast_connect(Connection *con) override {}
52 void ms_handle_fast_accept(Connection *con) override {}
53 bool ms_dispatch(Message *m) override { return true; }
54 void ms_fast_dispatch(Message *m) override;
55 bool ms_handle_reset(Connection *con) override { return true; }
56 void ms_handle_remote_reset(Connection *con) override {}
57 bool ms_handle_refused(Connection *con) override { return false; }
58 bool ms_verify_authorizer(Connection *con, int peer_type, int protocol,
59 bufferlist& authorizer, bufferlist& authorizer_reply,
60 bool& isvalid, CryptoKey& session_key) override {
61 isvalid = true;
62 return true;
63 }
64 };
65
66 class ClientThread : public Thread {
67 Messenger *msgr;
68 int concurrent;
69 ConnectionRef conn;
70 atomic_t client_inc;
71 object_t oid;
72 object_locator_t oloc;
73 pg_t pgid;
74 int msg_len;
75 bufferlist data;
76 int ops;
77 ClientDispatcher dispatcher;
78
79 public:
80 Mutex lock;
81 Cond cond;
82 uint64_t inflight;
83
84 ClientThread(Messenger *m, int c, ConnectionRef con, int len, int ops, int think_time_us):
85 msgr(m), concurrent(c), conn(con), client_inc(0), oid("object-name"), oloc(1, 1), msg_len(len), ops(ops),
86 dispatcher(think_time_us, this), lock("MessengerBenchmark::ClientThread::lock") {
87 m->add_dispatcher_head(&dispatcher);
88 bufferptr ptr(msg_len);
89 memset(ptr.c_str(), 0, msg_len);
90 data.append(ptr);
91 }
92 void *entry() override {
93 lock.Lock();
94 for (int i = 0; i < ops; ++i) {
95 if (inflight > uint64_t(concurrent)) {
96 cond.Wait(lock);
97 }
98 hobject_t hobj(oid, oloc.key, CEPH_NOSNAP, pgid.ps(), pgid.pool(),
99 oloc.nspace);
100 spg_t spgid(pgid);
101 MOSDOp *m = new MOSDOp(client_inc.read(), 0, hobj, spgid, 0, 0, 0);
102 m->write(0, msg_len, data);
103 inflight++;
104 conn->send_message(m);
105 //cerr << __func__ << " send m=" << m << std::endl;
106 }
107 lock.Unlock();
108 msgr->shutdown();
109 return 0;
110 }
111 };
112
113 string type;
114 string serveraddr;
115 int think_time_us;
116 vector<Messenger*> msgrs;
117 vector<ClientThread*> clients;
118
119 public:
120 MessengerClient(string t, string addr, int delay):
121 type(t), serveraddr(addr), think_time_us(delay) {
122 }
123 ~MessengerClient() {
124 for (uint64_t i = 0; i < clients.size(); ++i)
125 delete clients[i];
126 for (uint64_t i = 0; i < msgrs.size(); ++i) {
127 msgrs[i]->shutdown();
128 msgrs[i]->wait();
129 }
130 }
131 void ready(int c, int jobs, int ops, int msg_len) {
132 entity_addr_t addr;
133 addr.parse(serveraddr.c_str());
134 addr.set_nonce(0);
135 for (int i = 0; i < jobs; ++i) {
136 Messenger *msgr = Messenger::create(g_ceph_context, type, entity_name_t::CLIENT(0), "client", getpid()+i, 0);
137 msgr->set_default_policy(Messenger::Policy::lossless_client(0));
138 entity_inst_t inst(entity_name_t::OSD(0), addr);
139 ConnectionRef conn = msgr->get_connection(inst);
140 ClientThread *t = new ClientThread(msgr, c, conn, msg_len, ops, think_time_us);
141 msgrs.push_back(msgr);
142 clients.push_back(t);
143 msgr->start();
144 }
145 usleep(1000*1000);
146 }
147 void start() {
148 for (uint64_t i = 0; i < clients.size(); ++i)
149 clients[i]->create("client");
150 for (uint64_t i = 0; i < msgrs.size(); ++i)
151 msgrs[i]->wait();
152 }
153};
154
155void MessengerClient::ClientDispatcher::ms_fast_dispatch(Message *m) {
156 usleep(think_time);
157 m->put();
158 Mutex::Locker l(thread->lock);
159 thread->inflight--;
160 thread->cond.Signal();
161}
162
163
164void usage(const string &name) {
165 cerr << "Usage: " << name << " [server ip:port] [numjobs] [concurrency] [ios] [thinktime us] [msg length]" << std::endl;
166 cerr << " [server ip:port]: connect to the ip:port pair" << std::endl;
167 cerr << " [numjobs]: how much client threads spawned and do benchmark" << std::endl;
168 cerr << " [concurrency]: the max inflight messages(like iodepth in fio)" << std::endl;
169 cerr << " [ios]: how much messages sent for each client" << std::endl;
170 cerr << " [thinktime]: sleep time when do fast dispatching(match client logic)" << std::endl;
171 cerr << " [msg length]: message data bytes" << std::endl;
172}
173
174int main(int argc, char **argv)
175{
176 vector<const char*> args;
177 argv_to_vec(argc, (const char **)argv, args);
178
179 auto cct = global_init(NULL, args, CEPH_ENTITY_TYPE_CLIENT,
180 CODE_ENVIRONMENT_UTILITY, 0);
181 common_init_finish(g_ceph_context);
182 g_ceph_context->_conf->apply_changes(NULL);
183
184 if (args.size() < 6) {
185 usage(argv[0]);
186 return 1;
187 }
188
189 int numjobs = atoi(args[1]);
190 int concurrent = atoi(args[2]);
191 int ios = atoi(args[3]);
192 int think_time = atoi(args[4]);
193 int len = atoi(args[5]);
194
195 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;
196
197 cerr << " using ms-public-type " << public_msgr_type << std::endl;
198 cerr << " server ip:port " << args[0] << std::endl;
199 cerr << " numjobs " << numjobs << std::endl;
200 cerr << " concurrency " << concurrent << std::endl;
201 cerr << " ios " << ios << std::endl;
202 cerr << " thinktime(us) " << think_time << std::endl;
203 cerr << " message data bytes " << len << std::endl;
204
205 MessengerClient client(public_msgr_type, args[0], think_time);
206
207 client.ready(concurrent, numjobs, ios, len);
208 Cycles::init();
209 uint64_t start = Cycles::rdtsc();
210 client.start();
211 uint64_t stop = Cycles::rdtsc();
212 cerr << " Total op " << ios << " run time " << Cycles::to_microseconds(stop - start) << "us." << std::endl;
213
214 return 0;
215}