1 /* Copyright (C) 1998-99 Martin Baulig
2    This file is part of LibGTop 1.0.
3 
4    Contributed by Martin Baulig <martin@home-of-linux.org>, April 1998.
5 
6    LibGTop is free software; you can redistribute it and/or modify it
7    under the terms of the GNU General Public License as published by
8    the Free Software Foundation; either version 2 of the License,
9    or (at your option) any later version.
10 
11    LibGTop is distributed in the hope that it will be useful, but WITHOUT
12    ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13    FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
14    for more details.
15 
16    You should have received a copy of the GNU General Public License
17    along with LibGTop; see the file COPYING. If not, write to the
18    Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
19    Boston, MA 02110-1301, USA.
20 */
21 
22 #include <config.h>
23 #include <glibtop/error.h>
24 #include <glibtop/swap.h>
25 
26 #include "glibtop_private.h"
27 
28 #include <fcntl.h>
29 
30 static const unsigned long _glibtop_sysdeps_swap =
31 (1L << GLIBTOP_SWAP_TOTAL) + (1L << GLIBTOP_SWAP_USED) +
32 (1L << GLIBTOP_SWAP_FREE);
33 
34 static const unsigned long _glibtop_sysdeps_swap_paging =
35 (1L << GLIBTOP_SWAP_PAGEIN) + (1L << GLIBTOP_SWAP_PAGEOUT);
36 
37 /* Init function. */
38 
39 void
_glibtop_init_swap_s(glibtop * server)40 _glibtop_init_swap_s (glibtop *server)
41 {
42 	server->sysdeps.swap = _glibtop_sysdeps_swap |
43 		_glibtop_sysdeps_swap_paging;
44 }
45 
46 /* Provides information about swap usage. */
47 
48 #define MEMINFO		"/proc/meminfo"
49 #define PROC_STAT	"/proc/stat"
50 
51 void
glibtop_get_swap_s(glibtop * server,glibtop_swap * buf)52 glibtop_get_swap_s (glibtop *server, glibtop_swap *buf)
53 {
54 	char buffer [BUFSIZ], *p;
55 
56 	memset (buf, 0, sizeof (glibtop_swap));
57 
58 	file_to_buffer(server, buffer, sizeof buffer, MEMINFO);
59 
60 	/* Kernel 2.6 with multiple lines */
61 
62 	buf->total = get_scaled(buffer, "SwapTotal:");
63 	buf->free = get_scaled(buffer, "SwapFree:");
64 	buf->used = buf->total - buf->free;
65 
66 	buf->flags = _glibtop_sysdeps_swap;
67 
68 	file_to_buffer (server, buffer, sizeof buffer, PROC_STAT);
69 
70 	p = strstr (buffer, "\nswap");
71 
72 	if(p)
73 	{
74 		p = skip_token (p);
75 
76 		buf->pagein  = strtoull (p, &p, 0);
77 		buf->pageout = strtoull (p, &p, 0);
78 
79 		buf->flags |= _glibtop_sysdeps_swap_paging;
80 	}
81 }
82