da2f7f901b8c0b549c22ccfdb246205a52352e8f
[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)
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     yaz_mutex_create(&man->iochan_mutex);
133     return man;
134 }
135
136 IOCHAN iochan_destroy_real(IOCHAN chan)
137 {
138     IOCHAN next = chan->next;
139     if (chan->name)
140         xfree(chan->name);
141     xfree(chan);
142     iochan_use(-1);
143     return next;
144 }
145
146 void iochan_man_destroy(iochan_man_t *mp)
147 {
148     if (*mp)
149     {
150         IOCHAN c;
151         if ((*mp)->sel_thread)
152             sel_thread_destroy((*mp)->sel_thread);
153
154         yaz_mutex_enter((*mp)->iochan_mutex);
155         c = (*mp)->channel_list;
156         (*mp)->channel_list = NULL;
157         yaz_mutex_leave((*mp)->iochan_mutex);
158         while (c)
159             c = iochan_destroy_real(c);
160         yaz_mutex_destroy(&(*mp)->iochan_mutex);
161         xfree((*mp)->fds);
162         xfree(*mp);
163         *mp = 0;
164     }
165 }
166
167 int iochan_add(iochan_man_t man, IOCHAN chan)
168 {
169     int r = 0, no_fds = 0;
170     IOCHAN p;
171     chan->man = man;
172
173     yaz_log(man->log_level, "iochan_add : chan=%p channel list=%p", chan,
174             man->channel_list);
175     yaz_mutex_enter(man->iochan_mutex);
176     for (p = man->channel_list; p; p = p->next)
177         no_fds++;
178     if (man->limit_fd > 0 && no_fds >= man->limit_fd)
179         r = -1;
180     else
181     {
182         chan->next = man->channel_list;
183         man->channel_list = chan;
184     }
185     yaz_mutex_leave(man->iochan_mutex);
186     return r;
187 }
188
189 IOCHAN iochan_create(int fd, IOC_CALLBACK cb, int flags, const char *name)
190 {
191     IOCHAN new_iochan;
192
193     if (!(new_iochan = (IOCHAN) xmalloc(sizeof(*new_iochan))))
194         return 0;
195     iochan_use(1);
196     new_iochan->destroyed = 0;
197     new_iochan->fd = fd;
198     new_iochan->flags = flags;
199     new_iochan->fun = cb;
200     new_iochan->last_event = new_iochan->max_idle = 0;
201     new_iochan->next = NULL;
202     new_iochan->man = 0;
203     new_iochan->thread_users = 0;
204     new_iochan->name = name ? xstrdup(name) : 0;
205     return new_iochan;
206 }
207
208 static void work_handler(void *work_data)
209 {
210     IOCHAN p = work_data;
211
212     yaz_log(p->man->log_level, "eventl: work begin chan=%p name=%s event=%d",
213             p, p->name ? p->name : "", p->this_event);
214
215     if (!p->destroyed && (p->this_event & EVENT_TIMEOUT))
216         (*p->fun)(p, EVENT_TIMEOUT);
217     if (!p->destroyed && (p->this_event & EVENT_INPUT))
218         (*p->fun)(p, EVENT_INPUT);
219     if (!p->destroyed && (p->this_event & EVENT_OUTPUT))
220         (*p->fun)(p, EVENT_OUTPUT);
221     if (!p->destroyed && (p->this_event & EVENT_EXCEPT))
222         (*p->fun)(p, EVENT_EXCEPT);
223
224     yaz_log(p->man->log_level, "eventl: work end chan=%p name=%s event=%d", p,
225             p->name ? p->name : "", p->this_event);
226 }
227
228 static void run_fun(iochan_man_t man, IOCHAN p)
229 {
230     if (man->sel_thread)
231     {
232         yaz_log(man->log_level,
233                 "eventl: work add chan=%p name=%s event=%d", p,
234                 p->name ? p->name : "", p->this_event);
235         p->thread_users++;
236         sel_thread_add(man->sel_thread, p);
237     }
238     else
239         work_handler(p);
240 }
241
242 static int event_loop(iochan_man_t man, IOCHAN *iochans)
243 {
244     do /* loop as long as there are active associations to process */
245     {
246         IOCHAN p, *nextp;
247         IOCHAN start;
248         IOCHAN inv_start;
249         int res;
250         struct yaz_poll_fd *fds;
251         int i, no_fds = 0;
252         int connection_fired = 0;
253         int tv_sec = 300;
254
255         yaz_mutex_enter(man->iochan_mutex);
256         start = man->channel_list;
257         yaz_mutex_leave(man->iochan_mutex);
258         inv_start = start;
259         for (p = start; p; p = p->next)
260             no_fds++;
261         if (man->sel_fd != -1)
262             no_fds++;
263         if (no_fds > man->size_fds)
264         {
265             man->size_fds = no_fds * 2;
266             man->fds = xrealloc(man->fds, man->size_fds * sizeof(*man->fds));
267         }
268         fds = man->fds;
269         i = 0;
270         if (man->sel_fd != -1)
271         {
272             fds[i].fd = man->sel_fd;
273             fds[i].input_mask = yaz_poll_read;
274             fds[i].client_data = 0;
275             i++;
276         }
277         for (p = start; p; p = p->next, i++)
278         {
279             p->poll_offset = i;
280             fds[i].client_data = p;
281             fds[i].fd = -1;
282             fds[i].input_mask = 0;
283             if (p->thread_users > 0)
284                 continue;
285             if (p->max_idle && p->max_idle < tv_sec)
286                 tv_sec = p->max_idle;
287             if (p->fd < 0)
288                 continue;
289             if (p->flags & EVENT_INPUT)
290                 fds[i].input_mask |= yaz_poll_read;
291             if (p->flags & EVENT_OUTPUT)
292                 fds[i].input_mask |= yaz_poll_write;
293             if (p->flags & EVENT_EXCEPT)
294                 fds[i].input_mask |= yaz_poll_except;
295             if (fds[i].input_mask)
296                 fds[i].fd = p->fd;
297         }
298         assert(i == no_fds);
299         yaz_log(man->log_level, "yaz_poll begin tv_sec=%d nofds=%d", tv_sec,
300                 no_fds);
301         res = yaz_poll(fds, no_fds, tv_sec, 0);
302         yaz_log(man->log_level, "yaz_poll returned res=%d", res);
303         if (res < 0)
304         {
305             if (errno == EINTR)
306                 continue;
307             else
308             {
309                 yaz_log(YLOG_ERRNO | YLOG_WARN, "poll");
310                 abort();
311             }
312         }
313         if (man->sel_fd != -1)
314         {
315             i = 0;
316             assert(fds[i].fd == man->sel_fd);
317             if (fds[i].output_mask)
318             {
319                 IOCHAN chan;
320
321                 yaz_log(man->log_level, "eventl: sel input on sel_fd=%d",
322                         man->sel_fd);
323                 while ((chan = sel_thread_result(man->sel_thread)))
324                 {
325                     yaz_log(man->log_level,
326                             "eventl: got thread result chan=%p name=%s", chan,
327                             chan->name ? chan->name : "");
328                     chan->thread_users--;
329                 }
330             }
331         }
332         if (man->log_level)
333         {
334             int no = 0;
335             for (p = start; p; p = p->next)
336                 no++;
337             yaz_log(man->log_level, "%d channels", no);
338         }
339         for (p = start; p; p = p->next)
340         {
341             time_t now = time(0);
342             i = p->poll_offset;
343
344             if (p->destroyed)
345             {
346                 yaz_log(man->log_level,
347                         "eventl: skip destroyed chan=%p name=%s", p,
348                         p->name ? p->name : "");
349                 continue;
350             }
351             if (p->thread_users > 0)
352             {
353                 yaz_log(man->log_level,
354                         "eventl: skip chan=%p name=%s users=%d", p,
355                         p->name ? p->name : "", p->thread_users);
356                 continue;
357             }
358             p->this_event = 0;
359             if (p->max_idle && now - p->last_event > p->max_idle)
360             {
361                 p->last_event = now;
362                 p->this_event |= EVENT_TIMEOUT;
363             }
364             if (fds[i].fd >= 0 && p->fd == fds[i].fd)
365             {
366                 if (fds[i].output_mask & yaz_poll_read)
367                 {
368                     p->last_event = now;
369                     p->this_event |= EVENT_INPUT;
370                 }
371                 if (fds[i].output_mask & yaz_poll_write)
372                 {
373                     p->last_event = now;
374                     p->this_event |= EVENT_OUTPUT;
375                 }
376                 if (fds[i].output_mask & yaz_poll_except)
377                 {
378                     p->last_event = now;
379                     p->this_event |= EVENT_EXCEPT;
380                 }
381             }
382             /* only fire one Z39.50/SRU socket event.. except for timeout */
383             if (p->this_event)
384             {
385                 if (!(p->this_event & EVENT_TIMEOUT) &&
386                     !strcmp(p->name, "connection_socket"))
387                 {
388                     /* not a timeout and we have a Z39.50/SRU socket */
389                     if (connection_fired == 0)
390                         run_fun(man, p);
391                     connection_fired++;
392                 }
393                 else
394                     run_fun(man, p);
395             }
396         }
397
398         assert(inv_start == start);
399         yaz_mutex_enter(man->iochan_mutex);
400         for (nextp = iochans; *nextp; )
401         {
402             IOCHAN p = *nextp;
403             if (p->destroyed && p->thread_users == 0)
404                 *nextp = iochan_destroy_real(p);
405             else
406                 nextp = &p->next;
407         }
408         yaz_mutex_leave(man->iochan_mutex);
409     } while (*iochans);
410     return 0;
411 }
412
413 void iochan_man_events(iochan_man_t man)
414 {
415     if (man->no_threads > 0 && !man->sel_thread)
416     {
417         man->sel_thread = sel_thread_create(work_handler, 0 /*work_destroy */,
418                 &man->sel_fd, man->no_threads);
419         yaz_log(man->log_level, "iochan_man_events. Using %d threads",
420                 man->no_threads);
421     }
422     event_loop(man, &man->channel_list);
423 }
424
425 /*
426  * Local variables:
427  * c-basic-offset: 4
428  * c-file-style: "Stroustrup"
429  * indent-tabs-mode: nil
430  * End:
431  * vim: shiftwidth=4 tabstop=8 expandtab
432  */
433