支持输出日志到指定目录

This commit is contained in:
guo_peixu
2022-06-22 09:44:55 +08:00
parent 93c07a240b
commit 3421f97f95
6 changed files with 1004 additions and 78 deletions

View File

@@ -2,13 +2,15 @@
#define _MESA_SHM_RING_QUEUE_H_ #define _MESA_SHM_RING_QUEUE_H_
#include <pthread.h> #include <pthread.h>
#define MESA_SHM_LOG_PATH_LEN 1024
#define MESA_SHM_RING_QUEUE_NUM 128 #define MESA_SHM_RING_QUEUE_NUM 128
#define MESA_SHM_RING_QUEUE_BLOCK_NUM 8192 #define MESA_SHM_RING_QUEUE_BLOCK_NUM 8192
#define MESA_SHM_RING_QUEUE_BLOCK_BUFLEN 4096 #define MESA_SHM_RING_QUEUE_BLOCK_BUFLEN 4096
#define MESA_SHM_RING_QUEUE_BLOCK_SIZE (MESA_SHM_RING_QUEUE_BLOCK_BUFLEN + sizeof(int) + 1) /*user buf + (int)len flag + '\n'*/ #define MESA_SHM_RING_QUEUE_BLOCK_SIZE (MESA_SHM_LOG_PATH_LEN + sizeof(int) + MESA_SHM_RING_QUEUE_BLOCK_BUFLEN) /*log file + (int)payload len + payload*/
#define MESA_SHM_KEY_OVERVIEW 35720 #define MESA_SHM_KEY_OVERVIEW 35720
#define MESA_SHM_KEY_MIN (MESA_SHM_KEY_OVERVIEW + 1) #define MESA_SHM_KEY_MIN (MESA_SHM_KEY_OVERVIEW + 1)
#define MESA_SHM_KEY_MAX (MESA_SHM_KEY_MIN + MESA_SHM_RING_QUEUE_NUM -1) #define MESA_SHM_KEY_MAX (MESA_SHM_KEY_MIN + MESA_SHM_RING_QUEUE_NUM -1)
#define MESA_SHM_LOG_BUF_PREFIX_LEN 1024
#define MESA_SHM_RING_QUEUE_INITIAL 0 #define MESA_SHM_RING_QUEUE_INITIAL 0
#define MESA_SHM_RING_QUEUE_IDLE 1 #define MESA_SHM_RING_QUEUE_IDLE 1
@@ -20,8 +22,9 @@ struct MESA_shm_queue_head *MESA_shm_get_ring_queue();
void MESA_shm_init_mutex(); void MESA_shm_init_mutex();
void MESA_shm_init_overview(); void MESA_shm_init_overview();
void MESA_shm_recycle_ring_queue(struct MESA_shm_queue_head *ring_queue_head); void MESA_shm_recycle_ring_queue(struct MESA_shm_queue_head *ring_queue_head);
int MESA_shm_copy_buf_to_ring_queue(char *buf, int buflen, struct MESA_shm_queue_head *head); int MESA_shm_copy_buf_to_ring_queue(char *buf, int buflen, struct MESA_shm_queue_head *head, char *log_file, int log_file_len);
void MESA_shm_write_ring_queue_to_file(int fd, struct MESA_shm_queue_head *head); int MESA_shm_ring_queue_is_empty(struct MESA_shm_queue_head *head);
int MESA_shm_ring_queue_is_full(struct MESA_shm_queue_head *head);
@@ -29,6 +32,7 @@ struct MESA_shm_overview{
int shmkey; int shmkey;
int shmid; int shmid;
int idx; int idx;
int producer_pid;
pthread_mutex_t mutex; pthread_mutex_t mutex;
}; };

783
inc/list.h Normal file
View File

@@ -0,0 +1,783 @@
/*
* @file list.h
* @author PF
* @date 2017/05/1
*
* port from linux kernel list.h: https://github.com/torvalds/linux/raw/master/include/linux/list.h
*
* Here is a recipe to cook list.h for user space program.
* 1. copy list.h from linux/include/list.h
* 2. remove
* - #ifdef __KERNE__ and its #endif
* - all #include line
* - prefetch() and rcu related functions
* 3. add macro offsetof() and container_of
*/
#ifndef LIST_H_
#define LIST_H_ (1)
// import from include/linux/types.h
struct list_head {
struct list_head *next, *prev;
};
struct hlist_head {
struct hlist_node *first;
};
struct hlist_node {
struct hlist_node *next, **pprev;
};
// import from include/linux/poison.h
/*
* Architectures might want to move the poison pointer offset
* into some well-recognized area such as 0xdead000000000000,
* that is also not mappable by user-space exploits:
*/
#ifdef CONFIG_ILLEGAL_POINTER_VALUE
# define POISON_POINTER_DELTA _AC(CONFIG_ILLEGAL_POINTER_VALUE, UL)
#else
# define POISON_POINTER_DELTA (0)
#endif
/*
* These are non-NULL pointers that will result in page faults
* under normal circumstances, used to verify that nobody uses
* non-initialized list entries.
*/
#define LIST_POISON1 ((void *) 0x00100100 + POISON_POINTER_DELTA)
#define LIST_POISON2 ((void *) 0x00200200 + POISON_POINTER_DELTA)
// import from include/linux/stddef.h
#undef offsetof
#ifdef __compiler_offsetof
#define offsetof(TYPE,MEMBER) __compiler_offsetof(TYPE,MEMBER)
#else
#define offsetof(TYPE, MEMBER) ((size_t) &((TYPE *)0)->MEMBER)
#endif
// import from include/linux/kernel.h
/**
* container_of - cast a member of a structure out to the containing structure
* @ptr: the pointer to the member.
* @type: the type of the container struct this is embedded in.
* @member: the name of the member within the struct.
*
*/
#define container_of(ptr, type, member) ({ \
const typeof( ((type *)0)->member ) *__mptr = (ptr); \
(type *)( (char *)__mptr - offsetof(type,member) );})
/*
* Simple doubly linked list implementation.
*
* Some of the internal functions ("__xxx") are useful when
* manipulating whole lists rather than single entries, as
* sometimes we already know the next/prev entries and we can
* generate better code by using them directly rather than
* using the generic single-entry routines.
*/
#define LIST_HEAD_INIT(name) { &(name), &(name) }
#define LIST_HEAD(name) \
struct list_head name = LIST_HEAD_INIT(name)
static inline void INIT_LIST_HEAD(struct list_head *list) {
list->next = list;
list->prev = list;
}
/*
* Insert a new entry between two known consecutive entries.
*
* This is only for internal list manipulation where we know
* the prev/next entries already!
*/
#ifndef CONFIG_DEBUG_LIST
static inline void __list_add(struct list_head *new,
struct list_head *prev,
struct list_head *next) {
next->prev = new;
new->next = next;
new->prev = prev;
prev->next = new;
}
#else
extern void __list_add(struct list_head *new,
struct list_head *prev,
struct list_head *next);
#endif
/**
* list_add - add a new entry
* @new: new entry to be added
* @head: list head to add it after
*
* Insert a new entry after the specified head.
* This is good for implementing stacks.
*/
static inline void list_add(struct list_head *new, struct list_head *head) {
__list_add(new, head, head->next);
}
/**
* list_add_tail - add a new entry
* @new: new entry to be added
* @head: list head to add it before
*
* Insert a new entry before the specified head.
* This is useful for implementing queues.
*/
static inline void list_add_tail(struct list_head *new, struct list_head *head) {
__list_add(new, head->prev, head);
}
/*
* Delete a list entry by making the prev/next entries
* point to each other.
*
* This is only for internal list manipulation where we know
* the prev/next entries already!
*/
static inline void __list_del(struct list_head *prev, struct list_head *next) {
next->prev = prev;
prev->next = next;
}
/**
* list_del - deletes entry from list.
* @entry: the element to delete from the list.
* Note: list_empty() on entry does not return true after this, the entry is
* in an undefined state.
*/
#ifndef CONFIG_DEBUG_LIST
static inline void __list_del_entry(struct list_head *entry) {
__list_del(entry->prev, entry->next);
}
static inline void list_del(struct list_head *entry) {
__list_del(entry->prev, entry->next);
entry->next = LIST_POISON1;
entry->prev = LIST_POISON2;
}
#else
extern void __list_del_entry(struct list_head *entry);
extern void list_del(struct list_head *entry);
#endif
/**
* list_replace - replace old entry by new one
* @old : the element to be replaced
* @new : the new element to insert
*
* If @old was empty, it will be overwritten.
*/
static inline void list_replace(struct list_head *old,
struct list_head *new) {
new->next = old->next;
new->next->prev = new;
new->prev = old->prev;
new->prev->next = new;
}
static inline void list_replace_init(struct list_head *old,
struct list_head *new) {
list_replace(old, new);
INIT_LIST_HEAD(old);
}
/**
* list_del_init - deletes entry from list and reinitialize it.
* @entry: the element to delete from the list.
*/
static inline void list_del_init(struct list_head *entry) {
__list_del_entry(entry);
INIT_LIST_HEAD(entry);
}
/**
* list_move - delete from one list and add as another's head
* @list: the entry to move
* @head: the head that will precede our entry
*/
static inline void list_move(struct list_head *list, struct list_head *head) {
__list_del_entry(list);
list_add(list, head);
}
/**
* list_move_tail - delete from one list and add as another's tail
* @list: the entry to move
* @head: the head that will follow our entry
*/
static inline void list_move_tail(struct list_head *list,
struct list_head *head) {
__list_del_entry(list);
list_add_tail(list, head);
}
/**
* list_is_last - tests whether @list is the last entry in list @head
* @list: the entry to test
* @head: the head of the list
*/
static inline int list_is_last(const struct list_head *list,
const struct list_head *head) {
return list->next == head;
}
/**
* list_empty - tests whether a list is empty
* @head: the list to test.
*/
static inline int list_empty(const struct list_head *head) {
return head->next == head;
}
/**
* list_empty_careful - tests whether a list is empty and not being modified
* @head: the list to test
*
* Description:
* tests whether a list is empty _and_ checks that no other CPU might be
* in the process of modifying either member (next or prev)
*
* NOTE: using list_empty_careful() without synchronization
* can only be safe if the only activity that can happen
* to the list entry is list_del_init(). Eg. it cannot be used
* if another CPU could re-list_add() it.
*/
static inline int list_empty_careful(const struct list_head *head) {
struct list_head *next = head->next;
return (next == head) && (next == head->prev);
}
/**
* list_rotate_left - rotate the list to the left
* @head: the head of the list
*/
static inline void list_rotate_left(struct list_head *head) {
struct list_head *first;
if (!list_empty(head)) {
first = head->next;
list_move_tail(first, head);
}
}
/**
* list_is_singular - tests whether a list has just one entry.
* @head: the list to test.
*/
static inline int list_is_singular(const struct list_head *head) {
return !list_empty(head) && (head->next == head->prev);
}
static inline void __list_cut_position(struct list_head *list,
struct list_head *head, struct list_head *entry) {
struct list_head *new_first = entry->next;
list->next = head->next;
list->next->prev = list;
list->prev = entry;
entry->next = list;
head->next = new_first;
new_first->prev = head;
}
/**
* list_cut_position - cut a list into two
* @list: a new list to add all removed entries
* @head: a list with entries
* @entry: an entry within head, could be the head itself
* and if so we won't cut the list
*
* This helper moves the initial part of @head, up to and
* including @entry, from @head to @list. You should
* pass on @entry an element you know is on @head. @list
* should be an empty list or a list you do not care about
* losing its data.
*
*/
static inline void list_cut_position(struct list_head *list,
struct list_head *head, struct list_head *entry) {
if (list_empty(head)) {
return;
}
if (list_is_singular(head) &&
(head->next != entry && head != entry)) {
return;
}
if (entry == head) {
INIT_LIST_HEAD(list);
} else {
__list_cut_position(list, head, entry);
}
}
static inline void __list_splice(const struct list_head *list,
struct list_head *prev,
struct list_head *next) {
struct list_head *first = list->next;
struct list_head *last = list->prev;
first->prev = prev;
prev->next = first;
last->next = next;
next->prev = last;
}
/**
* list_splice - join two lists, this is designed for stacks
* @list: the new list to add.
* @head: the place to add it in the first list.
*/
static inline void list_splice(const struct list_head *list,
struct list_head *head) {
if (!list_empty(list)) {
__list_splice(list, head, head->next);
}
}
/**
* list_splice_tail - join two lists, each list being a queue
* @list: the new list to add.
* @head: the place to add it in the first list.
*/
static inline void list_splice_tail(struct list_head *list,
struct list_head *head) {
if (!list_empty(list)) {
__list_splice(list, head->prev, head);
}
}
/**
* list_splice_init - join two lists and reinitialise the emptied list.
* @list: the new list to add.
* @head: the place to add it in the first list.
*
* The list at @list is reinitialised
*/
static inline void list_splice_init(struct list_head *list,
struct list_head *head) {
if (!list_empty(list)) {
__list_splice(list, head, head->next);
INIT_LIST_HEAD(list);
}
}
/**
* list_splice_tail_init - join two lists and reinitialise the emptied list
* @list: the new list to add.
* @head: the place to add it in the first list.
*
* Each of the lists is a queue.
* The list at @list is reinitialised
*/
static inline void list_splice_tail_init(struct list_head *list,
struct list_head *head) {
if (!list_empty(list)) {
__list_splice(list, head->prev, head);
INIT_LIST_HEAD(list);
}
}
/**
* list_entry - get the struct for this entry
* @ptr: the &struct list_head pointer.
* @type: the type of the struct this is embedded in.
* @member: the name of the list_head within the struct.
*/
#define list_entry(ptr, type, member) \
container_of(ptr, type, member)
/**
* list_first_entry - get the first element from a list
* @ptr: the list head to take the element from.
* @type: the type of the struct this is embedded in.
* @member: the name of the list_head within the struct.
*
* Note, that list is expected to be not empty.
*/
#define list_first_entry(ptr, type, member) \
list_entry((ptr)->next, type, member)
/**
* list_last_entry - get the last element from a list
* @ptr: the list head to take the element from.
* @type: the type of the struct this is embedded in.
* @member: the name of the list_head within the struct.
*
* Note, that list is expected to be not empty.
*/
#define list_last_entry(ptr, type, member) \
list_entry((ptr)->prev, type, member)
/**
* list_first_entry_or_null - get the first element from a list
* @ptr: the list head to take the element from.
* @type: the type of the struct this is embedded in.
* @member: the name of the list_head within the struct.
*
* Note that if the list is empty, it returns NULL.
*/
#define list_first_entry_or_null(ptr, type, member) \
(!list_empty(ptr) ? list_first_entry(ptr, type, member) : NULL)
/**
* list_next_entry - get the next element in list
* @pos: the type * to cursor
* @member: the name of the list_head within the struct.
*/
#define list_next_entry(pos, member) \
list_entry((pos)->member.next, typeof(*(pos)), member)
/**
* list_prev_entry - get the prev element in list
* @pos: the type * to cursor
* @member: the name of the list_head within the struct.
*/
#define list_prev_entry(pos, member) \
list_entry((pos)->member.prev, typeof(*(pos)), member)
/**
* list_for_each - iterate over a list
* @pos: the &struct list_head to use as a loop cursor.
* @head: the head for your list.
*/
#define list_for_each(pos, head) \
for (pos = (head)->next; pos != (head); pos = pos->next)
/**
* list_for_each_prev - iterate over a list backwards
* @pos: the &struct list_head to use as a loop cursor.
* @head: the head for your list.
*/
#define list_for_each_prev(pos, head) \
for (pos = (head)->prev; pos != (head); pos = pos->prev)
/**
* list_for_each_safe - iterate over a list safe against removal of list entry
* @pos: the &struct list_head to use as a loop cursor.
* @n: another &struct list_head to use as temporary storage
* @head: the head for your list.
*/
#define list_for_each_safe(pos, n, head) \
for (pos = (head)->next, n = pos->next; pos != (head); \
pos = n, n = pos->next)
/**
* list_for_each_prev_safe - iterate over a list backwards safe against removal of list entry
* @pos: the &struct list_head to use as a loop cursor.
* @n: another &struct list_head to use as temporary storage
* @head: the head for your list.
*/
#define list_for_each_prev_safe(pos, n, head) \
for (pos = (head)->prev, n = pos->prev; \
pos != (head); \
pos = n, n = pos->prev)
/**
* list_for_each_entry - iterate over list of given type
* @pos: the type * to use as a loop cursor.
* @head: the head for your list.
* @member: the name of the list_head within the struct.
*/
#define list_for_each_entry(pos, head, member) \
for (pos = list_first_entry(head, typeof(*pos), member); \
&pos->member != (head); \
pos = list_next_entry(pos, member))
/**
* list_for_each_entry_reverse - iterate backwards over list of given type.
* @pos: the type * to use as a loop cursor.
* @head: the head for your list.
* @member: the name of the list_head within the struct.
*/
#define list_for_each_entry_reverse(pos, head, member) \
for (pos = list_last_entry(head, typeof(*pos), member); \
&pos->member != (head); \
pos = list_prev_entry(pos, member))
/**
* list_prepare_entry - prepare a pos entry for use in list_for_each_entry_continue()
* @pos: the type * to use as a start point
* @head: the head of the list
* @member: the name of the list_head within the struct.
*
* Prepares a pos entry for use as a start point in list_for_each_entry_continue().
*/
#define list_prepare_entry(pos, head, member) \
((pos) ? : list_entry(head, typeof(*pos), member))
/**
* list_for_each_entry_continue - continue iteration over list of given type
* @pos: the type * to use as a loop cursor.
* @head: the head for your list.
* @member: the name of the list_head within the struct.
*
* Continue to iterate over list of given type, continuing after
* the current position.
*/
#define list_for_each_entry_continue(pos, head, member) \
for (pos = list_next_entry(pos, member); \
&pos->member != (head); \
pos = list_next_entry(pos, member))
/**
* list_for_each_entry_continue_reverse - iterate backwards from the given point
* @pos: the type * to use as a loop cursor.
* @head: the head for your list.
* @member: the name of the list_head within the struct.
*
* Start to iterate over list of given type backwards, continuing after
* the current position.
*/
#define list_for_each_entry_continue_reverse(pos, head, member) \
for (pos = list_prev_entry(pos, member); \
&pos->member != (head); \
pos = list_prev_entry(pos, member))
/**
* list_for_each_entry_from - iterate over list of given type from the current point
* @pos: the type * to use as a loop cursor.
* @head: the head for your list.
* @member: the name of the list_head within the struct.
*
* Iterate over list of given type, continuing from current position.
*/
#define list_for_each_entry_from(pos, head, member) \
for (; &pos->member != (head); \
pos = list_next_entry(pos, member))
/**
* list_for_each_entry_safe - iterate over list of given type safe against removal of list entry
* @pos: the type * to use as a loop cursor.
* @n: another type * to use as temporary storage
* @head: the head for your list.
* @member: the name of the list_head within the struct.
*/
#define list_for_each_entry_safe(pos, n, head, member) \
for (pos = list_first_entry(head, typeof(*pos), member), \
n = list_next_entry(pos, member); \
&pos->member != (head); \
pos = n, n = list_next_entry(n, member))
/**
* list_for_each_entry_safe_continue - continue list iteration safe against removal
* @pos: the type * to use as a loop cursor.
* @n: another type * to use as temporary storage
* @head: the head for your list.
* @member: the name of the list_head within the struct.
*
* Iterate over list of given type, continuing after current point,
* safe against removal of list entry.
*/
#define list_for_each_entry_safe_continue(pos, n, head, member) \
for (pos = list_next_entry(pos, member), \
n = list_next_entry(pos, member); \
&pos->member != (head); \
pos = n, n = list_next_entry(n, member))
/**
* list_for_each_entry_safe_from - iterate over list from current point safe against removal
* @pos: the type * to use as a loop cursor.
* @n: another type * to use as temporary storage
* @head: the head for your list.
* @member: the name of the list_head within the struct.
*
* Iterate over list of given type from current point, safe against
* removal of list entry.
*/
#define list_for_each_entry_safe_from(pos, n, head, member) \
for (n = list_next_entry(pos, member); \
&pos->member != (head); \
pos = n, n = list_next_entry(n, member))
/**
* list_for_each_entry_safe_reverse - iterate backwards over list safe against removal
* @pos: the type * to use as a loop cursor.
* @n: another type * to use as temporary storage
* @head: the head for your list.
* @member: the name of the list_head within the struct.
*
* Iterate backwards over list of given type, safe against removal
* of list entry.
*/
#define list_for_each_entry_safe_reverse(pos, n, head, member) \
for (pos = list_last_entry(head, typeof(*pos), member), \
n = list_prev_entry(pos, member); \
&pos->member != (head); \
pos = n, n = list_prev_entry(n, member))
/**
* list_safe_reset_next - reset a stale list_for_each_entry_safe loop
* @pos: the loop cursor used in the list_for_each_entry_safe loop
* @n: temporary storage used in list_for_each_entry_safe
* @member: the name of the list_head within the struct.
*
* list_safe_reset_next is not safe to use in general if the list may be
* modified concurrently (eg. the lock is dropped in the loop body). An
* exception to this is if the cursor element (pos) is pinned in the list,
* and list_safe_reset_next is called after re-taking the lock and before
* completing the current iteration of the loop body.
*/
#define list_safe_reset_next(pos, n, member) \
n = list_next_entry(pos, member)
/*
* Double linked lists with a single pointer list head.
* Mostly useful for hash tables where the two pointer list head is
* too wasteful.
* You lose the ability to access the tail in O(1).
*/
#define HLIST_HEAD_INIT { .first = NULL }
#define HLIST_HEAD(name) struct hlist_head name = { .first = NULL }
#define INIT_HLIST_HEAD(ptr) ((ptr)->first = NULL)
static inline void INIT_HLIST_NODE(struct hlist_node *h) {
h->next = NULL;
h->pprev = NULL;
}
static inline int hlist_unhashed(const struct hlist_node *h) {
return !h->pprev;
}
static inline int hlist_empty(const struct hlist_head *h) {
return !h->first;
}
static inline void __hlist_del(struct hlist_node *n) {
struct hlist_node *next = n->next;
struct hlist_node **pprev = n->pprev;
*pprev = next;
if (next)
next->pprev = pprev;
}
static inline void hlist_del(struct hlist_node *n) {
__hlist_del(n);
n->next = LIST_POISON1;
n->pprev = LIST_POISON2;
}
static inline void hlist_del_init(struct hlist_node *n) {
if (!hlist_unhashed(n)) {
__hlist_del(n);
INIT_HLIST_NODE(n);
}
}
static inline void hlist_add_head(struct hlist_node *n, struct hlist_head *h) {
struct hlist_node *first = h->first;
n->next = first;
if (first) {
first->pprev = &n->next;
}
h->first = n;
n->pprev = &h->first;
}
/* next must be != NULL */
static inline void hlist_add_before(struct hlist_node *n,
struct hlist_node *next) {
n->pprev = next->pprev;
n->next = next;
next->pprev = &n->next;
*(n->pprev) = n;
}
static inline void hlist_add_behind(struct hlist_node *n,
struct hlist_node *prev) {
n->next = prev->next;
prev->next = n;
n->pprev = &prev->next;
if (n->next) {
n->next->pprev = &n->next;
}
}
/* after that we'll appear to be on some hlist and hlist_del will work */
static inline void hlist_add_fake(struct hlist_node *n) {
n->pprev = &n->next;
}
/*
* Move a list from one list head to another. Fixup the pprev
* reference of the first entry if it exists.
*/
static inline void hlist_move_list(struct hlist_head *old,
struct hlist_head *new) {
new->first = old->first;
if (new->first) {
new->first->pprev = &new->first;
}
old->first = NULL;
}
#define hlist_entry(ptr, type, member) container_of(ptr,type,member)
#define hlist_for_each(pos, head) \
for (pos = (head)->first; pos ; pos = pos->next)
#define hlist_for_each_safe(pos, n, head) \
for (pos = (head)->first; pos && ({ n = pos->next; 1; }); \
pos = n)
#define hlist_entry_safe(ptr, type, member) \
({ typeof(ptr) ____ptr = (ptr); \
____ptr ? hlist_entry(____ptr, type, member) : NULL; \
})
/**
* hlist_for_each_entry - iterate over list of given type
* @pos: the type * to use as a loop cursor.
* @head: the head for your list.
* @member: the name of the hlist_node within the struct.
*/
#define hlist_for_each_entry(pos, head, member) \
for (pos = hlist_entry_safe((head)->first, typeof(*(pos)), member);\
pos; \
pos = hlist_entry_safe((pos)->member.next, typeof(*(pos)), member))
/**
* hlist_for_each_entry_continue - iterate over a hlist continuing after current point
* @pos: the type * to use as a loop cursor.
* @member: the name of the hlist_node within the struct.
*/
#define hlist_for_each_entry_continue(pos, member) \
for (pos = hlist_entry_safe((pos)->member.next, typeof(*(pos)), member);\
pos; \
pos = hlist_entry_safe((pos)->member.next, typeof(*(pos)), member))
/**
* hlist_for_each_entry_from - iterate over a hlist continuing from current point
* @pos: the type * to use as a loop cursor.
* @member: the name of the hlist_node within the struct.
*/
#define hlist_for_each_entry_from(pos, member) \
for (; pos; \
pos = hlist_entry_safe((pos)->member.next, typeof(*(pos)), member))
/**
* hlist_for_each_entry_safe - iterate over list of given type safe against removal of list entry
* @pos: the type * to use as a loop cursor.
* @n: another &struct hlist_node to use as temporary storage
* @head: the head for your list.
* @member: the name of the hlist_node within the struct.
*/
#define hlist_for_each_entry_safe(pos, n, head, member) \
for (pos = hlist_entry_safe((head)->first, typeof(*pos), member);\
pos && ({ n = pos->member.next; 1; }); \
pos = hlist_entry_safe(n, typeof(*pos), member))
#endif // LIST_H_

View File

@@ -7,10 +7,24 @@
#include <sys/types.h> #include <sys/types.h>
#include <sys/stat.h> #include <sys/stat.h>
#include <fcntl.h> #include <fcntl.h>
#include <time.h>
#include "list.h"
#include "MESA_shm_ring_queue.h" #include "MESA_shm_ring_queue.h"
#define BUFSIZE 256 #define DEFAUT_BUF_SIZE 256
#define CONSUMER_SUCCESS 0
#define CONSUMER_ERROR -1
struct log_file_list{
char log_file_pre[MESA_SHM_LOG_PATH_LEN];
char real_log_file[MESA_SHM_LOG_PATH_LEN];
struct tm create_date;
int fd;
dev_t dev;
ino_t ino;
struct list_head list;
};
struct log_file_list g_log_file_list;
void init_ring_queue_head_arrray(struct MESA_shm_overview *ovw, struct MESA_shm_queue_head **ring_queue_head) void init_ring_queue_head_arrray(struct MESA_shm_overview *ovw, struct MESA_shm_queue_head **ring_queue_head)
{ {
int i = 0; int i = 0;
@@ -61,9 +75,9 @@ void consumer_daemo()
int consumer_is_running(char *process_name) int consumer_is_running(char *process_name)
{ {
FILE *fp = NULL; FILE *fp = NULL;
char buf[BUFSIZE] = {0}; char buf[DEFAUT_BUF_SIZE] = {0};
int count = 0; int count = 0;
char command[BUFSIZE] = {0}; char command[DEFAUT_BUF_SIZE] = {0};
if(process_name == NULL){ if(process_name == NULL){
return 0; return 0;
} }
@@ -94,6 +108,149 @@ char *get_exe_name(char *argv)
} }
return p; return p;
} }
struct log_file_list *get_log_file_node(char *log_file)
{
struct log_file_list *tmp;
struct log_file_list *n;
list_for_each_entry_safe(tmp, n, &g_log_file_list.list, list){
if(strcmp(tmp->log_file_pre, log_file) == 0){
return tmp;
}
}
return NULL;
}
struct log_file_list * create_log_file_node(char *log_file)
{
struct log_file_list *node;
struct tm date;
time_t curtime = 0;
struct stat buf;
int n = 0;
node = (struct log_file_list *)malloc(sizeof(struct log_file_list));
if(node == NULL){
return NULL;
}
memset(node, 0 ,sizeof(struct log_file_list));
memcpy(node->log_file_pre, log_file, strlen(log_file));
curtime = time(NULL);
localtime_r(&curtime, &date);
n = snprintf(node->real_log_file, sizeof(node->real_log_file), "%s.%d-%d-%d",
node->log_file_pre, date.tm_year + 1900, date.tm_mon, date.tm_mday);
if(n >= sizeof(node->real_log_file)){
goto error;
}
node->fd = open(node->real_log_file, O_RDWR | O_CREAT | O_APPEND, 0666);
if(node->fd < 0){
goto error;
}
if(stat(node->real_log_file, &buf) < 0){
goto error;
}
node->dev = buf.st_dev;
node->ino = buf.st_ino;
node->create_date = date;
list_add(&node->list, &g_log_file_list.list);
return node;
error:
if(node != NULL){
free(node);
}
return NULL;
}
void get_cur_date(struct tm *date)
{
time_t curtime = 0;
curtime = time(NULL);
localtime_r(&curtime, date);
return ;
}
void get_cur_strftime(char *buf, int maxlen)
{
struct tm local;
time_t curtime = time(NULL);
localtime_r(&curtime, &local);
strftime(buf, maxlen, "%c", &local);
return ;
}
int reopen_log_file(struct log_file_list *node)
{
struct stat buf;
close(node->fd);
node->fd = open(node->real_log_file, O_RDWR | O_CREAT | O_APPEND, 0666);
if(node->fd < 0){
return CONSUMER_ERROR;
}
if(stat(node->real_log_file, &buf) < 0){
return CONSUMER_ERROR;
}
node->dev = buf.st_dev;
node->ino = buf.st_ino;
return CONSUMER_SUCCESS;
}
int check_reopen_log_file(struct log_file_list *node)
{
struct stat buf;
struct tm date;
int n = 0;
get_cur_date(&date);
if(date.tm_year != node->create_date.tm_year
|| date.tm_mon != node->create_date.tm_mon
|| date.tm_mday != node->create_date.tm_mday){
node->create_date = date;
n = snprintf(node->real_log_file, sizeof(node->real_log_file), "%s.%d-%d-%d", node->log_file_pre,
node->create_date.tm_year, node->create_date.tm_mon, node->create_date.tm_mday);
if(n >= sizeof(node->real_log_file)){
return CONSUMER_ERROR;
}
return reopen_log_file(node);
}
if(stat(node->real_log_file, &buf) < 0){
return reopen_log_file(node); /* we'll have to restat the newly created file to get the inode info*/
}
if(buf.st_dev != node->dev || buf.st_ino != node->ino){
return reopen_log_file(node);
}
return CONSUMER_SUCCESS;
}
void consumer_ring_queue_to_file(struct MESA_shm_queue_head *head)
{
char *p_file = NULL;
int *p_payload_len = NULL;
char *payload = NULL;
int payload_len = 0;
char buf[MESA_SHM_LOG_BUF_PREFIX_LEN + MESA_SHM_RING_QUEUE_BLOCK_BUFLEN] = {0};
int n = 0;
char strtime[DEFAUT_BUF_SIZE] = {0};
struct log_file_list *node = NULL;
get_cur_strftime(strtime, sizeof(strtime));
while(!MESA_shm_ring_queue_is_empty(head)){
p_file = (char *)(head + 1) + (head->blksize * head->rd_idx);
node = get_log_file_node(p_file);
if(node == NULL){
node = create_log_file_node(p_file);
if(node == NULL){
head->rd_idx = (head->rd_idx + 1) % head->blknum;
continue ;
}
}else{
if(check_reopen_log_file(node) != CONSUMER_SUCCESS){
head->rd_idx = (head->rd_idx + 1) % head->blknum;
continue ;
}
}
p_payload_len = (int *)(p_file + MESA_SHM_LOG_PATH_LEN);
payload_len = *p_payload_len;
payload = (char *)(p_payload_len + 1);
n = snprintf(buf, sizeof(buf), "%s, %s\n", strtime, payload);
write(node->fd, buf, n);
head->rd_idx = (head->rd_idx + 1) % head->blknum;
}
}
int main(int argc, char **argv) int main(int argc, char **argv)
{ {
@@ -108,15 +265,13 @@ int main(int argc, char **argv)
struct MESA_shm_overview *tmp_ovw = NULL; struct MESA_shm_overview *tmp_ovw = NULL;
struct MESA_shm_queue_head *ring_queue_head[MESA_SHM_RING_QUEUE_NUM] = {NULL}; struct MESA_shm_queue_head *ring_queue_head[MESA_SHM_RING_QUEUE_NUM] = {NULL};
int log_file_fd = -1; int log_file_fd = -1;
struct log_file_list *log_file_node = NULL;
INIT_LIST_HEAD(&g_log_file_list.list);
shm_overview = MESA_shm_alloc_overview(); shm_overview = MESA_shm_alloc_overview();
if(shm_overview == NULL){ if(shm_overview == NULL){
return 0; return 0;
} }
init_ring_queue_head_arrray(shm_overview, ring_queue_head); init_ring_queue_head_arrray(shm_overview, ring_queue_head);
log_file_fd = open("/root/MESA_log", O_RDWR | O_CREAT | O_APPEND, 0666);
if(log_file_fd < 0){
return 0;
}
while(1){ while(1){
for(i = 0; i< MESA_SHM_RING_QUEUE_NUM; i++){ for(i = 0; i< MESA_SHM_RING_QUEUE_NUM; i++){
tmp_ovw = shm_overview + i; tmp_ovw = shm_overview + i;
@@ -129,7 +284,9 @@ int main(int argc, char **argv)
break ; break ;
} }
} }
MESA_shm_write_ring_queue_to_file(log_file_fd, ring_queue_head[i]); if(!MESA_shm_ring_queue_is_empty(ring_queue_head[i])){
consumer_ring_queue_to_file(ring_queue_head[i]);
}
} }
usleep(5000); usleep(5000);
} }

View File

@@ -12,6 +12,7 @@
#include <string.h> #include <string.h>
#include <unistd.h> #include <unistd.h>
#include <time.h> #include <time.h>
#include <limits.h>
#define MAX_HANDLE_LOG_PATH 4096 #define MAX_HANDLE_LOG_PATH 4096
static int g_zlog_inited = 0; static int g_zlog_inited = 0;
@@ -20,7 +21,6 @@ static char global_conf_filepath[MAX_HANDLE_LOG_PATH] = "";
static char tmp_conf_filepath[MAX_HANDLE_LOG_PATH] = ""; static char tmp_conf_filepath[MAX_HANDLE_LOG_PATH] = "";
#define MESA_FORMAT_RULE_MAX 16 #define MESA_FORMAT_RULE_MAX 16
#define MESA_PTHREAD_CACHE_BUF_DEFAULT_LEN 1024
#define MESA_PTHREAD_FMT_BUF_DEFAULT_LEN 256 #define MESA_PTHREAD_FMT_BUF_DEFAULT_LEN 256
pthread_key_t MESA_pthread_key; pthread_key_t MESA_pthread_key;
@@ -47,6 +47,9 @@ typedef struct log_handle_t
char runtime_log_file[MAX_HANDLE_LOG_PATH]; char runtime_log_file[MAX_HANDLE_LOG_PATH];
int fmt_buf_len; int fmt_buf_len;
struct MESA_fmt_obj fmt_rule[MESA_FORMAT_RULE_MAX]; struct MESA_fmt_obj fmt_rule[MESA_FORMAT_RULE_MAX];
char real_log_file[MESA_SHM_LOG_PATH_LEN];
int real_log_file_len;
int pid;
} log_handle_t; } log_handle_t;
@@ -213,17 +216,15 @@ struct MESA_pthread_private *MESA_create_pthread_private(void *handle)
goto error; goto error;
} }
pri->fmt_buf_len = p_handle->fmt_buf_len; pri->fmt_buf_len = p_handle->fmt_buf_len;
pri->cache_buf_len = MESA_PTHREAD_CACHE_BUF_DEFAULT_LEN; pri->cache_buf_len = MESA_SHM_RING_QUEUE_BLOCK_BUFLEN;
pri->cache_buf = malloc(pri->cache_buf_len); pri->cache_buf = malloc(pri->cache_buf_len);
if(pri->cache_buf == NULL){ if(pri->cache_buf == NULL){
goto error; goto error;
} }
pri->ring_queue_head = MESA_shm_get_ring_queue(); pri->ring_queue_head = MESA_shm_get_ring_queue(p_handle->pid);
/* if(pri->ring_queue_head == NULL){
if(pri->queue_head == NULL){
goto error; goto error;
} }
*/
return pri; return pri;
error: error:
@@ -271,7 +272,8 @@ void *MESA_create_runtime_log_handle(const char *file_path, int level)
{ {
if(file_path == NULL) if(file_path == NULL)
return NULL; return NULL;
char rpath[PATH_MAX] = {0};
int rpath_len = 0;
zlog_category_t *zc = NULL; zlog_category_t *zc = NULL;
log_handle_t *p_handle = NULL; log_handle_t *p_handle = NULL;
@@ -302,6 +304,13 @@ void *MESA_create_runtime_log_handle(const char *file_path, int level)
p_handle->runtime_log_level = level; p_handle->runtime_log_level = level;
p_handle->zc = zc; p_handle->zc = zc;
p_handle->fmt_buf_len = MESA_PTHREAD_FMT_BUF_DEFAULT_LEN; p_handle->fmt_buf_len = MESA_PTHREAD_FMT_BUF_DEFAULT_LEN;
realpath(file_path, rpath);
rpath_len = strlen(rpath);
if(rpath_len < MESA_SHM_LOG_PATH_LEN){
memcpy(p_handle->real_log_file, rpath, rpath_len);
p_handle->real_log_file_len = rpath_len;
}
p_handle->pid = getpid();
pthread_once(&MESA_pthread_key_once, MESA_alloc_pthread_key); pthread_once(&MESA_pthread_key_once, MESA_alloc_pthread_key);
return (void *)p_handle; return (void *)p_handle;
} }
@@ -328,7 +337,6 @@ void MESA_handle_runtime_log(void *handle, int level, const char *module, const
if(p_handle->zc == NULL)return; if(p_handle->zc == NULL)return;
va_list ap; va_list ap;
va_list ap_bk;
va_start(ap, fmt); va_start(ap, fmt);
struct MESA_pthread_private *pri = (struct MESA_pthread_private *)pthread_getspecific(MESA_pthread_key); struct MESA_pthread_private *pri = (struct MESA_pthread_private *)pthread_getspecific(MESA_pthread_key);
if(pri == NULL){ if(pri == NULL){
@@ -338,22 +346,10 @@ void MESA_handle_runtime_log(void *handle, int level, const char *module, const
} }
pthread_setspecific(MESA_pthread_key,(void*)pri); pthread_setspecific(MESA_pthread_key,(void*)pri);
} }
va_copy(ap_bk, ap); portable_vsnprintf(handle, pri->cache_buf, pri->cache_buf_len, fmt, ap);
int n = portable_vsnprintf(handle, pri->cache_buf, pri->cache_buf_len, fmt, ap_bk); MESA_shm_copy_buf_to_ring_queue(pri->cache_buf, pri->cache_buf_len, pri->ring_queue_head, p_handle->real_log_file,p_handle->real_log_file_len);
if(n >= pri->cache_buf_len){
char *p = realloc(pri->cache_buf, n + 1);
if(p != NULL){
pri->cache_buf_len = n + 1;
pri->cache_buf = p;
va_copy(ap_bk, ap);
n = portable_vsnprintf(handle, pri->cache_buf, pri->cache_buf_len, fmt, ap_bk);
}
}
MESA_shm_copy_buf_to_ring_queue(pri->cache_buf, n, pri->ring_queue_head);
va_end(ap);
va_end(ap_bk);
zlog(p_handle->zc, p_handle->runtime_log_file, strlen(p_handle->runtime_log_file), module, strlen(module), __LINE__, level, "%s", pri->cache_buf); zlog(p_handle->zc, p_handle->runtime_log_file, strlen(p_handle->runtime_log_file), module, strlen(module), __LINE__, level, "%s", pri->cache_buf);
va_end(ap);
return ; return ;
} }

View File

@@ -1,14 +1,15 @@
#include <stdio.h> #include <stdio.h>
#include <sys/ipc.h> #include <sys/ipc.h>
#include <sys/shm.h> #include <sys/shm.h>
#include <sys/sem.h>
#include <pthread.h> #include <pthread.h>
#include <string.h> #include <string.h>
#include <stdlib.h> #include <stdlib.h>
#include <unistd.h> #include <unistd.h>
#include <string.h>
#include <errno.h> #include <errno.h>
#include "MESA_handle_logger.h" #include "MESA_handle_logger.h"
#include "MESA_shm_ring_queue.h" #include "MESA_shm_ring_queue.h"
#include "list.h"
struct MESA_shm_overview *MESA_shm_ovw = NULL; struct MESA_shm_overview *MESA_shm_ovw = NULL;
@@ -61,7 +62,7 @@ void MESA_shm_init_overview()
return ; return ;
} }
void MESA_shm_init_ring_queue(struct MESA_shm_queue_head *head, struct MESA_shm_overview *ovw) void MESA_shm_init_new_ring_queue(struct MESA_shm_queue_head *head, struct MESA_shm_overview *ovw)
{ {
head->blksize = MESA_SHM_RING_QUEUE_BLOCK_SIZE; head->blksize = MESA_SHM_RING_QUEUE_BLOCK_SIZE;
head->blknum = MESA_SHM_RING_QUEUE_BLOCK_NUM; head->blknum = MESA_SHM_RING_QUEUE_BLOCK_NUM;
@@ -108,14 +109,13 @@ struct MESA_shm_queue_head *MESA_shm_alloc_new_ring_queue(struct MESA_shm_overvi
if(shmid == -1){ if(shmid == -1){
return NULL; return NULL;
} }
ovw->shmid = shmid;
head = (struct MESA_shm_queue_head *)shmat(shmid, NULL, 0); head = (struct MESA_shm_queue_head *)shmat(shmid, NULL, 0);
}else{ }else{
ovw->shmid = shmid;
head = (struct MESA_shm_queue_head *)shmat(shmid, NULL, 0); head = (struct MESA_shm_queue_head *)shmat(shmid, NULL, 0);
} }
if(head != NULL){ if(head != NULL){
MESA_shm_init_ring_queue(head, ovw); MESA_shm_init_new_ring_queue(head, ovw);
ovw->shmid = shmid;
}else{ }else{
pthread_mutex_unlock(&ovw->mutex); pthread_mutex_unlock(&ovw->mutex);
} }
@@ -135,9 +135,13 @@ struct MESA_shm_queue_head *MESA_shm_try_ring_queue(struct MESA_shm_overview *ov
if(head == NULL){ if(head == NULL){
pthread_mutex_unlock(&ovw->mutex); pthread_mutex_unlock(&ovw->mutex);
} }
if(!MESA_shm_ring_queue_is_empty(head)){
pthread_mutex_unlock(&ovw->mutex);
head == NULL;
}
return head; return head;
} }
struct MESA_shm_queue_head *MESA_shm_get_ring_queue() struct MESA_shm_queue_head *MESA_shm_get_ring_queue(int pid)
{ {
int i = 0; int i = 0;
struct MESA_shm_overview *tmp_ovw = NULL; struct MESA_shm_overview *tmp_ovw = NULL;
@@ -157,7 +161,7 @@ struct MESA_shm_queue_head *MESA_shm_get_ring_queue()
} }
} }
if(head != NULL){ if(head != NULL){
MESA_shm_init_ring_queue(head, tmp_ovw); tmp_ovw->producer_pid = pid;
} }
return head; return head;
} }
@@ -177,47 +181,29 @@ int MESA_shm_ring_queue_is_full(struct MESA_shm_queue_head *head)
return 0; return 0;
} }
} }
int MESA_shm_copy_buf_to_ring_queue(char *buf, int buflen, struct MESA_shm_queue_head *head) int MESA_shm_copy_buf_to_ring_queue(char *buf, int buflen, struct MESA_shm_queue_head *head, char *log_file, int log_file_len)
{ {
int len = buflen < MESA_SHM_RING_QUEUE_BLOCK_BUFLEN?buflen:MESA_SHM_RING_QUEUE_BLOCK_BUFLEN; int len = buflen < (MESA_SHM_RING_QUEUE_BLOCK_BUFLEN -1)?buflen:(MESA_SHM_RING_QUEUE_BLOCK_BUFLEN - 1);
int *wr_len = NULL; char *p_file = NULL;
char *wr_blk = NULL; int *p_buf_len = NULL;
char *wr_pos = NULL; char *p_buf = NULL;
if(head == NULL){ if(head == NULL){
return 0; return 0;
} }
if(log_file_len == 0){
return 0;
}
if(MESA_shm_ring_queue_is_full(head)){ if(MESA_shm_ring_queue_is_full(head)){
return 0; return 0;
} }
wr_blk = (char *)(head + 1) + (head->blksize * head->wr_idx); p_file = (char *)(head + 1) + (head->blksize * head->wr_idx);
wr_len = (int *)wr_blk; memcpy(p_file, log_file, log_file_len);
*wr_len = len + 1; p_file[log_file_len] = '\0';
wr_pos = (char *)(wr_len + 1); p_buf_len = (int *)(p_file + MESA_SHM_LOG_PATH_LEN);
wr_pos[len] = '\n'; *p_buf_len = len;
memcpy(wr_pos, buf, len); p_buf = (char *)(p_buf_len + 1);
memcpy(p_buf, buf, len);
p_buf[len] = '\0';
head->wr_idx = (head->wr_idx + 1) % head->blknum; head->wr_idx = (head->wr_idx + 1) % head->blknum;
return len; return len;
} }
void MESA_shm_write_ring_queue_to_file(int fd, struct MESA_shm_queue_head *head)
{
int len = 0;
int write_len = 0;
int *rd_len = NULL;
char *rd_blk = NULL;
char *rd_pos = NULL;
while(!MESA_shm_ring_queue_is_empty(head)){
rd_blk = (char *)(head + 1) + (head->blksize * head->rd_idx);
rd_len = (int *)rd_blk;
len = *rd_len;
rd_pos = (char *)(rd_len + 1);
write_len = write(fd, rd_pos, len);
if(write_len < 0){
printf("write file error, func=%s, line=%d\n",__FUNCTION__, __LINE__);
}
/*fsync(fd);*/
head->rd_idx = (head->rd_idx + 1) % head->blknum;
}
return ;
}

View File

@@ -1,4 +1,4 @@
{ {
global: MESA*runtime_log*;GIT_VERSION_*;MESA_shm_alloc_overview;MESA_shm_write_ring_queue_to_file;MESA_handle_fmt_rule_register; global: MESA*runtime_log*;GIT_VERSION_*;MESA_shm_alloc_overview;MESA_handle_fmt_rule_register;MESA_shm_ring_queue_is_empty;MESA_shm_ring_queue_is_full;
local: *; local: *;
}; };