aboutsummaryrefslogtreecommitdiffstats
path: root/tools/f2fstat.c
blob: 89dd7ff4ba302c047303ce59914a7b8152ab0e03 (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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
#include <fcntl.h>
#include <libgen.h>

#ifdef ANDROID
#include "include/f2fs_version.h"
#endif

#ifdef DEBUG
#define dbg(fmt, args...)	printf(fmt, __VA_ARGS__);
#else
#define dbg(fmt, args...)
#endif

/*
 * f2fs status
 */
#define F2FS_STATUS	"/sys/kernel/debug/f2fs/status"

#define KEY_NODE	0x00000001
#define KEY_META	0x00000010

unsigned long util;
unsigned long used_node_blks;
unsigned long used_data_blks;
//unsigned long inline_inode;

unsigned long free_segs;
unsigned long valid_segs;
unsigned long dirty_segs;
unsigned long prefree_segs;

unsigned long gc, bg_gc;
unsigned long cp;
unsigned long gc_data_blks;
unsigned long gc_node_blks;

//unsigned long extent_hit_ratio;

unsigned long dirty_node, node_kb;
unsigned long dirty_dents;
unsigned long dirty_meta, meta_kb;
unsigned long nat_caches;
unsigned long dirty_sit;

unsigned long free_nids;

unsigned long ssr_blks;
unsigned long lfs_blks;
unsigned long memory_kb;

struct options {
	int delay;
	int interval;
	char partname[32];
};

struct mm_table {
	const char *name;
	unsigned long *val;
	int flag;
};

static int compare_mm_table(const void *a, const void *b)
{
	dbg("[COMPARE] %s, %s\n", ((struct mm_table *)a)->name, ((struct mm_table *)b)->name);
	return strcmp(((struct mm_table *)a)->name, ((struct mm_table *)b)->name);
}

static inline void remove_newline(char **head)
{
again:
	if (**head == '\n') {
		*head = *head + 1;
		goto again;
	}
}

void f2fstat(struct options *opt)
{
	int fd;
	int ret;
	char keyname[32];
	char buf[4096];
	struct mm_table key = { keyname, NULL };
	struct mm_table *found;
	int f2fstat_table_cnt;
	char *head, *tail;
	int found_cnt = 0;

	static struct mm_table f2fstat_table[] = {
		{ "  - Data",		&used_data_blks,	0 },
		{ "  - Dirty",		&dirty_segs,		0 },
		{ "  - Free",		&free_segs,		0 },
		{ "  - NATs",		&nat_caches,		0 },
		{ "  - Node",		&used_node_blks,	0 },
		{ "  - Prefree",	&prefree_segs,		0 },
		{ "  - SITs",		&dirty_sit,		0 },
		{ "  - Valid",		&valid_segs,		0 },
		{ "  - dents",		&dirty_dents,		0 },
		{ "  - free_nids",	&free_nids,		0 },
		{ "  - meta",		&dirty_meta,		KEY_META },
		{ "  - nodes",		&dirty_node,		KEY_NODE },
		{ "CP calls",		&cp,			0 },
		{ "GC calls",		&gc,			0 },
		{ "LFS",		&lfs_blks,		0 },
		{ "Memory",		&memory_kb,		0 },
		{ "SSR",		&ssr_blks,		0 },
		{ "Utilization",	&util,			0 },
	};

	f2fstat_table_cnt = sizeof(f2fstat_table)/sizeof(struct mm_table);

	fd = open(F2FS_STATUS, O_RDONLY);
	if (fd < 0) {
		perror("open " F2FS_STATUS);
		exit(EXIT_FAILURE);
	}

	ret = read(fd, buf, 4096);
	if (ret < 0) {
		perror("read " F2FS_STATUS);
		exit(EXIT_FAILURE);
	}
	buf[ret] = '\0';

	head = buf;

	if (opt->partname[0] != '\0') {
		head = strstr(buf, opt->partname);
		if (head == NULL)
			exit(EXIT_FAILURE);
	}

	for (;;) {
		remove_newline(&head);
		tail = strchr(head, ':');
		if (!tail)
			break;
		*tail = '\0';
		if (strlen(head) >= sizeof(keyname)) {
			dbg("[OVER] %s\n", head);
			*tail = ':';
			tail = strchr(head, '\n');
			head = tail + 1;
			continue;
		}

		strcpy(keyname, head);

		found = bsearch(&key, f2fstat_table, f2fstat_table_cnt, sizeof(struct mm_table), compare_mm_table);
		dbg("[RESULT] %s (%s)\n", head, (found) ? "O" : "X");
		head = tail + 1;
		if (!found)
			goto nextline;

		*(found->val) = strtoul(head, &tail, 10);
		if (found->flag) {
			int npages;
			tail = strstr(head, "in");
			head = tail + 2;
			npages = strtoul(head, &tail, 10);
			switch (found->flag & (KEY_NODE | KEY_META)) {
			case KEY_NODE:
				node_kb = npages * 4;
				break;
			case KEY_META:
				meta_kb = npages * 4;
				break;
			}
		}
		if (++found_cnt == f2fstat_table_cnt)
			break;
nextline:
		tail = strchr(head, '\n');
		if (!tail)
			break;
		head =  tail + 1;
	}

	close(fd);
}

void usage(void)
{
	printf("Usage: f2fstat [option]\n"
			"    -d    delay (secs)\n"
			"    -i    interval of head info\n"
			"    -p    partition name (e.g. /dev/sda3)\n");
	exit(EXIT_FAILURE);
}

void parse_option(int argc, char *argv[], struct options *opt)
{
#ifndef ANDROID
	char option;
	const char *option_string = "d:i:p:h";
#else
	int option;
	const char *option_string = "d:i:p:";
#endif

	while ((option = getopt(argc, argv, option_string)) != EOF) {
		switch (option) {
		case 'd':
			opt->delay = atoi(optarg);
			break;
		case 'i':
			opt->interval = atoi(optarg);
			break;
		case 'p':
			strcpy(opt->partname, basename(optarg));
			break;
		default:
			usage();
			break;
		}
	}
}

void print_head(void)
{
	fprintf(stderr, "---utilization--- -----------main area-------- ---------balancing async------- ---gc--- ---alloc--- -----memory-----\n");
	fprintf(stderr, "util  node   data   free  valid  dirty prefree node  dent meta  sit   nat fnid  cp   gc    ssr    lfs  total  node  meta\n");
}

#ifndef ANDROID
int main(int argc, char *argv[])
#else
int f2fstat_main(int argc, char *argv[])
#endif
{
	char format[] = "%3ld %6ld %6ld %6ld %6ld %6ld %6ld %5ld %5ld %3ld %5ld %5ld %3ld %3ld %3ld %6ld %6ld %6ld %6ld %6ld\n";
	int head_interval;
	struct options opt = {
		.delay = 1,
		.interval = 20,
		.partname = { 0, },
	};

	printf("\n\tF2FS-tools: f2fstat Ver: %s (%s)\n\n",
	    F2FS_TOOLS_VERSION, F2FS_TOOLS_DATE);

	parse_option(argc, argv, &opt);
	head_interval = opt.interval;

	print_head();
	while (1) {
		if (head_interval-- == 0) {
			print_head();
			head_interval = opt.interval;
		}

		f2fstat(&opt);

		fprintf(stderr, format, util, used_node_blks, used_data_blks,
			free_segs, valid_segs, dirty_segs, prefree_segs,
			dirty_node, dirty_dents, dirty_meta, dirty_sit, nat_caches, free_nids,
			cp, gc, ssr_blks, lfs_blks, memory_kb, node_kb, meta_kb);

		sleep(opt.delay);
	}

	return 0;
}