Like Tree1Likes
  • 1 Post By Nominal Animal

LD_PRELOAD reduces the speed of execution. why it is like that??

This is a discussion on LD_PRELOAD reduces the speed of execution. why it is like that?? within the C Programming forums, part of the General Programming Boards category; i am writing a memcpy wrapper. and i am doing a LD_PRELOAD of the library on which memcpy wrapper is ...

  1. #1
    Registered User
    Join Date
    Nov 2012
    Posts
    17

    LD_PRELOAD reduces the speed of execution. why it is like that?? help needed

    i am writing a memcpy wrapper. and i am doing a LD_PRELOAD of the library on which memcpy wrapper is written. Now if i run a application which calls strcpy it is not doing it fast. if i am runing the same application without LD_PRELOAD, it is running faster. but if i am running it with a preloaded library it is slow. even if it not defined in the library. Will LD_PRELOAD will decrease the speed of execution??
    Last edited by josymadamana; 11-14-2012 at 01:47 AM. Reason: not getting reply

  2. #2
    Registered User
    Join Date
    Oct 2011
    Posts
    890
    Quote Originally Posted by josymadamana View Post
    i am writing a memcpy wrapper. and i am doing a LD_PRELOAD of the library on which memcpy wrapper is written. Now if i run a application which calls strcpy it is not doing it fast. if i am runing the same application without LD_PRELOAD, it is running faster. but if i am running it with a preloaded library it is slow. even if it not defined in the library.
    If the compiler knows the string length, it may be calling memcpy() instead of strcpy(), since the former is more efficient when the string length is already known. You could verify it by for example adding
    Code:
    write(STDERR_FILENO, ".", 1);
    to your memcpy() wrapper, as that will try to output a . to standard error whenever memcpy() is called.

    Quote Originally Posted by josymadamana View Post
    Will LD_PRELOAD will decrease the speed of execution??
    Adding a new library to a program will increase the startup latency. It is very small, but measurable, at least on slow machines. This happens because loading the dynamic library file, and resolving its symbols, does take (a little bit) of CPU time.

    By itself, the LD_PRELOADed library should only slow down the functions it interposes. (In addition to the startup load and initial symbol lookup latency I mentioned above.) It should not affect the rate you can call a non-interposed function in a tight loop, for example.

    However, it is possible that the C library itself behaves differently when LD_PRELOAD is set.

    One trick you might wish to try is to unset the LD_PRELOAD environment variable in your library init/constructor function [EDIT: use environ instead of unsetenv()]:
    Code:
    extern char **environ;
    
    static void my_lib_init(void) __attribute__((constructor));
    
    static void my_lib_init(void)
    {
        /* Find "LD_PRELOAD=" in the environ array, and remove it
         * by replacing it with the final pointer in the array,
         * and setting the final pointer to NULL.
        */
    }
    I am unsure whether it works, because it depends on whether the constructor gets executed before the C library constructor does. If this gets executed first, then it should stop the C library from behaving differently due to LD_PRELOAD being set.

    If LD_PRELOAD being set causes the linker (dynamic library) to work in inefficient ways, then there is not much you can do about it.

    Edit 2:

    I wrote a trivial dynamic library that interposes only the open() syscall, then tested whether it affects other functions; a loop that copies the program name (argv[0]) from one buffer to another for one second wall clock time, and prints the number of iterations.

    Loading the library actually yielded more iterations per second, rather than less. I suspect that is related to memory (stack) alignment or something similar, and has nothing to do with the LD_PRELOAD mechanism.

    So, according to my quick observations, I cannot duplicate the LD_PRELOAD slowdown on unrelated functions.
    Last edited by Nominal Animal; 11-14-2012 at 05:47 AM.
    dwks likes this.

  3. #3
    Registered User
    Join Date
    Nov 2012
    Posts
    17
    thanks for ur detailed mail. environ is not doing the work. when i do a LD_PRELOAD of memcpy even in the constructor function my own memcpy is called. i tried to unset environ LD_PRELOAD by like this
    Code:
    for (i=0;environ[i];i++ )
    {
        if ( strstr(environ[i],"LD_PRELOAD=") )
            {
                    printf("hacking out LD_PRELOAD from environ[%d]\n %s",i,environ[i]);
                    strcpy(environ[i],"LD_PRELOAD=" );
                    printf("hacking out LD_PRELOAD from environ[%d]\n %s",i,environ[i]);
            }
    }
    but it was not working.

    Slowdown is happening with me. it is very less it comes in micro seconds.

  4. #4
    Registered User
    Join Date
    Oct 2011
    Posts
    890
    Quote Originally Posted by josymadamana View Post
    thanks for ur detailed mail. environ is not doing the work. when i do a LD_PRELOAD of memcpy even in the constructor function my own memcpy is called.
    Of course it is.

    I just meant that if the slowdown is due to non-optimal code paths chosen in your C library because LD_PRELOAD is set, removing it from the environment might help. (Besides, you're not removing it, just setting it to an empty value.)

    Quote Originally Posted by josymadamana View Post
    Slowdown is happening with me. it is very less it comes in micro seconds.
    It sounds like your library is doing something wrong, then.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. How to unset LD_PRELOAD in runtime for a particular process
    By josymadamana in forum C Programming
    Replies: 8
    Last Post: 11-13-2012, 03:39 AM
  2. LD_PRELOAD getting ignored .. can anyone tell me why ?
    By NetworkLearning in forum C Programming
    Replies: 2
    Last Post: 09-19-2012, 11:13 PM
  3. Execution speed slow initially, then speeds up
    By megafiddle in forum Windows Programming
    Replies: 22
    Last Post: 12-08-2011, 11:16 PM
  4. Execution speed test in c++
    By nick2price in forum C++ Programming
    Replies: 1
    Last Post: 03-12-2009, 05:23 PM
  5. Questions on Speed of Execution
    By wavering in forum C Programming
    Replies: 22
    Last Post: 01-20-2002, 02:04 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21