d86840580415c773be85fb433b9a30b54b1e5fc9
[m6w6/libmemcached] / libtest / blobslap_worker.cc
1 /* vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
2 *
3 * libtest
4 *
5 * Copyright (C) 2011 Data Differential, http://datadifferential.com/
6 *
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 3 of the License, or (at your option) any later version.
11 *
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20 */
21
22
23 #include <libtest/common.h>
24
25 #include <libtest/blobslap_worker.h>
26
27 #include <cassert>
28 #include <cerrno>
29 #include <cstdio>
30 #include <cstdlib>
31 #include <cstring>
32 #include <iostream>
33 #include <signal.h>
34 #include <sys/types.h>
35 #include <sys/wait.h>
36 #include <unistd.h>
37
38 #include <libgearman/gearman.h>
39
40 #ifndef __INTEL_COMPILER
41 #pragma GCC diagnostic ignored "-Wold-style-cast"
42 #endif
43
44 namespace libtest {
45
46 class BlobslapWorker : public Server
47 {
48 private:
49 public:
50 BlobslapWorker(in_port_t port_arg) :
51 Server("localhost", port_arg)
52 {
53 set_pid_file();
54 }
55
56 pid_t get_pid(bool error_is_ok)
57 {
58 if (pid_file().empty())
59 {
60 Error << "pid_file was empty";
61 return -1;
62 }
63
64 Wait wait(pid_file(), 0);
65
66 if (error_is_ok and not wait.successful())
67 {
68 Error << "Pidfile was not found:" << pid_file();
69 return -1;
70 }
71
72 std::stringstream error_message;
73 pid_t ret= get_pid_from_file(pid_file(), error_message);
74
75 if (error_is_ok and is_pid_valid(ret) == false)
76 {
77 Error << error_message.str();
78 }
79
80 return ret;
81 }
82
83 bool ping()
84 {
85 if (pid_file().empty())
86 {
87 Error << "No pid file available";
88 return false;
89 }
90
91 Wait wait(pid_file(), 0);
92 if (not wait.successful())
93 {
94 Error << "Pidfile was not found:" << pid_file();
95 return false;
96 }
97
98 std::stringstream error_message;
99 pid_t local_pid= get_pid_from_file(pid_file(), error_message);
100 if (is_pid_valid(local_pid) == false)
101 {
102 Error << error_message.str();
103 return false;
104 }
105
106 // Use kill to determine is the process exist
107 if (::kill(local_pid, 0) == 0)
108 {
109 return true;
110 }
111
112 return false;
113 }
114
115 const char *name()
116 {
117 return "blobslap_worker";
118 };
119
120 const char *executable()
121 {
122 return "benchmark/blobslap_worker";
123 }
124
125 const char *daemon_file_option()
126 {
127 return "--daemon";
128 }
129
130 void has_port_option() const
131 {
132 return true;
133 }
134
135 bool has_log_file_option() const
136 {
137 return true;
138 }
139
140 bool is_libtool()
141 {
142 return true;
143 }
144
145 bool build(int argc, const char *argv[]);
146 };
147
148
149 #include <sstream>
150
151 bool BlobslapWorker::build(int argc, const char *argv[])
152 {
153 std::stringstream arg_buffer;
154
155 for (int x= 1 ; x < argc ; x++)
156 {
157 add_option(argv[x]);
158 }
159
160 return true;
161 }
162
163 Server *build_blobslap_worker(in_port_t try_port)
164 {
165 return new BlobslapWorker(try_port);
166 }
167
168 } // namespace libtest