mmap-based fixed-size record double-buffered logger
[vpp.git] / src / vppinfra / maplog.h
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
21    mmap-based fixed-size record double-buffered logging
22 */
23
24 #include <vppinfra/clib.h>
25 #include <vppinfra/cache.h>
26 #include <vppinfra/format.h>
27 #include <string.h>
28 #include <sys/types.h>
29 #include <sys/stat.h>
30 #include <fcntl.h>
31 #include <sys/mman.h>
32
33 typedef struct
34 {
35   /* rw: atomic ticket-counter, file index */
36   CLIB_CACHE_LINE_ALIGN_MARK (cacheline1);
37   volatile u64 next_record_index;
38   u64 file_size_in_records; /**< power of two */
39   u32 log2_file_size_in_records;
40   volatile u32 current_file_index;
41   volatile u32 flags;
42
43   /* ro: size parameters */
44     CLIB_CACHE_LINE_ALIGN_MARK (cacheline2);
45   u32 record_size_in_cachelines;
46
47   /* double-buffered mmap'ed logfiles */
48   volatile u8 *file_baseva[2];
49   u8 *filenames[2];
50   /* vector not c-string */
51   u8 *file_basename;
52 } clib_maplog_main_t;
53
54 int clib_maplog_init (clib_maplog_main_t * mm, char *file_basename,
55                       u64 file_size, u32 record_size_in_bytes);
56
57 void clib_maplog_close (clib_maplog_main_t * mm);
58
59 #define CLIB_MAPLOG_FLAG_INIT   (1<<0)
60
61 u8 *_clib_maplog_get_entry_slowpath (clib_maplog_main_t * mm,
62                                      u64 my_record_index);
63
64 static inline void *
65 clib_maplog_get_entry (clib_maplog_main_t * mm)
66 {
67   u64 my_record_index;
68   u8 *rv;
69
70   ASSERT (mm->flags & CLIB_MAPLOG_FLAG_INIT);
71
72   my_record_index = __sync_fetch_and_add (&mm->next_record_index, 1);
73
74   /* Time to unmap and create a new logfile? */
75   if (PREDICT_FALSE ((my_record_index & (mm->file_size_in_records - 1)) == 0))
76     {
77       /* Yes, but not the very first time... (;-)... */
78       if (my_record_index)
79         return _clib_maplog_get_entry_slowpath (mm, my_record_index);
80       /* FALLTHROUGH */
81     }
82
83   rv = (u8 *)
84     mm->file_baseva[(my_record_index >> mm->log2_file_size_in_records) & 1] +
85     (my_record_index & (mm->file_size_in_records - 1))
86     * mm->record_size_in_cachelines * CLIB_CACHE_LINE_BYTES;
87
88   return rv;
89 }
90
91 #endif /* __included_maplog_h__ */
92
93 /*
94  * fd.io coding-style-patch-verification: ON
95  *
96  * Local Variables:
97  * eval: (c-set-style "gnu")
98  * End:
99  */