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)
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;
132 yaz_mutex_create(&man->iochan_mutex);
136 IOCHAN iochan_destroy_real(IOCHAN chan)
138 IOCHAN next = chan->next;
146 void iochan_man_destroy(iochan_man_t *mp)
151 if ((*mp)->sel_thread)
152 sel_thread_destroy((*mp)->sel_thread);
154 yaz_mutex_enter((*mp)->iochan_mutex);
155 c = (*mp)->channel_list;
156 (*mp)->channel_list = NULL;
157 yaz_mutex_leave((*mp)->iochan_mutex);
159 c = iochan_destroy_real(c);
160 yaz_mutex_destroy(&(*mp)->iochan_mutex);
167 int iochan_add(iochan_man_t man, IOCHAN chan)
169 int r = 0, no_fds = 0;
173 yaz_log(man->log_level, "iochan_add : chan=%p channel list=%p", chan,
175 yaz_mutex_enter(man->iochan_mutex);
176 for (p = man->channel_list; p; p = p->next)
178 if (man->limit_fd > 0 && no_fds >= man->limit_fd)
182 chan->next = man->channel_list;
183 man->channel_list = chan;
185 yaz_mutex_leave(man->iochan_mutex);
189 IOCHAN iochan_create(int fd, IOC_CALLBACK cb, int flags, const char *name)
193 if (!(new_iochan = (IOCHAN) xmalloc(sizeof(*new_iochan))))
196 new_iochan->destroyed = 0;
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;
203 new_iochan->thread_users = 0;
204 new_iochan->name = name ? xstrdup(name) : 0;
208 static void work_handler(void *work_data)
210 IOCHAN p = work_data;
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);
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);
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);
228 static void run_fun(iochan_man_t man, IOCHAN p)
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);
236 sel_thread_add(man->sel_thread, p);
242 static int event_loop(iochan_man_t man, IOCHAN *iochans)
244 do /* loop as long as there are active associations to process */
250 struct yaz_poll_fd *fds;
252 int connection_fired = 0;
255 yaz_mutex_enter(man->iochan_mutex);
256 start = man->channel_list;
257 yaz_mutex_leave(man->iochan_mutex);
259 for (p = start; p; p = p->next)
261 if (man->sel_fd != -1)
263 if (no_fds > man->size_fds)
265 man->size_fds = no_fds * 2;
266 man->fds = xrealloc(man->fds, man->size_fds * sizeof(*man->fds));
270 if (man->sel_fd != -1)
272 fds[i].fd = man->sel_fd;
273 fds[i].input_mask = yaz_poll_read;
274 fds[i].client_data = 0;
277 for (p = start; p; p = p->next, i++)
280 fds[i].client_data = p;
282 fds[i].input_mask = 0;
283 if (p->thread_users > 0)
285 if (p->max_idle && p->max_idle < tv_sec)
286 tv_sec = p->max_idle;
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)
299 yaz_log(man->log_level, "yaz_poll begin tv_sec=%d nofds=%d", tv_sec,
301 res = yaz_poll(fds, no_fds, tv_sec, 0);
302 yaz_log(man->log_level, "yaz_poll returned res=%d", res);
309 yaz_log(YLOG_ERRNO | YLOG_WARN, "poll");
313 if (man->sel_fd != -1)
316 assert(fds[i].fd == man->sel_fd);
317 if (fds[i].output_mask)
321 yaz_log(man->log_level, "eventl: sel input on sel_fd=%d",
323 while ((chan = sel_thread_result(man->sel_thread)))
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--;
335 for (p = start; p; p = p->next)
337 yaz_log(man->log_level, "%d channels", no);
339 for (p = start; p; p = p->next)
341 time_t now = time(0);
346 yaz_log(man->log_level,
347 "eventl: skip destroyed chan=%p name=%s", p,
348 p->name ? p->name : "");
351 if (p->thread_users > 0)
353 yaz_log(man->log_level,
354 "eventl: skip chan=%p name=%s users=%d", p,
355 p->name ? p->name : "", p->thread_users);
359 if (p->max_idle && now - p->last_event > p->max_idle)
362 p->this_event |= EVENT_TIMEOUT;
364 if (fds[i].fd >= 0 && p->fd == fds[i].fd)
366 if (fds[i].output_mask & yaz_poll_read)
369 p->this_event |= EVENT_INPUT;
371 if (fds[i].output_mask & yaz_poll_write)
374 p->this_event |= EVENT_OUTPUT;
376 if (fds[i].output_mask & yaz_poll_except)
379 p->this_event |= EVENT_EXCEPT;
382 /* only fire one Z39.50/SRU socket event.. except for timeout */
385 if (!(p->this_event & EVENT_TIMEOUT) &&
386 !strcmp(p->name, "connection_socket"))
388 /* not a timeout and we have a Z39.50/SRU socket */
389 if (connection_fired == 0)
398 assert(inv_start == start);
399 yaz_mutex_enter(man->iochan_mutex);
400 for (nextp = iochans; *nextp; )
403 if (p->destroyed && p->thread_users == 0)
404 *nextp = iochan_destroy_real(p);
408 yaz_mutex_leave(man->iochan_mutex);
413 void iochan_man_events(iochan_man_t man)
415 if (man->no_threads > 0 && !man->sel_thread)
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",
422 event_loop(man, &man->channel_list);
428 * c-file-style: "Stroustrup"
429 * indent-tabs-mode: nil
431 * vim: shiftwidth=4 tabstop=8 expandtab