aboutsummaryrefslogtreecommitdiffstats
path: root/src/statfile.c
blob: ca78b79e9d499e622c3c95f71e9d4f450fb1bff2 (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
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
#include <sys/types.h>
#include <fcntl.h>
#include <sys/stat.h>
#include <errno.h>
#include <time.h>
#include <limits.h>
#include <sys/mman.h>
#include "config.h"
#include <string.h>
#include <unistd.h>
#include <glib.h>

#include "statfile.h"
#include "main.h"

/* Check whether specified file is statistic file and calculate its len in blocks */
static int
statfile_pool_check (stat_file_t *file)
{
	struct stat_file *f;
	char *c;

	if (!file || !file->map) {
		return -1;
	}

	if (file->len < sizeof (struct stat_file)) {
		msg_info ("statfile_pool_check: file %s is too short to be stat file: %zd", file->filename, file->len);
		return -1;
	}

	f = (struct stat_file *)file->map;
	c = f->header.magic;
	/* Check magic and version */
	if (*c++ != 'r' || *c++ != 's' || *c++ != 'd' ||
		/* version */ *c++ != 1 || *c != 0) {
		msg_info ("statfile_pool_check: file %s is invalid stat file", file->filename);
		return -1;
	}
	
	if ((file->len - sizeof (f->header)) % sizeof (struct stat_file_block) != 0) {
		msg_info ("statfile_pool_check: file %s does not contain integer count of stat blocks", file->filename);
		return -1;
	}

	file->blocks = (file->len - sizeof (f->header)) / sizeof (struct stat_file_block);
	return 0;
}


struct expiration_data {
	statfile_pool_t *pool;
	uint64_t oldest;
	char *filename;
};

static void
pool_expiration_callback (gpointer key, gpointer value, void *data)
{
	struct expiration_data *exp = data;
	stat_file_t *file = (stat_file_t *)value;

	if ((uint64_t)file->access_time < exp->oldest) {
		exp->oldest = file->access_time;
		exp->filename = file->filename;
	}
}

static int
statfile_pool_expire (statfile_pool_t *pool)
{
	struct expiration_data exp;

	if (g_hash_table_size (pool->files) == 0) {
		return -1;
	}

	exp.pool = pool;
	exp.oldest = ULLONG_MAX;
	exp.filename = NULL;
	g_hash_table_foreach (pool->files, pool_expiration_callback, &exp);

	if (exp.filename) {
		statfile_pool_close (pool, exp.filename);
	}
}

statfile_pool_t* 
statfile_pool_new (size_t max_size)
{
	statfile_pool_t *new;

	new = g_malloc (sizeof (statfile_pool_t));
	bzero (new, sizeof (statfile_pool_t));
	new->pool = memory_pool_new (memory_pool_get_size ());
	new->max = max_size;
	new->files = g_hash_table_new (g_str_hash, g_str_equal);
	memory_pool_add_destructor (new->pool, (pool_destruct_func)g_hash_table_destroy, new->files);

	return new;
}

int 
statfile_pool_open (statfile_pool_t *pool, char *filename)
{
	struct stat st;
	stat_file_t *new_file;	
	
	if (g_hash_table_lookup (pool->files, filename) != NULL) {
		msg_info ("statfile_pool_open: file %s is already opened", filename);
		return 0;
	}

	if (stat (filename, &st) == -1) {
		msg_info ("statfile_pool_open: cannot stat file %s, error %m, %d", filename, errno);
		return -1;
	}
	
	if (st.st_size > pool->max) {
		msg_info ("statfile_pool_open: cannot attach file to pool, too large: %zd", (size_t)st.st_size);
		return -1;
	}

	while (pool->max <= pool->occupied + st.st_size) {
		if (statfile_pool_expire (pool) == -1) {
			/* Failed to find any more free space in pool */
			msg_info ("statfile_pool_open: expiration for pool failed, opening file %s failed", filename);
			return -1;
		}
	}

	new_file = memory_pool_alloc (pool->pool, sizeof (stat_file_t));
	if ((new_file->fd = open (filename, O_RDWR)) == -1 ) {
		msg_info ("statfile_pool_open: cannot open file %s, error %m, %d", filename, errno);
		return -1;
	}
	
	if ((new_file->map = mmap (NULL, st.st_size, PROT_READ | PROT_WRITE, MAP_SHARED, new_file->fd, 0)) == NULL) {
		close (new_file->fd);
		msg_info ("statfile_pool_open: cannot mmap file %s, error %m, %d", filename, errno);
		return -1;
	
	}
	
	/* XXX: this is temporary copy of name to avoid strdup early */
	new_file->filename = filename;
	new_file->len = st.st_size;
	if (statfile_pool_check (new_file) == -1) {
		return -1;
	}

	pool->occupied += st.st_size;
	new_file->filename = memory_pool_strdup (pool->pool, filename);
	new_file->open_time = time (NULL);
	new_file->access_time = new_file->open_time;
	new_file->lock = memory_pool_get_mutex (pool->pool);
	g_hash_table_insert (pool->files, new_file->filename, new_file);

	return 0;
}

int
statfile_pool_close (statfile_pool_t *pool, char *filename) 
{
	stat_file_t *file;	
	
	if ((file = g_hash_table_lookup (pool->files, filename)) == NULL) {
		msg_info ("statfile_pool_open: file %s is not opened", filename);
		return -1;
	}
	
	if (file->lock) {
		memory_pool_lock_mutex (file->lock);
	}

	if (file->map) {
		munmap (file->map, file->len);
	}
	if (file->fd != -1) {
		close (file->fd);
	}
	pool->occupied -= file->len;
	g_hash_table_remove (pool->files, file->filename);
}

int
statfile_pool_create (statfile_pool_t *pool, char *filename, size_t blocks)
{
	static struct stat_file_header header = {
		{'r', 's', 'd'},
		{1, 0},
		{0, 0, 0},
		0
	};
	static struct stat_file_block block = {0, 0, 0, 0};
	int fd;
	
	if (g_hash_table_lookup (pool->files, filename) != NULL) {
		msg_info ("statfile_pool_create: file %s is already opened", filename);
		return 0;
	}

	if ((fd = open (filename, O_RDWR | O_TRUNC | O_CREAT, S_IWUSR | S_IRUSR)) == -1 ) {
		msg_info ("statfile_pool_create: cannot create file %s, error %m, %d", filename, errno);
		return -1;
	}

	header.create_time = (uint64_t)time (NULL);
	if (write (fd, &header, sizeof (header)) == -1) {
		msg_info ("statfile_pool_create: cannot write header to file %s, error %m, %d", filename, errno);
		close (fd);
		return -1;
	}
	
	while (blocks --) {
		if (write (fd, &block, sizeof (block)) == -1) {
			msg_info ("statfile_pool_create: cannot write block to file %s, error %m, %d", filename, errno);
			close (fd);
			return -1;
		}
	}

	close (fd);
	
	return 0;
}

static void
pool_delete_callback (gpointer key, gpointer value, void *data)
{
	statfile_pool_t *pool = (statfile_pool_t *)data;

	statfile_pool_close (pool, (char *)key);
}

void
statfile_pool_delete (statfile_pool_t *pool)
{
	g_hash_table_foreach (pool->files, pool_delete_callback, pool);
	memory_pool_delete (pool->pool);
	g_free (pool);
}

void
statfile_pool_lock_file (statfile_pool_t *pool, char *filename) 
{
	stat_file_t *file;

	if ((file = g_hash_table_lookup (pool->files, filename)) == NULL) {
		msg_info ("statfile_pool_lock_file: file %s is not opened", filename);
		return;
	}

	memory_pool_lock_mutex (file->lock);
}

void
statfile_pool_unlock_file (statfile_pool_t *pool, char *filename) 
{
	stat_file_t *file;

	if ((file = g_hash_table_lookup (pool->files, filename)) == NULL) {
		msg_info ("statfile_pool_unlock_file: file %s is not opened", filename);
		return;
	}

	memory_pool_unlock_mutex (file->lock);
}

float
statfile_pool_get_block (statfile_pool_t *pool, char *filename, uint32_t h1, uint32_t h2, time_t now)
{
	stat_file_t *file;
	struct stat_file_block *block;
	struct stat_file_header *header;
	unsigned int i, blocknum;
	u_char *c;
	
	if ((file = g_hash_table_lookup (pool->files, filename)) == NULL) {
		msg_info ("statfile_pool_get_block: file %s is not opened", filename);
		return 0;
	}
	
	file->access_time = now;
	if (!file->map) {
		return 0;
	}
	
	blocknum = h1 % file->blocks;
	header = (struct stat_file_header *)file->map;
	c = (u_char *)file->map + sizeof (struct stat_file_header) + blocknum * sizeof (struct stat_file_block);
	block = (struct stat_file_block *)c;

	for (i = 0; i < CHAIN_LENGTH; i ++) {
		if (i + blocknum > file->blocks) {
			break;
		}
		msg_debug ("statfile_pool_get_block: test block with h1=%u, h2=%u, number %u in chain %u", block->hash1, block->hash2, i, blocknum);
		if (block->hash1 == h1 && block->hash2 == h2) {
			msg_debug ("statfile_pool_get_block: found block with h1=%u, h2=%u, number %u in chain %u", h1, h2, i, blocknum);
			block->last_access = now - (time_t)header->create_time;
			return block->value;
		}
		c += sizeof (struct stat_file_block);
		block = (struct stat_file_block *)c;
	}

	msg_debug ("statfile_pool_get_block: block with h1=%u, h2=%u, not found in chain %u", h1, h2, blocknum);

	return 0;
}

void
statfile_pool_set_block (statfile_pool_t *pool, char *filename, uint32_t h1, uint32_t h2, time_t now, float value)
{
	stat_file_t *file;
	struct stat_file_block *block, *to_expire = NULL;
	struct stat_file_header *header;
	unsigned int i, blocknum, oldest = 0;
	u_char *c;
	
	if ((file = g_hash_table_lookup (pool->files, filename)) == NULL) {
		msg_info ("statfile_pool_set_block: file %s is not opened", filename);
		return;
	}
	
	file->access_time = now;
	if (!file->map) {
		return;
	}
	
	blocknum = h1 % file->blocks;
	header = (struct stat_file_header *)file->map;
	c = (u_char *)file->map + sizeof (struct stat_file_header) + blocknum * sizeof (struct stat_file_block);
	block = (struct stat_file_block *)c;

	for (i = 0; i < CHAIN_LENGTH; i ++) {
		if (i + blocknum > file->blocks) {
			/* Need to expire some block in chain */
			msg_debug ("statfile_pool_set_block: chain %u is full, starting expire", blocknum);
			break;
		}
		/* Check whether we have a free block in chain */
		if (block->hash1 == 0 && block->hash2 == 0) {
			/* Write new block here */
			msg_debug ("statfile_pool_set_block: found free block %u in chain %u, set h1=%u, h2=%u", i, blocknum, h1, h2);
			block->hash1 = h1;
			block->hash2 = h2;
			block->value = value;
			block->last_access = now - (time_t)header->create_time;
			return;
		}
		if (block->last_access > oldest) {
			to_expire = block;
		}
		c += sizeof (struct stat_file_block);
		block = (struct stat_file_block *)c;
	}

	/* Try expire some block */
	if (to_expire) {
		block = to_expire;
	}
	else {
		/* Expire first block in chain */
		c = (u_char *)file->map + sizeof (struct stat_file_header) + blocknum * sizeof (struct stat_file_block);
		block = (struct stat_file_block *)c;
	}
	block->last_access = now - (time_t)header->create_time;
	block->hash1 = h1;
	block->hash2 = h2;
	block->value = value;
}

int
statfile_pool_is_open (statfile_pool_t *pool, char *filename)
{
	return g_hash_table_lookup (pool->files, filename) != NULL;
}