Main Page | Modules | Alphabetical List | Data Structures | File List | Data Fields | Globals | Related Pages

monitor.h

Go to the documentation of this file.
00001 
00002 /*
00003 ** Copyright (C) 2005 Russell Leighton
00004 **
00005 ** This program is free software; you can redistribute it and/or modify
00006 ** it under the terms of the GNU General Public License as published by
00007 ** the Free Software Foundation; either version 2 of the License, or
00008 ** (at your option) any later version.
00009 **
00010 ** This program is distributed in the hope that it will be useful,
00011 ** but WITHOUT ANY WARRANTY; without even the implied warranty of
00012 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00013 ** GNU General Public License for more details.
00014 **
00015 ** You should have received a copy of the GNU General Public License
00016 ** along with this program; if not, write to the Free Software
00017 ** Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
00018 */
00019 
00020 
00021 #ifndef __AWMONITOR__
00022 #define __AWMONITOR__
00023 
00144 #include "sysnet.h"
00145 
00146 #include "util.h"
00147 #include "address.h"
00148 #include "hash.h"
00149 #include "mask.h"
00150 #include "log.h"
00151 
00152 #include "monsignals.h"
00153 #include "monthread.h"
00154 #include "event.h"
00155 #include "alarm.h"
00156 #include "handler.h"
00157 #include "tasklet.h"
00158 
00159 /* stack sizes for threads */
00160 #define AW_ALARM_THREAD_STACKSIZE (8*1024)
00161 #define AW_HANDLER_THREAD_STACKSIZE (128*1024)
00162 #define AW_TASKLET_THREAD_STACKSIZE (512*1024)
00163 
00164 /* default event factory cache size */
00165 #define AW_DEFAULT_EVENT_CACHE_SIZE 256
00166 
00167 /* default alarm factory cache size */
00168 #define AW_DEFAULT_ALARM_CACHE_SIZE 256
00169 
00170 /* default fdcallback factory cache size */
00171 #define AW_DEFAULT_FDCALLBACK_CACHE_SIZE 256
00172 
00173 /* default irecallback factory cache size */
00174 #define AW_DEFAULT_IRECALLBACK_CACHE_SIZE 256
00175 
00176 /* fixed depth of fd q for signals */
00177 #define AW_FD_QSIZE 1024
00178 
00179 /* -------- Alarm --------- */
00180 
00181 typedef struct {
00182 
00183   /* duty cycle */
00184   aw_timestamp_t
00185    cycle_time;
00186 
00187   /* number of alarms */
00188   u_int32_t
00189    nalarms;
00190 
00191   /* list of alarms */
00192   aw_alarm_t
00193    *alarms;
00194 
00195   /* thread to run alarms */
00196   aw_monthread_t
00197    *thread;
00198 
00199   /* holds command events for the alarm_q */
00200   aw_event_q_t
00201    alarm_cmd_event_q;
00202   aw_plock_t
00203    alarm_cmd_event_q_lock;
00204 
00205   /* event factory */
00206   aw_cache_allocator_t
00207    event_factory;
00208 
00209   /* alarm factory */
00210   aw_cache_allocator_t
00211    alarm_factory;
00212   aw_plock_t
00213    alarm_factory_lock;
00214 
00215 } aw_monitor_alarm_q_t;
00216 
00217 
00218 /* -------- Handler --------- */
00219 /* used to abstract the lookup of handler event q's */
00220 typedef aw_hash32_t aw_handler_qlookup_t;
00221 
00222 /* used to abstract the masked lookup of handler event q's */
00223 typedef aw_mask_t aw_handler_maskqlookup_t;
00224 
00225 /* points to handler's event q's */
00226 typedef struct aw_handler_q_s {
00227 
00228   /* used to link together the runnable q's */
00229   struct aw_handler_q_s 
00230    *next_runnable;
00231 
00232   /* non-zero if in runnable state */
00233   u_int32_t
00234    runnable;
00235 
00236   /* master list */
00237   struct aw_handler_q_s 
00238    *prev,*next;
00239   
00240   /* the handler for events in this q */
00241   aw_handler_t
00242    *handler;
00243 
00244   /* active event q */
00245   aw_event_q_t
00246    q;
00247 
00248   /* deferred event q, events arriving while handler running in tasklet */
00249   aw_event_q_t
00250    deferred_q;
00251 
00252  /* wait event q, events arriving if handler is waiting on xaction signal */
00253   aw_event_q_t
00254    wait_q;
00255 
00256 } aw_handler_q_t;
00257 
00258 typedef struct {
00259 
00260   /* number of handlers */
00261   u_int32_t
00262    nhandlers;
00263 
00264   /* lookup table for handlers */
00265   struct {
00266 
00267     /* need to lock the hash and counter */
00268     aw_plock_t
00269      lock;
00270 
00271     /* sequencer */
00272     u_int32_t
00273      counter;
00274 
00275     /* table */
00276     aw_hash32_t
00277      *id2handler;
00278 
00279   } lut ;
00280 
00281   /* event handler qs for above handlers */
00282   aw_handler_q_t
00283    *handler_qs;
00284 
00285   /* lookup table for handler's q */
00286   aw_handler_qlookup_t
00287    *handler_qlookup_byevent;
00288 
00289   /* mask lookup table for handler's q*/
00290   aw_handler_maskqlookup_t
00291    *handler_maskqlookup_byevent;
00292 
00293   /* thread to run event handlers */
00294   aw_monthread_t
00295    *thread;
00296 
00297   /* non-zero if thread has been initialized */
00298   u_int32_t
00299    running;
00300 
00301   /* holds command events for the handler_q */
00302   aw_event_q_t
00303    handler_cmd_event_q;
00304   aw_plock_t
00305    handler_cmd_event_q_lock;
00306 
00307   /* q to hold events generated by
00308      handlers that will be dispatched
00309      to other handlers */
00310   aw_event_q_t
00311    handlers_event_q;
00312 
00313   /* event factory */
00314   aw_cache_allocator_t
00315    event_factory;
00316 
00317 } aw_monitor_handler_q_t;
00318 
00319 /* -------- Monitor --------- */
00320 
00321 typedef struct {
00322 
00323   /* lock for manipulating q's */
00324   aw_plock_t
00325    lock;
00326 
00327   /* mapping of interrupt event id -> icallback */
00328   aw_hash32_t
00329    *ire2callback;
00330 
00331   /* holds events sent externally into monitor */
00332   aw_event_q_t
00333    input_event_q;
00334 
00335   /* holds events relayed from alarm_q to handler_q */
00336   aw_event_q_t
00337    alarm_generated_event_q;
00338 
00339   /* holds events relayed handler_q to alarm_q for reuse */
00340   aw_event_q_t
00341    alarm_processed_event_q;
00342 
00343   /* holds interrupt events relayed from alarm_q to handler_q */
00344   aw_event_q_t
00345    alarm_generated_interrupt_q;
00346 
00347   /* holds interrupt events relayed handler_q to alarm_q for reuse */
00348   aw_event_q_t
00349    alarm_processed_interrupt_q;
00350 
00351   /* holds events relayed from rx_q to handler_q */
00352   aw_event_q_t
00353    rx_generated_event_q;
00354 
00355   /* holds events relayed handler_q to rx_q for reuse */
00356   aw_event_q_t
00357    rx_processed_event_q;
00358 
00359   /* holds command events for the rx_q */
00360   aw_event_q_t
00361    rx_cmd_event_q;
00362 
00363   /* object cache */
00364   aw_cache_allocator_t
00365    irecallback_factory;
00366 
00367 } aw_monitor_relay_t;
00368 
00369 /* describes fd q */
00370 typedef struct {
00371 
00372   /* how many in the q */
00373   u_int16_t
00374    nentries;
00375 
00376   /* how many failed inserts into q */
00377   u_int16_t
00378    nmisses;
00379 
00380 } aw_monitor_fd_q_info_t;
00381 
00382 
00383 /* holds fds delivered via signals */
00384 typedef struct {
00385 
00386   aw_monitor_fd_q_info_t
00387    info;
00388 
00389   u_int16_t
00390    fds[AW_FD_QSIZE];
00391 
00392 } aw_monitor_fd_q_t;
00393 
00394 typedef struct {
00395 
00396   /* NOTE: all access MUST be from within handler thread! */
00397   
00398   /* mapping of fd -> fdcallback */
00399   aw_hash32_t
00400    *fd2callback;
00401 
00402   /* holds fds generated from signal handler ...
00403      uses double buffer technique to minimize race condition */
00404   volatile aw_monitor_fd_q_t
00405    *current_fd_q,
00406    *next_fd_q;
00407   aw_monitor_fd_q_t
00408    signal_fd_q1,
00409    signal_fd_q2;
00410 
00411   /* object cache */
00412   aw_cache_allocator_t
00413    fdcallback_factory;
00414 
00415 } aw_monitor_fd_relay_t;
00416 
00420 typedef struct aw_monitor_handler_fdcallback_s {
00421 
00422   /* the fd */
00423   int32_t
00424    fd;
00425 
00426   aw_handler_t
00427    *handler;
00428 
00429   /* the method to call when there is data ready to read on fd */
00430   aw_handler_code_t 
00431    (*callback)(aw_handler_args_t *args, int32_t fd );
00432 
00433 } aw_monitor_handler_fdcallback_t;
00434 
00438 typedef struct aw_monitor_handler_irecallback_s {
00439 
00440   aw_alarm_sched_t 
00441    sched;
00442 
00443   aw_alarm_t
00444    *interrupt;
00445 
00446   aw_handler_t
00447    *handler;
00448 
00449   /* the method to call when there is data ready to read on fd */
00450   aw_handler_code_t 
00451    (*callback)(aw_handler_args_t *args, aw_event_t *e );
00452 
00453 } aw_monitor_handler_irecallback_t;
00454 
00455 
00459 typedef struct aw_monitor_xaction_state_s {
00460 
00461   aw_hash32_t
00462    *handler_xaction2count;
00463 
00464 } aw_monitor_xaction_state_t;
00465 
00469 typedef struct aw_monitor_sched_s {
00470 
00471   int32_t
00472    policy;
00473 
00474   struct sched_param
00475    params;
00476 
00477 } aw_monitor_sched_t;
00478 
00482 typedef enum {
00483   AW_MONITOR_STOP_STATE,
00484   AW_MONITOR_RUN_STATE,
00485   AW_MONITOR_PAUSE_STATE,
00486   AW_MONITOR_FUBAR_STATE,
00487   AW_MONITOR_NSTATES              /* must be last! */
00488 } aw_monitor_state_t;
00489 
00493 typedef struct aw_monitor_s {
00494 
00495   /* one of aw_monitor_state_t */
00496   aw_monitor_state_t
00497    state;   
00498 
00499   /* logger for warning/error messages */
00500   aw_logger_t
00501    logger;
00502 
00503   /* lock for manipulating monitor */
00504   aw_plock_t
00505    lock;
00506 
00507   /* alarms */
00508   aw_monitor_alarm_q_t
00509    alarm_q;
00510 
00511   /* id squencer for event sigs + lock */
00512   struct {
00513 
00514     /* for mask based addressing, needed
00515        so that handlers don't accidently
00516        reuse mask bits */
00517     u_int32_t
00518      high_mask_bit; // highest mask bit available
00519 
00520     /* for point 2 point addressing */
00521     aw_address_t 
00522      counter;
00523 
00524     aw_plock_t
00525      lock;
00526 
00527   } event_addr;
00528 
00529   /* sychronized xfer point for events */
00530   aw_monitor_relay_t
00531    relay_q;
00532 
00533   /* sychronized xfer point for fd events */
00534   aw_monitor_fd_relay_t
00535    fd_relay_q;
00536 
00537   /* event handlers */
00538   aw_monitor_handler_q_t
00539    handler_q;
00540 
00541   /* thread pool for handler tasklets */
00542   aw_tasklet_pool_t
00543    handler_tasklet_pool;
00544 
00545   /* info about current transactions */
00546   aw_monitor_xaction_state_t
00547    xaction_state;
00548 
00549   /* used if you want to send signals to creating process */
00550   pid_t
00551    creation_pid;
00552 
00553   /* thread control */
00554   u_int32_t
00555    threadstop;
00556 
00557 } aw_monitor_t;
00558 
00562 typedef struct {
00563 
00564   /* logger params for warning/error messages */
00565   aw_logger_params_t
00566    logger_params;
00567 
00568   /* process init */
00569   aw_monitor_sched_t
00570    sched;
00571 
00572 } aw_monitor_params_t;
00573 
00574 /* #################### macros ######################## */
00575 
00576 /* change default min level for debugging */
00577 #ifdef AW_DEBUG
00578   #define AW_MONITOR_LOGGER_DEFAULT_MINLEVEL AW_DEBUG_LOG_LEVEL 
00579 #else
00580   #define AW_MONITOR_LOGGER_DEFAULT_MINLEVEL AW_INFO_LOG_LEVEL 
00581 #endif
00582 
00586 #define AW_MONITOR_LOGGER_DEFAULT_PARAMS { \
00587 \
00588  .filename = NULL,\
00589 \
00590  /* stderr */ \
00591  .fd = 2, \
00592  \
00593  .min_level = AW_MONITOR_LOGGER_DEFAULT_MINLEVEL,\
00594 \
00595  .max_level = AW_ERROR_LOG_LEVEL, \
00596 \
00597  .strlevel = NULL, \
00598 \
00599  .flags = AW_LOGGER_DEFAULT_FLAGS, \
00600 \
00601  .error_logger = NULL \
00602 }
00603 
00604 /* @define AW_MONITOR_SCHED_DEFAULT_PARAMS
00605    Default process scheduling parameters
00606 */
00607 #define AW_MONITOR_SCHED_DEFAULT_PARAMS {\
00608 \
00609  .policy = SCHED_OTHER, \
00610  .params.sched_priority = 0 \
00611 }
00612 
00616 #define AW_MONITOR_DEFAULT_PARAMS { \
00617  .logger_params = AW_MONITOR_LOGGER_DEFAULT_PARAMS , \
00618  .sched = AW_MONITOR_SCHED_DEFAULT_PARAMS \
00619 }
00620 
00621 /* #################### exported functions ######################## */
00622 
00623 /* monitor.c */
00624 
00631 aw_address_t aw_reserve_event_mask_bit(aw_monitor_t *mon);
00632 
00639 aw_address_t aw_next_event_id(aw_monitor_t *mon);
00640 
00648 aw_handlerid_t aw_add_handler(aw_monitor_t *mon, aw_handler_t *handler);
00649 
00657 void aw_reschedule_handler(aw_monitor_t *mon, aw_handler_t *handler, aw_alarm_sched_t *sched);
00658 
00665 void aw_remove_handler(aw_monitor_t *mon, aw_handlerid_t id);
00666 
00676 int32_t aw_add_handler_fdcallback(aw_monitor_t *mon, aw_handler_t *handler, int32_t fd,   
00677                                   aw_handler_code_t (*fdcallback)(aw_handler_args_t *args, int32_t fd ));
00678 
00686 int32_t aw_remove_handler_fdcallback(aw_monitor_t *mon, int32_t fd);
00687 
00698 int32_t aw_add_handler_irecallback(aw_monitor_t *mon, aw_handler_t *handler,
00699                                    aw_address_t ire_id, aw_alarm_sched_t *sched,
00700                                    aw_handler_code_t (*callback)(aw_handler_args_t *args, aw_event_t *e ));
00701 
00709 int32_t aw_remove_handler_irecallback(aw_monitor_t *mon, aw_address_t ire_id);
00710 
00711 
00718 void aw_signal_handler_irecallback(aw_monitor_t *mon, aw_address_t ire_id);
00719 
00720 
00727 void aw_monitor_append_q(aw_event_q_t *q, aw_monitor_t *mon);
00728 
00735 void aw_monitor_append_event(aw_event_t *event, aw_monitor_t *mon);
00736 
00744 void aw_monitor_int32_event(aw_monitor_t *mon, aw_event_sig_t *event_sig, u_int32_t val);
00745 
00753 void aw_monitor_int64_event(aw_monitor_t *mon, aw_event_sig_t *event_sig, u_int64_t val);
00754 
00755 
00763 void aw_monitor_float32_event(aw_monitor_t *mon, aw_event_sig_t *event_sig, float32_t val);
00764 
00772 void aw_monitor_float64_event(aw_monitor_t *mon, aw_event_sig_t *event_sig, float64_t val);
00773 
00774 
00775 
00783 void aw_monitor_timestamp_event(aw_monitor_t *mon, aw_event_sig_t *event_sig, aw_timestamp_t val);
00784 
00793 void aw_monitor_string_event(aw_monitor_t *mon, aw_event_sig_t *event_sig, byte_t *val, int32_t dup);
00794 
00801 int32_t aw_wait_monitor(aw_monitor_t *mon);
00802 
00803 
00810 aw_monitor_t * aw_create_monitor(aw_monitor_params_t *params);
00811 
00812 
00818 void aw_destroy_monitor(aw_monitor_t *mon);
00819 
00826 void aw_monitor_logstats(aw_monitor_t *mon, aw_logger_t *logger);
00827 
00828 #endif
Aware 0.11.1 Copyright (C) 1998-2005 Russell Leighton (russ@elegant-software.com)