1 /* -*- mode: C; c-file-style: "gnu"; indent-tabs-mode: nil; -*- */
2 /* dbus-resources.c Resource tracking/limits
4 * Copyright (C) 2003 Red Hat Inc.
6 * Licensed under the Academic Free License version 2.1
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
25 #include <dbus/dbus-resources.h>
26 #include <dbus/dbus-internals.h>
29 * @defgroup DBusResources Resource limits related code
30 * @ingroup DBusInternals
31 * @brief DBusCounter and other stuff related to resource limits
33 * Types and functions related to tracking resource limits,
34 * such as the maximum amount of memory/unix fds a connection can use
39 * @defgroup DBusResourcesInternals Resource limits implementation details
40 * @ingroup DBusInternals
41 * @brief Resource limits implementation details
43 * Implementation details of resource limits code.
49 * @brief Internals of DBusCounter.
51 * DBusCounter internals. DBusCounter is an opaque object, it must be
52 * used via accessor functions.
56 int refcount; /**< reference count */
58 long size_value; /**< current size counter value */
59 long unix_fd_value; /**< current unix fd counter value */
61 #ifdef DBUS_ENABLE_STATS
62 long peak_size_value; /**< largest ever size counter value */
63 long peak_unix_fd_value; /**< largest ever unix fd counter value */
66 long notify_size_guard_value; /**< call notify function when crossing this size value */
67 long notify_unix_fd_guard_value; /**< call notify function when crossing this unix fd value */
69 DBusCounterNotifyFunction notify_function; /**< notify function */
70 void *notify_data; /**< data for notify function */
71 dbus_bool_t notify_pending : 1; /**< TRUE if the guard value has been crossed */
72 DBusRMutex *mutex; /**< Lock on the entire DBusCounter */
75 /** @} */ /* end of resource limits internals docs */
78 * @addtogroup DBusResources
83 * Creates a new DBusCounter. DBusCounter is used
84 * to count usage of some resource such as memory.
86 * @returns new counter or #NULL on failure
89 _dbus_counter_new (void)
93 counter = dbus_new0 (DBusCounter, 1);
97 counter->refcount = 1;
99 _dbus_rmutex_new_at_location (&counter->mutex);
100 if (counter->mutex == NULL)
110 * Increments refcount of the counter
112 * @param counter the counter
113 * @returns the counter
116 _dbus_counter_ref (DBusCounter *counter)
118 _dbus_rmutex_lock (counter->mutex);
120 _dbus_assert (counter->refcount > 0);
122 counter->refcount += 1;
124 _dbus_rmutex_unlock (counter->mutex);
130 * Decrements refcount of the counter and possibly
131 * finalizes the counter.
133 * @param counter the counter
136 _dbus_counter_unref (DBusCounter *counter)
138 dbus_bool_t last_ref = FALSE;
140 _dbus_rmutex_lock (counter->mutex);
142 _dbus_assert (counter->refcount > 0);
144 counter->refcount -= 1;
145 last_ref = (counter->refcount == 0);
147 _dbus_rmutex_unlock (counter->mutex);
151 _dbus_rmutex_free_at_location (&counter->mutex);
157 * Adjusts the value of the size counter by the given
158 * delta which may be positive or negative.
160 * This function may be called with locks held. After calling it, when
161 * any relevant locks are no longer held you must call _dbus_counter_notify().
163 * @param counter the counter
164 * @param delta value to add to the size counter's current value
167 _dbus_counter_adjust_size (DBusCounter *counter,
172 _dbus_rmutex_lock (counter->mutex);
174 old = counter->size_value;
176 counter->size_value += delta;
178 #ifdef DBUS_ENABLE_STATS
179 if (counter->peak_size_value < counter->size_value)
180 counter->peak_size_value = counter->size_value;
184 _dbus_verbose ("Adjusting counter %ld by %ld = %ld\n",
185 old, delta, counter->size_value);
188 if (counter->notify_function != NULL &&
189 ((old < counter->notify_size_guard_value &&
190 counter->size_value >= counter->notify_size_guard_value) ||
191 (old >= counter->notify_size_guard_value &&
192 counter->size_value < counter->notify_size_guard_value)))
193 counter->notify_pending = TRUE;
195 _dbus_rmutex_unlock (counter->mutex);
199 * Calls the notify function from _dbus_counter_set_notify(),
200 * if that function has been specified and the counter has crossed the
201 * guard value (in either direction) since the last call to this function.
203 * This function must not be called with locks held, since it can call out
207 _dbus_counter_notify (DBusCounter *counter)
209 DBusCounterNotifyFunction notify_function = NULL;
210 void *notify_data = NULL;
212 _dbus_rmutex_lock (counter->mutex);
213 if (counter->notify_pending)
215 counter->notify_pending = FALSE;
216 notify_function = counter->notify_function;
217 notify_data = counter->notify_data;
219 _dbus_rmutex_unlock (counter->mutex);
221 if (notify_function != NULL)
222 (* notify_function) (counter, notify_data);
226 * Adjusts the value of the unix fd counter by the given
227 * delta which may be positive or negative.
229 * This function may be called with locks held. After calling it, when
230 * any relevant locks are no longer held you must call _dbus_counter_notify().
232 * @param counter the counter
233 * @param delta value to add to the unix fds counter's current value
236 _dbus_counter_adjust_unix_fd (DBusCounter *counter,
241 _dbus_rmutex_lock (counter->mutex);
243 old = counter->unix_fd_value;
245 counter->unix_fd_value += delta;
247 #ifdef DBUS_ENABLE_STATS
248 if (counter->peak_unix_fd_value < counter->unix_fd_value)
249 counter->peak_unix_fd_value = counter->unix_fd_value;
253 _dbus_verbose ("Adjusting counter %ld by %ld = %ld\n",
254 old, delta, counter->unix_fd_value);
257 if (counter->notify_function != NULL &&
258 ((old < counter->notify_unix_fd_guard_value &&
259 counter->unix_fd_value >= counter->notify_unix_fd_guard_value) ||
260 (old >= counter->notify_unix_fd_guard_value &&
261 counter->unix_fd_value < counter->notify_unix_fd_guard_value)))
262 counter->notify_pending = TRUE;
264 _dbus_rmutex_unlock (counter->mutex);
268 * Gets the current value of the size counter.
270 * @param counter the counter
271 * @returns its current size value
274 _dbus_counter_get_size_value (DBusCounter *counter)
276 return counter->size_value;
280 * Gets the current value of the unix fd counter.
282 * @param counter the counter
283 * @returns its current unix fd value
286 _dbus_counter_get_unix_fd_value (DBusCounter *counter)
288 return counter->unix_fd_value;
292 * Sets the notify function for this counter; the notify function is
293 * called whenever the counter's values cross the guard values in
294 * either direction (moving up, or moving down).
296 * @param counter the counter
297 * @param size_guard_value the value we're notified if the size counter crosses
298 * @param unix_fd_guard_value the value we're notified if the unix fd counter crosses
299 * @param function function to call in order to notify
300 * @param user_data data to pass to the function
303 _dbus_counter_set_notify (DBusCounter *counter,
304 long size_guard_value,
305 long unix_fd_guard_value,
306 DBusCounterNotifyFunction function,
309 _dbus_rmutex_lock (counter->mutex);
310 counter->notify_size_guard_value = size_guard_value;
311 counter->notify_unix_fd_guard_value = unix_fd_guard_value;
312 counter->notify_function = function;
313 counter->notify_data = user_data;
314 counter->notify_pending = FALSE;
315 _dbus_rmutex_unlock (counter->mutex);
318 #ifdef DBUS_ENABLE_STATS
320 _dbus_counter_get_peak_size_value (DBusCounter *counter)
322 return counter->peak_size_value;
326 _dbus_counter_get_peak_unix_fd_value (DBusCounter *counter)
328 return counter->peak_unix_fd_value;
332 /** @} */ /* end of resource limits exported API */