AERA
time_job.cpp
1 //_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/
2 //_/_/
3 //_/_/ AERA
4 //_/_/ Autocatalytic Endogenous Reflective Architecture
5 //_/_/
6 //_/_/ Copyright (c) 2018-2025 Jeff Thompson
7 //_/_/ Copyright (c) 2018-2025 Kristinn R. Thorisson
8 //_/_/ Copyright (c) 2018-2025 Icelandic Institute for Intelligent Machines
9 //_/_/ http://www.iiim.is
10 //_/_/
11 //_/_/ Copyright (c) 2010-2012 Eric Nivel
12 //_/_/ Center for Analysis and Design of Intelligent Agents
13 //_/_/ Reykjavik University, Menntavegur 1, 102 Reykjavik, Iceland
14 //_/_/ http://cadia.ru.is
15 //_/_/
16 //_/_/ Part of this software was developed by Eric Nivel
17 //_/_/ in the HUMANOBS EU research project, which included
18 //_/_/ the following parties:
19 //_/_/
20 //_/_/ Autonomous Systems Laboratory
21 //_/_/ Technical University of Madrid, Spain
22 //_/_/ http://www.aslab.org/
23 //_/_/
24 //_/_/ Communicative Machines
25 //_/_/ Edinburgh, United Kingdom
26 //_/_/ http://www.cmlabs.com/
27 //_/_/
28 //_/_/ Istituto Dalle Molle di Studi sull'Intelligenza Artificiale
29 //_/_/ University of Lugano and SUPSI, Switzerland
30 //_/_/ http://www.idsia.ch/
31 //_/_/
32 //_/_/ Institute of Cognitive Sciences and Technologies
33 //_/_/ Consiglio Nazionale delle Ricerche, Italy
34 //_/_/ http://www.istc.cnr.it/
35 //_/_/
36 //_/_/ Dipartimento di Ingegneria Informatica
37 //_/_/ University of Palermo, Italy
38 //_/_/ http://diid.unipa.it/roboticslab/
39 //_/_/
40 //_/_/
41 //_/_/ --- HUMANOBS Open-Source BSD License, with CADIA Clause v 1.0 ---
42 //_/_/
43 //_/_/ Redistribution and use in source and binary forms, with or without
44 //_/_/ modification, is permitted provided that the following conditions
45 //_/_/ are met:
46 //_/_/ - Redistributions of source code must retain the above copyright
47 //_/_/ and collaboration notice, this list of conditions and the
48 //_/_/ following disclaimer.
49 //_/_/ - Redistributions in binary form must reproduce the above copyright
50 //_/_/ notice, this list of conditions and the following disclaimer
51 //_/_/ in the documentation and/or other materials provided with
52 //_/_/ the distribution.
53 //_/_/
54 //_/_/ - Neither the name of its copyright holders nor the names of its
55 //_/_/ contributors may be used to endorse or promote products
56 //_/_/ derived from this software without specific prior
57 //_/_/ written permission.
58 //_/_/
59 //_/_/ - CADIA Clause: The license granted in and to the software
60 //_/_/ under this agreement is a limited-use license.
61 //_/_/ The software may not be used in furtherance of:
62 //_/_/ (i) intentionally causing bodily injury or severe emotional
63 //_/_/ distress to any person;
64 //_/_/ (ii) invading the personal privacy or violating the human
65 //_/_/ rights of any person; or
66 //_/_/ (iii) committing or preparing for any act of war.
67 //_/_/
68 //_/_/ THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
69 //_/_/ CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
70 //_/_/ INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
71 //_/_/ MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
72 //_/_/ DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
73 //_/_/ CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
74 //_/_/ SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
75 //_/_/ BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
76 //_/_/ SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
77 //_/_/ INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
78 //_/_/ WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
79 //_/_/ NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
80 //_/_/ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
81 //_/_/ OF SUCH DAMAGE.
82 //_/_/
83 //_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/
84 
85 #include "time_job.h"
86 #include "pgm_controller.h"
87 #include "mem.h"
88 
89 using namespace std;
90 using namespace std::chrono;
91 using namespace r_code;
92 
93 namespace r_exec {
94 
95 uint32 TimeJob::job_count_ = 0;
96 
97 TimeJob::TimeJob(Timestamp target_time) : _Object(), target_time_(target_time) {
98  // Increment without thread lock. It's only for tracing.
99  job_id_ = ++job_count_;
100 }
101 
102 bool TimeJob::is_alive() const {
103 
104  return true;
105 }
106 
107 void TimeJob::report(microseconds lag) const {
108 
109  std::cout << "> late generic: " << lag.count() << " us behind." << std::endl;
110 }
111 
113 
114 UpdateJob::UpdateJob(Group *g, Timestamp ijt) : TimeJob(ijt) {
115 
116  group_ = g;
117 }
118 
119 bool UpdateJob::update(Timestamp &next_target) {
120 
121 #ifdef WITH_DETAIL_OID
122  OUTPUT_LINE((TraceLevel)0, Utils::RelativeTime(Now()) << " UpdateJob::TimeJob " << get_job_id() <<
123  ": group_" << group_->get_oid() << "->update()");
124 #endif
125  group_->update(target_time_);
126  return true;
127 }
128 
129 void UpdateJob::report(int64 lag) const {
130 
131  std::cout << "> late update: " << lag << " us behind." << std::endl;
132 }
133 
135 
136 SignalingJob::SignalingJob(View *v, Timestamp ijt) : TimeJob(ijt) {
137 
138  view_ = v;
139 }
140 
141 bool SignalingJob::is_alive() const {
142 
143  return view_->controller_->is_alive();
144 }
145 
147 
148 AntiPGMSignalingJob::AntiPGMSignalingJob(View *v, Timestamp ijt) : SignalingJob(v, ijt) {
149 }
150 
151 bool AntiPGMSignalingJob::update(Timestamp &next_target) {
152 
153 #ifdef WITH_DETAIL_OID
154  OUTPUT_LINE((TraceLevel)0, Utils::RelativeTime(Now()) << " AntiPGMSignalingJob::TimeJob " << get_job_id() <<
155  ": controller(" << view_->controller_->get_detail_oid() << ")->signal_anti_pgm()");
156 #endif
157  if (is_alive())
158  ((AntiPGMController *)view_->controller_)->signal_anti_pgm();
159  return true;
160 }
161 
162 void AntiPGMSignalingJob::report(int64 lag) const {
163 
164  std::cout << "> late |pgm signaling: " << lag << " us behind." << std::endl;
165 }
166 
168 
169 InputLessPGMSignalingJob::InputLessPGMSignalingJob(View *v, Timestamp ijt) : SignalingJob(v, ijt) {
170 }
171 
172 bool InputLessPGMSignalingJob::update(Timestamp &next_target) {
173 
174 #ifdef WITH_DETAIL_OID
175  OUTPUT_LINE((TraceLevel)0, Utils::RelativeTime(Now()) << " InputLessPGMSignalingJob::TimeJob " << get_job_id() <<
176  ": controller(" << view_->controller_->get_detail_oid() << ")->signal_input_less_pgm()");
177 #endif
178  if (is_alive())
179  ((InputLessPGMController *)view_->controller_)->signal_input_less_pgm();
180  return true;
181 }
182 
183 void InputLessPGMSignalingJob::report(int64 lag) const {
184 
185  std::cout << "> late input-less pgm signaling: " << lag << " us behind." << std::endl;
186 }
187 
189 
190 InjectionJob::InjectionJob(View *v, Timestamp target_time, bool is_from_io_device) : TimeJob(target_time) {
191 
192  view_ = v;
193  is_from_io_device_ = is_from_io_device;
194 }
195 
196 bool InjectionJob::update(Timestamp &next_target) {
197 
198 #ifdef WITH_DETAIL_OID
199  OUTPUT_LINE((TraceLevel)0, Utils::RelativeTime(Now()) << " InjectionJob::TimeJob " << get_job_id() <<
200  ": inject(View(fact(" << view_->object_->get_detail_oid() << ")))");
201 #endif
202  _Mem::Get()->inject(view_);
203  if (is_from_io_device_)
204  // The view injection time may be different than now, so log it too.
205  OUTPUT_LINE(IO_DEVICE_INJ_EJT, Utils::RelativeTime(Now()) << " I/O device inject " <<
206  view_->object_->get_oid() << ", ijt " << Utils::RelativeTime(view_->get_ijt()));
207  return true;
208 }
209 
210 void InjectionJob::report(int64 lag) const {
211 
212  std::cout << "> late injection: " << lag << " us behind." << std::endl;
213 }
214 
216 
217 EInjectionJob::EInjectionJob(View *v, Timestamp ijt) : TimeJob(ijt) {
218 
219  view_ = v;
220 }
221 
222 bool EInjectionJob::update(Timestamp &next_target) {
223 
224 #ifdef WITH_DETAIL_OID
225  OUTPUT_LINE((TraceLevel)0, Utils::RelativeTime(Now()) << " EInjectionJob::TimeJob " << get_job_id() <<
226  ": inject_existing_object(View(fact(" << view_->object_->get_detail_oid() << ")))");
227 #endif
228  _Mem::Get()->inject_existing_object(view_, view_->object_, view_->get_host());
229  return true;
230 }
231 
232 void EInjectionJob::report(int64 lag) const {
233 
234  std::cout << "> late injection: " << lag << " us behind." << std::endl;
235 }
236 
238 
239 SaliencyPropagationJob::SaliencyPropagationJob(Code *o, float32 sln_change, float32 source_sln_thr, Timestamp ijt) : TimeJob(ijt), sln_change_(sln_change), source_sln_thr_(source_sln_thr) {
240 
241  object_ = o;
242 }
243 
244 bool SaliencyPropagationJob::update(Timestamp &next_target) {
245 
246 #ifdef WITH_DETAIL_OID
247  OUTPUT_LINE((TraceLevel)0, Utils::RelativeTime(Now()) << " SaliencyPropagationJob::TimeJob " << get_job_id() <<
248  ": propagate_sln(fact(" << object_->get_detail_oid() << "))");
249 #endif
250  if (!object_->is_invalidated())
251  _Mem::Get()->propagate_sln(object_, sln_change_, source_sln_thr_);
252  return true;
253 }
254 
255 void SaliencyPropagationJob::report(int64 lag) const {
256 
257  std::cout << "> late sln propagation: " << lag << " us behind." << std::endl;
258 }
259 
261 
262 ShutdownTimeCore::ShutdownTimeCore() : TimeJob(Timestamp(seconds(0))) {
263 }
264 
265 bool ShutdownTimeCore::update(Timestamp &next_target) {
266 
267  return false;
268 }
269 
271 
272 PerfSamplingJob::PerfSamplingJob(Timestamp start, microseconds period) : TimeJob(start), period_(period) {
273 }
274 
275 bool PerfSamplingJob::update(Timestamp &next_target) {
276 
277 #ifdef WITH_DETAIL_OID
278  OUTPUT_LINE((TraceLevel)0, Utils::RelativeTime(Now()) << " PerfSamplingJob::TimeJob " << get_job_id() <<
279  ": inject_perf_stats()");
280 #endif
281  _Mem::Get()->inject_perf_stats();
282  target_time_ += period_;
283  next_target = target_time_;
284  return true;
285 }
286 
287 bool PerfSamplingJob::is_alive() const {
288 
289  return true;
290 }
291 }
r_exec::TimeJob
Definition: time_job.h:97
r_code::Code
Definition: r_code/object.h:224
r_exec::InjectionJob::InjectionJob
InjectionJob(View *v, Timestamp target_time, bool is_from_io_device)
Definition: time_job.cpp:190
core::_Object
Definition: base.h:131
r_exec::View
Definition: view.h:102