kern_idle.c revision 76440
1/*- 2 * Copyright (c) 2000, All rights reserved. See /usr/src/COPYRIGHT 3 * 4 * $FreeBSD: head/sys/kern/kern_idle.c 76440 2001-05-10 17:45:49Z jhb $ 5 */ 6 7#include "opt_ktrace.h" 8 9#include <sys/param.h> 10#include <sys/systm.h> 11#include <sys/kernel.h> 12#include <sys/ktr.h> 13#include <sys/kthread.h> 14#include <sys/lock.h> 15#include <sys/mutex.h> 16#include <sys/pcpu.h> 17#include <sys/proc.h> 18#include <sys/resourcevar.h> 19#include <sys/smp.h> 20#include <sys/unistd.h> 21#if 0 22#include <vm/vm.h> 23#include <vm/vm_extern.h> 24#endif 25#ifdef KTRACE 26#include <sys/uio.h> 27#include <sys/ktrace.h> 28#endif 29 30static void idle_setup(void *dummy); 31SYSINIT(idle_setup, SI_SUB_SCHED_IDLE, SI_ORDER_FIRST, idle_setup, NULL) 32 33static void idle_proc(void *dummy); 34 35/* 36 * Setup per-cpu idle process contexts. The AP's shouldn't be running or 37 * accessing their idle processes at this point, so don't bother with 38 * locking. 39 */ 40static void 41idle_setup(void *dummy) 42{ 43#ifdef SMP 44 struct globaldata *gd; 45#endif 46 struct proc *p; 47 int error; 48 49#ifdef SMP 50 SLIST_FOREACH(gd, &cpuhead, gd_allcpu) { 51 error = kthread_create(idle_proc, NULL, &p, 52 RFSTOPPED | RFHIGHPID, "idle: cpu%d", gd->gd_cpuid); 53 gd->gd_idleproc = p; 54 if (gd->gd_curproc == NULL) 55 gd->gd_curproc = p; 56#else 57 error = kthread_create(idle_proc, NULL, &p, 58 RFSTOPPED | RFHIGHPID, "idle"); 59 PCPU_SET(idleproc, p); 60#endif 61 if (error) 62 panic("idle_setup: kthread_create error %d\n", error); 63 64 p->p_flag |= P_NOLOAD; 65 p->p_stat = SRUN; 66#ifdef SMP 67 } 68#endif 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 0 103 if (vm_page_zero_idle() != 0) 104 continue; 105#endif 106 107#ifdef __i386__ 108 cpu_idle(); 109#endif 110 } 111 112 mtx_lock_spin(&sched_lock); 113 curproc->p_stats->p_ru.ru_nvcsw++; 114 mi_switch(); 115 mtx_unlock_spin(&sched_lock); 116 } 117} 118