source: rtems-libbsd/freebsd/sys/kern/kern_timeout.c @ e37f03e

55-freebsd-126-freebsd-12
Last change on this file since e37f03e was e37f03e, checked in by Sebastian Huber <sebastian.huber@…>, on 07/26/18 at 12:35:56

Make sure CALLOUT_PROCESSED is not used

This flag is only used in combination with the callout expire list,
which we do not use in libbsd.

  • Property mode set to 100644
File size: 50.5 KB
Line 
1#include <machine/rtems-bsd-kernel-space.h>
2
3/*-
4 * Copyright (c) 1982, 1986, 1991, 1993
5 *      The Regents of the University of California.  All rights reserved.
6 * (c) UNIX System Laboratories, Inc.
7 * All or some portions of this file are derived from material licensed
8 * to the University of California by American Telephone and Telegraph
9 * Co. or Unix System Laboratories, Inc. and are reproduced herein with
10 * the permission of UNIX System Laboratories, Inc.
11 *
12 * Redistribution and use in source and binary forms, with or without
13 * modification, are permitted provided that the following conditions
14 * are met:
15 * 1. Redistributions of source code must retain the above copyright
16 *    notice, this list of conditions and the following disclaimer.
17 * 2. Redistributions in binary form must reproduce the above copyright
18 *    notice, this list of conditions and the following disclaimer in the
19 *    documentation and/or other materials provided with the distribution.
20 * 3. Neither the name of the University nor the names of its contributors
21 *    may be used to endorse or promote products derived from this software
22 *    without specific prior written permission.
23 *
24 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 * SUCH DAMAGE.
35 *
36 *      From: @(#)kern_clock.c  8.5 (Berkeley) 1/21/94
37 */
38
39#include <sys/cdefs.h>
40__FBSDID("$FreeBSD$");
41
42#include <rtems/bsd/local/opt_callout_profiling.h>
43#include <rtems/bsd/local/opt_ddb.h>
44#if defined(__arm__) || defined(__rtems__)
45#include <rtems/bsd/local/opt_timer.h>
46#endif
47#include <rtems/bsd/local/opt_rss.h>
48
49#include <sys/param.h>
50#include <sys/systm.h>
51#include <sys/bus.h>
52#include <sys/callout.h>
53#include <sys/file.h>
54#include <sys/interrupt.h>
55#include <sys/kernel.h>
56#include <sys/ktr.h>
57#include <sys/lock.h>
58#include <sys/malloc.h>
59#include <sys/mutex.h>
60#include <sys/proc.h>
61#include <sys/sdt.h>
62#include <sys/sleepqueue.h>
63#include <sys/sysctl.h>
64#include <sys/smp.h>
65
66#ifdef DDB
67#include <ddb/ddb.h>
68#include <machine/_inttypes.h>
69#endif
70
71#ifdef SMP
72#include <machine/cpu.h>
73#endif
74
75#ifndef NO_EVENTTIMERS
76DPCPU_DECLARE(sbintime_t, hardclocktime);
77#endif
78
79SDT_PROVIDER_DEFINE(callout_execute);
80SDT_PROBE_DEFINE1(callout_execute, , , callout__start, "struct callout *");
81SDT_PROBE_DEFINE1(callout_execute, , , callout__end, "struct callout *");
82
83#ifdef CALLOUT_PROFILING
84static int avg_depth;
85SYSCTL_INT(_debug, OID_AUTO, to_avg_depth, CTLFLAG_RD, &avg_depth, 0,
86    "Average number of items examined per softclock call. Units = 1/1000");
87static int avg_gcalls;
88SYSCTL_INT(_debug, OID_AUTO, to_avg_gcalls, CTLFLAG_RD, &avg_gcalls, 0,
89    "Average number of Giant callouts made per softclock call. Units = 1/1000");
90static int avg_lockcalls;
91SYSCTL_INT(_debug, OID_AUTO, to_avg_lockcalls, CTLFLAG_RD, &avg_lockcalls, 0,
92    "Average number of lock callouts made per softclock call. Units = 1/1000");
93static int avg_mpcalls;
94SYSCTL_INT(_debug, OID_AUTO, to_avg_mpcalls, CTLFLAG_RD, &avg_mpcalls, 0,
95    "Average number of MP callouts made per softclock call. Units = 1/1000");
96static int avg_depth_dir;
97SYSCTL_INT(_debug, OID_AUTO, to_avg_depth_dir, CTLFLAG_RD, &avg_depth_dir, 0,
98    "Average number of direct callouts examined per callout_process call. "
99    "Units = 1/1000");
100static int avg_lockcalls_dir;
101SYSCTL_INT(_debug, OID_AUTO, to_avg_lockcalls_dir, CTLFLAG_RD,
102    &avg_lockcalls_dir, 0, "Average number of lock direct callouts made per "
103    "callout_process call. Units = 1/1000");
104static int avg_mpcalls_dir;
105SYSCTL_INT(_debug, OID_AUTO, to_avg_mpcalls_dir, CTLFLAG_RD, &avg_mpcalls_dir,
106    0, "Average number of MP direct callouts made per callout_process call. "
107    "Units = 1/1000");
108#endif
109
110#ifndef __rtems__
111static int ncallout;
112SYSCTL_INT(_kern, OID_AUTO, ncallout, CTLFLAG_RDTUN | CTLFLAG_NOFETCH, &ncallout, 0,
113    "Number of entries in callwheel and size of timeout() preallocation");
114#else /* __rtems__ */
115#define ncallout 16
116#endif /* __rtems__ */
117
118#ifdef  RSS
119static int pin_default_swi = 1;
120static int pin_pcpu_swi = 1;
121#else
122static int pin_default_swi = 0;
123static int pin_pcpu_swi = 0;
124#endif
125
126SYSCTL_INT(_kern, OID_AUTO, pin_default_swi, CTLFLAG_RDTUN | CTLFLAG_NOFETCH, &pin_default_swi,
127    0, "Pin the default (non-per-cpu) swi (shared with PCPU 0 swi)");
128SYSCTL_INT(_kern, OID_AUTO, pin_pcpu_swi, CTLFLAG_RDTUN | CTLFLAG_NOFETCH, &pin_pcpu_swi,
129    0, "Pin the per-CPU swis (except PCPU 0, which is also default");
130
131/*
132 * TODO:
133 *      allocate more timeout table slots when table overflows.
134 */
135u_int callwheelsize, callwheelmask;
136
137/*
138 * The callout cpu exec entities represent informations necessary for
139 * describing the state of callouts currently running on the CPU and the ones
140 * necessary for migrating callouts to the new callout cpu. In particular,
141 * the first entry of the array cc_exec_entity holds informations for callout
142 * running in SWI thread context, while the second one holds informations
143 * for callout running directly from hardware interrupt context.
144 * The cached informations are very important for deferring migration when
145 * the migrating callout is already running.
146 */
147struct cc_exec {
148        struct callout          *cc_curr;
149        void                    (*cc_drain)(void *);
150#ifdef SMP
151        void                    (*ce_migration_func)(void *);
152        void                    *ce_migration_arg;
153        int                     ce_migration_cpu;
154        sbintime_t              ce_migration_time;
155        sbintime_t              ce_migration_prec;
156#endif
157        bool                    cc_cancel;
158        bool                    cc_waiting;
159};
160
161/*
162 * There is one struct callout_cpu per cpu, holding all relevant
163 * state for the callout processing thread on the individual CPU.
164 */
165struct callout_cpu {
166        struct mtx_padalign     cc_lock;
167#ifndef __rtems__
168        struct cc_exec          cc_exec_entity[2];
169#else /* __rtems__ */
170        struct cc_exec          cc_exec_entity;
171#endif /* __rtems__ */
172        struct callout          *cc_next;
173        struct callout          *cc_callout;
174        struct callout_list     *cc_callwheel;
175#ifndef __rtems__
176        struct callout_tailq    cc_expireq;
177#endif /* __rtems__ */
178        struct callout_slist    cc_callfree;
179        sbintime_t              cc_firstevent;
180        sbintime_t              cc_lastscan;
181        void                    *cc_cookie;
182        u_int                   cc_bucket;
183        u_int                   cc_inited;
184        char                    cc_ktr_event_name[20];
185};
186
187#define callout_migrating(c)    ((c)->c_iflags & CALLOUT_DFRMIGRATION)
188
189#ifndef __rtems__
190#define cc_exec_curr(cc, dir)           cc->cc_exec_entity[dir].cc_curr
191#define cc_exec_drain(cc, dir)          cc->cc_exec_entity[dir].cc_drain
192#else /* __rtems__ */
193#define cc_exec_curr(cc, dir)           cc->cc_exec_entity.cc_curr
194#define cc_exec_drain(cc, dir)          cc->cc_exec_entity.cc_drain
195#endif /* __rtems__ */
196#define cc_exec_next(cc)                cc->cc_next
197#ifndef __rtems__
198#define cc_exec_cancel(cc, dir)         cc->cc_exec_entity[dir].cc_cancel
199#define cc_exec_waiting(cc, dir)        cc->cc_exec_entity[dir].cc_waiting
200#else /* __rtems__ */
201#define cc_exec_cancel(cc, dir)         cc->cc_exec_entity.cc_cancel
202#define cc_exec_waiting(cc, dir)        cc->cc_exec_entity.cc_waiting
203#endif /* __rtems__ */
204#ifdef SMP
205#define cc_migration_func(cc, dir)      cc->cc_exec_entity[dir].ce_migration_func
206#define cc_migration_arg(cc, dir)       cc->cc_exec_entity[dir].ce_migration_arg
207#define cc_migration_cpu(cc, dir)       cc->cc_exec_entity[dir].ce_migration_cpu
208#define cc_migration_time(cc, dir)      cc->cc_exec_entity[dir].ce_migration_time
209#define cc_migration_prec(cc, dir)      cc->cc_exec_entity[dir].ce_migration_prec
210
211struct callout_cpu cc_cpu[MAXCPU];
212#define CPUBLOCK        MAXCPU
213#define CC_CPU(cpu)     (&cc_cpu[(cpu)])
214#define CC_SELF()       CC_CPU(PCPU_GET(cpuid))
215#else
216struct callout_cpu cc_cpu;
217#define CC_CPU(cpu)     &cc_cpu
218#define CC_SELF()       &cc_cpu
219#endif
220#define CC_LOCK(cc)     mtx_lock_spin(&(cc)->cc_lock)
221#define CC_UNLOCK(cc)   mtx_unlock_spin(&(cc)->cc_lock)
222#define CC_LOCK_ASSERT(cc)      mtx_assert(&(cc)->cc_lock, MA_OWNED)
223
224static int timeout_cpu;
225
226static void     callout_cpu_init(struct callout_cpu *cc, int cpu);
227static void     softclock_call_cc(struct callout *c, struct callout_cpu *cc,
228#ifdef CALLOUT_PROFILING
229                    int *mpcalls, int *lockcalls, int *gcalls,
230#endif
231                    int direct);
232
233static MALLOC_DEFINE(M_CALLOUT, "callout", "Callout datastructures");
234
235/**
236 * Locked by cc_lock:
237 *   cc_curr         - If a callout is in progress, it is cc_curr.
238 *                     If cc_curr is non-NULL, threads waiting in
239 *                     callout_drain() will be woken up as soon as the
240 *                     relevant callout completes.
241 *   cc_cancel       - Changing to 1 with both callout_lock and cc_lock held
242 *                     guarantees that the current callout will not run.
243 *                     The softclock() function sets this to 0 before it
244 *                     drops callout_lock to acquire c_lock, and it calls
245 *                     the handler only if curr_cancelled is still 0 after
246 *                     cc_lock is successfully acquired.
247 *   cc_waiting      - If a thread is waiting in callout_drain(), then
248 *                     callout_wait is nonzero.  Set only when
249 *                     cc_curr is non-NULL.
250 */
251
252/*
253 * Resets the execution entity tied to a specific callout cpu.
254 */
255static void
256cc_cce_cleanup(struct callout_cpu *cc, int direct)
257{
258
259        cc_exec_curr(cc, direct) = NULL;
260        cc_exec_cancel(cc, direct) = false;
261        cc_exec_waiting(cc, direct) = false;
262#ifdef SMP
263        cc_migration_cpu(cc, direct) = CPUBLOCK;
264        cc_migration_time(cc, direct) = 0;
265        cc_migration_prec(cc, direct) = 0;
266        cc_migration_func(cc, direct) = NULL;
267        cc_migration_arg(cc, direct) = NULL;
268#endif
269}
270
271/*
272 * Checks if migration is requested by a specific callout cpu.
273 */
274static int
275cc_cce_migrating(struct callout_cpu *cc, int direct)
276{
277
278#ifdef SMP
279        return (cc_migration_cpu(cc, direct) != CPUBLOCK);
280#else
281        return (0);
282#endif
283}
284
285/*
286 * Kernel low level callwheel initialization
287 * called on cpu0 during kernel startup.
288 */
289#ifdef __rtems__
290static void rtems_bsd_timeout_init_early(void *);
291
292static void
293rtems_bsd_callout_timer(rtems_id id, void *arg)
294{
295        rtems_status_code sc;
296
297        (void) arg;
298
299        sc = rtems_timer_reset(id);
300        BSD_ASSERT(sc == RTEMS_SUCCESSFUL);
301
302        callout_process(sbinuptime());
303}
304
305static void
306rtems_bsd_timeout_init_late(void *unused)
307{
308        rtems_status_code sc;
309        rtems_id id;
310
311        (void) unused;
312
313        sc = rtems_timer_create(rtems_build_name('_', 'C', 'L', 'O'), &id);
314        BSD_ASSERT(sc == RTEMS_SUCCESSFUL);
315
316        sc = rtems_timer_server_fire_after(id, 1, rtems_bsd_callout_timer, NULL);
317        BSD_ASSERT(sc == RTEMS_SUCCESSFUL);
318}
319
320SYSINIT(rtems_bsd_timeout_early, SI_SUB_VM, SI_ORDER_FIRST,
321    rtems_bsd_timeout_init_early, NULL);
322
323SYSINIT(rtems_bsd_timeout_late, SI_SUB_LAST, SI_ORDER_FIRST,
324    rtems_bsd_timeout_init_late, NULL);
325
326static void
327rtems_bsd_timeout_init_early(void *dummy)
328#else /* __rtems__ */
329static void
330callout_callwheel_init(void *dummy)
331#endif /* __rtems__ */
332{
333        struct callout_cpu *cc;
334#ifdef __rtems__
335        (void) dummy;
336#endif /* __rtems__ */
337
338        /*
339         * Calculate the size of the callout wheel and the preallocated
340         * timeout() structures.
341         * XXX: Clip callout to result of previous function of maxusers
342         * maximum 384.  This is still huge, but acceptable.
343         */
344        memset(CC_CPU(0), 0, sizeof(cc_cpu));
345#ifndef __rtems__
346        ncallout = imin(16 + maxproc + maxfiles, 18508);
347        TUNABLE_INT_FETCH("kern.ncallout", &ncallout);
348#endif /* __rtems__ */
349
350        /*
351         * Calculate callout wheel size, should be next power of two higher
352         * than 'ncallout'.
353         */
354        callwheelsize = 1 << fls(ncallout);
355        callwheelmask = callwheelsize - 1;
356
357#ifndef __rtems__
358        /*
359         * Fetch whether we're pinning the swi's or not.
360         */
361        TUNABLE_INT_FETCH("kern.pin_default_swi", &pin_default_swi);
362        TUNABLE_INT_FETCH("kern.pin_pcpu_swi", &pin_pcpu_swi);
363#endif /* __rtems__ */
364
365        /*
366         * Only cpu0 handles timeout(9) and receives a preallocation.
367         *
368         * XXX: Once all timeout(9) consumers are converted this can
369         * be removed.
370         */
371        timeout_cpu = PCPU_GET(cpuid);
372        cc = CC_CPU(timeout_cpu);
373        cc->cc_callout = malloc(ncallout * sizeof(struct callout),
374            M_CALLOUT, M_WAITOK);
375        callout_cpu_init(cc, timeout_cpu);
376}
377#ifndef __rtems__
378SYSINIT(callwheel_init, SI_SUB_CPU, SI_ORDER_ANY, callout_callwheel_init, NULL);
379#endif /* __rtems__ */
380
381/*
382 * Initialize the per-cpu callout structures.
383 */
384static void
385callout_cpu_init(struct callout_cpu *cc, int cpu)
386{
387        struct callout *c;
388        int i;
389
390        mtx_init(&cc->cc_lock, "callout", NULL, MTX_SPIN | MTX_RECURSE);
391        SLIST_INIT(&cc->cc_callfree);
392        cc->cc_inited = 1;
393        cc->cc_callwheel = malloc(sizeof(struct callout_list) * callwheelsize,
394            M_CALLOUT, M_WAITOK);
395        for (i = 0; i < callwheelsize; i++)
396                LIST_INIT(&cc->cc_callwheel[i]);
397#ifndef __rtems__
398        TAILQ_INIT(&cc->cc_expireq);
399#endif /* __rtems__ */
400        cc->cc_firstevent = SBT_MAX;
401        for (i = 0; i < 2; i++)
402                cc_cce_cleanup(cc, i);
403        snprintf(cc->cc_ktr_event_name, sizeof(cc->cc_ktr_event_name),
404            "callwheel cpu %d", cpu);
405        if (cc->cc_callout == NULL)     /* Only cpu0 handles timeout(9) */
406                return;
407        for (i = 0; i < ncallout; i++) {
408                c = &cc->cc_callout[i];
409                callout_init(c, 0);
410                c->c_iflags = CALLOUT_LOCAL_ALLOC;
411                SLIST_INSERT_HEAD(&cc->cc_callfree, c, c_links.sle);
412        }
413}
414
415#ifdef SMP
416/*
417 * Switches the cpu tied to a specific callout.
418 * The function expects a locked incoming callout cpu and returns with
419 * locked outcoming callout cpu.
420 */
421static struct callout_cpu *
422callout_cpu_switch(struct callout *c, struct callout_cpu *cc, int new_cpu)
423{
424        struct callout_cpu *new_cc;
425
426        MPASS(c != NULL && cc != NULL);
427        CC_LOCK_ASSERT(cc);
428
429        /*
430         * Avoid interrupts and preemption firing after the callout cpu
431         * is blocked in order to avoid deadlocks as the new thread
432         * may be willing to acquire the callout cpu lock.
433         */
434        c->c_cpu = CPUBLOCK;
435        spinlock_enter();
436        CC_UNLOCK(cc);
437        new_cc = CC_CPU(new_cpu);
438        CC_LOCK(new_cc);
439        spinlock_exit();
440        c->c_cpu = new_cpu;
441        return (new_cc);
442}
443#endif
444
445#ifndef __rtems__
446/*
447 * Start standard softclock thread.
448 */
449static void
450start_softclock(void *dummy)
451{
452        struct callout_cpu *cc;
453        char name[MAXCOMLEN];
454#ifdef SMP
455        int cpu;
456        struct intr_event *ie;
457#endif
458
459        cc = CC_CPU(timeout_cpu);
460        snprintf(name, sizeof(name), "clock (%d)", timeout_cpu);
461        if (swi_add(&clk_intr_event, name, softclock, cc, SWI_CLOCK,
462            INTR_MPSAFE, &cc->cc_cookie))
463                panic("died while creating standard software ithreads");
464        if (pin_default_swi &&
465            (intr_event_bind(clk_intr_event, timeout_cpu) != 0)) {
466                printf("%s: timeout clock couldn't be pinned to cpu %d\n",
467                    __func__,
468                    timeout_cpu);
469        }
470
471#ifdef SMP
472        CPU_FOREACH(cpu) {
473                if (cpu == timeout_cpu)
474                        continue;
475                cc = CC_CPU(cpu);
476                cc->cc_callout = NULL;  /* Only cpu0 handles timeout(9). */
477                callout_cpu_init(cc, cpu);
478                snprintf(name, sizeof(name), "clock (%d)", cpu);
479                ie = NULL;
480                if (swi_add(&ie, name, softclock, cc, SWI_CLOCK,
481                    INTR_MPSAFE, &cc->cc_cookie))
482                        panic("died while creating standard software ithreads");
483                if (pin_pcpu_swi && (intr_event_bind(ie, cpu) != 0)) {
484                        printf("%s: per-cpu clock couldn't be pinned to "
485                            "cpu %d\n",
486                            __func__,
487                            cpu);
488                }
489        }
490#endif
491}
492SYSINIT(start_softclock, SI_SUB_SOFTINTR, SI_ORDER_FIRST, start_softclock, NULL);
493#endif /* __rtems__ */
494
495#define CC_HASH_SHIFT   8
496
497static inline u_int
498callout_hash(sbintime_t sbt)
499{
500
501        return (sbt >> (32 - CC_HASH_SHIFT));
502}
503
504static inline u_int
505callout_get_bucket(sbintime_t sbt)
506{
507
508        return (callout_hash(sbt) & callwheelmask);
509}
510
511void
512callout_process(sbintime_t now)
513{
514#ifndef __rtems__
515        struct callout *tmp, *tmpn;
516#else /* __rtems__ */
517        struct callout *tmp;
518#endif /* __rtems__ */
519        struct callout_cpu *cc;
520        struct callout_list *sc;
521        sbintime_t first, last, max, tmp_max;
522        uint32_t lookahead;
523        u_int firstb, lastb, nowb;
524#ifdef CALLOUT_PROFILING
525        int depth_dir = 0, mpcalls_dir = 0, lockcalls_dir = 0;
526#endif
527
528        cc = CC_SELF();
529        mtx_lock_spin_flags(&cc->cc_lock, MTX_QUIET);
530
531        /* Compute the buckets of the last scan and present times. */
532        firstb = callout_hash(cc->cc_lastscan);
533        cc->cc_lastscan = now;
534        nowb = callout_hash(now);
535
536        /* Compute the last bucket and minimum time of the bucket after it. */
537        if (nowb == firstb)
538                lookahead = (SBT_1S / 16);
539        else if (nowb - firstb == 1)
540                lookahead = (SBT_1S / 8);
541        else
542                lookahead = (SBT_1S / 2);
543        first = last = now;
544        first += (lookahead / 2);
545        last += lookahead;
546        last &= (0xffffffffffffffffLLU << (32 - CC_HASH_SHIFT));
547        lastb = callout_hash(last) - 1;
548        max = last;
549
550        /*
551         * Check if we wrapped around the entire wheel from the last scan.
552         * In case, we need to scan entirely the wheel for pending callouts.
553         */
554        if (lastb - firstb >= callwheelsize) {
555                lastb = firstb + callwheelsize - 1;
556                if (nowb - firstb >= callwheelsize)
557                        nowb = lastb;
558        }
559
560        /* Iterate callwheel from firstb to nowb and then up to lastb. */
561        do {
562                sc = &cc->cc_callwheel[firstb & callwheelmask];
563                tmp = LIST_FIRST(sc);
564                while (tmp != NULL) {
565                        /* Run the callout if present time within allowed. */
566                        if (tmp->c_time <= now) {
567#ifndef __rtems__
568                                /*
569                                 * Consumer told us the callout may be run
570                                 * directly from hardware interrupt context.
571                                 */
572                                if (tmp->c_iflags & CALLOUT_DIRECT) {
573#endif /* __rtems__ */
574#ifdef CALLOUT_PROFILING
575                                        ++depth_dir;
576#endif
577                                        cc_exec_next(cc) =
578                                            LIST_NEXT(tmp, c_links.le);
579                                        cc->cc_bucket = firstb & callwheelmask;
580                                        LIST_REMOVE(tmp, c_links.le);
581                                        softclock_call_cc(tmp, cc,
582#ifdef CALLOUT_PROFILING
583                                            &mpcalls_dir, &lockcalls_dir, NULL,
584#endif
585                                            1);
586                                        tmp = cc_exec_next(cc);
587                                        cc_exec_next(cc) = NULL;
588#ifndef __rtems__
589                                } else {
590                                        tmpn = LIST_NEXT(tmp, c_links.le);
591                                        LIST_REMOVE(tmp, c_links.le);
592                                        TAILQ_INSERT_TAIL(&cc->cc_expireq,
593                                            tmp, c_links.tqe);
594                                        tmp->c_iflags |= CALLOUT_PROCESSED;
595                                        tmp = tmpn;
596                                }
597#endif /* __rtems__ */
598                                continue;
599                        }
600                        /* Skip events from distant future. */
601                        if (tmp->c_time >= max)
602                                goto next;
603                        /*
604                         * Event minimal time is bigger than present maximal
605                         * time, so it cannot be aggregated.
606                         */
607                        if (tmp->c_time > last) {
608                                lastb = nowb;
609                                goto next;
610                        }
611                        /* Update first and last time, respecting this event. */
612                        if (tmp->c_time < first)
613                                first = tmp->c_time;
614                        tmp_max = tmp->c_time + tmp->c_precision;
615                        if (tmp_max < last)
616                                last = tmp_max;
617next:
618                        tmp = LIST_NEXT(tmp, c_links.le);
619                }
620                /* Proceed with the next bucket. */
621                firstb++;
622                /*
623                 * Stop if we looked after present time and found
624                 * some event we can't execute at now.
625                 * Stop if we looked far enough into the future.
626                 */
627        } while (((int)(firstb - lastb)) <= 0);
628        cc->cc_firstevent = last;
629#ifndef NO_EVENTTIMERS
630        cpu_new_callout(curcpu, last, first);
631#endif
632#ifdef CALLOUT_PROFILING
633        avg_depth_dir += (depth_dir * 1000 - avg_depth_dir) >> 8;
634        avg_mpcalls_dir += (mpcalls_dir * 1000 - avg_mpcalls_dir) >> 8;
635        avg_lockcalls_dir += (lockcalls_dir * 1000 - avg_lockcalls_dir) >> 8;
636#endif
637        mtx_unlock_spin_flags(&cc->cc_lock, MTX_QUIET);
638#ifndef __rtems__
639        /*
640         * swi_sched acquires the thread lock, so we don't want to call it
641         * with cc_lock held; incorrect locking order.
642         */
643        if (!TAILQ_EMPTY(&cc->cc_expireq))
644                swi_sched(cc->cc_cookie, 0);
645#endif /* __rtems__ */
646}
647
648static struct callout_cpu *
649callout_lock(struct callout *c)
650{
651        struct callout_cpu *cc;
652        int cpu;
653
654        for (;;) {
655                cpu = c->c_cpu;
656#ifdef SMP
657                if (cpu == CPUBLOCK) {
658                        while (c->c_cpu == CPUBLOCK)
659                                cpu_spinwait();
660                        continue;
661                }
662#endif
663                cc = CC_CPU(cpu);
664                CC_LOCK(cc);
665                if (cpu == c->c_cpu)
666                        break;
667                CC_UNLOCK(cc);
668        }
669        return (cc);
670}
671
672static void
673callout_cc_add(struct callout *c, struct callout_cpu *cc,
674    sbintime_t sbt, sbintime_t precision, void (*func)(void *),
675    void *arg, int cpu, int flags)
676{
677        int bucket;
678
679        CC_LOCK_ASSERT(cc);
680        if (sbt < cc->cc_lastscan)
681                sbt = cc->cc_lastscan;
682        c->c_arg = arg;
683        c->c_iflags |= CALLOUT_PENDING;
684#ifndef __rtems__
685        c->c_iflags &= ~CALLOUT_PROCESSED;
686#endif /* __rtems__ */
687        c->c_flags |= CALLOUT_ACTIVE;
688#ifndef __rtems__
689        if (flags & C_DIRECT_EXEC)
690                c->c_iflags |= CALLOUT_DIRECT;
691#endif /* __rtems__ */
692        c->c_func = func;
693        c->c_time = sbt;
694        c->c_precision = precision;
695        bucket = callout_get_bucket(c->c_time);
696        CTR3(KTR_CALLOUT, "precision set for %p: %d.%08x",
697            c, (int)(c->c_precision >> 32),
698            (u_int)(c->c_precision & 0xffffffff));
699        LIST_INSERT_HEAD(&cc->cc_callwheel[bucket], c, c_links.le);
700        if (cc->cc_bucket == bucket)
701                cc_exec_next(cc) = c;
702#ifndef NO_EVENTTIMERS
703        /*
704         * Inform the eventtimers(4) subsystem there's a new callout
705         * that has been inserted, but only if really required.
706         */
707        if (SBT_MAX - c->c_time < c->c_precision)
708                c->c_precision = SBT_MAX - c->c_time;
709        sbt = c->c_time + c->c_precision;
710        if (sbt < cc->cc_firstevent) {
711                cc->cc_firstevent = sbt;
712                cpu_new_callout(cpu, sbt, c->c_time);
713        }
714#endif
715}
716
717static void
718callout_cc_del(struct callout *c, struct callout_cpu *cc)
719{
720
721        if ((c->c_iflags & CALLOUT_LOCAL_ALLOC) == 0)
722                return;
723        c->c_func = NULL;
724        SLIST_INSERT_HEAD(&cc->cc_callfree, c, c_links.sle);
725}
726
727static void
728softclock_call_cc(struct callout *c, struct callout_cpu *cc,
729#ifdef CALLOUT_PROFILING
730    int *mpcalls, int *lockcalls, int *gcalls,
731#endif
732    int direct)
733{
734#ifndef __rtems__
735        struct rm_priotracker tracker;
736#endif /* __rtems__ */
737        void (*c_func)(void *);
738        void *c_arg;
739        struct lock_class *class;
740        struct lock_object *c_lock;
741        uintptr_t lock_status;
742        int c_iflags;
743#ifdef SMP
744        struct callout_cpu *new_cc;
745        void (*new_func)(void *);
746        void *new_arg;
747        int flags, new_cpu;
748        sbintime_t new_prec, new_time;
749#endif
750#if defined(DIAGNOSTIC) || defined(CALLOUT_PROFILING)
751        sbintime_t sbt1, sbt2;
752        struct timespec ts2;
753        static sbintime_t maxdt = 2 * SBT_1MS;  /* 2 msec */
754        static timeout_t *lastfunc;
755#endif
756
757        KASSERT((c->c_iflags & CALLOUT_PENDING) == CALLOUT_PENDING,
758            ("softclock_call_cc: pend %p %x", c, c->c_iflags));
759        KASSERT((c->c_flags & CALLOUT_ACTIVE) == CALLOUT_ACTIVE,
760            ("softclock_call_cc: act %p %x", c, c->c_flags));
761        class = (c->c_lock != NULL) ? LOCK_CLASS(c->c_lock) : NULL;
762        lock_status = 0;
763        if (c->c_flags & CALLOUT_SHAREDLOCK) {
764#ifndef __rtems__
765                if (class == &lock_class_rm)
766                        lock_status = (uintptr_t)&tracker;
767                else
768#endif /* __rtems__ */
769                        lock_status = 1;
770        }
771        c_lock = c->c_lock;
772        c_func = c->c_func;
773        c_arg = c->c_arg;
774        c_iflags = c->c_iflags;
775        if (c->c_iflags & CALLOUT_LOCAL_ALLOC)
776                c->c_iflags = CALLOUT_LOCAL_ALLOC;
777        else
778                c->c_iflags &= ~CALLOUT_PENDING;
779       
780        cc_exec_curr(cc, direct) = c;
781        cc_exec_cancel(cc, direct) = false;
782        cc_exec_drain(cc, direct) = NULL;
783        CC_UNLOCK(cc);
784        if (c_lock != NULL) {
785                class->lc_lock(c_lock, lock_status);
786                /*
787                 * The callout may have been cancelled
788                 * while we switched locks.
789                 */
790                if (cc_exec_cancel(cc, direct)) {
791                        class->lc_unlock(c_lock);
792                        goto skip;
793                }
794                /* The callout cannot be stopped now. */
795                cc_exec_cancel(cc, direct) = true;
796                if (c_lock == &Giant.lock_object) {
797#ifdef CALLOUT_PROFILING
798                        (*gcalls)++;
799#endif
800                        CTR3(KTR_CALLOUT, "callout giant %p func %p arg %p",
801                            c, c_func, c_arg);
802                } else {
803#ifdef CALLOUT_PROFILING
804                        (*lockcalls)++;
805#endif
806                        CTR3(KTR_CALLOUT, "callout lock %p func %p arg %p",
807                            c, c_func, c_arg);
808                }
809        } else {
810#ifdef CALLOUT_PROFILING
811                (*mpcalls)++;
812#endif
813                CTR3(KTR_CALLOUT, "callout %p func %p arg %p",
814                    c, c_func, c_arg);
815        }
816        KTR_STATE3(KTR_SCHED, "callout", cc->cc_ktr_event_name, "running",
817            "func:%p", c_func, "arg:%p", c_arg, "direct:%d", direct);
818#if defined(DIAGNOSTIC) || defined(CALLOUT_PROFILING)
819        sbt1 = sbinuptime();
820#endif
821#ifndef __rtems__
822        THREAD_NO_SLEEPING();
823        SDT_PROBE1(callout_execute, , , callout__start, c);
824#endif /* __rtems__ */
825        c_func(c_arg);
826#ifndef __rtems__
827        SDT_PROBE1(callout_execute, , , callout__end, c);
828        THREAD_SLEEPING_OK();
829#endif /* __rtems__ */
830#if defined(DIAGNOSTIC) || defined(CALLOUT_PROFILING)
831        sbt2 = sbinuptime();
832        sbt2 -= sbt1;
833        if (sbt2 > maxdt) {
834                if (lastfunc != c_func || sbt2 > maxdt * 2) {
835                        ts2 = sbttots(sbt2);
836                        printf(
837                "Expensive timeout(9) function: %p(%p) %jd.%09ld s\n",
838                            c_func, c_arg, (intmax_t)ts2.tv_sec, ts2.tv_nsec);
839                }
840                maxdt = sbt2;
841                lastfunc = c_func;
842        }
843#endif
844        KTR_STATE0(KTR_SCHED, "callout", cc->cc_ktr_event_name, "idle");
845        CTR1(KTR_CALLOUT, "callout %p finished", c);
846        if ((c_iflags & CALLOUT_RETURNUNLOCKED) == 0)
847                class->lc_unlock(c_lock);
848skip:
849        CC_LOCK(cc);
850        KASSERT(cc_exec_curr(cc, direct) == c, ("mishandled cc_curr"));
851        cc_exec_curr(cc, direct) = NULL;
852        if (cc_exec_drain(cc, direct)) {
853                void (*drain)(void *);
854               
855                drain = cc_exec_drain(cc, direct);
856                cc_exec_drain(cc, direct) = NULL;
857                CC_UNLOCK(cc);
858                drain(c_arg);
859                CC_LOCK(cc);
860        }
861        if (cc_exec_waiting(cc, direct)) {
862                /*
863                 * There is someone waiting for the
864                 * callout to complete.
865                 * If the callout was scheduled for
866                 * migration just cancel it.
867                 */
868                if (cc_cce_migrating(cc, direct)) {
869                        cc_cce_cleanup(cc, direct);
870
871                        /*
872                         * It should be assert here that the callout is not
873                         * destroyed but that is not easy.
874                         */
875                        c->c_iflags &= ~CALLOUT_DFRMIGRATION;
876                }
877                cc_exec_waiting(cc, direct) = false;
878                CC_UNLOCK(cc);
879                wakeup(&cc_exec_waiting(cc, direct));
880                CC_LOCK(cc);
881        } else if (cc_cce_migrating(cc, direct)) {
882                KASSERT((c_iflags & CALLOUT_LOCAL_ALLOC) == 0,
883                    ("Migrating legacy callout %p", c));
884#ifdef SMP
885                /*
886                 * If the callout was scheduled for
887                 * migration just perform it now.
888                 */
889                new_cpu = cc_migration_cpu(cc, direct);
890                new_time = cc_migration_time(cc, direct);
891                new_prec = cc_migration_prec(cc, direct);
892                new_func = cc_migration_func(cc, direct);
893                new_arg = cc_migration_arg(cc, direct);
894                cc_cce_cleanup(cc, direct);
895
896                /*
897                 * It should be assert here that the callout is not destroyed
898                 * but that is not easy.
899                 *
900                 * As first thing, handle deferred callout stops.
901                 */
902                if (!callout_migrating(c)) {
903                        CTR3(KTR_CALLOUT,
904                             "deferred cancelled %p func %p arg %p",
905                             c, new_func, new_arg);
906                        callout_cc_del(c, cc);
907                        return;
908                }
909                c->c_iflags &= ~CALLOUT_DFRMIGRATION;
910
911                new_cc = callout_cpu_switch(c, cc, new_cpu);
912                flags = (direct) ? C_DIRECT_EXEC : 0;
913                callout_cc_add(c, new_cc, new_time, new_prec, new_func,
914                    new_arg, new_cpu, flags);
915                CC_UNLOCK(new_cc);
916                CC_LOCK(cc);
917#else
918                panic("migration should not happen");
919#endif
920        }
921        /*
922         * If the current callout is locally allocated (from
923         * timeout(9)) then put it on the freelist.
924         *
925         * Note: we need to check the cached copy of c_iflags because
926         * if it was not local, then it's not safe to deref the
927         * callout pointer.
928         */
929        KASSERT((c_iflags & CALLOUT_LOCAL_ALLOC) == 0 ||
930            c->c_iflags == CALLOUT_LOCAL_ALLOC,
931            ("corrupted callout"));
932        if (c_iflags & CALLOUT_LOCAL_ALLOC)
933                callout_cc_del(c, cc);
934}
935
936/*
937 * The callout mechanism is based on the work of Adam M. Costello and
938 * George Varghese, published in a technical report entitled "Redesigning
939 * the BSD Callout and Timer Facilities" and modified slightly for inclusion
940 * in FreeBSD by Justin T. Gibbs.  The original work on the data structures
941 * used in this implementation was published by G. Varghese and T. Lauck in
942 * the paper "Hashed and Hierarchical Timing Wheels: Data Structures for
943 * the Efficient Implementation of a Timer Facility" in the Proceedings of
944 * the 11th ACM Annual Symposium on Operating Systems Principles,
945 * Austin, Texas Nov 1987.
946 */
947
948#ifndef __rtems__
949/*
950 * Software (low priority) clock interrupt.
951 * Run periodic events from timeout queue.
952 */
953void
954softclock(void *arg)
955{
956        struct callout_cpu *cc;
957        struct callout *c;
958#ifdef CALLOUT_PROFILING
959        int depth = 0, gcalls = 0, lockcalls = 0, mpcalls = 0;
960#endif
961
962        cc = (struct callout_cpu *)arg;
963        CC_LOCK(cc);
964        while ((c = TAILQ_FIRST(&cc->cc_expireq)) != NULL) {
965                TAILQ_REMOVE(&cc->cc_expireq, c, c_links.tqe);
966                softclock_call_cc(c, cc,
967#ifdef CALLOUT_PROFILING
968                    &mpcalls, &lockcalls, &gcalls,
969#endif
970                    0);
971#ifdef CALLOUT_PROFILING
972                ++depth;
973#endif
974        }
975#ifdef CALLOUT_PROFILING
976        avg_depth += (depth * 1000 - avg_depth) >> 8;
977        avg_mpcalls += (mpcalls * 1000 - avg_mpcalls) >> 8;
978        avg_lockcalls += (lockcalls * 1000 - avg_lockcalls) >> 8;
979        avg_gcalls += (gcalls * 1000 - avg_gcalls) >> 8;
980#endif
981        CC_UNLOCK(cc);
982}
983#endif /* __rtems__ */
984
985/*
986 * timeout --
987 *      Execute a function after a specified length of time.
988 *
989 * untimeout --
990 *      Cancel previous timeout function call.
991 *
992 * callout_handle_init --
993 *      Initialize a handle so that using it with untimeout is benign.
994 *
995 *      See AT&T BCI Driver Reference Manual for specification.  This
996 *      implementation differs from that one in that although an
997 *      identification value is returned from timeout, the original
998 *      arguments to timeout as well as the identifier are used to
999 *      identify entries for untimeout.
1000 */
1001struct callout_handle
1002timeout(timeout_t *ftn, void *arg, int to_ticks)
1003{
1004        struct callout_cpu *cc;
1005        struct callout *new;
1006        struct callout_handle handle;
1007
1008        cc = CC_CPU(timeout_cpu);
1009        CC_LOCK(cc);
1010        /* Fill in the next free callout structure. */
1011        new = SLIST_FIRST(&cc->cc_callfree);
1012        if (new == NULL)
1013                /* XXX Attempt to malloc first */
1014                panic("timeout table full");
1015        SLIST_REMOVE_HEAD(&cc->cc_callfree, c_links.sle);
1016        callout_reset(new, to_ticks, ftn, arg);
1017        handle.callout = new;
1018        CC_UNLOCK(cc);
1019
1020        return (handle);
1021}
1022
1023void
1024untimeout(timeout_t *ftn, void *arg, struct callout_handle handle)
1025{
1026        struct callout_cpu *cc;
1027
1028        /*
1029         * Check for a handle that was initialized
1030         * by callout_handle_init, but never used
1031         * for a real timeout.
1032         */
1033        if (handle.callout == NULL)
1034                return;
1035
1036        cc = callout_lock(handle.callout);
1037        if (handle.callout->c_func == ftn && handle.callout->c_arg == arg)
1038                callout_stop(handle.callout);
1039        CC_UNLOCK(cc);
1040}
1041
1042void
1043callout_handle_init(struct callout_handle *handle)
1044{
1045        handle->callout = NULL;
1046}
1047
1048void
1049callout_when(sbintime_t sbt, sbintime_t precision, int flags,
1050    sbintime_t *res, sbintime_t *prec_res)
1051{
1052        sbintime_t to_sbt, to_pr;
1053
1054        if ((flags & (C_ABSOLUTE | C_PRECALC)) != 0) {
1055                *res = sbt;
1056                *prec_res = precision;
1057                return;
1058        }
1059        if ((flags & C_HARDCLOCK) != 0 && sbt < tick_sbt)
1060                sbt = tick_sbt;
1061        if ((flags & C_HARDCLOCK) != 0 ||
1062#ifdef NO_EVENTTIMERS
1063            sbt >= sbt_timethreshold) {
1064                to_sbt = getsbinuptime();
1065
1066                /* Add safety belt for the case of hz > 1000. */
1067                to_sbt += tc_tick_sbt - tick_sbt;
1068#else
1069            sbt >= sbt_tickthreshold) {
1070                /*
1071                 * Obtain the time of the last hardclock() call on
1072                 * this CPU directly from the kern_clocksource.c.
1073                 * This value is per-CPU, but it is equal for all
1074                 * active ones.
1075                 */
1076#ifdef __LP64__
1077                to_sbt = DPCPU_GET(hardclocktime);
1078#else
1079                spinlock_enter();
1080                to_sbt = DPCPU_GET(hardclocktime);
1081                spinlock_exit();
1082#endif
1083#endif
1084                if (cold && to_sbt == 0)
1085                        to_sbt = sbinuptime();
1086                if ((flags & C_HARDCLOCK) == 0)
1087                        to_sbt += tick_sbt;
1088        } else
1089                to_sbt = sbinuptime();
1090        if (SBT_MAX - to_sbt < sbt)
1091                to_sbt = SBT_MAX;
1092        else
1093                to_sbt += sbt;
1094        *res = to_sbt;
1095        to_pr = ((C_PRELGET(flags) < 0) ? sbt >> tc_precexp :
1096            sbt >> C_PRELGET(flags));
1097        *prec_res = to_pr > precision ? to_pr : precision;
1098}
1099
1100/*
1101 * New interface; clients allocate their own callout structures.
1102 *
1103 * callout_reset() - establish or change a timeout
1104 * callout_stop() - disestablish a timeout
1105 * callout_init() - initialize a callout structure so that it can
1106 *      safely be passed to callout_reset() and callout_stop()
1107 *
1108 * <sys/callout.h> defines three convenience macros:
1109 *
1110 * callout_active() - returns truth if callout has not been stopped,
1111 *      drained, or deactivated since the last time the callout was
1112 *      reset.
1113 * callout_pending() - returns truth if callout is still waiting for timeout
1114 * callout_deactivate() - marks the callout as having been serviced
1115 */
1116int
1117callout_reset_sbt_on(struct callout *c, sbintime_t sbt, sbintime_t prec,
1118    void (*ftn)(void *), void *arg, int cpu, int flags)
1119{
1120        sbintime_t to_sbt, precision;
1121        struct callout_cpu *cc;
1122#ifndef __rtems__
1123        int cancelled, direct;
1124#else /* __rtems__ */
1125        int cancelled;
1126#endif /* __rtems__ */
1127        int ignore_cpu=0;
1128
1129        cancelled = 0;
1130        if (cpu == -1) {
1131                ignore_cpu = 1;
1132        } else if ((cpu >= MAXCPU) ||
1133                   ((CC_CPU(cpu))->cc_inited == 0)) {
1134                /* Invalid CPU spec */
1135                panic("Invalid CPU in callout %d", cpu);
1136        }
1137        callout_when(sbt, prec, flags, &to_sbt, &precision);
1138
1139#ifndef __rtems__
1140        /*
1141         * This flag used to be added by callout_cc_add, but the
1142         * first time you call this we could end up with the
1143         * wrong direct flag if we don't do it before we add.
1144         */
1145        if (flags & C_DIRECT_EXEC) {
1146                direct = 1;
1147        } else {
1148                direct = 0;
1149        }
1150        KASSERT(!direct || c->c_lock == NULL,
1151            ("%s: direct callout %p has lock", __func__, c));
1152#endif /* __rtems__ */
1153        cc = callout_lock(c);
1154        /*
1155         * Don't allow migration of pre-allocated callouts lest they
1156         * become unbalanced or handle the case where the user does
1157         * not care.
1158         */
1159        if ((c->c_iflags & CALLOUT_LOCAL_ALLOC) ||
1160            ignore_cpu) {
1161                cpu = c->c_cpu;
1162        }
1163
1164        if (cc_exec_curr(cc, direct) == c) {
1165                /*
1166                 * We're being asked to reschedule a callout which is
1167                 * currently in progress.  If there is a lock then we
1168                 * can cancel the callout if it has not really started.
1169                 */
1170                if (c->c_lock != NULL && !cc_exec_cancel(cc, direct))
1171                        cancelled = cc_exec_cancel(cc, direct) = true;
1172                if (cc_exec_waiting(cc, direct) || cc_exec_drain(cc, direct)) {
1173                        /*
1174                         * Someone has called callout_drain to kill this
1175                         * callout.  Don't reschedule.
1176                         */
1177                        CTR4(KTR_CALLOUT, "%s %p func %p arg %p",
1178                            cancelled ? "cancelled" : "failed to cancel",
1179                            c, c->c_func, c->c_arg);
1180                        CC_UNLOCK(cc);
1181                        return (cancelled);
1182                }
1183#ifdef SMP
1184                if (callout_migrating(c)) {
1185                        /*
1186                         * This only occurs when a second callout_reset_sbt_on
1187                         * is made after a previous one moved it into
1188                         * deferred migration (below). Note we do *not* change
1189                         * the prev_cpu even though the previous target may
1190                         * be different.
1191                         */
1192                        cc_migration_cpu(cc, direct) = cpu;
1193                        cc_migration_time(cc, direct) = to_sbt;
1194                        cc_migration_prec(cc, direct) = precision;
1195                        cc_migration_func(cc, direct) = ftn;
1196                        cc_migration_arg(cc, direct) = arg;
1197                        cancelled = 1;
1198                        CC_UNLOCK(cc);
1199                        return (cancelled);
1200                }
1201#endif
1202        }
1203        if (c->c_iflags & CALLOUT_PENDING) {
1204#ifndef __rtems__
1205                if ((c->c_iflags & CALLOUT_PROCESSED) == 0) {
1206#endif /* __rtems__ */
1207                        if (cc_exec_next(cc) == c)
1208                                cc_exec_next(cc) = LIST_NEXT(c, c_links.le);
1209                        LIST_REMOVE(c, c_links.le);
1210#ifndef __rtems__
1211                } else {
1212                        TAILQ_REMOVE(&cc->cc_expireq, c, c_links.tqe);
1213                }
1214#endif /* __rtems__ */
1215                cancelled = 1;
1216                c->c_iflags &= ~ CALLOUT_PENDING;
1217                c->c_flags &= ~ CALLOUT_ACTIVE;
1218        }
1219
1220#ifdef SMP
1221        /*
1222         * If the callout must migrate try to perform it immediately.
1223         * If the callout is currently running, just defer the migration
1224         * to a more appropriate moment.
1225         */
1226        if (c->c_cpu != cpu) {
1227                if (cc_exec_curr(cc, direct) == c) {
1228                        /*
1229                         * Pending will have been removed since we are
1230                         * actually executing the callout on another
1231                         * CPU. That callout should be waiting on the
1232                         * lock the caller holds. If we set both
1233                         * active/and/pending after we return and the
1234                         * lock on the executing callout proceeds, it
1235                         * will then see pending is true and return.
1236                         * At the return from the actual callout execution
1237                         * the migration will occur in softclock_call_cc
1238                         * and this new callout will be placed on the
1239                         * new CPU via a call to callout_cpu_switch() which
1240                         * will get the lock on the right CPU followed
1241                         * by a call callout_cc_add() which will add it there.
1242                         * (see above in softclock_call_cc()).
1243                         */
1244                        cc_migration_cpu(cc, direct) = cpu;
1245                        cc_migration_time(cc, direct) = to_sbt;
1246                        cc_migration_prec(cc, direct) = precision;
1247                        cc_migration_func(cc, direct) = ftn;
1248                        cc_migration_arg(cc, direct) = arg;
1249                        c->c_iflags |= (CALLOUT_DFRMIGRATION | CALLOUT_PENDING);
1250                        c->c_flags |= CALLOUT_ACTIVE;
1251                        CTR6(KTR_CALLOUT,
1252                    "migration of %p func %p arg %p in %d.%08x to %u deferred",
1253                            c, c->c_func, c->c_arg, (int)(to_sbt >> 32),
1254                            (u_int)(to_sbt & 0xffffffff), cpu);
1255                        CC_UNLOCK(cc);
1256                        return (cancelled);
1257                }
1258                cc = callout_cpu_switch(c, cc, cpu);
1259        }
1260#endif
1261
1262        callout_cc_add(c, cc, to_sbt, precision, ftn, arg, cpu, flags);
1263        CTR6(KTR_CALLOUT, "%sscheduled %p func %p arg %p in %d.%08x",
1264            cancelled ? "re" : "", c, c->c_func, c->c_arg, (int)(to_sbt >> 32),
1265            (u_int)(to_sbt & 0xffffffff));
1266        CC_UNLOCK(cc);
1267
1268        return (cancelled);
1269}
1270
1271/*
1272 * Common idioms that can be optimized in the future.
1273 */
1274int
1275callout_schedule_on(struct callout *c, int to_ticks, int cpu)
1276{
1277        return callout_reset_on(c, to_ticks, c->c_func, c->c_arg, cpu);
1278}
1279
1280int
1281callout_schedule(struct callout *c, int to_ticks)
1282{
1283        return callout_reset_on(c, to_ticks, c->c_func, c->c_arg, c->c_cpu);
1284}
1285
1286int
1287_callout_stop_safe(struct callout *c, int flags, void (*drain)(void *))
1288{
1289        struct callout_cpu *cc, *old_cc;
1290        struct lock_class *class;
1291#ifndef __rtems__
1292        int direct, sq_locked, use_lock;
1293#else /* __rtems__ */
1294        int sq_locked, use_lock;
1295#endif /* __rtems__ */
1296        int cancelled, not_on_a_list;
1297#ifdef __rtems__
1298        (void)old_cc;
1299        (void)sq_locked;
1300#endif /* __rtems__ */
1301
1302        if ((flags & CS_DRAIN) != 0)
1303                WITNESS_WARN(WARN_GIANTOK | WARN_SLEEPOK, c->c_lock,
1304                    "calling %s", __func__);
1305
1306        /*
1307         * Some old subsystems don't hold Giant while running a callout_stop(),
1308         * so just discard this check for the moment.
1309         */
1310        if ((flags & CS_DRAIN) == 0 && c->c_lock != NULL) {
1311                if (c->c_lock == &Giant.lock_object)
1312                        use_lock = mtx_owned(&Giant);
1313                else {
1314                        use_lock = 1;
1315                        class = LOCK_CLASS(c->c_lock);
1316                        class->lc_assert(c->c_lock, LA_XLOCKED);
1317                }
1318        } else
1319                use_lock = 0;
1320#ifndef __rtems__
1321        if (c->c_iflags & CALLOUT_DIRECT) {
1322                direct = 1;
1323        } else {
1324                direct = 0;
1325        }
1326
1327        sq_locked = 0;
1328        old_cc = NULL;
1329again:
1330#endif /* __rtems__ */
1331        cc = callout_lock(c);
1332
1333        if ((c->c_iflags & (CALLOUT_DFRMIGRATION | CALLOUT_PENDING)) ==
1334            (CALLOUT_DFRMIGRATION | CALLOUT_PENDING) &&
1335            ((c->c_flags & CALLOUT_ACTIVE) == CALLOUT_ACTIVE)) {
1336                /*
1337                 * Special case where this slipped in while we
1338                 * were migrating *as* the callout is about to
1339                 * execute. The caller probably holds the lock
1340                 * the callout wants.
1341                 *
1342                 * Get rid of the migration first. Then set
1343                 * the flag that tells this code *not* to
1344                 * try to remove it from any lists (its not
1345                 * on one yet). When the callout wheel runs,
1346                 * it will ignore this callout.
1347                 */
1348                c->c_iflags &= ~CALLOUT_PENDING;
1349                c->c_flags &= ~CALLOUT_ACTIVE;
1350                not_on_a_list = 1;
1351        } else {
1352                not_on_a_list = 0;
1353        }
1354
1355#ifndef __rtems__
1356        /*
1357         * If the callout was migrating while the callout cpu lock was
1358         * dropped,  just drop the sleepqueue lock and check the states
1359         * again.
1360         */
1361        if (sq_locked != 0 && cc != old_cc) {
1362#ifdef SMP
1363                CC_UNLOCK(cc);
1364                sleepq_release(&cc_exec_waiting(old_cc, direct));
1365                sq_locked = 0;
1366                old_cc = NULL;
1367                goto again;
1368#else
1369                panic("migration should not happen");
1370#endif
1371        }
1372#endif /* __rtems__ */
1373
1374        /*
1375         * If the callout is running, try to stop it or drain it.
1376         */
1377        if (cc_exec_curr(cc, direct) == c) {
1378                /*
1379                 * Succeed we to stop it or not, we must clear the
1380                 * active flag - this is what API users expect.  If we're
1381                 * draining and the callout is currently executing, first wait
1382                 * until it finishes.
1383                 */
1384                if ((flags & CS_DRAIN) == 0)
1385                        c->c_flags &= ~CALLOUT_ACTIVE;
1386
1387                if ((flags & CS_DRAIN) != 0) {
1388                        /*
1389                         * The current callout is running (or just
1390                         * about to run) and blocking is allowed, so
1391                         * just wait for the current invocation to
1392                         * finish.
1393                         */
1394                        while (cc_exec_curr(cc, direct) == c) {
1395#ifndef __rtems__
1396
1397                                /*
1398                                 * Use direct calls to sleepqueue interface
1399                                 * instead of cv/msleep in order to avoid
1400                                 * a LOR between cc_lock and sleepqueue
1401                                 * chain spinlocks.  This piece of code
1402                                 * emulates a msleep_spin() call actually.
1403                                 *
1404                                 * If we already have the sleepqueue chain
1405                                 * locked, then we can safely block.  If we
1406                                 * don't already have it locked, however,
1407                                 * we have to drop the cc_lock to lock
1408                                 * it.  This opens several races, so we
1409                                 * restart at the beginning once we have
1410                                 * both locks.  If nothing has changed, then
1411                                 * we will end up back here with sq_locked
1412                                 * set.
1413                                 */
1414                                if (!sq_locked) {
1415                                        CC_UNLOCK(cc);
1416                                        sleepq_lock(
1417                                            &cc_exec_waiting(cc, direct));
1418                                        sq_locked = 1;
1419                                        old_cc = cc;
1420                                        goto again;
1421                                }
1422
1423                                /*
1424                                 * Migration could be cancelled here, but
1425                                 * as long as it is still not sure when it
1426                                 * will be packed up, just let softclock()
1427                                 * take care of it.
1428                                 */
1429                                cc_exec_waiting(cc, direct) = true;
1430                                DROP_GIANT();
1431                                CC_UNLOCK(cc);
1432                                sleepq_add(
1433                                    &cc_exec_waiting(cc, direct),
1434                                    &cc->cc_lock.lock_object, "codrain",
1435                                    SLEEPQ_SLEEP, 0);
1436                                sleepq_wait(
1437                                    &cc_exec_waiting(cc, direct),
1438                                             0);
1439                                sq_locked = 0;
1440                                old_cc = NULL;
1441
1442                                /* Reacquire locks previously released. */
1443                                PICKUP_GIANT();
1444                                CC_LOCK(cc);
1445#else /* __rtems__ */
1446                                /*
1447                                 * On RTEMS the LOR problem above does not
1448                                 * exist since here we do not use
1449                                 * sleepq_set_timeout() and instead use the
1450                                 * RTEMS watchdog.
1451                                 */
1452                                cc_exec_waiting(cc, direct) = true;
1453                                msleep_spin(&cc_exec_waiting(cc, direct),
1454                                    &cc->cc_lock, "codrain", 0);
1455#endif /* __rtems__ */
1456                        }
1457                        c->c_flags &= ~CALLOUT_ACTIVE;
1458                } else if (use_lock &&
1459                           !cc_exec_cancel(cc, direct) && (drain == NULL)) {
1460                       
1461                        /*
1462                         * The current callout is waiting for its
1463                         * lock which we hold.  Cancel the callout
1464                         * and return.  After our caller drops the
1465                         * lock, the callout will be skipped in
1466                         * softclock(). This *only* works with a
1467                         * callout_stop() *not* callout_drain() or
1468                         * callout_async_drain().
1469                         */
1470                        cc_exec_cancel(cc, direct) = true;
1471                        CTR3(KTR_CALLOUT, "cancelled %p func %p arg %p",
1472                            c, c->c_func, c->c_arg);
1473                        KASSERT(!cc_cce_migrating(cc, direct),
1474                            ("callout wrongly scheduled for migration"));
1475                        if (callout_migrating(c)) {
1476                                c->c_iflags &= ~CALLOUT_DFRMIGRATION;
1477#ifdef SMP
1478                                cc_migration_cpu(cc, direct) = CPUBLOCK;
1479                                cc_migration_time(cc, direct) = 0;
1480                                cc_migration_prec(cc, direct) = 0;
1481                                cc_migration_func(cc, direct) = NULL;
1482                                cc_migration_arg(cc, direct) = NULL;
1483#endif
1484                        }
1485                        CC_UNLOCK(cc);
1486#ifndef __rtems__
1487                        KASSERT(!sq_locked, ("sleepqueue chain locked"));
1488#endif /* __rtems__ */
1489                        return (1);
1490                } else if (callout_migrating(c)) {
1491                        /*
1492                         * The callout is currently being serviced
1493                         * and the "next" callout is scheduled at
1494                         * its completion with a migration. We remove
1495                         * the migration flag so it *won't* get rescheduled,
1496                         * but we can't stop the one thats running so
1497                         * we return 0.
1498                         */
1499                        c->c_iflags &= ~CALLOUT_DFRMIGRATION;
1500#ifdef SMP
1501                        /*
1502                         * We can't call cc_cce_cleanup here since
1503                         * if we do it will remove .ce_curr and
1504                         * its still running. This will prevent a
1505                         * reschedule of the callout when the
1506                         * execution completes.
1507                         */
1508                        cc_migration_cpu(cc, direct) = CPUBLOCK;
1509                        cc_migration_time(cc, direct) = 0;
1510                        cc_migration_prec(cc, direct) = 0;
1511                        cc_migration_func(cc, direct) = NULL;
1512                        cc_migration_arg(cc, direct) = NULL;
1513#endif
1514                        CTR3(KTR_CALLOUT, "postponing stop %p func %p arg %p",
1515                            c, c->c_func, c->c_arg);
1516                        if (drain) {
1517                                cc_exec_drain(cc, direct) = drain;
1518                        }
1519                        CC_UNLOCK(cc);
1520                        return ((flags & CS_EXECUTING) != 0);
1521                }
1522                CTR3(KTR_CALLOUT, "failed to stop %p func %p arg %p",
1523                    c, c->c_func, c->c_arg);
1524                if (drain) {
1525                        cc_exec_drain(cc, direct) = drain;
1526                }
1527#ifndef __rtems__
1528                KASSERT(!sq_locked, ("sleepqueue chain still locked"));
1529#endif /* __rtems__ */
1530                cancelled = ((flags & CS_EXECUTING) != 0);
1531        } else
1532                cancelled = 1;
1533
1534#ifndef __rtems__
1535        if (sq_locked)
1536                sleepq_release(&cc_exec_waiting(cc, direct));
1537#endif /* __rtems__ */
1538
1539        if ((c->c_iflags & CALLOUT_PENDING) == 0) {
1540                CTR3(KTR_CALLOUT, "failed to stop %p func %p arg %p",
1541                    c, c->c_func, c->c_arg);
1542                /*
1543                 * For not scheduled and not executing callout return
1544                 * negative value.
1545                 */
1546                if (cc_exec_curr(cc, direct) != c)
1547                        cancelled = -1;
1548                CC_UNLOCK(cc);
1549                return (cancelled);
1550        }
1551
1552        c->c_iflags &= ~CALLOUT_PENDING;
1553        c->c_flags &= ~CALLOUT_ACTIVE;
1554
1555        CTR3(KTR_CALLOUT, "cancelled %p func %p arg %p",
1556            c, c->c_func, c->c_arg);
1557        if (not_on_a_list == 0) {
1558#ifndef __rtems__
1559                if ((c->c_iflags & CALLOUT_PROCESSED) == 0) {
1560#endif /* __rtems__ */
1561                        if (cc_exec_next(cc) == c)
1562                                cc_exec_next(cc) = LIST_NEXT(c, c_links.le);
1563                        LIST_REMOVE(c, c_links.le);
1564#ifndef __rtems__
1565                } else {
1566                        TAILQ_REMOVE(&cc->cc_expireq, c, c_links.tqe);
1567                }
1568#endif /* __rtems__ */
1569        }
1570        callout_cc_del(c, cc);
1571        CC_UNLOCK(cc);
1572        return (cancelled);
1573}
1574
1575void
1576callout_init(struct callout *c, int mpsafe)
1577{
1578        bzero(c, sizeof *c);
1579        if (mpsafe) {
1580                c->c_lock = NULL;
1581                c->c_iflags = CALLOUT_RETURNUNLOCKED;
1582        } else {
1583                c->c_lock = &Giant.lock_object;
1584                c->c_iflags = 0;
1585        }
1586        c->c_cpu = timeout_cpu;
1587}
1588
1589void
1590_callout_init_lock(struct callout *c, struct lock_object *lock, int flags)
1591{
1592        bzero(c, sizeof *c);
1593        c->c_lock = lock;
1594        KASSERT((flags & ~(CALLOUT_RETURNUNLOCKED | CALLOUT_SHAREDLOCK)) == 0,
1595            ("callout_init_lock: bad flags %d", flags));
1596        KASSERT(lock != NULL || (flags & CALLOUT_RETURNUNLOCKED) == 0,
1597            ("callout_init_lock: CALLOUT_RETURNUNLOCKED with no lock"));
1598        KASSERT(lock == NULL || !(LOCK_CLASS(lock)->lc_flags &
1599            (LC_SPINLOCK | LC_SLEEPABLE)), ("%s: invalid lock class",
1600            __func__));
1601        c->c_iflags = flags & (CALLOUT_RETURNUNLOCKED | CALLOUT_SHAREDLOCK);
1602        c->c_cpu = timeout_cpu;
1603}
1604
1605#ifdef APM_FIXUP_CALLTODO
1606/*
1607 * Adjust the kernel calltodo timeout list.  This routine is used after
1608 * an APM resume to recalculate the calltodo timer list values with the
1609 * number of hz's we have been sleeping.  The next hardclock() will detect
1610 * that there are fired timers and run softclock() to execute them.
1611 *
1612 * Please note, I have not done an exhaustive analysis of what code this
1613 * might break.  I am motivated to have my select()'s and alarm()'s that
1614 * have expired during suspend firing upon resume so that the applications
1615 * which set the timer can do the maintanence the timer was for as close
1616 * as possible to the originally intended time.  Testing this code for a
1617 * week showed that resuming from a suspend resulted in 22 to 25 timers
1618 * firing, which seemed independent on whether the suspend was 2 hours or
1619 * 2 days.  Your milage may vary.   - Ken Key <key@cs.utk.edu>
1620 */
1621void
1622adjust_timeout_calltodo(struct timeval *time_change)
1623{
1624        register struct callout *p;
1625        unsigned long delta_ticks;
1626
1627        /*
1628         * How many ticks were we asleep?
1629         * (stolen from tvtohz()).
1630         */
1631
1632        /* Don't do anything */
1633        if (time_change->tv_sec < 0)
1634                return;
1635        else if (time_change->tv_sec <= LONG_MAX / 1000000)
1636                delta_ticks = howmany(time_change->tv_sec * 1000000 +
1637                    time_change->tv_usec, tick) + 1;
1638        else if (time_change->tv_sec <= LONG_MAX / hz)
1639                delta_ticks = time_change->tv_sec * hz +
1640                    howmany(time_change->tv_usec, tick) + 1;
1641        else
1642                delta_ticks = LONG_MAX;
1643
1644        if (delta_ticks > INT_MAX)
1645                delta_ticks = INT_MAX;
1646
1647        /*
1648         * Now rip through the timer calltodo list looking for timers
1649         * to expire.
1650         */
1651
1652        /* don't collide with softclock() */
1653        CC_LOCK(cc);
1654        for (p = calltodo.c_next; p != NULL; p = p->c_next) {
1655                p->c_time -= delta_ticks;
1656
1657                /* Break if the timer had more time on it than delta_ticks */
1658                if (p->c_time > 0)
1659                        break;
1660
1661                /* take back the ticks the timer didn't use (p->c_time <= 0) */
1662                delta_ticks = -p->c_time;
1663        }
1664        CC_UNLOCK(cc);
1665
1666        return;
1667}
1668#endif /* APM_FIXUP_CALLTODO */
1669
1670static int
1671flssbt(sbintime_t sbt)
1672{
1673
1674        sbt += (uint64_t)sbt >> 1;
1675        if (sizeof(long) >= sizeof(sbintime_t))
1676                return (flsl(sbt));
1677        if (sbt >= SBT_1S)
1678                return (flsl(((uint64_t)sbt) >> 32) + 32);
1679        return (flsl(sbt));
1680}
1681
1682/*
1683 * Dump immediate statistic snapshot of the scheduled callouts.
1684 */
1685static int
1686sysctl_kern_callout_stat(SYSCTL_HANDLER_ARGS)
1687{
1688        struct callout *tmp;
1689        struct callout_cpu *cc;
1690        struct callout_list *sc;
1691        sbintime_t maxpr, maxt, medpr, medt, now, spr, st, t;
1692        int ct[64], cpr[64], ccpbk[32];
1693        int error, val, i, count, tcum, pcum, maxc, c, medc;
1694#ifdef SMP
1695        int cpu;
1696#endif
1697
1698        val = 0;
1699        error = sysctl_handle_int(oidp, &val, 0, req);
1700        if (error != 0 || req->newptr == NULL)
1701                return (error);
1702        count = maxc = 0;
1703        st = spr = maxt = maxpr = 0;
1704        bzero(ccpbk, sizeof(ccpbk));
1705        bzero(ct, sizeof(ct));
1706        bzero(cpr, sizeof(cpr));
1707        now = sbinuptime();
1708#ifdef SMP
1709        CPU_FOREACH(cpu) {
1710                cc = CC_CPU(cpu);
1711#else
1712                cc = CC_CPU(timeout_cpu);
1713#endif
1714                CC_LOCK(cc);
1715                for (i = 0; i < callwheelsize; i++) {
1716                        sc = &cc->cc_callwheel[i];
1717                        c = 0;
1718                        LIST_FOREACH(tmp, sc, c_links.le) {
1719                                c++;
1720                                t = tmp->c_time - now;
1721                                if (t < 0)
1722                                        t = 0;
1723                                st += t / SBT_1US;
1724                                spr += tmp->c_precision / SBT_1US;
1725                                if (t > maxt)
1726                                        maxt = t;
1727                                if (tmp->c_precision > maxpr)
1728                                        maxpr = tmp->c_precision;
1729                                ct[flssbt(t)]++;
1730                                cpr[flssbt(tmp->c_precision)]++;
1731                        }
1732                        if (c > maxc)
1733                                maxc = c;
1734                        ccpbk[fls(c + c / 2)]++;
1735                        count += c;
1736                }
1737                CC_UNLOCK(cc);
1738#ifdef SMP
1739        }
1740#endif
1741
1742        for (i = 0, tcum = 0; i < 64 && tcum < count / 2; i++)
1743                tcum += ct[i];
1744        medt = (i >= 2) ? (((sbintime_t)1) << (i - 2)) : 0;
1745        for (i = 0, pcum = 0; i < 64 && pcum < count / 2; i++)
1746                pcum += cpr[i];
1747        medpr = (i >= 2) ? (((sbintime_t)1) << (i - 2)) : 0;
1748        for (i = 0, c = 0; i < 32 && c < count / 2; i++)
1749                c += ccpbk[i];
1750        medc = (i >= 2) ? (1 << (i - 2)) : 0;
1751
1752        printf("Scheduled callouts statistic snapshot:\n");
1753        printf("  Callouts: %6d  Buckets: %6d*%-3d  Bucket size: 0.%06ds\n",
1754            count, callwheelsize, mp_ncpus, 1000000 >> CC_HASH_SHIFT);
1755        printf("  C/Bk: med %5d         avg %6d.%06jd  max %6d\n",
1756            medc,
1757            count / callwheelsize / mp_ncpus,
1758            (uint64_t)count * 1000000 / callwheelsize / mp_ncpus % 1000000,
1759            maxc);
1760        printf("  Time: med %5jd.%06jds avg %6jd.%06jds max %6jd.%06jds\n",
1761            medt / SBT_1S, (medt & 0xffffffff) * 1000000 >> 32,
1762            (st / count) / 1000000, (st / count) % 1000000,
1763            maxt / SBT_1S, (maxt & 0xffffffff) * 1000000 >> 32);
1764        printf("  Prec: med %5jd.%06jds avg %6jd.%06jds max %6jd.%06jds\n",
1765            medpr / SBT_1S, (medpr & 0xffffffff) * 1000000 >> 32,
1766            (spr / count) / 1000000, (spr / count) % 1000000,
1767            maxpr / SBT_1S, (maxpr & 0xffffffff) * 1000000 >> 32);
1768        printf("  Distribution:       \tbuckets\t   time\t   tcum\t"
1769            "   prec\t   pcum\n");
1770        for (i = 0, tcum = pcum = 0; i < 64; i++) {
1771                if (ct[i] == 0 && cpr[i] == 0)
1772                        continue;
1773                t = (i != 0) ? (((sbintime_t)1) << (i - 1)) : 0;
1774                tcum += ct[i];
1775                pcum += cpr[i];
1776                printf("  %10jd.%06jds\t 2**%d\t%7d\t%7d\t%7d\t%7d\n",
1777                    t / SBT_1S, (t & 0xffffffff) * 1000000 >> 32,
1778                    i - 1 - (32 - CC_HASH_SHIFT),
1779                    ct[i], tcum, cpr[i], pcum);
1780        }
1781        return (error);
1782}
1783SYSCTL_PROC(_kern, OID_AUTO, callout_stat,
1784    CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE,
1785    0, 0, sysctl_kern_callout_stat, "I",
1786    "Dump immediate statistic snapshot of the scheduled callouts");
1787
1788#ifdef DDB
1789static void
1790_show_callout(struct callout *c)
1791{
1792
1793        db_printf("callout %p\n", c);
1794#define C_DB_PRINTF(f, e)       db_printf("   %s = " f "\n", #e, c->e);
1795        db_printf("   &c_links = %p\n", &(c->c_links));
1796        C_DB_PRINTF("%" PRId64, c_time);
1797        C_DB_PRINTF("%" PRId64, c_precision);
1798        C_DB_PRINTF("%p",       c_arg);
1799        C_DB_PRINTF("%p",       c_func);
1800        C_DB_PRINTF("%p",       c_lock);
1801        C_DB_PRINTF("%#x",      c_flags);
1802        C_DB_PRINTF("%#x",      c_iflags);
1803        C_DB_PRINTF("%d",       c_cpu);
1804#undef  C_DB_PRINTF
1805}
1806
1807DB_SHOW_COMMAND(callout, db_show_callout)
1808{
1809
1810        if (!have_addr) {
1811                db_printf("usage: show callout <struct callout *>\n");
1812                return;
1813        }
1814
1815        _show_callout((struct callout *)addr);
1816}
1817#endif /* DDB */
Note: See TracBrowser for help on using the repository browser.