kern_idle.c revision 67297
1/*- 2 * Copyright (c) 2000, All rights reserved. See /usr/src/COPYRIGHT 3 * 4 * $FreeBSD: head/sys/kern/kern_idle.c 67297 2000-10-18 17:56:06Z peter $ 5 */ 6 7#include "opt_ktrace.h" 8 9#include <sys/param.h> 10#include <sys/systm.h> 11#include <sys/proc.h> 12#include <sys/kernel.h> 13#include <sys/ktr.h> 14#include <sys/signalvar.h> 15#include <sys/resourcevar.h> 16#include <sys/vmmeter.h> 17#include <sys/sysctl.h> 18#include <sys/unistd.h> 19#include <sys/ipl.h> 20#include <sys/kthread.h> 21#include <sys/queue.h> 22#include <sys/eventhandler.h> 23#include <vm/vm.h> 24#include <vm/vm_extern.h> 25#ifdef KTRACE 26#include <sys/uio.h> 27#include <sys/ktrace.h> 28#endif 29 30#include <machine/cpu.h> 31#include <machine/md_var.h> 32#include <machine/mutex.h> 33#include <machine/smp.h> 34 35#include <machine/globaldata.h> 36#include <machine/globals.h> 37 38static void idle_setup(void *dummy); 39SYSINIT(idle_setup, SI_SUB_SCHED_IDLE, SI_ORDER_FIRST, idle_setup, NULL) 40 41static void idle_proc(void *dummy); 42 43EVENTHANDLER_FAST_DEFINE(idle_event, idle_eventhandler_t); 44 45/* 46 * setup per-cpu idle process contexts 47 */ 48static void 49idle_setup(void *dummy) 50{ 51 struct globaldata *gd; 52 int error; 53 54 SLIST_FOREACH(gd, &cpuhead, gd_allcpu) { 55#ifdef SMP 56 error = kthread_create(idle_proc, NULL, &gd->gd_idleproc, 57 RFSTOPPED|RFHIGHPID, "idle: cpu%d", 58 gd->gd_cpuid); 59#else 60 error = kthread_create(idle_proc, NULL, &gd->gd_idleproc, 61 RFSTOPPED|RFHIGHPID, "idle"); 62#endif 63 if (error) 64 panic("idle_setup: kthread_create error %d\n", error); 65 66 gd->gd_idleproc->p_flag |= P_NOLOAD; 67 gd->gd_idleproc->p_stat = SRUN; 68 } 69} 70 71/* 72 * idle process context 73 */ 74static void 75idle_proc(void *dummy) 76{ 77#ifdef DIAGNOSTIC 78 int count; 79#endif 80 81 for (;;) { 82 mtx_assert(&Giant, MA_NOTOWNED); 83 84#ifdef DIAGNOSTIC 85 count = 0; 86 87 while (count >= 0 && procrunnable() == 0) { 88#else 89 while (procrunnable() == 0) { 90#endif 91 /* 92 * This is a good place to put things to be done in 93 * the background, including sanity checks. 94 */ 95 96#ifdef DIAGNOSTIC 97 if (count++ < 0) 98 CTR0(KTR_PROC, "idle_proc: timed out waiting" 99 " for a process"); 100#endif 101 102 if (vm_page_zero_idle() != 0) 103 continue; 104 105 /* call out to any cpu-becoming-idle events */ 106 EVENTHANDLER_FAST_INVOKE(idle_event, 0); 107 } 108 109 mtx_enter(&sched_lock, MTX_SPIN); 110 mi_switch(); 111 mtx_exit(&sched_lock, MTX_SPIN); 112 } 113} 114