summaryrefslogtreecommitdiff
path: root/sysdeps/linux/fsusage.c
blob: 35b21ceef25d42ac18596627f35710a0f31b119d (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
#include <config.h>
#include <glibtop.h>
#include <glibtop/fsusage.h>

#include "glibtop_private.h"

#include <glib.h>

#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <linux/kdev_t.h>

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

void
_glibtop_linux_get_fsusage_read_write(glibtop *server,
				      glibtop_fsusage *buf,
				      const char *path) G_GNUC_INTERNAL;

/*
 * Linux 2.6.x
 * linux/Documentation/iostats.txt
 */




static char *
get_partition(const char *mountpoint)
{
	FILE *partitions;
	char *name = NULL;
	char line[1024];
	struct stat statb;

	if(stat(mountpoint, &statb) == -1)
		return NULL;

	if((partitions = fopen("/proc/partitions", "r")) == NULL)
		return NULL;

	while(fgets(line, sizeof line, partitions))
	{
		unsigned major, minor;
		char dev[32];

		if(sscanf(line, "%u %u %*u %31s", &major, &minor, dev) != 3)
			continue;

		if(MKDEV(major, minor) != statb.st_dev)
			continue;

		name = g_strdup(dev);
		break;
	}

	fclose(partitions);
	return name;
}


static void
get_sys_path(const char *device, char **stat_path, const char **parse_format)
{
	if(g_str_has_prefix(device, "hd") || g_str_has_prefix(device, "sd"))
	{
		char *prefix;
		char *path;
		size_t offset;

		offset = strcspn(device, "0123456789");

		prefix = g_strdup(device);
		prefix [offset] = '\0';

		path = g_strdup_printf("/sys/block/%s/%s/stat",
				       prefix, device);

		g_free(prefix);

		*stat_path = path;
		*parse_format = "%*llu %llu %*llu %llu";
	}
	else
	{
		*stat_path = g_strdup_printf("/sys/block/%s/stat", device);
		*parse_format = "%*llu %*llu %llu %*llu %*llu %*llu %llu";
	}
}



static void linux_2_6_0(glibtop *server, glibtop_fsusage *buf, const char *path)
{
	char *device;
	char *filename;
	const char *format;
	int ret;
	char buffer[BUFSIZ];

	device = get_partition(path);
	if(!device) return;

	get_sys_path(device, &filename, &format);
	g_free(device);

	ret = try_file_to_buffer(buffer, filename);

	if(ret < 0) return;

	if (sscanf(buffer, format, &buf->read, &buf->write) != 2) {
		glibtop_warn_io_r(server, "Could not parse %s", filename);
		return;
	}

	g_free(filename);

	buf->flags |= (1 << GLIBTOP_FSUSAGE_READ) | (1 << GLIBTOP_FSUSAGE_WRITE);
}


static void linux_2_4_0(glibtop *server, glibtop_fsusage *buf, const char *path)
{
}


void G_GNUC_INTERNAL
_glibtop_linux_get_fsusage_read_write(glibtop *server,
				      glibtop_fsusage *buf,
				      const char *path)
{
  if(server->os_version_code >= LINUX_VERSION_CODE(2, 6, 0))
    {
      linux_2_6_0(server, buf, path);
    }
  else if(server->os_version_code >= LINUX_VERSION_CODE(2, 4, 0))
    {
      linux_2_4_0(server, buf, path);
    }
}