summaryrefslogtreecommitdiffstats
path: root/src/usr/diag/attn/test/attnrandsource.C
blob: c229d2455d1d2fe101f4fb5e5fe65ea407dc8090 (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
/* IBM_PROLOG_BEGIN_TAG                                                   */
/* This is an automatically generated prolog.                             */
/*                                                                        */
/* $Source: src/usr/diag/attn/test/attnrandsource.C $                     */
/*                                                                        */
/* OpenPOWER HostBoot Project                                             */
/*                                                                        */
/* COPYRIGHT International Business Machines Corp. 2012,2014              */
/*                                                                        */
/* Licensed under the Apache License, Version 2.0 (the "License");        */
/* you may not use this file except in compliance with the License.       */
/* You may obtain a copy of the License at                                */
/*                                                                        */
/*     http://www.apache.org/licenses/LICENSE-2.0                         */
/*                                                                        */
/* Unless required by applicable law or agreed to in writing, software    */
/* distributed under the License is distributed on an "AS IS" BASIS,      */
/* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or        */
/* implied. See the License for the specific language governing           */
/* permissions and limitations under the License.                         */
/*                                                                        */
/* IBM_PROLOG_END_TAG                                                     */
/**
 * @file attnrandsource.C
 *
 * @brief HBATTN Random attention source class method definitions.
 */

#include "attnrandsource.H"
#include "attninject.H"
#include "../attntrace.H"
#include "sys/time.h"

using namespace PRDF;
using namespace TARGETING;
using namespace std;

namespace ATTN
{

bool RandSource::start()
{
    bool success = false;

    mutex_lock(&iv_mutex);

    if(!iv_tid)
    {
        iv_tid = task_create(&main, this);
    }

    success = iv_tid != 0;

    ATTN_DBG("RandSource started: %d", iv_tid);

    mutex_unlock(&iv_mutex);

    return success;
}

bool RandSource::wait()
{
    mutex_lock(&iv_mutex);

    tid_t t = iv_tid;
    iv_tid = 0;

    mutex_unlock(&iv_mutex);

    if(t)
    {
        task_wait_tid(t, 0, 0);
    }

    return true;
}

void* RandSource::main(void * i_source)
{
    RandSource * src = static_cast<RandSource *>(i_source);

    src->run();
    return NULL;
}

void RandSource::run()
{
    mutex_lock(&iv_mutex);

    uint64_t iterations = iv_iterations, delay, count;

    mutex_unlock(&iv_mutex);

    while(iterations--)
    {
        delay = randint(TEN_CTX_SWITCHES_NS, TEN_CTX_SWITCHES_NS * 10) / iv_iterations +1;
        count = randint(1, iv_max);

        nanosleep(0, delay);

        AttnList l;
        AttnData d;

        while(count--)
        {
            // select a random target

            // generate a random attention

            d.targetHndl = *(iv_first + randint(0, distance(iv_first, iv_last) -1));
            d.attnType = getRandomAttentionType();

            l.push_back(d);
        }

        iv_injectSink->putAttentions(l);
    }
}

RandSource::RandSource(
        uint64_t i_iterations,
        uint64_t i_maxAttnsPerIteration,
        InjectSink & i_injectSink,
        TargetHandle_t * i_first,
        TargetHandle_t * i_last)
    : iv_tid(0),
    iv_iterations(i_iterations),
    iv_max(i_maxAttnsPerIteration),
    iv_injectSink(&i_injectSink),
    iv_first(i_first),
    iv_last(i_last)
{
    mutex_init(&iv_mutex);
}

RandSource::~RandSource()
{
    mutex_destroy(&iv_mutex);
}
}
OpenPOWER on IntegriCloud