1 /* This file is part of Pazpar2.
2 Copyright (C) Index Data
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
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
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
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.
28 * Based on revision YAZ' server/eventl.c 1.29.
47 #include <yaz/yconfig.h>
49 #include <yaz/comstack.h>
50 #include <yaz/xmalloc.h>
51 #include <yaz/mutex.h>
53 #include <sys/resource.h>
55 #include "sel_thread.h"
58 static YAZ_MUTEX g_mutex = 0;
59 static int no_iochans = 0;
60 static int no_iochans_total = 0;
62 static int iochan_use(int delta)
66 yaz_mutex_create(&g_mutex);
67 yaz_mutex_enter(g_mutex);
70 no_iochans_total += delta;
72 yaz_mutex_leave(g_mutex);
73 yaz_log(YLOG_DEBUG, "%s iochans=%d",
74 delta == 0 ? "" : (delta > 0 ? "INC" : "DEC"), iochans);
78 int iochans_count(void)
83 int iochans_count_total(void)
88 yaz_mutex_enter(g_mutex);
89 total = no_iochans_total;
90 yaz_mutex_leave(g_mutex);
95 #define iochans_count(x) 0
96 #define iochans_count_total(x) 0
101 sel_thread_t sel_thread;
105 YAZ_MUTEX iochan_mutex;
108 struct yaz_poll_fd *fds;
111 iochan_man_t iochan_man_create(int no_threads, int max_sockets)
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 */
117 man->no_threads = no_threads;
118 man->log_level = yaz_log_module_level("iochan");
119 man->iochan_mutex = 0;
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;
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);
140 IOCHAN iochan_destroy_real(IOCHAN chan)
142 IOCHAN next = chan->next;
150 void iochan_man_destroy(iochan_man_t *mp)
155 if ((*mp)->sel_thread)
156 sel_thread_destroy((*mp)->sel_thread);
158 yaz_mutex_enter((*mp)->iochan_mutex);
159 c = (*mp)->channel_list;
160 (*mp)->channel_list = NULL;
161 yaz_mutex_leave((*mp)->iochan_mutex);
163 c = iochan_destroy_real(c);
164 yaz_mutex_destroy(&(*mp)->iochan_mutex);
171 int iochan_add(iochan_man_t man, IOCHAN chan)
173 int r = 0, no_fds = 0;
176 yaz_log(man->log_level, "iochan_add : chan=%p channel list=%p", chan,
178 yaz_mutex_enter(man->iochan_mutex);
179 for (p = man->channel_list; p; p = p->next)
184 if (chan->fd > 0 && man->limit_fd > 0 && no_fds >= man->limit_fd)
187 yaz_log(YLOG_WARN, "max channels %d in use", no_fds);
192 chan->next = man->channel_list;
193 man->channel_list = chan;
195 yaz_mutex_leave(man->iochan_mutex);
199 void iochan_destroy(IOCHAN chan)
204 iochan_destroy_real(chan);
207 IOCHAN iochan_create(int fd, IOC_CALLBACK cb, int flags, const char *name)
211 if (!(new_iochan = (IOCHAN) xmalloc(sizeof(*new_iochan))))
214 new_iochan->destroyed = 0;
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;
221 new_iochan->thread_users = 0;
222 new_iochan->name = name ? xstrdup(name) : 0;
226 static void work_handler(void *work_data)
228 IOCHAN p = work_data;
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);
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);
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);
246 static void run_fun(iochan_man_t man, IOCHAN p)
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);
254 sel_thread_add(man->sel_thread, p);
260 static int event_loop(iochan_man_t man, IOCHAN *iochans)
262 do /* loop as long as there are active associations to process */
268 struct yaz_poll_fd *fds;
270 int connection_fired = 0;
273 yaz_mutex_enter(man->iochan_mutex);
274 start = man->channel_list;
275 yaz_mutex_leave(man->iochan_mutex);
277 for (p = start; p; p = p->next)
279 if (man->sel_fd != -1)
281 if (no_fds > man->size_fds)
283 man->size_fds = no_fds * 2;
284 man->fds = xrealloc(man->fds, man->size_fds * sizeof(*man->fds));
288 if (man->sel_fd != -1)
290 fds[i].fd = man->sel_fd;
291 fds[i].input_mask = yaz_poll_read;
292 fds[i].client_data = 0;
295 for (p = start; p; p = p->next, i++)
298 fds[i].client_data = p;
300 fds[i].input_mask = 0;
301 if (p->thread_users > 0)
303 if (p->max_idle && p->max_idle < tv_sec)
304 tv_sec = p->max_idle;
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)
317 yaz_log(man->log_level, "yaz_poll begin tv_sec=%d nofds=%d", tv_sec,
319 res = yaz_poll(fds, no_fds, tv_sec, 0);
320 yaz_log(man->log_level, "yaz_poll returned res=%d", res);
327 yaz_log(YLOG_ERRNO | YLOG_WARN, "poll");
331 if (man->sel_fd != -1)
334 assert(fds[i].fd == man->sel_fd);
335 if (fds[i].output_mask)
339 yaz_log(man->log_level, "eventl: sel input on sel_fd=%d",
341 while ((chan = sel_thread_result(man->sel_thread)))
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--;
353 for (p = start; p; p = p->next)
355 yaz_log(man->log_level, "%d channels", no);
357 for (p = start; p; p = p->next)
359 time_t now = time(0);
364 yaz_log(man->log_level,
365 "eventl: skip destroyed chan=%p name=%s", p,
366 p->name ? p->name : "");
369 if (p->thread_users > 0)
371 yaz_log(man->log_level,
372 "eventl: skip chan=%p name=%s users=%d", p,
373 p->name ? p->name : "", p->thread_users);
377 if (p->max_idle && now - p->last_event > p->max_idle)
380 p->this_event |= EVENT_TIMEOUT;
382 if (fds[i].fd >= 0 && p->fd == fds[i].fd)
384 if (fds[i].output_mask & yaz_poll_read)
387 p->this_event |= EVENT_INPUT;
389 if (fds[i].output_mask & yaz_poll_write)
392 p->this_event |= EVENT_OUTPUT;
394 if (fds[i].output_mask & yaz_poll_except)
397 p->this_event |= EVENT_EXCEPT;
400 /* only fire one Z39.50/SRU socket event.. except for timeout */
403 if (!(p->this_event & EVENT_TIMEOUT) &&
404 !strcmp(p->name, "connection_socket"))
406 /* not a timeout and we have a Z39.50/SRU socket */
407 if (connection_fired == 0)
416 assert(inv_start == start);
417 yaz_mutex_enter(man->iochan_mutex);
418 for (nextp = iochans; *nextp; )
421 if (p->destroyed && p->thread_users == 0)
422 *nextp = iochan_destroy_real(p);
426 yaz_mutex_leave(man->iochan_mutex);
431 void iochan_man_events(iochan_man_t man)
433 if (man->no_threads > 0 && !man->sel_thread)
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",
440 event_loop(man, &man->channel_list);
446 * c-file-style: "Stroustrup"
447 * indent-tabs-mode: nil
449 * vim: shiftwidth=4 tabstop=8 expandtab