Skip to content

Instantly share code, notes, and snippets.

@bahamas10
Last active December 16, 2015 20:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bahamas10/5493551 to your computer and use it in GitHub Desktop.
Save bahamas10/5493551 to your computer and use it in GitHub Desktop.
libfasttime.so on smartos

libfasttime.so

Testing performance from http://blog.csdn.net/undead/article/details/6447339

Installation

Compile and install the shared object libfasttime.c

gcc -fPIC -shared -m64 -o libfasttime.so-64 libfasttime.c -ldl -lrt -lm
gcc -fPIC -shared -o libfasttime.so-32 libfasttime.c -ldl -lrt -lm
mv libfasttime.so-64 /opt/local/lib/amd64/libfasttime.so
mv libfasttime.so-32 /opt/local/lib/libfasttime.so

Testing

Compile test-time.c

gcc -o test-time test-time.c

Without libfasttime.so

#  unset LD_PRELOAD
#  dtrace -n 'syscall::gtime:entry /pid == $target/ { @[probefunc] = count(); }' -c ./test-time
dtrace: description 'syscall::gtime:entry ' matched 1 probe
dtrace: pid 26204 has exited

  gtime                                                       5000000
#  ptime ./test-time 

real        1.732187527
user        0.988937693
sys         0.733504670

With libfasttime.so

#  export LD_PRELOAD=libfasttime.so
#  dtrace -n 'syscall::gtime:entry /pid == $target/ { @[probefunc] = count(); }' -c ./test-time
dtrace: description 'syscall::gtime:entry ' matched 1 probe
dtrace: pid 32725 has exited

  gtime                                                          2334
#  ptime ./test-time

real        2.245796417
user        2.240161843
sys         0.004911160

Results

Significantly less gtime syscalls are fired with the library in place, but the program takes longer to execute...

/*
*
* Copyright 2004 Sun Microsystems, Inc.
* 4150 Network Circle, Santa Clara, CA 95054
* All Rights Reserved.
*
* This software is the proprietary information of Sun Microsystems, Inc.
* This code is provided by Sun "as is" and "with all faults." Sun
* makes no representations or warranties concerning the quality, safety
* or suitability of the code, either express or implied, including
* without limitation any implied warranties of merchantability, fitness
* for a particular purpose, or non-infringement. In no event will Sun
* be liable for any direct, indirect, punitive, special, incidental
* or consequential damages arising from the use of this code. By
* downloading or otherwise utilizing this codes, you agree that you
* have read, understood, and agreed to these terms.
*
*/
#include <sys/types.h>
#include <time.h>
#include <stdio.h>
#include <dlfcn.h>
/* to compile, use cc -G -Kpic -o libfasttime.so -xO3 -xarch=v8plus time.c */
/**
* added by Dave Eddy <dave@daveeddy.com>
* gcc -fPIC -shared -m64 -o 64/libfasttime.so libfasttime.c -ldl -lrt -lm
*/
/* time in nanoseconds to cache the time system call */
#define DELTA 1000000 /* 1 millisecond */
static time_t (*func) (time_t *);
time_t time(time_t *tloc)
{
static time_t global = 0;
static hrtime_t old = 0;
hrtime_t new = gethrtime();
if(new - old > DELTA ){
global = func(tloc);
old = new;
}
return global;
}
#pragma init (init_func)
void init_func()
{
func = (time_t (*) (time_t *)) dlsym (RTLD_NEXT, "time");
if (!func)
{
fprintf(stderr, "Error initializing library/n");
}
}
#include <time.h>
#include <errno.h>
#define ITERATIONS 1e6 * 5
int main() {
int i;
for (i = 0; i < ITERATIONS; i++) {
if (time(NULL) < 0) {
perror("time(2)");
return 1;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment