Compute lucene-style scores for our hits.
[beagle.git] / glue / inotify-glue.c
blob0d0ef9e32b3849c0c0d790ca2467a58606133561
1 /*
2 * inotify-glue.c
4 * Copyright (C) 2004 Novell, Inc.
6 */
8 /*
9 * Permission is hereby granted, free of charge, to any person obtaining a
10 * copy of this software and associated documentation files (the "Software"),
11 * to deal in the Software without restriction, including without limitation
12 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
13 * and/or sell copies of the Software, and to permit persons to whom the
14 * Software is furnished to do so, subject to the following conditions:
16 * The above copyright notice and this permission notice shall be included in
17 * all copies or substantial portions of the Software.
19 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
22 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
23 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
24 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
25 * DEALINGS IN THE SOFTWARE.
28 #include <stdio.h>
29 #include <stdlib.h>
30 #include <fcntl.h>
31 #include <time.h>
32 #include <unistd.h>
33 #include <errno.h>
34 #include <sys/ioctl.h>
35 #include <sys/poll.h>
36 #include <sys/types.h>
38 #include "inotify.h"
39 #include "inotify-syscalls.h"
41 #define PROCFS_PREFIX "/proc/sys/fs/inotify"
43 #define PROCFS_MAX_USER_DEVICES PROCFS_PREFIX "/max_user_instances"
44 #define PROCFS_MAX_USER_WATCHES PROCFS_PREFIX "/max_user_watches"
45 #define PROCFS_MAX_QUEUED_EVENTS PROCFS_PREFIX "/max_queued_events"
47 /* Inotify sysfs knobs, initialized to their pre-sysfs defaults */
48 static int max_user_instances = 8;
49 static int max_user_watches = 8192;
50 static int max_queued_events = 256;
52 /* Paranoid code to read an integer from a sysfs (well, any) file. */
53 static void
54 read_int (const char *filename, int *var)
56 int fd, n;
57 char buffer[32];
58 char *buffer_endptr = NULL;
60 fd = open (filename, O_RDONLY);
61 if (fd == -1)
62 return;
63 if (read (fd, buffer, 31) > 0) {
64 n = (int) strtol (buffer, &buffer_endptr, 10);
65 if (*buffer != '\0' && *buffer_endptr == '\0')
66 *var = n;
68 close (fd);
72 int
73 inotify_glue_init (void)
75 static int fd = 0;
77 if (fd)
78 return fd;
80 fd = inotify_init ();
81 if (fd < 0) {
82 int err = errno;
83 perror ("inotify_init");
84 if (err == ENOSYS)
85 fprintf(stderr, "Inotify not supported! You need a "
86 "2.6.13 kernel or later with CONFIG_INOTIFY "
87 "enabled.");
90 read_int (PROCFS_MAX_USER_DEVICES, &max_user_instances);
91 read_int (PROCFS_MAX_USER_WATCHES, &max_user_watches);
92 read_int (PROCFS_MAX_QUEUED_EVENTS, &max_queued_events);
94 return fd;
98 int
99 inotify_glue_watch (int fd, const char *filename, __u32 mask)
101 int wd;
103 wd = inotify_add_watch (fd, filename, mask);
104 if (wd < 0) {
105 int err = errno;
106 perror ("inotify_add_watch");
107 if (err == ENOSPC)
108 fprintf(stderr, "Maximum watch limit hit. "
109 "Try adjusting " PROCFS_MAX_USER_WATCHES ".\n");
112 return wd;
117 inotify_glue_ignore (int fd, __u32 wd)
119 int ret;
121 ret = inotify_rm_watch (fd, wd);
122 if (ret < 0)
123 perror ("inotify_rm_watch");
125 return ret;
129 #define MAX_PENDING_COUNT 5
130 #define PENDING_PAUSE_NANOSECONDS 2000000
131 #define PENDING_THRESHOLD(qsize) ((unsigned int) (qsize) >> 1)
132 #define PENDING_MARGINAL_COST(p) ((unsigned int) (1 << (p)))
134 void
135 inotify_snarf_events (int fd, int timeout_ms, int *nr, void **buffer_out)
137 struct pollfd pollfd = { fd, POLLIN | POLLPRI, 0 };
138 unsigned int prev_pending = 0, pending_count = 0;
139 static struct inotify_event *buffer = NULL;
140 static size_t buffer_size;
141 int ret;
143 /* Allocate our buffer the first time we try to read events. */
144 if (buffer == NULL) {
145 /* guess the avg len */
146 buffer_size = sizeof (struct inotify_event) + 16;
147 buffer_size *= max_queued_events;
148 buffer = malloc (buffer_size);
149 if (!buffer) {
150 perror ("malloc");
151 *buffer_out = NULL;
152 return;
156 /* Set nr to 0, so it will be sure to contain something
157 valid if the poll times out. */
158 *nr = 0;
160 /* Wait for the file descriptor to be ready to read. */
161 ret = poll (&pollfd, 1, timeout_ms);
162 if (ret == -1) {
163 perror ("poll");
164 return;
165 } else if (ret == 0)
166 return;
168 /* Reading events in groups significantly helps performance.
169 * If there are some events (but not too many!) ready, wait a
170 * bit more to see if more events come in. */
172 while (pending_count < MAX_PENDING_COUNT) {
173 struct timespec ts = {0, PENDING_PAUSE_NANOSECONDS};
174 unsigned int pending;
176 if (ioctl (fd, FIONREAD, &pending) == -1)
177 break;
179 /* Don't wait if the number of pending events is too close
180 * to the maximum queue size. */
181 pending /= sizeof (struct inotify_event) + 16;
182 if (pending > PENDING_THRESHOLD (max_queued_events))
183 break;
185 /* With each successive iteration, the minimum rate for
186 * further sleep doubles. */
187 if (pending-prev_pending < PENDING_MARGINAL_COST(pending_count))
188 break;
190 prev_pending = pending;
191 ++pending_count;
193 nanosleep (&ts, NULL);
196 *nr = read (fd, buffer, buffer_size);
198 *buffer_out = buffer;