1/*
2 * Copyright (c) 2008, 2009 Apple Inc.  All rights reserved.
3 *
4 * @APPLE_LICENSE_HEADER_START@
5 *
6 * The contents of this file constitute Original Code as defined in and
7 * are subject to the Apple Public Source License Version 1.1 (the
8 * "License").  You may not use this file except in compliance with the
9 * License.  Please obtain a copy of the License at
10 * http://www.apple.com/publicsource and read it before using this file.
11 *
12 * This Original Code and all software distributed under the License are
13 * distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, EITHER
14 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
15 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT.  Please see the
17 * License for the specific language governing rights and limitations
18 * under the License.
19 *
20 * @APPLE_LICENSE_HEADER_END@
21 */
22
23#include <stdlib.h>
24#include <string.h>
25#include <inttypes.h>
26#include <libutil.h>
27#include "libtop.h"
28#include "memstats.h"
29#include "generic.h"
30#include "uinteger.h"
31#include "preferences.h"
32
33#define NA(buf) do {	       	   \
34	memcpy(buf, "N/A", 4); 	   \
35    } while(0)
36
37#ifndef TOP_ANONYMOUS_MEMORY
38/*rsize*/
39static bool rsize_insert_cell(struct statistic *s, const void *sample) {
40    const libtop_psamp_t *psamp = sample;
41    char buf[7];
42
43    if(top_prefs_get_mmr()) {
44	if(top_uinteger_format_mem_result(buf, sizeof(buf),
45					  psamp->rsize, psamp->p_rsize,
46					  /*There is no b_rsize*/ 0ULL)) {
47	    return true;
48	}
49    } else {
50	NA(buf);
51    }
52
53    return generic_insert_cell(s, buf);
54}
55
56static struct statistic_callbacks rsize_callbacks = {
57    .draw = generic_draw,
58    .resize_cells = generic_resize_cells,
59    .move_cells = generic_move_cells,
60    .get_request_size = generic_get_request_size,
61    .get_minimum_size = generic_get_minimum_size,
62    .insert_cell = rsize_insert_cell,
63    .reset_insertion = generic_reset_insertion
64};
65
66struct statistic *top_rsize_create(WINDOW *parent, const char *name) {
67    return create_statistic(STATISTIC_RSIZE, parent, NULL, &rsize_callbacks,
68			    name);
69}
70#endif /* !TOP_ANONYMOUS_MEMORY */
71
72/*vsize*/
73static bool vsize_insert_cell(struct statistic *s, const void *sample) {
74    const libtop_psamp_t *psamp = sample;
75    char buf[7];
76
77    if(top_prefs_get_mmr()) {
78	if(top_uinteger_format_mem_result(buf, sizeof(buf),
79					  psamp->vsize, psamp->p_vsize, 0ULL)) {
80	    return true;
81	}
82    } else {
83	NA(buf);
84    }
85
86    return generic_insert_cell(s, buf);
87}
88
89static struct statistic_callbacks vsize_callbacks = {
90    .draw = generic_draw,
91    .resize_cells = generic_resize_cells,
92    .move_cells = generic_move_cells,
93    .get_request_size = generic_get_request_size,
94    .get_minimum_size = generic_get_minimum_size,
95    .insert_cell = vsize_insert_cell,
96    .reset_insertion = generic_reset_insertion
97};
98
99struct statistic *top_vsize_create(WINDOW *parent, const char *name) {
100    return create_statistic(STATISTIC_VSIZE, parent, NULL, &vsize_callbacks,
101                            name);
102}
103
104
105/*rprvt*/
106static bool rprvt_insert_cell(struct statistic *s, const void *sample) {
107    const libtop_psamp_t *psamp = sample;
108    char buf[7];
109
110    if(top_prefs_get_mmr()) {
111	if(top_uinteger_format_mem_result(buf, sizeof(buf),
112					  psamp->rprvt, psamp->p_rprvt, 0ULL)) {
113	    return true;
114	}
115    } else {
116	NA(buf);
117    }
118
119    return generic_insert_cell(s, buf);
120}
121
122static struct statistic_callbacks rprvt_callbacks = {
123    .draw = generic_draw,
124    .resize_cells = generic_resize_cells,
125    .move_cells = generic_move_cells,
126    .get_request_size = generic_get_request_size,
127    .get_minimum_size = generic_get_minimum_size,
128    .insert_cell = rprvt_insert_cell,
129    .reset_insertion = generic_reset_insertion
130};
131
132struct statistic *top_rprvt_create(WINDOW *parent, const char *name) {
133    return create_statistic(STATISTIC_RPRVT, parent, NULL, &rprvt_callbacks,
134			    name);
135}
136
137/*vprvt*/
138static bool vprvt_insert_cell(struct statistic *s, const void *sample) {
139    const libtop_psamp_t *psamp = sample;
140    char buf[7];
141
142    if(top_prefs_get_mmr()) {
143	if(top_uinteger_format_mem_result(buf, sizeof(buf),
144					  psamp->vprvt, psamp->p_vprvt, 0ULL)) {
145	    return true;
146	}
147    } else {
148	NA(buf);
149    }
150
151    return generic_insert_cell(s, buf);
152}
153
154static struct statistic_callbacks vprvt_callbacks = {
155    .draw = generic_draw,
156    .resize_cells = generic_resize_cells,
157    .move_cells = generic_move_cells,
158    .get_request_size = generic_get_request_size,
159    .get_minimum_size = generic_get_minimum_size,
160    .insert_cell = vprvt_insert_cell,
161    .reset_insertion = generic_reset_insertion
162};
163
164struct statistic *top_vprvt_create(WINDOW *parent, const char *name) {
165    return create_statistic(STATISTIC_VPRVT, parent, NULL, &vprvt_callbacks,
166                            name);
167}
168
169#ifndef TOP_ANONYMOUS_MEMORY
170/*rshrd*/
171static bool rshrd_insert_cell(struct statistic *s, const void *sample) {
172    const libtop_psamp_t *psamp = sample;
173    char buf[7];
174
175    if(top_prefs_get_mmr()) {
176	if(top_uinteger_format_mem_result(buf, sizeof(buf),
177					  psamp->rshrd, psamp->p_rshrd, 0ULL)) {
178	    return true;
179	}
180    } else {
181	NA(buf);
182    }
183
184    return generic_insert_cell(s, buf);
185}
186
187static struct statistic_callbacks rshrd_callbacks = {
188    .draw = generic_draw,
189    .resize_cells = generic_resize_cells,
190    .move_cells = generic_move_cells,
191    .get_request_size = generic_get_request_size,
192    .get_minimum_size = generic_get_minimum_size,
193    .insert_cell = rshrd_insert_cell,
194    .reset_insertion = generic_reset_insertion
195};
196
197struct statistic *top_rshrd_create(WINDOW *parent, const char *name) {
198    return create_statistic(STATISTIC_RSHRD, parent, NULL, &rshrd_callbacks,
199			    name);
200}
201#endif /* !TOP_ANONYMOUS_MEMORY */
202
203
204/*reg/mregions*/
205static bool mregion_insert_cell(struct statistic *s, const void *sample) {
206    const libtop_psamp_t *psamp = sample;
207    char buf[GENERIC_INT_SIZE];
208
209    if(top_prefs_get_mmr()) {
210	if(top_uinteger_format_result(buf, sizeof(buf),
211				      psamp->reg, psamp->p_reg, 0ULL)) {
212	    return true;
213	}
214    } else {
215	NA(buf);
216    }
217
218    return generic_insert_cell(s, buf);
219}
220
221static struct statistic_callbacks mregion_callbacks = {
222    .draw = generic_draw,
223    .resize_cells = generic_resize_cells,
224    .move_cells = generic_move_cells,
225    .get_request_size = generic_get_request_size,
226    .get_minimum_size = generic_get_minimum_size,
227    .insert_cell = mregion_insert_cell,
228    .reset_insertion = generic_reset_insertion
229};
230
231struct statistic *top_mregion_create(WINDOW *parent, const char *name) {
232    return create_statistic(STATISTIC_MREGION, parent, NULL, &mregion_callbacks,
233			    name);
234}
235
236
237static bool pageins_insert_cell(struct statistic *s, const void *sample) {
238    const libtop_psamp_t *psamp = sample;
239    char buf[GENERIC_INT_SIZE];
240
241    if(top_uinteger_format_result(buf, sizeof(buf),
242				  psamp->pageins.now,
243				  psamp->pageins.previous,
244				  psamp->pageins.began)) {
245	return true;
246    }
247
248    return generic_insert_cell(s, buf);
249}
250
251static struct statistic_callbacks pageins_callbacks = {
252    .draw = generic_draw,
253    .resize_cells = generic_resize_cells,
254    .move_cells = generic_move_cells,
255    .get_request_size = generic_get_request_size,
256    .get_minimum_size = generic_get_minimum_size,
257    .insert_cell = pageins_insert_cell,
258    .reset_insertion = generic_reset_insertion
259};
260
261struct statistic *top_pageins_create(WINDOW *parent, const char *name) {
262    return create_statistic(STATISTIC_PAGEINS, parent, NULL, &pageins_callbacks,
263			    name);
264}
265
266
267/*kprvt*/
268static bool kprvt_insert_cell(struct statistic *s, const void *sample) {
269    const libtop_psamp_t *psamp = sample;
270    char buf[7];
271
272    if(top_prefs_get_mmr()) {
273	if(top_uinteger_format_mem_result(buf, sizeof(buf),
274		  psamp->palloc - psamp->pfree,
275		  psamp->p_palloc - psamp->p_pfree,
276		  0ULL)) {
277	    return true;
278	}
279    } else {
280	NA(buf);
281    }
282
283    return generic_insert_cell(s, buf);
284}
285
286static struct statistic_callbacks kprvt_callbacks = {
287    .draw = generic_draw,
288    .resize_cells = generic_resize_cells,
289    .move_cells = generic_move_cells,
290    .get_request_size = generic_get_request_size,
291    .get_minimum_size = generic_get_minimum_size,
292    .insert_cell = kprvt_insert_cell,
293    .reset_insertion = generic_reset_insertion
294};
295
296struct statistic *top_kprvt_create(WINDOW *parent, const char *name) {
297    return create_statistic(STATISTIC_KPRVT, parent, NULL, &kprvt_callbacks,
298                            name);
299}
300
301/*kshrd*/
302static bool kshrd_insert_cell(struct statistic *s, const void *sample) {
303    const libtop_psamp_t *psamp = sample;
304    char buf[7];
305
306    if(top_prefs_get_mmr()) {
307	if(top_uinteger_format_mem_result(buf, sizeof(buf),
308		  psamp->salloc - psamp->sfree,
309		  psamp->p_salloc - psamp->p_sfree,
310		  0ULL)) {
311	    return true;
312	}
313    } else {
314	NA(buf);
315    }
316
317    return generic_insert_cell(s, buf);
318}
319
320static struct statistic_callbacks kshrd_callbacks = {
321    .draw = generic_draw,
322    .resize_cells = generic_resize_cells,
323    .move_cells = generic_move_cells,
324    .get_request_size = generic_get_request_size,
325    .get_minimum_size = generic_get_minimum_size,
326    .insert_cell = kshrd_insert_cell,
327    .reset_insertion = generic_reset_insertion
328};
329
330struct statistic *top_kshrd_create(WINDOW *parent, const char *name) {
331    return create_statistic(STATISTIC_KSHRD, parent, NULL, &kshrd_callbacks,
332                            name);
333}
334
335#ifdef TOP_ANONYMOUS_MEMORY
336static bool
337rmem_insert_cell(struct statistic *s, const void *sample)
338{
339	const libtop_psamp_t *psamp = sample;
340	char buf[7];
341
342	if (top_uinteger_format_mem_result(buf, sizeof(buf), psamp->anonymous, psamp->p_anonymous, 0ULL)) {
343		return true;
344	}
345
346	return generic_insert_cell(s, buf);
347}
348
349static struct statistic_callbacks rmem_callbacks = {
350	.draw = generic_draw,
351	.resize_cells = generic_resize_cells,
352	.move_cells = generic_move_cells,
353	.get_request_size = generic_get_request_size,
354	.get_minimum_size = generic_get_minimum_size,
355	.insert_cell = rmem_insert_cell,
356	.reset_insertion = generic_reset_insertion,
357};
358
359struct statistic *
360top_rmem_create(WINDOW *parent, const char *name)
361{
362	return create_statistic(STATISTIC_RMEM, parent, NULL, &rmem_callbacks, name);
363}
364
365static bool
366purg_insert_cell(struct statistic *s, const void *sample)
367{
368	const libtop_psamp_t *psamp = sample;
369	char buf[7];
370
371	if (top_uinteger_format_mem_result(buf, sizeof(buf), psamp->purgeable, psamp->p_purgeable, 0ULL)) {
372		return true;
373	}
374
375	return generic_insert_cell(s, buf);
376}
377
378static struct statistic_callbacks purg_callbacks = {
379	.draw = generic_draw,
380	.resize_cells = generic_resize_cells,
381	.move_cells = generic_move_cells,
382	.get_request_size = generic_get_request_size,
383	.get_minimum_size = generic_get_minimum_size,
384	.insert_cell = purg_insert_cell,
385	.reset_insertion = generic_reset_insertion,
386};
387
388struct statistic *
389top_purg_create(WINDOW *parent, const char *name)
390{
391	return create_statistic(STATISTIC_PURG, parent, NULL, &purg_callbacks, name);
392}
393
394static bool
395compressed_insert_cell(struct statistic *s, const void *sample)
396{
397	const libtop_psamp_t *psamp = sample;
398	char buf[7];
399
400	if (top_uinteger_format_mem_result(buf, sizeof(buf), psamp->compressed, psamp->p_compressed, 0ULL)) {
401		return true;
402	}
403
404	return generic_insert_cell(s, buf);
405}
406
407static struct statistic_callbacks compressed_callbacks = {
408	.draw = generic_draw,
409	.resize_cells = generic_resize_cells,
410	.move_cells = generic_move_cells,
411	.get_request_size = generic_get_request_size,
412	.get_minimum_size = generic_get_minimum_size,
413	.insert_cell = compressed_insert_cell,
414	.reset_insertion = generic_reset_insertion,
415};
416
417struct statistic *
418top_compressed_create(WINDOW *parent, const char *name)
419{
420	return create_statistic(STATISTIC_COMPRESSED, parent, NULL, &compressed_callbacks, name);
421}
422#endif /* TOP_ANONYMOUS_MEMORY */
423