feisty meow concerns codebase  2.140
heartbeat.cpp
Go to the documentation of this file.
1 
2 
3 
4 /*****************************************************************************\
5 * *
6 * Name : heartbeat *
7 * Author : Chris Koeritz *
8 * *
9 *******************************************************************************
10 * Copyright (c) 1996-$now By Author. This program is free software; you can *
11 * redistribute it and/or modify it under the terms of the GNU General Public *
12 * License as published by the Free Software Foundation; either version 2 of *
13 * the License or (at your option) any later version. This is online at: *
14 * http://www.fsf.org/copyleft/gpl.html *
15 * Please send any updates to: fred@gruntose.com *
16 \*****************************************************************************/
17 
18 #include "heartbeat.h"
19 
20 #include <basis/astring.h>
21 #include <basis/functions.h>
22 #include <timely/time_stamp.h>
23 
24 using namespace basis;
25 using namespace timely;
26 
27 namespace processes {
28 
29 heartbeat::heartbeat(int misses_allowed, int check_interval)
30 : _next_heartbeat(new time_stamp()),
31  _check_interval(0),
32  _misses_allowed(0),
33  _misses(0)
34 { reset(misses_allowed, check_interval); }
35 
37 : root_object(),
38  _next_heartbeat(new time_stamp()),
39  _check_interval(0),
40  _misses_allowed(0),
41  _misses(0)
42 { *this = to_copy; }
43 
44 heartbeat::~heartbeat() { WHACK(_next_heartbeat); }
45 
46 time_stamp heartbeat::heartbeat_time() const { return *_next_heartbeat; }
47 
48 bool heartbeat::due() const { return time_left() <= 0; }
49 
50 void heartbeat::made_request() { _misses++; reset_next_beat(); }
51 
52 void heartbeat::kabump() { _misses = 0; reset_next_beat(); }
53 
54 void heartbeat::reset_next_beat()
55 { *_next_heartbeat = time_stamp(_check_interval); }
56 
58 { return int(_next_heartbeat->value() - time_stamp().value()); }
59 
60 bool heartbeat::dead() const
61 {
62  // two cases mean the timer's dead; (1) if the misses are already too high,
63  // or (2) if the heartbeat is due and the misses are as many as allowed.
64  return (_misses > _misses_allowed)
65  || (due() && (_misses >= _misses_allowed));
66 }
67 
68 void heartbeat::reset(int misses_allowed, int check_interval)
69 {
70  _misses_allowed = misses_allowed;
71  _misses = 0;
72  _check_interval = check_interval;
73  reset_next_beat();
74 }
75 
76 astring heartbeat::text_form(bool detailed) const
77 {
78  astring to_return = (dead()? astring("expired, ") : astring("alive, "));
79  to_return += (!dead() && due() ? astring("due now, ")
80  : astring::empty_string());
81  to_return += a_sprintf("beats left=%d", misses_left());
82  if (detailed) {
83  to_return += a_sprintf(", missed=%d, interval=%d, ",
85  to_return += astring("next=") + heartbeat_time().text_form();
86  }
87  return to_return;
88 }
89 
91 {
92  if (this == &to_copy) return *this;
93  _check_interval = to_copy._check_interval;
94  _misses_allowed = to_copy._misses_allowed;
95  _misses = to_copy._misses;
96  *_next_heartbeat = *to_copy._next_heartbeat;
97  return *this;
98 }
99 
100 } //namespace.
101 
102 
a_sprintf is a specialization of astring that provides printf style support.
Definition: astring.h:440
Provides a dynamically resizable ASCII character string.
Definition: astring.h:35
Monitors a periodic heartbeat to track a resource's health.
Definition: heartbeat.h:34
void made_request()
records that another heartbeat request was sent out.
Definition: heartbeat.cpp:50
int misses_left() const
the number of misses that this object is still allowed.
Definition: heartbeat.h:85
int checking_interval() const
returns the period of the heartbeats.
Definition: heartbeat.h:90
heartbeat & operator=(const heartbeat &to_copy)
Definition: heartbeat.cpp:90
timely::time_stamp heartbeat_time() const
returns the time when the next heartbeat will be requested.
Definition: heartbeat.cpp:46
void reset(int misses_allowed, int check_interval)
retrains the heartbeat monitor for a new configuration.
Definition: heartbeat.cpp:68
bool due() const
is the next heartbeat due yet?
Definition: heartbeat.cpp:48
int missed_so_far() const
returns the number of heartbeat responses that are pending.
Definition: heartbeat.h:83
int time_left() const
number of milliseconds left before the next beat will be requested.
Definition: heartbeat.cpp:57
heartbeat(int misses_allowed=500, int check_interval=10000)
creates a heartbeat monitor with the specified interval and maximum skips permitted.
Definition: heartbeat.cpp:29
bool dead() const
is this object considered dead from missing too many heartbeats?
Definition: heartbeat.cpp:60
basis::astring text_form(bool detailed=false) const
returns a readable form of the heartbeat's information.
Definition: heartbeat.cpp:76
void kabump()
registers a heartbeat response and sets the state to healthy.
Definition: heartbeat.cpp:52
Represents a point in time relative to the operating system startup time.
Definition: time_stamp.h:38
basis::astring text_form(stamp_display_style style=STAMP_RELATIVE) const
returns a simple textual representation of the time_stamp.
Definition: time_stamp.cpp:61
time_representation value() const
returns the time_stamp in terms of the lower level type.
Definition: time_stamp.h:61
The guards collection helps in testing preconditions and reporting errors.
Definition: array.h:30
void WHACK(contents *&ptr)
deletion with clearing of the pointer.
Definition: functions.h:121
#include <time.h>
Definition: earth_time.cpp:37