3

I'm having trouble to create timer under my embedded Linux running ARM. I'm using a home made C++ library to manage timer. I didn't code it myself, I don't know deeply the implementation despite I have access to the source code... It works for a while and then I got the error "EAGAIN".

Using strace I noticed that when it doesn't work the timer ID is quiet high!

timer_create(CLOCK_MONOTONIC, {0, SIGRT_3, SIGEV_SIGNAL, {...}}, 0xbed50af4) = -1 EAGAIN (Resource temporarily unavailable)

See the pretty low timer ID when it's working:

timer_create(CLOCK_MONOTONIC, {0x3, SIGRT_3, SIGEV_SIGNAL, {...}}, {0x3d}) = 0

I thought that the number of timers was unlimited! Actually not? Should we destroy the timer once we are done with it? I also used the "timer_stats" kernel utility but this didn't help me much ... Are there other debug utility for the timers inside the kernel or any other tool?

Thanks for your help!

morandg
  • 1,066
  • 3
  • 14
  • 30
  • Can you not simple handle the `EAGAIN` error and repeat to create your timer? Or does the `EAGAIN` error apear after some time on every call? – rekire Nov 18 '11 at 08:11
  • 1
    I really don't know much about your specific timer issues, but destroying something when you don't longer need it definitely seems to be a good idea. – ereOn Nov 18 '11 at 08:11
  • It always make the EAGAIN error ... – morandg Nov 18 '11 at 08:30
  • "Should we destroy the timer once we are done with it" - duh? – user253751 Jan 18 '16 at 20:54
  • In order to deallocate resources and don't bump into the maximum created timer amount, of course we shall. This is not only valid for timer but any kind of resource allocated (file descriptors, mutexes, thread, ...). However, this also depends on your design and the context, if you plan to reuse the timer, keeping the resources might be slightly faster at next use. See the accepted answer from sarnold "I suggest modifying your application to delete or re-use old timers." ... – morandg Jan 19 '16 at 15:11

1 Answers1

6

You've guessed correctly, you do have a maximum number of timers:

   The kernel preallocates a "queued real-time signal" for each
   timer created using timer_create().  Consequently, the number
   of timers is limited by the RLIMIT_SIGPENDING resource limit
   (see setrlimit(2)).

The timer_create(3posix) manpage is a bit more blunt about it:

   The timer_create() function shall fail if:

   EAGAIN The system lacks sufficient signal queuing resources
          to honor the request.

   EAGAIN The calling process has already created all of the
          timers it is allowed by this implementation.

While you could raise the setrlimit(2) limit on pending signals (ulimit -i in bash(1)), be aware that this allocates real kernel memory -- which is an extremely limited resource.

I suggest modifying your application to delete or re-use old timers.

sarnold
  • 102,305
  • 22
  • 181
  • 238
  • Thanks for your clever answer! I think our applications will need some refactoring and timer destroying... As said ereOn, destroying ressources that you no longer needed is always a good idea! Thank you all! – morandg Nov 18 '11 at 08:30