Function calls take longer and longer to complete over time.

The MagickWand interface is a new high-level C API interface to ImageMagick core methods. We discourage the use of the core methods and encourage the use of this API instead. Post MagickWand questions, bug reports, and suggestions to this forum.
Locked
chu_bun
Posts: 4
Joined: 2020-03-03T14:58:22-07:00
Authentication code: 1152

Function calls take longer and longer to complete over time.

Post by chu_bun »

We have an app using libgphoto2 and libmagickwand on Raspberry Pi. The app captures a preview from a camera with gphoto2 and uses magickwand to draw a circle at the center of the preview. The app starts up fine. But over time, calls to magickwand often take longer and longer to complete.
The code in question look like the following (irrelevant lines removed):

Code: Select all

while(1)
{
    retval = gp_camera_capture_preview(m_camera, m_pCamFile, m_context);  // (1) gphoto2 call to get a preview image from a camera.
    gp_file_get_data_and_size(m_pCamFile, &pData, (unsigned long *) &dataSize); // (2) gphoto2 call to copy the image (jpeg file) to a buffer.

    MagickReadImageBlob(m_magickWand, pData, dataSize); // (3) magickwand call to load the in-memory file to a buffer.
    MagickDrawImage(m_magickWand, m_drawWand); // (4) m_drawWand hold a simple shape such as a circle.
    pModData = MagickGetImageBlob(m_magickWand, &dataSize); // (5) magickwand call to get the preview+circle to a buffer

    // memcpy pModData to a shared memory. 
    MagickRelinquishMemory(pModData);  // (6)
}
Below is the timing in milliseconds at the beginning (measured using boost::chrono::high_resolution_clock): (1), (2), ... corresponding to // (1), // (2), ... in the code.

Code: Select all

Duration Capture: 84. (1)
 Duration Getdata: 0. (2)
 Duration ReadImageBlob: 73. (3)
 Duration Draw: 16. (4)
(forgot to print (5))
 Duration copy and cleanup: 0. (6)
The timing will be like this for about 10-15 seconds. Then some functions start to takes a very long time to complete. For example:

Code: Select all

 Duration Capture: 42.
 Duration Getdata: 0.
 Duration ReadImageBlob: 2562. <=====
 Duration Draw: 65.
 Duration copy and cleanup: 0.
This does not happen every cycle, but regularly enough to make the code unusable. The delay is limited to some magickwand functions (read blob, scale, sample, ...). None of the gphoto2 functions show any changes in the processing time. We use "top" to monitor CPU usage, and during these delays the usage jumps from 30+% to 90+%.

Any guess on what cause this delay and how to fix it? By the way, the problem happens only on a Raspberry Pi (limited resources), and not on a regular desktop running Linux.

User avatar
magick
Site Admin
Posts: 11254
Joined: 2003-05-31T11:32:55-07:00

Re: Function calls take longer and longer to complete over time.

Post by magick »

See https://github.com/gphoto/libgphoto2/issues/278. Do you need to free the gphoto2 buffer?

snibgo
Posts: 13034
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Function calls take longer and longer to complete over time.

Post by snibgo »

Does your memory usage increase at each iteration? MagickReadImageBlob() allocates memory for the image in the wand. You don't show code that frees this image.
snibgo's IM pages: im.snibgo.com

chu_bun
Posts: 4
Joined: 2020-03-03T14:58:22-07:00
Authentication code: 1152

Re: Function calls take longer and longer to complete over time.

Post by chu_bun »

magick wrote:
2020-03-03T17:12:40-07:00
See https://github.com/gphoto/libgphoto2/issues/278. Do you need to free the gphoto2 buffer?
I didn't free the buffer, because the document seems to imply it will be managed by the CamFile object. Will try free() to see what happens.

chu_bun
Posts: 4
Joined: 2020-03-03T14:58:22-07:00
Authentication code: 1152

Re: Function calls take longer and longer to complete over time.

Post by chu_bun »

snibgo wrote:
2020-03-03T17:18:24-07:00
Does your memory usage increase at each iteration? MagickReadImageBlob() allocates memory for the image in the wand. You don't show code that frees this image.
So I should call ClearMagickWand on each iteration? By the way, "top" does not show memory usage increase, just spikes in CPU usage once in a while.

chu_bun
Posts: 4
Joined: 2020-03-03T14:58:22-07:00
Authentication code: 1152

Re: Function calls take longer and longer to complete over time.

Post by chu_bun »

ClearMagickWand fixes the problem. Thanks.

Regarding freeing the gphoto2 buffer, it is declared const and shows a compiler warning/error when freed. Since the above works, we didn't inspect this further.

Locked