summaryrefslogtreecommitdiffstats
path: root/src/usr/diag/mdia/mdiamonitor.C
blob: 925bef2186f2a6b8f3178b21c92fb3421435e1ce (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
/* IBM_PROLOG_BEGIN_TAG                                                   */
/* This is an automatically generated prolog.                             */
/*                                                                        */
/* $Source: src/usr/diag/mdia/mdiamonitor.C $                             */
/*                                                                        */
/* IBM CONFIDENTIAL                                                       */
/*                                                                        */
/* COPYRIGHT International Business Machines Corp. 2012,2013              */
/*                                                                        */
/* p1                                                                     */
/*                                                                        */
/* Object Code Only (OCO) source materials                                */
/* Licensed Internal Code Source Materials                                */
/* IBM HostBoot Licensed Internal Code                                    */
/*                                                                        */
/* The source code for this program is not published or otherwise         */
/* divested of its trade secrets, irrespective of what has been           */
/* deposited with the U.S. Copyright Office.                              */
/*                                                                        */
/* Origin: 30                                                             */
/*                                                                        */
/* IBM_PROLOG_END_TAG                                                     */
#include <sys/time.h>
#include <targeting/common/util.H>
#include <targeting/common/target.H>
#include <vector>
#include <diag/mdia/mdiamevent.H>
#include "mdiamonitor.H"
#include "mdiasm.H"
#include "mdiatrace.H"

using namespace TARGETING;

namespace MDIA
{

uint64_t CommandMonitor::addMonitor(uint64_t i_to)
{
    uint64_t monitor = 0;

    mutex_lock(&iv_mutex);

    monitor = ++iv_nextMonitor;

    // add a new monitor to our map

    iv_monitors[monitor] = i_to;

    mutex_unlock(&iv_mutex);

    return monitor;
}

void CommandMonitor::removeMonitor(uint64_t i_monitor)
{
    mutex_lock(&iv_mutex);

    // remove the specified monitor

    iv_monitors.erase(i_monitor);

    mutex_unlock(&iv_mutex);
}

uint64_t CommandMonitor::getMonitorMapTimeoutEntry(uint64_t i_monitor)
{
    uint64_t timeout = 0;

    mutex_lock(&iv_mutex);

    timeout = iv_monitors[i_monitor];

    mutex_unlock(&iv_mutex);

    return timeout;
}

void CommandMonitor::threadMain(StateMachine & i_sm)
{
    static const uint64_t singleStepPauseSecs = 0;
    static const uint64_t singleStepPauseNSecs = 10000000;
    static uint64_t wakeupIntervalNanoSecs = 0;

    // periodically wakeup and check for any command
    // timeouts

    if(TARGETING::is_vpo())
    {
        wakeupIntervalNanoSecs = TEN_CTX_SWITCHES_NS;
    }
    else
    {
        wakeupIntervalNanoSecs = singleStepPauseNSecs;
    }

    bool shutdown = false;
    MonitorIDs monitorsTimedout;

    while(true)
    {
        if( TARGETING::is_vpo() )
        {
            nanosleep(0, wakeupIntervalNanoSecs);
        }
        else
        {
            // sleep for 10 ms
            nanosleep(singleStepPauseSecs, wakeupIntervalNanoSecs);
        }

        mutex_lock(&iv_mutex);

        // check to see if the istep is finished

        shutdown = iv_shutdown;

        if(!shutdown)
        {
            // scan the monitor map and if any
            // timed out, inform the state machine

            monitorMapIterator it = iv_monitors.begin();

            while(it != iv_monitors.end())
            {
                if(it->second >= wakeupIntervalNanoSecs)
                {
                    it->second -= wakeupIntervalNanoSecs;
                    it++;
                }

                else
                {
                    monitorsTimedout.push_back(it->first);

                    // remove the monitor

                    iv_monitors.erase(it++);
                }
            }
        }

        mutex_unlock(&iv_mutex);

        if(!monitorsTimedout.empty()) {
            i_sm.processCommandTimeout(monitorsTimedout);
            monitorsTimedout.clear();
        }

        // istep finished...shutdown

        if(shutdown)
        {
            MDIA_FAST("cm: CommandMonitor will be shutdown");
            break;
        }
    }
}

namespace CommandMonitorImpl
{
struct ThreadArgs
{
    CommandMonitor * obj;
    StateMachine * sm;
};
}

void CommandMonitor::start(StateMachine & i_sm)
{
    MDIA_FAST("cm: Start the CommandMonitor");

    using namespace CommandMonitorImpl;

    // start the monitor thread

    mutex_lock(&iv_mutex);

    // no-op if already started

    if(!iv_tid)
    {
        ThreadArgs * args = new ThreadArgs();
        if(NULL != args)
        {
            args->obj = this;
            args->sm = &i_sm;

            iv_shutdown = false;
            iv_tid = task_create(&CommandMonitor::staticMain, args);

        }
    }

    mutex_unlock(&iv_mutex);
}

void CommandMonitor::shutdown()
{
    mutex_lock(&iv_mutex);

    // instruct the monitor thread to shutdown

    iv_shutdown = true;

    // wait for it to exit
    // unless it was stopped already

    tid_t tid = iv_tid;
    iv_tid = 0;

    mutex_unlock(&iv_mutex);

    if(tid)
        task_wait_tid(tid, 0, 0);
}

void* CommandMonitor::staticMain(void * i_args)
{
    using namespace CommandMonitorImpl;

    if(NULL != i_args)
    {
        ThreadArgs * args = static_cast<ThreadArgs *>(i_args);

        args->obj->threadMain(*args->sm);

        delete args;
        args = NULL;
    }
    return NULL;
}

CommandMonitor::CommandMonitor() :
    iv_shutdown(false),
    iv_tid(0),
    iv_nextMonitor(0)
{
    mutex_init(&iv_mutex);
}

CommandMonitor::~CommandMonitor()
{
    shutdown();

    mutex_destroy(&iv_mutex);
}
}
OpenPOWER on IntegriCloud