aboutsummaryrefslogtreecommitdiffstats
path: root/net-mgmt/net-snmp-devel/files/patch-hr_storage.c
blob: ef250fa6540e4800cbf822d333ee7a2dc0c687b0 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
--- agent/mibgroup/host/hr_storage.c.orig   Fri Oct  7 07:55:23 2005
+++ agent/mibgroup/host/hr_storage.c    Wed Nov 30 12:01:05 2005
@@ -233,6 +233,10 @@
 void            sol_get_swapinfo(int *, int *);
 #endif
 
+#if defined(__FreeBSD__) && __FreeBSD_version >= 500102
+void       collect_mbuf(long *long_mbuf, long *long_mbufc);
+#endif
+
 #define    HRSTORE_MEMSIZE     1
 #define    HRSTORE_INDEX       2
 #define    HRSTORE_TYPE        3
@@ -472,7 +476,8 @@
     NULL,
     "Memory Buffers",           /* HRS_TYPE_MBUF */
     "Real Memory",              /* HRS_TYPE_MEM */
-    "Swap Space"                /* HRS_TYPE_SWAP */
+    "Swap Space",               /* HRS_TYPE_SWAP */
+    "Memory Buffer Clusters"    /* HRS_TYPE_MBUFCLUSTER */
 };
 
 
@@ -611,6 +616,7 @@
                 storage_type_id[storage_type_len - 1] = 3;      /* Virtual Mem */
                 break;
             case HRS_TYPE_MBUF:
+            case HRS_TYPE_MBUFCLUSTER:
                 storage_type_id[storage_type_len - 1] = 1;      /* Other */
                 break;
             default:
@@ -704,7 +710,7 @@
                 long_return = memory_totals.t_vm;
 #endif
                 break;
-#else               /* !linux && !solaris2 && !hpux10 && !hpux11 && ... */
+#else
             case HRS_TYPE_MEM:
                 long_return = physmem;
                 break;
@@ -726,13 +732,45 @@
                      i++)
                     long_return += mbstat.m_mtypes[i];
 #elif defined(MBSTAT_SYMBOL) && defined(STRUCT_MBSTAT_HAS_M_MBUFS)
+#if !defined(__FreeBSD__) || __FreeBSD_version < 500021
                 long_return = mbstat.m_mbufs;
+#elif defined(__FreeBSD__) && __FreeBSD_version < 500024
+       /* mbuf stats disabled */
+       return NULL;
+#elif defined(__FreeBSD__)
+       {
+         size_t mlen = sizeof(int);
+         int nmbufs;
+         if (sysctlbyname("kern.ipc.nmbufs", &nmbufs, &mlen,
+                  NULL, 0) < 0) {
+           return NULL;
+         }
+         long_return = nmbufs;
+         break;
+       }
+#else
+#error "XXX"
+       /* XXX TODO: implement new method */
+       return NULL;
+#endif /* __FreeBSD__ */
 #elif defined(NO_DUMMY_VALUES)
                 goto try_next;
 #else
                 long_return = 0;
 #endif
                 break;
+#if defined(__FreeBSD__) && __FreeBSD_version >= 500024
+       case HRS_TYPE_MBUFCLUSTER: {
+         size_t mlen = sizeof(int);
+         int nmbclusters;
+         if (sysctlbyname("kern.ipc.nmbclusters", &nmbclusters, &mlen,
+                  NULL, 0) < 0) {
+       return NULL;
+         }
+         long_return = nmbclusters;
+         break;
+       }
+#endif
             default:
 #if NO_DUMMY_VALUES
                 goto try_next;
@@ -796,6 +834,12 @@
 #endif
 #elif defined(MBSTAT_SYMBOL) && defined(STRUCT_MBSTAT_HAS_M_CLUSTERS)
                 long_return = mbstat.m_clusters - mbstat.m_clfree;      /* unlikely, but... */
+#elif defined(__FreeBSD__) && __FreeBSD_version < 500102
+       /* mbuf stats disabled */
+       return NULL;
+#elif defined(__FreeBSD__)
+       collect_mbuf((long*)&long_return, (long*)NULL);
+       break;
 #elif defined(NO_DUMMY_VALUES)
                 goto try_next;
 #else
@@ -803,6 +847,11 @@
 #endif
                 break;
 #endif                      /* !linux && !solaris2 && !hpux10 && !hpux11 && ... */
+#if defined(__FreeBSD__) && __FreeBSD_version >= 500102
+       case HRS_TYPE_MBUFCLUSTER:
+         collect_mbuf(NULL, &long_return);
+         break;
+#endif
             default:
 #if NO_DUMMY_VALUES
                 goto try_next;
@@ -829,7 +878,11 @@
                 break;
 #if !defined(linux) && !defined(solaris2) && !defined(hpux10) && !defined(hpux11)  && defined(MBSTAT_SYMBOL)
             case HRS_TYPE_MBUF:
+#if defined(__FreeBSD__) && __FreeBSD_version >= 502113
+                long_return = mbstat.m_mcfail + mbstat.m_mpfail;
+#else
                 long_return = mbstat.m_drops;
+#endif
                 break;
 #endif                          /* !linux && !solaris2 && !hpux10 && !hpux11 && MBSTAT_SYMBOL */
             default:
@@ -955,3 +1008,97 @@
     *usedP = ainfo.ani_resv;
 }
 #endif                          /* solaris2 */
+
+#if defined(__FreeBSD__) && __FreeBSD_version >= 500102
+void
+collect_mbuf(long *long_mbuf, long *long_mbufc)
+{
+#if __FreeBSD_version >= 502113
+  size_t mlen;
+  struct mbstat mbstat;
+
+  mlen = sizeof mbstat;
+  if (sysctlbyname("kern.ipc.mbstat", &mbstat, &mlen, NULL, 0) < 0) {
+    warn("sysctl: retrieving mbstat");
+    return;
+  }
+  if (mbstat.m_mbufs < 0) mbstat.m_mbufs = 0;      /* XXX */
+  if (mbstat.m_mclusts < 0) mbstat.m_mclusts = 0;  /* XXX */
+  if (long_mbuf) {
+    *long_mbuf = mbstat.m_mbufs;
+  }
+  if (long_mbufc) {
+    *long_mbufc = mbstat.m_mclusts;
+  }
+#else
+  int i, j, num_objs;
+  size_t mlen;
+  u_long totused[2];
+  u_long totnum, totfree;
+  struct mbstat mbstat;
+  struct mbpstat **mbpstat = NULL;
+
+  if (sysctlbyname("kern.ipc.mb_statpcpu", NULL, &mlen, NULL, 0) < 0) {
+    warn("sysctl: retrieving mb_statpcpu len");
+    goto err;
+  } 
+  num_objs = (int)(mlen / sizeof(struct mbpstat));
+  if ((mbpstat = calloc(num_objs, sizeof(struct mbpstat *))) == NULL) {
+    warn("calloc: cannot allocate memory for mbpstats pointers");
+    goto err;
+  }
+  if ((mbpstat[0] = calloc(num_objs, sizeof(struct mbpstat))) == NULL) {
+    warn("calloc: cannot allocate memory for mbpstats");
+    goto err;
+  }
+
+  if (sysctlbyname("kern.ipc.mb_statpcpu", mbpstat[0], &mlen, NULL, 0) < 0) {
+    warn("sysctl: retrieving mb_statpcpu");
+    goto err;
+  }
+  mlen = sizeof mbstat;
+  if (sysctlbyname("kern.ipc.mbstat", &mbstat, &mlen, NULL, 0) < 0) {
+    warn("sysctl: retrieving mbstat");
+    goto err;
+  }
+
+  for (i = 0; i < num_objs; i++)
+    mbpstat[i] = mbpstat[0] + i;
+
+#define    GENLST      (num_objs - 1)
+
+  totnum = mbpstat[GENLST]->mb_mbbucks * mbstat.m_mbperbuck;
+  totfree = mbpstat[GENLST]->mb_mbfree;
+  for (i = 0; i < (num_objs - 1); i++) {
+    if (mbpstat[i]->mb_active == 0)
+      continue;
+    totnum += mbpstat[i]->mb_mbbucks * mbstat.m_mbperbuck;
+    totfree += mbpstat[i]->mb_mbfree;
+  }
+  totused[0] = totnum - totfree;
+  totnum = mbpstat[GENLST]->mb_clbucks * mbstat.m_clperbuck;
+  totfree = mbpstat[GENLST]->mb_clfree;
+  for (i = 0; i < (num_objs - 1); i++) {
+    if (mbpstat[i]->mb_active == 0)
+      continue;
+    totnum += mbpstat[i]->mb_clbucks * mbstat.m_clperbuck;
+    totfree += mbpstat[i]->mb_clfree;
+  }
+  totused[1] = totnum - totfree;
+
+  if (long_mbuf) {
+    *long_mbuf = totused[0];
+  }
+  if (long_mbufc) {
+    *long_mbufc = totused[1];
+  }
+
+ err:
+  if (mbpstat != NULL) {
+    if (mbpstat[0] != NULL)
+      free(mbpstat[0]);
+    free(mbpstat);
+  }
+#endif
+}
+#endif