root/src/noit_check_tools.c

Revision f207009593817c50a5fb4fd4e75cbdc4c79dcfcc, 5.3 kB (checked in by Theo Schlossnagle <jesus@omniti.com>, 3 years ago)

merge dtrace branch to master

  • Property mode set to 100644
Line 
1 /*
2  * Copyright (c) 2007, OmniTI Computer Consulting, Inc.
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions are
7  * met:
8  *
9  *     * Redistributions of source code must retain the above copyright
10  *       notice, this list of conditions and the following disclaimer.
11  *     * Redistributions in binary form must reproduce the above
12  *       copyright notice, this list of conditions and the following
13  *       disclaimer in the documentation and/or other materials provided
14  *       with the distribution.
15  *     * Neither the name OmniTI Computer Consulting, Inc. nor the names
16  *       of its contributors may be used to endorse or promote products
17  *       derived from this software without specific prior written
18  *       permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
23  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
24  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
25  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
26  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
30  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31  */
32
33 #include "noit_defines.h"
34 #include "dtrace_probes.h"
35 #include "noit_check_tools.h"
36 #include "noit_check_tools_shared.h"
37 #include "utils/noit_str.h"
38
39 #include <assert.h>
40
41 typedef struct {
42   noit_module_t *self;
43   noit_check_t *check;
44   noit_check_t *cause;
45   dispatch_func_t dispatch;
46 } recur_closure_t;
47
48 static int
49 noit_check_recur_handler(eventer_t e, int mask, void *closure,
50                               struct timeval *now) {
51   recur_closure_t *rcl = closure;
52   rcl->check->fire_event = NULL; /* This is us, we get free post-return */
53   noit_check_resolve(rcl->check);
54   noit_check_schedule_next(rcl->self, &e->whence, rcl->check, now,
55                            rcl->dispatch, NULL);
56   if(NOIT_CHECK_RESOLVED(rcl->check)) {
57     if(NOIT_CHECK_DISPATCH_ENABLED()) {
58       char id[UUID_STR_LEN+1];
59       uuid_unparse_lower(rcl->check->checkid, id);
60       NOIT_CHECK_DISPATCH(id, rcl->check->module, rcl->check->name,
61                           rcl->check->target);
62     }
63     rcl->dispatch(rcl->self, rcl->check, rcl->cause);
64   }
65   else
66     noitL(noit_debug, "skipping %s`%s`%s, unresolved\n",
67           rcl->check->target, rcl->check->module, rcl->check->name);
68   free(rcl);
69   return 0;
70 }
71
72 int
73 noit_check_schedule_next(noit_module_t *self,
74                          struct timeval *last_check, noit_check_t *check,
75                          struct timeval *now, dispatch_func_t dispatch,
76                          noit_check_t *cause) {
77   eventer_t newe;
78   struct timeval period, earliest;
79   recur_closure_t *rcl;
80
81   assert(cause == NULL);
82   assert(check->fire_event == NULL);
83   if(check->period == 0) return 0;
84   if(NOIT_CHECK_DISABLED(check) || NOIT_CHECK_KILLED(check)) return 0;
85
86   /* If we have an event, we know when we intended it to fire.  This means
87    * we should schedule that point + period.
88    */
89   if(now)
90     memcpy(&earliest, now, sizeof(earliest));
91   else
92     gettimeofday(&earliest, NULL);
93
94   /* If the check is unconfigured and needs resolving, we'll set the
95    * period down a bit lower so we can pick up the resolution quickly.
96    */
97   if(!NOIT_CHECK_RESOLVED(check) && NOIT_CHECK_SHOULD_RESOLVE(check) &&
98       check->period > 1000) {
99     period.tv_sec = 1;
100     period.tv_usec = 0;
101   }
102   else {
103     period.tv_sec = check->period / 1000;
104     period.tv_usec = (check->period % 1000) * 1000;
105   }
106
107   newe = eventer_alloc();
108   memcpy(&newe->whence, last_check, sizeof(*last_check));
109   add_timeval(newe->whence, period, &newe->whence);
110   if(compare_timeval(newe->whence, earliest) < 0)
111     memcpy(&newe->whence, &earliest, sizeof(earliest));
112   newe->mask = EVENTER_TIMER;
113   newe->callback = noit_check_recur_handler;
114   rcl = calloc(1, sizeof(*rcl));
115   rcl->self = self;
116   rcl->check = check;
117   rcl->cause = cause;
118   rcl->dispatch = dispatch;
119   newe->closure = rcl;
120
121   eventer_add(newe);
122   check->fire_event = newe;
123   return 0;
124 }
125
126 void
127 noit_check_run_full_asynch_opts(noit_check_t *check, eventer_func_t callback,
128                                 int mask) {
129   struct timeval __now, p_int;
130   eventer_t e;
131   e = eventer_alloc();
132   e->fd = -1;
133   e->mask = EVENTER_ASYNCH | mask;
134   gettimeofday(&__now, NULL);
135   memcpy(&e->whence, &__now, sizeof(__now));
136   p_int.tv_sec = check->timeout / 1000;
137   p_int.tv_usec = (check->timeout % 1000) * 1000;
138   add_timeval(e->whence, p_int, &e->whence);
139   e->callback = callback;
140   e->closure =  check->closure;
141   eventer_add(e);
142 }
143 void
144 noit_check_run_full_asynch(noit_check_t *check, eventer_func_t callback) {
145   noit_check_run_full_asynch_opts(check, callback,
146                                   EVENTER_DEFAULT_ASYNCH_ABORT);
147 }
148
149 void
150 noit_check_tools_init() {
151   noit_check_tools_shared_init();
152   eventer_name_callback("noit_check_recur_handler", noit_check_recur_handler);
153 }
154
Note: See TracBrowser for help on using the browser.