Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Two instances of jemalloc in same program - compile warnings #2554

Open
danokeeffe opened this issue Oct 10, 2023 · 1 comment
Open

Two instances of jemalloc in same program - compile warnings #2554

danokeeffe opened this issue Oct 10, 2023 · 1 comment

Comments

@danokeeffe
Copy link

Hi,
I'm trying to compile a simple test program that uses two different copies of jemalloc in the same program statically. I think it's working, but I have a few compile warnings that I would like to resolve if possible (or at least confirm they are a non-issue).

My setup is the following. I have two copies of the jemalloc source, one in a subdirectory called jemalloc and the other in a subdirectory called jemalloc-other. The former I configure and compile as normal (i.e. no special options other than --enable-stats. The second I configure using:

./configure --enable-stats --with-jemalloc-prefix=other --with-private-namespace=other_ --with-install-suffix=other

I have the following test program (main.c):

  #include "jemalloc/jemalloc.h"
  #include <stdio.h>

  int main() {

      size_t n;
      size_t sz = sizeof(n);
      uint64_t cnt;
      size_t cnt_sz = sizeof(cnt);
      for (int i = 0; i < 20; i++) {
          void* p1 = malloc(4096);

          mallctl("thread.allocated", &cnt, &cnt_sz, NULL, 0);
          printf("Thread allocated 1: %zu\n", cnt);
      }

      for (int i = 0; i < 20; i++) {
          void* p2 = (void*)transientmalloc(512);

          transientmallctl("thread.allocated", &cnt, &cnt_sz, NULL, 0);
          printf("Thread allocated 2: %zu\n", cnt);
      }

      printf("Working properly\n");
  }

I then compile the above using gcc -o main -I./jemalloc/include main.c ./jemalloc/lib/libjemalloc.a ./jemalloc-other/lib/libjemallocother.a -lpthread -ldl

This gives the following compiler warnings:

main.c: In function ‘main’:
main.c:19:27: warning: implicit declaration of function ‘othermalloc’ [-Wimplicit-function-declaration]
   19 |         void* p2 = (void*)othermalloc(512);
      |                           ^~~~~~~~~~~~~~~
main.c:19:20: warning: cast to pointer from integer of different size [-Wint-to-pointer-cast]
   19 |         void* p2 = (void*)othermalloc(512);
      |                    ^
main.c:21:9: warning: implicit declaration of function ‘othermallctl’ [-Wimplicit-function-declaration]
   21 |         othermallctl("thread.allocated", &cnt, &cnt_sz, NULL, 0);

However, when I run the program it seems to work OK (i.e. the symbols are resolved and the allocations seem to come from different allocator instances):

> ./main
Thread allocated 1: 4096
Thread allocated 1: 9216
Thread allocated 1: 13312
Thread allocated 1: 17408
Thread allocated 1: 21504
Thread allocated 1: 25600
Thread allocated 1: 29696
Thread allocated 1: 33792
Thread allocated 1: 37888
Thread allocated 1: 41984
Thread allocated 1: 46080
Thread allocated 1: 50176
Thread allocated 1: 54272
Thread allocated 1: 58368
Thread allocated 1: 62464
Thread allocated 1: 66560
Thread allocated 1: 70656
Thread allocated 1: 74752
Thread allocated 1: 78848
Thread allocated 1: 82944
Thread allocated 2: 512
Thread allocated 2: 1024
Thread allocated 2: 1536
Thread allocated 2: 2048
Thread allocated 2: 2560
Thread allocated 2: 3072
Thread allocated 2: 3584
Thread allocated 2: 4096
Thread allocated 2: 4608
Thread allocated 2: 5120
Thread allocated 2: 5632
Thread allocated 2: 6144
Thread allocated 2: 6656
Thread allocated 2: 7168
Thread allocated 2: 7680
Thread allocated 2: 8192
Thread allocated 2: 8704
Thread allocated 2: 9216
Thread allocated 2: 9728
Thread allocated 2: 10240
Working properly

I'm a little confused as to how to get rid of the above warnings. I tried including different header files from jemalloc-other/include (e.g. jemalloc-other/include/jemallocother.h), but it didn't seem to make a difference. Am I missing something?

@ericm1024
Copy link
Contributor

I have some experience loading two different versions of jemalloc, but our usage was a bit different

  • We were loading them as shared libraries instead of static linking
  • We were using two different versions of jemalloc instead of two builds of the same version (each built with a different with-jemalloc-prefix)
  • We never tried using them both in the same .c/.cpp file.

That said, I can share what worked for us.

In out experience, the jemalloc.h header file was not renamed with the various namespacing configurations, so we had to include both jemalloc.h files. It looks like you're passing -I./jemalloc/include on the compiler command line. I'd imagine you need to also pass -I./jemalloc-other/include to make the headers from the other install visible.

The problem with that is that now if you just #include "jemalloc/jemalloc.h", the name is ambiguous, so the compiler will just include the first one it finds. To get around that, we put a symlink in the different jemalloc directories, so that we could do things like

#include "jemallocA/jemalloc.h"
#include "jemallocB/jemalloc.h"

the directory structure was something like this.

jemallocA/
    include/
        jemalloc/
            jemalloc.h
    include_public/
        jemallocA -> ../include/jemalloc/
jemallocB/
    include/
        jemalloc/
            jemalloc.h
    include_public/
        jemallocB -> ../include/jemalloc/

We added the include_public directories and the symlinks, the rest is just the normal jemalloc repo.

Then we pass -I./jemallocA/include_public and -I./jemallocB/include_public to the compiler, and in the code we can include both header files as "jemallocA/jemalloc.h" and "jemallocB/jemalloc.h".

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants
@danokeeffe @ericm1024 and others