Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Vsync proposal #4133

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
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
1 change: 1 addition & 0 deletions bin/varnishd/Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ varnishd_SOURCES = \
cache/cache_vrt_var.c \
cache/cache_vrt_vcl.c \
cache/cache_vrt_vmod.c \
cache/cache_vsync.c \
cache/cache_wrk.c \
cache/cache_ws_common.c \
common/common_vsc.c \
Expand Down
1 change: 1 addition & 0 deletions bin/varnishd/cache/cache_main.c
Original file line number Diff line number Diff line change
Expand Up @@ -402,6 +402,7 @@ child_main(int sigmagic, size_t altstksz)
VSM_Init(); /* First, LCK needs it. */

LCK_Init(); /* Second, locking */
SYNC_Init();

Lck_New(&vxid_lock, lck_vxid);

Expand Down
3 changes: 3 additions & 0 deletions bin/varnishd/cache/cache_varnishd.h
Original file line number Diff line number Diff line change
Expand Up @@ -604,3 +604,6 @@ void SMP_Ready(void);
if (DO_DEBUG(debug_bit)) \
WRK_Log(SLT_Debug, __VA_ARGS__); \
} while (0)

/* cache_vsync.c */
void SYNC_Init(void);
122 changes: 122 additions & 0 deletions bin/varnishd/cache/cache_vsync.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
/*-
* Copyright (c) 2024 Varnish Software AS
* All rights reserved.
*
* Author: Stephane Cance <[email protected]>
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL AUTHOR OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/

#include "config.h"

#include <stdlib.h>
#include <errno.h>

#include "cache_varnishd.h"

#include "vtim.h"
#include "vsync.h"

#include "VSC_lck.h"
#include "VSC_cond.h"

static void
mtx_sync_evt(const char *func, const char *file,
int line, enum vsync_mtx_event evt, struct VSC_lck *vsc)
{
AN(vsc);

(void)func;
(void)line;
(void)file;


switch (evt) {
case VSYNC_MTX_INIT:
__sync_fetch_and_add(&vsc->creat, 1);
break;
case VSYNC_MTX_FINI:
__sync_fetch_and_add(&vsc->destroy, 1);
break;
case VSYNC_MTX_LOCK:
__sync_fetch_and_add(&vsc->locks, 1);
break;
case VSYNC_MTX_UNLOCK:
break;
}
}

static void
cond_sync_evt(const char *func, const char *file, int line,
enum vsync_cond_event evt, struct VSC_cond *vsc_cond,
struct VSC_lck *vsc_mtx, double *start_atp)
{
vtim_mono now;

AN(vsc_cond);

(void)func;
(void)line;
(void)file;
(void)vsc_mtx;

switch (evt) {
case VSYNC_COND_INIT:
__sync_fetch_and_add(&vsc_cond->init, 1);
AZ(start_atp);
break;
case VSYNC_COND_FINI:
__sync_fetch_and_add(&vsc_cond->fini, 1);
AZ(start_atp);
break;
case VSYNC_COND_SIGNAL:
__sync_fetch_and_add(&vsc_cond->signal, 1);
AZ(start_atp);
break;
case VSYNC_COND_BROADCAST:
__sync_fetch_and_add(&vsc_cond->broadcast, 1);
AZ(start_atp);
break;
case VSYNC_COND_WAIT_START:
__sync_fetch_and_add(&vsc_cond->waiting, 1);
AN(start_atp);
*start_atp = VTIM_mono();
break;
case VSYNC_COND_WAIT_END:
AN(start_atp);
now = VTIM_mono();
assert(now > *start_atp);

__sync_fetch_and_sub(&vsc_cond->waiting, 1);
__sync_fetch_and_add(&vsc_cond->waits, 1);
__sync_fetch_and_add(&vsc_cond->wait_duration_ns,
(uint64_t)((now - *start_atp) * 1e9));
break;
}
}

void
SYNC_Init(void)
{
VSYNC_mtx_event_func = mtx_sync_evt;
VSYNC_cond_event_func = cond_sync_evt;
}
1 change: 1 addition & 0 deletions configure.ac
Original file line number Diff line number Diff line change
Expand Up @@ -229,6 +229,7 @@ save_LIBS="${LIBS}"
LIBS="${PTHREAD_LIBS}"
AC_CHECK_FUNCS([pthread_mutex_isowned_np])
AC_CHECK_FUNCS([pthread_getattr_np])
AC_CHECK_FUNCS([pthread_condattr_setclock])
LIBS="${save_LIBS}"

AC_CHECK_DECL([__SUNPRO_C], [SUNCC="yes"], [SUNCC="no"])
Expand Down
1 change: 1 addition & 0 deletions include/Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ nobase_pkginclude_HEADERS += \
vsa.h \
vsb.h \
vsha256.h \
vsync.h \
vtcp.h \
vte.h \
vtim.h \
Expand Down
2 changes: 2 additions & 0 deletions include/vdef.h
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@
#endif
#define VDEF_H_INCLUDED

#include <math.h>

/* Safe printf into a fixed-size buffer */
#define bprintf(buf, fmt, ...) \
do { \
Expand Down
Loading