You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

snfGBUdbmgr.cpp 14KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  1. // snfGBUdbmgr.cpp
  2. // Copyright (C) 2006 - 2009 ARM Research Labs, LLC.
  3. // See www.armresearch.com for the copyright terms.
  4. //
  5. // See snfGBUdbmgr.hpp for details.
  6. #include "SNFMulti/snfGBUdbmgr.hpp"
  7. #include <unistd.h>
  8. using namespace std;
  9. using namespace CodeDweller;
  10. namespace SNFMulti {
  11. const ThreadType snfGBUdbmgr::Type("snfGBUdbmgr"); // The thread's type.
  12. snfGBUdbmgr::snfGBUdbmgr() : // Clean init and start thread.
  13. Thread(snfGBUdbmgr::Type, "GBUdb Manager"), // XCI Manager type and Name.
  14. MyGBUdb(NULL), // NULL our links to avoid
  15. MyLOGmgr(NULL), // any errors when the thread starts.
  16. Configured(false), // Not configured yet.
  17. TimeToStop(false), // It is not time to stop ;-)
  18. CondenseGuardTime(600000), // 10 minute guard time by default.
  19. TimeTriggerOnOff(true), // By default, condense once per day.
  20. TimeTrigger(86400000),
  21. PostsTriggerOnOff(false), // By default do not trigger on posts.
  22. PostsTriggerValue(262144), // but if we do, use a quarter million.
  23. RecordsTriggerOnOff(false), // By default do not trigger on records.
  24. RecordsTriggerValue(150000), // but if we do, use 150K.
  25. SizeTriggerOnOff(true), // By default trigger on size as a
  26. SizeTriggerValue(150), // safety valve at 150Mbytes.
  27. CheckpointOnOff(true), // By default save a snapshot once
  28. CheckpointTrigger(3600000) { // every hour.
  29. run(); // Start our thread.
  30. }
  31. snfGBUdbmgr::~snfGBUdbmgr() { // Clean shutdown & stop thread.
  32. stop(); // Stop the thread if it's not already.
  33. MyGBUdb = NULL; // NULL our links and false our
  34. MyLOGmgr = NULL; // configuration for safety.
  35. Configured = false;
  36. }
  37. void snfGBUdbmgr::linkGBUdb(GBUdb& G) { // Connect to our GBUdb
  38. ScopeMutex JustMe(MyMutex); // Lock for the config change.
  39. MyGBUdb = &G; // Set the new link.
  40. }
  41. void snfGBUdbmgr::linkLOGmgr(snfLOGmgr& L) { // Connect to our LOGmgr
  42. ScopeMutex JustMe(MyMutex); // Lock for the config change.
  43. MyLOGmgr = &L; // Set the new link.
  44. }
  45. const int SECsASms = 1000; // How to convert seconds to milliseconds.
  46. msclock toDuration(int Configuration_Integer) { // Convert int Seconds to timer duration.
  47. return (Configuration_Integer * SECsASms); // Do the math and send it back.
  48. }
  49. void snfGBUdbmgr::configure(snfCFGData& CFGData) { // Establish or change our CFG.
  50. ScopeMutex JustMe(MyMutex); // Only when we're not busy.
  51. // Set up our configuration from the CFGData provided.
  52. // Being careful not to muck with running timers unless their
  53. // configuration values have actually changed...
  54. if(CondenseGuardTime.getDuration() != // If the condensation guard time is
  55. toDuration(CFGData.gbudb_database_condense_minimum_seconds_between)) { // new and different then set the
  56. CondenseGuardTime.setDuration( // condensation guard timer to the
  57. toDuration(CFGData.gbudb_database_condense_minimum_seconds_between) // new value.
  58. );
  59. }
  60. TimeTriggerOnOff = CFGData.gbudb_database_condense_time_trigger_on_off; // Time-Trigger On?
  61. if(TimeTrigger.getDuration() != // Time-Trigger different?
  62. toDuration(CFGData.gbudb_database_condense_time_trigger_seconds)) {
  63. TimeTrigger.setDuration( // If it is then adopt the new value.
  64. toDuration(CFGData.gbudb_database_condense_time_trigger_seconds)
  65. );
  66. }
  67. PostsTriggerOnOff = CFGData.gbudb_database_condense_posts_trigger_on_off; // Posts trigger on?
  68. PostsTriggerValue = CFGData.gbudb_database_condense_posts_trigger_posts; // What is the posts trigger threshold?
  69. RecordsTriggerOnOff = CFGData.gbudb_database_condense_records_trigger_on_off; // Records trigger on?
  70. RecordsTriggerValue = CFGData.gbudb_database_condense_records_trigger_records; // What is the records trigger threshold?
  71. SizeTriggerOnOff = CFGData.gbudb_database_condense_size_trigger_on_off; // Size trigger on?
  72. SizeTriggerValue = CFGData.gbudb_database_condense_size_trigger_megabytes; // What is the size trigger threshold?
  73. // Checkpoint
  74. CheckpointOnOff = CFGData.gbudb_database_checkpoint_on_off; // Checkpoint on?
  75. if(CheckpointTrigger.getDuration() != // If the Checkpoint time is
  76. toDuration(CFGData.gbudb_database_checkpoint_secs)) { // new and different then
  77. CheckpointTrigger.setDuration( // adopt the new value.
  78. toDuration(CFGData.gbudb_database_checkpoint_secs)
  79. );
  80. }
  81. // GBUdb file name
  82. string GBUdbFileName; // Formulate the correct GBUdb file name
  83. GBUdbFileName = CFGData.paths_workspace_path + // using the CFGData.
  84. CFGData.node_licenseid + ".gbx";
  85. if( // If the file name for our GBUdb
  86. NULL == (*MyGBUdb).FileName() || // is not yet set, or
  87. 0 != GBUdbFileName.compare((*MyGBUdb).FileName()) // if it is different than the
  88. ) { // formulated file name we have then
  89. (*MyGBUdb).FileName(GBUdbFileName.c_str()); // set the GBUdb file name.
  90. }
  91. // Safety check to set the Configured bit.
  92. if(NULL != MyGBUdb && NULL != MyLOGmgr) { // If we have all of our parts
  93. Configured = true; // then set our configured flag.
  94. } else { // If anything is missing then
  95. Configured = false; // make sure the flag is false.
  96. }
  97. }
  98. //// The snfGBUdbmgr::load() method isn't exactly what you would expect. It
  99. // will load the rulebase file if that file exists, but if not it does nothing.
  100. // The intention is that a new GBUdb will alread have been created. If a
  101. // pre-existing GBUdb is available then that one will be loaded for use. If
  102. // it does not exist, then the new, empty GBUdb will be used instead and will
  103. // eventually be saved for later re-use.
  104. void snfGBUdbmgr::load() { // Load the GBUdb as configured.
  105. ScopeMutex JustMe(MyMutex); // Just me while I do this.
  106. if( // Perform some sanity checks.
  107. NULL != MyGBUdb && // If we have a GBUdb and
  108. 0 < string(MyGBUdb->FileName()).length() && // it has a file name and
  109. 0 == access(MyGBUdb->FileName(),R_OK) // the file can be accessed
  110. ) { // then we can proceed:
  111. MyGBUdb->load(); // Load the GBUdb from disk.
  112. } // If that didn't work we'll assume
  113. } // we're starting up a new gbx file ;-)
  114. // DoMaintenanceWork encapsulates all of our maintenance functions. It runs
  115. // with the mutex locked so that the configuration is stable during each pass.
  116. void snfGBUdbmgr::DoMaintenanceWork() { // Do our watchdog work.
  117. if(!Configured) return; // Do nothing if we're not configured.
  118. ScopeMutex JustMe(MyMutex); // No CFG changes while I'm busy.
  119. if(CondenseGuardTime.isExpired()) { // If we are allowed to condense
  120. bool CondenseTriggered = false; // check to see if we should.
  121. // time-trigger
  122. if(
  123. TimeTriggerOnOff && // If the time-trigger is on
  124. TimeTrigger.isExpired() // and the time has expired
  125. ) { // then it is time to condense.
  126. CondenseTriggered = true; // Set the condense flag and
  127. TimeTrigger.restart(); // restart the timer.
  128. }
  129. // posts-trigger
  130. if(
  131. PostsTriggerOnOff && // If posts-trigger is on
  132. (*MyGBUdb).Posts() >= PostsTriggerValue // and the Posts() count is high
  133. ) { // enough then trigger the
  134. CondenseTriggered = true; // condense operation.
  135. }
  136. // records-trigger
  137. if(
  138. RecordsTriggerOnOff && // If records-trigger is on
  139. (*MyGBUdb).IPCount() >= RecordsTriggerValue // and the number of IPs is high
  140. ) { // enough then trigger the
  141. CondenseTriggered = true; // condense operation.
  142. }
  143. // size-trigger
  144. const int MByte = 1048576; // How big is a megabyte anyway?
  145. if(
  146. SizeTriggerOnOff && // If size-trigger is on
  147. ((*MyGBUdb).Size()/MByte) >= SizeTriggerValue // and the size of the db is high
  148. ) { // enough then trigger
  149. CondenseTriggered = true; // the condense operation.
  150. }
  151. if(CondenseTriggered) { // If we need to condense then
  152. (*MyGBUdb).reduce(); // reduce all counts in the db
  153. (*MyGBUdb).compress(); // and elminate any that drop to zero.
  154. CondenseGuardTime.restart(); // That done, reset the guard timer.
  155. (*MyLOGmgr).RecordCondenseEvent(); // Log the event.
  156. }
  157. }
  158. // Time to save a snapshot?
  159. if(
  160. CheckpointOnOff && // If checkpoints are turned on
  161. CheckpointTrigger.isExpired() // and it is time to create one
  162. ) {
  163. (*MyGBUdb).saveSnapshot(); // then save a snapshot and
  164. CheckpointTrigger.restart(); // restart the timer.
  165. (*MyLOGmgr).RecordSaveEvent(); // Log the event.
  166. }
  167. }
  168. // Stopping the thread...
  169. void snfGBUdbmgr::stop() { // To stop the manager thread we
  170. if(!TimeToStop) { // check to see we need to then
  171. TimeToStop = true; // set the time to stop flag
  172. join(); // and join the thread.
  173. }
  174. }
  175. // The thread's task is to call DoMaintenanceWork() once every second.
  176. void snfGBUdbmgr::myTask() { // This is what our thread does.
  177. Sleeper WaitATic(1000); // We need a 1 second sleeper.
  178. while(!TimeToStop) { // While it's not time to stop
  179. WaitATic(); // wait a tic and then do work.
  180. DoMaintenanceWork();
  181. }
  182. }
  183. void snfGBUdbmgr::GetAlertsForSync(list<GBUdbAlert>& AlertList) { // Fill AlertList w/ outgoing alerts.
  184. (*MyGBUdb).GetAlerts(AlertList); // For now, just pass this through.
  185. }
  186. void snfGBUdbmgr::ProcessReflections(list<GBUdbAlert>& Reflections) { // Integrate returning reflections.
  187. (*MyGBUdb).ImportAlerts(Reflections); // For now, just pass this through.
  188. }
  189. }