3
A brief file description
5
@section license License
7
Licensed to the Apache Software Foundation (ASF) under one
8
or more contributor license agreements. See the NOTICE file
9
distributed with this work for additional information
10
regarding copyright ownership. The ASF licenses this file
11
to you under the Apache License, Version 2.0 (the
12
"License"); you may not use this file except in compliance
13
with the License. You may obtain a copy of the License at
15
http://www.apache.org/licenses/LICENSE-2.0
17
Unless required by applicable law or agreed to in writing, software
18
distributed under the License is distributed on an "AS IS" BASIS,
19
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
20
See the License for the specific language governing permissions and
21
limitations under the License.
25
#if !defined (INK_NO_LOG)
30
#include "LogFormatType.h"
31
#include "LogLimits.h"
33
#include "LogBufferV1.h"
34
#include "LogAccess.h"
37
class LogBufferIterator;
39
#define LOG_SEGMENT_COOKIE 0xaceface
40
#define LOG_SEGMENT_VERSION 2
43
#define LB_DEFAULT_ALIGN 512
45
#define LB_DEFAULT_ALIGN 8
48
/*-------------------------------------------------------------------------
51
This struct is automatically laid down at the head of each entry in the
53
-------------------------------------------------------------------------*/
57
// unsigned timestamp;
58
long timestamp; // the seconds portion of the timestamp
59
long timestamp_usec; // the microseconds portion of the timestamp
63
/*-------------------------------------------------------------------------
66
This struct is automatically laid down at the head of each buffer.
67
-------------------------------------------------------------------------*/
69
struct LogBufferHeader
72
unsigned cookie; // so we can find it on disk
73
unsigned version; // in case we want to change it later
74
unsigned format_type; // SQUID_LOG, COMMON_LOG, ...
75
unsigned byte_count; // acutal # of bytes for the segment
76
unsigned entry_count; // actual number of entries stored
77
unsigned low_timestamp; // lowest timestamp value of entries
78
unsigned high_timestamp; // highest timestamp value of entries
79
unsigned int log_object_flags; // log object flags
80
uint64_t log_object_signature; // log object signature
81
#if defined(LOG_BUFFER_TRACKING)
83
#endif // defined(LOG_BUFFER_TRACKING)
85
// all offsets are computed from the start of the buffer (ie, "this"),
86
// and so any valid offset will be at least sizeof(LogBufferHeader).
88
unsigned fmt_name_offset; // offset to format name string
89
unsigned fmt_fieldlist_offset; // offset to format fieldlist string
90
unsigned fmt_printf_offset; // offset to format printf string
91
unsigned src_hostname_offset; // offset to source (client) hostname
92
unsigned log_filename_offset; // offset to log filename
93
unsigned data_offset; // offset to start of data entry
96
// some helper functions to return the header strings
98
char *fmt_name(); // not used
99
char *fmt_fieldlist();
101
char *src_hostname();
102
char *log_filename();
112
LB_State(volatile LB_State & vs)
117
LB_State & operator =(volatile LB_State & vs)
126
uint16_t offset; // buffer should be <= 64KB
127
uint16_t num_entries; // number of entries in buffer
128
uint16_t byte_count; // bytes in buffer
129
uint16_t full:1; // not accepting more checkouts
130
uint16_t num_writers:15; // number of writers
135
/* ---------------------------------- iObject ------------------------------ */
136
class iObjectActivator;
140
static iObject *free_heap; /* list of free blocks */
141
static ink_mutex iObjectMutex; /* mutex for access to iObject class global variables */
143
size_t class_size; /* real class size */
144
iObject *next_object;
148
iObject(const iObject &); /* declared; not implemented - block copying and assignment */
149
iObject & operator=(const iObject &); /* ditto */
152
static void Init(void);
153
void *operator new(size_t size);
154
void operator delete(void *p);
164
friend class iObjectActivator;
167
/* ------------------------------ iLogBufferBuffer ------------------------- */
168
class iLogBufferBuffer
171
static iLogBufferBuffer *free_heap; /* list of free blocks */
172
static ink_mutex iLogBufferBufferMutex; /* mutex for access iLogBufferBuffer class global variables */
174
iLogBufferBuffer *next;
175
size_t real_buf_size;
182
real_buf_size = (size = 0);
189
real_buf_size = (size = 0);
193
iLogBufferBuffer(const iLogBufferBuffer &); /* declared; not implemented - block copying and assignment */
194
iLogBufferBuffer & operator=(const iLogBufferBuffer &); /* ditto */
200
static void Init(void);
201
static iLogBufferBuffer *New_iLogBufferBuffer(size_t _buf_size);
202
static iLogBufferBuffer *Delete_iLogBufferBuffer(iLogBufferBuffer * _b);
204
friend class iObjectActivator;
207
/* ---------------------------- iObjectActivator --------------------------- */
208
class iObjectActivator
214
iLogBufferBuffer::Init();
222
/*-------------------------------------------------------------------------
224
-------------------------------------------------------------------------*/
225
#define CLASS_SIGN_LOGBUFFER 0xFACE5370 /* LogBuffer class signature */
227
class LogBuffer:public iObject
230
unsigned long sign; /* class signature (must be CLASS_SIGN_LOGBUFFER) */
231
LogBuffer *next_flush; /* next in flush list */
232
LogBuffer *next_list; /* next in list */
238
LB_FULL_ACTIVE_WRITERS,
245
LogBuffer(LogObject * owner, size_t size,
246
size_t buf_align = LB_DEFAULT_ALIGN, size_t write_align = INK_MIN_ALIGN);
247
LogBuffer(LogObject * owner, LogBufferHeader * header);
249
char &operator [] (int idx)
251
ink_debug_assert(idx >= 0);
252
ink_debug_assert((size_t) idx < m_size);
253
return m_buffer[idx];
256
int switch_state(LB_State & old_state, LB_State & new_state)
258
INK_WRITE_MEMORY_BARRIER;
259
return (ink_atomic_cas64((int64_t *) & m_state.ival, old_state.ival, new_state.ival));
262
LB_ResultCode checkout_write(size_t * write_offset, size_t write_size);
263
LB_ResultCode checkin_write(size_t write_offset);
266
LogBufferHeader *header()
270
long expiration_time()
272
return m_expiration_time;
275
// this should only be called when buffer is ready to be flushed
276
void update_header_data();
277
void convert_to_network_order();
278
void convert_to_host_order();
283
LogObject *get_owner() const
288
Link<LogBuffer> link;
294
static size_t max_entry_bytes();
295
static int to_ascii(LogEntryHeader * entry, LogFormatType type,
296
char *buf, int max_len, char *symbol_str, char *printf_str,
297
unsigned buffer_version, char *alt_format = NULL);
298
static int resolve_custom_entry(LogFieldList * fieldlist,
299
char *printf_str, char *read_from, char *write_to,
300
int write_to_len, long timestamp, long timestamp_us,
301
unsigned buffer_version, LogFieldList * alt_fieldlist = NULL,
302
char *alt_printf_str = NULL);
303
static void convert_to_network_order(LogBufferHeader * header);
304
static void convert_to_host_order(LogBufferHeader * header);
307
iLogBufferBuffer * m_bb; // real buffer
308
char *m_new_buffer; // new buffer (must be free)
309
char *m_unaligned_buffer; // the unaligned buffer
310
char *m_buffer; // the buffer
311
size_t m_size; // the buffer size
312
size_t m_buf_align; // the buffer alignment
313
size_t m_write_align; // the write alignment mask
315
volatile LB_State m_state; // buffer state
317
int m_max_entries; // max number of entries allowed
318
long m_expiration_time; // buffer expiration time
320
LogObject *m_owner; // the LogObject that owns this buf.
321
LogBufferHeader *m_header;
323
uint32_t m_id; // unique buffer id (for debugging)
326
size_t _add_buffer_header();
327
unsigned add_header_str(char *str, char *buf_ptr, unsigned buf_len);
329
// -- member functions that are not allowed --
331
LogBuffer(const LogBuffer & rhs);
332
LogBuffer & operator=(const LogBuffer & rhs);
334
friend class LogBufferIterator;
339
/*-------------------------------------------------------------------------
342
Support atomic operations on a list of LogBuffer objects.
343
-------------------------------------------------------------------------*/
349
LogBuffer *m_list_last_ptr;
357
void add(LogBuffer * lb);
358
LogBuffer *get(void);
365
/*-------------------------------------------------------------------------
368
This class will iterate over the entries in a LogBuffer.
369
-------------------------------------------------------------------------*/
371
class LogBufferIterator
374
LogBufferIterator(LogBufferHeader * header, bool in_network_order = false);
375
~LogBufferIterator();
377
LogEntryHeader *next();
380
bool m_in_network_order;
382
unsigned m_iter_entry_count;
383
unsigned m_buffer_entry_count;
385
// -- member functions not allowed --
387
LogBufferIterator(const LogBufferIterator &);
388
LogBufferIterator & operator=(const LogBufferIterator &);
392
/*-------------------------------------------------------------------------
395
This class provides the ability to iterate over the LogEntries stored
396
within a given LogBuffer.
397
-------------------------------------------------------------------------*/
400
LogBufferIterator::LogBufferIterator(LogBufferHeader * header, bool in_network_order)
401
: m_in_network_order(in_network_order),
403
m_iter_entry_count(0),
404
m_buffer_entry_count(0)
406
ink_debug_assert(header);
408
switch (header->version) {
409
case LOG_SEGMENT_VERSION:
410
m_next = (char *) header + header->data_offset;
411
m_buffer_entry_count = header->entry_count;
415
m_next = (char *) header + ((LogBufferHeaderV1 *) header)->data_offset;
416
m_buffer_entry_count = ((LogBufferHeaderV1 *) header)->entry_count;
420
Note("Invalid LogBuffer version %d in LogBufferIterator; "
421
"current version is %d", header->version, LOG_SEGMENT_VERSION);
427
/*-------------------------------------------------------------------------
428
-------------------------------------------------------------------------*/