1368931Smr/*-
2368931Smr * Copyright (c) 2019 Yoshihiro Ota
3368931Smr *
4368931Smr * Redistribution and use in source and binary forms, with or without
5368931Smr * modification, are permitted provided that the following conditions
6368931Smr * are met:
7368931Smr * 1. Redistributions of source code must retain the above copyright
8368931Smr *    notice, this list of conditions and the following disclaimer.
9368931Smr * 2. Redistributions in binary form must reproduce the above copyright
10368931Smr *    notice, this list of conditions and the following disclaimer in the
11368931Smr *    documentation and/or other materials provided with the distribution.
12368931Smr * 3. Neither the name of the University nor the names of its contributors
13368931Smr *    may be used to endorse or promote products derived from this software
14368931Smr *    without specific prior written permission.
15368931Smr *
16368931Smr * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
17368931Smr * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18368931Smr * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19368931Smr * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
20368931Smr * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21368931Smr * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22368931Smr * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23368931Smr * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24368931Smr * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25368931Smr * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26368931Smr * SUCH DAMAGE.
27368931Smr */
28368931Smr
29368931Smr#include <sys/cdefs.h>
30368931Smr__FBSDID("$FreeBSD$");
31368931Smr
32368931Smr#include <sys/types.h>
33368931Smr#include <sys/sysctl.h>
34368931Smr
35368931Smr#include <inttypes.h>
36368931Smr#include <string.h>
37368931Smr#include <err.h>
38368931Smr#include <libutil.h>
39368931Smr
40368931Smr#include "systat.h"
41368931Smr#include "extern.h"
42368931Smr
43368931Smrvoid
44368931Smrsysputspaces(WINDOW *wd, int row, int col, int width)
45368931Smr{
46368931Smr	static char str40[] = "                                        ";
47368931Smr
48368931Smr	mvwaddstr(wd, row, col, str40 + sizeof(str40) - width - 1);
49368931Smr}
50368931Smr
51368931Smrvoid
52368931Smrsysputstrs(WINDOW *wd, int row, int col, int width)
53368931Smr{
54368931Smr	static char str40[] = "****************************************";
55368931Smr
56368931Smr	mvwaddstr(wnd, row, col, str40 + sizeof(str40) - width - 1);
57368931Smr}
58368931Smr
59368931Smrvoid
60368931Smrsysputuint64(WINDOW *wd, int row, int col, int width, uint64_t val, int flags)
61368931Smr{
62368931Smr	char unit, *ptr, *start, wrtbuf[width + width + 1];
63368931Smr	int len;
64368931Smr
65368931Smr	unit = 0;
66368931Smr	start = wrtbuf;
67368931Smr	flags |= HN_NOSPACE;
68368931Smr
69368931Smr	if (val > INT64_MAX)
70368931Smr		goto error;
71368931Smr	else
72368931Smr		len = humanize_number(&wrtbuf[width], width + 1, val, "",
73368931Smr			HN_AUTOSCALE, flags);
74368931Smr	if (len < 0)
75368931Smr		goto error;
76368931Smr	else if (len < width)
77368931Smr		memset(wrtbuf + len, ' ', width - len);
78368931Smr	start += len;
79368931Smr
80368931Smr	mvwaddstr(wd, row, col, start);
81368931Smr	return;
82368931Smr
83368931Smrerror:
84368931Smr	sysputstrs(wd, row, col, width);
85368931Smr}
86368931Smr
87368931Smrvoid
88368931Smrsysputwuint64(WINDOW *wd, int row, int col, int width, uint64_t val, int flags)
89368931Smr{
90368931Smr	if(val == 0)
91368931Smr		sysputspaces(wd, row, col, width);
92368931Smr	else
93368931Smr		sysputuint64(wd, row, col, width, val, flags);
94368931Smr}
95368931Smr
96368931Smrstatic int
97368931Smrcalc_page_shift()
98368931Smr{
99368931Smr	u_int page_size;
100368931Smr	int shifts;
101368931Smr
102368931Smr	shifts = 0;
103368931Smr	GETSYSCTL("vm.stats.vm.v_page_size", page_size);
104368931Smr	for(; page_size > 1; page_size >>= 1)
105368931Smr		shifts++;
106368931Smr	return shifts;
107368931Smr}
108368931Smr
109368931Smrvoid
110368931Smrsysputpage(WINDOW *wd, int row, int col, int width, uint64_t pages, int flags)
111368931Smr{
112368931Smr	static int shifts = 0;
113368931Smr
114368931Smr	if (shifts == 0)
115368931Smr		shifts = calc_page_shift();
116368931Smr	pages <<= shifts;
117368931Smr	sysputuint64(wd, row, col, width, pages, flags);
118368931Smr}
119