source: trunk/third/sysinfo/virtmem.c @ 13188

Revision 13188, 4.7 KB checked in by rbasch, 25 years ago (diff)
Fix GetVirtMemSwapctl() for IRIX: Use SC_GETLSWAPTOT instead of SC_GETSWAPVIRT; value passed back is an off_t, not a 32-bit int; convert to kbytes for GetVirtMemStr().
Line 
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
10static char *RCSid = "$Revision: 1.2 $";
11#endif
12
13/*
14 * Virtual Memory related functions.
15 */
16
17#include "defs.h"
18
19/*
20 * Change memory Amount into a string.
21 * Amount should always be in kilobytes.
22 */
23extern char *GetVirtMemStr(Amount)
24    Large_t                     Amount;
25{
26    static char                 Buff[32];
27
28    if (Amount == 0)
29        return((char *) NULL);
30
31    (void) snprintf(Buff, sizeof(Buff), "%s", GetSizeStr(Amount, KBYTES));
32
33    return(Buff);
34}
35
36/*
37 * Find amount of virtual memory using "anoninfo" symbol.
38 */
39
40#if     defined(HAVE_ANONINFO) && !defined(HAVE_SWAPCTL)
41#       include <vm/anon.h>
42#if     !defined(ANONINFO_SYM)
43#       define ANONINFO_SYM             "_anoninfo"
44#endif
45#endif  /* HAVE_ANONINFO && !HAVE_SWAPCTL */
46
47extern char *GetVirtMemAnoninfo()
48{
49#if     defined(HAVE_ANONINFO) && !defined(HAVE_SWAPCTL)
50    kvm_t                      *kd;
51    static struct anoninfo      AnonInfo;
52    Large_t                     Amount = 0;
53    nlist_t                    *nlPtr;
54    int                         PageSize;
55
56    if (kd = KVMopen()) {
57        if ((nlPtr = KVMnlist(kd, ANONINFO_SYM, (nlist_t *)NULL, 0)) == NULL)
58            return(0);
59
60        if (CheckNlist(nlPtr))
61            return(0);
62
63        if (KVMget(kd, nlPtr->n_value, (char *) &AnonInfo,
64                   sizeof(AnonInfo), KDT_DATA) >= 0)
65            Amount = (Large_t)AnonInfo.ani_max;
66
67        if (Amount) {
68            /*
69             * Try to avoid overflow
70             */
71            PageSize = getpagesize();
72            if (PageSize >= KBYTES)
73                Amount = Amount * ((Large_t)((PageSize / KBYTES)));
74            else
75                Amount = (Amount * (Large_t)PageSize) /
76                    (Large_t)KBYTES;
77            SImsg(SIM_DBG, "GetVirtMemAnon: Amount=%.0f PageSize=%d",
78                  (float) Amount, PageSize);
79        }
80
81        KVMclose(kd);
82    }
83
84    return(GetVirtMemStr(Amount));
85#else   /* !HAVE_ANONINFO */
86    return((char *) NULL);
87#endif  /* HAVE_ANONINFO && !HAVE_SWAPCTL*/
88}
89
90/*
91 * Find amount of virtual memory using swapctl()
92 */
93
94#if     defined(HAVE_SWAPCTL)
95#       include <sys/stat.h>
96#       include <sys/swap.h>
97#endif  /* HAVE_SWAPCTL */
98
99extern char *GetVirtMemSwapctl()
100{
101    char                       *ValStr = NULL;
102    Large_t                     Amount = 0;
103#if     defined(HAVE_ANONINFO) && defined(HAVE_SWAPCTL)
104    static struct anoninfo      AnonInfo;
105    int                         PageSize;
106
107    if (swapctl(SC_AINFO, &AnonInfo) == -1) {
108        SImsg(SIM_GERR, "swapctl(SC_AINFO) failed: %s", SYSERR);
109        return((char *) NULL);
110    }
111
112    SImsg(SIM_DBG, "GetVirtMemSwapctl: max=%d free=%d resv=%d",
113          AnonInfo.ani_max, AnonInfo.ani_free, AnonInfo.ani_resv);
114
115    Amount = (Large_t)AnonInfo.ani_max;
116
117    if (Amount) {
118        /*
119         * Try to avoid overflow
120         */
121        PageSize = getpagesize();
122        if (PageSize >= KBYTES)
123            Amount = Amount * ((Large_t)((PageSize / KBYTES)));
124        else
125            Amount = (Amount * (Large_t)PageSize) / (Large_t)KBYTES;
126        SImsg(SIM_DBG, "GetVirtMemAnon: Amount=%.0f PageSize=%d",
127              (float) Amount, PageSize);
128    }
129
130    ValStr = GetVirtMemStr(Amount);
131#endif  /* HAVE_ANONINFO && HAVE_SWAPCTL */
132
133#if     defined(HAVE_SWAPCTL) && defined(SC_GETLSWAPTOT)
134    off_t                       lswaptot;
135
136    if (swapctl(SC_GETLSWAPTOT, &lswaptot) == -1) {
137        SImsg(SIM_GERR, "swapctl(SC_GETLSWAPTOT) failed: %s", SYSERR);
138        return((char *) NULL);
139    }
140
141    SImsg(SIM_DBG, "SC_GETLSWAPTOT = %.0f blocks", (float) lswaptot);
142
143    Amount = (Large_t) ((lswaptot * 512) / 1024);
144
145    ValStr = GetVirtMemStr(Amount);
146#endif  /* HAVE_SWAPCTL && SC_GETLSWAPTOT */
147   
148    return(ValStr);
149}
150
151/*
152 * Use the "nswap" symbol to determine amount of
153 * virtual memory.
154 */
155#if     defined(NSWAP_SYM) && !defined(HAVE_NSWAP)
156#       define HAVE_NSWAP
157#endif  /* NSWAP_SYM */
158
159#if     defined(HAVE_NSWAP)
160
161#if     !defined(NSWAP_SIZE)
162#       define NSWAP_SIZE       512
163#endif  /* NSWAP_SIZE */
164#if     !defined(NSWAP_SYM)
165#       define NSWAP_SYM        "_nswap"
166#endif  /* NSWAP_SYM */
167
168#endif  /* HAVE_NSWAP */
169
170extern char *GetVirtMemNswap()
171{
172#if     defined(HAVE_NSWAP)
173    kvm_t                      *kd;
174    int                         Nswap;
175    Large_t                     Amount = 0;
176    nlist_t                    *nlPtr;
177
178    if (kd = KVMopen()) {
179        if ((nlPtr = KVMnlist(kd, NSWAP_SYM, (nlist_t *)NULL, 0)) == NULL)
180            return(0);
181
182        if (CheckNlist(nlPtr))
183            return(0);
184
185        if (KVMget(kd, nlPtr->n_value, (char *) &Nswap,
186                   sizeof(Nswap), KDT_DATA) >= 0)
187            Amount = (Large_t)Nswap;
188
189        Amount /= (Large_t)KBYTES / (Large_t)NSWAP_SIZE;
190        SImsg(SIM_DBG, "GetVirtMemNswap: Amount=%.0f Nswap=%d",
191              (float) Amount, Nswap);
192
193        KVMclose(kd);
194    }
195
196    return(GetVirtMemStr(Amount));
197#else   /* !HAVE_NSWAP */
198    return((char *) NULL);
199#endif  /* HAVE_NSWAP */
200}
201
202/*
203 * Get Virtual Memory
204 */
205extern char *
206GetVirtMem()
207{
208    extern PSI_t               GetVirtMemPSI[];
209    static char               *Str = NULL;
210
211    if (Str)
212        return(Str);
213
214    return(Str = PSIquery(GetVirtMemPSI));
215}
Note: See TracBrowser for help on using the repository browser.