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:
7 * http://www.apache.org/licenses/LICENSE-2.0
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.
16 #ifndef __included_maplog_h__
17 #define __included_maplog_h__
21 mmap-based fixed-size record double-buffered logging
24 #include <vppinfra/clib.h>
25 #include <vppinfra/cache.h>
26 #include <vppinfra/format.h>
28 #include <sys/types.h>
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;
43 /* ro: size parameters */
44 CLIB_CACHE_LINE_ALIGN_MARK (cacheline2);
45 u32 record_size_in_cachelines;
47 /* double-buffered mmap'ed logfiles */
48 volatile u8 *file_baseva[2];
50 /* vector not c-string */
54 int clib_maplog_init (clib_maplog_main_t * mm, char *file_basename,
55 u64 file_size, u32 record_size_in_bytes);
57 void clib_maplog_close (clib_maplog_main_t * mm);
59 #define CLIB_MAPLOG_FLAG_INIT (1<<0)
61 u8 *_clib_maplog_get_entry_slowpath (clib_maplog_main_t * mm,
65 clib_maplog_get_entry (clib_maplog_main_t * mm)
70 ASSERT (mm->flags & CLIB_MAPLOG_FLAG_INIT);
72 my_record_index = __sync_fetch_and_add (&mm->next_record_index, 1);
74 /* Time to unmap and create a new logfile? */
75 if (PREDICT_FALSE ((my_record_index & (mm->file_size_in_records - 1)) == 0))
77 /* Yes, but not the very first time... (;-)... */
79 return _clib_maplog_get_entry_slowpath (mm, my_record_index);
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;
91 #endif /* __included_maplog_h__ */
94 * fd.io coding-style-patch-verification: ON
97 * eval: (c-set-style "gnu")