summaryrefslogtreecommitdiffstats
path: root/src/kernel/timemgr.C
blob: 37d2580294f091ddea5002754b6f0539d2e0112f (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
/* IBM_PROLOG_BEGIN_TAG                                                   */
/* This is an automatically generated prolog.                             */
/*                                                                        */
/* $Source: src/kernel/timemgr.C $                                        */
/*                                                                        */
/* IBM CONFIDENTIAL                                                       */
/*                                                                        */
/* COPYRIGHT International Business Machines Corp. 2010,2014              */
/*                                                                        */
/* 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 <kernel/timemgr.H>
#include <kernel/scheduler.H>
#include <util/singleton.H>
#include <kernel/task.H>
#include <kernel/cpumgr.H>
#include <util/misc.H>
#include <kernel/misc.H>
#include <sys/task.h>

uint64_t TimeManager::iv_timebaseFreq = 0xFFFFFFFF;

bool TimeManager::cv_isSimicsRunning = Util::isSimicsRunning();

void TimeManager::init()
{
    Singleton<TimeManager>::instance()._init();
}

void TimeManager::_init()
{
    iv_timebaseFreq = 512000000ULL;
}

void TimeManager::init_cpu(cpu_t* cpu)
{
    Singleton<TimeManager>::instance()._init_cpu(cpu);
}

void TimeManager::_init_cpu(cpu_t* cpu)
{
    cpu->delay_list = new delaylist_t();
}

uint64_t TimeManager::convertSecToTicks(uint64_t i_sec, uint64_t i_nsec)
{
    // This code will handle times almost up to a year without overflowing a
    // uint64.  This should be more than sufficient for our purposes.

    // Result = ((sec * 10^9 + nsec) * tb) / 10^9.
    uint64_t result = ((i_sec * 1000000000ULL) + i_nsec);
    result *= (iv_timebaseFreq / 1000000);
    result /= 1000;
    return result;
}

void TimeManager::convertTicksToSec(uint64_t i_ticks,
                                    uint64_t& o_sec, uint64_t& o_nsec)
{
    o_sec = i_ticks / iv_timebaseFreq;

    o_nsec = (i_ticks - (o_sec * iv_timebaseFreq)) * 1000;
    o_nsec /= (iv_timebaseFreq / 1000000);
}

void TimeManager::delayTask(task_t* t, uint64_t i_sec, uint64_t i_nsec)
{
    Singleton<TimeManager>::instance()._delayTask(t,i_sec,i_nsec);
}

void TimeManager::_delayTask(task_t* t, uint64_t i_sec, uint64_t i_nsec)
{
    _TimeManager_Delay_t* node = new _TimeManager_Delay_t();

    node->key = this->getCurrentTimeBase() +
                this->convertSecToTicks(i_sec, i_nsec);
    node->task = t;

    t->state = TASK_STATE_BLOCK_SLEEP;
    t->state_info = (void*)node->key;

    _get_delaylist()->insert(node);
}

void TimeManager::checkReleaseTasks(Scheduler* s)
{
    Singleton<TimeManager>::instance()._checkReleaseTasks(s);
}

void TimeManager::_checkReleaseTasks(Scheduler* s)
{
    uint64_t l_currentTime = getCurrentTimeBase();
    _TimeManager_Delay_t* node = NULL;

    while(NULL != (node = _get_delaylist()->remove_if(l_currentTime)))
    {
        s->addTask(node->task);
        delete node;
    }
}

TimeManager::delaylist_t* TimeManager::_get_delaylist()
{
    return static_cast<delaylist_t*>(CpuManager::getCurrentCPU()->delay_list);
}

uint64_t TimeManager::getIdleTimeSliceCount()
{
    uint64_t sliceCount = getTimeSliceCount();

    // In Simics we want to idle longer to improve overall performance.  Set
    // the idle baseline at 10x normal idle frequency.
    if (cv_isSimicsRunning)
    {
        sliceCount *= 10;
    }

    // Get a delayed task, if there is one
    _TimeManager_Delay_t* node = _get_delaylist()->front();

    if(node)
    {
        uint64_t currentTime = getCurrentTimeBase();
        if(currentTime < node->key)
        {
            uint64_t diffTime = node->key - currentTime;
            if(diffTime < sliceCount)
            {
                sliceCount = diffTime;
            }
        }
        else  // ready to run now! Minimum delay
        {
            sliceCount = 1;
        }
    }

    return sliceCount;
}

bool TimeManager::simpleDelay(uint64_t i_sec, uint64_t i_nsec)
{
    bool result = false;

    uint64_t threshold = getTimeSliceCount()/YIELD_THRESHOLD_PER_SLICE;
    uint64_t delay = convertSecToTicks(i_sec, i_nsec);

    bool isUserspace = !KernelMisc::in_kernel_mode();

    // TB is not synchronized between cores, so if we're doing a poll we need
    // to pin the task to the current cpu.  Otherwise the getTB can get a
    // drifted answer after a task migration.
    if (isUserspace)
    {
        task_affinity_pin();
    }

    // Do polling delay.
    if(delay < threshold)
    {
        uint64_t expire = getCurrentTimeBase() + delay;
        while(getCurrentTimeBase() < expire)
        {
            setThreadPriorityLow();
        }
        setThreadPriorityHigh();
        result = true;
    }

    if (isUserspace)
    {
        task_affinity_unpin();
    }

    return result;
}

OpenPOWER on IntegriCloud