FD.io VPP  v18.01.2-1-g9b554f3
Vector Packet Processing
maplog.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2017 Cisco and/or its affiliates.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at:
6  *
7  * http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 #ifndef __included_maplog_h__
17 #define __included_maplog_h__
18 
19 /** @file
20  @brief mmap-based thread-safe fixed-size record double-buffered logging.
21 
22  This scheme should be about as fast as practicable. By fiat, log
23  records are rounded to a multiple of CLIB_CACHE_LINE_BYTES.
24  Consumer code calls clib_maplog_get_entry(...) to obtain a pointer
25  to a log entry.
26 
27  We use an atomic ticket-counter to dole out log entries. Whichever
28  client thread crosses the double-buffer boundary is in charge of
29  replacing the log segment which just filled.
30 */
31 
32 #include <vppinfra/clib.h>
33 #include <vppinfra/cache.h>
34 #include <vppinfra/format.h>
35 #include <string.h>
36 #include <sys/types.h>
37 #include <sys/stat.h>
38 #include <fcntl.h>
39 #include <sys/mman.h>
40 
41 /** Maplog log file header segment. In a separate file */
42 
43 typedef struct
44 {
45  u8 maplog_major_version; /**< library major version number */
46  u8 maplog_minor_version; /**< library minor version number */
47  u8 maplog_patch_version; /**< library patch version number */
49  u32 application_id; /**< application identifier */
50  u8 application_major_version; /**< application major version number */
51  u8 application_minor_version; /**< application minor version number */
52  u8 application_patch_version; /**< application patch version number */
54  u32 record_size_in_cachelines; /**< record size in cache lines */
55  u32 cacheline_size; /**< cache line size */
56  u64 file_size_in_records; /**< file size in records */
57  u64 number_of_records; /**< number of records in entire log */
58  u64 number_of_files; /**< number of files in entire log */
59  u8 file_basename[256]; /**< file basename */
61 
62 #define MAPLOG_MAJOR_VERSION 1
63 #define MAPLOG_MINOR_VERSION 0
64 #define MAPLOG_PATCH_VERSION 0
65 
66 /** Process-private main data structure */
67 
68 typedef struct
69 {
70  CLIB_CACHE_LINE_ALIGN_MARK (cacheline1);
71  /** rw cache line: atomic ticket-counter, file index */
73  /** file size in records, rounded to a power of two */
75  u32 log2_file_size_in_records; /**< lg file size in records */
76  volatile u32 current_file_index; /**< current file index */
77  volatile u32 flags; /**< flags, currently just "init" or not */
78 
79  /* read-mostly cache line: size parameters, file names, etc. */
80  CLIB_CACHE_LINE_ALIGN_MARK (cacheline2);
81  u32 record_size_in_cachelines; /**< record size in cache lines */
82 
83  /* double-buffered mmap'ed logfiles */
84  volatile u8 *file_baseva[2]; /**< active segment base addresses */
85  u8 *filenames[2]; /**< active segment file names */
86  /* vector not c-string */
87  u8 *file_basename; /**< basename, e.g. "/tmp/mylog" */
88  u8 *header_filename; /**< log header file name */
90 
91 /* flag bits */
92 #define CLIB_MAPLOG_FLAG_INIT (1<<0)
93 
94 /** log initialization structure */
95 typedef struct
96 {
97  clib_maplog_main_t *mm; /**< pointer to the main structure */
98  char *file_basename; /**< file base name */
99  u64 file_size_in_bytes; /**< file size in bytes */
100  u32 record_size_in_bytes; /**< record size in bytes */
101  u32 application_id; /**< application identifier */
102  u8 application_major_version; /**< applcation major version number */
103  u8 application_minor_version; /**< applcation minor version number */
104  u8 application_patch_version; /**< applcation patch version number */
106 
107 /* function prototypes */
108 
112 int clib_maplog_process (char *file_basename, void *fp_arg);
113 
115 
116 u8 *_clib_maplog_get_entry_slowpath (clib_maplog_main_t * mm,
117  u64 my_record_index);
118 
119 /**
120  * @brief Obtain a log entry pointer
121  *
122  * Increments the atomic ticket counter, and returns a pointer to
123  * the newly-allocated log entry. The slowpath function replaces
124  * a full log segment with a new/fresh/empty log segment
125  *
126  * @param[in] mm maplog object pointer
127  * @return pointer to the allocated log entry
128  */
129 static inline void *
131 {
132  u64 my_record_index;
133  u8 *rv;
134 
136 
137  my_record_index = __sync_fetch_and_add (&mm->next_record_index, 1);
138 
139  /* Time to unmap and create a new logfile? */
140  if (PREDICT_FALSE ((my_record_index & (mm->file_size_in_records - 1)) == 0))
141  {
142  /* Yes, but not the very first time... (;-)... */
143  if (my_record_index)
144  return _clib_maplog_get_entry_slowpath (mm, my_record_index);
145  /* FALLTHROUGH */
146  }
147 
148  rv = (u8 *)
149  mm->file_baseva[(my_record_index >> mm->log2_file_size_in_records) & 1] +
150  (my_record_index & (mm->file_size_in_records - 1))
152 
153  return rv;
154 }
155 
156 #endif /* __included_maplog_h__ */
157 
158 /*
159  * fd.io coding-style-patch-verification: ON
160  *
161  * Local Variables:
162  * eval: (c-set-style "gnu")
163  * End:
164  */
#define CLIB_CACHE_LINE_ALIGN_MARK(mark)
Definition: cache.h:68
u8 application_patch_version
applcation patch version number
Definition: maplog.h:104
volatile u64 next_record_index
rw cache line: atomic ticket-counter, file index
Definition: maplog.h:72
void clib_maplog_close(clib_maplog_main_t *mm)
Close a mapped log, and update the log header file.
Definition: maplog.c:285
u8 maplog_minor_version
library minor version number
Definition: maplog.h:46
int clib_maplog_init(clib_maplog_init_args_t *ap)
Initialize a maplog object.
Definition: maplog.c:28
u64 file_size_in_records
file size in records, rounded to a power of two
Definition: maplog.h:74
u8 *( format_function_t)(u8 *s, va_list *args)
Definition: format.h:48
u32 record_size_in_cachelines
record size in cache lines
Definition: maplog.h:54
u64 file_size_in_records
file size in records
Definition: maplog.h:56
volatile u32 current_file_index
current file index
Definition: maplog.h:76
Maplog log file header segment.
Definition: maplog.h:43
u32 record_size_in_cachelines
record size in cache lines
Definition: maplog.h:81
u8 application_minor_version
application minor version number
Definition: maplog.h:51
#define CLIB_MAPLOG_FLAG_INIT
Definition: maplog.h:92
unsigned long u64
Definition: types.h:89
u32 log2_file_size_in_records
lg file size in records
Definition: maplog.h:75
u32 cacheline_size
cache line size
Definition: maplog.h:55
#define PREDICT_FALSE(x)
Definition: clib.h:105
u8 * file_basename
basename, e.g.
Definition: maplog.h:87
u32 application_id
application identifier
Definition: maplog.h:49
u8 application_minor_version
applcation minor version number
Definition: maplog.h:103
u32 application_id
application identifier
Definition: maplog.h:101
volatile u32 flags
flags, currently just "init" or not
Definition: maplog.h:77
u8 maplog_major_version
library major version number
Definition: maplog.h:45
static void * clib_maplog_get_entry(clib_maplog_main_t *mm)
Obtain a log entry pointer.
Definition: maplog.h:130
u8 application_major_version
applcation major version number
Definition: maplog.h:102
volatile u8 * file_baseva[2]
active segment base addresses
Definition: maplog.h:84
#define ASSERT(truth)
unsigned int u32
Definition: types.h:88
char * file_basename
file base name
Definition: maplog.h:98
u32 record_size_in_bytes
record size in bytes
Definition: maplog.h:100
u64 number_of_records
number of records in entire log
Definition: maplog.h:57
format_function_t format_maplog_header
Definition: maplog.h:114
u8 * header_filename
log header file name
Definition: maplog.h:88
unsigned char u8
Definition: types.h:56
int clib_maplog_process(char *file_basename, void *fp_arg)
Process a complete maplog.
Definition: maplog.c:366
Process-private main data structure.
Definition: maplog.h:68
u8 application_patch_version
application patch version number
Definition: maplog.h:52
u64 file_size_in_bytes
file size in bytes
Definition: maplog.h:99
u8 application_major_version
application major version number
Definition: maplog.h:50
void clib_maplog_update_header(clib_maplog_main_t *mm)
Update a mapped log header file.
Definition: maplog.c:233
u64 number_of_files
number of files in entire log
Definition: maplog.h:58
#define CLIB_CACHE_LINE_BYTES
Definition: cache.h:67
clib_maplog_main_t * mm
pointer to the main structure
Definition: maplog.h:97
log initialization structure
Definition: maplog.h:95
u8 maplog_patch_version
library patch version number
Definition: maplog.h:47