1*7c478bd9Sstevel@tonic-gate /*- 2*7c478bd9Sstevel@tonic-gate * See the file LICENSE for redistribution information. 3*7c478bd9Sstevel@tonic-gate * 4*7c478bd9Sstevel@tonic-gate * Copyright (c) 1997, 1998 5*7c478bd9Sstevel@tonic-gate * Sleepycat Software. All rights reserved. 6*7c478bd9Sstevel@tonic-gate */ 7*7c478bd9Sstevel@tonic-gate 8*7c478bd9Sstevel@tonic-gate #include "config.h" 9*7c478bd9Sstevel@tonic-gate 10*7c478bd9Sstevel@tonic-gate #ifndef lint 11*7c478bd9Sstevel@tonic-gate static const char sccsid[] = "@(#)os_spin.c 10.10 (Sleepycat) 10/12/98"; 12*7c478bd9Sstevel@tonic-gate #endif /* not lint */ 13*7c478bd9Sstevel@tonic-gate 14*7c478bd9Sstevel@tonic-gate #ifndef NO_SYSTEM_INCLUDES 15*7c478bd9Sstevel@tonic-gate #include <sys/types.h> 16*7c478bd9Sstevel@tonic-gate #if defined(HAVE_PSTAT_GETDYNAMIC) 17*7c478bd9Sstevel@tonic-gate #include <sys/pstat.h> 18*7c478bd9Sstevel@tonic-gate #endif 19*7c478bd9Sstevel@tonic-gate 20*7c478bd9Sstevel@tonic-gate #include <limits.h> 21*7c478bd9Sstevel@tonic-gate #include <unistd.h> 22*7c478bd9Sstevel@tonic-gate #endif 23*7c478bd9Sstevel@tonic-gate 24*7c478bd9Sstevel@tonic-gate #include "db_int.h" 25*7c478bd9Sstevel@tonic-gate #include "os_jump.h" 26*7c478bd9Sstevel@tonic-gate 27*7c478bd9Sstevel@tonic-gate #if defined(HAVE_PSTAT_GETDYNAMIC) 28*7c478bd9Sstevel@tonic-gate /* 29*7c478bd9Sstevel@tonic-gate * __os_pstat_getdynamic -- 30*7c478bd9Sstevel@tonic-gate * HP/UX. 31*7c478bd9Sstevel@tonic-gate */ 32*7c478bd9Sstevel@tonic-gate static int __os_pstat_getdynamic()33*7c478bd9Sstevel@tonic-gate__os_pstat_getdynamic() 34*7c478bd9Sstevel@tonic-gate { 35*7c478bd9Sstevel@tonic-gate struct pst_dynamic psd; 36*7c478bd9Sstevel@tonic-gate 37*7c478bd9Sstevel@tonic-gate return (pstat_getdynamic(&psd, 38*7c478bd9Sstevel@tonic-gate sizeof(psd), (size_t)1, 0) == -1 ? 1 : psd.psd_proc_cnt); 39*7c478bd9Sstevel@tonic-gate } 40*7c478bd9Sstevel@tonic-gate #endif 41*7c478bd9Sstevel@tonic-gate 42*7c478bd9Sstevel@tonic-gate #if defined(HAVE_SYSCONF) && defined(_SC_NPROCESSORS_ONLN) 43*7c478bd9Sstevel@tonic-gate /* 44*7c478bd9Sstevel@tonic-gate * __os_sysconf -- 45*7c478bd9Sstevel@tonic-gate * Solaris, Linux. 46*7c478bd9Sstevel@tonic-gate */ 47*7c478bd9Sstevel@tonic-gate static int __os_sysconf()48*7c478bd9Sstevel@tonic-gate__os_sysconf() 49*7c478bd9Sstevel@tonic-gate { 50*7c478bd9Sstevel@tonic-gate int nproc; 51*7c478bd9Sstevel@tonic-gate 52*7c478bd9Sstevel@tonic-gate return ((nproc = sysconf(_SC_NPROCESSORS_ONLN)) > 1 ? nproc : 1); 53*7c478bd9Sstevel@tonic-gate } 54*7c478bd9Sstevel@tonic-gate #endif 55*7c478bd9Sstevel@tonic-gate 56*7c478bd9Sstevel@tonic-gate /* 57*7c478bd9Sstevel@tonic-gate * __os_spin -- 58*7c478bd9Sstevel@tonic-gate * Return the number of default spins before blocking. 59*7c478bd9Sstevel@tonic-gate * 60*7c478bd9Sstevel@tonic-gate * PUBLIC: int __os_spin __P((void)); 61*7c478bd9Sstevel@tonic-gate */ 62*7c478bd9Sstevel@tonic-gate int __os_spin()63*7c478bd9Sstevel@tonic-gate__os_spin() 64*7c478bd9Sstevel@tonic-gate { 65*7c478bd9Sstevel@tonic-gate /* 66*7c478bd9Sstevel@tonic-gate * If the application specified a value or we've already figured it 67*7c478bd9Sstevel@tonic-gate * out, return it. 68*7c478bd9Sstevel@tonic-gate * 69*7c478bd9Sstevel@tonic-gate * XXX 70*7c478bd9Sstevel@tonic-gate * We don't want to repeatedly call the underlying function because 71*7c478bd9Sstevel@tonic-gate * it can be expensive (e.g., requiring multiple filesystem accesses 72*7c478bd9Sstevel@tonic-gate * under Debian Linux). 73*7c478bd9Sstevel@tonic-gate */ 74*7c478bd9Sstevel@tonic-gate if (DB_GLOBAL(db_tsl_spins) != 0) 75*7c478bd9Sstevel@tonic-gate return (DB_GLOBAL(db_tsl_spins)); 76*7c478bd9Sstevel@tonic-gate 77*7c478bd9Sstevel@tonic-gate DB_GLOBAL(db_tsl_spins) = 1; 78*7c478bd9Sstevel@tonic-gate #if defined(HAVE_PSTAT_GETDYNAMIC) 79*7c478bd9Sstevel@tonic-gate DB_GLOBAL(db_tsl_spins) = __os_pstat_getdynamic(); 80*7c478bd9Sstevel@tonic-gate #endif 81*7c478bd9Sstevel@tonic-gate #if defined(HAVE_SYSCONF) && defined(_SC_NPROCESSORS_ONLN) 82*7c478bd9Sstevel@tonic-gate DB_GLOBAL(db_tsl_spins) = __os_sysconf(); 83*7c478bd9Sstevel@tonic-gate #endif 84*7c478bd9Sstevel@tonic-gate 85*7c478bd9Sstevel@tonic-gate /* 86*7c478bd9Sstevel@tonic-gate * Spin 50 times per processor, we have anecdotal evidence that this 87*7c478bd9Sstevel@tonic-gate * is a reasonable value. 88*7c478bd9Sstevel@tonic-gate */ 89*7c478bd9Sstevel@tonic-gate DB_GLOBAL(db_tsl_spins) *= 50; 90*7c478bd9Sstevel@tonic-gate 91*7c478bd9Sstevel@tonic-gate return (DB_GLOBAL(db_tsl_spins)); 92*7c478bd9Sstevel@tonic-gate } 93*7c478bd9Sstevel@tonic-gate 94*7c478bd9Sstevel@tonic-gate /* 95*7c478bd9Sstevel@tonic-gate * __os_yield -- 96*7c478bd9Sstevel@tonic-gate * Yield the processor. 97*7c478bd9Sstevel@tonic-gate * 98*7c478bd9Sstevel@tonic-gate * PUBLIC: void __os_yield __P((u_long)); 99*7c478bd9Sstevel@tonic-gate */ 100*7c478bd9Sstevel@tonic-gate void __os_yield(usecs)101*7c478bd9Sstevel@tonic-gate__os_yield(usecs) 102*7c478bd9Sstevel@tonic-gate u_long usecs; 103*7c478bd9Sstevel@tonic-gate { 104*7c478bd9Sstevel@tonic-gate if (__db_jump.j_yield != NULL && __db_jump.j_yield() == 0) 105*7c478bd9Sstevel@tonic-gate return; 106*7c478bd9Sstevel@tonic-gate __os_sleep(0, usecs); 107*7c478bd9Sstevel@tonic-gate } 108