summaryrefslogtreecommitdiff
path: root/src/report-layout.c
blob: e5af386027a5bdeac95aeb2ca2657f1932ccd363 (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
/*
 *  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 <stdio_ext.h>

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

#include "journals.h"
#include "machid.h"
#include "report-layout.h"

#include "upstream/journal-def.h"


static const char type_map[_OBJECT_TYPE_MAX] = {
	[OBJECT_UNUSED] = '?',
	[OBJECT_DATA] = 'd',
	[OBJECT_FIELD] = 'f',
	[OBJECT_ENTRY] = 'e',
	[OBJECT_DATA_HASH_TABLE] = 'D',
	[OBJECT_FIELD_HASH_TABLE] = 'F',
	[OBJECT_ENTRY_ARRAY] = 'A',
	[OBJECT_TAG] = 't',
};

/* TODO: this should be either argv settable or just determined at runtime */
#define PAGE_SIZE 4096

THUNK_DEFINE_STATIC(per_data_object, uint64_t *, iter_offset, ObjectHeader *, iter_object_header, FILE *, out)
{
	char		boundary_marker[22] = "";
	char		alignment_marker[3] = "";
	uint64_t	off, this_page, next_page, page_delta, aligned_delta;

	assert(iter_offset);
	assert(iter_object_header);
	assert(out);

	off = *iter_offset;

	if (!off) {
		fprintf(out, "\n");
		fclose(out);
		return 0;
	}


	this_page = off & ~(PAGE_SIZE-1);
	next_page = (off + iter_object_header->size + PAGE_SIZE-1) & ~(PAGE_SIZE-1);
	page_delta = next_page - this_page;

	if (page_delta > PAGE_SIZE * 2)
		snprintf(boundary_marker, sizeof(boundary_marker), "|%"PRIu64"|", page_delta / PAGE_SIZE - 1);
	else if (page_delta > PAGE_SIZE)
		snprintf(boundary_marker, sizeof(boundary_marker), "|");

	aligned_delta = ALIGN64(iter_object_header->size) - iter_object_header->size;

	if (aligned_delta > 1)
		snprintf(alignment_marker, sizeof(alignment_marker), "+%"PRIu64, aligned_delta);
	else if (aligned_delta)
		snprintf(alignment_marker, sizeof(alignment_marker), "+");

	fprintf(out, "%s%c%s%"PRIu64"%s ",
		this_page == off ? "| " : "",
		type_map[iter_object_header->type],
		boundary_marker,
		iter_object_header->size,
		alignment_marker);

	return 0;
}


THUNK_DEFINE_STATIC(per_journal, iou_t *, iou, journal_t **, journal_iter)
{
	struct {
		journal_t	*journal;
		Header		header;
		uint64_t	iter_offset;
		ObjectHeader	iter_object_header;
		FILE		*out;
	} *foo;

	thunk_t		*closure;
	char		*fname;
	FILE		*f;

	assert(iou);
	assert(journal_iter);

	fname = malloc(strlen((*journal_iter)->name) + sizeof(".layout"));
	if (!fname)
		return -ENOMEM;

	sprintf(fname, "%s.layout", (*journal_iter)->name);
	f = fopen(fname, "w+");
	free(fname);
	if (!f)
		return -errno;

	__fsetlocking(f, FSETLOCKING_BYCALLER);

	fprintf(f, "Layout for \"%s\"\n", (*journal_iter)->name);
	fprintf(f,
		"Legend:\n"
		"%c     OBJECT_UNUSED\n"
		"%c     OBJECT_DATA\n"
		"%c     OBJECT_FIELD\n"
		"%c     OBJECT_ENTRY\n"
		"%c     OBJECT_DATA_HASH_TABLE\n"
		"%c     OBJECT_FIELD_HASH_TABLE\n"
		"%c     OBJECT_ENTRY_ARRAY\n"
		"%c     OBJECT_TAG\n\n"
		"|N|    object spans N page boundaries (page size used=%u)\n"
		"|      single page boundary\n"
		"+N     N bytes of alignment padding\n"
		"+      single byte of alignment padding\n\n",

		type_map[OBJECT_UNUSED],
		type_map[OBJECT_DATA],
		type_map[OBJECT_FIELD],
		type_map[OBJECT_ENTRY],
		type_map[OBJECT_DATA_HASH_TABLE],
		type_map[OBJECT_FIELD_HASH_TABLE],
		type_map[OBJECT_ENTRY_ARRAY],
		type_map[OBJECT_TAG],
		PAGE_SIZE);

	closure = THUNK_ALLOC(per_data_object, (void **)&foo, sizeof(*foo));
	foo->journal = *journal_iter;
	foo->out = f;

	return 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_offset, &foo->iter_object_header, foo->out)))));
}


/* print the layout of contents per journal */
int jio_report_layout(iou_t *iou, int argc, char *argv[])
{
	char		*machid;
	journals_t	*journals;
	journal_t	*journal_iter;
	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)))))));
	if (r < 0)
		return r;

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

	return 0;
}
© All Rights Reserved