summaryrefslogtreecommitdiffstats
path: root/src/vppinfra/maplog.h
blob: 192dc22a71b4ec6fd24916025bef72423620ab31 (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
/*
 * Copyright (c) 2017 Cisco and/or its affiliates.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at:
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#ifndef __included_maplog_h__
#define __included_maplog_h__

/** \file

   mmap-based fixed-size record double-buffered logging
*/

#include <vppinfra/clib.h>
#include <vppinfra/cache.h>
#include <vppinfra/format.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <sys/mman.h>

typedef struct
{
  /* rw: atomic ticket-counter, file index */
  CLIB_CACHE_LINE_ALIGN_MARK (cacheline1);
  volatile u64 next_record_index;
  u64 file_size_in_records; /**< power of two */
  u32 log2_file_size_in_records;
  volatile u32 current_file_index;
  volatile u32 flags;

  /* ro: size parameters */
    CLIB_CACHE_LINE_ALIGN_MARK (cacheline2);
  u32 record_size_in_cachelines;

  /* double-buffered mmap'ed logfiles */
  volatile u8 *file_baseva[2];
  u8 *filenames[2];
  /* vector not c-string */
  u8 *file_basename;
} clib_maplog_main_t;

int clib_maplog_init (clib_maplog_main_t * mm, char *file_basename,
		      u64 file_size, u32 record_size_in_bytes);

void clib_maplog_close (clib_maplog_main_t * mm);

#define CLIB_MAPLOG_FLAG_INIT 	(1<<0)

u8 *_clib_maplog_get_entry_slowpath (clib_maplog_main_t * mm,
				     u64 my_record_index);

static inline void *
clib_maplog_get_entry (clib_maplog_main_t * mm)
{
  u64 my_record_index;
  u8 *rv;

  ASSERT (mm->flags & CLIB_MAPLOG_FLAG_INIT);

  my_record_index = __sync_fetch_and_add (&mm->next_record_index, 1);

  /* Time to unmap and create a new logfile? */
  if (PREDICT_FALSE ((my_record_index & (mm->file_size_in_records - 1)) == 0))
    {
      /* Yes, but not the very first time... (;-)... */
      if (my_record_index)
	return _clib_maplog_get_entry_slowpath (mm, my_record_index);
      /* FALLTHROUGH */
    }

  rv = (u8 *)
    mm->file_baseva[(my_record_index >> mm->log2_file_size_in_records) & 1] +
    (my_record_index & (mm->file_size_in_records - 1))
    * mm->record_size_in_cachelines * CLIB_CACHE_LINE_BYTES;

  return rv;
}

#endif /* __included_maplog_h__ */

/*
 * fd.io coding-style-patch-verification: ON
 *
 * Local Variables:
 * eval: (c-set-style "gnu")
 * End:
 */