summaryrefslogtreecommitdiffstats
path: root/src/usr/diag/mdia/test/mdiatestmonitor.H
blob: b90f82162fdb21673c07b998fcf3716d288be9f0 (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
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
/* IBM_PROLOG_BEGIN_TAG                                                   */
/* This is an automatically generated prolog.                             */
/*                                                                        */
/* $Source: src/usr/diag/mdia/test/mdiatestmonitor.H $                    */
/*                                                                        */
/* 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                                                     */
#ifndef __TEST_MDIATESTMONITOR_H
#define __TEST_MDIATESTMONITOR_H

#include <cxxtest/TestSuite.H>
#include <sys/task.h>
#include <sys/time.h>
#include <stdint.h>
#include <targeting/common/util.H>
#include "../mdiafwd.H"
#include "../mdiamonitor.H"
#include "../mdiasm.H"
#include "../mdiatrace.H"
#include "mdiafakesm.H"

using namespace MDIA;

class MdiaCommandMonitorTest: public CxxTest::TestSuite
{
    private:
        void nanoSleep(uint64_t i_sleepSec, uint64_t i_sleepNanoSec) const
        {
            if( TARGETING::is_vpo() )
            {
                nanosleep(0, TEN_CTX_SWITCHES_NS);
            }
            else
            {
                nanosleep(i_sleepSec, i_sleepNanoSec);
            }
        }

        void TimeoutAll(mutex_t *i_mutex, sync_cond_t *i_cond)
        {
            TS_TRACE(ENTER_MRK "TimeoutAll");

            uint64_t l_mntCmdTime = 10000000;
            FakeStateMachine1 l_sm;
            CommandMonitor l_cm;

            //Start the CM thread. Force a timeout.
            //All monitorIDs should timeout.
            uint64_t l_monID1 = 0, l_monID2 = 0;
            l_cm.start(l_sm);
            l_monID1 = l_cm.addMonitor(l_mntCmdTime);
            l_sm.addMonitor(l_monID1);
            l_monID2 = l_cm.addMonitor(l_mntCmdTime);
            l_sm.addMonitor(l_monID2);

            //2 monitorIDs should timeout
            l_sm.setTotalExpectedMons(2);
            l_sm.setSyncVars(i_mutex, i_cond);

            mutex_lock(i_mutex);
            // Wait until all monitorIDs timeout
            while(false == l_sm.isTimeoutProcessed())
            {
                sync_cond_wait(i_cond, i_mutex);
            }
            mutex_unlock(i_mutex);

            if(true != l_sm.isContentsEqual())
            {
                TS_FAIL("Expected monitorIDs to timeout are not equal "
                        "to actual timedout monitorIDs");
            }

            //All maint cmds/monitorIDs have timedout
            //Therefore all timedout iv_monitors should be erased
            if(l_cm.iv_monitors.size() != 0)
            {
                TS_FAIL("Size not zero");
            }

            TS_TRACE(EXIT_MRK "TimeoutAll");
        }

        void TimeoutSubset(mutex_t *i_mutex, sync_cond_t *i_cond)
        {
            TS_TRACE(ENTER_MRK "TimeoutSubset");

            FakeStateMachine1 l_sm;
            CommandMonitor l_cm;
            l_sm.setSyncVars(i_mutex, i_cond);
            l_cm.start(l_sm);

            uint64_t l_mntCmdTime = 10000000;

            //Add 7 more
            //3 of the 7 monitorIDs should timeout.
            uint64_t l_monID1 = 0,
                     l_monID2 = 0,
                     l_monID3 = 0,
                     l_monID4 = 0,
                     l_monID5 = 0,
                     l_monID6 = 0,
                     l_monID7 = 0;
            l_monID1 = l_cm.addMonitor(l_mntCmdTime);
            l_sm.addMonitor(l_monID1);
            l_monID2 = l_cm.addMonitor(l_mntCmdTime);
            l_sm.addMonitor(l_monID2);
            l_monID3 = l_cm.addMonitor(l_mntCmdTime);
            l_sm.addMonitor(l_monID3);
            l_monID4 = l_cm.addMonitor(l_mntCmdTime);
            l_sm.addMonitor(l_monID4);
            l_monID5 = l_cm.addMonitor(l_mntCmdTime);
            l_sm.addMonitor(l_monID5);
            l_monID6 = l_cm.addMonitor(l_mntCmdTime);
            l_sm.addMonitor(l_monID6);
            l_monID7 = l_cm.addMonitor(l_mntCmdTime);
            l_sm.addMonitor(l_monID7);

            //Remove 4 of the 7 monIDs
            l_cm.removeMonitor(l_monID1);
            l_sm.removeMonitor(0);
            nanoSleep(0, 100);
            l_cm.removeMonitor(l_monID4);
            l_sm.removeMonitor(2);
            l_cm.removeMonitor(l_monID5);
            l_sm.removeMonitor(2);
            l_cm.removeMonitor(l_monID6);
            l_sm.removeMonitor(2);

            //3 monitorIDs should timeout
            l_sm.setTotalExpectedMons(3);

            if(false != l_sm.isTimeoutProcessed())
            {
                TS_FAIL("Unexpected timeout");
            }

            //Wait till all monitorIDs have timed out
            mutex_lock(i_mutex);
            while(false == l_sm.isTimeoutProcessed())
            {
                sync_cond_wait(i_cond, i_mutex);
            }
            mutex_unlock(i_mutex);

            if(true != l_sm.isContentsEqual())
            {
                TS_FAIL("Expected monitorIDs to timeout are not equal "
                        "to actual timedout monitorIDs");
            }

            TS_TRACE(EXIT_MRK "TimeoutSubset");

        }

    public:
        void testCommandMonitor(void)
        {

            TS_TRACE(ENTER_MRK "testCommandMonitor");

            CommandMonitor l_cm;
            do
            {
                if(0 != l_cm.iv_tid)
                {
                    TS_FAIL("Thread id != 0");
                    break;
                }
                else if(true == l_cm.iv_shutdown)
                {
                    TS_FAIL("Var iv_shutdown != false");
                    break;
                }
                else if(0 != l_cm.iv_nextMonitor)
                {
                    TS_FAIL("Monitor ID counter != 0");
                    break;
                }
            }while(0);

            TS_TRACE(EXIT_MRK "testCommandMonitor");
        }

        void testaddMonitor(void)
        {
            TS_TRACE(ENTER_MRK "testaddMonitor");

            uint64_t l_timeout = 10;
            CommandMonitor l_cm1;

            //Add mutiple <monitor, timeout> entries
            uint64_t l_mon = l_cm1.addMonitor(l_timeout);
            if(1 != l_mon)
            {
                TS_FAIL("Key monitor ID not set corretly");
            }
            else
            {
                l_mon = l_cm1.addMonitor(++l_timeout);
                if(l_timeout != l_cm1.getMonitorMapTimeoutEntry(l_mon))
                {
                    TS_FAIL("Map entries not set right (1)");
                }

                // Add monitor after starting the CM thread
                StateMachine l_sm;
                l_cm1.start(l_sm);
                l_cm1.addMonitor(++l_timeout);
                l_mon = l_cm1.addMonitor(++l_timeout);
                if(l_timeout != l_cm1.getMonitorMapTimeoutEntry(l_mon))
                {
                    TS_FAIL("Map entries not set right (2)");
                }
            }

            TS_TRACE(EXIT_MRK "testaddMonitor");
        }

        void teststart(void)
        {
            TS_TRACE(ENTER_MRK "teststart");

            tid_t l_tid = 0;
            StateMachine l_sm;
            CommandMonitor l_cm1;
            //Check if CM thread was created.
            l_cm1.start(l_sm);
            l_tid = l_cm1.iv_tid;
            if(0 == l_tid)
            {
                TS_FAIL("Command Monitor task not started (1)");
            }
            else
            {
                //Once started should not start again.
                l_cm1.start(l_sm);
                if(l_tid != l_cm1.iv_tid)
                {
                    TS_FAIL("Command Monitor task started again");
                }

                //Call start after shutdown
                l_cm1.shutdown();
                if(0 != l_cm1.iv_tid)
                {
                    TS_FAIL("CM not shutdown");
                }
                else
                {
                    l_cm1.start(l_sm);
                    if(0 == l_cm1.iv_tid)
                    {
                        TS_FAIL("CommandMonitor task not started (2)");
                    }
                }
            }

            TS_TRACE(EXIT_MRK "teststart");
        }

        void testthreadMainTimeout(void)
        {
            return; // FIXME RTC: 67008

            TS_TRACE(ENTER_MRK "testthreadMainTimeout");
            mutex_t mutex;
            sync_cond_t cond;
            mutex_init(&mutex);
            sync_cond_init(&cond);

            for(uint64_t iterations=0; iterations<20; iterations++)
            {
                TimeoutAll(&mutex, &cond);
            }

            for(uint64_t iterations=0; iterations<20; iterations++)
            {
                TimeoutSubset(&mutex, &cond);
            }

            sync_cond_destroy(&cond);
            mutex_destroy(&mutex);

            TS_TRACE(EXIT_MRK "testthreadMainTimeout");
        }

        void testremoveMonitor(void)
        {
            TS_TRACE(ENTER_MRK "testremoveMonitor");

            CommandMonitor l_cm1;
            uint64_t l_monID = 0;
            //Removing from empty list
            l_cm1.removeMonitor(l_monID);

            //Populate monitor. Remove last entry
            uint64_t l_timeout = 20;
            l_monID = l_cm1.addMonitor(l_timeout);
            l_monID = l_cm1.addMonitor(++l_timeout);
            l_monID = l_cm1.addMonitor(++l_timeout);
            l_cm1.removeMonitor(l_monID);
            //Check if removed
            if(0 != l_cm1.getMonitorMapTimeoutEntry(l_monID))
            {
                TS_FAIL("Last Map entry not removed");
            }

            //Remove first entry
            l_monID = 1;
            l_cm1.removeMonitor(l_monID);
            if(0 != l_cm1.getMonitorMapTimeoutEntry(l_monID))
            {
                TS_FAIL("First Map entry not removed");
            }

            l_cm1.addMonitor(l_timeout);
            l_cm1.addMonitor(l_timeout);
            l_cm1.addMonitor(l_timeout);
            l_cm1.addMonitor(l_timeout);
            l_cm1.addMonitor(l_timeout);
            //Remove 2nd and 3rd entry
            l_cm1.removeMonitor(2);
            l_cm1.removeMonitor(4);
            //Verify removal
            if(0 != l_cm1.getMonitorMapTimeoutEntry(2) &&
               0 != l_cm1.getMonitorMapTimeoutEntry(4) )
            {
                TS_FAIL("Entries in middle not removed");
            }

            TS_TRACE(EXIT_MRK "testremoveMonitor");
        }

        void testshutdown(void)
        {
            TS_TRACE(ENTER_MRK "testshutdown");

            StateMachine l_sm;
            CommandMonitor l_cm;

            //Shutdown without starting CM
            l_cm.shutdown();
            if(false == l_cm.iv_shutdown && (0 != l_cm.iv_tid))
            {
                TS_FAIL("Command Monitor not shutdown (1)");
            }

            //Start CM, then shutdown
            l_cm.start(l_sm);
            l_cm.shutdown();
            if(false == l_cm.iv_shutdown && (0 != l_cm.iv_tid))
            {
                TS_FAIL("Command Monitor not shutdown (2)");
            }

            //Shutdown again
            l_cm.shutdown();
            if(false == l_cm.iv_shutdown && (0 != l_cm.iv_tid))
            {
                TS_FAIL("Command Monitor not shutdown (3");
            }

            TS_TRACE(EXIT_MRK "testshutdown");
        }

};
#endif
OpenPOWER on IntegriCloud