mirror of
https://github.com/cuberite/libevent.git
synced 2025-08-04 09:46:23 -04:00
Merge remote-tracking branch 'asweeny86/event-count-max'
This commit is contained in:
commit
f9e091bf4e
@ -224,10 +224,16 @@ struct event_base {
|
||||
|
||||
/** Number of virtual events */
|
||||
int virtual_event_count;
|
||||
/** Maximum number of virtual events active */
|
||||
int virtual_event_count_max;
|
||||
/** Number of total events added to this event_base */
|
||||
int event_count;
|
||||
/** Maximum number of total events added to this event_base */
|
||||
int event_count_max;
|
||||
/** Number of total events active in this event_base */
|
||||
int event_count_active;
|
||||
/** Maximum number of total events active in this event_base */
|
||||
int event_count_active_max;
|
||||
|
||||
/** Set if we should terminate the loop once we're done processing
|
||||
* events. */
|
||||
|
46
event.c
46
event.c
@ -1204,6 +1204,36 @@ event_base_get_num_events(struct event_base *base, unsigned int type)
|
||||
return r;
|
||||
}
|
||||
|
||||
int
|
||||
event_base_get_max_events(struct event_base *base, unsigned int type, int clear)
|
||||
{
|
||||
int r = 0;
|
||||
|
||||
EVBASE_ACQUIRE_LOCK(base, th_base_lock);
|
||||
|
||||
if (type & EVENT_BASE_COUNT_ACTIVE) {
|
||||
r += base->event_count_active_max;
|
||||
if (clear)
|
||||
base->event_count_active_max = 0;
|
||||
}
|
||||
|
||||
if (type & EVENT_BASE_COUNT_VIRTUAL) {
|
||||
r += base->virtual_event_count_max;
|
||||
if (clear)
|
||||
base->virtual_event_count_max = 0;
|
||||
}
|
||||
|
||||
if (type & EVENT_BASE_COUNT_ADDED) {
|
||||
r += base->event_count_max;
|
||||
if (clear)
|
||||
base->event_count_max = 0;
|
||||
}
|
||||
|
||||
EVBASE_RELEASE_LOCK(base, th_base_lock);
|
||||
|
||||
return r;
|
||||
}
|
||||
|
||||
/* Returns true iff we're currently watching any events. */
|
||||
static int
|
||||
event_haveevents(struct event_base *base)
|
||||
@ -3026,14 +3056,23 @@ timeout_process(struct event_base *base)
|
||||
#if (EVLIST_INTERNAL >> 4) != 1
|
||||
#error "Mismatch for value of EVLIST_INTERNAL"
|
||||
#endif
|
||||
|
||||
#ifndef MAX
|
||||
#define MAX(a,b) (((a)>(b))?(a):(b))
|
||||
#endif
|
||||
|
||||
#define MAX_EVENT_COUNT(var, v) var = MAX(var, v)
|
||||
|
||||
/* These are a fancy way to spell
|
||||
if (flags & EVLIST_INTERNAL)
|
||||
base->event_count--/++;
|
||||
*/
|
||||
#define DECR_EVENT_COUNT(base,flags) \
|
||||
((base)->event_count -= (~((flags) >> 4) & 1))
|
||||
#define INCR_EVENT_COUNT(base,flags) \
|
||||
((base)->event_count += (~((flags) >> 4) & 1))
|
||||
#define INCR_EVENT_COUNT(base,flags) do { \
|
||||
((base)->event_count += (~((flags) >> 4) & 1)); \
|
||||
MAX_EVENT_COUNT((base)->event_count_max, (base)->event_count); \
|
||||
} while (0)
|
||||
|
||||
static void
|
||||
event_queue_remove_inserted(struct event_base *base, struct event *ev)
|
||||
@ -3203,6 +3242,7 @@ event_queue_insert_active(struct event_base *base, struct event_callback *evcb)
|
||||
evcb->evcb_flags |= EVLIST_ACTIVE;
|
||||
|
||||
base->event_count_active++;
|
||||
MAX_EVENT_COUNT(base->event_count_active_max, base->event_count_active);
|
||||
EVUTIL_ASSERT(evcb->evcb_pri < base->nactivequeues);
|
||||
TAILQ_INSERT_TAIL(&base->activequeues[evcb->evcb_pri],
|
||||
evcb, evcb_active_next);
|
||||
@ -3220,6 +3260,7 @@ event_queue_insert_active_later(struct event_base *base, struct event_callback *
|
||||
INCR_EVENT_COUNT(base, evcb->evcb_flags);
|
||||
evcb->evcb_flags |= EVLIST_ACTIVE_LATER;
|
||||
base->event_count_active++;
|
||||
MAX_EVENT_COUNT(base->event_count_active_max, base->event_count_active);
|
||||
EVUTIL_ASSERT(evcb->evcb_pri < base->nactivequeues);
|
||||
TAILQ_INSERT_TAIL(&base->active_later_queue, evcb, evcb_active_next);
|
||||
}
|
||||
@ -3654,6 +3695,7 @@ event_base_add_virtual_(struct event_base *base)
|
||||
{
|
||||
EVBASE_ACQUIRE_LOCK(base, th_base_lock);
|
||||
base->virtual_event_count++;
|
||||
MAX_EVENT_COUNT(base->virtual_event_count_max, base->virtual_event_count);
|
||||
EVBASE_RELEASE_LOCK(base, th_base_lock);
|
||||
}
|
||||
|
||||
|
@ -424,6 +424,18 @@ const char **event_get_supported_methods(void);
|
||||
*/
|
||||
int event_base_get_num_events(struct event_base *, unsigned int);
|
||||
|
||||
/**
|
||||
Get the maximum number of events in a given event_base as specified in the
|
||||
flags.
|
||||
|
||||
@param eb the event_base structure returned by event_base_new()
|
||||
@param flags a bitwise combination of the kinds of events to aggregate
|
||||
counts for
|
||||
@param clear option used to reset the maximum count.
|
||||
@return the number of events specified in the flags
|
||||
*/
|
||||
int event_base_get_max_events(struct event_base *, unsigned int, int);
|
||||
|
||||
/**
|
||||
Allocates a new event configuration object.
|
||||
|
||||
|
145
test/regress.c
145
test/regress.c
@ -1435,6 +1435,150 @@ end:
|
||||
;
|
||||
}
|
||||
|
||||
static void
|
||||
test_event_base_get_max_events(void *ptr)
|
||||
{
|
||||
struct basic_test_data *data = ptr;
|
||||
struct event_base *base = data->base;
|
||||
struct event ev;
|
||||
struct event ev2;
|
||||
int event_count_active;
|
||||
int event_count_virtual;
|
||||
int event_count_added;
|
||||
int event_count_active_virtual;
|
||||
int event_count_active_added;
|
||||
int event_count_virtual_added;
|
||||
int event_count_active_added_virtual;
|
||||
|
||||
struct timeval qsec = {0, 100000};
|
||||
|
||||
event_assign(&ev, base, -1, EV_READ, event_selfarg_cb,
|
||||
event_self_cbarg());
|
||||
event_assign(&ev2, base, -1, EV_READ, event_selfarg_cb,
|
||||
event_self_cbarg());
|
||||
|
||||
event_add(&ev, &qsec);
|
||||
event_add(&ev2, &qsec);
|
||||
event_del(&ev2);
|
||||
|
||||
event_count_active = event_base_get_max_events(base,
|
||||
EVENT_BASE_COUNT_ACTIVE, 0);
|
||||
event_count_virtual = event_base_get_max_events(base,
|
||||
EVENT_BASE_COUNT_VIRTUAL, 0);
|
||||
event_count_added = event_base_get_max_events(base,
|
||||
EVENT_BASE_COUNT_ADDED, 0);
|
||||
event_count_active_virtual = event_base_get_max_events(base,
|
||||
EVENT_BASE_COUNT_ACTIVE | EVENT_BASE_COUNT_VIRTUAL, 0);
|
||||
event_count_active_added = event_base_get_max_events(base,
|
||||
EVENT_BASE_COUNT_ACTIVE | EVENT_BASE_COUNT_ADDED, 0);
|
||||
event_count_virtual_added = event_base_get_max_events(base,
|
||||
EVENT_BASE_COUNT_VIRTUAL | EVENT_BASE_COUNT_ADDED, 0);
|
||||
event_count_active_added_virtual = event_base_get_max_events(base,
|
||||
EVENT_BASE_COUNT_ACTIVE |
|
||||
EVENT_BASE_COUNT_ADDED |
|
||||
EVENT_BASE_COUNT_VIRTUAL, 0);
|
||||
|
||||
tt_int_op(event_count_active, ==, 0);
|
||||
tt_int_op(event_count_virtual, ==, 0);
|
||||
/* libevent itself adds a timeout event, so the event_count is 4 here */
|
||||
tt_int_op(event_count_added, ==, 4);
|
||||
tt_int_op(event_count_active_virtual, ==, 0);
|
||||
tt_int_op(event_count_active_added, ==, 4);
|
||||
tt_int_op(event_count_virtual_added, ==, 4);
|
||||
tt_int_op(event_count_active_added_virtual, ==, 4);
|
||||
|
||||
event_active(&ev, EV_READ, 1);
|
||||
event_count_active = event_base_get_max_events(base,
|
||||
EVENT_BASE_COUNT_ACTIVE, 0);
|
||||
event_count_virtual = event_base_get_max_events(base,
|
||||
EVENT_BASE_COUNT_VIRTUAL, 0);
|
||||
event_count_added = event_base_get_max_events(base,
|
||||
EVENT_BASE_COUNT_ADDED, 0);
|
||||
event_count_active_virtual = event_base_get_max_events(base,
|
||||
EVENT_BASE_COUNT_ACTIVE | EVENT_BASE_COUNT_VIRTUAL, 0);
|
||||
event_count_active_added = event_base_get_max_events(base,
|
||||
EVENT_BASE_COUNT_ACTIVE | EVENT_BASE_COUNT_ADDED, 0);
|
||||
event_count_virtual_added = event_base_get_max_events(base,
|
||||
EVENT_BASE_COUNT_VIRTUAL | EVENT_BASE_COUNT_ADDED, 0);
|
||||
event_count_active_added_virtual = event_base_get_max_events(base,
|
||||
EVENT_BASE_COUNT_ACTIVE |
|
||||
EVENT_BASE_COUNT_ADDED |
|
||||
EVENT_BASE_COUNT_VIRTUAL, 0);
|
||||
|
||||
tt_int_op(event_count_active, ==, 1);
|
||||
tt_int_op(event_count_virtual, ==, 0);
|
||||
tt_int_op(event_count_added, ==, 4);
|
||||
tt_int_op(event_count_active_virtual, ==, 1);
|
||||
tt_int_op(event_count_active_added, ==, 5);
|
||||
tt_int_op(event_count_virtual_added, ==, 4);
|
||||
tt_int_op(event_count_active_added_virtual, ==, 5);
|
||||
|
||||
event_base_loop(base, 0);
|
||||
event_count_active = event_base_get_max_events(base,
|
||||
EVENT_BASE_COUNT_ACTIVE, 1);
|
||||
event_count_virtual = event_base_get_max_events(base,
|
||||
EVENT_BASE_COUNT_VIRTUAL, 1);
|
||||
event_count_added = event_base_get_max_events(base,
|
||||
EVENT_BASE_COUNT_ADDED, 1);
|
||||
event_count_active_virtual = event_base_get_max_events(base,
|
||||
EVENT_BASE_COUNT_ACTIVE | EVENT_BASE_COUNT_VIRTUAL, 0);
|
||||
event_count_active_added = event_base_get_max_events(base,
|
||||
EVENT_BASE_COUNT_ACTIVE | EVENT_BASE_COUNT_ADDED, 0);
|
||||
event_count_virtual_added = event_base_get_max_events(base,
|
||||
EVENT_BASE_COUNT_VIRTUAL | EVENT_BASE_COUNT_ADDED, 0);
|
||||
event_count_active_added_virtual = event_base_get_max_events(base,
|
||||
EVENT_BASE_COUNT_ACTIVE |
|
||||
EVENT_BASE_COUNT_ADDED |
|
||||
EVENT_BASE_COUNT_VIRTUAL, 1);
|
||||
|
||||
tt_int_op(event_count_active, ==, 1);
|
||||
tt_int_op(event_count_virtual, ==, 0);
|
||||
tt_int_op(event_count_added, ==, 4);
|
||||
tt_int_op(event_count_active_virtual, ==, 0);
|
||||
tt_int_op(event_count_active_added, ==, 0);
|
||||
tt_int_op(event_count_virtual_added, ==, 0);
|
||||
tt_int_op(event_count_active_added_virtual, ==, 0);
|
||||
|
||||
event_count_active = event_base_get_max_events(base,
|
||||
EVENT_BASE_COUNT_ACTIVE, 0);
|
||||
event_count_virtual = event_base_get_max_events(base,
|
||||
EVENT_BASE_COUNT_VIRTUAL, 0);
|
||||
event_count_added = event_base_get_max_events(base,
|
||||
EVENT_BASE_COUNT_ADDED, 0);
|
||||
tt_int_op(event_count_active, ==, 0);
|
||||
tt_int_op(event_count_virtual, ==, 0);
|
||||
tt_int_op(event_count_added, ==, 0);
|
||||
|
||||
event_base_add_virtual_(base);
|
||||
event_count_active = event_base_get_max_events(base,
|
||||
EVENT_BASE_COUNT_ACTIVE, 0);
|
||||
event_count_virtual = event_base_get_max_events(base,
|
||||
EVENT_BASE_COUNT_VIRTUAL, 0);
|
||||
event_count_added = event_base_get_max_events(base,
|
||||
EVENT_BASE_COUNT_ADDED, 0);
|
||||
event_count_active_virtual = event_base_get_max_events(base,
|
||||
EVENT_BASE_COUNT_ACTIVE | EVENT_BASE_COUNT_VIRTUAL, 0);
|
||||
event_count_active_added = event_base_get_max_events(base,
|
||||
EVENT_BASE_COUNT_ACTIVE | EVENT_BASE_COUNT_ADDED, 0);
|
||||
event_count_virtual_added = event_base_get_max_events(base,
|
||||
EVENT_BASE_COUNT_VIRTUAL | EVENT_BASE_COUNT_ADDED, 0);
|
||||
event_count_active_added_virtual = event_base_get_max_events(base,
|
||||
EVENT_BASE_COUNT_ACTIVE |
|
||||
EVENT_BASE_COUNT_ADDED |
|
||||
EVENT_BASE_COUNT_VIRTUAL, 0);
|
||||
|
||||
tt_int_op(event_count_active, ==, 0);
|
||||
tt_int_op(event_count_virtual, ==, 1);
|
||||
tt_int_op(event_count_added, ==, 0);
|
||||
tt_int_op(event_count_active_virtual, ==, 1);
|
||||
tt_int_op(event_count_active_added, ==, 0);
|
||||
tt_int_op(event_count_virtual_added, ==, 1);
|
||||
tt_int_op(event_count_active_added_virtual, ==, 1);
|
||||
|
||||
end:
|
||||
;
|
||||
}
|
||||
|
||||
static void
|
||||
test_bad_assign(void *ptr)
|
||||
{
|
||||
@ -3062,6 +3206,7 @@ struct testcase_t main_testcases[] = {
|
||||
BASIC(event_new_selfarg, TT_FORK|TT_NEED_BASE),
|
||||
BASIC(event_assign_selfarg, TT_FORK|TT_NEED_BASE),
|
||||
BASIC(event_base_get_num_events, TT_FORK|TT_NEED_BASE),
|
||||
BASIC(event_base_get_max_events, TT_FORK|TT_NEED_BASE),
|
||||
|
||||
BASIC(bad_assign, TT_FORK|TT_NEED_BASE|TT_NO_LOGS),
|
||||
BASIC(bad_reentrant, TT_FORK|TT_NEED_BASE|TT_NO_LOGS),
|
||||
|
Loading…
x
Reference in New Issue
Block a user