summaryrefslogtreecommitdiffstats
path: root/xlators/performance/stat-prefetch/src/stat-prefetch.c
blob: c6bf1e684cf40ddf6cb6f954fdc411a0f1c96302 (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
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
/*
   Copyright (c) 2006-2009 Z RESEARCH, Inc. <http://www.zresearch.com>
   This file is part of GlusterFS.

   GlusterFS is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published
   by the Free Software Foundation; either version 3 of the License,
   or (at your option) any later version.

   GlusterFS 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/>.
*/

#ifndef _CONFIG_H
#define _CONFIG_H
#include "config.h"
#endif

#include "glusterfs.h"
#include "stat-prefetch.h"
#include "dict.h"
#include "xlator.h"
#include <sys/time.h>

struct sp_cache {
  struct sp_cache *next;
  struct sp_cache *prev;
  pid_t pid;
  long long tv_time;
  char *dirname;
  dir_entry_t entries;
  int32_t count;
  pthread_mutex_t lock;
};

static void
stat_prefetch_cache_flush (struct sp_cache *cache, int32_t force)
{
  struct sp_cache *trav;
  struct timeval tv;
  long long tv_time;

  gettimeofday (&tv, NULL);
  tv_time = (tv.tv_usec + (tv.tv_sec * 1000000));

  pthread_mutex_lock (&cache->lock);

  trav = cache->next;
  while (trav != cache) {
    struct sp_cache *next = trav->next;
    {
      if (tv_time > trav->tv_time || force) {
	gf_log ("stat-prefetch",
		GF_LOG_DEBUG,
		"flush on: %s",
		trav->dirname);
	dir_entry_t *entries;

	trav->prev->next = trav->next;
	trav->next->prev = trav->prev;

	entries = trav->entries.next;

	while (entries) {
	  dir_entry_t *nextentry = entries->next;
	  {
	    free (entries->name);
	    free (entries);
	  }
	  entries = nextentry;
	}
	free (trav->dirname);
	free (trav);
      }
    }
    trav = next;
  }

  pthread_mutex_unlock (&cache->lock);
}

static int32_t
stat_prefetch_cache_fill (struct sp_cache *cache,
			  pid_t pid,
			  char *dirname,
			  dir_entry_t *entries)
{
  struct sp_cache *trav;
  struct timeval tv;

  pthread_mutex_unlock (&cache->lock);
  trav = cache->next;
  while (trav != cache) {
    //    if (trav->pid == pid && !strcmp (trav->dirname, dirname)) {
    if (!strcmp (trav->dirname, dirname)) {
      break;
    }
    trav = trav->next;
  }

  if (trav == cache) {
    trav = CALLOC (1, sizeof (*trav));
    ERR_ABORT (trav);
    trav->pid = pid;
    trav->dirname = dirname;

    trav->prev = cache->prev;
    trav->next = cache;
    trav->next->prev = trav;
    trav->prev->next = trav;
  } else {
    free (dirname);
  }

  while (trav->entries.next) {
    dir_entry_t *tmp = trav->entries.next;

    trav->entries.next = trav->entries.next->next;
    free (tmp->name);
    free (tmp);
  }
  trav->entries.next = entries->next;
  entries->next = NULL;

  gettimeofday (&tv, NULL);
  trav->tv_time = (tv.tv_usec + (tv.tv_sec * 1000000)) + cache->tv_time;

  pthread_mutex_unlock (&cache->lock);
  return 0;
}

static int32_t
stat_prefetch_cache_lookup (struct sp_cache *cache,
			    pid_t pid,
			    const char *path,
			    struct stat *buf)
{
  struct sp_cache *trav;
  char *dirname = strdup (path);
  char *filename = strrchr (dirname, '/');
  dir_entry_t *entries;
  dir_entry_t *prev = NULL;

  *filename = '\0';
  filename ++;

  pthread_mutex_lock (&cache->lock);
  trav = cache->next;
  while (trav != cache) {
    //    if ((trav->pid == pid) && !strcmp (dirname, trav->dirname))
    if (!strcmp (dirname, trav->dirname))
      break;
    trav = trav->next;
  }
  if (trav == cache) {
    free (dirname);
    pthread_mutex_unlock (&cache->lock);
    return -1;
  }

  entries = trav->entries.next;
  prev = &trav->entries;
  while (entries) {
    if (!strcmp (entries->name, filename))
      break;
    prev = entries;
    entries = entries->next;
  }
  if (!entries) {
    free (dirname);
    pthread_mutex_unlock (&cache->lock);
    return -1;
  }

  *buf = entries->buf;
  prev->next = entries->next;
  free (entries->name);
  free (entries);
  free (dirname);

  pthread_mutex_unlock (&cache->lock);

  return 0;
}

			    
int32_t
stat_prefetch_readdir_cbk (call_frame_t *frame,
			   void *cookie,
			   xlator_t *this,
			   int32_t op_ret,
			   int32_t op_errno,
			   dir_entry_t *entries,
			   int32_t count)
{
  char *path = frame->local;
  pid_t pid = frame->root->pid;
  frame->local = NULL;

  STACK_UNWIND (frame, op_ret, op_errno, entries, count);

  if (op_ret == 0)
    stat_prefetch_cache_fill (this->private,
			      pid,
			      path,
			      entries);
  else
    free (path);

  return 0;
}

int32_t
stat_prefetch_readdir (call_frame_t *frame,
		       xlator_t *this,
		       const char *path)
{
  stat_prefetch_cache_flush (this->private, 0);

  frame->local = strdup (path);
  STACK_WIND (frame,
	      stat_prefetch_readdir_cbk,
	      FIRST_CHILD(this),
	      FIRST_CHILD(this)->fops->readdir,
	      path);
  return 0;
}


int32_t
stat_prefetch_getattr_cbk (call_frame_t *frame,
			   void *cookie,
			   xlator_t *this,
			   int32_t op_ret,
			   int32_t op_errno,
			   struct stat *buf)
{
  STACK_UNWIND (frame, op_ret, op_errno, buf);
  return 0;
}

int32_t
stat_prefetch_getattr (call_frame_t *frame,
		       struct xlator *this,
		       const char *path)
{
  struct stat buf;
  pid_t pid = frame->root->pid;
  stat_prefetch_cache_flush (this->private, 0);

  if (stat_prefetch_cache_lookup (this->private,
				  pid,
				  path,
				  &buf) == 0) {
    STACK_UNWIND (frame, 0, 0, &buf);
    return 0;
  }

  STACK_WIND (frame,
	      stat_prefetch_getattr_cbk,
	      FIRST_CHILD(this),
	      FIRST_CHILD(this)->fops->getattr,
	      path);

  return 0;
}


int32_t
stat_prefetch_unlink_cbk (call_frame_t *frame,
                          void *cookie,
                          xlator_t *this,
                          int32_t op_ret,
                          int32_t op_errno)
{
  STACK_UNWIND (frame, op_ret, op_errno);
  return 0;
}

int32_t
stat_prefetch_unlink (call_frame_t *frame,
                      struct xlator *this,
                      const char *path)
{
  stat_prefetch_cache_flush (this->private, 1);

  STACK_WIND (frame,
              stat_prefetch_unlink_cbk,
              FIRST_CHILD(this),
              FIRST_CHILD(this)->fops->unlink,
              path);

  return 0;
}


int32_t
stat_prefetch_chmod_cbk (call_frame_t *frame,
			 void *cookie,
			 xlator_t *this,
			 int32_t op_ret,
			 int32_t op_errno,
			 struct stat *buf)
{
  STACK_UNWIND (frame, op_ret, op_errno, buf);
  return 0;
}

int32_t
stat_prefetch_chmod (call_frame_t *frame,
		     struct xlator *this,
		     const char *path,
		     mode_t mode)
{
  stat_prefetch_cache_flush (this->private, 1);

  STACK_WIND (frame,
              stat_prefetch_chmod_cbk,
              FIRST_CHILD(this),
              FIRST_CHILD(this)->fops->chmod,
              path,
	      mode);

  return 0;
}


int32_t
stat_prefetch_chown_cbk (call_frame_t *frame,
			 void *cookie,
			 xlator_t *this,
			 int32_t op_ret,
			 int32_t op_errno,
			 struct stat *buf)
{
  STACK_UNWIND (frame, op_ret, op_errno, buf);
  return 0;
}

int32_t
stat_prefetch_chown (call_frame_t *frame,
		     struct xlator *this,
		     const char *path,
		     uid_t uid,
		     gid_t gid)
{
  stat_prefetch_cache_flush (this->private, 1);

  STACK_WIND (frame,
              stat_prefetch_chown_cbk,
              FIRST_CHILD(this),
              FIRST_CHILD(this)->fops->chown,
              path,
	      uid,
	      gid);

  return 0;
}


int32_t
stat_prefetch_utimes_cbk (call_frame_t *frame,
                          void *cookie,
                          xlator_t *this,
                          int32_t op_ret,
                          int32_t op_errno,
			  struct stat *buf)
{
  STACK_UNWIND (frame, op_ret, op_errno, buf);
  return 0;
}

int32_t
stat_prefetch_utimes (call_frame_t *frame,
		      struct xlator *this,
		      const char *path,
		      struct timespec *tvp)
{
  stat_prefetch_cache_flush (this->private, 1);

  STACK_WIND (frame,
              stat_prefetch_utimes_cbk,
              FIRST_CHILD(this),
              FIRST_CHILD(this)->fops->utimes,
              path,
	      tvp);

  return 0;
}


int32_t
stat_prefetch_truncate_cbk (call_frame_t *frame,
			    void *cookie,
			    xlator_t *this,
			    int32_t op_ret,
			    int32_t op_errno,
			    struct stat *buf)
{
  STACK_UNWIND (frame, op_ret, op_errno, buf);
  return 0;
}

int32_t
stat_prefetch_truncate (call_frame_t *frame,
			struct xlator *this,
			const char *path,
			off_t offset)
{
  stat_prefetch_cache_flush (this->private, 1);

  STACK_WIND (frame,
              stat_prefetch_truncate_cbk,
              FIRST_CHILD(this),
              FIRST_CHILD(this)->fops->truncate,
              path,
	      offset);

  return 0;
}


int32_t
stat_prefetch_rename_cbk (call_frame_t *frame,
                          void *cookie,
                          xlator_t *this,
                          int32_t op_ret,
                          int32_t op_errno)
{
  STACK_UNWIND (frame, op_ret, op_errno);
  return 0;
}

int32_t
stat_prefetch_rename (call_frame_t *frame,
                      struct xlator *this,
                      const char *oldpath,
		      const char *newpath)
{
  stat_prefetch_cache_flush (this->private, 1);

  STACK_WIND (frame,
              stat_prefetch_rename_cbk,
              FIRST_CHILD(this),
              FIRST_CHILD(this)->fops->rename,
              oldpath,
	      newpath);

  return 0;
}

int32_t 
init (struct xlator *this)
{
  struct sp_cache *cache;
  dict_t *options = this->options;

  if (!this->children || this->children->next) {
    gf_log ("stat-prefetch",
	    GF_LOG_ERROR,
	    "FATAL: translator %s does not have exactly one child node",
	    this->name);
    return -1;
  }

  cache = (void *) CALLOC (1, sizeof (*cache));
  ERR_ABORT (cache);
  cache->next = cache->prev = cache;

  cache->tv_time = 1 * 1000000;

  if (dict_get (options, "cache-seconds")) {
    cache->tv_time = (data_to_int64 (dict_get (options, "cache-seconds")) *
		      1000000);
  }

  pthread_mutex_init (&cache->lock, NULL);

  this->private = cache;
  return 0;
}

void
fini (struct xlator *this)
{
  return;
}


struct xlator_fops fops = {
  .getattr     = stat_prefetch_getattr,
  .readdir     = stat_prefetch_readdir,
  .unlink      = stat_prefetch_unlink,
  .chmod       = stat_prefetch_chmod,
  .chown       = stat_prefetch_chown,
  .rename      = stat_prefetch_rename,
  .utimes      = stat_prefetch_utimes,
  .truncate    = stat_prefetch_truncate,
};

struct xlator_mops mops = {
};