Filter frontend_net may log statistics
[metaproxy-moved-to-github.git] / src / thread_pool_observer.cpp
1 /* This file is part of Metaproxy.
2    Copyright (C) 2005-2012 Index Data
3
4 Metaproxy is free software; you can redistribute it and/or modify it under
5 the terms of the GNU General Public License as published by the Free
6 Software Foundation; either version 2, or (at your option) any later
7 version.
8
9 Metaproxy is distributed in the hope that it will be useful, but WITHOUT ANY
10 WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
12 for more details.
13
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
17 */
18
19 #include "config.hpp"
20
21 #if HAVE_UNISTD_H
22 #include <unistd.h>
23 #endif
24
25 #ifdef WIN32
26 #include <windows.h>
27 #include <winsock.h>
28 #endif
29 #include <boost/thread/thread.hpp>
30 #include <boost/thread/mutex.hpp>
31 #include <boost/thread/condition.hpp>
32
33 #include <ctype.h>
34 #include <stdio.h>
35
36 #include <deque>
37
38 #include <yazpp/socket-observer.h>
39 #include <yaz/log.h>
40
41 #include "thread_pool_observer.hpp"
42 #include "pipe.hpp"
43
44 namespace metaproxy_1 {
45     class ThreadPoolSocketObserver::Worker {
46     public:
47         Worker(ThreadPoolSocketObserver *s) : m_s(s) {};
48         ThreadPoolSocketObserver *m_s;
49         void operator() (void) {
50             m_s->run(0);
51         }
52     };
53
54     class ThreadPoolSocketObserver::Rep : public boost::noncopyable {
55         friend class ThreadPoolSocketObserver;
56     public:
57         Rep(yazpp_1::ISocketObservable *obs);
58         ~Rep();
59     private:
60         yazpp_1::ISocketObservable *m_socketObservable;
61         Pipe m_pipe;
62         boost::thread_group m_thrds;
63         boost::mutex m_mutex_input_data;
64         boost::condition m_cond_input_data;
65         boost::condition m_cond_input_full;
66         boost::mutex m_mutex_output_data;
67         std::deque<IThreadPoolMsg *> m_input;
68         std::deque<IThreadPoolMsg *> m_output;
69         bool m_stop_flag;
70         unsigned m_no_threads;
71         unsigned m_no_threads_waiting;
72     };
73     const unsigned int queue_size_per_thread = 64;
74 }
75
76
77
78 using namespace yazpp_1;
79 using namespace metaproxy_1;
80
81 ThreadPoolSocketObserver::Rep::Rep(yazpp_1::ISocketObservable *obs)
82     : m_socketObservable(obs), m_pipe(9123)
83 {
84 }
85
86 ThreadPoolSocketObserver::Rep::~Rep()
87 {
88 }
89
90 IThreadPoolMsg::~IThreadPoolMsg()
91 {
92
93 }
94
95 ThreadPoolSocketObserver::ThreadPoolSocketObserver(
96     yazpp_1::ISocketObservable *obs, int no_threads)
97     : m_p(new Rep(obs))
98 {
99     obs->addObserver(m_p->m_pipe.read_fd(), this);
100     obs->maskObserver(this, SOCKET_OBSERVE_READ);
101
102     m_p->m_stop_flag = false;
103     m_p->m_no_threads = no_threads;
104     m_p->m_no_threads_waiting = 0;
105     int i;
106     for (i = 0; i<no_threads; i++)
107     {
108         Worker w(this);
109         m_p->m_thrds.add_thread(new boost::thread(w));
110     }
111 }
112
113 ThreadPoolSocketObserver::~ThreadPoolSocketObserver()
114 {
115     {
116         boost::mutex::scoped_lock input_lock(m_p->m_mutex_input_data);
117         m_p->m_stop_flag = true;
118         m_p->m_cond_input_data.notify_all();
119     }
120     m_p->m_thrds.join_all();
121
122     m_p->m_socketObservable->deleteObserver(this);
123 }
124
125 void ThreadPoolSocketObserver::socketNotify(int event)
126 {
127     if (event & SOCKET_OBSERVE_READ)
128     {
129         char buf[2];
130 #ifdef WIN32
131         recv(m_p->m_pipe.read_fd(), buf, 1, 0);
132 #else
133         ssize_t r = read(m_p->m_pipe.read_fd(), buf, 1);
134         if (r != 1)
135         {
136             if (r == (ssize_t) (-1))
137                 yaz_log(YLOG_WARN|YLOG_ERRNO,
138                         "ThreadPoolSocketObserver::socketNotify. read fail");
139             else
140                 yaz_log(YLOG_WARN,
141                         "ThreadPoolSocketObserver::socketNotify. read returned 0");
142         }
143 #endif
144         IThreadPoolMsg *out;
145         {
146             boost::mutex::scoped_lock output_lock(m_p->m_mutex_output_data);
147             out = m_p->m_output.front();
148             m_p->m_output.pop_front();
149         }
150
151
152         if (out)
153         {
154             std::ostringstream os;
155             {
156                 boost::mutex::scoped_lock input_lock(m_p->m_mutex_input_data);
157                 os  << "tbusy/total " <<
158                     m_p->m_no_threads - m_p->m_no_threads_waiting <<
159                     "/" << m_p->m_no_threads
160                     << " queue in/out " << m_p->m_input.size() << "/"
161                     << m_p->m_output.size();
162             }
163             out->result(os.str().c_str());
164         }
165     }
166 }
167
168 void ThreadPoolSocketObserver::run(void *p)
169 {
170     while(1)
171     {
172         IThreadPoolMsg *in = 0;
173         {
174             boost::mutex::scoped_lock input_lock(m_p->m_mutex_input_data);
175             m_p->m_no_threads_waiting++;
176             while (!m_p->m_stop_flag && m_p->m_input.size() == 0)
177                 m_p->m_cond_input_data.wait(input_lock);
178             m_p->m_no_threads_waiting--;
179             if (m_p->m_stop_flag)
180                 break;
181             
182             in = m_p->m_input.front();
183             m_p->m_input.pop_front(); 
184             m_p->m_cond_input_full.notify_all();
185         }
186         IThreadPoolMsg *out = in->handle();
187         {
188             boost::mutex::scoped_lock output_lock(m_p->m_mutex_output_data);
189             m_p->m_output.push_back(out);
190 #ifdef WIN32
191             send(m_p->m_pipe.write_fd(), "", 1, 0);
192 #else
193             ssize_t r = write(m_p->m_pipe.write_fd(), "", 1);
194             if (r != 1)
195             {
196                 if (r == (ssize_t) (-1))
197                     yaz_log(YLOG_WARN|YLOG_ERRNO,
198                             "ThreadPoolSocketObserver::run. write fail");
199                 else
200                     yaz_log(YLOG_WARN,
201                             "ThreadPoolSocketObserver::run. write returned 0");
202             }
203 #endif
204         }
205     }
206 }
207
208 void ThreadPoolSocketObserver::put(IThreadPoolMsg *m)
209 {
210     boost::mutex::scoped_lock input_lock(m_p->m_mutex_input_data);
211     while (m_p->m_input.size() >= m_p->m_no_threads * queue_size_per_thread)
212         m_p->m_cond_input_full.wait(input_lock);
213     m_p->m_input.push_back(m);
214     m_p->m_cond_input_data.notify_one();
215 }
216 /*
217  * Local variables:
218  * c-basic-offset: 4
219  * c-file-style: "Stroustrup"
220  * indent-tabs-mode: nil
221  * End:
222  * vim: shiftwidth=4 tabstop=8 expandtab
223  */
224