1 /* @(#)xdelay.c	1.16 09/07/11 Copyright 1991-2009 J. Schilling */
2 #include <schily/mconfig.h>
3 #ifndef lint
4 static	UConst char sccsid[] =
5 	"@(#)xdelay.c	1.16 09/07/11 Copyright 1991-2009 J. Schilling";
6 #endif
7 /*
8  *	Delay for disks that cannot disconnect
9  *
10  *	Copyright (c) 1991-2009 J. Schilling
11  */
12 /*
13  * The contents of this file are subject to the terms of the
14  * Common Development and Distribution License, Version 1.0 only
15  * (the "License").  You may not use this file except in compliance
16  * with the License.
17  *
18  * See the file CDDL.Schily.txt in this distribution for details.
19  * A copy of the CDDL is also available via the Internet at
20  * http://www.opensource.org/licenses/cddl1.txt
21  *
22  * When distributing Covered Code, include this CDDL HEADER in each
23  * file and include the License file CDDL.Schily.txt from this distribution.
24  */
25 
26 #ifdef	sun
27 
28 #include <schily/stdio.h>
29 #include <schily/unistd.h>
30 #include <kvm.h>
31 #include <schily/fcntl.h>
32 #include <nlist.h>
33 #include <schily/param.h>	/* Include various defs needed with some OS */
34 #include <schily/standard.h>
35 #include <schily/schily.h>
36 #include <schily/libport.h>
37 
38 #ifdef	FMT
39 #include "fmt.h"
40 #endif
41 
42 static int initialized;
43 static kvm_t *kd;
44 
45 static long avenrun[3];
46 
47 struct nlist nl[] = {
48 #ifdef	SVR4
49 	{ "avenrun"},
50 #else
51 	{ "_avenrun"},
52 #endif
53 	{ 0 },
54 };
55 
56 LOCAL	void	xinit	__PR((void));
57 EXPORT	void	xdelay	__PR((void));
58 
59 LOCAL void
xinit()60 xinit()
61 {
62 	initialized = 1;
63 
64 	kd = kvm_open(0, 0, 0, O_RDONLY, 0);
65 	if (!kd)
66 		return;
67 
68 	kvm_nlist(kd, nl);
69 	if (nl[0].n_type == 0) {
70 		kvm_close(kd);
71 		kd = 0;
72 		return;
73 	}
74 /*	printf("avenrun: %X\n", nl[0].n_value);*/
75 }
76 
77 EXPORT void
xdelay()78 xdelay()
79 {
80 	static long oavrun = 0;
81 	long avrun;
82 	long x;
83 
84 	if (!initialized)
85 		xinit();
86 	if (!kd) {
87 		usleep(10000);
88 		return;
89 	}
90 	if (kvm_read(kd, nl[0].n_value, (char *)avenrun, sizeof (avenrun)) !=
91 		sizeof (avenrun))
92 		errmsg("kvm_read\n");
93 
94 /*	printf("\n%f %d\n", (double)avenrun[0]/FSCALE, avenrun[0]*1000/FSCALE);*/
95 	avrun = avenrun[0]*100/FSCALE;
96 	x = avrun - 95;
97 	x *= 4;
98 	if (x < 20) x = 20;
99 	printf("\r\t\tload %ld.%02ld wtime: %ld ms  \r",
100 						avrun/100, avrun%100, x);
101 	if (x > 2000) x = 2000;
102 	usleep(x*1000);
103 	if ((oavrun - avrun) > 10)
104 		usleep(100000);
105 	oavrun = avrun;
106 }
107 
108 #else	/* sun */
109 
110 #include <schily/mconfig.h>
111 #include <schily/unistd.h>
112 #include <schily/standard.h>
113 
114 #include "fmt.h"
115 
116 EXPORT	void	xdelay	__PR((void));
117 
118 EXPORT void
xdelay()119 xdelay()
120 {
121 	usleep(10000);
122 }
123 #endif	/* sun */
124