Custom compilation flag for an nginx module

Hi all,

Is it possible for an nginx module to define custom compilation switches
that add external libs / preprocessor macros ? Is there some example of
a
module that does it ?
Specifically, what I’m trying to do is measure time accurately in my
module
for benchmarking purposes. Since I use Linux, I was planning to use
clock_gettime(CLOCK_MONOTONIC) for that. This function is
Linux-specific,
and so I would like to make it optional at compilation time:

  • if nginx is compile with --with-clock-gettime (or something like
    that) I
    will use clock_gettime and link against the required library (librt)
  • otherwise, I will fall back to using gettimeofday or drop the feature
    altogether

Thanks,

Eran

Posted at Nginx Forum:

Hello!

On Wed, Dec 03, 2014 at 10:47:01AM -0500, erankor2 wrote:

Hi all,

Is it possible for an nginx module to define custom compilation switches
that add external libs / preprocessor macros ? Is there some example of a
module that does it ?

No.

Specifically, what I’m trying to do is measure time accurately in my module
for benchmarking purposes. Since I use Linux, I was planning to use
clock_gettime(CLOCK_MONOTONIC) for that. This function is Linux-specific,
and so I would like to make it optional at compilation time:

  • if nginx is compile with --with-clock-gettime (or something like that) I
    will use clock_gettime and link against the required library (librt)
  • otherwise, I will fall back to using gettimeofday or drop the feature
    altogether

It may be better to just detect if the function is available on
a system in your module config script, much like it’s done for
many other functions in auto/unix script.

Note well that clock_gettime() isn’t Linux-specific, it’s in POSIX:

http://pubs.opengroup.org/onlinepubs/9699919799/functions/clock_gettime.html

Need for librt is Linux-specific though. It can be easily tested
as well, and there are couple of feature tests in auto/unix which
do such tests for other functions.


Maxim D.
http://nginx.org/

Thank you Maxim, your suggestion will definitely work for me.

Are you familiar with any simple “non-core” module that does it ? I will
need to test for the existence of this function or the need to
explicitly
add librt, and update CORE_LIBS accordingly + define some preprocessor
macro
that I can later #ifdef on in my code.

I googled this up:

but don’t really understand what is going on there :slight_smile:

Thanks,

Eran

Posted at Nginx Forum:

Hello!

On Wed, Dec 03, 2014 at 11:28:28AM -0500, erankor2 wrote:

Thank you Maxim, your suggestion will definitely work for me.

Are you familiar with any simple “non-core” module that does it ? I will
need to test for the existence of this function or the need to explicitly
add librt, and update CORE_LIBS accordingly + define some preprocessor macro
that I can later #ifdef on in my code.

The code as in feature tests in auto/unix is more or less the same
you’ll need in your module config file, e.g.:

ngx_feature=“sched_yield()”
ngx_feature_name=“NGX_HAVE_SCHED_YIELD”
ngx_feature_run=no
ngx_feature_incs=“#include <sched.h>”
ngx_feature_path=
ngx_feature_libs=
ngx_feature_test=“sched_yield()”
. auto/feature

if [ $ngx_found != yes ]; then

ngx_feature="sched_yield() in librt"
ngx_feature_libs="-lrt"
. auto/feature

if [ $ngx_found = yes ]; then
    CORE_LIBS="$CORE_LIBS -lrt"
fi

fi

Replacing sched_yield() here with clock_gettime() as appropriate
should do the trick.

Note well that the whole configure system is in shell, so it’s
easy to read and understand.


Maxim D.
http://nginx.org/

Thanks Maxim, that’s very helpful, it works great

Eran

Posted at Nginx Forum: