VMOD - Varnish Modules·续2
You can find an example of VCL references in vmod-debug:
priv_vcl->vclref = VRT_VCL_Prevent_Discard(ctx, "vmod-debug");
...
VRT_VCL_Allow_Discard(&ctx, &priv_vcl->vclref);
In this simplified version, you can see that you need at least a VCL-bound data structure like a PRIV_VCL or a VMOD object to keep track of the reference and later release it. You also have to provide a description, it will be printed to the user if they try to warm up a cooling VCL:
$ varnishadm vcl.list
available auto/cooling 0 vcl1
active auto/warm 0 vcl2
$ varnishadm vcl.state vcl1 warm
Command failed with error code 300
Failed <vcl.state vcl1 auto>
Message:
VCL vcl1 is waiting for:
- vmod-debug
In the case where properly releasing resources may take some time, you can opt for an asynchronous worker, either by spawning a thread and tracking it, or by using Varnish’s worker pools.
When to lock, and when not to lock#
Varnish is heavily multithreaded, so by default VMODs must implement their own locking to protect shared resources.
When a VCL is loaded or unloaded, the event and priv->free are run sequentially all in a single thread, and there is guaranteed to be no other activity related to this particular VCL, nor are there init/fini activity in any other VCL or VMOD at this time.
That means that the VMOD init, and any object init/fini functions are already serialized in sensible order, and won’t need any locking, unless they access VMOD specific global state, shared with other VCLs.
Traffic in other VCLs which also import this VMOD, will be happening while housekeeping is going on.
Statistics Counters#
Starting in Varnish 6.0, VMODs can define their own counters that appear in varnishstat.
If you’re using autotools, see the VARNISH_COUNTERS macro in varnish.m4 for documentation on getting your build set up.
Counters are defined in a .vsc file. The VARNISH_COUNTERS macro calls vsctool.py to turn a foo.vsc file into VSC_foo.c and VSC_foo.h files, just like vmodtool.py turns foo.vcc into vcc_foo_if.c and vcc_foo_if.h files. Similarly to the VCC files, the generated VSC files give you a structure and functions that you can use in your VMOD’s code to create and destroy the counters your defined. The vsctool.py tool also generates a VSC_foo.rst file that you can include in your documentation to describe the counters your VMOD has.
The .vsc file looks like this:
.. varnish_vsc_begin:: xkey
:oneliner: xkey Counters
:order: 70
Metrics from vmod_xkey
.. varnish_vsc:: g_keys
:type: gauge
:oneliner: Number of surrogate keys
Number of surrogate keys in use. Increases after a request that includes a new key in the xkey header. Decreases when a key is purged or when all cache objects associated with a key expire.
.. varnish_vsc_end:: xkey
Counters can have the following parameters:
type
The type of metric this is. Can be one of counter, gauge, or bitmap.
ctype
The type that this counter will have in the C code. This can only be uint64_t and does not need to be specified.
level
The verbosity level of this counter. varnishstat will only show counters with a higher verbosity level than the one currently configured. Can be one of info, diag, or debug.
oneliner
A short, one line description of the counter.
group
I don’t know what this does.
format
Can be one of integer, bytes, bitmap, or duration.
After these parameters, a counter can have a longer description, though this description has to be all on one line in the .vsc file.
You should call VSC_*_New() when your VMOD is loaded and VSC_*_Destroy() when it is unloaded. See the generated VSC_*.h file for the full details about the structure that contains your counters.
Temporary Files#
varnishd creates a directory named worker_tmpdir under the varnish working directory (see varnishd -n argument) for read/write access by the worker process.
From the perspective of VMODs, the relative path is always worker_tmpdir.
This directory is intended (though not limited) to provide a place for VMODs to create temporary files using mkstemp() and related libc functions. VMODs are responsible for cleaning up files which are no longer required, and they will ultimately be removed when the varnishd worker process restarts. There is no isolation between VMODs (as is the case anyway).
A simple example for how to use it:
#include <stdlib.h>
#include <unistd.h>
#include "vdef.h"
#include "vas.h"
static void
tmpfile_example(void) {
int fd;
char name[] = "worker_tmpdir/myvmod.XXXXXX";
fd = mkstemp(name);
if (fd < 0) {
// handle error
return;
}
// hide file
AZ(unlink(name));
// use fd
AZ(close(fd));
}
VMOD Version Information#
Panic messages (see Troubleshooting Varnish for an introduction) are an invaluable information source to developers when analyzing crashes. To avoid uncertainties about the exact code base a VMOD was built from, Panics contain, if possible, a maintainer-controlled version and a “version” from a version control system (VCS, usually git).
The maintainer-controlled version is taken from the $Version stanza with a fallback to PACKAGE_STRING from the Makefile, see The vmod.vcc file.
The VCS version is intended to be more specific than the maintainer-controlled version and is thus automatically generated unless deliberately overwritten: If vmodtool.py finds itself invoked from a git repository, it uses the version output by git rev-parse HEAD. To also have proper VCS version information in builds from a dist archive (as created with make dist when using autotools), vmodtool.py also writes a VCS version it gets from git to vmod_vcs_version.txt. This file is intended to be added to the distribution, such that when vmodtool.py is run from a distribution archive, it also has the VCS version available.
Supporting vmod_vcs_version.txt with autotools#
To support vmod_vcs_version.txt with autotools, VMOD authors are advised to implement the following:
- add a dependency from
vmod_vcs_version.txtto (one of) the files generated byvmodtool.py - add
vmod_vcs_version.txttoDISTCLEANFILESandEXTRA_DIST
vmod_vcs_version.txt should also be added to .gitignore
To illustrate, here is an example diff for a trivial vmod by the name blobsynth :
diff --git a/src/Makefile.am b/src/Makefile.am
index 63e4d55..d8cc659 100644
--- a/src/Makefile.am
+++ b/src/Makefile.am
@@ -10,6 +10,10 @@ nodist_libvmod_blobsynth_la_SOURCES = \
vcc_blobsynth_if.c \
vcc_blobsynth_if.h
+DISTCLEANFILES = vmod_vcs_version.txt
+
+vmod_vcs_version.txt: $(nodist_libvmod_blobsynth_la_SOURCES)
+
dist_man_MANS = vmod_blobsynth.3
@BUILD_VMOD_BLOBSYNTH@
@@ -25,4 +29,5 @@ TESTS = @VMOD_TESTS@
EXTRA_DIST = \
vmod_blobsynth.vcc \
+ vmod_vcs_version.txt \
$(VMOD_TESTS)
Retrieving version information using the CLI#
The debug.vmod CLI command outputs the version information, for example (edited for readability, the original output is on a single line):
$ varnishadm debug.vmod
1 dynamic (path=".../vmods/libvmod_dynamic.so",
version="libvmod-dynamic trunk",
vcs="1e4179430404aaf170530af7514fbecb1f38f8af")
Reading the VCS version from the binary shared object#
As a useful by-product, the version information can also be extracted from the vmod .so file on many platforms. On Linux using readelf -p.vmod_vcs <file>:
$ readelf -p.vmod_vcs .../vmods/libvmod_dynamic.so
String dump of section '.vmod_vcs':
[ 0] 1e4179430404aaf170530af7514fbecb1f38f8af
On MacOS:
$ otool -s __TEXT vmod_vcs .../vmods/libvmod_debug.so |
awk '/^[0-9a-f]/ {for(i=2;i<=NF;i++) printf "%s",$i}' |
xxd -r -p | strings
18e27f081788b5e5d44c480f6d9749c07d53ddb9
