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