summaryrefslogtreecommitdiff
path: root/sysdeps/freebsd/fsusage.c
blob: 0463c8c39acf5b2cf0c6ba32339c19ed0c3d5d85 (plain)
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
#include <config.h>
#include <glibtop.h>
#include <glibtop/error.h>
#include <glibtop/fsusage.h>

#include <glibtop_suid.h>

#include <glib.h>

#include <unistd.h>
#include <sys/param.h>
#include <sys/mount.h>
#include <sys/statvfs.h>
#if 0
#include <libgeom.h>
#include <sys/resource.h>
#include <devstat.h>
#include <sys/devicestat.h>
#endif

#include <stdio.h>
#include <string.h>
#include <stdlib.h>

static const unsigned long _glibtop_sysdeps_fsusage =
(1L << GLIBTOP_FSUSAGE_BLOCKS) + (1L << GLIBTOP_FSUSAGE_BFREE)
+ (1L << GLIBTOP_FSUSAGE_BAVAIL) + (1L << GLIBTOP_FSUSAGE_FILES)
+ (1L << GLIBTOP_FSUSAGE_FFREE) + (1L << GLIBTOP_FSUSAGE_BLOCK_SIZE);

static void
_glibtop_get_fsusage_read_write (glibtop *server, glibtop_fsusage *buf, const char *path)
{
        int result;
        struct statfs sfs;
#if 0
        struct devstat *ds;
        void *sc;
        struct timespec ts;
        struct gprovider *gp;
        struct gident *gid;
        struct gmesh gmp;
        double etime;
        uint64_t ld[2];
#endif

        result = statfs (path, &sfs);

        if (result == -1) {
                glibtop_warn_io_r (server, "statfs");
                return;
        }
#if 0
        ld[0] = 0;
        ld[1] = 0;
        result = geom_gettree (&gmp);
        if (result != 0) {
                glibtop_warn_io_r (server, "geom_gettree = %d", result);
                return;
        }

        result = geom_stats_open ();
        if (result != 0) {
                glibtop_warn_io_r (server, "geom_stats_open()");
                geom_deletetree (&gmp);
                return;
        }

        sc = NULL;
        sc = geom_stats_snapshot_get ();
        if (sc == NULL) {
                glibtop_warn_io_r (server, "geom_stats_snapshot_get()");
                geom_stats_close ();
                geom_deletetree (&gmp);
                return;
        }
        geom_stats_snapshot_timestamp (sc, &ts);
        etime = ts.tv_sec + (ts.tv_nsec * 1e-9);
        geom_stats_snapshot_reset (sc);

        for (;;) {
                ds = geom_stats_snapshot_next (sc);
                if (ds == NULL) {
                        break;
                }
                if (ds->id == NULL) {
                        continue;
                }

                gid = geom_lookupid (&gmp, ds->id);
                if (gid == NULL) {
                        geom_deletetree (&gmp);
                        result = geom_gettree (&gmp);
                        gid = geom_lookupid (&gmp, ds->id);
                }

                if (gid == NULL) {
                        continue;
                }
                if (gid->lg_what == ISCONSUMER) {
                        continue;
                }

                gp = gid->lg_ptr;

                if (!g_str_has_suffix (sfs.f_mntfromname, gp->lg_name)) {
                        continue;
                } else {
                        result = devstat_compute_statistics (ds, NULL, etime,
                                                             DSM_TOTAL_TRANSFERS_READ,
                                                             &ld[0],
                                                             DSM_TOTAL_TRANSFERS_WRITE,
                                                             &ld[1], DSM_NONE);
                        if (result != 0) {
                                glibtop_warn_io_r (server,
                                                   "devstat_compute_statistics()");
                                geom_stats_snapshot_free (sc);
                                geom_stats_close ();
                                geom_deletetree (&gmp);
                                return;
                        }
                        break;
                }
        }

        geom_stats_snapshot_free (sc);
        geom_stats_close ();
        geom_deletetree (&gmp);

        buf->read = ld[0];
        buf->write = ld[1];
#else
        buf->read = sfs.f_syncreads + sfs.f_asyncreads;
        buf->write = sfs.f_syncwrites + sfs.f_asyncwrites;
#endif
	if (buf->read || buf->write) {
                buf->flags |= (1 << GLIBTOP_FSUSAGE_READ) | (1 << GLIBTOP_FSUSAGE_WRITE);
	}
}

void
glibtop_get_fsusage_s(glibtop *server, glibtop_fsusage *buf, const char *path)
{
	struct statvfs fsd;

	memset (buf, 0, sizeof (glibtop_fsusage));

	if (statvfs (path, &fsd) < 0)
		return;

	buf->block_size = fsd.f_frsize;
	buf->blocks = fsd.f_blocks;
	buf->bfree  = fsd.f_bfree;
	buf->bavail = (fsd.f_bavail > fsd.f_bfree) ? 0 : fsd.f_bavail;
	buf->files  = fsd.f_files;
	buf->ffree  = fsd.f_ffree;

	buf->flags = _glibtop_sysdeps_fsusage;

	_glibtop_get_fsusage_read_write(server, buf, path);
}