Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 549201
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T11:09:34+00:00 2026-05-13T11:09:34+00:00

I have a program written in Ruby and C. The C portion is a

  • 0

I have a program written in Ruby and C. The C portion is a shared library, which is an extension for the Ruby program. I want to profile the C shared library I wrote, using gprof. I compile the shared library like this:

gcc -I. -I/usr/lib/ruby/1.8/i486-linux -I/usr/lib/ruby/1.8/i486-linux -I. -D_FILE_OFFSET_BITS=64  -fPIC -fno-strict-aliasing -g -march=i686 -O2 -ggdb -pg -fPIC -c extension.c
gcc -shared -o extension.so extension.o -L. -L/usr/lib -L. -Wl,-Bsymbolic-functions -rdynamic -Wl,-export-dynamic  -lruby1.8  -lpthread -lrt -ldl -lcrypt -lm -lc

Then I execute the ruby program, which loads this shared library, and I expect a gmon.out file in the current directory, but for some reason the file gmon.out does not get created. How do I do this?

I googled for this but couldn’t find a satisfactory answer (which worked).

P.S. – As a workaround I can have a modified version of the extension which is a pure C program (instead of being created as a shared library) that I can use to profile, but it becomes tedious to maintain two versions of the same C extension (large number of differences between the two).

I tried writing a C program which uses the shared library directly too. I immediately get a page fault in one of the ruby library functions which get called during the initialization of the shared library. I think it’s really expecting to be loaded from a ruby program, which may internally be doing some magic.

(gdb) bt
#0  0x0091556e in st_lookup () from /usr/lib/libruby1.8.so.1.8
#1  0x008e87c2 in rb_intern () from /usr/lib/libruby1.8.so.1.8
#2  0x008984a5 in rb_define_module () from /usr/lib/libruby1.8.so.1.8
#3  0x08048dd0 in Init_SimilarStr () at extension.c:542
#4  0x0804933e in main (argc=2, argv=0xbffff454) at extension.c:564

Update: Never mind. I used #ifdef to compile out Ruby portions of the extension and get a profile. Closing.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-13T11:09:34+00:00Added an answer on May 13, 2026 at 11:09 am

    I found oprofile to be a lot better option than gprof for profiling, in this situation. The reports from oprofile are much more comprehensive. I compiled out the ruby portions which were causing the seg-faults (not all of them were), from the C extension using #ifndef PROFILE, and replaced them with non-ruby code. I wrote a main() routine within the extension itself, to call the functions in the extension. Then I set up a makefile to compile the extension as a C program with PROFILE defined. Then I installed oprofile on Ubuntu. Wrote this script.

    #!/bin/bash
    sudo opcontrol --reset
    sudo opcontrol --start
    ./a.out Rome Damascus NewYork Delhi Bangalore
    sudo opcontrol --shutdown
    opreport -lt1
    

    Compiled my program, and executed the above script, which gives output like this from the “opreport” command:

    ...
    ...
    Killing daemon.
    warning: /no-vmlinux could not be found.
    warning: [vdso] (tgid:10675 range:0x920000-0x921000) could not be found.
    warning: [vdso] (tgid:1270 range:0xba1000-0xba2000) could not be found.
    warning: [vdso] (tgid:1675 range:0x973000-0x974000) could not be found.
    warning: [vdso] (tgid:1711 range:0x264000-0x265000) could not be found.
    warning: [vdso] (tgid:1737 range:0x990000-0x991000) could not be found.
    warning: [vdso] (tgid:2477 range:0xa53000-0xa54000) could not be found.
    warning: [vdso] (tgid:5658 range:0x7ae000-0x7af000) could not be found.
    CPU: Core Solo / Duo, speed 1000 MHz (estimated)
    Counted CPU_CLK_UNHALTED events (Unhalted clock cycles) with a unit mask of 0x00 (Unhalted core cycles) count 100000
    samples  %        app name                 symbol name
    12731    32.8949  a.out                    levenshtein
    11958    30.8976  a.out                    corpora_pass2
    5231     13.5161  no-vmlinux               /no-vmlinux
    4021     10.3896  a.out                    corpora_pass1
    1733      4.4778  libc-2.10.1.so           /lib/tls/i686/cmov/libc-2.10.1.so
    542       1.4004  ld-2.10.1.so             /lib/ld-2.10.1.so
    398       1.0284  a.out                    method_top_matches
    

    There it is: the top consumer is the function levenshtein(). I followed this by another command to generate disassembled output annotated with source code and execution count/time of each line. This looks like this (counts/times are on the left of each executed line):

    > opannotate --source --assembly ./a.out > report.as.handcoded.1
    > cat report.as.handcoded.1
    
    ...
    ...
    ...
               :         __asm__ (
     2  0.0069 : 804918a:       mov    -0x50(%ebp),%ecx
     4  0.0137 : 804918d:       mov    -0x54(%ebp),%ebx
               : 8049190:       mov    -0x4c(%ebp),%eax
    12  0.0412 : 8049193:       cmp    %eax,%ecx
    10  0.0344 : 8049195:       cmovbe %ecx,%eax
     8  0.0275 : 8049198:       cmp    %eax,%ebx
    11  0.0378 : 804919a:       cmovbe %ebx,%eax
    16  0.0550 : 804919d:       mov    %eax,-0x4c(%ebp)
               :                   "cmp     %0, %2\n\t"
               :                   "cmovbe  %2, %0\n\t"
               :                  : "+r"(a) :
               :                    "%r"(b), "r"(c)
               :                  );
               :          return a;
     ...
     ...
     ...
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 407k
  • Answers 407k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer "How I can use NHibernate Vaidators to implement sophisticated custom… May 15, 2026 at 6:32 am
  • Editorial Team
    Editorial Team added an answer use the -batch option and the program will run without… May 15, 2026 at 6:32 am
  • Editorial Team
    Editorial Team added an answer Kind of anti-climactic, but it turns out there was a… May 15, 2026 at 6:32 am

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.