2

The question is about the performance of protected type and entry using the GNAT compiler on Linux. Mutex is only used as an example (Ada does not need it).

I compared the performance of Ada Mutex implementation from Rosetta Code (https://rosettacode.org/wiki/Mutex#Ada) to a very simple C/pthread implementation called from Ada using import and C interface.

It turned out the Ada protected type + entry was 36.8x times slower. I know that GNAT may go through its run time library and eventually ends up calling OS primitives s.a. pthread. I excepted some overhead but not that much.

The question is: Why?

Just in case - this is my simple pthread implementation:

     -- cmutex.ads

     package CMutex is
        procedure  Lock with
            Import => True,
            Convention => C,
            External_Name => "mutex_lock";

        procedure Unlock with
            Import => True,
            Convention => C,
            External_Name => "mutex_unlock";
    end CMutex;
    // C code
    #include <pthread.h>

    static  pthread_mutex_t mtx     = PTHREAD_MUTEX_INITIALIZER;

    void mutex_lock()
    {
        pthread_mutex_lock(&mtx);
    }

    void mutex_unlock()
    {
        pthread_mutex_unlock(&mtx);
    }

==== EDIT ===

Adding minimal reproachable code. It is a single thread (just for test), the dummy variable is to prevent the optimizer from optimizing the entire loop out.

The test code for the pthread (cmutex) above is:

with Text_IO; use Text_IO;
with CMutex;
procedure test is
   dummy : Integer := 0;
begin
    for i in 1 .. 100_000_000 loop
        CMutex.Lock;
        dummy := dummy + 1;
        CMutex.Unlock;
    end loop;
    Text_IO.Put_Line(Integer'image(dummy));
end test;

And the test code for the protected type + entry example is:

with Text_IO; use Text_IO;
with Ada_Mutex;
procedure test1 is
   dummy : Integer := 0;
   mtx   : Ada_Mutex.Mutex;
begin
    for i in 1 .. 100_000_000 loop
        mtx.Seize;
        dummy := dummy + 1;
        mtx.Release;
    end loop;
    Text_IO.Put_Line(Integer'image(dummy));
end test1;

Where Ada_Mutex is a package containing the example form Rosetta code:

package Ada_Mutex is
protected type Mutex is
   entry Seize;
   procedure Release;
private
   Owned : Boolean := False;
end Mutex;
end Ada_mutex;
--------------------------------
package body Ada_Mutex is
   protected body Mutex is
      entry Seize when not Owned is
      begin
         Owned := True;
      end Seize;
      procedure Release is
      begin
         Owned := False;
      end Release;
   end Mutex;
end Ada_Mutex;

Running time of the code that using the pthread mutex is (in Intel NUC i7):

$ time ./test

 100000000

 real   0m0.557s
 user   0m0.553s
 sys    0m0.005s

And the code that uses protected type and entry: $ time ./test1

  100000000

  real  0m19.009s
  user  0m19.005s
  sys   0m0.005s

With no optimization (-O0) times are:

real    0m0.746s
user    0m0.746s
sys     0m0.000s

and

real    0m20.173s
user    0m20.172s
sys     0m0.000s

For pthread and protected type+entry respectively.

Note that the user time ~= real time, which means the processor was busy (it did not idle, or otherwise yield control)

5
  • 2
    Please edit your question to include a minimal reproducible example that illustrates your finding.
    – trashgod
    Oct 2, 2019 at 3:00
  • 5
    It is erroneous to assume that a protected entry is only locking and unlocking shared data. The protected entry enqueues and dequeues tasks in the entry queue and contains the equivalent of a C condition variable. C mutex implementations do not support a thread queuing policy and therefore do not guarantee deterministic handling of the shared data.
    – Jim Rogers
    Oct 2, 2019 at 5:49
  • as @trashgod pointed out, it would be better to provide full code, production tools, versions and parameters so anyone can produce both programs on their own. Oct 2, 2019 at 8:33
  • @JimRogers I didn't know that - thank you! If I'm not mistaken, queuing will prevent starvation, but to guarantee deterministic handling, we need to guarantee the queuing order. Oct 2, 2019 at 22:15
  • 2
    Ada provides the ability to specify queuing policies. The default queuing policy is FIFO. All Ada compilers are required to support a FIFO queuing policy and a Priority queuing policy. In this manner the handling is deterministic. See ada-auth.org/standards/12rm/html/RM-D-4.html for details.
    – Jim Rogers
    Oct 2, 2019 at 22:52

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Browse other questions tagged or ask your own question.