#include <linux/compat.h>
#include <uapi/linux/zlogger.h>
-#define MB (1 << 20)
-#define KB (1 << 10)
-
-#define DEVICE_COUNT (8)
-#define MAP_SIZE (4 * MB)
-#define BUFFER_SIZE (DEVICE_COUNT * MAP_SIZE)
-#define V_BLOCK_SIZE (2 * KB)
-#define BLOCK_COUNT (BUFFER_SIZE / V_BLOCK_SIZE)
-#define DATA_MAX (V_BLOCK_SIZE - sizeof(struct head_t))
#define MS_PER_SEC (1000)
#define NS_PER_SEC (1000000000UL)
#define ZLOGGER_DEVICE_NAME "zlogger"
#define ZLOGGER_SMACK_LABEL "*"
-#define MAX_TAG_SIZE (32)
-#define MAX_MSG_SIZE (140)
+#define BLOCK_RATIO(count) (count*100/ZLOGGER_BLOCK_COUNT)
-#define BLOCK_RATIO(count) (count*100/BLOCK_COUNT)
-
-#define ZLOG_FD_BUFER (2 * MB)
+#define ZLOG_FD_BUFER (2 * ZLOGGER_MB)
struct thread_t {
uint16_t block;
};
-struct entry_t {
- uint64_t time;
- uint16_t CRC;
- uint16_t len;
- char data[0];
-};
-
-struct head_t {
- uint64_t ts;
- pid_t pid;
- pid_t tid;
- uint16_t offset;
-};
-
-struct block_t {
- struct head_t head;
- char data[DATA_MAX];
-};
-
struct queue_t {
char name[5];
uint16_t front;
const char default_tag[] = "STDOUT";
typedef struct {
char prio;
- char tag[MAX_TAG_SIZE];
- char msg[MAX_MSG_SIZE];
+ char tag[ZLOGGER_TAG_MAX];
+ char msg[ZLOGGER_MSG_MAX];
char *buffer;
size_t buffer_len;
} zlog_file_t;
static struct miscdevice zlogger_device;
static int g_init;
-static char *g_shm_ptr[DEVICE_COUNT];
+static char *g_shm_ptr[ZLOGGER_DEVICE_COUNT];
static struct thread_table *g_thread_table;
static struct mutex g_block_mutex;
static inline char *get_shared_memory(int dev_index)
{
- if (dev_index < 0 || dev_index >= DEVICE_COUNT) {
+ if (dev_index < 0 || dev_index >= ZLOGGER_DEVICE_COUNT) {
pr_debug("Invalid index: %d\n", dev_index);
return NULL;
}
return g_shm_ptr[dev_index];
}
-static inline struct block_t *get_block(uint16_t block_index)
+static inline struct zlogger_block *get_block(uint16_t block_index)
{
uint16_t index = block_index - 1;
- int offset = index & (V_BLOCK_SIZE - 1);
- char *p = get_shared_memory(index / V_BLOCK_SIZE);
+ int offset = index & (ZLOGGER_BLOCK_SIZE - 1);
+ char *p = get_shared_memory(index / ZLOGGER_BLOCK_SIZE);
if (!p)
return NULL;
- return (struct block_t *)(p + (offset * V_BLOCK_SIZE));
+ return (struct zlogger_block *)(p + (offset * ZLOGGER_BLOCK_SIZE));
}
static inline void queue_init(struct queue_t *q, const char *name, uint16_t capacity)
pid_t pid = current->tgid;
pid_t tid = current->pid;
uint16_t blk;
- struct block_t *block;
+ struct zlogger_block *block;
if (g_max_thread_id < tid)
g_max_thread_id = tid;
return (long)blk;
}
-static inline struct block_t *get_valid_block(int tid, size_t len, bool is_stdout)
+static inline struct zlogger_block *get_valid_block(int tid, size_t len, bool is_stdout)
{
uint16_t blk = 0;
long r;
blk = get_thread_table(tid, is_stdout);
if (blk != 0) {
- struct block_t *block = get_block(blk);
+ struct zlogger_block *block = get_block(blk);
if (!block)
return NULL;
- if (block->head.offset + len < DATA_MAX)
+ if (block->head.offset + len < ZLOGGER_DATA_MAX)
return block;
}
static int zlogger_mmap(struct file *filep, struct vm_area_struct *vma)
{
- const int PAGES_PER_MAP = MAP_SIZE / PAGE_SIZE;
+ const int PAGES_PER_MAP = ZLOGGER_MAP_SIZE / PAGE_SIZE;
int dev_index = (int)vma->vm_pgoff / PAGES_PER_MAP;
unsigned long offset = vma->vm_pgoff % PAGES_PER_MAP;
unsigned long size = vma->vm_end - vma->vm_start;
char *p;
struct page *page;
- if (dev_index > DEVICE_COUNT || offset != 0 || size > MAP_SIZE) {
+ if (dev_index > ZLOGGER_DEVICE_COUNT || offset != 0 || size > ZLOGGER_MAP_SIZE) {
pr_err("mmap failed: dev(%d) offset(%lu), size(%lu), pgoff(%lu)\n", dev_index, offset, size, vma->vm_pgoff);
return -EINVAL;
}
uint64_t ts = ktime_get_ns();
uint16_t *slt = (uint16_t *)&ts;
const pid_t tid = current->pid;
- size_t hd_size = sizeof(struct entry_t);
+ size_t hd_size = sizeof(struct zlogger_entry);
size_t prio_size = 1;
- size_t tag_size = strnlen(tag, MAX_TAG_SIZE) + 1;
- size_t msg_size = strnlen(msg, MAX_MSG_SIZE) + 1;
+ size_t tag_size = strnlen(tag, ZLOGGER_TAG_MAX) + 1;
+ size_t msg_size = strnlen(msg, ZLOGGER_MSG_MAX) + 1;
size_t entry_size = hd_size + prio_size + tag_size + msg_size;
- struct block_t *block = get_valid_block(tid, entry_size, true);
- struct entry_t *entry;
- struct entry_t tmp;
+ struct zlogger_block *block = get_valid_block(tid, entry_size, true);
+ struct zlogger_entry *entry;
+ struct zlogger_entry tmp;
if (block == NULL) {
pr_err("_zlog_write: no memory\n");
return -ENOMEM;
}
- entry = (struct entry_t *)(block->data + block->head.offset);
+ entry = (struct zlogger_entry *)(block->data + block->head.offset);
- if ((char *)entry + entry_size > (char *)block + V_BLOCK_SIZE) {
+ if ((char *)entry + entry_size > (char *)block + ZLOGGER_BLOCK_SIZE) {
// TODO: This is not unexpected: the offset can change at any time,
// as it is userspace writable.
pr_err("[%d] block:%p(tid:%d offset:%x) entry:%p(%zu)\n",
static long zlogger_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
{
- if (cmd == IOCTL_COMMAND_ALLOC)
+ if (cmd == ZLOGGER_IOCTL_COMMAND_ALLOC)
return alloc_block_for_thread(false);
#ifdef CONFIG_COMPAT
void __user *argp = (void __user *)arg;
#endif
- if (cmd == SET_DEFAULT_PRIORITY) {
+ if (cmd == ZLOGGER_IOCTL_COMMAND_SET_DEFAULT_PRIORITY) {
if( (!file->private_data ) && init_file_zlog_tag_data(file)) {
pr_err("zlogger_write init zlog tag data failed\n");
return -ENOMEM;
return update_prio(zlog_file_data, argp);
}
- if(cmd == SET_DEFAULT_TAG) {
+ if(cmd == ZLOGGER_IOCTL_COMMAND_SET_DEFAULT_TAG) {
if( (!file->private_data ) && init_file_zlog_tag_data(file)) {
pr_err("zlogger_write init zlog tag data failed\n");
return -ENOMEM;
zlog_file_t *zlog_file_data = (zlog_file_t *)file->private_data;
- return copy_from_user(&zlog_file_data->tag, (char *)(uintptr_t)argp, strnlen((char *)(uintptr_t)argp, MAX_TAG_SIZE));
+ return copy_from_user(&zlog_file_data->tag, (char *)(uintptr_t)argp, strnlen((char *)(uintptr_t)argp, ZLOGGER_TAG_MAX));
}
return -EINVAL;
return -EINVAL;
}
- if (value < 1 || value > BLOCK_COUNT)
+ if (value < 1 || value > ZLOGGER_BLOCK_COUNT)
return -EINVAL;
g_block = value;
static ssize_t block_show(struct device *dev,
struct device_attribute *attr, char *buf)
{
- struct block_t *block = get_block((uint16_t)g_block);
+ struct zlogger_block *block = get_block((uint16_t)g_block);
if (!block)
return snprintf(buf, PAGE_SIZE, "[%d] Invalid block\n", g_block);
return snprintf(buf, PAGE_SIZE, "[%d] pid:%u tid:%u offset:%u %llu %llu\n",
g_block, block->head.pid, block->head.tid, block->head.offset,
- ((struct entry_t *)(block->data))->time, block->head.ts);
+ ((struct zlogger_entry *)(block->data))->time, block->head.ts);
}
static DEVICE_ATTR_RW(block);
return -ENOMEM;
hash_init(g_thread_table->data);
- for (g_shm_ptr_i = 0; g_shm_ptr_i < DEVICE_COUNT; g_shm_ptr_i++) {
- g_shm_ptr[g_shm_ptr_i] = kzalloc(MAP_SIZE, GFP_KERNEL);
+ for (g_shm_ptr_i = 0; g_shm_ptr_i < ZLOGGER_DEVICE_COUNT; g_shm_ptr_i++) {
+ g_shm_ptr[g_shm_ptr_i] = kzalloc(ZLOGGER_MAP_SIZE, GFP_KERNEL);
if (g_shm_ptr[g_shm_ptr_i] == NULL) {
r = -ENOMEM;
goto out_free_g_thread_table_g_shm_ptr;
init_completion(&g_completion);
- queue_init(&g_free_q, "free", BLOCK_COUNT);
- for (i = 1; i <= BLOCK_COUNT; i++)
+ queue_init(&g_free_q, "free", ZLOGGER_BLOCK_COUNT);
+ for (i = 1; i <= ZLOGGER_BLOCK_COUNT; i++)
queue_push(&g_free_q, i);
#ifdef CONFIG_TRACE_CLOCK
kfree(g_thread_table);
g_thread_table = NULL;
- for (i = 0; i < DEVICE_COUNT; i++) {
+ for (i = 0; i < ZLOGGER_DEVICE_COUNT; i++) {
kfree(g_shm_ptr[i]);
g_shm_ptr[i] = NULL;
}
return -1;
}
- ioctl(fd, SET_DEFAULT_PRIORITY, (char)DLOG_DEFAULT);
+ ioctl(fd, ZLOGGER_IOCTL_COMMAND_SET_DEFAULT_PRIORITY, (char)DLOG_DEFAULT);
write(fd, prio_default, strlen(prio_default));
- ioctl(fd, SET_DEFAULT_PRIORITY, (char)DLOG_VERBOSE);
+ ioctl(fd, ZLOGGER_IOCTL_COMMAND_SET_DEFAULT_PRIORITY, (char)DLOG_VERBOSE);
write(fd, prio_verbose, strlen(prio_verbose));
- ioctl(fd, SET_DEFAULT_PRIORITY, (char)DLOG_DEBUG);
+ ioctl(fd, ZLOGGER_IOCTL_COMMAND_SET_DEFAULT_PRIORITY, (char)DLOG_DEBUG);
write(fd, prio_debug, strlen(prio_debug));
- ioctl(fd, SET_DEFAULT_PRIORITY, (char)DLOG_INFO);
+ ioctl(fd, ZLOGGER_IOCTL_COMMAND_SET_DEFAULT_PRIORITY, (char)DLOG_INFO);
write(fd, prio_info, strlen(prio_info));
- ioctl(fd, SET_DEFAULT_PRIORITY, (char)DLOG_WARN);
+ ioctl(fd, ZLOGGER_IOCTL_COMMAND_SET_DEFAULT_PRIORITY, (char)DLOG_WARN);
write(fd, prio_warn, strlen(prio_warn));
- ioctl(fd, SET_DEFAULT_PRIORITY, (char)DLOG_ERROR);
+ ioctl(fd, ZLOGGER_IOCTL_COMMAND_SET_DEFAULT_PRIORITY, (char)DLOG_ERROR);
write(fd, prio_error, strlen(prio_error));
- ioctl(fd, SET_DEFAULT_PRIORITY, (char)DLOG_FATAL);
+ ioctl(fd, ZLOGGER_IOCTL_COMMAND_SET_DEFAULT_PRIORITY, (char)DLOG_FATAL);
write(fd, prio_fatal, strlen(prio_fatal));
- ioctl(fd, SET_DEFAULT_PRIORITY, (char)DLOG_SILENT);
+ ioctl(fd, ZLOGGER_IOCTL_COMMAND_SET_DEFAULT_PRIORITY, (char)DLOG_SILENT);
write(fd, prio_silent, strlen(prio_silent));
- ioctl(fd, SET_DEFAULT_TAG, "TESTPROGRAM_1");
+ ioctl(fd, ZLOGGER_IOCTL_COMMAND_SET_DEFAULT_TAG, "TESTPROGRAM_1");
write(fd, test_message, strlen(test_message));
- ioctl(fd, SET_DEFAULT_PRIORITY, (char)DLOG_DEFAULT);
+ ioctl(fd, ZLOGGER_IOCTL_COMMAND_SET_DEFAULT_PRIORITY, (char)DLOG_DEFAULT);
write(fd, prio_default, strlen(prio_default));
- ioctl(fd, SET_DEFAULT_PRIORITY, (char)DLOG_VERBOSE);
+ ioctl(fd, ZLOGGER_IOCTL_COMMAND_SET_DEFAULT_PRIORITY, (char)DLOG_VERBOSE);
write(fd, prio_verbose, strlen(prio_verbose));
- ioctl(fd, SET_DEFAULT_PRIORITY, (char)DLOG_DEBUG);
+ ioctl(fd, ZLOGGER_IOCTL_COMMAND_SET_DEFAULT_PRIORITY, (char)DLOG_DEBUG);
write(fd, prio_debug, strlen(prio_debug));
- ioctl(fd, SET_DEFAULT_PRIORITY, (char)DLOG_INFO);
+ ioctl(fd, ZLOGGER_IOCTL_COMMAND_SET_DEFAULT_PRIORITY, (char)DLOG_INFO);
write(fd, prio_info, strlen(prio_info));
- ioctl(fd, SET_DEFAULT_PRIORITY, (char)DLOG_WARN);
+ ioctl(fd, ZLOGGER_IOCTL_COMMAND_SET_DEFAULT_PRIORITY, (char)DLOG_WARN);
write(fd, prio_warn, strlen(prio_warn));
- ioctl(fd, SET_DEFAULT_PRIORITY, (char)DLOG_ERROR);
+ ioctl(fd, ZLOGGER_IOCTL_COMMAND_SET_DEFAULT_PRIORITY, (char)DLOG_ERROR);
write(fd, prio_error, strlen(prio_error));
- ioctl(fd, SET_DEFAULT_PRIORITY, (char)DLOG_FATAL);
+ ioctl(fd, ZLOGGER_IOCTL_COMMAND_SET_DEFAULT_PRIORITY, (char)DLOG_FATAL);
write(fd, prio_fatal, strlen(prio_fatal));
- ioctl(fd, SET_DEFAULT_PRIORITY, (char)DLOG_SILENT);
+ ioctl(fd, ZLOGGER_IOCTL_COMMAND_SET_DEFAULT_PRIORITY, (char)DLOG_SILENT);
write(fd, prio_silent, strlen(prio_silent));
- ioctl(fd, SET_DEFAULT_TAG, "TESTPROGRAM_2");
+ ioctl(fd, ZLOGGER_IOCTL_COMMAND_SET_DEFAULT_TAG, "TESTPROGRAM_2");
write(fd, test_message, strlen(test_message));
return 0;
}