1 | /* |
---|
2 | * Copyright (c) 1992-1998 Michael A. Cooper. |
---|
3 | * This software may be freely used and distributed provided it is not |
---|
4 | * sold for profit or used in part or in whole for commercial gain |
---|
5 | * without prior written agreement, and the author is credited |
---|
6 | * appropriately. |
---|
7 | */ |
---|
8 | |
---|
9 | #ifndef lint |
---|
10 | static char *RCSid = "$Revision: 1.1.1.3 $"; |
---|
11 | #endif |
---|
12 | |
---|
13 | /* |
---|
14 | * Memory related functions. |
---|
15 | */ |
---|
16 | |
---|
17 | #include "defs.h" |
---|
18 | |
---|
19 | /* |
---|
20 | * Divide and Round Up |
---|
21 | */ |
---|
22 | extern Large_t DivRndUp(Num, Div) |
---|
23 | Large_t Num; |
---|
24 | Large_t Div; |
---|
25 | { |
---|
26 | Large_t i; |
---|
27 | |
---|
28 | i = Num / Div; |
---|
29 | |
---|
30 | return((Num % Div) ? i+1 : i); |
---|
31 | } |
---|
32 | |
---|
33 | /* |
---|
34 | * Get string of the amount of physical memory |
---|
35 | */ |
---|
36 | extern char *GetMemoryStr(Amount) |
---|
37 | Large_t Amount; |
---|
38 | { |
---|
39 | static char Buff[64]; |
---|
40 | |
---|
41 | if (Amount > 0) { |
---|
42 | (void) snprintf(Buff, sizeof(Buff), "%s", GetSizeStr(Amount, MBYTES)); |
---|
43 | return(Buff); |
---|
44 | } |
---|
45 | |
---|
46 | return((char *) NULL); |
---|
47 | } |
---|
48 | |
---|
49 | #if defined(PHYSMEM_SYM) && !defined(HAVE_PHYSMEM) |
---|
50 | # define HAVE_PHYSMEM 1 |
---|
51 | #endif /* PHYSMEM_SYM */ |
---|
52 | #if !defined(PHYSMEM_SYM) |
---|
53 | # define PHYSMEM_SYM "_physmem" |
---|
54 | #endif /* PHYSMEM_SYM */ |
---|
55 | #if !defined(GETPAGESIZE) |
---|
56 | # define GETPAGESIZE() getpagesize() |
---|
57 | #endif /* GETPAGESIZE */ |
---|
58 | |
---|
59 | /* |
---|
60 | * Common method of determining amount of physical memory in a |
---|
61 | * BSD Unix machine. |
---|
62 | * |
---|
63 | * Get memory by reading the variable "physmem" from the kernel |
---|
64 | * and the system page size. |
---|
65 | */ |
---|
66 | extern char *GetMemoryPhysmemSym() |
---|
67 | { |
---|
68 | #if defined(HAVE_PHYSMEM) |
---|
69 | nlist_t *nlptr; |
---|
70 | Large_t Bytes; |
---|
71 | Large_t Amount = 0; |
---|
72 | kvm_t *kd; |
---|
73 | int physmem; |
---|
74 | |
---|
75 | if (kd = KVMopen()) { |
---|
76 | if ((nlptr = KVMnlist(kd, PHYSMEM_SYM, (nlist_t *)NULL, 0)) == NULL) |
---|
77 | return((char *)NULL); |
---|
78 | |
---|
79 | if (CheckNlist(nlptr)) |
---|
80 | return((char *)NULL); |
---|
81 | |
---|
82 | if (KVMget(kd, nlptr->n_value, (char *)&physmem, |
---|
83 | sizeof(physmem), KDT_DATA) >= 0) { |
---|
84 | /* |
---|
85 | * Could use ctob() instead of "Page Size * Num Pages", |
---|
86 | * but this is more portable. |
---|
87 | */ |
---|
88 | Bytes = (Large_t)GETPAGESIZE() * (Large_t)physmem; |
---|
89 | SImsg(SIM_DBG, "Physmem: Bytes = %.0f physmem = %d pagesize = %d", |
---|
90 | (float) Bytes, physmem, GETPAGESIZE()); |
---|
91 | Amount = DivRndUp(Bytes, (Large_t)MBYTES); |
---|
92 | } |
---|
93 | } |
---|
94 | |
---|
95 | if (kd) |
---|
96 | KVMclose(kd); |
---|
97 | |
---|
98 | return(GetMemoryStr(Amount)); |
---|
99 | #else /* !HAVE_PHYSMEM */ |
---|
100 | return((char *) NULL); |
---|
101 | #endif /* HAVE_PHYSMEM */ |
---|
102 | } |
---|
103 | |
---|
104 | /* |
---|
105 | * Get amount of memory on machine. |
---|
106 | */ |
---|
107 | extern char *GetMemory() |
---|
108 | { |
---|
109 | extern PSI_t GetMemoryPSI[]; |
---|
110 | static char *Str = NULL; |
---|
111 | |
---|
112 | if (Str) |
---|
113 | return(Str); |
---|
114 | |
---|
115 | return(Str = PSIquery(GetMemoryPSI)); |
---|
116 | } |
---|