Skip to content

Commit 6050fc5

Browse files
Merge pull request #5 from WebPlatformForEmbedded/timer
Memory Observer updated based on the Job creation and Job revocation added
2 parents 9d3b7d4 + 1c677c3 commit 6050fc5

2 files changed

Lines changed: 80 additions & 62 deletions

File tree

Launcher.cpp

Lines changed: 17 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -15,64 +15,6 @@ namespace Plugin {
1515

1616
SERVICE_REGISTRATION(Launcher, 1, 0);
1717

18-
class MemoryObserverImpl : public Exchange::IMemory {
19-
private:
20-
MemoryObserverImpl();
21-
MemoryObserverImpl(const MemoryObserverImpl&);
22-
MemoryObserverImpl& operator=(const MemoryObserverImpl&);
23-
24-
public:
25-
MemoryObserverImpl(const uint32_t id)
26-
: _main(id == 0 ? Core::ProcessInfo().Id() : id)
27-
, _observable(false)
28-
{
29-
}
30-
~MemoryObserverImpl()
31-
{
32-
}
33-
34-
public:
35-
virtual void Observe(const uint32_t pid)
36-
{
37-
if (pid == 0) {
38-
_observable = false;
39-
}
40-
else {
41-
_main = Core::ProcessInfo(pid);
42-
_observable = true;
43-
}
44-
}
45-
virtual uint64_t Resident() const
46-
{
47-
return (_observable == false ? 0 : _main.Resident());
48-
}
49-
virtual uint64_t Allocated() const
50-
{
51-
return (_observable == false ? 0 : _main.Allocated());
52-
}
53-
virtual uint64_t Shared() const
54-
{
55-
return (_observable == false ? 0 : _main.Shared());
56-
}
57-
virtual uint8_t Processes() const
58-
{
59-
return (IsOperational() ? 1 : 0);
60-
}
61-
virtual const bool IsOperational() const
62-
{
63-
return (_observable == false) || (_main.IsActive());
64-
}
65-
66-
BEGIN_INTERFACE_MAP(MemoryObserverImpl)
67-
INTERFACE_ENTRY(Exchange::IMemory)
68-
END_INTERFACE_MAP
69-
70-
private:
71-
Core::ProcessInfo _main;
72-
bool _observable;
73-
};
74-
75-
7618
/* static */ Launcher::ProcessObserver Launcher::_observer;
7719

7820
/* virtual */ const string Launcher::Initialize(PluginHost::IShell* service)
@@ -106,7 +48,10 @@ SERVICE_REGISTRATION(Launcher, 1, 0);
10648
}
10749
}
10850

109-
_activity = Core::ProxyType<Job>::Create(&config, interval);
51+
_memory = Core::Service<MemoryObserverImpl>::Create<Exchange::IMemory>(0);
52+
ASSERT(_memory != nullptr);
53+
54+
_activity = Core::ProxyType<Job>::Create(&config, interval, _memory);
11055
if (_activity.IsValid() == true) {
11156
if (_activity->IsOperational() == false) {
11257
// Well if we where able to parse the parameters (if needed) we are ready to start it..
@@ -136,15 +81,24 @@ SERVICE_REGISTRATION(Launcher, 1, 0);
13681
else {
13782
PluginHost::WorkerPool::Instance().Submit(_activity);
13883
}
84+
13985
}
14086
else {
87+
_activity.Release();
14188
message = _T("Could not parse the configuration for the job.");
14289
}
14390
}
14491
else {
14592
message = _T("Could not create the job.");
14693
}
14794

95+
if (_activity.IsValid() == false) {
96+
if (_memory != nullptr) {
97+
_memory->Release();
98+
_memory = nullptr;
99+
}
100+
}
101+
148102
return (message);
149103
}
150104

@@ -153,6 +107,8 @@ SERVICE_REGISTRATION(Launcher, 1, 0);
153107
ASSERT(_service == service);
154108
ASSERT(_memory != nullptr);
155109

110+
PluginHost::WorkerPool::Instance().Revoke(_activity);
111+
156112
_observer.Unregister(&_notification);
157113

158114
if (_memory != nullptr) {
@@ -188,7 +144,8 @@ void Launcher::Update(const ProcessObserver::Info& info)
188144
ASSERT(_service != nullptr);
189145

190146
if (info.Event() == ProcessObserver::Info::EVENT_EXIT) {
191-
147+
148+
_memory->Observe(0);
192149
if ((info.ExitCode() & 0xFFFF) == 0) {
193150
// Only do this if we do not need a retrigger on an intervall.
194151
if (_activity->IsOperational() == false) {

Launcher.h

Lines changed: 63 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -265,6 +265,63 @@ class Launcher : public PluginHost::IPlugin {
265265
Launcher& _parent;
266266
};
267267

268+
class MemoryObserverImpl : public Exchange::IMemory {
269+
private:
270+
MemoryObserverImpl();
271+
MemoryObserverImpl(const MemoryObserverImpl&);
272+
MemoryObserverImpl& operator=(const MemoryObserverImpl&);
273+
274+
public:
275+
MemoryObserverImpl(const uint32_t id)
276+
: _main(id == 0 ? Core::ProcessInfo().Id() : id)
277+
, _observable(false)
278+
{
279+
}
280+
~MemoryObserverImpl()
281+
{
282+
}
283+
284+
public:
285+
virtual void Observe(const uint32_t pid)
286+
{
287+
if (pid == 0) {
288+
_observable = false;
289+
}
290+
else {
291+
_main = Core::ProcessInfo(pid);
292+
_observable = true;
293+
}
294+
}
295+
virtual uint64_t Resident() const
296+
{
297+
return (_observable == false ? 0 : _main.Resident());
298+
}
299+
virtual uint64_t Allocated() const
300+
{
301+
return (_observable == false ? 0 : _main.Allocated());
302+
}
303+
virtual uint64_t Shared() const
304+
{
305+
return (_observable == false ? 0 : _main.Shared());
306+
}
307+
virtual uint8_t Processes() const
308+
{
309+
return (IsOperational() ? 1 : 0);
310+
}
311+
virtual const bool IsOperational() const
312+
{
313+
return (_observable == false) || (_main.IsActive());
314+
}
315+
316+
BEGIN_INTERFACE_MAP(MemoryObserverImpl)
317+
INTERFACE_ENTRY(Exchange::IMemory)
318+
END_INTERFACE_MAP
319+
320+
private:
321+
Core::ProcessInfo _main;
322+
bool _observable;
323+
};
324+
268325
public:
269326
class Config : public Core::JSON::Container {
270327
private:
@@ -485,11 +542,12 @@ class Launcher : public PluginHost::IPlugin {
485542
Job& operator=(const Job&) = delete;
486543

487544
public:
488-
Job(Config* config, const Time& interval)
545+
Job(Config* config, const Time& interval, Exchange::IMemory* memory)
489546
: _hasRun(false)
490547
, _pid(0)
491548
, _options(config->Command.Value().c_str())
492549
, _process(false)
550+
, _memory(memory)
493551
, _interval(interval)
494552
{
495553
auto iter = config->Parameters.Elements();
@@ -528,8 +586,10 @@ class Launcher : public PluginHost::IPlugin {
528586
// Check if the process is not active, no need to reschedule the same job again.
529587
if (_process.IsActive() == false) {
530588

531-
Core::Time currentTime(Core::Time::Now());
532589
_process.Launch(_options, &_pid);
590+
if (_memory != nullptr) {
591+
_memory->Observe(_pid);
592+
}
533593
}
534594

535595
if (_interval.IsValid() == true) {
@@ -550,6 +610,7 @@ class Launcher : public PluginHost::IPlugin {
550610
uint32_t _pid;
551611
Core::Process::Options _options;
552612
Core::Process _process;
613+
Exchange::IMemory* _memory;
553614
Time _interval;
554615
};
555616

0 commit comments

Comments
 (0)