58 static int firstsub, nbuckets, qsize, lastbucket;
59 static bool resize_enabled;
60 static double top_threshold, bot_threshold, lastprio;
62 static double buckettop, cwidth;
73 static void localinit(
int qbase,
int nbucks,
double bwidth,
double startprio)
80 calendar = calq + qbase;
86 for (i = 0; i < nbuckets; i++) {
92 n = (long)((
double)startprio / cwidth);
93 lastbucket = n % nbuckets;
94 buckettop = (double)(n + 1) * cwidth + 0.5 * cwidth;
97 bot_threshold = (int)((
double)nbuckets / 2) - 2;
98 top_threshold = 2 * nbuckets;
104 static double new_width(
void)
106 int nsamples, templastbucket, i, j;
108 double tempbuckettop, average, newaverage;
112 for (i = 0; i < 25; i++) {
123 nsamples = 5 + (int)((
double)qsize / 10);
129 templastbucket = lastbucket;
130 templastprio = lastprio;
131 tempbuckettop = buckettop;
133 resize_enabled =
false;
136 for (i = 0; i < nsamples; i++) {
138 temp[i] = calqueue_deq();
140 average += temp[i]->timestamp - temp[i - 1]->timestamp;
145 average = average / (double)(nsamples - 1);
151 calqueue_put(temp[0]->timestamp, temp[0]->payload);
154 for (i = 1; i < nsamples; i++) {
155 if ((temp[i]->timestamp - temp[i - 1]->timestamp) < (average * 2.0)) {
156 newaverage += (temp[i]->timestamp - temp[i - 1]->timestamp);
159 calqueue_put(temp[i]->timestamp, temp[i]->payload);
163 for (i = 0; i < 25; i++) {
164 if (temp[i] != NULL) {
170 newaverage = (newaverage / (double)j) * 3.0;
173 lastbucket = templastbucket;
174 lastprio = templastprio;
175 buckettop = tempbuckettop;
176 resize_enabled =
true;
183 static void resize(
int newsize)
194 bwidth = new_width();
197 oldcalendar = calendar;
198 oldnbuckets = nbuckets;
202 localinit(CALQSPACE - newsize, newsize, bwidth, lastprio);
204 localinit(0, newsize, bwidth, lastprio);
208 for (i = oldnbuckets - 1; i >= 0; --i) {
209 temp = oldcalendar[i];
210 while (temp != NULL) {
211 calqueue_put(temp->timestamp, temp->payload);
231 for (i = lastbucket;;) {
233 if (calendar[i] != NULL && calendar[i]->timestamp < buckettop) {
242 lastprio = e->timestamp;
246 calendar[i] = calendar[i]->next;
249 if (qsize < bot_threshold)
250 resize((
int)((
double)nbuckets / 2));
270 lowest = (double)LLONG_MAX;
271 for (i = 0; i < nbuckets; i++) {
272 if ((calendar[i] != NULL)
273 && ((temp2 == -1) || (calendar[i]->timestamp < lowest))) {
275 lowest = calendar[i]->timestamp;
281 goto calendar_process;
286 void calqueue_init(
void)
288 localinit(0, 2, 1.0, 0.0);
289 resize_enabled =
true;
292 void calqueue_put(
double timestamp,
void *payload)
299 new_node->timestamp = timestamp;
300 new_node->payload = payload;
301 new_node->next = NULL;
304 i = (int)round(timestamp / (
double)cwidth);
308 traverse = calendar[i];
311 if (traverse == NULL || traverse->timestamp > timestamp) {
312 new_node->next = traverse;
313 calendar[i] = new_node;
316 while (traverse->next != NULL
317 && traverse->next->timestamp <= timestamp)
318 traverse = traverse->next;
321 new_node->next = traverse->next;
322 traverse->next = new_node;
329 if (qsize > top_threshold && nbuckets < MAXNBUCKETS) {
330 resize(2 * nbuckets);
334 void *calqueue_get(
void)
339 node = calqueue_deq();
344 payload = node->payload;
#define likely(exp)
Optimize the branch as likely taken.
Core ROOT-Sim functionalities.
Memory Manager main header.
Calendar Queue Implementation.
#define unlikely(exp)
Optimize the branch as likely not taken.