aboutsummaryrefslogtreecommitdiffstats
path: root/xen/common/ac_timer.c
blob: 9bb5d7e3015212131f937ad0de8dabd892efadab (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
/* -*-  Mode:C; c-basic-offset:4; tab-width:4 -*-
 ****************************************************************************
 * (C) 2002 - Rolf Neugebauer - Intel Research Cambridge
 ****************************************************************************
 *
 *        File: ac_timer.c
 *      Author: Rolf Neugebauer (neugebar@dcs.gla.ac.uk)
 *     Changes: 
 *              
 *        Date: Nov 2002
 * 
 * Environment: Xen Hypervisor
 * Description: Accurate timer for the Hypervisor
 *
 ****************************************************************************
 * $Id: c-insert.c,v 1.7 2002/11/08 16:04:34 rn Exp $
 ****************************************************************************
 */

#include <xeno/config.h>
#include <xeno/init.h>
#include <xeno/types.h>
#include <xeno/errno.h>
#include <xeno/sched.h>
#include <xeno/lib.h>
#include <xeno/config.h>
#include <xeno/smp.h>
#include <xeno/init.h>

#include <xeno/time.h>
#include <xeno/ac_timer.h>
#include <xeno/keyhandler.h>

#include <asm/system.h>
#include <asm/desc.h>


#undef AC_TIMER_TRACE
#undef AC_TIMER_STATS

#ifdef AC_TIMER_TRACE
#define TRC(_x) _x
#else
#define TRC(_x)
#endif

/*
 * We pull handlers off the timer list this far in future,
 * rather than reprogramming the time hardware.
 */
#define TIMER_SLOP (50*1000) /* ns */

/* A timer list per CPU */
typedef struct ac_timers_st
{
    spinlock_t lock;
    struct list_head timers;
    struct ac_timer *prev, *curr;
} __cacheline_aligned ac_timers_t;
static ac_timers_t ac_timers[NR_CPUS];

#ifdef AC_TIMER_STATS
#define BUCKETS		1000
#define MAX_STATS
typedef struct act_stats_st
{
    u32 count;
    u32 times[2*(BUCKETS)];
} __cacheline_aligned act_stats_t;
static act_stats_t act_stats[NR_CPUS];

#endif

/* local prototypes */
static int  detach_ac_timer(struct ac_timer *timer);
/*static void ac_timer_debug(unsigned long);*/

/*
 * add a timer.
 * return value:
 *  0: success
 *  1: failure, timer in the past or timeout value to small
 * -1: failure, timer uninitialised
 * fail
 */
int add_ac_timer(struct ac_timer *timer)
{
    int 			 cpu = smp_processor_id();
    unsigned long 	 flags;
    s_time_t		 now;

    /* make sure timeout value is in the future */
	
    now = NOW();
    if (timer->expires <= now) {	
        TRC(printk("ACT[%02d] add_ac_timer:now=0x%08X%08X>expire=0x%08X%08X\n",
				   cpu, (u32)(now>>32), (u32)now,
				   (u32)(timer->expires>>32), (u32)timer->expires));
        return 1;
    }
    spin_lock_irqsave(&ac_timers[cpu].lock, flags);
    /*
     * Add timer to the list. If it gets added to the front we have to
     * reprogramm the timer
     */
    if (list_empty(&ac_timers[cpu].timers)) {
        /* Reprogramm and add to head of list */
        if (!reprogram_ac_timer(timer->expires)) {
            spin_unlock_irqrestore(&ac_timers[cpu].lock, flags);
            return 1; /* failed */
        }
        list_add(&timer->timer_list, &ac_timers[cpu].timers);
    } else {
        struct list_head *pos;
        struct ac_timer	 *t;

		list_for_each(pos, &ac_timers[cpu].timers) {
			t = list_entry(pos, struct ac_timer, timer_list);
			if (t->expires > timer->expires)
                break;
		}
		list_add (&(timer->timer_list), pos->prev);

		if (timer->timer_list.prev == &ac_timers[cpu].timers) {
			/* added at head */
            if (!reprogram_ac_timer(timer->expires)) {
				detach_ac_timer(timer);
                spin_unlock_irqrestore(&ac_timers[cpu].lock, flags);
                return 1; /* failed */
            }
		}
    }
    spin_unlock_irqrestore(&ac_timers[cpu].lock, flags);
    return 0;
}

/*
 * remove a timer
 * return values:
 *  0: success
 * -1: bogus timer
 */
static int detach_ac_timer(struct ac_timer *timer)
{  
    TRC(int 			 cpu = smp_processor_id());
    TRC(printk("ACT  [%02d] detach(): \n", cpu));
    list_del(&timer->timer_list);
    timer->timer_list.next = NULL;
    return 0;
}

/*
 * remove a timer
 * return values:
 *  0: success
 * -1: bogus timer
 */
int rem_ac_timer(struct ac_timer *timer)
{
    int 		  cpu = smp_processor_id();
    int           res = 0;
    unsigned long flags;

    TRC(printk("ACT  [%02d] remove(): timo=%lld \n", cpu, timer->expires));

    spin_lock_irqsave(&ac_timers[cpu].lock, flags);
	if (!timer->timer_list.next == NULL)
		res = detach_ac_timer(timer);	
    spin_unlock_irqrestore(&ac_timers[cpu].lock, flags);

    return res;
}

/*
 * modify a timer, i.e., set a new timeout value
 * return value:
 *  0: sucess
 * -1: error
 */
int mod_ac_timer(struct ac_timer *timer, s_time_t new_time)
{
    if (rem_ac_timer(timer) != 0)
        return -1;
    timer->expires = new_time;
    if (add_ac_timer(timer) != 0)
        return -1;
    return 0;
}

/*
 * do_ac_timer
 * deal with timeouts and run the handlers
 */
void do_ac_timer(void)
{
    int 			 cpu = smp_processor_id();
    unsigned long 	 flags;
    struct ac_timer	 *t;

    spin_lock_irqsave(&ac_timers[cpu].lock, flags);

 do_timer_again:

    TRC(printk("ACT  [%02d] do(): now=%lld\n", cpu, NOW()));
		
	/* Sanity: is the timer list empty? */
    if ( list_empty(&ac_timers[cpu].timers) )
        printk("ACT[%02d] do_ac_timer(): timer irq without timer\n", cpu);

#ifdef AC_TIMER_STATS
    {
        s32	diff;
        u32 i;
        diff = ((s32)(NOW() - t->expires)) / 1000; /* delta in us */
        if (diff < -BUCKETS)
            diff = -BUCKETS;
        else if (diff > BUCKETS)
            diff = BUCKETS;
        act_stats[cpu].times[diff+BUCKETS]++;
        act_stats[cpu].count++;

        if (act_stats[cpu].count >= 5000) {
            printk("ACT Stats\n");
			for (i=0; i < 2*BUCKETS; i++) {
				if (act_stats[cpu].times[i] != 0)
                    printk("ACT [%02d]: %3dus: %5d\n",
                           cpu,i-BUCKETS, act_stats[cpu].times[i]);
                act_stats[cpu].times[i]=0;
            }
            act_stats[cpu].count = 0;
            printk("\n");
        }
    }
#endif

    /* Handle all timeouts in the near future. */
    while ( !list_empty(&ac_timers[cpu].timers) )
    {
        t = list_entry(ac_timers[cpu].timers.next, 
                       struct ac_timer, timer_list);
        if ( t->expires > (NOW() + TIMER_SLOP) ) break;
        detach_ac_timer(t);
        spin_unlock_irqrestore(&ac_timers[cpu].lock, flags);
        if ( t->function != NULL ) t->function(t->data);
        spin_lock_irqsave(&ac_timers[cpu].lock, flags);
    }
		
    /* If list not empty then reprogram timer to new head of list */
    if ( !list_empty(&ac_timers[cpu].timers) )
    {
        t = list_entry(ac_timers[cpu].timers.next, 
                       struct ac_timer, timer_list);
        if ( t->expires > 0 )
        {
            TRC(printk("ACT  [%02d] do(): reprog timo=%lld\n",cpu,t->expires));
            if ( !reprogram_ac_timer(t->expires) )
            {
                TRC(printk("ACT  [%02d] do(): again\n", cpu));
                goto do_timer_again;
            }
        }
    }

    spin_unlock_irqrestore(&ac_timers[cpu].lock, flags);
    TRC(printk("ACT  [%02d] do(): end\n", cpu));
}

/*
 * debug dump_queue
 * arguments: queue head, name of queue
 */
static void dump_tqueue(struct list_head *queue, char *name)
{
    struct list_head *list;
    int loop = 0;
    struct ac_timer	 *t;

    printk ("QUEUE %s %lx   n: %lx, p: %lx\n", name,  (unsigned long)queue,
            (unsigned long) queue->next, (unsigned long) queue->prev);
    list_for_each (list, queue) {
        t = list_entry(list, struct ac_timer, timer_list);
        printk ("  %s %d : %lx ex=0x%08X%08X %lu  n: %lx, p: %lx\n",
                name, loop++, 
                (unsigned long)list,
                (u32)(t->expires>>32), (u32)t->expires, t->data,
                (unsigned long)list->next, (unsigned long)list->prev);
    }
    return; 
}


static void dump_timerq(u_char key, void *dev_id, struct pt_regs *regs)
{
    u_long   flags; 
    s_time_t now = NOW();

    printk("Dumping ac_timer queues for cpu 0: NOW=0x%08X%08X\n",
           (u32)(now>>32), (u32)now); 
	
    spin_lock_irqsave(&ac_timers[0].lock, flags);
    dump_tqueue(&ac_timers[0].timers, "ac_time"); 
    spin_unlock_irqrestore(&ac_timers[0].lock, flags);
    printk("\n");
    return; 
}


void __init ac_timer_init(void)
{
    int i;

    printk ("ACT: Initialising Accurate timers\n");

    for (i = 0; i < NR_CPUS; i++)
    {
        INIT_LIST_HEAD(&ac_timers[i].timers);
        spin_lock_init(&ac_timers[i].lock);
    }

    add_key_handler('a', dump_timerq, "dump ac_timer queues");
}