89138c29a902baeec3b313afc1df3bb476fe0d4c
[pazpar2-moved-to-github.git] / src / eventl.c
1 /* This file is part of Pazpar2.
2  Copyright (C) Index Data
3
4  Pazpar2 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  Pazpar2 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
20 /*
21  * Based on  ParaZ - a simple tool for harvesting performance data for
22  * parallel operations using Z39.50.
23  * Copyright (C) Index Data ApS
24  * See LICENSE file for details.
25  */
26
27 /*
28  * Based on revision YAZ' server/eventl.c 1.29.
29  */
30
31 #if HAVE_CONFIG_H
32 #include <config.h>
33 #endif
34
35 #include <math.h>
36 #include <stdio.h>
37 #include <assert.h>
38
39 #if HAVE_UNISTD_H
40 #include <unistd.h>
41 #endif
42
43 #include <stdlib.h>
44 #include <errno.h>
45 #include <string.h>
46
47 #include <yaz/yconfig.h>
48 #include <yaz/log.h>
49 #include <yaz/comstack.h>
50 #include <yaz/xmalloc.h>
51 #include <yaz/mutex.h>
52 #include <yaz/poll.h>
53 #include <sys/resource.h>
54 #include "eventl.h"
55 #include "sel_thread.h"
56
57 #if 1
58 static YAZ_MUTEX g_mutex = 0;
59 static int no_iochans = 0;
60 static int no_iochans_total = 0;
61
62 static int iochan_use(int delta)
63 {
64     int iochans;
65     if (!g_mutex)
66         yaz_mutex_create(&g_mutex);
67     yaz_mutex_enter(g_mutex);
68     no_iochans += delta;
69     if (delta > 0)
70         no_iochans_total += delta;
71     iochans = no_iochans;
72     yaz_mutex_leave(g_mutex);
73     yaz_log(YLOG_DEBUG, "%s iochans=%d",
74             delta == 0 ? "" : (delta > 0 ? "INC" : "DEC"), iochans);
75     return iochans;
76 }
77
78 int iochans_count(void)
79 {
80     return iochan_use(0);
81 }
82
83 int iochans_count_total(void)
84 {
85     int total = 0;
86     if (!g_mutex)
87         return 0;
88     yaz_mutex_enter(g_mutex);
89     total = no_iochans_total;
90     yaz_mutex_leave(g_mutex);
91     return total;
92 }
93 #else
94 #define iochan_use(x)
95 #define iochans_count(x) 0
96 #define iochans_count_total(x) 0
97 #endif
98
99 struct iochan_man_s {
100     IOCHAN channel_list;
101     sel_thread_t sel_thread;
102     int sel_fd;
103     int no_threads;
104     int log_level;
105     YAZ_MUTEX iochan_mutex;
106     int size_fds;
107     int limit_fd;
108     struct yaz_poll_fd *fds;
109 };
110
111 iochan_man_t iochan_man_create(int no_threads, int max_sockets)
112 {
113     iochan_man_t man = xmalloc(sizeof(*man));
114     man->channel_list = 0;
115     man->sel_thread = 0; /* can't create sel_thread yet because we may fork */
116     man->sel_fd = -1;
117     man->no_threads = no_threads;
118     man->log_level = yaz_log_module_level("iochan");
119     man->iochan_mutex = 0;
120     man->size_fds = 0;
121     man->fds = 0;
122     man->limit_fd = 0;
123 #if HAVE_GETRLIMIT
124     {
125         struct rlimit limit_data;
126         getrlimit(RLIMIT_NOFILE, &limit_data);
127         yaz_log(YLOG_LOG, "getrlimit NOFILE cur=%ld max=%ld",
128                 (long) limit_data.rlim_cur, (long) limit_data.rlim_max);
129         man->limit_fd = limit_data.rlim_cur - 200;
130     }
131 #endif
132     if (max_sockets)
133         man->limit_fd = max_sockets;
134     yaz_log(YLOG_LOG, "iochan max threads %d max sockets %d",
135             no_threads, max_sockets);
136     yaz_mutex_create(&man->iochan_mutex);
137     return man;
138 }
139
140 IOCHAN iochan_destroy_real(IOCHAN chan)
141 {
142     IOCHAN next = chan->next;
143     if (chan->name)
144         xfree(chan->name);
145     xfree(chan);
146     iochan_use(-1);
147     return next;
148 }
149
150 void iochan_man_destroy(iochan_man_t *mp)
151 {
152     if (*mp)
153     {
154         IOCHAN c;
155         if ((*mp)->sel_thread)
156             sel_thread_destroy((*mp)->sel_thread);
157
158         yaz_mutex_enter((*mp)->iochan_mutex);
159         c = (*mp)->channel_list;
160         (*mp)->channel_list = NULL;
161         yaz_mutex_leave((*mp)->iochan_mutex);
162         while (c)
163             c = iochan_destroy_real(c);
164         yaz_mutex_destroy(&(*mp)->iochan_mutex);
165         xfree((*mp)->fds);
166         xfree(*mp);
167         *mp = 0;
168     }
169 }
170
171 int iochan_add(iochan_man_t man, IOCHAN chan)
172 {
173     int r = 0, no_fds = 0;
174     IOCHAN p;
175
176     yaz_log(man->log_level, "iochan_add : chan=%p channel list=%p", chan,
177             man->channel_list);
178     yaz_mutex_enter(man->iochan_mutex);
179     for (p = man->channel_list; p; p = p->next)
180     {
181         if (p->fd >= 0)
182             no_fds++;
183     }
184     if (chan->fd > 0 && man->limit_fd > 0 && no_fds >= man->limit_fd)
185     {
186         r = -1;
187         yaz_log(YLOG_WARN, "max channels %d in use", no_fds);
188     }
189     else
190     {
191         chan->man = man;
192         chan->next = man->channel_list;
193         man->channel_list = chan;
194     }
195     yaz_mutex_leave(man->iochan_mutex);
196     return r;
197 }
198
199 void iochan_destroy(IOCHAN chan)
200 {
201     if (chan->man)
202         chan->destroyed = 1;
203     else
204         iochan_destroy_real(chan);
205 }
206
207 IOCHAN iochan_create(int fd, IOC_CALLBACK cb, int flags, const char *name)
208 {
209     IOCHAN new_iochan;
210
211     if (!(new_iochan = (IOCHAN) xmalloc(sizeof(*new_iochan))))
212         return 0;
213     iochan_use(1);
214     new_iochan->destroyed = 0;
215     new_iochan->fd = fd;
216     new_iochan->flags = flags;
217     new_iochan->fun = cb;
218     new_iochan->last_event = new_iochan->max_idle = 0;
219     new_iochan->next = NULL;
220     new_iochan->man = 0;
221     new_iochan->thread_users = 0;
222     new_iochan->name = name ? xstrdup(name) : 0;
223     return new_iochan;
224 }
225
226 static void work_handler(void *work_data)
227 {
228     IOCHAN p = work_data;
229
230     yaz_log(p->man->log_level, "eventl: work begin chan=%p name=%s event=%d",
231             p, p->name ? p->name : "", p->this_event);
232
233     if (!p->destroyed && (p->this_event & EVENT_TIMEOUT))
234         (*p->fun)(p, EVENT_TIMEOUT);
235     if (!p->destroyed && (p->this_event & EVENT_INPUT))
236         (*p->fun)(p, EVENT_INPUT);
237     if (!p->destroyed && (p->this_event & EVENT_OUTPUT))
238         (*p->fun)(p, EVENT_OUTPUT);
239     if (!p->destroyed && (p->this_event & EVENT_EXCEPT))
240         (*p->fun)(p, EVENT_EXCEPT);
241
242     yaz_log(p->man->log_level, "eventl: work end chan=%p name=%s event=%d", p,
243             p->name ? p->name : "", p->this_event);
244 }
245
246 static void run_fun(iochan_man_t man, IOCHAN p)
247 {
248     if (man->sel_thread)
249     {
250         yaz_log(man->log_level,
251                 "eventl: work add chan=%p name=%s event=%d", p,
252                 p->name ? p->name : "", p->this_event);
253         p->thread_users++;
254         sel_thread_add(man->sel_thread, p);
255     }
256     else
257         work_handler(p);
258 }
259
260 static int event_loop(iochan_man_t man, IOCHAN *iochans)
261 {
262     do /* loop as long as there are active associations to process */
263     {
264         IOCHAN p, *nextp;
265         IOCHAN start;
266         IOCHAN inv_start;
267         int res;
268         struct yaz_poll_fd *fds;
269         int i, no_fds = 0;
270         int connection_fired = 0;
271         int tv_sec = 300;
272
273         yaz_mutex_enter(man->iochan_mutex);
274         start = man->channel_list;
275         yaz_mutex_leave(man->iochan_mutex);
276         inv_start = start;
277         for (p = start; p; p = p->next)
278             no_fds++;
279         if (man->sel_fd != -1)
280             no_fds++;
281         if (no_fds > man->size_fds)
282         {
283             man->size_fds = no_fds * 2;
284             man->fds = xrealloc(man->fds, man->size_fds * sizeof(*man->fds));
285         }
286         fds = man->fds;
287         i = 0;
288         if (man->sel_fd != -1)
289         {
290             fds[i].fd = man->sel_fd;
291             fds[i].input_mask = yaz_poll_read;
292             fds[i].client_data = 0;
293             i++;
294         }
295         for (p = start; p; p = p->next, i++)
296         {
297             p->poll_offset = i;
298             fds[i].client_data = p;
299             fds[i].fd = -1;
300             fds[i].input_mask = 0;
301             if (p->thread_users > 0)
302                 continue;
303             if (p->max_idle && p->max_idle < tv_sec)
304                 tv_sec = p->max_idle;
305             if (p->fd < 0)
306                 continue;
307             if (p->flags & EVENT_INPUT)
308                 fds[i].input_mask |= yaz_poll_read;
309             if (p->flags & EVENT_OUTPUT)
310                 fds[i].input_mask |= yaz_poll_write;
311             if (p->flags & EVENT_EXCEPT)
312                 fds[i].input_mask |= yaz_poll_except;
313             if (fds[i].input_mask)
314                 fds[i].fd = p->fd;
315         }
316         assert(i == no_fds);
317         yaz_log(man->log_level, "yaz_poll begin tv_sec=%d nofds=%d", tv_sec,
318                 no_fds);
319         res = yaz_poll(fds, no_fds, tv_sec, 0);
320         yaz_log(man->log_level, "yaz_poll returned res=%d", res);
321         if (res < 0)
322         {
323             if (errno == EINTR)
324                 continue;
325             else
326             {
327                 yaz_log(YLOG_ERRNO | YLOG_WARN, "poll");
328                 abort();
329             }
330         }
331         if (man->sel_fd != -1)
332         {
333             i = 0;
334             assert(fds[i].fd == man->sel_fd);
335             if (fds[i].output_mask)
336             {
337                 IOCHAN chan;
338
339                 yaz_log(man->log_level, "eventl: sel input on sel_fd=%d",
340                         man->sel_fd);
341                 while ((chan = sel_thread_result(man->sel_thread)))
342                 {
343                     yaz_log(man->log_level,
344                             "eventl: got thread result chan=%p name=%s", chan,
345                             chan->name ? chan->name : "");
346                     chan->thread_users--;
347                 }
348             }
349         }
350         if (man->log_level)
351         {
352             int no = 0;
353             for (p = start; p; p = p->next)
354                 no++;
355             yaz_log(man->log_level, "%d channels", no);
356         }
357         for (p = start; p; p = p->next)
358         {
359             time_t now = time(0);
360             i = p->poll_offset;
361
362             if (p->destroyed)
363             {
364                 yaz_log(man->log_level,
365                         "eventl: skip destroyed chan=%p name=%s", p,
366                         p->name ? p->name : "");
367                 continue;
368             }
369             if (p->thread_users > 0)
370             {
371                 yaz_log(man->log_level,
372                         "eventl: skip chan=%p name=%s users=%d", p,
373                         p->name ? p->name : "", p->thread_users);
374                 continue;
375             }
376             p->this_event = 0;
377             if (p->max_idle && now - p->last_event > p->max_idle)
378             {
379                 p->last_event = now;
380                 p->this_event |= EVENT_TIMEOUT;
381             }
382             if (fds[i].fd >= 0 && p->fd == fds[i].fd)
383             {
384                 if (fds[i].output_mask & yaz_poll_read)
385                 {
386                     p->last_event = now;
387                     p->this_event |= EVENT_INPUT;
388                 }
389                 if (fds[i].output_mask & yaz_poll_write)
390                 {
391                     p->last_event = now;
392                     p->this_event |= EVENT_OUTPUT;
393                 }
394                 if (fds[i].output_mask & yaz_poll_except)
395                 {
396                     p->last_event = now;
397                     p->this_event |= EVENT_EXCEPT;
398                 }
399             }
400             /* only fire one Z39.50/SRU socket event.. except for timeout */
401             if (p->this_event)
402             {
403                 if (!(p->this_event & EVENT_TIMEOUT) &&
404                     !strcmp(p->name, "connection_socket"))
405                 {
406                     /* not a timeout and we have a Z39.50/SRU socket */
407                     if (connection_fired == 0)
408                         run_fun(man, p);
409                     connection_fired++;
410                 }
411                 else
412                     run_fun(man, p);
413             }
414         }
415
416         assert(inv_start == start);
417         yaz_mutex_enter(man->iochan_mutex);
418         for (nextp = iochans; *nextp; )
419         {
420             IOCHAN p = *nextp;
421             if (p->destroyed && p->thread_users == 0)
422                 *nextp = iochan_destroy_real(p);
423             else
424                 nextp = &p->next;
425         }
426         yaz_mutex_leave(man->iochan_mutex);
427     } while (*iochans);
428     return 0;
429 }
430
431 void iochan_man_events(iochan_man_t man)
432 {
433     if (man->no_threads > 0 && !man->sel_thread)
434     {
435         man->sel_thread = sel_thread_create(work_handler, 0 /*work_destroy */,
436                 &man->sel_fd, man->no_threads);
437         yaz_log(man->log_level, "iochan_man_events. Using %d threads",
438                 man->no_threads);
439     }
440     event_loop(man, &man->channel_list);
441 }
442
443 /*
444  * Local variables:
445  * c-basic-offset: 4
446  * c-file-style: "Stroustrup"
447  * indent-tabs-mode: nil
448  * End:
449  * vim: shiftwidth=4 tabstop=8 expandtab
450  */
451