Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
76 changes: 76 additions & 0 deletions .github/workflows/cmake-multi-platform.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
# This starter workflow is for a CMake project running on multiple platforms. There is a different starter workflow if you just want a single platform.
# See: https://github.com/actions/starter-workflows/blob/main/ci/cmake-single-platform.yml
name: CMake on multiple platforms

on:
push:
branches: [ "master" ]
pull_request:
branches: [ "master" ]

jobs:
build:
runs-on: ${{ matrix.os }}

strategy:
# Set fail-fast to false to ensure that feedback is delivered for all matrix combinations. Consider changing this to true when your workflow is stable.
fail-fast: false

# Set up a matrix to run the following 3 configurations:
# 1. <Windows, Release, latest MSVC compiler toolchain on the default runner image, default generator>
# 2. <Linux, Release, latest GCC compiler toolchain on the default runner image, default generator>
# 3. <Linux, Release, latest Clang compiler toolchain on the default runner image, default generator>
#
# To add more build types (Release, Debug, RelWithDebInfo, etc.) customize the build_type list.
# todo: add windows-latest
matrix:
os: [ubuntu-latest]
build_type: [Release, Debug]
c_compiler: [gcc, clang, cl]
include:
# - os: windows-latest
# c_compiler: cl
# cpp_compiler: cl
- os: ubuntu-latest
c_compiler: gcc
cpp_compiler: g++
- os: ubuntu-latest
c_compiler: clang
cpp_compiler: clang++
exclude:
# - os: windows-latest
# c_compiler: gcc
# - os: windows-latest
# c_compiler: clang
- os: ubuntu-latest
c_compiler: cl

steps:
- uses: actions/checkout@v4

- name: Set reusable strings
# Turn repeated input strings (such as the build output directory) into step outputs. These step outputs can be used throughout the workflow file.
id: strings
shell: bash
run: |
echo "build-output-dir=${{ github.workspace }}/build" >> "$GITHUB_OUTPUT"

- name: Configure CMake
# Configure CMake in a 'build' subdirectory. `CMAKE_BUILD_TYPE` is only required if you are using a single-configuration generator such as make.
# See https://cmake.org/cmake/help/latest/variable/CMAKE_BUILD_TYPE.html?highlight=cmake_build_type
run: >
cmake -B ${{ steps.strings.outputs.build-output-dir }}
-DCMAKE_CXX_COMPILER=${{ matrix.cpp_compiler }}
-DCMAKE_C_COMPILER=${{ matrix.c_compiler }}
-DCMAKE_BUILD_TYPE=${{ matrix.build_type }}
-S ${{ github.workspace }}

- name: Build
# Build your program with the given configuration. Note that --config is needed because the default Windows generator is a multi-config generator (Visual Studio generator).
run: cmake --build ${{ steps.strings.outputs.build-output-dir }} --config ${{ matrix.build_type }}

- name: Test
working-directory: ${{ steps.strings.outputs.build-output-dir }}
# Execute tests defined by the CMake configuration. Note that --build-config is needed because the default Windows generator is a multi-config generator (Visual Studio generator).
# See https://cmake.org/cmake/help/latest/manual/ctest.1.html for more detail
run: ctest --build-config ${{ matrix.build_type }}
39 changes: 0 additions & 39 deletions .github/workflows/cmake-single-platform.yml

This file was deleted.

56 changes: 12 additions & 44 deletions src/fifo.c
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
#define _GNU_SOURCE // For distros like Centos for syscall interface
#include <assert.h>
#include <errno.h>
#include <stdatomic.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/mman.h>
Expand Down Expand Up @@ -100,8 +99,8 @@ struct fifo *fifo_create(unsigned int size)
fifo->memfd = fd;
fifo->base_addr = base_addr;
fifo->base_addr_len = total_page_map_size;
fifo->in = 0;
fifo->out = 0;
atomic_init(&fifo->in, 0);
atomic_init(&fifo->out, 0);
fifo->mask = data_page_p2_size - 1;
return fifo;

Expand All @@ -126,55 +125,22 @@ void fifo_destroy(struct fifo *fifo)
close(fd);
}

char *fifo_in_ref(struct fifo *fifo, unsigned int size)
{
unsigned int free_size =
fifo_size(fifo) - (fifo->in - atomic_load_explicit(&fifo->out, memory_order_acquire));
if (size > free_size) {
/* zc_error("fifo not enough space"); */
return NULL;
}

return &fifo->data[fifo->in & fifo->mask];
}

void fifo_in_commit(struct fifo *fifo, unsigned int size)
{
assert(fifo_unused(fifo) >= size);
atomic_store_explicit(&fifo->in, fifo->in + size, memory_order_release);
}

unsigned int fifo_out_ref(struct fifo *fifo, char **buf)
{
unsigned int used_size = atomic_load_explicit(&fifo->in, memory_order_acquire) - fifo->out;
if (used_size == 0)
return 0;

*buf = &fifo->data[fifo->out & fifo->mask];
return used_size;
}

void fifo_out_commit(struct fifo *fifo, unsigned int size)
{
assert(size <= fifo_used(fifo));
atomic_store_explicit(&fifo->out, fifo->out + size, memory_order_release);
}

struct msg_head *fifo_reserve(struct fifo *fifo, unsigned int size)
{
unsigned old_in = atomic_load_explicit(&fifo->in, memory_order_relaxed);
unsigned int free_size =
fifo_size(fifo) - (fifo->in - atomic_load_explicit(&fifo->out, memory_order_acquire));
fifo_size(fifo) - (old_in - atomic_load_explicit(&fifo->out, memory_order_acquire));
unsigned total_size = size + msg_head_size();
if (total_size > free_size) {
zc_error("fifo not enough space");
return NULL;
}

struct msg_head *head = (struct msg_head *)&fifo->data[fifo->in & fifo->mask];
struct msg_head *head = (struct msg_head *)&fifo->data[old_in & fifo->mask];
head->total_size = total_size;
head->flags = MSG_HEAD_FLAG_RESERVED;
atomic_load_explicit(&head->flags, memory_order_relaxed);

atomic_store_explicit(&fifo->in, fifo->in + total_size, memory_order_release);
atomic_store_explicit(&fifo->in, old_in + total_size, memory_order_release);
return head;
}

Expand All @@ -196,16 +162,18 @@ void fifo_discard(struct fifo *fifo, struct msg_head *head)

struct msg_head *fifo_peek(struct fifo *fifo)
{
unsigned int used_size = atomic_load_explicit(&fifo->in, memory_order_acquire) - fifo->out;
unsigned out = atomic_load_explicit(&fifo->out, memory_order_relaxed);
unsigned int used_size = atomic_load_explicit(&fifo->in, memory_order_acquire) - out;
if (used_size == 0)
return NULL;

struct msg_head *head = (struct msg_head *)&fifo->data[fifo->out & fifo->mask];
struct msg_head *head = (struct msg_head *)&fifo->data[out & fifo->mask];

return head;
}

void fifo_out(struct fifo *fifo, struct msg_head *head)
{
atomic_store_explicit(&fifo->out, fifo->out + head->total_size, memory_order_release);
unsigned out = atomic_load_explicit(&fifo->out, memory_order_relaxed);
atomic_store_explicit(&fifo->out, out + head->total_size, memory_order_release);
}
14 changes: 5 additions & 9 deletions src/fifo.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#ifndef __FIFO_H
#define __FIFO_H

#include <stdatomic.h>
#include <stdio.h>

/* todo: optimize page size macro */
Expand All @@ -23,8 +24,8 @@ struct fifo {
unsigned char *base_addr;
unsigned base_addr_len;

unsigned in;
unsigned out;
atomic_uint in;
atomic_uint out;
unsigned mask;
int memfd;
_Alignas(PAGE_SIZE) char data[];
Expand All @@ -33,12 +34,6 @@ struct fifo {
struct fifo *fifo_create(unsigned int size);
void fifo_destroy(struct fifo *fifo);

char *fifo_in_ref(struct fifo *fifo, unsigned int size);
void fifo_in_commit(struct fifo *fifo, unsigned int size);

unsigned int fifo_out_ref(struct fifo *fifo, char **buf);
void fifo_out_commit(struct fifo *fifo, unsigned int size);

/**
* fifo_in_reserve -
*
Expand All @@ -58,7 +53,8 @@ static inline size_t fifo_size(struct fifo *fifo)

static inline unsigned int fifo_used(struct fifo *fifo)
{
return fifo->in - fifo->out;
return atomic_load_explicit(&fifo->in, memory_order_relaxed) -
atomic_load_explicit(&fifo->out, memory_order_relaxed);
}

static inline unsigned int fifo_unused(struct fifo *fifo)
Expand Down
3 changes: 2 additions & 1 deletion src/misc.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

#include <pthread.h>
#include <stdalign.h>
#include <stdatomic.h>
#include <stddef.h>

#define container_of(ptr, type, member) ((type *)((char *)(ptr) - offsetof(type, member)))
Expand Down Expand Up @@ -33,7 +34,7 @@ enum msg_head_flag {

struct msg_head {
unsigned total_size;
unsigned flags;
atomic_uint flags;

char data[];
};
Expand Down
9 changes: 6 additions & 3 deletions src/thread.c
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,8 @@ void zlog_thread_del(zlog_thread_t * a_thread)
zc_debug("fullcnt %d\n", a_thread->producer.full_cnt);
}
zc_debug("zlog_thread_del[%lx], producer en %d, cnt %d", a_thread->event->tid,
a_thread->producer.en, a_thread->producer.refcnt);
a_thread->producer.en,
atomic_load_explicit(&a_thread->producer.refcnt, memory_order_relaxed));
if (a_thread->mdc)
zlog_mdc_del(a_thread->mdc);
if (a_thread->event)
Expand Down Expand Up @@ -142,7 +143,8 @@ zlog_thread_t *zlog_thread_new(int init_version, size_t buf_size_min, size_t buf
if (conf->log_consumer.en) {
a_thread->producer.en = true;
atomic_init(&a_thread->producer.refcnt, 1);
zc_debug("init %lx, refcnt %d", pthread_self(), a_thread->producer.refcnt);
zc_debug("init %lx, refcnt %d", pthread_self(),
atomic_load_explicit(&a_thread->producer.refcnt, memory_order_relaxed));
}

// zlog_thread_profile(a_thread, ZC_DEBUG);
Expand Down Expand Up @@ -226,5 +228,6 @@ void zlog_thread_rebuild_producer(zlog_thread_t * thread, bool en)
thread->producer.en = en;
thread->producer.full_cnt = 0;
atomic_init(&thread->producer.refcnt, 1);
zc_debug("init %lx, refcnt %d", pthread_self(), thread->producer.refcnt);
zc_debug("init %lx, refcnt %d", pthread_self(),
atomic_load_explicit(&thread->producer.refcnt, memory_order_relaxed));
}
3 changes: 2 additions & 1 deletion src/thread.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
#ifndef __zlog_thread_h
#define __zlog_thread_h

#include <stdatomic.h>
#include <stdbool.h>

#include "zc_defs.h"
Expand Down Expand Up @@ -44,7 +45,7 @@ typedef struct zlog_thread_s {
struct {
/* change per conf start */
bool en;
int refcnt;
atomic_int refcnt;
/* change per conf end */
unsigned int full_cnt;
} producer;
Expand Down