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
14 changes: 7 additions & 7 deletions example/plugins/c-api/cache_scan/cache_scan.cc
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@

#define PLUGIN_NAME "cache_scan"

static const TSDbgCtl *const dbg_ctl = TSDbgCtlCreate(PLUGIN_NAME);
static DbgCtl dbg_ctl{PLUGIN_NAME};

static TSCont global_contp;

Expand Down Expand Up @@ -285,14 +285,14 @@ handle_io(TSCont contp, TSEvent event, void * /* edata ATS_UNUSED */)
return 0;
} break;
case TS_EVENT_VCONN_WRITE_READY: {
TSDbg(dbg_ctl, "ndone: %" PRId64 " total_bytes: % " PRId64, TSVIONDoneGet(cstate->write_vio), cstate->total_bytes);
Dbg(dbg_ctl, "ndone: %" PRId64 " total_bytes: % " PRId64, TSVIONDoneGet(cstate->write_vio), cstate->total_bytes);
cstate->write_pending = false;
// the cache scan handler should call vio reenable when there is
// available data
return 0;
} break;
case TS_EVENT_VCONN_WRITE_COMPLETE: {
TSDbg(dbg_ctl, "write complete");
Dbg(dbg_ctl, "write complete");
cstate->done = 1;
cleanup(contp);
} break;
Expand All @@ -311,7 +311,7 @@ handle_io(TSCont contp, TSEvent event, void * /* edata ATS_UNUSED */)
static int
cache_intercept(TSCont contp, TSEvent event, void *edata)
{
TSDbg(dbg_ctl, "cache_intercept event: %d", event);
Dbg(dbg_ctl, "cache_intercept event: %d", event);

switch (event) {
case TS_EVENT_NET_ACCEPT:
Expand Down Expand Up @@ -439,7 +439,7 @@ setup_request(TSCont contp, TSHttpTxn txnp)
end = start + del_url_len;

cstate->key_to_delete = TSCacheKeyCreate();
TSDbg(dbg_ctl, "deleting url: %s", start);
Dbg(dbg_ctl, "deleting url: %s", start);

TSMBuffer urlBuf = TSMBufferCreate();
TSMLoc urlLoc;
Expand All @@ -463,9 +463,9 @@ setup_request(TSCont contp, TSHttpTxn txnp)
}

TSContDataSet(scan_contp, cstate);
TSDbg(dbg_ctl, "setup cache intercept");
Dbg(dbg_ctl, "setup cache intercept");
} else {
TSDbg(dbg_ctl, "not a cache iter request");
Dbg(dbg_ctl, "not a cache iter request");
}

Ldone:
Expand Down
120 changes: 120 additions & 0 deletions include/ts/DbgCtl.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
/** @file

DbgCtl class header file.

@section license License

Licensed to the Apache Software Foundation (ASF) under one
or more contributor license agreements. See the NOTICE file
distributed with this work for additional information
regarding copyright ownership. The ASF licenses this file
to you under the Apache License, Version 2.0 (the
"License"); you may not use this file except in compliance
with the License. You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

#pragma once

#include <atomic>
#include <utility>

#include <ts/apidefs.h> // For TS_PRINTFLIKE

class DiagsConfigState;

class DbgCtl
{
public:
// Tag is a debug tag. Debug output associated with this control will be output when debug output
// is enabled globally, and the tag matches the configured debug tag regular expression.
//
DbgCtl(char const *tag) : _ptr{_new_reference(tag)} {}

~DbgCtl() { _rm_reference(); }

bool
tag_on() const
{
return _ptr->second;
}

char const *
tag() const
{
return _ptr->first;
}

bool
on() const
{
auto m{_config_mode.load(std::memory_order_relaxed)};
if (!m) {
return false;
}
if (!_ptr->second) {
Copy link
Contributor Author

@ywkaras ywkaras Aug 24, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Technically second should be std::atomic (or at least volatile), since it can be written from another thread. But doing that risks causing the generation of less performant code. And, it seems unlikely that any register buffering of the value would cause actual problems. std::atomic instances are not moveable, so they can' t be used in STL containers.

return false;
}
if (m & 1) {
return true;
}
return (2 == m) && (_override_global_on());
}

static bool
global_on()
{
auto m{_config_mode.load(std::memory_order_relaxed)};
if (!m) {
return false;
}
if (m & 1) {
return true;
}
return (2 == m) && (_override_global_on());
}

// Call this when the compiled regex to enable tags may have changed.
//
static void update();

// For use in DbgPrint() only.
//
static void print(char const *tag, char const *file, char const *function, int line, char const *fmt_str, ...)
TS_PRINTFLIKE(5, 6);

private:
using _TagData = std::pair<char const *const, bool>;

_TagData const *const _ptr;

static const _TagData *_new_reference(char const *tag);

static void _rm_reference();

class _RegistryAccessor;

static std::atomic<int> _config_mode;

static bool _override_global_on();

friend class DiagsConfigState;
};

// Always generates output when called.
//
#define DbgPrint(CTL, ...) (DbgCtl::print((CTL).tag(), __FILE__, __FUNCTION__, __LINE__, __VA_ARGS__))

#define Dbg(CTL, ...) \
do { \
if ((CTL).on()) { \
DbgPrint((CTL), __VA_ARGS__); \
} \
} while (false)
1 change: 1 addition & 0 deletions include/ts/Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ library_includedir=$(includedir)/ts

library_include_HEADERS = \
apidefs.h \
DbgCtl.h \
ts.h \
remap.h \
experimental.h \
Expand Down
5 changes: 0 additions & 5 deletions include/ts/apidefs.h.in
Original file line number Diff line number Diff line change
Expand Up @@ -1114,11 +1114,6 @@ char const TS_VERSION_STRING[] = "@TS_VERSION_STRING@";
TSIOBufferWaterMark buffer_water_mark;
};

struct TSDbgCtl {
char volatile on; // Flag
char const *tag;
};

/* --------------------------------------------------------------------------
URL schemes */
extern const char *TS_URL_SCHEME_FILE;
Expand Down
49 changes: 1 addition & 48 deletions include/ts/ts.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@

#include <ts/apidefs.h>
#include <ts/parentselectdefs.h>
#include <ts/DbgCtl.h>

class DiagsConfigState;

Expand Down Expand Up @@ -2206,54 +2207,6 @@ namespace c
if (diags_on_for_plugins) \
TSDebug

class TSDbgCtlDetail
{
friend bool TSIsDbgCtlSet(TSDbgCtl const *ctlp);

static bool debug_on;

friend class ::DiagsConfigState;
};

inline bool
TSIsDbgCtlSet(TSDbgCtl const *ctlp)
{
return (TSDbgCtlDetail::debug_on && ctlp->on);
}

/**
Output a debug line if the debug output control is turned on.

@param ctlp pointer to TSDbgCtl, returned by TSDbgCtlCreate().
@param ... Format string and (optional) arguments.
*/
#define TSDbg(ctlp, ...) \
do { \
if (TSIsDbgCtlSet(ctlp)) { \
_TSDbg(ctlp->tag, __VA_ARGS__); \
} \
} while (0)

/**
Return a pointer for use with TSDbg(). For good performance,
this should be called in TSPluginInit() or TSRemapInit(), or in
static initialization in C++ (with the result stored in a
static pointer).

@param tag Debug tag for the control.
*/
TSDbgCtl const *TSDbgCtlCreate(char const *tag);

/**
Destroy (dereference) a debug control object previously created
with TSDbgCtlCreate().

@param dbg_ctl pointer to debug control object.
*/
void TSDbgCtlDestroy(TSDbgCtl const *dbg_ctl);

void _TSDbg(const char *tag, const char *format_str, ...) TS_PRINTFLIKE(2, 3); /* Not for direct use. */

/* --------------------------------------------------------------------------
logging api */

Expand Down
62 changes: 0 additions & 62 deletions include/tscore/DbgCtl.h

This file was deleted.

Loading