summaryrefslogtreecommitdiff
path: root/src/report-usage.c
blob: 3b19c8631b33565a6b46e347f5c754d67069e0c3 (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
/*
 *  Copyright (C) 2020 - Vito Caputo - <vcaputo@pengaru.com>
 *
 *  This program is free software: you can redistribute it and/or modify it
 *  under the terms of the GNU General Public License version 3 as published
 *  by the Free Software Foundation.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

#include <assert.h>
#include <inttypes.h>
#include <stdint.h>
#include <stdio.h>

#include <iou.h>
#include <thunk.h>

#include "bootid.h"
#include "humane.h"
#include "journals.h"
#include "machid.h"
#include "report-usage.h"

#include "upstream/journal-def.h"


typedef struct usage_t {
	uint64_t	count_per_type[_OBJECT_TYPE_MAX];
	uint64_t	use_per_type[_OBJECT_TYPE_MAX];
	uint64_t	use_total;
	uint64_t	file_size, file_count;
} usage_t;


THUNK_DEFINE_STATIC(per_data_object, ObjectHeader *, iter_object_header, usage_t *, usage, usage_t *, total_usage)
{
	assert(iter_object_header);
	assert(usage);
	assert(total_usage);

	usage->count_per_type[iter_object_header->type]++;
	usage->use_per_type[iter_object_header->type] += iter_object_header->size;
	usage->use_total += iter_object_header->size;

	total_usage->count_per_type[iter_object_header->type]++;
	total_usage->use_per_type[iter_object_header->type] += iter_object_header->size;
	total_usage->use_total += iter_object_header->size;

	return 1;
}


THUNK_DEFINE_STATIC(per_journal, iou_t *, iou, journal_t **, journal_iter, usage_t *, total_usage, unsigned *, n_journals)
{
	struct {
		journal_t		*journal;
		Header			header;
		usage_t			usage;
		uint64_t		iter_offset;
		ObjectHeader		iter_object_header;
	} *foo;

	thunk_t		*closure;
	struct stat	st;
	int		r;

	assert(iou);
	assert(journal_iter);
	assert(total_usage);

	/* XXX: io_uring has a STATX opcode, so this too could be async */
	r = fstat((*journal_iter)->fd, &st);
	if (r < 0)
		return r;

	closure = THUNK_ALLOC(per_data_object, (void **)&foo, sizeof(*foo));
	foo->journal = *journal_iter;
	foo->usage.file_size = st.st_size;

	total_usage->file_size += st.st_size;
	(*n_journals)++;

	return thunk_mid(journal_get_header(iou, &foo->journal, &foo->header, THUNK(
			journal_iter_objects(iou, &foo->journal, &foo->header, &foo->iter_offset, &foo->iter_object_header, THUNK_INIT(
				per_data_object(closure, &foo->iter_object_header, &foo->usage, total_usage))))));
}


/* print the amount of space used by various objects per journal, and sum totals */
int jio_report_usage(iou_t *iou, int argc, char *argv[])
{
	usage_t		aggregate_usage = {};
	char		*machid;
	journals_t	*journals;
	journal_t	*journal_iter;
	unsigned	n_journals = 0;
	humane_t	h1, h2;
	int		r;

	r = machid_get(iou, &machid, THUNK(
		journals_open(iou, &machid, O_RDONLY, &journals, THUNK(
			journals_for_each(&journals, &journal_iter, THUNK(
				per_journal(iou, &journal_iter, &aggregate_usage, &n_journals)))))));
	if (r < 0)
		return r;

	r = iou_run(iou);
	if (r < 0)
		return r;

	printf("Per-object-type usage:\n");
	for (int i = 0; i < _OBJECT_TYPE_MAX; i++)
		printf("%16s: [%"PRIu64"] %s\n",
			journal_object_type_str(i),
			aggregate_usage.count_per_type[i],
			humane_bytes(&h1,
			aggregate_usage.use_per_type[i]));

	printf("Aggregate object usage: %s of %s spanning %u journal files\n",
		humane_bytes(&h1, aggregate_usage.use_total),
		humane_bytes(&h2, aggregate_usage.file_size),
		n_journals);

	return 0;
}
© All Rights Reserved